Games 101 – Shading
Shading Frequencies ( 着色频率 )
1、Shade each triangle (flat shading)
flat shading (平面着色)
-
Triangle face is flat-— one normal vector (一个法向量)
-
Not good for smooth surfaces
2、Shade each vertex ( Gouraud shading)
Gouraud shading (Gouraud 着色法)
- Interpolate colorsfrom vertices across triangle ( 从三角形顶点插值颜色)
- Each vertex has a normal vector (how?)
3、Shade each pixel (Phong shading)
Phong shading (Phong 着色法)
- Interpolate normal vectors across each triangle
- Compute full shading model at each pixel
- Not the Blinn-PhongReflectance Model
contrast(对比)
Defining Per-Vertex Normal Vectors
(定义每个顶点法法线)
Best to get vertex normals from the underlying geometry(基础几何图形)
- e.g. consider a sphere(球)
Otherwise have to infer(推测) vertex normals from triangle faces
- Simple scheme: average surrounding face normals(平均周围面法线)
N v = ∑ i N i ∥ ∑ i N i ∥ N_{v}=\frac{\sum_{i} N_{i}}{\left\|\sum_{i} N_{i}\right\|} Nv=∥∑iNi∥∑iNi
Defining Per-Pixel Normal Vectors
Barycentric interpolation(齐次插值) of vertex normals
Don’t forget to normalize(归一化) the interpolated directions(插值方向)
Graphics (Real-time Rendering) Pipeline 管线
图形管线;实时渲染管线
shader programs
- Program vertex and fragment processing stages(程序顶点和片段处理阶段)
- Describe operation(描述操作) on a single vertex (or fragment)
shader
- Shader function executes(执行) once per fragment.
- Outputs color of surface at the current fragment’s screen sample position.(在当前片段的屏幕采样位置输出曲面颜色)
- This shader performs a texture lookup to obtain the surface’s material color at this point, thenperforms a diffuse lighting calculation.(该着色器执行纹理查找以获取此时曲面的材质颜色,然后执行漫反射照明计算。)
http://shadertoy.com/view/ld3Gz2(在线shader的网站)
(GPU:并行计算)Specialized processors for executing graphics pipeline computations
Texture Mapping(纹理映射)
Different Colors at Different Places ?
L − d = k − d ∗ ( I / r ∧ 2 ) ∗ ( n dot l ) \mathbf{L}_{-} \mathbf{d}=k_{-} d^{*}\left(\mathbf{I} / \mathbf{r}^{\wedge} \mathbf{2}\right){ }^{*}(\mathbf{n} \operatorname{dot} \mathbf{l}) L−d=k−d∗(I/r∧2)∗(ndotl)
Texture: 定义任意一个点的基本属性
Surfaces are 2D
Surface lives in 3D world space
Every 3D surface point also has a placewhere it goes in the 2D image (texture).(每个3D曲面点在2D图像(纹理)中都有一个位置。)
Texture Applied to Surface*(应用于表面的纹理)
Each triangle “copies” a piece of the texture image to the surface. (每个三角形都将纹理图像的一部分“复制”到曲面上。)
Visuaization of Texture Coordinates (纹理坐标的可视化)
Each triangle vertex is assigned a texture coordinate (u,v)
约定熟成(u,v)都在0~1之间
Textures can be used multiple times!
纹理无缝衔接
lnterpolation Across Triangles: Barycentric Coordinates ( 重心坐标)
interpolation Across Triangles
- Specify values at vertices
- Obtain smoothly varying values across triangles(获得三角形中平滑变化的值)
- Texture coordinates, colors, normal vectors,…
How do we interpolate? Barycentric Coordinates
Acoordinate system for triangles ( α , β , γ ) (\alpha, \beta, \gamma) (α,β,γ)
( x , y ) = α A + β B + γ C α + β + γ = 1 \begin{array}{c} (x, y)=\alpha A+\beta B+\gamma C \\ \alpha+\beta+\gamma=1 \end{array} (x,y)=αA+βB+γCα+β+γ=1
Inside the triangle ifall three coordinates are non-negative ( ( α , β , γ ) (\alpha, \beta, \gamma) (α,β,γ)都为非负,则在三角形内部)
Geometric viewpoint —— proportional areas ( 几何视点–比例区域 )
α = A A A A + A B + A C β = A B A A + A B + A C γ = A C A A + A B + A C \begin{array}{r} \alpha=\frac{A_{A}}{A_{A}+A_{B}+A_{C}} \\ \beta=\frac{A_{B}}{A_{A}+A_{B}+A_{C}} \\ \gamma=\frac{A_{C}}{A_{A}+A_{B}+A_{C}} \end{array} α=AA+AB+ACAAβ=AA+AB+ACABγ=AA+AB+ACAC
面积比
What’s the barycentric coordinate of the centroid? ( 重心坐标)
( α , β , γ ) = ( 1 3 , 1 3 , 1 3 ) ( x , y ) = 1 3 A + 1 3 B + 1 3 C \begin{aligned} (\alpha, \beta, \gamma) &=\left(\frac{1}{3}, \frac{1}{3}, \frac{1}{3}\right) \\ (x, y) &=\frac{1}{3} A+\frac{1}{3} B+\frac{1}{3} C \end{aligned} (α,β,γ)(x,y)=(31,31,31)=31A+31B+31C
α = − ( x − x B ) ( y C − y B ) + ( y − y B ) ( x C − x B ) − ( x A − x B ) ( y C − y B ) + ( y A − y B ) ( x C − x B ) β = − ( x − x C ) ( y A − y C ) + ( y − y C ) ( x A − x C ) − ( x B − x C ) ( y A − y C ) + ( y B − y C ) ( x A − x C ) γ = 1 − α − β \begin{aligned} \alpha &=\frac{-\left(x-x_{B}\right)\left(y_{C}-y_{B}\right)+\left(y-y_{B}\right)\left(x_{C}-x_{B}\right)}{-\left(x_{A}-x_{B}\right)\left(y_{C}-y_{B}\right)+\left(y_{A}-y_{B}\right)\left(x_{C}-x_{B}\right)} \\ \beta &=\frac{-\left(x-x_{C}\right)\left(y_{A}-y_{C}\right)+\left(y-y_{C}\right)\left(x_{A}-x_{C}\right)}{-\left(x_{B}-x_{C}\right)\left(y_{A}-y_{C}\right)+\left(y_{B}-y_{C}\right)\left(x_{A}-x_{C}\right)} \\ \gamma &=1-\alpha-\beta \end{aligned} αβγ=−(xA−xB)(yC−yB)+(yA−yB)(xC−xB)−(x−xB)(yC−yB)+(y−yB)(xC−xB)=−(xB−xC)(yA−yC)+(yB−yC)(xA−xC)−(x−xC)(yA−yC)+(y−yC)(xA−xC)=1−α−β
Linearly interpolate values at vertices
V = α V A + β V B + γ V C V=\alpha V_{A}+\beta V_{B}+\gamma V_{C} V=αVA+βVB+γVC
VA,VB, Vc can bepositions,texturecoordinates, color, normal, depth, material attributes… .
Applying Textures
Simpie Texture Mapping: Diffuse Color (简单纹理映射:漫反射颜色)
Texture Magnification (What if the texture is too small?) ( 纹理放大 )过小
Generally don’t want this -insufficient texture resolution (纹理分辨率不足)
A pixel on a texture -a texel ( 纹理元素、纹素 )
Bilinear interpolation ( 双线性插值 )
And fractional offsets,(s,t) as shown (以及分数偏移量(s,t)
Linear interpolation(1D)
lerp ( x , v 0 , v 1 ) = v 0 + x ( v 1 − v 0 ) \operatorname{lerp}\left(x, v_{0}, v_{1}\right)=v_{0}+x\left(v_{1}-v_{0}\right) lerp(x,v0,v1)=v0+x(v1−v0)
Two helper lerps (horizontal)
u 0 = lerp ( s , u 00 , u 10 ) u 1 = lerp ( s , u 01 , u 11 ) \begin{array}{l} u_{0}=\operatorname{lerp}\left(s, u_{00}, u_{10}\right) \\ u_{1}=\operatorname{lerp}\left(s, u_{01}, u_{11}\right) \end{array} u0=lerp(s,u00,u10)u1=lerp(s,u01,u11)
Final vertical lerp, to get result:
f ( x , y ) = lerp ( t , u 0 , u 1 ) f(x, y)=\operatorname{lerp}\left(t, u_{0}, u_{1}\right) f(x,y)=lerp(t,u0,u1)
Bilinear interpolation usually gives pretty good resultsat reasonable costs
Texture Magnification (hard case) (What if the texture is too large?) 过大
Point Sampling Textures — Problem(点采样纹理-问题)
Screen Pixel “Footprint” in Texture (纹理中的屏幕像素“足迹”)
Antiaiiasing -Supersampling?
Will supersampling work?
- Yes, high quality, but costly
- When highly minified, many texels in pixel footprintSignal frequency too large in a pixel(高度缩小时,像素足迹中的许多纹理信号频率在像素中太大)
- Need even higher sampling frequency (需要更高的采样频率)
Let’s understand this problem in another way
- What if we don’t sample?
- Just need to get the average value within a range!(只需要得到一个范围内的平均值)
Point Ouery vs.(Avg.) Range Query(每点与(平均)范围查询)
Mipmap
只能做近似的正方形的查询
Allowing (fast, approx., square) range queries (允许(快速、近似、平方)范围查询)
“Mip” comes from the Latin “multum in parvo”, meaning a multitude in a small space (“Mip”来自拉丁语“multum in parvo”,意思是小空间中的人群)
mipmap 额外的开销仅仅占正常的1/3的存储
Computing Mipmap Level D
D = log 2 L L = max ( ( d u d x ) 2 + ( d v d x ) 2 , ( d u d y ) 2 + ( d v d y ) 2 ) D=\log _{2} L \quad L=\max \left(\sqrt{\left(\frac{d u}{d x}\right)^{2}+\left(\frac{d v}{d x}\right)^{2}}, \sqrt{\left(\frac{d u}{d y}\right)^{2}+\left(\frac{d v}{d y}\right)^{2}}\right) D=log2LL=max((dxdu)2+(dxdv)2,(dydu)2+(dydv)2)
Visuaiization of Mipmap Level ( Mipmap级别可视化 )
Trilinear interpolation
Linear interpolation based on continuous D value
Mipmap Limitations (Mipmap限制)
Overblur ( 过度模糊 ) Why?
Anisotropic Filtering ( 各向异性过滤) 比三线性插值的效果好
Ripmaps and summed area tables (Ripmap和合计面积表)
- Can look up axis-alignedrectangular zones (可以查找轴对齐矩形区域)
- Diagonal footprints(对角线足迹) still a problem
EWA filtering
- Use multiple lookups
- Weighted average
- Mipmap hierarchy still helps (Mipmap层次结构仍然有帮助)
- Can handle irregular footprints (可以处理不规则的足迹)
irregular Pixel Footprint in Texture (纹理中的不规则像素足迹)
filtering
- Use multiple lookups
- Weighted average
- Mipmap hierarchy still helps (Mipmap层次结构仍然有帮助)
- Can handle irregular footprints (可以处理不规则的足迹)
irregular Pixel Footprint in Texture (纹理中的不规则像素足迹)
[外链图片转存中…(img-i8ID9Plk-1666083156515)]
[外链图片转存中…(img-jx50Tagx-1666083156515)]