reprinted from the Wind Yu Chong Unity3d Tutorial College
14 We've achieved basic surface Shader, and we've talked about the basics of lighting models. This is a lecture on how to write a light model. The custom lighting model is divided into 4 main steps:(0) erecting the frame, fill in the required parameters(1) Calculation of diffuse reflectance intensity(2) Calculate specular reflectionStrength (3) combined with diffuse reflection light and specular reflection light code with Chinese annotation, with the above-said lighting formula, step by step implementation can be.
Author: Wind Yu Chong Shader "Custom/t_customlightmodel" {Properties {_maintex ("Texture", 2D) = "white" {}} Subshader {//alpha test, with O in surf. Alpha = C.A; Alphatest Greater 0.4 Cgprogram #pragma surface surf Lsylightmodel//naming rules: Lighting Connect The name of the suface after #pragma//lightdir: point to the unit vector of the light source Viewdir: Point to camera unit vector atten: Attenuation factor float4 Lightinglsylightmodel (Su Rfaceoutput s, float3 lightdir,half3 viewdir, half atten) {FLOAT4 C; (1) Diffuse reflectance strength float Diffusef = max (0,dot (S.normal,lightdir)); (2) Specular reflection strength float specf; FLOAT3 H = normalize (Lightdir+viewdir); float specbase = max (0,dot (s.normal,h)); Shininess specular strength factor, set here to 8 specf = POW (specbase,8); (3) Combining diffuse reflection light with specular 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) {//This can be done in special position processing} sampler2d _maintex; void Surf (Input in,inout surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O.albedo = C.rgb; O.alpha = C.A; } ENDCG}}
Unity3d Tutorial Shader: The 16th lecture on custom lighting models