[Feng yuchong] The shader of the unity3d tutorial book: 14th surface shader

Source: Internet
Author: User

If you need to repost an article, please note: Reprinted from Fengyu Chong unity3d tutorial Institute

      Lecture on shader 14thSurfaceShader

  It is complicated to use shader to implement illumination. There are different light types, different shadow options, and different render paths (forward and deferred ). Unity only encapsulates the illumination model, and the shader code is written by CG/HLSL.

 Example 1: Simplest surface shader

 

  1. Shader "Custom/t_3_0 "{
  2. Properties {
  3. _ Maintex ("base (RGB)", 2d) = "white "{}
  4. }
  5. Subshader {
  6. Cgprogram
  7. # Pragma surface surf Lambert
  8. Sampler2d _ maintex;
  9. Struct input {
  10. Float2 uv_maintex;
  11. };
  12. Void SURF (input in, inout surfaceoutput O ){
  13. Half4 c = tex2d (_ maintex, In. uv_maintex );
  14. O. albedo = C. RGB;
  15. O. Alpha = C.;
  16. }
  17. Endcg
  18. }
  19. }
The surface processing function receives the input parameter from the vertex processing function (automatically vertex processing here) and outputs surfaceoutput O to the illumination model (Lambert here ). Here we have done three things: (1) We have obtained the color C corresponding to the UV through UV. (2) Assign C. RGB to O. Albedo  (3) Assign C. A to O. AlphaThis is done. Then, if any object uses the shader, the shader can be correctly displayed no matter what light is added. It's really easy!
 Input and Output of the surface processing function surf
(1) Input
  1. StructInput {
  2. Float2 uv_maintex;
  3. Float3 viewdir;
  4. Float4 anyname: color;
  5. Float4 screenpos;
  6.  Float3 worldpos;
  7. Float3 worldrefl;
  8. };
Uv_maintex: uvviewdir: In-view screenpos: Crop Space Location 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-emitting color specular: mirror reflection coefficient gloss: Alpha: Transparency value for more parameters, see the documentation  Surface shader ParametersSurface shader can implement additional control by adding parameters, such as adding vertex processing functions and Alpha mixing. Note: If you use the Lambert, blinnphong: Alpha, Alpha, and other features of unity, you must use the surface shader parameter. Previously, Code such as alphatest greater 0.3 cannot be added. If it is added, the light becomes invalid and the object becomes dark. If the illumination model used is a custom illumination model, you only need to give O. if Alpha is assigned a normal value (such as a value of texture color), the previous Code such as alphatest greater 0.3 can still be used as usual. For specific code, refer to the Code in the next lecture. (1) Alpha MixingDirectly add the Alpha parameter.

# Pragma surface surf blinnphong alpha

(2) alphatest

Add a variable named _ cutoff.Add at the end of the surfaceAlphatest: _ cutoff.

The color is output only when the Alpha value is greater than _ cutoff.

 

 

  1. Shader "Custom/t_3_0 "{
  2. Properties {
  3. _ Maintex ("base (RGB)", 2d) = "white "{}
  4. _ Cutoff ("Alpha cutoff", range (0, 1) =
    0.5
  5. }
  6. Subshader {
  7. Cgprogram
  8. # Pragma surface surf Lambert alphatest: _ cutoff
  9. Sampler2d _ maintex;
  10. Struct input {
  11. Float2 uv_maintex;
  12. };
  13. Void SURF (input in, inout surfaceoutput O ){
  14. Half4 c = tex2d (_ maintex, In. uv_maintex );
  15. O. albedo = C. RGB;
  16. O. Alpha = C.;
  17. }
  18. Endcg
  19. }
  20. }

(3) vertex functions

Example: bloated Model

Implementation Method: extend the vertex to the normal direction for a distance

 

 

  1.  Shader "example/normal extrusion "{
  2.   Properties {
  3.   _ Maintex ("texture", 2d) = "white "{}
  4.   _ Amount ("extrusion amount", range (-1, 1) = 0.5
  5.   }
  6.   Subshader {
  7.   Tags {"rendertype" = "Opaque "}
  8.   Cgprogram
  9.   # Pragma surface surf Lambert vertex: vert
  10.   Struct input {
  11.   Float2 uv_maintex;
  12.   };
  13.   Float _ amount;
  14.   Void Vert (inout appdata_full v ){
  15.   V. vertex. xyz + = V. Normal * _ amount;
  16.   }
  17.   Sampler2d _ maintex;
  18.   Void SURF (input in, inout surfaceoutput O ){
  19.   O. albedo = tex2d (_ maintex, In. uv_maintex). RGB;
  20.   }
  21.   Endcg
  22.   }
  23.   Fallback "diffuse"
  24.   }

(4) finalcolor

Finalcolor: colorfunction

Make the final change to the color output

 

  
  1. Shader
    "Example/tint final color "{
  2.   Properties
    {
  3.   _ Maintex
    ("Texture ",
    2d) =
    "White"
    {}
  4.   _ Colortint
    ("Tint ",
    Color) = (1.0,
    0.6,
    0.6,
    1.0)
  5.   }
  6.   Subshader
    {
  7.   Cgprogram
  8.   # Pragma

    Surface surf Lambert
    Finalcolor: mycolor
  9.   Struct

    Input {
  10.   Float2
    Uv_maintex;
  11.   };
  12.   Fixed4
    _ Colortint;
  13.  
      Void

    Mycolor (input in, surfaceoutput o, inout fixed4 color)
  14.   {
  15. // Modify the color of the final output here
  16.   Color

    * = _ Colortint;
  17.   }
  18.   Sampler2d
    _ Maintex;
  19.   Void

    Surf (input in, inout surfaceoutput O ){
  20.  O. Albedo
    = Tex2d (_ maintex, In. uv_maintex). RGB;
  21.   }
  22.   Endcg
  23.   }
  24.  }
For more parameters, see

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.