Last active
February 12, 2017 23:41
-
-
Save eleanor-em/9814c25fa43a8e820ca68fb4eaad8a9c to your computer and use it in GitHub Desktop.
Saturation Shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Shader "Sprites/Default" | |
| { | |
| Properties | |
| { | |
| [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {} | |
| _Saturation("Saturation", Range(0, 1)) = 1 | |
| } | |
| SubShader | |
| { | |
| Tags | |
| { | |
| "Queue" = "Transparent" | |
| "IgnoreProjector" = "True" | |
| "RenderType" = "Transparent" | |
| "PreviewType" = "Plane" | |
| "CanUseSpriteAtlas" = "True" | |
| } | |
| Cull Off | |
| Lighting Off | |
| ZWrite Off | |
| Blend One OneMinusSrcAlpha | |
| Pass | |
| { | |
| CGPROGRAM | |
| #pragma vertex vert | |
| #pragma fragment frag | |
| #pragma target 2.0 | |
| #include "UnityCG.cginc" | |
| struct appdata_t | |
| { | |
| float4 vertex : POSITION; | |
| float4 color : COLOR; | |
| float2 texcoord : TEXCOORD0; | |
| UNITY_VERTEX_INPUT_INSTANCE_ID | |
| }; | |
| struct v2f | |
| { | |
| float4 vertex : SV_POSITION; | |
| fixed4 color : COLOR; | |
| float2 texcoord : TEXCOORD0; | |
| UNITY_VERTEX_OUTPUT_STEREO | |
| }; | |
| float _Saturation; | |
| // Source: http://www.chilliant.com/rgb2hsv.html | |
| // HSV to RGB | |
| float3 HUEtoRGB(in float H) | |
| { | |
| float R = abs(H * 6 - 3) - 1; | |
| float G = 2 - abs(H * 6 - 2); | |
| float B = 2 - abs(H * 6 - 4); | |
| return saturate(float3(R, G, B)); | |
| } | |
| float3 HSVtoRGB(in float3 HSV) | |
| { | |
| float3 RGB = HUEtoRGB(HSV.x); | |
| return ((RGB - 1) * HSV.y + 1) * HSV.z; | |
| } | |
| // RGB to HSV | |
| float Epsilon = 1e-10; | |
| float3 RGBtoHCV(in float3 RGB) | |
| { | |
| // Based on work by Sam Hocevar and Emil Persson | |
| float4 P = (RGB.g < RGB.b) ? float4(RGB.bg, -1.0, 2.0 / 3.0) : float4(RGB.gb, 0.0, -1.0 / 3.0); | |
| float4 Q = (RGB.r < P.x) ? float4(P.xyw, RGB.r) : float4(RGB.r, P.yzx); | |
| float C = Q.x - min(Q.w, Q.y); | |
| float H = abs((Q.w - Q.y) / (6 * C + Epsilon) + Q.z); | |
| return float3(H, C, Q.x); | |
| } | |
| float3 RGBtoHSV(in float3 RGB) | |
| { | |
| float3 HCV = RGBtoHCV(RGB); | |
| float S = HCV.y / (HCV.z + Epsilon); | |
| return float3(HCV.x, S, HCV.z); | |
| } | |
| v2f vert(appdata_t IN) | |
| { | |
| v2f OUT; | |
| UNITY_SETUP_INSTANCE_ID(IN); | |
| UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT); | |
| OUT.vertex = UnityObjectToClipPos(IN.vertex); | |
| OUT.texcoord = IN.texcoord; | |
| OUT.color = IN.color; | |
| return OUT; | |
| } | |
| sampler2D _MainTex; | |
| sampler2D _AlphaTex; | |
| fixed4 SampleSpriteTexture(float2 uv) | |
| { | |
| fixed4 color = tex2D(_MainTex, uv); | |
| return color; | |
| } | |
| fixed4 frag(v2f IN) : SV_Target | |
| { | |
| fixed4 c = SampleSpriteTexture(IN.texcoord) * IN.color; | |
| float3 inHSV = RGBtoHSV(c); | |
| inHSV.y *= _Saturation; | |
| half4 outRGB = half4(HSVtoRGB(inHSV), c.a); | |
| outRGB.rgb *= outRGB.a; | |
| return outRGB; | |
| } | |
| ENDCG | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment