Unity3d Tutorial Shader: the 14th surface Shader

Source: Internet
Author: User

reprinted from the Wind Yu Chong Unity3d Tutorial Collegeusing shader to achieve illumination is more complex, with different light types, different shading options, and different render paths (forward and deferred). Unity just encapsulates the lighting model, and shader's code is written in CG/HLSL.    Example one: The simplest surface Shader
Shader "Custom/t_3_0" {    Properties {        _maintex ("Base (RGB)", 2D) = "White" {}    }    subshader {        Cgprogram        #pragma surface surf Lambert        sampler2d _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.A;        }        ENDCG    }  }

  

The surface processing function receives the parameter input in, from the vertex processing function (here the automatic vertex processing), the output surfaceoutput O to the Light model (here is Lambert). Here we do three things: (1) by UV made map corresponding to the color of the UV C (2) to the C.rgb to O.albedo (3) to the C.A assigned to O.alpha this is done. Then let any object use the shader, no matter what light is added to the correct display. It's really simple! input and output of surface treatment function Surf   (1) Input
struct Input {    float2 Uv_maintex;    FLOAT3 Viewdir;    FLOAT4 Anyname:color;    FLOAT4 Screenpos;    FLOAT3 Worldpos;    FLOAT3 WORLDREFL;} ;

  

Uv_maintex:uvviewdir: Into perspective Screenpos: Clipping space position worldpos: World Space Location (2) Output
struct Surfaceoutput {    half3 Albedo;    Half3 Normal;    HALF3 emission;    Half specular;    Half Gloss;    Half Alpha;};

  

Albedo: Diffuse color Normal: normal emission: self-luminous color specular: Specular reflection factor Gloss:alpha: Transparent valueFor more parameters please refer to the documentation Surface Shader ParametersSurface Shader provides additional control by adding parameters, such as vertex processing functions, alpha blending, and so on. Note: If you use thethe light model is Unity's ownLambert or Blinnphong:alpha blending, alpha blending and other functions must use surface shader parameters. Previously, such as Alphatest Greater 0.3 code can not be added. If added, the light fails and the object is dark. If you use theLighting model is a custom lighting model: Just give o in surf. Alpha is normally assigned (such as a value of the assigned map color), then code such as Alphatest Greater 0.3 can still be used as usual. Specific code can refer to the code in the next lecture. (1) Alpha blendDirectly add the parameter alpha to

#pragma surface Surf Blinnphong alpha

(2) Alphatest

Add a variable, named _cutoff, and add Alphatest:_cutoff at the end of #pragma surface.

The effect is that the color is only output when the alpha value is greater than _cutoff

Shader "Custom/t_3_0" {    Properties {        _maintex ("Base (RGB)", 2D) = "White" {}        _cutoff ("Alpha Cutoff", Range (0 , 1)) = 0.5    }    subshader {        cgprogram        #pragma surface surf Lambert Alphatest:_cutoff        sampler2d _ 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.A;        }        ENDCG    }  }

  

(3) Vertex function

Example: a bloated model

Implementation method, extending the vertex to the normal direction for a certain distance



Shader "Example/normal Extrusion" {    Properties {        _maintex ("Texture", 2D) = "White" {}        _amount ("Extrusion Am Ount ", Range ( -1,1)) = 0.5    }    subshader {        Tags {" Rendertype "=" Opaque "}        cgprogram        #pragma surface su RF Lambert Vertex:vert        struct Input {            float2 uv_maintex;        }        ; float _amount;        void Vert (InOut appdata_full v) {            v.vertex.xyz + = V.normal * _amount;        }        Sampler2d _maintex;        void Surf (Input in, InOut surfaceoutput o) {            O.albedo = tex2d (_maintex, In.uv_maintex). RGB;        ENDCG    }     Fallback "diffuse"}

  

(4) Finalcolor

Finalcolor:colorfunction

Make final changes to the color output

Shader "Example/tint Final Color" {    Properties {        _maintex ("Texture", 2D) = "White" {}        _colortint ("Tint", Col OR) = (1.0, 0.6, 0.6, 1.0)    }    subshader {        cgprogram        #pragma surface surf        Lambert Finalcolor:mycolor struct Input {            float2 uv_maintex;        };        Fixed4 _colortint;        void MyColor (Input in, Surfaceoutput o, inout fixed4 color)        {            //The color of the last output is modified in this case. Color            *= _colortint ;        }        Sampler2d _maintex;        void Surf (Input in, InOut surfaceoutput o) {            O.albedo = tex2d (_maintex, In.uv_maintex). RGB;        ENDCG    }  }

  

For more parameters please refer to the documentation

Unity3d Tutorial Shader: the 14th surface 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.