Table of contents
foreword
This is a note I wrote for myself to learn shader. First of all, the first part is about the lighting model involved in shader.
1. Diffuse reflection
1.1 Lambert model
1.1.1 Basic formula
Diffuse lighting calculations conform to the Lambert model
diffuse = lightColor * _Diffuse * max(0, Dot(normalDir, lightDir)))
Among them, lightColor is the color of the light source, _Diffuse is the inherent color of the object, the meaning of multiplication is color superposition (to do), normalDir is the normal direction normalized vector, lightDir is the incident light direction normalized vector, the result of the dot product between the two is actually cosθ
Among them, the value range of max(0, dot(normalDir, lightDir)) is [0, 1], and its function correspondence is shown in the figure below:
The backlight surface of the object is calculated completely according to the value of 0, so that the details of the backlight surface of the object are lost
Diffuse lighting therefore obeys Lambert's law: the intensity of reflected light is proportional to the cosine of the angle between the surface normal and the direction of the light source
It should be noted that this intensity has nothing to do with the camera position, that is, the observer position
1.1.2 Physical principles
Understanding Lambert's Empirical Formula from the Perspective of Physical Energy
The amount of light received per unit area is proportional to cosθ
1.2 Half Lambert model
In order to prevent the backlit surface of the Lambert model from being too dark and cause the loss of detail observation, the model correction method is used to improve the effect.
The specific modification part is:
float halfLambert = dot(normalDir, lightDir) * 0.5 + 0.5;
diffuse = lightColor * _Diffuse * halfLambert;
Here, the value range of halfLambert is [0, 1], and its function correspondence is shown in the figure below:
The vector dot product is not zero at the backlit surface of the object, so details are also preserved on the backlit surface
2. High light reflection
2.1 Phong model
2.1.1 Basic formula
specular = lightColor * pow(max(0,cosθ),x)
Where lightColor is the color of the incident light, θ is the angle between the reflected light and the direction of the camera, and x is the exponential power, the larger x is, the more concentrated the highlights are
Secondly, you can add a color specularColor to the highlight separately, superimposed with the incident light color
specular = lightColor * specularColor * pow(max(0,cosθ),x)
2.2 Blinn-Phong model
Based on the formula of the Blinn model, θ is changed from the angle between the original reflected light direction and the line of sight direction to the angle between the normal direction and the direction of the incident light direction and the line of sight direction angle bisector (half-way vector).