Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
7
Question by nomadic · Apr 26, 2012 at 05:16 AM · shaderalphasurface shaderhuehsbcolor

Shader Help: Adding transparency to a shader.

I have been trying to make a shader that allows me to rotate the hue of a texture. I found one posted by gnoblin on this thread that performs that function perfectly. However as it is it does not support an alpha channel. I tried merging lines from some transparent shaders but in my ignorance had no luck.

If anyone can help me out or point me to some learning resources, I'd really appreciate it!

The shader I'm working with below:

 Shader "HSB_HSV_Colorpicker" {

 Properties {

   _MainTex ("Texture", 2D) = "white" {}
   _HueShift("HueShift", Float) = 0
 }

 SubShader {

   Tags { "RenderType" = "Opaque" }

   CGPROGRAM

   #pragma surface surf Lambert
   #pragma target 3.0


     float3 rgb_to_hsv_no_clip(float3 RGB)

     {

             float3 HSV;
        

      float minChannel, maxChannel;

      if (RGB.x > RGB.y) {

       maxChannel = RGB.x;
       minChannel = RGB.y;

      }

      else {

       maxChannel = RGB.y;
       minChannel = RGB.x;

      }

      

      if (RGB.z > maxChannel) maxChannel = RGB.z;

      if (RGB.z < minChannel) minChannel = RGB.z;
        

             HSV.xy = 0;

             HSV.z = maxChannel;

             float delta = maxChannel - minChannel;             //Delta RGB value

             if (delta != 0) {                    // If gray, leave H & S at zero

                HSV.y = delta / HSV.z;

                float3 delRGB;

                delRGB = (HSV.zzz - RGB + 3*delta) / (6.0*delta);

                if      ( RGB.x == HSV.z ) HSV.x = delRGB.z - delRGB.y;

                else if ( RGB.y == HSV.z ) HSV.x = ( 1.0/3.0) + delRGB.x - delRGB.z;

                else if ( RGB.z == HSV.z ) HSV.x = ( 2.0/3.0) + delRGB.y - delRGB.x;

             }

             return (HSV);

     }

 

     float3 hsv_to_rgb(float3 HSV)
     {

             float3 RGB = HSV.z;

        

                float var_h = HSV.x * 6;

                float var_i = floor(var_h);   // Or ... var_i = floor( var_h )

                float var_1 = HSV.z * (1.0 - HSV.y);

                float var_2 = HSV.z * (1.0 - HSV.y * (var_h-var_i));

                float var_3 = HSV.z * (1.0 - HSV.y * (1-(var_h-var_i)));

                if      (var_i == 0) { RGB = float3(HSV.z, var_3, var_1); }

                else if (var_i == 1) { RGB = float3(var_2, HSV.z, var_1); }

                else if (var_i == 2) { RGB = float3(var_1, HSV.z, var_3); }

                else if (var_i == 3) { RGB = float3(var_1, var_2, HSV.z); }

                else if (var_i == 4) { RGB = float3(var_3, var_1, HSV.z); }

                else                 { RGB = float3(HSV.z, var_1, var_2); }

        

        return (RGB);

     }

 

   struct Input {

       float2 uv_MainTex;

   };

  

   sampler2D _MainTex;

   float _HueShift;

  

   void surf (Input IN, inout SurfaceOutput o) {

       o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    

       float3 hsv = rgb_to_hsv_no_clip(o.Albedo.xyz);

       hsv.x+=_HueShift;
              

       if ( hsv.x > 1.0 ) { hsv.x -= 1.0; }

       o.Albedo = half3(hsv_to_rgb(hsv));

 

   }

  

   ENDCG

 }

 Fallback "Diffuse"

}

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
33
Best Answer

Answer by nomadic · May 10, 2012 at 02:48 AM

Success! Alright, I feel silly for posting this only to work it out myself. But I learned a ton about shaders in the process (mostly thanks to great Unity/community resources).

For anyone who is trying to Write a Surface Shader with Semi-Transparency take note of these lines:

.

I changed:

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


to:

 _MainTex ("Color (RGB) Alpha (A)", 2D) = "white"
 Tags { "Queue"="Transparent" "RenderType"="Transparent" }

.

Appended "alpha":

 #pragma surface surf Lambert alpha

.

Added this line after o.Albedo...

 o.Alpha = tex2D (_MainTex, IN.uv_MainTex).a;

.

Seems pretty obvious in hindsight, but I hope this helps someone!

Note also that this shader lets you "rotate" the hue value of a texture.

Comment
Add comment · Show 11 · 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 LeoJr · Feb 28, 2013 at 10:00 AM 0
Share

omg ty <3

avatar image LantsInPance · Jun 29, 2013 at 05:24 AM 0
Share

It definitely helped me! Thank you so much!

avatar image Baykush · Oct 07, 2013 at 03:39 PM 0
Share

Woah! That was great. Thanks for the tip!

avatar image FuzzyQuills · Mar 09, 2014 at 11:30 PM 0
Share

Hmm... what do you mean by rotating a hue? just curious, and thanks for pointing this out. this isn't actually detailed in the documentation, so again, well done for donating your two cents!

avatar image ManuA · Feb 17, 2017 at 10:35 PM 1
Share

Thank you very much! From the bottom of my heart! I am crying of joy!

Show more comments
avatar image
0

Answer by Nikolat93 · Nov 24, 2019 at 07:16 PM

Thanks for succinct guide, got it up and running in a few minutes with zero shader experience

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Alpha Shader not working with obj collision 1 Answer

Luminance to Alpha 0 Answers

transparent mode does not work at alpha=255 1 Answer

Glass Shader with Strumpy Shader Editor 1 Answer

About shader! 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges