//RGB转换:http://www.easyrgb.com/en/math.php#text22
Shader "HSVShader/ColorGradation_HSV" {
Properties {
//贴图
_MainTex ("MainTex (RGB)", 2D) = "white" {}
//Hue的值范围为0-359. 其他两个为0-1 ,这里我们设置到3,因为乘以3后 都不一定能到超过.
_Hue ("Hue", Range(0,359)) = 0
_Saturation ("Saturation", Range(0,3.0)) = 1.0
_Value ("Value", Range(0,3.0)) = 1.0
}
SubShader {
Pass {
Tags { "RenderType"="Opaque" }
LOD 200
Lighting Off
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
half _Hue;
half _Saturation;
half _Value;
struct Input {
float2 uv_MainTex;
};
//RGB to HSV
float3 RGBConvertToHSV(float3 rgb)
{
float R = rgb.x/255,G = rgb.y/255,B = rgb.z/255;
float3 hsv;
float max1=max(R,max(G,B));
float min1=min(R,min(G,B));
float del_max = max1 - min1;
hsv.z = max1;
if (del_max == 0)
{
hsv.x = 0;
hsv.y = 0;
}
else
{
hsv.y = del_max / max1;
float del_R = (((max1 - R) / 6) + (del_max / 2)) / del_max;
float del_G = (((max1 - G) / 6) + (del_max / 2)) / del_max;
float del_B = (((max1 - B) / 6) + (del_max / 2)) / del_max;
if (R == max1)hsv.x = del_B - del_G;
else if (G == max1)hsv.x = (1 / 3) + del_R - del_B;
else if (B == max1)hsv.x = (2 / 3) + del_G - del_R;
if (hsv.x < 0)hsv.x += 1;
if (hsv.x > 1)hsv.x -= 1;
}
/*if (R == max1)
{
hsv.x = (G-B)/(max1-min1);
}
if (G == max1)
{
hsv.x = 2 + (B-R)/(max1-min1);
}
if (B == max1)
{
hsv.x = 4 + (R-G)/(max1-min1);
}
hsv.x = hsv.x * 60.0;
if (hsv.x < 0)
hsv.x = hsv.x + 360;
hsv.z=max1;
hsv.y=(max1-min1)/max1;*/
return hsv;
}
//HSV to RGB
float3 HSVConvertToRGB(float3 hsv)
{
float R,G,B;
//float3 rgb;
if( hsv.y == 0 )
{
/*R=G=B=hsv.z;*/
R = hsv.z * 255;
G = hsv.z * 255;
B = hsv.z * 255;
}
else
{
float var_r, var_g, var_b;
float var_h = hsv.x * 6;
if (var_h == 6)var_h = 0;
int var_i = (int)var_h;//把var_h转化为整数var_i;
float var_1 = hsv.z*(1 - hsv.y);
float var_2 = hsv.z*(1 - hsv.y*(var_h - var_i));
float var_3 = hsv.z*(1 - hsv.y*(1 - (var_h - var_i)));
if (var_i == 0) { var_r = hsv.z; var_g = var_3; var_b = var_1; }
else if (var_i == 1) { var_r = var_2; var_g = hsv.z; var_b = var_1; }
else if (var_i == 2) { var_r = var_1; var_g = hsv.z; var_b = var_3; }
else if (var_i == 3) { var_r = var_1; var_g = var_2; var_b = hsv.z; }
else if (var_i == 4) { var_r = var_3; var_g = var_1; var_b = hsv.z; }
else { var_r = hsv.z; var_g = var_1; var_b = var_2; }
R = var_r * 255;
G = var_g * 255;
B = var_b * 255;
}
/*else
{
hsv.x = hsv.x/60.0;
int i = (int)hsv.x;
float f = hsv.x - (float)i;
float a = hsv.z * ( 1 - hsv.y );
float b = hsv.z * ( 1 - hsv.y * f );
float c = hsv.z * ( 1 - hsv.y * (1 - f ) );
switch(i)
{
case 0: R = hsv.z; G = c; B = a;
break;
case 1: R = b; G = hsv.z; B = a;
break;
case 2: R = a; G = hsv.z; B = c;
break;
case 3: R = a; G = b; B = hsv.z;
break;
case 4: R = c; G = a; B = hsv.z;
break;
default: R = hsv.z; G = a; B = b;
break;
}
}*/
return float3(R,G,B);
}
fixed4 frag (v2f_img i) : SV_Target
{
fixed4 original = tex2D(_MainTex, i.uv); //获取贴图原始颜色
float3 colorHSV;
colorHSV.xyz = RGBConvertToHSV(original.xyz); //转换为HSV
colorHSV.x += _Hue; //调整偏移Hue值
colorHSV.x = colorHSV.x%360; //超过360的值从0开始
colorHSV.y *= _Saturation; //调整饱和度
colorHSV.z *= _Value;
original.xyz = HSVConvertToRGB(colorHSV.xyz); //将调整后的HSV,转换为RGB颜色
return original;
}
ENDCG
}
}
FallBack "Diffuse"
}