This tutorial covers a wide variety of common uses for transparent texture maps, and theI.e.rgba texture map contains a(Alpha) portion of the texture's opaque values.
It unites the shader Code'sconcept of the "textured sphere" section, and describes "cutaways" and "Transparency".
If you have not read this tutorial, it will be a good opportunity to read.
Discard Transparent fragments (discarding Transparent fragments)
Let's start by explaining the "cutaways" section of the discarded fragment. Follow these steps to describe the "Texture Spheres" section and assign the image to the left of the material ball according to the following shader :
Shader "Cg texturing with Alpha Discard" {Properties {_maintex ("RGBA Texture Image", 2D) = ' white ' {} _cut Off ("Alpha Cutoff", Float) = 0.5} subshader {Pass {cull Off//Since the front is partially trans Parent,//We shouldn ' t cull the back cgprogram #pragma vertex vert #pragma fragme NT Frag uniform sampler2d _maintex; Uniform float _cutoff; struct Vertexinput {float4 vertex:position; FLOAT4 texcoord:texcoord0; }; struct Vertexoutput {float4 pos:sv_position; FLOAT4 tex:texcoord0; }; Vertexoutput Vert (Vertexinput input) {vertexoutput output; Output.tex = Input.texcoord; Output.pos = Mul (UNITY_MATRIX_MVP, Input.vertex); return output; } float4 Frag (Vertexoutput input): COLOR {float4 Texturecolor = Tex(_maintex, Input.tex.xy); if (Texturecolor.a < _cutoff)//alpha value less than user-specified threshold? {discard;//Yes:discard This fragment} return texturecolor; } ENDCG}}//The definition of a fallback shader should be commented out//during development: Fallback "Unlit/transparent Cutout"}
The fragment shader reads the RGBA map and compares the alpha value to the user-specified range value size . If the Alpha value is smaller than the range value, the shaded fragment is discarded to make it transparent. Note: This directive is very slow on some platforms, especially on mobile devices. Therefore, mixing (blending) is usually more efficient.
MixedBlending)
The "Transparency" section describes how to use Alpha to mix and render translucent objects. In this code , the RGBA map is used:
Shader "Cg texturing with alpha blending" {Properties {_maintex ("RGBA Texture Image", 2D) = "white" {}} Su Bshader {Tags {"Queue" = "Transparent"} Pass {cull Front//First render the back faces Zwri Te Off//don ' t write to depth buffer//on order not to occlude other objects Blend Srcalpha Oneminus Srcalpha//blend based on the fragment ' s alpha value cgprogram #pragma vertex vert #pragma fragment Frag uniform sampler2d _maintex; Uniform float _cutoff; struct Vertexinput {float4 vertex:position; FLOAT4 texcoord:texcoord0; }; struct Vertexoutput {float4 pos:sv_position; FLOAT4 tex:texcoord0; }; Vertexoutput Vert (Vertexinput input) {vertexoutput output; Output.tex = Input.texcoord; Output.pos = Mul (UNITY_MATRIX_MVP, input.veRtex); return output; } float4 Frag (Vertexoutput input): COLOR {return tex2d (_maintex, input.tex.xy); } ENDCG} Pass {Cull back//Now render the front faces zwrite Off//don ' t write to Depth buffer//In order not to occlude other objects Blend Srcalpha oneminussrcalpha//b Lend based on the fragment ' s alpha value cgprogram #pragma vertex vert #pragma fragment Frag Uniform sampler2d _maintex; Uniform float _cutoff; struct Vertexinput {float4 vertex:position; FLOAT4 texcoord:texcoord0; }; struct Vertexoutput {float4 pos:sv_position; FLOAT4 tex:texcoord0; }; Vertexoutput Vert (Vertexinput input) {vertexoutput output; Output.tex = Input.texcoord; Output.pos = Mul (Unity_matrix_MVP, Input.vertex); return output; } float4 Frag (Vertexoutput input): COLOR {return tex2d (_maintex, input.tex.xy); } ENDCG}}//The definition of a fallback shader should be commented out//during development:/ /Fallback "Unlit/transparent"}
Not to be continued ....
by-----wolf96
Unity3d internal transparent (transparent) shader code