## Shader

Object Hierarchy | Related C++ Class: Shader

### Inheritance

SIObject

ProjectItem

Shader

### Introduced

v1.5

### Description

The Shader object represents a node in the render tree of an object. The initial Shader of a render tree can be created with SceneItem.AddMaterial and additional shaders can be created and connected together with Parameter.ConnectFromPreset and Parameter.Connect.
A Shader is a type of DataSource.

### Methods

AddCustomOp | AddICEAttribute | AddProperty | AddScriptedOp
AddScriptedOpFromFile | AddSharedTextureLayer | AnimatedParameters2 | BelongsTo
CodePath | CodeText | CreateTextureLayer | EvaluateAt
GetAllShaders | GetICEAttributeFromName | GetPortDisplayName | GetShaderContainer
GetShaderInputType | GetShaderParameterTargets | GetShaderParameterType | HasRenderer
IsA | IsAnimated2 | IsClassOf | IsEqualTo
IsKindOf | IsLocked | IsSelected | LockOwners
PutPortDisplayName | RemoveICEAttribute | RemoveTextureLayer | RendererOptionValue
SetAsSelected | SetCapabilityFlag | SetLock | SymbolName
TaggedParameters | UnSetLock |  |

### Properties

AllImageClips | Application | BranchFlag | Capabilities
Categories | EvaluationID | Families | FullName
Help | HierarchicalEvaluationID | ICEAttributes | ImageClips
LockLevel | LockMasters | LockType | Model
Name | NestedObjects | ObjectID | Origin
OriginPath | OutputType | Owners | PPGLayout
Parameters | Parent | Parent3DObject | ProgID
Properties | Root | Selected | ShaderDef
ShaderType | Shaders | TextureLayers | Type

### Examples

#### 1. VBScript Example

    '
    '       This example shows how to access the shader on an object
    '
    NewScene , false
    dim root, grid
    set root = ActiveProject.ActiveScene.Root
    set grid = root.AddGeometry("Grid", "MeshSurface")
    'Create a Lambert shader
    grid.AddMaterial "Lambert"
    'Access the new Shader
    LogMessage "Shader name:" & grid.Material.Shaders(0).Name

#### 2. VBScript Example

    '
    '       This example shows how to recursively search a render tree
    '
    '       It shows how to access Shaders via the Source property of a Parameter
    '       It also shows how to use XSICollection object to accumulate a list of objects
    '
    '       Note: to find ImageClips the Material.ImageClips property can be used rather
    '       a recursive scan like this.  And the FindObjects command can be used to find
    '       all shaders of a particular type.
    '
    set oObj = BuildDemoScene                       ' Create a little sample render tree
    FindShaders(oObj)                                       ' Search the scene to find and log shaders
    ' ---------------------------------------------------
    ' Expected results:
    ' INFO : Skipping Sources.Materials.DefaultLib.Material.Phong
    ' INFO : SUMMARY: Searched 2 shader(s)
    ' INFO : SHADERS SEARCHED:
    ' INFO : Sources.Materials.DefaultLib.Material.Phong
    ' INFO : Sources.Materials.DefaultLib.Material.Lambert
    ' ---------------------------------------------------
    ' This is the actual code doing the searching
    sub FindShaders( in_oObj )
            dim oShaderList, oImageClipList
            set oShaderList = CreateObject("XSI.Collection")
            oShaderList.Unique = true
            ' We expect the input object to be a "SceneItem" object
            set oMat = in_oObj.Material
            ' Although not actually a shader, we start searching
            ' for shaders from the parameters of the material
            SearchShader oMat, oShaderList
            Application.LogMessage "SUMMARY: Searched " & oShaderList.Count & " shader(s)"
            if ( oShaderList.Count > 0 ) then
                    Application.LogMessage "SHADERS SEARCHED:"
                    for each oShader in oShaderList
                            Application.LogMessage oShader.Fullname
                    next
            end if
    end sub
    ' Recursively search any connected shaders
    ' Each shader is visited only once
    sub SearchShader( in_oShader, io_oVisitedShaderList)
            for each oParam in in_oShader.Parameters
                    if typename( oParam.Source ) = "Shader" then
                            if ( NOT IsShaderInList(oParam.Source, io_oVisitedShaderList) ) then
                                    io_oVisitedShaderList.Add(oParam.Source)
                                    ' Recursively search this shader
                                    SearchShader oParam.Source, io_oVisitedShaderList
                            end if
                    end if
            next
    end sub
    ' Determines if a shader is already in an XSICollection
    function IsShaderInList( oShader, io_oVisitedShaderList )
            for each oVisitedShader in io_oVisitedShaderList
                    if (oVisitedShader.FullName = oShader.FullName) then
                            Application.LogMessage "Skipping " & oShader.FullName
                            IsShaderInList = true
                            exit function
                    end if
            next
            IsShaderInList = false
    end function
    ' ---------------------------------------------------
    ' Create a little sample render tree
    ' It has no interesting visual appearance but is a render tree with
    ' various shaders and two images so that we can demonstrate the
    ' FindImageClips routine
    '
    ' Return value is the X3DObject
    function BuildDemoScene
            ImageFile1 = XSIUtils.BuildPath(_
                    Application.InstallationPath(siFactoryPath), _
                    "Data", "XSI_SAMPLES", "Pictures", "jaiqua_face.jpg" )
            ImageFile2 = XSIUtils.BuildPath(_
                    Application.InstallationPath(siFactoryPath), _
                    "Data", "XSI_SAMPLES", "Pictures", "ehair_08.jpg" )
            NewScene , false
            set oObj = ActiveSceneRoot.AddGeometry("Sphere", "MeshSurface")
            oObj.AddMaterial "Phong"
            dim oPhongShader, oAmbientParam, oDiffuseParam, oShinyParam
            set oPhongShader = oObj.Material.Shaders(0)
            set oAmbientParam = oPhongShader.Parameters("ambient")
            set oDiffuseParam = oPhongShader.Parameters("diffuse")
            set oShinyParam = oPhongShader.Parameters("shiny")
            dim oImageClip1, oImageClip2
            SICreateImageClip ImageFile1, ,oImageClip1
            SICreateImageClip ImageFile2, ,oImageClip2
            dim oImageNode1,oImageNode2
            set oImageNode1 = oAmbientParam.connectfrompreset("Image", siTextureShaderFamily)
            oDiffuseParam.Connect(oImageNode1)
            oImageNode1.Parameters("tex").Connect(oImageClip1)
            set oImageNode2 = oShinyParam.connectfrompreset("Image", siTextureShaderFamily)
            oImageNode2.Parameters("tex").Connect(oImageClip2)
            ' Commands can also be used to build a render tree
            CreateShaderFromPreset "Shaders\Material\Lambert.Preset", "Sources.Materials.DefaultLib.Material"
            SIConnectShaderToCnxPoint "Sources.Materials.DefaultLib.Material.Lambert", oObj.Material & ".Photon"
            ' Return the sphere
            set BuildDemoScene = oObj
    end function

