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
1
Question by knowbuddy · Aug 27, 2013 at 07:31 PM · shaderrendertexturealphatransparent

Transparent shader: final alpha value wrong

I'm currently working on a cloud system which shows near clouds as meshes and far clouds rendered into a skybox. The problem is, that the clouds being rendered to the RenderTexture have a different alpha value.

This is how a cloud looks like when being rendered to the screen:

alt text

I would expect the alpha channel to look something like the rendered cloud but it's not:

alt text

This is the shader I'm using:

 Shader "Custom/CloudShader"
 {
     Properties
     {
         _MainTex("Base (A)", 2D) = "white" {}
         _Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
     }
     
     SubShader
     {
         Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
         Blend SrcAlpha OneMinusSrcAlpha
         Cull Off
         ZWrite Off
         LOD 200
  
         CGPROGRAM
         #pragma surface surf NoLighting
         
         fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
         {
             fixed4 c;
             c.rgb = s.Albedo; 
             c.a = s.Alpha;
             return c;
         }
         
         
         uniform sampler _MainTex;
         uniform fixed4 _Color;
  
         struct Input
         {
             float2 uv_MainTex;
             float3 viewDir;
             float4 color : COLOR;
             float3 worldPos;
         };
  
         void surf (Input IN, inout SurfaceOutput o)
         {
             const float FADEOUT = 20;
             float dist = distance(_WorldSpaceCameraPos, IN.worldPos);
             float factor = saturate(dist / FADEOUT);
         
             o.Albedo = _Color.rgb * IN.color;
             o.Alpha = _Color.a * tex2D (_MainTex, IN.uv_MainTex).a;
             
             o.Alpha *= factor * saturate(abs(dot(o.Normal, normalize(IN.viewDir))) - 0.3);            
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }
     

It seems the alpha value rendered to the RenderTexture is different from the value on the screen. What would I have to do to get the same results?

(Using the grayscale for the alpha channel doesn't give me a correct result.)

clouds_rendered.jpg (21.8 kB)
clouds_alpha.jpg (19.6 kB)
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
4
Best Answer

Answer by knowbuddy · Aug 28, 2013 at 02:02 PM

I found the solution. Separate additive alpha blending does the trick:

 [...]
 Blend SrcAlpha OneMinusSrcAlpha, One One
 [...]
Comment
Add comment · Show 2 · 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 AKWolf · Jun 27, 2014 at 05:41 PM 0
Share

Just wanted to say thanks for this! I was dealing with the same problem on my cloud imposters and all I could find were old threads with no answers. Life saver!

avatar image dtaddis · Aug 26, 2020 at 10:26 AM 0
Share

7 years later, this was the answer I was looking for too (in my case for some AR shadows cast onto otherwise invisible planes). Thanks!

avatar image
0

Answer by Jona-Marklund · Aug 27, 2013 at 10:16 PM

Provided that you're trying to get the top image as your final result, this could work.

c.a = c.r + c.g + c.b;

Best Regards Jona

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 knowbuddy · Aug 28, 2013 at 12:08 PM 0
Share

Without a tint color your solution should work. But a dark color would also affect the transparency of the cloud...

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

17 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

Related Questions

[Unity5]how to change alpha by using shader. 1 Answer

Shader problem with 2D fake lighting 1 Answer

Unity Fog Shader with Alpha Change 0 Answers

Is it possible to tell unity not to render overlapping transparent objects after certain number of times? 1 Answer

Will Semi Transparent mesh with several folds of geometry sort correctly? 1 Answer


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