Effects Shaders (.cgfx Files)

You can set up Cg effects in Softimage using the programmable CgFX realtime shader. The CgFX shader provides support for nVIDIA’s CgFX file format, Microsoft’s FX file format, and COLLADA’s FX file format with the Cg profile.

The CgFX shader supports external file referencing as well as inlined code compilation for rapid prototyping. You can load and display .cgfx files in 3D views using the OpenGL display mode.

Setting Up a CgFX Shader

Although you can author .cgfx files directly in Softimage, this procedure assumes that you have a valid .cgfx file that you want to apply to an object and display in a 3D view.

To set up a CgFX effect on an object

1. Set any viewport to the Realtime Shaders > OpenGL display mode.

2. Select the object to which you want to apply the effect.

3. If your effect requires:

- Texture coordinates: Create texture coordinates by choosing Get > Property > Texture Projection > [type of projection].

- Normals: Normal maps are special texture maps that encode the normal (surface direction) per pixel on a geometry. When generating a normal map for use with realtime shaders, you should create the normal map in tangent space (relative to UV basis). You can use RenderMap, RenderVertex, or Ultimapper to create normal maps in Softimage.

- Tangents and Binormals: Tangents and binormals are necessary for normal mapping and other effects. Generate tangents or binormals for the selected object by choosing Get > Property > Tangent or Get > Property > Binormal. See Applying Tangent and Binormal Properties [Texturing].

4. With the object still selected, open the render tree (press 7).

5. From the preset manager, drag and drop the Realtime > Cg > Cg FX node to the render tree workspace.

6. Connect the CgFx node to the Material node’s RealTime input.

7. Double-click the CgFX node to open its property editor.

8. Click the CgFX tab and select the Use External CgFX file checkbox.

9. In the CgFX File text box, enter the name and path for the .cgfx file you wish to use. Alternatively, you can click the browse button (…) to select the file from a browser.

The CgFX shader accepts a relative path to the effects file. The path should be relative to the root of the current project. This allows you to transfer your project to another path or machine without having to re-associate your referenced file to the new scene location.

10. If your .cgfx file is valid, your effect is now displayed in the OpenGL view with it's specified default values.

11. When the effect is compiled, the following things happen:

• Vertex shader variables bound to specific input semantics dynamically generate a user interface. You can use the vertex attribute widget to customize the per-vertex attributes that are passed to the effect. See Vertex Attribute Mapping (.cgfx).

• Effects parameters dynamically generate a user interface.

- A custom property set, called CgFX_Parameters, is appended to the CgFX shader’s property editor. The property set allows you to tune your tweakable effects parameters and select from the techniques defined within your code. See Creating User Interfaces for Cg Effects Parameters.

• Textures are dynamically created.

- The texture nodes required by your effect are automatically created and connected by the CgFX shader. You should open the texture shader nodes and make sure that each one is using the correct texture projection.

- If you defined a ResourceType for 1D, 2D, 3D, or CUBE textures, they will appear on the Images tab. See Widget for Textures and Images.

Vertex Attribute Mapping (.cgfx)

Variables declared as input to a vertex shader are often bound to predefined names (POSITION, COLOR0, COLOR1, TEXCOORD0, etc.) that are referred to as binding semantics. These predefined names implicitly specify the mapping of the inputs to particular hardware registers. Depending on the semantics that you use, either Softimage or your vertex program must supply the data for each of these variables.

A vertex attribute widget is created in the property editor for each vertex shader variable that is bound to semantics such as COLOR[n], TEXCOORD[n], TANGENT, and BINORMAL. A vertex attribute widget is not created for all binding semantics. If, for example, a variable is bound to the POSITION or NORMAL semantic, it will not generate a vertex attribute widget because these semantics are automatically bound by Softimage. For a list of the supported semantics, see Vertex Shader Input Semantics [Shader Reference].

Note that per-vertex variable data can be bound to a TEXCOORD semantic even when it is not a texture coordinate. This gives you the freedom to connect a texture projection in a COLOR register or a vertex color in a TEXCOORD register (it’s as you wish).

As an example, the members of the struct (defined below) have semantics specified, which connect the variables in the shader code to the underlying graphics system (OpenGL/DirectX). The input structure identifies the data from the vertex buffer that will provide the shader inputs. This snippet of code maps the position, normal, and texture coordinate data from the vertex buffer into vertex shader registers.

struct appdata {
    float3 Position : POSITION;
    float4 UV : TEXCOORD0;
    float4 Tangent : TEXCOORD1;
    float4 Binormal : TEXCOORD2;
    float4 Normal : NORMAL;
};

The bindings (defined above) dynamically generate the following vertex attribute widgets as displayed in the CgFx property editor:

 

Vertex Attribute Widget

1

The vertex attribute widget is always labeled as IN.VariableName.

2

The vertex attribute widget provides a drop-down list where you can select any 0D component, including any ICE attribute, that exists or can be created on a mesh or point cloud and pass it into the specified vertex input register.

3

The Edit button opens the property editor for the associated attribute so that you can edit its parameters. For example, if you have set tangent data to be passed to the input register, pressing Edit will open the TangentOp2 operator property editor.

4

The New button lets you create a new texture projection, color at vertices, or weight map cluster property on the selected object and sets it as the data to be passed into the specified vertex input register.

Creating User Interfaces for Cg Effects Parameters

Effects parameters defined in the .cgfx file dynamically generate a user interface when loaded into the CgFX shader. Every global variable that is not bound to SAS is automatically displayed in the user interface unless specified otherwise.

User interface widgets are defined in the .cgfx file using annotations. An annotation is a list of variables and values denoted by angle brackets immediately following a declaration. The CgFX file format does not interpret the meaning of annotations in any way. Therefore, annotations make it is possible to attach additional information to parameters in the effect file for use by Softimage.

Specifically, annotations that define the UIWidget variable are used by the CgFX shader to generate an appropriate user interface widget that the artist can use for tweaking parameter values.

Widget for Scalar Values

UIWidget

“Slider”

Example

float SpecularPower
<
   string UIWidget = "Slider";
   string UIName = "Specular Power";
   float UiMin = 1;
   float UiMax = 200;
> = 20.0f;

 

Widget for Color Values

UIWidget

“Color”

float3

Defines sliders for the R,G,B color channels.

float4

Defines sliders for the R,G,B,A color channels.

Example

float3 SpecularColor
<
   string UIWidget = "Color";
   string UIName =  "Specular";
> = {1.0f, 1.0f, 1.0f};

 

Widget for Textures and Images

ResourceType

“1D”; “2D”; “3D”; and “Cube”

Example

texture2D AlbedoMap
<
   string ResourceName = "default_surface_map.png";
   string ResourceType = "2D";
>;

sampler2D AlbedoSampler = sampler_state
{
   Texture = <AlbedoMap>;
   MinFilter = LinearMipMapLinear;
   MagFilter = Linear;
   WrapS = Repeat;
   WrapT = Repeat;
};

 



Autodesk Softimage v.7.5