• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cybertiger · Dec 19, 2013 at 07:39 PM · shaderalpha

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"
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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); }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cybertiger · Dec 20, 2013 at 07:41 PM 0
Share

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.

avatar image
0

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"
 
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image reefwirrax · Dec 29, 2013 at 07:30 PM 0
Share

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 OneMinusSrcAlpha is the right one?

first task is to verify Blend SrcAlpha OneMinusSrcAlpha

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

avatar image reefwirrax · Dec 29, 2013 at 07:35 PM 0
Share

https://www.google.fr/search?q=blend+ios+unity&oq=blend+ios+unity&aqs=chrome..69i57.9780j0j4&sourceid=chrome&espv=210&es_sm=93&ie=UTF-8#es_sm=93&espv=210&psj=1&q=blend+ios+unity&tbm=vid

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

avatar image Cybertiger · Dec 29, 2013 at 09:06 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing material color´s alpha in self iluminated shader 1 Answer

How should I add an alpha mask AND an alpha multiplier into a shader? 0 Answers

Is there a way to create an fading alpha-cutoff effect? 0 Answers

Custom shader of sprite results in black alpha. 0 Answers

Making a cutout semi-transparent? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges