1, basic knowledge explained
An object in nature receives three light effects, ambient light, diffuse reflection, and specular reflection. So for the computer to simulate the reality of light, it should also achieve the three kinds of basic lighting, ambient light, diffuse reflection, specular highlight. For these three kinds of illumination, there are some influential factors, which need to investigate the formula of the light model.
(1) The influence factors of environmental light intensity
, it can be seen that the effect of ambient light depends on the intensity of the general ambient light and the diffuse material color
(2), the influence factor of diffuse reflection light intensity
It can be seen that diffuse reflection light depends on the incident light intensity and material constants, for the black surface, the value of the material constant is 0, for the white surface, the value of the material constant is 1. When the direction of the incident light is related to the angle of the surface normal of the object, the smaller the angle, the closer the normal, then the more light is received, the brighter the more. The dot product of this time is also greater. (Recall the function of COS)
(3), the influence factors of specular reflection light intensity
Specular reflection by the observer direction V to calculate the specular reflection, if v close to R, the intensity of the reflection will also be larger (also affected by the gloss), according to R and V angle cosine of-th (POW value) to generate different gloss highlights, we need to limit the negative cosine of the value of 0, in addition to specular reflection also requires a material Quality color (usually white)
2, Code Walkthrough
Shader"Jiki/vertexlighting_specular"{Properties {_color ("Base RGB", Color) = (1,1,1,1) _shininess ("shininess", Range (0.001,Ten))=Ten_speccolor ("Specular Color", Color) = (1,1,1,1)} subshader {Pass {Tags {"Lightmode"="Forwardbase"} cgprogram#pragmaVertex vert#pragmaFragment Frag#include"Unitycg.cginc"uniform float4 _lightcolor0; Uniform FLOAT4 _color; Uniformfloat_shininess; Uniform FLOAT4 _speccolor; structInputvertex {float4 vertex:position; FLOAT3 Normal:normal; }; structOutputvertex {float4 pos:sv_position; FLOAT4 Col:color; }; Outputvertex Vert (Inputvertex input) {Outputvertex o; //Tool Functionsfloat4x4 modelmatrix=_object2world; float4x4 Modelmatrixverse=_world2object; //normal vectors in the world spaceFLOAT3 normaldir=normalize (Mul (FLOAT4 (Input.normal,0.0), Modelmatrixverse). xyz); //Sight DirectionFLOAT3 Viewdir=normalize (_worldspacecamerapos-Mul (Modelmatrix,input.vertex). xyz); //LightFLOAT3 Lightdir; floatattenuation;//Attenuation Amount if(_worldspacelightpos0.w==0)//proving to be a directional vector of infinite length{Attenuation=1.0; Lightdir=normalize (_WORLDSPACELIGHTPOS0). xyz; } Else{FLOAT3 Vertextolightsource= (_worldspacelightpos0-Mul (Modelmatrix,input.vertex)). xyz; floatdist=length (Vertextolightsource); Lightdir=normalize (Vertextolightsource); Attenuation=1/Dist; } //Ambient LightFLOAT3 ambientcolor=unity_lightmodel_ambient.rgb*_color.rgb; //Diffuse ReflectionFLOAT3 Diffusecolor=attenuation*_lightcolor0.rgb*_color.rgb*max (0, Dot (normaldir,lightdir)); //Specular HighlightsFLOAT3 Specularcolor; if(Dot (Lightdir,normaldir) <0)//prove to be back{Specularcolor=FLOAT3 (0,0,0); } Else{Specularcolor=attenuation*_lightcolor0.rgb*_speccolor.rgb*pow ((Max (0, Dot (Viewdir,reflect (-Lightdir,normaldir))) , _shininess); } //MixedO.COL=FLOAT4 (Ambientcolor+diffusecolor+specularcolor,1.0); O.pos=Mul (Unity_matrix_mvp,input.vertex); returno; } float4 Frag (Outputvertex input): COLOR {returnInput.col; } ENDCG} }}
Finally,
The above code to write some simple, but the personal feel reflected out the principle of light, there are problems also ask you to correct me.
Because this is based on the vertex, so the lighting effect appears somewhat rough, not enough delicate, follow-up will be in the fragment to achieve the light, so that will be smooth specular highlights. Please look forward to!! It's not too early to sleep.
Specular highlight effect for each vertex illumination in vertex