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.
- //Author: Feng yuchong
- Shader "Custom/t_customlightmodel "{
- Properties
- {
- _ Maintex ("texture", 2d) = "white "{}
- }
-
- Subshader
- {
- // Alpha test, with O. Alpha = c. a In surf;
- Alphatest greater 0.4
- Cgprogram
- # Pragma surface surfLsylightmodel
-
- // Naming rule: the name after the lighting connection # pragma suface
- // Lightdir: the unit vector of the point to the light source.Viewdir: the unit vector of the point to the camera.Atten: attenuation coefficient
- Float4 lightinglsylightmodel (surfaceoutput S, float3 lightdir, half3 viewdir, half atten)
- {
- Float4 C;
-
- // (1) diffuse reflection intensity
- Float diffusef = max (0, dot (S. Normal, lightdir ));
-
- // (2) mirror reflection strength
- Float specf;
- Float3 H = normalize (lightdir + viewdir );
- Float specbase = max (0, dot (S. Normal, h ));
- // Shininess mirror intensity coefficient, which is set to 8
- Specf = POW (specbase, 8 );
-
- // (3) combined with diffuse reflection light and mirror reflection light
- C. RGB = S. albedo * _ lightcolor0 * diffusef * atten + _ lightcolor0 * specf;
- C. A = S. Alpha;
- Return C;
- }
-
- Struct Input
- {
- Float2 uv_maintex;
- };
-
- Void Vert (inout appdata_full V)
- {
- // Processing on special locations can be performed here
- }
- Sampler2d _ maintex;
-
- Void SURF (input in, inout surfaceoutput O)
- {
- Half4 c = tex2d (_ maintex, In. uv_maintex );
- O. albedo = C. RGB;
- O. Alpha = C.;
- }
- Endcg
- }
- }