Shader.AddSharedTextureLayer
 
 
 

Shader.AddSharedTextureLayer

Introduced

v4.0

Description

Adds an existing texture layer to the container. The layer already belongs to at least one other texture layer container (either Shader or Material).

A texture layer is an object which simplifies layering texture effects on top of shaders and materials. Each layer has a set of properties which describe its characteristics (such as color, mixing mode, weight) to specify how it will affect the shader ports it drives. The order that layers appear under a container indicates the order the layering is applied, similar to the way a "mix N colors" shader node works. Texture layer port objects (see TextureLayer.AddTextureLayerPort) are owned by layers, and are used to specify which shader ports the layer should affect.

Tip: A Material can also be a texture layer container, and has the same set of related methods.

C# Syntax

Shader.AddSharedTextureLayer( Object in_varLayerToAdd, Boolean in_bAfter, Object in_varRefLayer );

Scripting Syntax

Shader.AddSharedTextureLayer( Layer, [After], [Reference] );

Parameters

Parameter Type Description
Layer TextureLayer or String Texture layer to add to this container.
After Boolean True to insert the new texture layer after the reference layer. False to insert it before. If no reference layer, then True will add at the end of the container's list, False will add it at the start.

Default Value: True

Reference TextureLayer or String Reference texture layer, to indicate where the newly-added layer should be located in the stack. The layer will be inserted adjacent to (before or after) the reference layer. If not specified, then the new layer will be added at the beginning or the end, depending on the value of the following argument.

Examples

1. JScript Example

/*
        This JScript example shows creating and sharing 
        texture layers.  It also shows how both Shaders and
        Materials can be texture layer containers, and the same
        layer can drive ports on each of them (if desired).
*/
oRoot = ActiveProject.ActiveScene.Root;
oSph = oRoot.AddGeometry( "Sphere", "MeshSurface" );
oMat = oSph.AddMaterial( "Phong" );
oPhong = oMat.Shaders(0);
// Add a layer to the Phong.
oShared = oPhong.CreateTextureLayer( "SharedLayer" );
// Add an unshared layer to the Material.
oRough = oMat.CreateTextureLayer( "Rough" );
// Add the first layer to the material, thereby sharing it.
// We'll put it before the Rough layer.
oMat.AddSharedTextureLayer( oShared, false, oRough );
// Now for fun, let's create a Fractal, attach it to the
// color input of the shared layer, and make it drive
// both the Phong's diffuse and the Material's displacement.
oParam = Application.Dictionary.GetObject( oShared + ".Color" );
oFractal = oParam.ConnectFromPreset( "Fractal", siTextureShaderFamily );
oDiffuse = oShared.AddTextureLayerPort( oPhong.Parameters( "diffuse" ) );
oDispl = oShared.AddTextureLayerPort( oMat.Parameters( "displacement" ) );
// Now let's explore what we have created...
DumpTextureLayerContainer( oMat );
function DumpTextureLayerContainer( in_cont )
{
        Application.LogMessage( "CONTAINER: " + in_cont.Name );
        for ( var i = 0; i < in_cont.TextureLayers.Count; i++ )
        {
                oTextureLayer = in_cont.TextureLayers(i);
                DumpTextureLayer( oTextureLayer, i+1 );
        }
}
function DumpTextureLayer( in_layer, in_index )
{
        Application.LogMessage( "   Layer " + in_index + ": " + in_layer.Name );
        oColor = in_layer.Parameters("Color");
        var empty;
        if ( oColor == empty )
        {
                Application.LogMessage( "     Color: (" + in_layer.Red.Value +
                                                "," + in_layer.Green.Value +
                                                "," + in_layer.Blue.Value + ")" );
        }
        else
        {
                oColorSrc = in_layer.Color.Source;
                Application.LogMessage( "     Color driven by: " + oColorSrc.Name );
        }
        oPorts = in_layer.TextureLayerPorts;
        count = oPorts.Count;
        Application.LogMessage( "     Layer drives:" );
        if ( count == 0 )
                Application.LogMessage( "       <nothing>" );
        else
        {
                for ( i = 0; i < count; i++ )
                {
                        Application.LogMessage( "       " + oPorts(i).Target.FullName );
                }
        }
}
// This example should log something like:
//INFO : CONTAINER: Material
//INFO :    Layer 1: SharedLayer
//INFO :      Color driven by: Fractal
//INFO :      Layer drives:
//INFO :        Sources.Materials.DefaultLib.Material.Phong.diffuse
//INFO :        Sources.Materials.DefaultLib.Material.displacement
//INFO :    Layer 2: Rough
//INFO :      Color: (0.699999988079071,0.699999988079071,0.699999988079071)
//INFO :      Layer drives:
//INFO :        <nothing>

