在网上搜索很久,这里应该是第一个发布的。
代码拷贝直接用就可以了.
效果:(颜色自定义,或者代码控制都可以)
如果需要在此基础上更改代码,注意加粗部分是否添加。
Tags { "RenderType"="Opaque" "DisableBatching" = "True" }
#pragma surface surf Standard fullforwardshadows vertex:vert。
代码大概逻辑就是根据verx顶点坐标 起始和结束颜色的差值计算。
最后渲染成根据高度生成对应渐变颜色。
挂到材质上使用
很简单哦!
代码:
Shader "Custom/NewSurfaceShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_EndColor("EndColor",Color)=(1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" "DisableBatching" = "True" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows vertex:vert
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
float3 my_vertPos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
fixed4 _EndColor;
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void vert(inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input, o)
o.my_vertPos = v.vertex;
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
c.r = _EndColor.r - IN.my_vertPos.y * (_EndColor - _Color).r;
c.g = _EndColor.g - IN.my_vertPos.y * (_EndColor - _Color).g;
c.b = _EndColor.b - IN.my_vertPos.y * (_EndColor - _Color).b;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}