Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
5
Question by Douvantzis · Apr 21, 2016 at 01:23 PM · shaderblendingalpha-channel

Unity's default shaders' blending is messed up?

alt text

When you have to deal with rendering transparency correctly and specially when using a renderTexture, it is crucial to perform correct blending based on pre-multiplying the RGB with the alpha channel. More on that here.

As a general rule, the blending mode for alpha channel should ALWAYS be: One OneMinusSrcAlpha. The blending used for RGB should either be 'One OneMinusSrcAlpha' or 'SRC_ALPHA OneMinusSrcAlpha', depending on whether the source is alpha pre-multiplied or not.

Let's see what unity does. The standard sprite shader uses One OneMinusSrcAlpha for RGB and alpha and premultiplies the rgb with the alpha in the fragment shader. This produces correct RGB and alpha values.

However, the Unlit-Transparent shader uses SrcAlpha OneMinusSrcAlpha which results in wrong alpha values. If you render on a solid surface and simply don't care about the resulting alpha, you won't notice the problem. If you render on a renderTexture and then render this on screen, you will notice the problem.

I have created a simple unity project that demonstrates the issue by rendering 2 semi-transparent grey sprites on a render texture. You can find it here and go through the readme file..

Also, when you render the renderTexture the rgb values are already premultiplied. Thus you want to use One OneMinusSrcAlpha for rgb. I had to create a "premultiplied" iteration of the standard sprite shader to make this work.

So, my question is if I am missing something and Unity provides a proper way to deal with this or you have to be really careful when dealing with transparency and adjust the shaders yourself?

untitled-2.png (3.8 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · May 16, 2016 at 06:28 PM

I'm not sure what the problem is? The built-in shaders provide a range of examples... if they don't behave the way you want to (and it sounds like you have already identified the way you would prefer), then you just make your own shader.

Comment
Add comment · Show 4 · 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 Douvantzis · May 17, 2016 at 10:04 AM 0
Share

The problem is that if you want to render something with transparency on a render texture and render that on screen, you won't get it right by default. I think something as trivial as this, should not be that easy to do wrong (see this question caused by wrong blending modes).

Also I don't understand why unity chose "srcAlpha One$$anonymous$$inusSrcAlpha" blending mode for both RGB and alpha in many shaders, as I can't think of an example where you want to blend the alpha channel this way. Unless of course, this is faster than specifying different blending modes for RGB and alpha.

avatar image tanoshimi Douvantzis · May 17, 2016 at 11:16 AM 0
Share

If you want to propose a change to the default shader blend mode, you should log this at https://feedback.unity3d.com/

avatar image Douvantzis · May 17, 2016 at 01:00 PM 0
Share

Thank's for the idea @tanoshimi . I posted my feedback: https://feedback.unity3d.com/suggestions/proper-blending-modes-regarding-alpha-channel

avatar image Jovssen · Feb 14, 2017 at 02:18 PM 1
Share

I have been learning shaders for use with rendertextures, and as you said, with the rendertextures it became apparent something was wrong in the shaders. I had been using the docs to modify the default sprite shader, and in them it says SrcAlpha One$$anonymous$$inusSrcAlpha is "Traditional transparency", but that went against my understanding of colors, and it took a lot of being puzzled, and having to check out wikipedia's alpha compositing math, to confirm SrcAlpha One$$anonymous$$inusSrcAlpha is incorrect. https://docs.unity3d.com/$$anonymous$$anual/SL-Blend.html If it's more efficient the wrong way, at least the docs shouldn't be confusing and call it "Traditional transparency", if it's just a tradeoff for speed. :/

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Transparency and blending issues with custom-made shader 1 Answer

Blending normal texture and texture made with shader 0 Answers

shader cg blending two textures 1 Answer

Blending parts of texture on 3d object with skinned mesh 0 Answers

Intersection / Geometry blend shader graph problem 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