#### 3. JScript Example

    /*
            This example lists all installed shaders with their ProgId and OutputType,
            plus all texturable parameters and their shader input type.
    */
    var re = / /g;
    var strShaderNames = Dictionary.info("",siShaderFamily).replace(re,"");
    var aShaders = strShaderNames.split(",");
    XSIUtils.QuickSort( aShaders );
    var colitem = XSIFactory.CreateObject("XSI.CollectionItem");
    var cShaders = 0;
    for ( var i = 0; i < aShaders.length; i++ )
    {
            var shader = null;
            var progid = "Softimage." + aShaders[i];
            if ( progid == "Softimage.TraversalCallback" ) continue;
            try {
                    shader = XSIFactory.CreateObject( progid );
            } catch (e) {
                    logmessage( "Error: can't create shader : " + progid );
                    continue;
            }
            cShaders++;
            logmessage( progid + " " + ShaderParameterTypeAsText(shader.OutputType) );
            var params = shader.parameters;
            for ( var j = 0; j < params.count; j++ )
            {
                    var param = shader.parameters(j);
                    if ( param.capabilities & siTexturable )
                            logmessage( "\t" + param.name + " " + ShaderParameterTypeAsText(shader.GetShaderInputType(param.scriptname)));
            }
    }
    logmessage( "Shaders found = " + cShaders );
    function ShaderParameterTypeAsText(type)
    {
            switch (type)
            {
                    case siUnknownParameterType : return "siUnknownParameterType";
                    case siBooleanParameterType : return "siBooleanParameterType";
                    case siColorParameterType : return "siColorParameterType";
                    case siDataParameterType : return "siDataParameterType";
                    case siIntegerParameterType : return "siIntegerParameterType";
                    case siLensParameterType : return "siLensParameterType";
                    case siLightParameterType : return "siLightParameterType";
                    case siMaterialParameterType : return "siMaterialParameterType";
                    case siMatrixParameterType : return "siMatrixParameterType";
                    case siModelParameterType : return "siModelParameterType";
                    case siRealTimeParameterType : return "siRealTimeParameterType";
                    case siReferenceParameterType : return "siReferenceParameterType";
                    case siScalarParameterType : return "siScalarParameterType";
                    case siShaderParameterType : return "siShaderParameterType";
                    case siStringParameterType : return "siStringParameterType";
                    case siStructParameterType : return "siStructParameterType";
                    case siTextureParameterType : return "siTextureParameterType";
                    case siTextureSpaceParameterType : return "siTextureSpaceParameterType";
                    case siVectorParameterType : return "siVectorParameterType";
                    default: return type;
            }
    }
    //

### See Also

Material.Shaders Light.Shaders Camera.Shaders ImageClip Parameter.Source Texture TextureLayer