2. VBScript Example

'
' This VBScript example shows creating and sharing 
' texture layers.  It also shows how both Shaders and
' Materials can be texture layer containers, and the same
' layer can drive ports on each of them (if desired).
'
set oRoot = ActiveProject.ActiveScene.Root
set oSph = oRoot.AddGeometry( "Sphere", "MeshSurface" )
set oMat = oSph.AddMaterial( "Phong" )
set oPhong = oMat.Shaders(0)
' Add a layer to the Phong.
set oShared = oPhong.CreateTextureLayer( "SharedLayer" )
' Add an unshared layer to the Material.
set oRough = oMat.CreateTextureLayer( "Rough" )
' Add the first layer to the material, thereby sharing it.
' We'll put it before the Rough layer.
oMat.AddSharedTextureLayer oShared, False, oRough
' Now for fun, let's create a Fractal, attach it to the
' color input of the shared layer, and make it drive
' both the Phong's diffuse and the Material's displacement.
set oParam = Dictionary.GetObject( oShared & ".Color" )
set oFractal = oParam.ConnectFromPreset( "Fractal", siTextureShaderFamily )
set oDiffuse = oShared.AddTextureLayerPort( oPhong.Parameters( "diffuse" ) )
set oDispl = oShared.AddTextureLayerPort( oMat.Parameters( "displacement" ) )
' Now let's explore what we have created...
DumpTextureLayerContainer oMat
sub DumpTextureLayerContainer( in_cont )
        LogMessage "CONTAINER: " & in_cont.Name
        i = 1
        for each oTextureLayer in in_cont.TextureLayers
                DumpTextureLayer oTextureLayer, i
                i = i + 1
        next
end sub
sub DumpTextureLayer( in_layer, in_index )
        LogMessage "   Layer " & in_index & ": " & in_layer.Name
        set oColor = in_layer.Parameters("Color")
        if TypeName( oColor ) = "Nothing" then
                LogMessage "     Color: (" & in_layer.Red.Value & _
                                                "," & in_layer.Green.Value & _
                                                "," & in_layer.Blue.Value & ")"
        else
                set oColorSrc = in_layer.Color.Source
                LogMessage "     Color driven by: " & oColorSrc.Name
        end if
        set oPorts = in_layer.TextureLayerPorts
        count = oPorts.Count
        if count = 0 then
                LogMessage "     <layer driving no ports>"
        else
                LogMessage "     Layer drives:"
                for i = 0 to count - 1
                        LogMessage "       " & oPorts(i).Target.FullName
                next                            
        end if
end sub
' This example should log something like:
'INFO : CONTAINER: Material
'INFO :    Layer 1: SharedLayer
'INFO :      Color driven by: Fractal
'INFO :      Layer drives:
'INFO :        sphere.Material.Phong.diffuse
'INFO :        sphere.Material.Color2scalar.input
'INFO :    Layer 2: Rough
'INFO :      Color: (0.7,0.7,0.7)
'INFO :      <layer driving no ports>

See Also

AddTextureLayer Shader.CreateTextureLayer Material.CreateTextureLayer Material.AddSharedTextureLayer