Use For Loops in a ShaderFX network

Using a Custom Code node

You can create a for loop in different ways. You can create a Custom Code node. Select Settings > Toggle Advanced Mode from the ShaderFX menu bar to enter advanced mode, then select Hw Shader Nodes > Various > Custom Code. Press Edit in the Attribute Editor to enter your code in the Custom Code editor that appears. Use functions that run in both DirectX and OpenGL if you plan to use both modes.

Using a For Loop node

You must also be in advanced mode in order to create a For Loop node.

The following example demonstrates how to use a for loop to sample a texture three times with different UV offsets. The sampled values are added; the total divided by three, and the average value applied to the Diffuse Color channel of the TraditionalGameSurfaceShader.

Setting up the for loop

  1. Select Hw Shader Nodes > Flow Control > For Loop to create a For Loop node.
    Tip: A square port signifies that it can accept more than one input.
  2. Select Hw Shader Nodes > Values > Int to create an Integer node. Set its value to 3 in the Attribute Editor and connect it to the B attribute of the For Loop node.
    • The A attribute represents the start of the loop.
    • The B attribute represents the end of the loop.
    • The C attribute represents the interval of the loop.
    • By default, the value of A is 0 and the value of C is 1. If you use these default values for your loop, you do not need to connect these attributes.

      In this example, we set for i = 0, i < 3, i++; therefore, we do not need to explicitly set the A and C values.

  3. Select Hw Shader Nodes > Various > String to create an iterator. In its Attribute Editor, select i from the Default options drop-down list, and connect it to the Iterator attribute of the For Loop node.
  4. Select Hw Shader Nodes > Values > Float3 to create a variable to store the values that are calculated within the loop.
    Tip: Use Float3 or Float4 to store the values. Do not use Color.
  5. In the Float3 Attribute Editor, rename the node to Loop Result and initialize the values to 1.
  6. Connect the float3 attribute to the Variables attribute of the For Loop node. Because this is a simple loop where only one variable is used, connect float3 to the Output attribute of the For Loop as well.

Setting up the texture sampling

  1. Select Hw Shader Nodes > Textures > Texture Map to create a Texture Map node. Connect your texture to the MyTexture/Path attribute in the Attribute Editor.
  2. Select Hw Shader Nodes > Values > Array to create an Array node. The three set of offset values are stored in an array.
  3. Select Hw Shader Nodes > Values > Float2 three times to create three Float2 nodes for three sets of offset values. For each Float2 node, set its attributes in the Attribute Editor as follows:
    • Name: Offset1, X: 0.1, Y: 0.3
    • Name: Offset2, X: 0.2, Y: -0.4
    • Name: Offset3, X: 0.3, Y: 0.2
  4. Connect all three Float2 nodes to the Values attribute of the Array node.
  5. Select the Texture Map node. In its Attribute Editor, ensure that the Sampler / U Coordinate and Sampling / V Coordinate attributes are set to WRAP.

    Because the texture is sampled with an offset, the sampling may exceed the 0-1 UV range. Setting this attribute to WRAP ensures that pixels are sampled from the beginning of the texture when the UV range exceeds 1.0.

  6. Select Hw Shader Nodes > Inputs Common > UV Set and Hw Shader Nodes > Math > Add to create a UV Set node and an Add node, respectively.
  7. Connect the nodes as follows:
    • Connect the Value attribute of Array to the Value attribute of Add.
    • Connect the UV attribute of UV set to the other Value attribute of Add.
    • Connect the Result attribute of Add to UV attribute of Texture Map.
    • Connect the Output attribute of the iterator (String node) to the Index attribute of Array. This allows the For Loop to obtain the offset values.
    • Connect the Color attribute of Texture Map to the Calculations attribute of For Loop. This sets the calculations in the for loop, which samples the texture map three times.

Calculate the average of the sampled results

  1. Select Hw Shader Nodes > Various > String to create a String node, then select += from the Default options drop-down list.
  2. Connect the Output value of String to the Operators attribute of the For Loop node. The For Loop adds the results of the texture sampling to the Loop Result variable.
  3. Select Hw Shader Nodes > Math > Divide to create a Divide node. Connect the Output attribute of For Loop to the first Value attribute of Divide. Because Loop Result is the sum of three sampled values, it must be divided by three to obtain its average.
  4. Select Hw Shader Nodes > Values > Float to create a Float node and set its value to 3 in the Attribute Editor. Connect its float attribute to the other Value attribute of Divide to divide the sum of the sampled results by three.
  5. Connect the Result attribute of Divide to the Diffuse Color attribute of TraditionalGamesSurfaceShader.

You can export your ShaderFX material to a shader file and examine the shader code to see the for loop you just created:

float3 LoopResult = float3(1.0, 1.0, 1.0);	
for(int i=0; i<3; i+=1)	
{		
    float2 AddOp = (Array_bphdfiwhhj[i] + IN.map1.xy);		
    float4 Sampler = numberTestTexture.Sample(MMMLWWWSampler, AddOp);		
    LoopResult += Sampler.xyz;
}

For more information about exporting to a shader file, see Export ShaderFX materials to HLSL, CgFX and GLSL.