"Unity Shaders" alpha Test and Alpha Blending

Source: Internet
Author: User


write in front


The question of Alpha has always been a more confusing thing, especially when it comes to translucency, and I don't know why A is blocking B, and B is in front of a. This article summarizes what I now know ~


The alpha test and alpha blending are two methods of processing transparency.


Alpha Test uses a very overbearing and extreme mechanism, as long as a pixel of alpha does not meet the conditions, then it will be fragment shader discard, "I do not want your class!" Otherwise, it will be written to the cache normally, with normal depth checking, and so on, i.e.Alpha test does not need to close zwrite . Alpha Test produces extreme results, either completely transparent, invisible, or completely opaque.


The Alpha Blending is a way of moderation, which uses the current fragment alpha as a blending factor to mix the previously written color values in the cache. But the problem with Alpha blending is that it needs to close zwrite and be very careful about the order in which objects are rendered. If the zwrite is not turned off, then the object behind it can be seen through the depth test, but it is removed because the depth is greater than its depth, so we cannot see the object behind it. Therefore, we need to make sure that the object's rendering order is from back to forward and closes the zwrite of the translucent object.



Surface Shader


Implementing both of these techniques is very simple in unity's surface shader, as described in the previous article-alpha Test and Alpha Blending. A simple summary is, as long as the #pragma set alphatest:_cutoff or Alpha command.


But what is the rationale behind them? That starts with the vertex & Fragment shader they generate. So, take a look at the next section ~



Vertex & Fragment Shader


Let's start with a simpler Alpha Test.


In Vertex & Fragment shader, it's very easy to implement it.


One way is to write your own code in shader, as long as you use a statement like this:

  Alpha Test  Clip (O.alpha-_cutoff);

ClipThe function is very simple to check whether its parameters are less than 0. If it is, callDiscardAbandon the fragment, or leave it alone.


Another approach is to use the alphatest directive for a fixed pipeline. The official documentation is visible. Use alphatest The method of instruction chooses more, we not only judge it to abandon the fragment when it is less than _cutoff , can also be judged whether it is greater than, is greater than equals, and so on. But the principle is the same as the first method, in the final analysis is to rely on the discard function to abandon the fragments that does not meet the criteria.


Alpha Blending is slightly more complicated because it involves some zwrite problems. It first needs to close the Zwrite:

Zwrite OFF

We can then specify the blending function, similar to the following:

Blend Srcalpha Oneminussrcalpha

These are the most common mixed function factors, and others can be found in the official documentation.


When using Alpha blending, it is important to be careful about the problems that result from shutting down the depth cache. From this picture of unity, you can see:



The depth test is done behind the vertex shader, so in the fragment shader phase, because it shuts down the depth cache, the overlap of pixels depends entirely on the order in which they are rendered.


Of course, sometimes we can mix these two techniques, for example, the first pass uses Alpha test to render the solid part, and the second pass uses the alpha blending to render the fragment in the previous pass .



Performance


In the official document of unity, there are two references to their performance issues-one that is performance hints when writing shaders, and one that optimizes image performance. Slightly feel the two pages have a lot of repetition, one day in the future may be composed of a page ...


That's how they write:

      Fixed function alphatest or it ' s programmable equivalent, clip () , have different performance characteristics on different platforms:

    • Generally it's a small advantage to use it for cull out totally transparent pixels on the most platforms.
    • However, on PowerVR GPUs found in IOS and some Android devices, alpha testing is expensive. Don't try to use it as "performance optimization" there, it'll be slower.

And

Keep in mind that Alpha test (discard) operation'll make your fragments slower.


To summarize, it may seem simpler to use alpha test, but on most platforms there is only a little bit of performance improvement over the alpha Blending. But!!! On iOS and some Android devices, because they use PowerVR GPUs, the performance of Alpha test is more expensive. Therefore, one caveat is to use alpha Blending as much as possible instead of using Alpha Test.


We would find it strange to not close the depth cache, do not need to calculate the mixed color, just call to discard discard fragment is not very simple? Why is it less efficient on a mobile platform? There is a saying, "Simple rough", can be used here. The reason for the decline in performance is that it's too rough! (I don't want to take it seriously ... )


Okay, back to the reason. Because of my limited experience, I can only rely on strong Google to find the answer. I found here, here. Summing up, is powervr GPUs uses a technique called Deferred tile-based-rendering. There is an optimization phase in this technique, that is, in order to reduce overdraw it will determine which tile will be truly rendered. However, due to the alpha Test in fragment shader used clip fragment fragments processing, so that the original can reduce overdraw is ineffective.


"Unity Shaders" alpha Test and Alpha Blending

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.