"Unity Shader" Transparent transparent Shader

Source: Internet
Author: User
Tags mul

Learning materials:

    • http://www.sikiedu.com/course/37/task/459/show#

The code for this example is based on the previous article, adding a transparency effect. To make it easier to differentiate between the new sections, this section uses bold and red bolded fonts.

Shader"Custom/14-rock Alpha"{properties{_maintex ("Main Tex", 2D) =" White"{}//Texture Map_color ("Color", Color) = (1,1,1,1)//control the color of a texture map_normalmap ("Normal Map", 2D) ="Bump"{}//indicates that the normals of the model vertices are used when no normal map is specified at that location_bumpscale ("Bump Scale", Float) =1   //The bump parameter of the normal map. 0 means that the original discovery of the model is used, and the value in the normal map is used for 1. More than 1, the bump degree is greater.         _alphascale("Alpha Scale", Float) =1 //Transparency Parameters} subshader{ Tags{"Queue"="Transparent"  "ignoreprojection"="True" "Rendertype"="Transparent"}//the transparent shaderPass {//only the correct lightmode can be defined to get some unity's built-in illumination variables .tags{"Lightmode"="Forwardbase"}            //the transparent shader             zwrite Off//turn off Deep write             Blend Srcalpha Oneminussrcalpha//Mixed ParametersCgprogram//include Unity's built-in files before you can use some of the variables built into Unity#include"Lighting.cginc" //gets the color of the first direct light _lightcolor0 the position of the First direct light _worldspacelightpos0 (i.e. direction)#pragmaVertex vert#pragmaFragment Fragfixed4 _color;            Sampler2d _maintex; FLOAT4 _maintex_st; //The name is fixed map name + suffix "_st", 4 values first two XY represents scaling, the latter two ZW represents offsetsampler2d _normalmap; FLOAT4 _normalmap_st; //The name is fixed map name + suffix "_st", 4 values first two XY represents scaling, the latter two ZW represents offset            float_bumpscale; float _alphascale; structa2v {float4 vertex:position; //tell Unity to populate the vertex property with vertex coordinates under model spaceFLOAT3 Normal:normal;//the normals that the model comes with are no longer used. This variable is retained because tangent space is determined by the normals (in the model) and tangent lines (in the model). FLOAT4 tangent:tangent;//The TANGENT.W is used to determine the direction of the axes in the tangent space. FLOAT4 texcoord:texcoord0;            }; structv2f {float4 position:sv_position;//declares the coordinates used to store vertices under the clipping space//FLOAT3 worldnormal:texcoord0; //no longer uses the normal direction under World spaceFLOAT3 lightdir:texcoord0;//direction of parallel light under tangent spaceFLOAT3 Worldvertex:texcoord1; FLOAT4 Uv:texcoord2; //XY storage Maintex texture coordinates, ZW stores normalmap texture coordinates            }; //calculate vertex coordinates from the model coordinate system to the clipping plane coordinate systemv2f Vert (a2v v) {v2f F; F.position= Mul (UNITY_MATRIX_MVP, V.vertex);//UNITY_MATRIX_MVP is a built-in matrix. This step is used to convert a coordinate from model space to the clipping space//The normal direction. Shift the normal direction from model space to world space//f.worldnormal = Mul (V.normal, (float3x3) unity_worldtoobject);//The reverse multiplication is from the model to the world, otherwise from the world to the modelF.worldvertex =Mul (V.vertex, unity_worldtoobject). xyz; //f.uv = v.texcoord.xy;//do not use scaling and offsetsF.uv.xy = v.texcoord.xy * _maintex_st.xy + _maintex_st.zw;//texture coordinates of the mapF.UV.ZW = v.texcoord.xy * _normalmap_st.xy + _normalmap_st.zw;//texture coordinates for normal mapstangent_space_rotation;//call this macro to get a matrix rotation, which is used to convert the direction of the model space under the tangent space//Objspacelightdir (V.vertex);//the direction of parallel light under model space is obtainedF.lightdir = Mul (rotation, Objspacelightdir (V.vertex));//direction of parallel light under tangent space                returnF; }            //all operations related to the normal direction are placed in the tangent space. Because the normal direction obtained from the normal map is under tangent space. fixed4 Frag (v2f f): sv_target {//Ambient LightFixed3 ambient =Unity_lightmodel_ambient.rgb; //The normal direction. Gets from the normal map. The color value of the normal map--normal direction//fixed3 Normaldir = normalize (f.worldnormal); //no longer use the model's own normalsFixed4 NormalColor = tex2d (_normalmap, F.UV.ZW);//The color values in the normal map//fixed3 tangentnormal = Normalize (NORMALCOLOR.XYZ * 2-1);//tangent space under the normal direction, found that the calculated normals are not correct! Fixed3 tangentnormal = Unpacknormal (NormalColor);//use Unity's built-in approach, from color-worthy to normal in tangent-space directionTangentnormal.xy = Tangentnormal.xy * _bumpscale;//control the degree of bumpTangentnormal =normalize (tangentnormal); //Light direction. Fixed3 Lightdir = normalize (F.lightdir);//direction of illumination under tangent space//The color of the points on the texture image corresponding to the texture coordinatesFixed4 Texcolor = tex2d (_maintex, F.uv.xy) *_color; //Diffuse diffuse color = direct light color * MAX (0, cos (angle of light direction and normal direction)) * Material itself color (the color of the point where the texture corresponds )FIXED3 diffuse = _lightcolor0 * MAX (0, Dot (Tangentnormal, lightdir)) * TEXCOLOR.RGB;//multiplication for color fusion//Final color = Diffuse reflection + ambient lightFixed3 Tempcolor = diffuse + ambient * texcolor;//Let the ambient light also blend with the texture color to prevent ambient light from making the texture look hazy//return Fixed4 (Tempcolor, TEXCOLOR.A); //Use the transparency that comes with each point in the texture map//return Fixed4 (Tempcolor, _alphascale);//use externally-customized transparency                return fixed4 (Tempcolor, TEXCOLOR.A * _alphascale); //Use the transparency of the two to combine} ENDCG}} FallBack"Diffuse"}

In the scene, the transparent and opaque are put together for easy observation and the effect is as follows:

Note the point:

    • Define tags in the Subshader block, using transparency.
    • Set the close depth write and blend parameters in the pass block.
    • Transparency can be taken from the texture map itself (texcolor) or externally customized transparency (_Alphascale), or it can be a combination of both. If External Custom Transparency (_Alphascale) is used, the transparency valid range is 0 to 1, beyond which the range is unchanged. if it is a combination of the two, the Transparency (texcolor) of the texture map itselfmay be less than 1, so a _alphascale of 1 o'clock may be a somewhat transparent model.

"Unity Shader" Transparent transparent 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.