Unity3d Shaderlab Transparent clipping shader

Source: Internet
Author: User

Unity3d Shaderlab Transparent clipping shader

In the previous article, we introduced the use of Alpha to achieve transparency, in fact , Unity for our #pragma provides another parameter, we can make more efficient Simple transparent effect, that is, clipping transparent.

This transparency uses a value to simply control that certain pixels do not need to be rendered to the screen, so we can also implement a shader that is either completely transparent or completely impervious.

We're going to use the value of grayscale to control the transparency of the material.

Preparation or new Shader Material, a grayscale change chart. The same code is done in minutes, see complete:

Shader"91ygame/transparentcutoff"{Properties {_maintex ("Base (RGB)", 2D) =" White" {}        //affirm the _cutoff variable;_cutoff ("Cutoff Value", Range (0.01,1))=0.5} subshader {Tags {"Rendertype"="Opaque"} LOD $                //tell the shader what we need is a clipping type of shader;Cgprogram#pragmaSurface Surf Lambert Alphatest:_cutoffsampler2d _maintex; structInput {float2 Uv_maintex;        }; voidSurf (Input in, InOut surfaceoutput o) {half4 c=tex2d (_maintex, In.uv_maintex); O.albedo=C.rgb; O.alpha=C.R; } ENDCG} FallBack"Diffuse"}

Go back to Unityand see how it works.

Change The effect of Cutoff Value.

The clipping shader above tries a new #pragma parameter called alphatest: The parameter name "Here we define the Alphatest:_cutoff", This makes the shader instantly become a simple transparent shader.

Unlike the previous transparency, it does not affect the global Alpha , the clipping shader has a effect on the transparency of each grayscale value in the range from white to black, but does not change every grayscale value.

just through our definition of _cutoff variable values to determine transparency. This means that if we re-set the _cutoff value of 0.5, then the gray value below 0.5 will be set to transparent, the inverse is greater than the 0.5 is considered opaque.

So when the grayscale value is calculated this way, only transparent and opaque 2 options are available.

This transparency is more advantageous than for screen performance, because the blending process for processing semitransparent shaders is more expensive than clipping transparency.

Mobile devices, on the other hand, have a very heavy burden on the phone's CPU as each pixel of the detection map, so you can use the translucent technology directly on the phone and use the clipping transparency technique with caution.

Unity3d Shaderlab Transparent clipping shader

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.