Unity Shaders transparency--using the alpha channel to create a transparency effect

Source: Internet
Author: User

This series is the main reference to Unity Shaders and Effects Cookbook (thanks to the original book author), at the same time will add a little personal understanding or expansion.

Here are all the illustrations of this book. Here is the code and resources required for this book (and of course you can download it from the website).

========================================== Cutting Line ==========================================



write in front



From this beginning is a whole new chapter: Transparency effect (Transparency). Before the creation of the logo flash effect when the background is not clear. It is at that time decided to learn the basic knowledge of shader, not to become what a great God. Just look at the internal principle of rendering a little bit of understanding ~


Start the text.


As we learn how to write transparent surface shader, we need to understand what code should be included so we can turn on transparency.

Once again, Unity provides us with some built-in references that we can use to include these parameters to achieve high-speed transparency.


This is done by adding an alpha parameter to the shader #pragma declaration. This statement tells Unity that we want to use transparency in shader. But when we create transparent shaders, we need to think about things in detail. That's the order in which the elements in the code are drawn. This article will tell you some basic questions to get a very easy transparent object.

Other issues relating to transparency will be explained in the following chapters.



preparatory work



As ever, we need to create a new scene.

    1. Create a new scene, add a parallel light, and a sphere (Sphere).

    2. Create a new shader and a new material. can be named Simplealpha. After assigning the shader to material, the material is assigned to the sphere in the first step.
    3. Finally, we need a decal as a control of privacy to control which parts are transparent. Which parts are opaque.
It's the map we used. This map only includes pure RGB and white (not in the own resource.) Able to draw it yourself, very easy). We use its RGB channel as a value of 0 or 1 of the transparency value.

Implement
This piece of shader is very easy.


  1. Adding a new property to the Properties block allows us to control transparency globally.
    Properties {_maintex ("Base (RGB)", 2D) = "White" {}_transval ("Transparency Value", Range (0,1)) = 0.5}
  2. To change the render queue:
    Tags {"Queue" = "Transparent"}

    explanation : This step is very important, the original book does not include this sentence is actually wrong.

  3. We then add a new parameter to the #pragma declaration: Alpha.
    Cgprogram#pragma Surface Surf Lambert Alpha

    explanation : Explain again the meaning of the above statement. Using the surface function named surf, use the built-in Lambert illumination function. Turn on transparent channels.



  4. Finally, add the code that controls transparency to the surf () function.
    void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb;o. Alpha = C.b * _transval;}

complete code such as the following:
Shader "Custom/simplealpha" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_transval ("Transparency Value", Range (0, 1) = 0.5}subshader {Tags {"rendertype" = "Opaque" "Queue" = "Transparent"}lod 200cgprogram#pragma surface surf Lambert Alph Asampler2d _maintex;float _transval;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.b * _transval;} ENDCG} FallBack "Diffuse"}


assume that there is no transparency effect. For example, see the following:
o.alpha = C.G * _ Transval, 

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2fuzhljyxqxotky/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>



Explain
You can see. It's easy to use unity's surface shaders to get a transparent effect.

This type of shader relies on three elements: Shader render queue is set correctly, #pragma In the DeclarationAlphaThe number of references, and theSurfaceoutputIn the structureAlphaValue.
Tags { "Queue"="Transparent " A step will determine the correct rendering relationship between a translucent object and an opaque object, assuming that it is not set correctly, then it is very likely that the object behind it will appear in front of the transparent object. Detailed explanations can be found in this article.
Once we are in #pragma the statement addedAlphaThe number of references. This tells Unity: Hey, next thing you have to agree with me is to render a transparent surface. Then, we just need to use a value range of 0 to 1 per pixel to populate the O in the surfaceoutput structure. Alpha values.

From a color perspective (where the color refers to a grayscale value, because transparency can be expressed as a single-channel grayscale value), a 1 transparency, or white, will produce a completely opaque effect. The 0 value, or black, represents a completely transparent effect.
This explains the effect of the above.

For example, when we use a statement such as the following to control transparency:

O.alpha = C.R * _transval

In addition to the red and white portions of the map (the RGB channel value of white is 1) its R channel value is 1. The remaining (green and blue parts) are 0. So only the red and white parts are opaque.


Although there is a great many things about transparency, we need to know that the above is the main realization.

In the following chapters, we will start with how to use alpha channels or translucent shader in real-time rendering.



Unity Shaders transparency--using the alpha channel to create a transparency effect

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.