Skip to content

Fragment Shader Input

Built-in Input Variables

Many parameters have been passed from Vertex Shader to Fragment Shader, listed below.

Fragment Shader InputTypeNeeded MacrosMeaning
FSInput_worldPosvec3N/AWorld Position
FSInput_worldNormalvec3N/AWorld Normal
FSInput_faceSideSignfloatN/ATwo Side Sign be used for double-sided materials
FSInput_texcoordvec2N/AUV0
FSInput_texcoord1vec2N/AUV1
FSInput_vertexColorvec4N/AVertex Color
FSInput_worldTangentvec3N/AWorld Tangent
FSInput_mirrorNormalfloatN/AMirror Normal Sign
FSInput_localPosvec4CC_SURFACES_TRANSFER_LOCAL_POSLocal Position
FSInput_clipPosvec4CC_SURFACES_TRANSFER_CLIP_POSClip Position

Macro Switch

When you need to use input parameters with macro switches, you need to enable the corresponding macros in the macro-remapping code section. Here's an example.

glsl
CCProgram macro-remapping %{
    ...
    //Enable FSInput_localPos
    #define CC_SURFACES_TRANSFER_LOCAL_POS 1
    //Enable FSInput_clipPos
    #define CC_SURFACES_TRANSFER_CLIP_POS 1
    ...
}

How to use

Directly call them in your shader code.

Customize Varying Variables

When creating some special effects, the Vertex Shader must pass more information to the Fragment Shader. At this time, we need to add new varying variables.

Adding a new custom varying variable is quite simple. We will use the example of adding a new variable called testVec3.

First, declare a variable with an out tag in the Vertex Shader, as shown in the following example.

glsl
CCProgram surface-vertex %{
    ...
    out vec3 testVec3;
    ...
}

Then declare a corresponding variable with an in tag in the Fragment Shader, as shown below.

glsl
CCProgram surface-fragment %{
    ...
    in vec3 testVec3;
    ...
}

After that, you can use testVec3 in the code of the Fragment Shader.