重要:在目前市面上常见的游戏引擎中,主要采用以下三种灯光实现方式:
顶点照明渲染路径细节 Vertex Lit Rendering Path Details
正向渲染路径细节 Forward Rendering Path Details
延迟光照渲染路径的细节 Deferred Lighting Rendering Path Details
以unity3d为例,以下将详细讲解三种灯光渲染方式的实现、原理及缺陷。
顶点照明渲染路径细节 Vertex Lit Rendering Path Details
Vertex Lit path generally renders each object in one pass, with lighting from all lights calculated at object vertices.
顶点照明渲染路径通常在一个通道中渲染物体,所有光源的照明都是在物体的顶点上进行计算的。
It's the fastest rendering path and has widest hardware support (however, keep in mind: it does not work on consoles).
顶点照明渲染路径是最快的渲染路径并且有最广泛的硬件支持(然而,请记住:它无法工作在游戏机上)。
Since all lighting is calculated at vertex level, this rendering path does not support most of per-pixel effects: shadows, normal mapping, light cookies, highly detailed specular highlights are not supported.
由于所有的光照都是在顶点层级上计算的,此渲染路径不支持大部分的逐像素渲染效果:如,阴影、法线贴图、灯光遮罩、高精度的高光。
Forward Rendering path renders each object in one or more passes, depending on lights that affect the object. Lights themselves are also treated differently by Forward Rendering, depending on their settings and intensity.
根据影响物体的光源的不同,正向渲染路径用单个或多个通道来渲染物体。在正向渲染中,光源本身也会根据他们的设置和强度受到不同的对待。
In Forward Rendering, some number of brightest lights that affect each object are rendered in fully per-pixel lit mode. Then, up to 4 point lights are calculated per-vertex. The other lights are computed as Spherical Harmonics (SH), which is much faster but is only an approximation. Whether a light will be per-pixel light or not is dependent on this:
在正向渲染中,影响物体的最亮的几个光源使用逐像素光照模式。接下来,最多有4个点光源会以逐顶点渲染的方式被计算。其他光源将以球面调和(Spherical Harmonics)的方式进行计算,球面调和技术计算很快但只能得到近似值。根据以下的规则判断一个光源是否为逐像素光源:
Rendering of each object happens as follows:
用以下的方法渲染每个物体:
For example, if there is some object that's affected by a number of lights (a circle in a picture below, affected by lights A to H):
例如,如果有一个物体受到若干光源的影响(下图中的圆圈,受到光源A到H的影响)
Let's assume lights A to H have the same color & intensity, all all of them have Auto rendering mode, so they would be sorted in exactly this order for this object. The brightest lights will be rendered in per-pixel lit mode (A to D), then up to 4 lights in per-vertex lit mode (D to G), and finally the rest of lights in SH (G to H):
假设光源A到H都有相同的颜色和强度,且它们的渲染模式都为自动的(Auto),那么它们严格的按照其名字排序。最亮的光源以逐像素光照模式的方式进行渲染(A到D),然后最多有4个光源以逐顶点光照模式进行渲染(D到G),其他光源以球面调和的方式进行渲染(G到H)。
Note that light groups overlap; for example last per-pixel light blends into per-vertex lit mode so there are less "light popping" as objects and lights move around.
注意不同的光照组间有重叠,如,最后一个逐像素光源也以逐顶点光照模式的方式渲染,这样能减少当物体和灯光移动时可能出现的"光照跳跃"现象。
Base pass renders object with one per-pixel directional light and all SH lights. This pass also adds any lightmaps, ambient and emissive lighting from the shader. Directional light rendered in this pass can have Shadows. Note that Lightmapped objects do not get illumination from SH lights.
基础通道用一个逐像素方向光和所有球面调和光渲染物体。此通道还负责渲染着色器中的光照贴图,环境光和自发光。在此通道中渲染的方向光可以产生阴影。需要注意的是,使用了光照贴图的物体不会得到球面调和光的光照。
Additional passes are rendered for each additional per-pixel light that affect this object. Lights in these passes can't have shadows (so in result, Forward Rendering supports one directional light with shadows).
附加通道用于渲染影响物体的其他逐像素光源。这些通道中渲染的光源无法产生阴影(因此,前向渲染支持一个能产生阴影的方向光)。
Spherical Harmonics lights are very fast to render. They have a tiny cost on the CPU, and are actually free for the GPU to apply (that is, base pass always computes SH lighting; but due to the way SH lights work, the cost is exactly the same no matter how many SH lights are there).
渲染球面调和光很快。它们只花费很少的CPU计算时间,并且实际上无需花费任何GPU计算时间(换言之,基础通道会计算球面调和光照,但由于球面调和光的计算方式,无论有多少球面调和光源,计算它们所花费的时间都是相同的)。
The downsides of SH lights are:
球面调和光源的缺点有:
In summary, SH lights are often good enough for small dynamic objects.
总的来说,球面调和光的效果对小的动态物体来说已经足够好了。
Deferred Lighting is rendering path with the most lighting and shadow fidelity:
延迟光照是一种当前最高级的能实现光线和阴影保真的渲染路径
Deferred Lighting's advantages 延迟光照的优点:
Disadvantages 缺点:
Cost of realtime lights in Deferred Lighting is proportional to number of pixels the light shines on; and not dependent on scene complexity. So small point or spot lights are very cheap to render. Point or spot lights that are fully or partially occluded by some scene objects get their pixels skipped on the GPU, so they are even cheaper.
延迟光照中实时光线的开销和光线照亮的像素值的数量成正比。而不取决于场景的复杂性。微小的点光源和聚光灯光源非常容易渲染。点光源或者完全或者部分被场景物体遮挡的聚光灯光源所照射的像素则被GPU所跳过,因此更加廉价。
Of course, lights with shadows are much more expensive than lights without shadows. In Deferred Lighting, shadow casters still need to be rendered once or more for each shadow-casting light. And the lighting shader that applies shadows is also more expensive than one without shadows.
当然,拥有阴影的光源比没有阴影的光源要昂贵许多。使用延迟光照,光影投射器仍然需要为每个阴影投射渲染一次或者多次。而且产生阴影的光线着色器也比不产生阴影的光线着色器要昂贵许多。
When Deferred Lighting is used, rendering process in Unity happens like this:
当延迟光照生效时,在Unity中发生的渲染过程如下:
Objects with shaders that can't handle Deferred Lighting are rendered after this process is done, using RenderTech-ForwardRendering path.
不能采用延迟光照技术的带阴影的物体在延迟光照渲染完后使用前向渲染路径处理。
Base pass renders each object once. View space normals and specular power are rendered into single ARGB32 Render Texture (normals in RGB channels, specular power in A). If platform & hardware supports reading Z buffer as a texture, then depth is not explicitly rendered. If Z buffer can't be accessed as a texture, then depth is rendered in additional rendering pass, using shader replacement.
基本渲染将每个物体都渲染一次。视图空间法线和高光强度被渲染进单一的ARGB32渲染纹理(法线在RGB通道,高光强度在A通道)中。如果平台和硬件支持将Z缓冲按纹理读取,那么深度不会被明确的渲染。如果Z缓冲不能被以纹理的方式访问,那么深度将在额外的渲染处理中被使用着色器替代技术渲染。
Result of the base pass is Z buffer filled with scene contents and Render Texture with normals & specular power.
基本渲染的结果是被屏幕内容填满的Z缓冲和带有法线和高光强度的渲染纹理。
Lighting pass computes lighting based on depth, normals and specular power. Lighting is computed in screen space, so it's independent of scene complexity. Lighting buffer is single ARGB32 Render Texture, with diffuse lighting in RGB channels and monochrome specular lighting in A channel. Lighting values are encoded using logarithmic encoding to provide extended dynamic range than usually possible with ARGB32 texture.
光照渲染基于深度,法线和高光强度计算光照。光照是被屏幕空间被计算的,因此和屏幕复杂性无关。光照缓冲是一个单一的ARGGB32渲染纹理,纹理的RGB通道带有漫反射的光照信息,在A通道带有单一特定颜色的光照。光照值采用对数值编码以产生比通常ARGB32纹理所能达到的动态扩展范围。
Lighting model is fixed to Blinn-Phong.
光照模式固定为Blinn-Phong。
Point and Spot lights that do not cross camera's near plane are rendered as 3D shapes, with Z buffer test against scene enabled. This makes partially or fully occluded Point and Spot lights very cheap to render. Directional lights and Point/Spot lights that cross the near plane are rendered as fullscreen quads.
不能跨越临近平面的点光源和聚光灯光源被作为带有开启测试场景的Z缓冲3D形状渲染,这部分和完全屏蔽的点光源和聚光灯光源可以非常廉价的渲染。 跨越临近区域的平行光或者点光源能作为全屏四边形。
If a light has shadows enabled, they are rendered and applies in this pass as well. Note that shadows are not "free"; shadow casters need to be rendered and a more complex light shader needs to be applied.
如果一个带有阴影的光源生效,在这个处理过程中会被很好的渲染。注意阴影并不免费,阴影投射器需要开销来渲染,同时一个更加复杂的光线着色器需要应用。
Final pass produces final rendered image. Here all objects are rendered again; with shaders that fetch the lighting, combine it with textures and add any emissive lighting.
Lightmaps are also applied in the final pass. Close to the camera, realtime lighting is used, and only baked indirect lighting is added. This crossfades into fully baked lighting further away from the camera.
最终渲染阶段产生最后渲染后的图像,到这一步,所有的对象都将被再次渲染,其中着色器将混合前一步生成的光源和纹理以及所有自发光照明。
在最后渲染阶段光照贴图也被应用。靠近相机,使用实时光照,并仅烘焙间接光照。
其他:lightingmap 烘焙贴图不在及时光的技术范围内。烘焙灯光是通过贴图记录光照信息来模拟固定的光照效果,场景中本身不包含及时灯光。
Deferred Lighting 延时光照 | Forward Rendering 正向渲染 | Vertex Lit 顶点光照 | |
Features 功能 | |||
Per-pixel lighting (normal maps, light cookies) 每像素计算光照(法线贴图、灯光cookies) |
Yes | Yes | - |
Realtime shadows 实时阴影 | Yes | 1 Directional Light(一盏平行光) | - |
Dual Lightmaps 双光照贴图 | Yes | - | - |
Depth&Normals Buffers 深度与法线缓冲区 | Yes | Additional render passes 额外渲染通道 | - |
Soft Particles 软粒子 | Yes | - | - |
Semitransparent objects 半透明的物体 | - | Yes | Yes |
Anti-Aliasing 抗锯齿 | - | Yes | Yes |
Light Culling Masks 灯光剔除蒙板 | Limited | Yes | Yes |
Lighting Fidelity 光照保真度 | All per-pixel 全部像素 | Some per-pixel 某些像素 | All per-vertex 所有顶点 |
Performance 性能 | |||
Cost of a per-pixel Light 每像素光照的花费 | Number of pixels it illuminates 照亮的像素数 |
Number of pixels * Number of objects it illuminates 像素数*照亮的像素数 |
- |
Platform Support 支持平台 | |||
PC (Windows/Mac) 台式机 | Shader Model 3.0+ | Shader Model 2.0+ | Anything |
Mobile (iOS/Android) 移动设备 | - | OpenGL ES 2.0 | OpenGL ES 2.0 & 1.1 |
Consoles (游戏)平台 | 360, PS3 | 360, PS3 | - |