Color de vértice del sombreador de unidad

Descripción general: use el color del vértice en la unidad para simplemente realizar el degradado del parche de abajo hacia arriba


1. Preparación preliminar

¿Para qué se utilizan los colores de los vértices?
Usualmente se usa para colorear por región

1. Método de color de vértice de dibujo modelo

  • Dibujar en software 3D, como Maya, 3Dmax, Blender, etc.
  • Usando complementos en la unidad

2. Ver el color del vértice del modelo.

  • Ver en software 3D
  • Use la salida en la función de fragmento en el sombreador
fixed4 frag (v2f i) : SV_Target{
    
    
	return i.vertColor;
}

Consulta el color del vértice del parche del modelo.

2. Utilice los datos de color de los vértices en el sombreador para colorear de forma sencilla

1. Cree un sombreador y un sombreador nuevos, asigne el sombreador creado al sombreador y asigne el sombreador al modelo.
2. atributo de sombra

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"{
    
    }
}

estructura de entrada

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

función de vértice

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

estructura de salida

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

definición de declaración

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

Función de fragmento

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;
	}

inserte la descripción de la imagen aquí


código completo

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
        }
    }
}

Supongo que te gusta

Origin blog.csdn.net/Poggio742/article/details/129683292
Recomendado
Clasificación