UnityShader——置灰

时间:2020-07-06 11:03:46   收藏:0   阅读:87

实现置灰非常简单,只要用原来颜色的rgb分量按照不同的权重加起来,就可以得到一个灰度值,这个灰度值就是新的颜色的rgb分量。

Shader "WS/Gray"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader
	{
		Tags { "RenderType"="Transparent" "Queue"="Transparent" }
		LOD 100

		Pass
		{
			ZTest Always
			ZWrite Off
			Cull Off
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

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

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			
			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);
                                //灰化
				fixed gray = mul(col.rgb, fixed3(0.299, 0.587, 0.114));
				return fixed4(gray, gray, gray, 1.0);
			}
			ENDCG
		}
	}
}

灰化之前:
技术图片
灰化之后:
技术图片

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!