Unity Shaders and Effects Cookbook (6-2) Transparent clipping shader

Source: Internet
Author: User

In the previous section, the RGB channel value of the texture is used as the grayscale to control the transparency of the pixel, which is called a semitransparent shader.

This section learns the transparent clipping shader, still reads the RGB channel value, assigns it to Alpha, and then assigns a fixed value as the ruler, and if the Alpha < ruler discards the fragment, that is, the cut is not displayed.

This time the effect of the implementation is as



Suitable as a monster to kill the effect.

Transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn

Start learning below.

Set up a good scene, new material, new shader.

Shader complete code is as follows:

Shader "Cookbookshaders/chapt6-2/cutoff" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_cutoff ("Cut Off", Range ( 0,1)) =0}subshader {Tags {"rendertype" = "Opaque"}lod 200cgprogram#pragma surface surf Lambert alphatest:_ Cutoffsampler2d _maintex;struct Input {float2 uv_maintex;}; void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb;o. Alpha = C.R;} ENDCG} FallBack "Diffuse"}

Relatively simple, there are two main points:

1. Alphatest:_cutoff is specified in the #pragma statement

#pragma surface surf Lambert alphatest:_cutoff

When Alphatest:_cutoff is specified, our shader becomes a transparent clipping shader. When the Alpha value of the fragment < _cutoff, the fragment is discarded.


2. Assign the channel value to O.alpha

O.alpha = C.R;


According to the above, I thought the workflow was like this.

Float _cutoff=0.5o. Alpha = texture. Rif (O.alpha < _cutoff) {DisCard ()}

But looking at unity-generated shader code discovery is using the Clip () function


This sentence in the box

  Alpha Test  Clip (O.alpha-_cutoff);

Clip (x): If any element in the input vector is less than 0, the current pixel is discarded.


That is, the specified _cutoff is subtracted from the alpha value, that is,fragments with an alpha value lower than _cutoff will be discarded .


According to the book, the study of the transparent clipping shader, because the pixel to determine whether Alpha is less than _cutoff, so it is more GPU-intensive, so the phone is not recommended.

But in the work, it will be found that in fact, to a large extent, is a degree of problem.

Like this dissolution effect, used in a copy, it is not possible to kill several dozen strange at the same time and show the effect of dissolution.

So there is nothing on the phone that can not be used with this kind of thing. Use it if you want.


Sample Project Package Download:

http://pan.baidu.com/s/1nvzpn2x


Unity Shaders and Effects Cookbook (6-2) 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.