Interpolant Shader Processes

From C4 Engine Wiki
Revision as of 11:59, 15 July 2023 by Eric Lengyel (talk | contribs) (Created page with "__NOTOC__ 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 Shader Editor under the Interpolants tab....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 Shader Editor under the Interpolants tab.

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.

Paint Texcoord

Inputs: None

Output: 2D vector

The paint space texture coordinates. These are determined by the paint space associated with the geometry being rendered. The output of this process is normally fed into a Paint Texture basic process.

Vertex Color

Inputs: None

Output: RGBA color

The vertex color.

The vertex color does not occupy any texcoord slots.

Vertex Geometry

Inputs: None

Output: 3D vector

This interpolant can be used with water shaders when land elevation data has been generated for water geometries. The z component contains the difference in height between the water surface and the land beneath it, and the x and y components contain the gradient of the land elevation.

Tangent-Space Light Direction

Inputs: None

Output: 3D vector

The unit-length tangent-space direction to light L.

If the ambient reflection output depends on this process, then the results are undefined.

Tangent-Space View Direction

Inputs: None

Output: 3D vector

The unit-length tangent-space direction to viewer V.

Tangent-Space 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.

If the ambient reflection output depends on this process, then the results are undefined.

Object-Space Light Direction

Inputs: None

Output: 3D vector

The unit-length object-space direction to light L.

If the ambient reflection output depends on this process, then the results are undefined.

Object-Space View Direction

Inputs: None

Output: 3D vector

The unit-length object-space direction to viewer V.

Object-Space 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.

If the ambient reflection output depends on this process, then the results are undefined.

Object-Space Vertex Position

Inputs: None

Output: 3D vector

The object-space vertex position P.

Object-Space Normal

Inputs: None

Output: 3D vector

The object-space vertex normal N.

Object-Space Tangent

Inputs: None

Output: 3D vector

The object-space vertex tangent T.

Object-Space Bitangent

Inputs: None

Output: 3D vector

The object-space vertex bitangent B.

World-Space Vertex Position

Inputs: None

Output: 3D vector

The world-space vertex position P.

World-Space Normal

Inputs: None

Output: 3D vector

The world-space vertex normal N.

World-Space Tangent

Inputs: None

Output: 3D vector

The world-space vertex tangent T.

World-Space Bitangent

Inputs: None

Output: 3D vector

The world-space vertex bitangent B.

Full Tangent-Space Light Vector

Inputs: None

Output: 3D vector

The full-length tangent-space direction to light L. This is the difference between the light position and vertex position in tangent space.

Full Tangent-Space View Vector

Inputs: None

Output: 3D vector

The full-length tangent-space direction to viewer V. This is the difference between the camera position and vertex position in object space.

Full Object-Space Light Vector

Inputs: None

Output: 3D vector

The full-length object-space direction to light L. This is the difference between the light position and vertex position in object space.

Full Object-Space View Vector

Inputs: None

Output: 3D vector

The full-length object-space direction to viewer V. This is the difference between the camera position and vertex position in object space.

See Also