Interpolant Shader Processes
From C4 Engine Wiki
When the graphics hardware renders a triangle, some values are calculated at each pixel and other values are calculated only at the vertices. The values calculated at the vertices are smoothly varied across the interior of a triangle through a process called perspective-correct interpolation. Hence, these values are called interpolants. The list below describes each of the interpolants available for use in the C4 Shader Editor.
Contents |
Interpolant Allocation
The graphics hardware can only handle so many interpolants, so there is a limit to how many can be used in a single shader. (The ambient and light graphs shown in the editor represent distinct shaders, so any interpolants used in one do not count against the other.) The limit is based on the availability of eight 4D vectors that the hardware provides for texture coordinates, or texcoords. We call these the eight texcoord slots. Among these slots, there are a total of exactly 32 scalar values that can be calculated at each vertex and interpolated across a triangle. However, interpolants that need 2, 3, or 4 values cannot be split up, so it may not be possible to pack all of the interpolants into the texcoord slots even though there is enough room for each of the component values. Furthermore, lighting and fog calculations that are not explicitly displayed in the shader graph need some of their own interpolants, so the number of usable texcoord slots is somewhat less than the full eight that exist in the hardware.
The shader compiler allocates interpolants to texcoord slots as follows:
- For any four-component interpolant, one entire texcoord slot is consumed.
- For any three-component interpolant, the first three components of a single texcoord slot are consumed. The fourth component can be used by a different one-component interpolant.
- For any two-component interpolant, either the first two or last two components of a single texcoord slot are consumed. If the first half of a texcoord slot is allocated, then the second half can be used by another two-component interpolant, or it can be used by two one-component interpolants.
- For any one-component interpolant, the first available unused component of any texcoord slot is consumed.
Interpolants are allocated in order of size from largest to smallest so that a bunch of small interpolants can't prevent a large interpolant from being allocated when there should be room.
Unless otherwise noted below, each interpolant provided in the Shader Editor requires the number of components in its output value.
Coordinate Spaces
There are three different coordinate systems that can be used in shaders: tangent-space coordinates, object-space coordinates, and world-space coordinates.
Tangent space is a special coordinate system that is required for normal mapping. In tangent space, the z-axis is always aligned with the vertex normal, and the x-axis is always aligned with the vertex tangent (which is aligned with the s direction of the texture coordinates). In the interior of a triangle, the tangent space is a blend of the tangent spaces at each of the three vertices. By definition, the normal vector in tangent space is always (0,0,1). Similarly, the tangent direction is always (1,0,0), and the bitangent direction is always (0,1,0).
Object space is the local coordinate space for an individual object. In object space, the normals and tangents are exactly those that are stored in the vertex arrays for an object. It is unusual for object-space coordinates to be used in a shader. The use of tangent-space coordinates is preferred because it is more efficient.
World space is the global coordinate system. The use of world-space coordinates is normally only needed when sampling cube texture maps.
Interpolants
|
Process |
Description |
|
Texcoord 1 |
Inputs: None Output: 2D vector The primary texture coordinates. |
|
Texcoord 2 |
Inputs: None Output: 2D vector The secondary texture coordinates. |
|
Vertex Color |
Inputs: None Output: RGBA color The vertex color. The vertex color does not occupy any texcoord slots. |
|
Tangent Light Direction |
Inputs: None Output: 3D vector The unit-length tangent-space direction to light L. This interpolant is not available in ambient shaders. |
|
Tangent View Direction |
Inputs: None Output: 3D vector The unit-length tangent-space direction to viewer V. |
|
Tangent Halfway Direction |
Inputs: None Output: 3D vector The unit-length tangent-space direction vector H. The halfway direction does not occupy any texcoord slots by itself, but it implicitly requires the light direction and view direction interpolants. This interpolant is not available in ambient shaders. |
|
Object Light Direction |
Inputs: None Output: 3D vector The unit-length object-space direction to light L. This interpolant is not available in ambient shaders. |
|
Object View Direction |
Inputs: None Output: 3D vector The unit-length object-space direction to viewer V. |
|
Object Halfway Direction |
Inputs: None Output: 3D vector The unit-length object-space halfway direction H. The halfway direction does not occupy any texcoord slots by itself, but it implicitly requires the light direction and view direction interpolants. This interpolant is not available in ambient shaders. |
|
Object Normal |
Inputs: None Output: 3D vector The object-space vertex normal N. |
|
Object Tangent |
Inputs: None Output: 3D vector The object-space vertex tangent T. |
|
Object Bitangent |
Inputs: None Output: 3D vector The object-space vertex bitangent B. |
|
World Normal |
Inputs: None Output: 3D vector The world-space vertex normal N. |
|
World Tangent |
Inputs: None Output: 3D vector The world-space vertex tangent T. |
|
World Bitangent |
Inputs: None Output: 3D vector The world-space vertex bitangent B. |















