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
2
Question by ravsters · Nov 28, 2012 at 06:37 AM · shadertextureprogrammingplanet

How to add transparency to a shader!?

I want to make it so that this shader can read the alpha properties from a texture and apply them. I cant seem to get it to work. :( The shader is below. Please help!

 Shader "Planet" {
     Properties {
       _MainTex ("Diffuse(RGB) Spec(A)", 2D) = "white" {}
       _BumpMap ("Bumpmap", 2D) = "bump" {}
       _RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
       _RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
       _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
    _Shininess ("Shininess", Range (0.01, 1)) = 0.078125 
     }
     SubShader {
       Tags { "RenderType" = "Opaque" }
       CGPROGRAM
       
        #pragma surface surf SimpleSpecular
    float _Shininess;
    
       half4 LightingSimpleSpecular (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
           half3 h = normalize (lightDir + viewDir);
           half diff = max (0, dot (s.Normal, lightDir));
           float nh = max (0, dot (s.Normal, h));
           float spec = pow (nh, 48.0);
           half4 c;
           c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec * s.Alpha * _Shininess * _SpecColor) * (atten * 2);
           c.a = s.Alpha;
           return c;
       }
             
       struct Input {
           float2 uv_MainTex;
           float2 uv_BumpMap;
           float3 viewDir;
       };
       sampler2D _MainTex;
       sampler2D _BumpMap;
       float4 _RimColor;
       float _RimPower;
       
       void surf (Input IN, inout SurfaceOutput o) {
           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
           o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
           half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
           o.Emission = _RimColor.rgb * pow (rim, _RimPower);
           o.Alpha = tex2D (_MainTex, IN.uv_MainTex).a;
       }
       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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Tim-Michels · Nov 28, 2012 at 07:30 AM

I took a look at your shader and added alpha to it. I also optimized the fact that you sample your texture twice, once for rgb and once for your alpha. It's better to store the tex2D in a fixed4, so you only need to sample your texture once.

the 'alpha' keyword was added and the RenderType was set to 'Transparent'

Here's your new shader:

 Shader "Planet" {
     Properties {
       _MainTex ("Diffuse(RGB) Spec(A)", 2D) = "white" {}
       _BumpMap ("Bumpmap", 2D) = "bump" {}
       _RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
       _RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
       _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
    _Shininess ("Shininess", Range (0.01, 1)) = 0.078125 
     }
     SubShader {
       Tags { "RenderType" = "Transparent" }
       CGPROGRAM
 
        #pragma surface surf SimpleSpecular alpha
    float _Shininess;
 
       half4 LightingSimpleSpecular (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
           half3 h = normalize (lightDir + viewDir);
           half diff = max (0, dot (s.Normal, lightDir));
           float nh = max (0, dot (s.Normal, h));
           float spec = pow (nh, 48.0);
           half4 c;
           c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec * s.Alpha * _Shininess * _SpecColor) * (atten * 2);
           c.a = s.Alpha;
           return c;
       }
 
       struct Input {
           float2 uv_MainTex;
           float2 uv_BumpMap;
           float3 viewDir;
       };
       sampler2D _MainTex;
       sampler2D _BumpMap;
       float4 _RimColor;
       float _RimPower;
 
       void surf (Input IN, inout SurfaceOutput o) {
           fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
           o.Albedo = c.rgb;
           o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
           half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
           o.Emission = _RimColor.rgb * pow (rim, _RimPower);
           o.Alpha = c.a;
       }
       ENDCG
     } 
     Fallback "Diffuse"
   }

Feel free to mark as answered if this is what you need.

Cheers ;)

Comment
Add comment · Show 6 · 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 ravsters · Nov 28, 2012 at 07:51 AM 0
Share

NICE! Its almost perfect. The one thing that I was trying to avoid was the fact that when a texture with transparency is attached, the rim color only creates that rim glow where the texture is visible... When applied to a sphere, only the visible parts are glowing around the edge, but I wanted that whole sphere to be have that glow.. so if would be a circle.. not just the visible parts. I hope this makes sense. If so, is it possible?

avatar image Tim-Michels · Nov 28, 2012 at 07:54 AM 1
Share

Change the line o.Alpha = c.a; to o.Alpha = c.a + rim; I hope that's the result you're going for...

avatar image ravsters · Nov 28, 2012 at 07:59 AM 1
Share

O$$anonymous$$G YESSSSS! YOU ARE A GODSEND! Thank you so much!!!!! <3 $$anonymous$$y birthday is in 1 $$anonymous$$ute.. I'll take this as an early b-day present! ;) Thanks again!!!!

avatar image ravsters · Nov 28, 2012 at 05:28 PM 0
Share

So, I ran into a few problems. In my scene, I have two objects. The earth and the atmosphere. The atmosphere is bigger than the earth object. I'm applying this shader to the atmosphere (which needs opacity for the clouds) but for some reason it doesn't show. I also have a moon object in the scene so I tried applying the shader to that as well (just to test) and for some reason when the moon travels over an object with a regular diffuse shader, it disappears. Any reason as to what this happens? Is there a fix for it?

avatar image Tim-Michels · Nov 29, 2012 at 07:41 AM 1
Share

Try changing the line

Tags { "RenderType" = "Transparent" }

to

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

Show more comments

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Get a UV texture of a models surface 2 Answers

Blending one material/texture/shader to another 1 Answer

What's the syntax for creating a skybox with a script? 2 Answers

CRT/LCD screen shader (for materials) 4 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