欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity Image Color Tune Shader

程序员文章站 2024-03-18 17:53:46
...

Hue, saturation, brightness, contrast shader - Unity Forum

Shader HSBC – Clonefactor

Matrix Operations for Image Processing (graficaobscura.com)

Shader "Custom/RGBTexture_tune"
{
	Properties
	{
		_MainTex("Texture", 2D) = "white" {}
		_k1("k1", range(-1, 1)) = 0
		_k2("k2", range(-1, 1)) = 0
		_p1("p1", range(-1, 1)) = 0
		_p2("p2", range(-1, 1)) = 0
		_k3("k3", range(-1, 1)) = 0
		_bImage("bImage", INT) = 0
		_bDistortion("bDistortion", INT) = 1
		_MainTextST("MainTextST", Vector) = (1, 0, 1, 0)
		//image color tune
		_Hue("Hue", Range(0, 1.0)) = 0
		_Saturation("Saturation", Range(0, 1.0)) = 0.5
		_Brightness("Brightness", Range(0, 1.0)) = 0.5
		_Contrast("Contrast", Range(0, 1.0)) = 0.5
	}

		SubShader
		{
			Tags { "RenderType" = "Opaque" }
			LOD 100
			Pass
			{
				ZTest Always
				ZWrite Off
				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"

			inline float3 applyHue(float3 aColor, float aHue)
			{
				float angle = radians(aHue);
				float3 k = float3(0.57735, 0.57735, 0.57735);
				float cosAngle = cos(angle);
				//Rodrigues' rotation formula
				return aColor * cosAngle + cross(k, aColor) * sin(angle) + k * dot(k, aColor) * (1 - cosAngle);
			}


			inline float4 applyHSBEffect(float4 startColor, fixed4 hsbc)
			{
				float hue = 360 * hsbc.r;
				float saturation = hsbc.g * 2;
				float brightness = hsbc.b * 2 - 1;
				float contrast = hsbc.a * 2;

				float4 outputColor = startColor;
				outputColor.rgb = applyHue(outputColor.rgb, hue);
				outputColor.rgb = (outputColor.rgb - 0.5f) * contrast + 0.5f + brightness;
				outputColor.rgb = lerp(Luminance(outputColor.rgb), outputColor.rgb, saturation);

				return outputColor;
			}

				struct v2f
				{
					float4 pos : POSITION;
					half2 uv : TEXCOORD0;
					fixed4 hsbc : COLOR;
				};


				sampler2D _MainTex;
				float _k1;
				float _k2;
				float _p1;
				float _p2;
				float _k3;
				int _bDistortion;
				int _bImage;
				float4 _MainTextST;
				fixed _Hue, _Saturation, _Brightness, _Contrast;

				//Our Vertex Shader 
			   v2f vert(appdata_img v)
			   {
				   v2f o;
				   o.pos = UnityObjectToClipPos(v.vertex);
				   o.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, v.texcoord.xy);
				   if (_bImage == 0)
				   {
					   o.uv = float2(o.uv.x, 1 - o.uv.y);
				   }
				   o.hsbc = fixed4(_Hue, _Saturation, _Brightness, _Contrast);
				   return o;
			   }

			   //Our Fragment Shader
			   float4 frag(v2f i) : COLOR
			   {
				   if (_bDistortion == 1)
				   {
					   // lens distortion coefficient
					   float x = i.uv.x - 0.5;
					   float y = i.uv.y - 0.5;
					   float r2 = x * x + y * y;
					   float f = 1 + _k1 * r2 + _k2 * r2 * r2 + _k3 * r2 * r2 * r2;

					   // get the right pixel for the current position
					   float x_new = x * f + 2 * _p1 * x * y + _p2 * (r2 + 2 * x * x) + 0.5;
					   float y_new = y * f + _p1 * (r2 + 2 * y * y) + 2 * _p2 * x * y + 0.5;

					   float texX = x_new * _MainTextST.x + _MainTextST.y;
					   float texY = y_new * _MainTextST.z + _MainTextST.w;

					   float3 inputDistord = tex2D(_MainTex,float2(texX,texY));
					   // return float4(texY,texY,texY,1);
					   float4 newColor = float4(inputDistord.r,inputDistord.g,inputDistord.b,1);
					   if (_bImage == 0)
					   {
						   newColor = float4(inputDistord.b, inputDistord.g, inputDistord.r, 1);
					   }
					   newColor= applyHSBEffect(newColor, i.hsbc);
					   return newColor;
				   }
				   else
				   {
						float3 inputDistord = tex2D(_MainTex, i.uv);
						// return float4(texY,texY,texY,1);r
						float4 newColor = float4(inputDistord.r,inputDistord.g,inputDistord.b,1);
						if (_bImage == 0)
						{
							newColor = float4(inputDistord.b, inputDistord.g, inputDistord.r, 1);
						}
						newColor = applyHSBEffect(newColor, i.hsbc);
						return newColor;
					}
				}
				ENDCG
			}
		}
			FallBack "Diffuse"
}