黑白图片切换shader

通过公式改变材质Color属性,实现黑白图片效果。

Shader "Unlit/NewUnlitShader"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_GrayImage ("ChangeGray",float) = 1
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				half4 color : COLOR;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f
			{
				float2 texcoord : TEXCOORD0;
				half4 color : COLOR;
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
		    float _GrayImage;
			float4 _MainTex_ST;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.color = v.color;
				o.texcoord = v.texcoord;
				return o;
			}
			
			fixed4 frag (v2f i) : COLOR
			{
				fixed4 col;  
				if (_GrayImage == 1)  
				{  
					col = tex2D(_MainTex, i.texcoord);  
					float grey = dot(col.rgb, float3(0.299, 0.587, 0.114));  
					col.rgb = float3(grey, grey, grey);  
				}  
				else  
				{  
					col = tex2D(_MainTex, i.texcoord) * i.color;  
				}  
				return col;  
			}
			ENDCG
		}
	}
}

猜你喜欢

转载自my.oschina.net/u/698044/blog/983029