/*
This example illustrates how to set instance values on a shared material parameter
Applies to parameters: ImageShader.tspace_id, Material.ImageClipName, Material.UV, Material.CAV.
*/
NewScene( null, false );
var app = Application;
var scene = app.ActiveProject.ActiveScene;
var root = scene.Root;
// Create cube with a texture projection (not connected)
var cube = root.AddGeometry( "Cube", "MeshSurface", "MyCubeWithTexture" );
// Create projection for cube
CreateProjection(
cube,
siTxtCubic,
siTxtDefaultCubic ,
"MyCubicTextureSupport",
"MyCubicTextureProjection" );
// Create sphere with a texture projection (not connected)
var sphere = root.AddGeometry( "Sphere", "MeshSurface", "MySphereWithTexture" );
// Create projection for sphere
CreateProjection(
sphere,
siTxtSpherical,
siTxtDefaultSpherical,
"MySphericalTextureSupport",
"MySphericalTextureProjection" );
// Create a group with a material hooked up to texture image
var objs = XSIFactory.CreateObject( "XSI.Collection" );
objs.Add( cube );
objs.Add( sphere );
var group = root.AddGroup( objs, "MyGroup", false );
var mat = group.AddMaterial( "Blinn", false, "MyGroupMaterialWithTexture" );
var shaders = mat.Shaders;
var blinn = shaders(0);
var ambient = blinn.Parameters("ambient");
var diffuse = blinn.Parameters("diffuse");
// Create image clip
var strFileName = app.InstallationPath( siFactoryPath ) + "\\Data\\XSI_SAMPLES\\Pictures\\xsilogo.jpg";
var imageclip = CreateImageClip( strFileName, "MyImageClip" );
// Connect imageshader1 to blinn.ambient
var imageshader1 = ambient.ConnectFromPreset( "Image", siTextureShaderFamily );
// Connect imageshader2 to blinn.diffuse
var imageshader2 = diffuse.ConnectFromPreset( "Image", siTextureShaderFamily );
// Connect the imageshader1.tex to imageclip
var tex = imageshader1.Parameters("tex");
tex.Connect( imageclip );
// get tspace_id from imageshader
var tspace_id = imageshader1.Parameters("tspace_id");
// set the cube.material.image.tspace_id = MyCubicTextureProjection
tspace_id.SetInstanceValue( cube, "MyCubicTextureProjection" );
// set the sphere.material.image.tspace_id = MySphericalTextureProjection
tspace_id.SetInstanceValue( sphere, "MySphericalTextureProjection" );
// Dump all shader parameter instance values for each objects found using
// a material from the active material library
var matlib = scene.ActiveMaterialLibrary;
for ( var i=0; i<matlib.Items.Count; i++ ) {
var mat = matlib.Items(i);
// Get all shaders in materials shader tree
var shaders = mat.FindShaders( siShaderFilter );
for ( j=0; j<shaders.Count; j++ ) {
var shader = shaders(j);
for ( var k=0; k<shader.Parameters.Count; k++ ) {
var shader_param = shader.Parameters(k);
// If the shader parameter has an instance value look up
// its object value
if ( shader_param.HasInstanceValue ) {
var eObjs = new Enumerator( mat.UsedBy );
for ( ; !eObjs.atEnd(); eObjs.moveNext() ) {
var obj = eObjs.item();
var instancevalue = shader_param.GetInstanceValue( obj );
// Log parameter instance value for object
app.LogMessage( obj.name + " " + shader.Name + " " + shader_param.Name + " = " + instancevalue );
}
}
}
}
}
// Expected results:
//INFO : MyCubeWithTexture Image tspace_id = MyCubicTextureProjection
//INFO : MySphereWithTexture Image tspace_id = MySphericalTextureProjection
//INFO : MyCubeWithTexture Image1 tspace_id =
//INFO : MySphereWithTexture Image1 tspace_id =
|