Home Reference Index Editor Scenery Tab NL2MAT Editor

NL2MAT Programmable Shader Core Language


The programmable shader core can be used to customize how the input textures and color from the RGBGen and AlphaGen units will be combined to produce the pixels being rendered. It is a text based language, made up of statements with input and outputs. All statements operate on 4 components (channels) parallely. Those 4 components typically represent the RGBA channels of a color. E.g. The statement "Result = Add ( Tex0, Tex1); " will represent internaly 4 additions for each component (Red, Green, Blue, Alpha). Each component of the first input (Tex0) is added with the corresponding component of the second input (Tex1) and is written to the corresponding component of the Result register. The pseudo code would be similar to " Result[0] = Tex0[0] + Tex1[0]; Result[1] = Tex0[1] + Tex1[1]; Result[2] = Tex0[2] + Tex1[2]; Result[3] = Tex0[3] + Tex1[3]; ".

There are 3 output registers (Result, Specular, Emission) that can be used to specify the pixel attributes for furter processing.

When lighting modes for RGB generation are used, the final pixel will be computed using the following pseudo code formula:
Red   = Result[0] * DiffuseLighting[0] + Specular[0] * SpecularLighting[0] + Emission[0]
Green = Result[1] * DiffuseLighting[1] + Specular[1] * SpecularLighting[1] + Emission[1]
Blue  = Result[2] * DiffuseLighting[2] + Specular[2] * SpecularLighting[2] + Emission[2]
Alpha = Result[3]

For all other RGB generation modes, the final pixel will be computed using the following pseudo code:
Red   = Result[0] + Emission[0]
Green = Result[1] + Emission[1]
Blue  = Result[2] + Emission[2]
Alpha = Result[3]

Note that Red, Green, Blue, Alpha are further processed by optional reflection and z-feather effects.


The shader program consists of a series of statements, empty lines or comments.

Destination = Parameter;Assignment Statement
Destination = Function ( Parameters... );Statement with built-in function (See below)
// ...Line Comment
/* ... */Multi-Line Comment
Parameter: Either an input register or a constant (See below)
Destination: Output register (See below)
Function: Built-in function (See below)

Registers (4 Component RGBA)

ResultThe main output register for the diffuse (RGB) and alpha values (output only). If not written to, it defaults to the value of the Color input register.
SpecularOptional register for specular mask (output only). When not used, it defaults to {1,1,1,1}. The RGB components of Specular will be multiplied with the specular components of lighting computations to create the final specular color. It will be ignored when RGBGen is not a lighting mode. The alpha component of Specular will be ignored.
EmissionOptional register for emission color (output only). When not used, it defaults to {0,0,0,0}. The RGB components of Emission will be added to the color from Result after diffuse and specular lighting was applied. The alpha component of Emission will be ignored.
ColorInput of the RGBGen and AlphaGen Color Units (Input only). When RGBGen is one of the lighting modes, the RGB components of Color will be 1.
Tmp0Temporary Register 0 (Input/Output)
Tmp1Temporary Register 1 (Input/Output)
Tmp2Temporary Register 2 (Input/Output)
Tmp3Temporary Register 3 (Input/Output)
Tex0Texel of Texture in Unit 0 (Input only)
Tex1Texel of Texture in Unit 1 (Input only)
Tex2Texel of Texture in Unit 2 (Input only)
Tex3Texel of Texture in Unit 3 (Input only)

Constants (Inputs)

Scalars (will be replicated to all channels) e.g. 1.0
Vectors e.g. {1.0, 2.0, 3.0, 4.0}


Saturate( Val )Clamps values to 0...1
Add (Val1, Val2)Addition. Pseudo Code: Val1 + Val2
Sub (Val1, Val2)Subtraction. Pseudo Code: Val1 - Val2
Mul (Val1, Val2)Multiply. Pseudo Code: Val1 * Val2
Mad (Val1, Val2, Val3)Multiply and Add. Pseudo Code:Val1 * Val2 + Val3
Lerp (Val1, Val2, Val3)Interpolate between Val2 and Val3. Pseudo Code: Val1 * Val2 + (1 - Val1) * Val3
Dp3 (Val1, Val2)Dot Product using the first 3 components (rgb); result will be replicated to all 4 channels. Pseudo Code: Val1[0] * Val2[0] + Val1[1] * Val2[1] + Val1[2] * Val2[2]

Write Masks

Output registers can have optional write masks that will prevent channels from being overwritten.
OutputRegisterNo Mask (all channels will be written to)
OutputRegister.rgbOnly RGB channels will be written to
OutputRegister.aOnly alpha channel will be written to


Input registers can have optional swizzles. Right now only one type of swizzle is supported.
InputRegisterNo swizzle (input is RGBA)
InputRegister.aThe fourth component (Alpha channel) will be replicated to all channels, for example say the temporary register tmp0 represents: {0.4, 0.3, 0.7, 2.0}. tmp0.a would then represent: {2.0, 2.0, 2.0, 2.0}


Example 1

Result.rgb = Tex0;
Result.a = Tex1;
The RGB channels will receive the RGB channels from the texture in Unit 0.
The A channel will receive the A channel from the texture in Unit 1.

Example 2

Tmp0 = Add(Tex0, Tex1);
Result.rgb = Lerp(Tex2.a, Tmp0, Tex2);
Result.a = 1.0;
The texel of the first and second texture will be added, the result will be interpolated between the third texture, using the A channel of the third texture as the interpolation control. The A channel of the result will be set to 1.

Example 3

Result.rgb = Tex0;
Result.a = Dp3(Tex1, 0.33333);
The RGB channels of the result will receive the RGB channels of the first texture. The A channel of the result will receive the average luminosity of the RGB channels of the second texture.

Example 4

Result = Tex0;
Specular = Tex1;
Emission = Mul (Color.a, {0,0,1,0});
The RGBA channels of the result will receive the RGBA channels of the first texture. The second texture is used as a specular mask for the specular lighting. The intensity of the RGBGen alpha mode is used to control a blueish glow effect, using the emission output register.