颜色渐变

作者:追风剑情 发布于:2019-10-28 20:56 分类:Shader

Shader "Custom/Gradient"
{
	Properties
	{
		_MainTex("Texture", 2D) = "white" {}
		//渐变颜色
		_GradientColor("Gradient Color", Color) = (1, 0, 0, 0)
		//离底部距离
		_GradientBottom("Gradient Bottom", Range(0, 1)) = 0.5
	}
	SubShader
	{
		Tags { "Queue" = "Transparent" "IgnoreProjector" = "true" "RenderType" = "Transparent"}
		LOD 100

		Pass
		{
			Tags {"LightMode" = "ForwardBase"}

			ZWrite Off
			//开启Alpha混合才能调节透明度
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				UNITY_FOG_COORDS(1)
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			fixed4 _GradientColor;
			float _GradientBottom;

			v2f vert(appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				return o;
			}

			fixed4 frag(v2f i) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, i.uv);
				//线性插值(y-y0)=k(x-x0)
				//+ step(_GradientBottom, 1) 是为了避免分母为0
				fixed t = max(0, 1 / (1 + step(_GradientBottom, 1) -_GradientBottom) * (i.uv.y - _GradientBottom));
				col = lerp(col, _GradientColor, t);
				return col;
			}
			ENDCG
		}
	}
}

222.png

测试

111111.gif

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号