Vertex color of unity shader

Overview: Use vertex color in unity to simply realize the gradient of the patch from bottom to top


1. Preliminary preparation

What are vertex colors used for?
Usually used for coloring by region

1. Model drawing vertex color method

  • Draw in 3D software, such as Maya, 3Dmax, Blender, etc.
  • Using plugins in unity

2. View the vertex color of the model

  • View in 3D software
  • Use the output in the fragment function in the shader
fixed4 frag (v2f i) : SV_Target{
    
    
	return i.vertColor;
}

Query the vertex color of the model patch

2. Use vertex color data in the shader for simple coloring

1. Create a new shader and shader, assign the created shader to the shader, and assign the shader to the model.
2. shader attribute

Properties
	{
    
    
		_MainColor("顶部颜色",Color) = (1.0,1.0,1.0,1.0)
		_MainTex("顶部颜色贴图",2D) = "White"{
    
    }
		_SecondColor("底部颜色",Color) = (1.0,1.0,1.0,1.0)
		_SecondTex("底部颜色贴图",2D) = "White"{
    
    }
}

input structure

struct appdata
	{
    
    
		float4 vertex : POSITION;
		float2 uv : TEXCOORD0;
		float4 vertexColor : COLOR;    //输入顶点色数据
		};

Vertex function

v2f vert (appdata v)
	{
    
    
		v2f o;
		o.vertex = UnityObjectToClipPos(v.vertex);
		o.uv = TRANSFORM_TEX(v.uv, _MainTex);
		o.vertColor = v.vertexColor;        //顶点色
		return o;
	}

output structure

struct v2f
	{
    
    
		float2 uv : TEXCOORD0;
		float4 vertColor : TEXCOORD1;           //顶点色
		float4 vertex : SV_POSITION;
	};

statement definition

sampler2D _MainTex;  float4 _MainTex_ST;
sampler2D _SecondTex;  float4 _SecondTex_ST;
float4 _MainColor;
float4 _SecondColor;

Fragment function

fixed4 frag (v2f i) : SV_Target
	{
    
    
		//用顶点色vertColor.r的r通道是因为刚才在查看模型顶点色的时候看到的渐变是用红色通道。所以就拿r通道来制作及渐变
		fixed4 col = tex2D(_MainTex, i.uv) * _MainColor * i.vertColor.r + tex2D(_SecondTex, i.uv) * _SecondColor * (1.0 - i.vertColor.r );
		return col;
	}

insert image description here


full code

Shader "Unlit/vertexColor"
{
    
    
    Properties
    {
    
    
        _MainColor("Main Color",Color) = (1.0,1.0,1.0,1.0)
		_MainTex("MainTex",2D) = "White"{
    
    }
		_SecondColor("SecondColor",Color) = (1.0,1.0,1.0,1.0)
		_SecondTex("SecondTex",2D) = "White"{
    
    }
    }
    SubShader
    {
    
    
        Tags {
    
     "RenderType"="Opaque" }
        Pass
        {
    
    
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
    
    
                float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float4 vertexColor : COLOR;    //输入顶点色数据
            };

            struct v2f
            {
    
    
                float2 uv : TEXCOORD0;
				float4 vertColor : TEXCOORD1;           //顶点色
				float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;  float4 _MainTex_ST;
			sampler2D _SecondTex;  float4 _SecondTex_ST;
			float4 _MainColor;
			float4 _SecondColor;

            v2f vert (appdata v)
            {
    
    
                v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				o.vertColor = v.vertexColor;        //顶点色
				return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
    
    
            	//着色
                fixed4 col = tex2D(_MainTex, i.uv) * _MainColor * i.vertColor.r + tex2D(_SecondTex, i.uv) * _SecondColor * (1.0 - i.vertColor.r );
				return col;
            }
            ENDCG
        }
    }
}

Guess you like

Origin blog.csdn.net/Poggio742/article/details/129683292