Shader饱和度,色值,灰度

Shader "Custom/HSLShader"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_DH("Hue",Range(0,360)) = 0
		_DS("Saturation",Range(-1,1)) = 0
		_DL("Lightness",Range(-1,1)) = 0
	}
	SubShader
	{
		// No culling or depth
		// Cull Off ZWrite Off ZTest Always
		Pass
		{
			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;
			float _DH;
			float _DS;
			float _DL;
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				return o;
			}
			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, i.uv);
				float r = col.r;
				float g = col.g;
				float b = col.b;
				float a = col.a;
				float h;
				float s;
				float l;
				float maxv = max(max(r,g),b);
				float minv = min(min(r,g),b);
				if (maxv == minv){
					h = 0.0;
				} else if (maxv == r && g >= b){
					h = 60.0*(g-b)/(maxv-minv)+0.0;
				} else if (maxv == r && g < b ){
					h = 60.0*(g-b)/(maxv-minv)+360.0;
				} else if (maxv == g){
					h = 60.0*(b-r)/(maxv-minv)+120.0;
				} else if (maxv == b){
					h = 60.0*(r-g)/(maxv-minv)+240.0;
				}
				l = 0.5*(maxv+minv);
				if (l == 0.0 || maxv == minv){
					s = 0.0;
				} else if (0.0 <= l && l <= 0.5){
					s = (maxv-minv)/(2.0*l);
				} else if (l > 0.5){
					s = (maxv-minv)/(2.0-2.0*l);
				}
				h = h + _DH;
				s = min(1.0,max(0.0,s+_DS));
				l = l + _DL;
				// final color
				float q;
				if (l < 0.5){
					q = l*(1.0+s);
				}else if (l >= 0.5){
					q = l+s-l*s;
				}
				float p = 2.0*l-q;
				float hk = h/360.0;
				float t[3];
				t[0] = hk+1.0/3.0;
				t[1] = hk;
				t[2] = hk-1.0/3.0;
				for(int i=0;i<3;i++){
					if (t[i] < 0.0){
						t[i] += 1.0;
					}else if (t[i] > 1.0){
						t[i] -= 1.0;
					}
				}
				float c[3];
				for (int i=0;i<3;i++){
					if (t[i] < 1.0/6.0){
						c[i] = p+((q-p)*6.0*t[i]);
					}else if (1.0/6.0 <= t[i] && t[i] < 0.5){
						c[i] = q;
					}else if (0.5 <= t[i] && t[i] < 2.0/3.0){
						c[i] = p+((q-p)*6.0*(2.0/3.0-t[i]));
					}else{
						c[i] = p;
					}
				}
				fixed4 finalColor = fixed4(c[0],c[1],c[2],a);
				finalColor += fixed4(_DL,_DL,_DL,0.0);
				return finalColor;
			}
			ENDCG
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值