Lighting Basics
From left to right are:
- ambient lighting
- Diffuse Lighting
- Specular Lighting
- Combining the above three is Feng's shading algorithm
Lighting properties
- Emitted light: emits light from the object itself
- Ambient light: is light that is sufficiently scattered in the environment and cannot tell its direction
- Diffuse light: light comes from a certain direction, but is reflected in all directions on the object
- Specular: Light comes from a specific direction and then bounces off the surface of the object in a specific direction
Material properties
- flood material
- Diffuse Material
- Specular Reflection Material
- emission material
Lighting calculation
- Calculation of ambient light (brightness)
Ambient light = ambient light color of the light source * material color of the object
2. Calculated emission color of emitted light = object's reflective material color
Lighting comparison
Diffuse Lighting Calculation
The light shines on the surface of the object, which determines the intensity of the light on the surface of the object, which is represented by the following figure:
The light intensity is the product of the intensity of the light itself and the angle cos between the light and the surface normal of the object, 有效的光照方向是与物体表面法线夹角在0~90度之间
.
漫反射颜色 = 光源的漫反射颜色 * 物体的漫反射材质 * DiffuseFactor
DiffuseFactor = max(0, dot(N, L))
The diffuse factor DiffuseFactor
is the dot product of the ray and the vertex normal vector
Specular light calculation
N
: plane normal
I
: incident light
H
: reflected light
E
: sight
α
: Angle between the viewpoint and the reflected light
镜面反射颜色 = 光源的镜面光的颜色 * 物体的镜面材质颜色 * SpecularFactor
SpecularFactor = power(max(0, dot(N,H)), shininess)
H
: half vector of sight vector E and light vector L
dot(N,H)
: The dot product of H, N, the geometric meaning is the cos value of the angle between the square line and the normal line
shiniess
: Reflectance of highlights
光照颜色 = (环境颜色 + 漫反射颜色 + 镜面反射颜色)* 衰减因子
Attenuation factor
衰减因子 = 1.0/(距离衰减常量 + 线性衰减常量 * 距离 + ⼆次衰减常量 * 距离的平方
The distance decay constant, linear decay constant and quadratic decay constant are all constant values.
注意
: The intensity of ambient light, diffuse light and specular light will all be attenuated by distance, only the intensity of emitted light and global ambient light will not be affected.
Spotlight factor
聚光灯夹⻆cos值 = power(max(0,dot(单位光源位置,单位光线向量)),聚光灯指数);
- The unit ray vector is the unit vector pointing from the light source to the vertex
- Spotlight index, indicating the brightness of the spotlight
- Interpretation of the formula: the power of the spotlight index of the dot product of the unit light source position and the unit light vector
Spotlight without transition and with transition processing
Add transition calculation
聚光灯因⼦ = clamp((外环的聚光灯⻆度cos值 - 当前顶点的聚光灯⻆度cos值)/ (外环的聚光灯⻆角度cos值 - 内环聚光灯的角度的cos值),0,1)
The ultimate formula for lighting calculations
光照颜色 = 发射颜色 + 全局环境颜色 + (环境颜色 + 漫反射颜色 + 镜⾯面反射颜色) * 聚光灯效果 * 衰减因子