Skip to content

Commit deb18ac

Browse files
authored
Create HueContrastSaturation.shader
1 parent 0405e09 commit deb18ac

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// source https://forum.unity.com/threads/adding-hue-saturation-and-contrast-to-standardshader.843697/#post-5572063
2+
3+
Shader "UnityLibrary/Standard/HueContrastSaturation"
4+
{
5+
Properties
6+
{
7+
_Color ("Color", Color) = (1,1,1,1)
8+
_MainTex ("Albedo Map", 2D) = "black" {}
9+
_BumpMap ("Normal Map", 2D) = "bump" {}
10+
_MetallicGlossMap ("Metallic (R) Smoothness (A) Map", 2D) = "black" {}
11+
_Hue ("Hue", Float) = 1.0
12+
_Contrast ("Contrast", Float) = 1.0
13+
_Saturation ("Saturation", Float) = 1.0
14+
}
15+
16+
Subshader
17+
{
18+
Tags { "RenderType" = "Opaque" }
19+
CGPROGRAM
20+
#pragma surface SurfaceShader Standard fullforwardshadows addshadow
21+
22+
sampler2D _MainTex, _BumpMap, _MetallicGlossMap;
23+
float4 _Color;
24+
float _Hue, _Contrast, _Saturation;
25+
26+
float4x4 contrastMatrix (float c)
27+
{
28+
float t = (1.0 - c) * 0.5;
29+
return float4x4 (c, 0, 0, 0, 0, c, 0, 0, 0, 0, c, 0, t, t, t, 1);
30+
}
31+
32+
float3 RGBToHSV(float3 c)
33+
{
34+
float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
35+
float4 p = lerp( float4( c.bg, K.wz ), float4( c.gb, K.xy ), step( c.b, c.g ) );
36+
float4 q = lerp( float4( p.xyw, c.r ), float4( c.r, p.yzx ), step( p.x, c.r ) );
37+
float d = q.x - min( q.w, q.y );
38+
float e = 1.0e-10;
39+
return float3( abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
40+
}
41+
42+
float3 HSVToRGB( float3 c )
43+
{
44+
float4 K = float4( 1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0 );
45+
float3 p = abs( frac( c.xxx + K.xyz ) * 6.0 - K.www );
46+
return c.z * lerp( K.xxx, saturate( p - K.xxx ), c.y );
47+
}
48+
49+
float3 Hue( float3 p, float v )
50+
{
51+
p = RGBToHSV(p);
52+
p.x *= v;
53+
return HSVToRGB(p);
54+
}
55+
56+
float3 Saturation( float3 p, float v )
57+
{
58+
p = RGBToHSV(p);
59+
p.y *= v;
60+
return HSVToRGB(p);
61+
}
62+
63+
float3 Contrast( float3 p, float v )
64+
{
65+
return mul(float4(p,1.0), contrastMatrix(v)).rgb;
66+
}
67+
68+
struct Input
69+
{
70+
float2 uv_MainTex;
71+
float2 uv_BumpMap;
72+
float2 uv_MetallicGlossMap;
73+
};
74+
75+
void SurfaceShader (Input IN, inout SurfaceOutputStandard o)
76+
{
77+
float4 color = tex2D(_MainTex,IN.uv_MainTex) * _Color;
78+
color.rgb = Hue(color.rgb, _Hue);
79+
color.rgb = Saturation(color.rgb, _Saturation);
80+
color.rgb = Contrast(color.rgb, _Contrast);
81+
o.Albedo = color;
82+
o.Normal = UnpackNormal (tex2D(_BumpMap, IN.uv_BumpMap));
83+
o.Metallic = tex2D(_MetallicGlossMap, IN.uv_MetallicGlossMap).r;
84+
o.Smoothness = tex2D(_MetallicGlossMap, IN.uv_MetallicGlossMap).a;
85+
}
86+
87+
ENDCG
88+
}
89+
}

0 commit comments

Comments
 (0)