有个Shader需求:
1.根据模型的Height进行颜色的渐变
2.a值读取给定的皮肤Texture
自己写了个,基本满足:
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
Shader "Test/Shader_HeightColorLocalSpace"
{
Properties
{
_Color("SourceColor", Color) = (1,0,0,1)
_Color2("DestColor2", Color) = (1,1,1,1)
_ButtonY("ModelHeight", Range(0.1,1000)) = 1
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
_MainTex("MainTex",2D)= "white" {}
}
SubShader
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off
Pass {
ColorMaterial AmbientAndDiffuse
SetTexture [_MainTex] {
Combine texture * primary
}
}
Pass
{
Offset 1, 1
Fog { Mode Off }
ZWrite On
ZTest LEqual
Cull Off
Lighting Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile __ ENABLE_SPLITALPHA
#include "UnityCG.cginc"
fixed4 _Color;
fixed4 _Color2;
float _ButtonY;
sampler2D _MainTex;
struct appdata_t
{
float4 vertex : POSITION;
half4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
float2 uv : TEXCOORD1;
half4 color : COLOR;
};
v2f o;
uniform float4 _MainTex_ST;
v2f vert (appdata_t v)
{
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.color = (lerp(_Color, _Color2, o.vertex.y / _ButtonY));
return o;
}
half4 frag (v2f IN) : COLOR
{
half4 col;
col.rgb = IN.color;
fixed4 texcol = tex2D(_MainTex, IN.uv);
col.a = texcol.a;
// col.a = 1;
return col;
}
ENDCG
}
}
}