This site uses strictly necessary cookies. More Information

X- Home /

# HSV Shader with Alpha

Hi there, Can some of you Shader Coding Ninja's please help me with my problem. I have been looking around and just can't figure it out by myself. Basically i want to add a Alpha property to the shader below which is a HSV Shader which i found in the forums. Many thanks in advance to you Ninja's out there.

```
Shader "Custom/HSVShader" {
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"
}
```

**Answer** by reefwirrax
·
Dec 19, 2013 at 09:16 PM

Hi,

you need to make a new properties line called alpha at top, then after include line write:

float alpha;

and replace col.a at the end with word alpha

this is a better hsv fct btwm, sane thing hsb

float3 Hue(float H) { half R = abs(H * 6 - 3) - 1; half G = 2 - abs(H * 6 - 2); half B = 2 - abs(H * 6 - 4); return saturate(half3(R,G,B)); } half4 HSVtoRGB(in half3 HSV) { return half4(((Hue(HSV.x) - 1) * HSV.y + 1) * HSV.z,1); }

Thanks for your effort but unfortunately this still doesn't work for me. It just turns everything transparent. Could you please help me to find out what is wrong here? Below is the new shader with your changes you suggested.

**Answer** by Cybertiger
·
Dec 20, 2013 at 02:22 PM

Here is my Final Shader. I got help in the Unity forums:

```
Shader "Custom/HSVShader" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_HueShift("HueShift", Float ) = 0
_Sat("Saturation", Float) = 1
_Val("Value", Float) = 1
_Alpha ("Alpha", Range(0,1)) = 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"
float _Alpha;
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 * _Alpha);
}
ENDCG
}
}
Fallback "Particles/Alpha Blended"
}
```

hey, sorry i took a while to reply. Did you attempt to set the alpha manually to 0.5 to see if it is half transparent, to be certain that the Blend SrcAlpha One$$anonymous$$inusSrcAlpha is the right one?

first task is to verify Blend SrcAlpha One$$anonymous$$inusSrcAlpha

by using an alpha value of .5

because windows 7 doesnt let me search my .shader file content i cant find the file where i learnt this, i thought i had posted a question about it... 1 monebnt

anyways i learnt from the above video, i had to use a different than Blend option jesse advised as it went invisible/grey/inverse, i tried all combinations i cant find my code

Thanks for you effort reefwirrax. I got help now in the unity forums and will post the final HSV shader with working Alpha property below. Thanks for you help

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Shader that does per Vert alpha with co 0 Answers

shader problem 0 Answers

Z-priming alpha-per-vertex 0 Answers

Shader with multiple alpha textures, single color for each texture, how to? 1 Answer

Transparent terrain shader not working 2 Answers