重要:在目前市面上常見的遊戲引擎中,主要採用如下三種燈光實現方式:html
頂點照明渲染路徑細節 Vertex Lit Rendering Path Detailschrome
正向渲染路徑細節 Forward Rendering Path Details緩存
延遲光照渲染路徑的細節 Deferred Lighting Rendering Path Detailscookie
以unity3d爲例,如下將詳細講解三種燈光渲染方式的實現、原理及缺陷。app
頂點照明渲染路徑細節 Vertex Lit Rendering Path Detailsless
Vertex Lit path generally renders each object in one pass, with lighting from all lights calculated at object vertices.ide
頂點照明渲染路徑一般在一個通道中渲染物體,全部光源的照明都是在物體的頂點上進行計算的。性能
It's the fastest rendering path and has widest hardware support (however, keep in mind: it does not work on consoles).測試
頂點照明渲染路徑是最快的渲染路徑而且有最普遍的硬件支持(然而,請記住:它沒法工做在遊戲機上)。fetch
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 | - |