Cocos Creator 3.1 Material Upgrade Guide

This article will detail the considerations for upgrading Cocos Creator 3.0 materials to v3.1.

1. Shader upgrades and changes

1.1 Built-in header file changes

The standard shader header shading-standard from v3.0 has become standard-surface-entry from v3.1, making the effect compatible with both the forward render pipeline and the deferred render pipeline.

The cc-fog header file from v3.0 is now cc-fog-vs/fs from v3.1, split into vertex shader and fragment shader versions.

1.2 Vertex shaders

  • gl_Position

    The main function name of VS in v3.1 has been changed from vert to main, and a new macro gl_Position has been added to assign a value to the return value.

    1. CCProgram standard-vs %{
    2. precision highp float;
    3. // Include your headfile
    4. #include <cc-fog-vs> // Note the change in the header file name here
    5. // Fill in your data here
    6. void main () {
    7. // Fill in your data here
    8. gl_Position = fill in your data result;
    9. }
    10. }%

1.3 Fragment shaders

  • CC_STANDARD_SURFACE_ENTRY()

    Load the standard shader header file standard-surface-entry and use the v3.1 standard shader output function CC_STANDARD_SURFACE_ENTRY() to replace the original v3.0 shader output function frag().

    1. CCProgram standard-fs %{
    2. // Include your headfile
    3. #include <cc-fog-fs> // Note the change in the header file name here
    4. #include <standard-surface-entry> // Note the change in the name of the standard shader header file here
    5. // Fill in your data here
    6. void surf (out StandardSurface s) {
    7. // Fill in your data here
    8. }
    9. CC_STANDARD_SURFACE_ENTRY() // Standard shader output function
    10. }%

2. Deferred Render Pipeline

2.1 Deferred Render Pipeline

The biggest difference between v3.1 and v3.0 is that v3.1 supports the deferred render pipeline. The engine comes with a standard standard-surface-entry header file that supports both the forward render pipeline and the deferred render pipeline, which is used as follows:

  1. CCEffect %{
  2. techniques:
  3. // Fill in your data here
  4. - &deferred
  5. vert: // your Vertex shader
  6. frag: // your Fragment shader
  7. phase: deferred
  8. propertyIndex: 0
  9. blendState:
  10. targets: // turn off blending
  11. - blend: false
  12. - blend: false
  13. - blend: false
  14. - blend: false
  15. properties: // your properties name
  16. // Fill in your data here
  17. }%
  18. // fill in your data here
  19. CCProgram standard-fs %{
  20. precision highp float;
  21. #include <cc-global>
  22. #include <shared-ubos>
  23. #include <cc-fog-fs> // Note the change in the header file name here.
  24. #include <standard-surface-entry> // Note the change in the name of the standard shader header file here
  25. // Fill in your data here
  26. void surf (out StandardSurface s) {
  27. // Fill in your data here
  28. }
  29. CC_STANDARD_SURFACE_ENTRY() // Standard shader output function
  30. }%
  31. // fill in your data here

2.2 Render pipeline determination

The header file standard-surface-entry determines which render pipeline is selected, and the lighting calculation is in the file shading-standard-additive.

If it is a deferred render pipeline, the deferred-lighting effect file is called first, followed by the light calculation file shading-standard-additive.

  1. #define CC_STANDARD_SURFACE_ENTRY()
  2. #if CC_FORWARD_ADD
  3. #include <shading-standard-additive>
  4. // Fill in your data here
  5. #elif CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_FORWARD // Determine if it is the forward render pipeline
  6. // Fill in your data here
  7. #elif CC_PIPELINE_TYPE == CC_PIPELINE_TYPE_DEFERRED // Determine if it is the deferred render pipeline
  8. // Fill in your data here
  9. #endif

3. Parameter transfer upgrade

The macro for passing shadow parameters from vertex shaders to fragment shaders was originally CCPassShadowParams in v3.0, but was changed to CC_TRANSFER_SHADOW in v3.1.

The v3.1 vertex shader transfers FOG parameters to the fragment shader, using the CC_TRANSFER_FOG macro directly.

Version comparison:

  • v3.0

    1. v_fog_factor = CC_TRANSFER_FOG(pos);
    2. CCPassShadowParams(pos);
  • v3.1

    1. CC_TRANSFER_FOG(pos);
    2. CC_TRANSFER_SHADOW(pos);