["Tips for the Development of Unity Shaders and Screen Special Effects"] Learning and finishing: Regarding the display issues of model vertex colors

Today, a problem occurred while imitating a surface shader that displays model vertex colors. The compiler reported the following error:

‘vert’: output parameter ‘o’ not completely initialized
Compiling Vertex program with DIRECTIONAL
Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_LIGHT_PROBE_PROXY_VOLUME

After checking, I found that it was caused by a missing line of code.
The more detailed reason is that the shader's render target is defined as Directx 11. It seems that Unity5.x now defaults to Directx 11.
Rendering with Directx 11 requires proper initialization of the output of vertex colors.
UNITY_INITIALIZE_OUTPUT(Input, name) needs to be added for initialization.

Below is the shader code

Shader "SurfaceShader/VertColor" 
    {
        Properties
        {
            _MainTint("Global Color Tint", Color) = (1, 1, 1, 1)
        }

        SubShader
        {
            Tags{
   
   "RenderType" = "Opaque"}
            LOD 200

            CGPROGRAM
            #pragma surface surf Lambert vertex:vert

            float4 _MainTint;

            struct Input
            {
                float2 uv_MainTex;
                float4 vertColor;
            };

            void vert(inout appdata_full v, out Input o)
            {
                //对输出进行初始化
                UNITY_INITIALIZE_OUTPUT(Input, o);
                //采集顶点色
                o.vertColor = v.color;
            }

            void surf(Input IN, inout SurfaceOutput o)
            {
                o.Albedo = IN.vertColor.rgb * _MainTint.rgb;
            }

            ENDCG
        }
    }

The picture below is the effect of the default shader of Unity5.x.
Default shader
Below is the same model using a vertex color shader.
Model shows vertex color
The last one is the shader used.
Vertex color shader

In the process of copying this shader, in addition to the above initialization problems, another problem also occurred.

#pragma surface surf Lambert vertex:vert

This is vertex:vertthe part of the code. Since I am used to using C# to enter a field with spaces, I wrote it at first. However, a vertex : vert
warning will be displayed saying that this part is an unrecognized #pragma instruction and will cause the shader to run abnormally.

Finally, what I know about the model vertex color so far is that it can be set through Maya. I don’t know if it is possible with 3dMax. Using this technology can reduce the number of textures to a certain extent and store the required information directly on the model vertices. .

Guess you like

Origin blog.csdn.net/EverNess010/article/details/79778004