[Feng yuchong] unity3d tutorial book shader: 16th custom Lighting Model

Source: Internet
Author: User
If the original article needs to be reproduced, please note: Reprinted from Fengyu Chong unity3d tutorial Institute

      Lecture on shader 16thCustom Illumination Model


14. We have implemented the basic surface shader, and 15 th talked about the basic knowledge of the illumination model. This article describes how to write the illumination model. The custom illumination model consists of four steps: (0) Setting up the framework, fill in the required parameters (1) Calculating the diffuse reflection intensity (2) Calculating the mirror reflection intensity (3) combining the diffuse reflection light with the mirror reflection light
The code is provided with Chinese comments, and can be implemented step by step in combination with the lighting formula described above.
  1. //Author: Feng yuchong
  2. Shader "Custom/t_customlightmodel "{
  3.  Properties
  4.  {
  5.  _ Maintex ("texture", 2d) = "white "{}
  6.  }
  7.  
  8.  Subshader
  9.  {
  10.  // Alpha test, with O. Alpha = c. a In surf;
  11.  Alphatest greater 0.4
  12.  Cgprogram
  13.  # Pragma surface surfLsylightmodel
  14.  
  15.  // Naming rule: the name after the lighting connection # pragma suface
  16.  // Lightdir: the unit vector of the point to the light source.Viewdir: the unit vector of the point to the camera.Atten: attenuation coefficient
  17.  Float4 lightinglsylightmodel (surfaceoutput S, float3 lightdir, half3 viewdir, half atten)
  18.  {
  19.  Float4 C;
  20.  
  21.  // (1) diffuse reflection intensity
  22.  Float diffusef = max (0, dot (S. Normal, lightdir ));
  23.  
  24.  // (2) mirror reflection strength
  25.  Float specf;
  26.  Float3 H = normalize (lightdir + viewdir );
  27.  Float specbase = max (0, dot (S. Normal, h ));
  28.  // Shininess mirror intensity coefficient, which is set to 8
  29.  Specf = POW (specbase, 8 );
  30.  
  31.  // (3) combined with diffuse reflection light and mirror reflection light
  32.  C. RGB = S. albedo * _ lightcolor0 * diffusef * atten + _ lightcolor0 * specf;
  33.  C. A = S. Alpha;
  34.  Return C;
  35.  }
  36.  
  37.  Struct Input
  38.  {
  39.  Float2 uv_maintex;
  40.  };
  41.  
  42.  Void Vert (inout appdata_full V)
  43.  {
  44. // Processing on special locations can be performed here
  45.  }
  46.  Sampler2d _ maintex;
  47.  
  48.  Void SURF (input in, inout surfaceoutput O)
  49.  {
  50.  Half4 c = tex2d (_ maintex, In. uv_maintex );
  51.  O. albedo = C. RGB;
  52.  O. Alpha = C.;
  53.  }
  54.  Endcg
  55.  }
  56. }

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.