Legacy Shader Key Functions and Structures

CCVertInput1

  • To connect with skeletal animation and data decompression processes, we provide the CCVertInput function, which has general and standard versions, as shown below.

    1. // Located in ‘input.chunk’, used to handle vertex input
    2. #define CCVertInput(position) \
    3. CCDecode(position); \
    4. #if CC_USE_MORPH \
    5. applyMorph(position); \
    6. #endif \
    7. #if CC_USE_SKINNING \
    8. CCSkin(position); \
    9. #endif \
    10. #pragma //
    11. // Located in ‘input-standard.chunk’, used to handle standard vertex input
    12. #define CCVertInput(In) \
    13. CCDecode(In); \
    14. #if CC_USE_MORPH \
    15. applyMorph(In); \
    16. #endif \
    17. #if CC_USE_SKINNING \
    18. CCSkin(In); \
    19. #endif \
    20. #pragma //
  • If you only need to get the positions, you can use the general version, and the code for this situation is as follows.

    1. #include <legacy/input>
    2. vec4 vert () {
    3. vec3 position;
    4. CCVertInput(position);
    5. // ... TO DO
    6. }
  • If you also need normals, you can use the standard version, as follows.

    1. #include <legacy/input-standard>
    2. vec4 vert () {
    3. StandardVertInput In;
    4. CCVertInput(In);
    5. // ... Now the ‘In.position’ is completely initialized, and can be used in the vertext shader
    6. }

In the example code above, the StandardVertInput object In returns the positions, normals, and tangents in model space, and completes the skinning calculation for the skeletal animation model.

The definition of the StandardVertInput structure is as follows.

  1. struct StandardVertInput {
  2. highp vec4 position;
  3. vec3 normal;
  4. vec4 tangent;
  5. };

Note: After including the header files, there is no need to declare these attributes anymore, such as a_position, a_normal, a_tangent, etc. For other vertex attributes, such as uv, etc., still need to be declared before used.

If you want to connect with the dynamic mesh batching and GPU Instancing features, you need to include the cc-local-batch and use the CCGetWorldMatrix function to get the world matrix. Here’s an example.

  1. mat4 matWorld;
  2. CCGetWorldMatrix(matWorld);
  3. mat4 matWorld, matWorldIT;
  4. CCGetWorldMatrixFull(matWorld, matWorldIT);

For more details, please refer to Cocos Shader Built-in Uniforms

CCFragOutput

Cocos Shader provides a CCFragOutput function to simplify the output of the fragment shader, which can be used to directly return the values needed by the fragment shader. Here is an example code.

  1. #include <legacy/output>
  2. vec4 frag () {
  3. vec4 o = vec4(0.0);
  4. // ...
  5. return CCFragOutput(o);
  6. }

The CCFragOutput will decide whether to perform ToneMap based on the pipeline type. This way, the intermediate color does not need to distinguish whether the current rendering pipeline is an HDR process.

Here is an example code.

  1. vec4 CCFragOutput (vec4 color) {
  2. #if CC_USE_HDR
  3. color.rgb = ACESToneMap(color.rgb);
  4. #endif
  5. color.rgb = LinearToSRGB(color.rgb);
  6. return color;
  7. }

Note:If you use the CCFragOutput function to output the final color of fragment shaders, the color must be converted to the Linear space before performing calculations. This is because the CCFragOutput assumes that the passed parameters are in the Linear space and will always call the LinearToSRGB function to transcode.

If you need to include standard PBR, you can use the StandardSurface structure together with the CCStandardShadingBase function to form a PBR shading process.

The content of the StandardSurface structure is as follows.

  1. struct StandardSurface {
  2. // albedo
  3. vec4 albedo;
  4. // these two need to be in the same coordinate system
  5. vec3 position;
  6. vec3 normal;
  7. // emissive
  8. vec3 emissive;
  9. // light map
  10. vec3 lightmap;
  11. float lightmap_test;
  12. // PBR params
  13. float roughness;
  14. float metallic;
  15. float occlusion;
  16. };

The example code is as below.

  1. #include <legacy/shading-standard-base>
  2. #include <legacy/output-standard>
  3. void surf (out StandardSurface s) {
  4. // fill in your data here
  5. }
  6. vec4 frag () {
  7. StandardSurface s; surf(s);
  8. vec4 color = CCStandardShadingBase(s);
  9. return CCFragOutput(color);
  10. }

You can refer to the builtin-standard.effect file, and use the combination of the surf function and the CC_STANDARD_SURFACE_ENTRY() macro.

The CC_STANDARD_SURFACE_ENTRY() is a wrapper that based on the render pipeline type, uses the surf function to construct a usable main function for the fragments. Here is an example of the code.

  1. CCProgram shader-fs %{
  2. #include <legacy/standard-surface-entry>
  3. void surf (out StandardSurface s) {
  4. // fill in your data here
  5. }
  6. CC_STANDARD_SURFACE_ENTRY()
  7. }%

StandardSurface

The StandardSurface is the structure of PBR material data that records the surface information required for lighting calculations. The surf function in Legacy Shaders is used to fill data and it will be used during the lighting phase.

  1. struct StandardSurface {
  2. // albedo
  3. vec4 albedo;
  4. // these two need to be in the same coordinate system
  5. HIGHP_VALUE_STRUCT_DEFINE(vec3, position);
  6. vec3 normal;
  7. // emissive
  8. vec3 emissive;
  9. // light map
  10. vec3 lightmap;
  11. float lightmap_test;
  12. // PBR params
  13. float roughness;
  14. float metallic;
  15. float occlusion;
  16. float specularIntensity;
  17. #if CC_RECEIVE_SHADOW
  18. vec2 shadowBias;
  19. #endif
  20. };

ToonSurface

  1. struct ToonSurface {
  2. vec4 baseColor;
  3. vec4 specular;
  4. // these two need to be in the same coordinate system
  5. HIGHP_VALUE_STRUCT_DEFINE(vec3, position);
  6. vec3 normal;
  7. // shading params
  8. vec3 shade1;
  9. vec3 shade2;
  10. vec3 emissive;
  11. float baseStep;
  12. float baseFeather;
  13. float shadeStep;
  14. float shadeFeather;
  15. float shadowCover;
  16. #if CC_RECEIVE_SHADOW
  17. vec2 shadowBias;
  18. #endif
  19. };

Similar to the StandardSurface, the ToonSurface is also used for surface information, but it is a structure specifically for cartoon rendering.

  1. Nothing to do with shaders that are not based on Mesh rendering, such as particles, Sprite, post-process, etc.