How to add a Alpha property to a HSV Shader

Hi Shader Ninja’s,

I am having troubles figuring out how to add a Alpha property to my existing HSV Shader.
As i am a newbie in terms of shader writing i would appreciate if someone could help me out here.

Many thanks in advance.

Here is my code:

Shader "Custom/HSVShaderBackup" {

    Properties {

        _MainTex ("Texture", 2D) = "white" {}

        _HueShift("HueShift", Float ) = 0

        _Sat("Saturation", Float) = 1

        _Val("Value", Float) = 1

    }

    SubShader {

 

        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType" = "Transparent" }

        ZWrite Off

        Blend SrcAlpha OneMinusSrcAlpha

        Cull Off

 

        Pass

        {

            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag

            #pragma target 2.0

 

            #include "UnityCG.cginc"

 

            float3 shift_col(float3 RGB, float3 shift)

            {

            float3 RESULT = float3(RGB);

            float VSU = shift.z*shift.y*cos(shift.x*3.14159265/180);

                float VSW = shift.z*shift.y*sin(shift.x*3.14159265/180);

                

                RESULT.x = (.299*shift.z+.701*VSU+.168*VSW)*RGB.x

                        + (.587*shift.z-.587*VSU+.330*VSW)*RGB.y

                        + (.114*shift.z-.114*VSU-.497*VSW)*RGB.z;

                

                RESULT.y = (.299*shift.z-.299*VSU-.328*VSW)*RGB.x

                        + (.587*shift.z+.413*VSU+.035*VSW)*RGB.y

                        + (.114*shift.z-.114*VSU+.292*VSW)*RGB.z;

                

                RESULT.z = (.299*shift.z-.3*VSU+1.25*VSW)*RGB.x

                        + (.587*shift.z-.588*VSU-1.05*VSW)*RGB.y

                        + (.114*shift.z+.886*VSU-.203*VSW)*RGB.z;

                

            return (RESULT);

            }

 

            struct v2f {

                float4  pos : SV_POSITION;

                float2  uv : TEXCOORD0;

            };

 

            float4 _MainTex_ST;

 

            v2f vert (appdata_base v)

            {

                v2f o;

                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

                return o;

            }

 

            sampler2D _MainTex;

            float _HueShift;

            float _Sat;

            float _Val;

 

            half4 frag(v2f i) : COLOR

            {

                half4 col = tex2D(_MainTex, i.uv);

                float3 shift = float3(_HueShift, _Sat, _Val);

                

                return half4( half3(shift_col(col, shift)), col.a);

            }

            ENDCG

        }

    }

    Fallback "Particles/Alpha Blended"

}

add into your properties:

_Alpha ("Alpha", Range(0,1)) = 1

then add along with your other user variables:

float _Alpha;

then replace your frag function result for:

return half4( half3(shift_col(col, shift)), col.a * _Alpha);

this should give you a slider to control the alpha

Thanks so much for your help “kebrus”.
Worked like a charm. You are a Ninja. :wink:

Hi Cybertiger.

Could you tell where did you get that HSV-correct technique from? I’d like to read about it to study where all these “magic numbers” come from.
I’ve already seen several methods for shifting hue/saturation: one, two, three, four.
And each of them tells their method is the fastest.

But from what I see in your code, looks like it could be the best of all, since it only uses simple operations: add, multiply, divide, subtract…
Almost only. The one thing that concerns me is sin and cos functions.

So could you tell which of these methods is faster? And maybe also tell which of them could be better depending on the platform (PC / Android / iOS).

Hello @Lex-DRL ,
I honestly can’t remember where i got that shader from. I think i googled it and picked the first one i found.
I have tested the shader already on mobile and i can tell you already that it is okay to use for small elements but if there are many pixels covering using this shader it will choke.

Hope this helps

1 Like