(轉)遊戲引擎中三大及時光照渲染方法介紹(以unity3d爲例)

重要:在目前市面上常見的遊戲引擎中,主要採用如下三種燈光實現方式: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 Details

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.

根據影響物體的光源的不一樣,正向渲染路徑用單個或多個通道來渲染物體。在正向渲染中,光源自己也會根據他們的設置和強度受到不一樣的對待。

Implementation Details 實現細節

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)的方式進行計算,球面調和技術計算很快但只能獲得近似值。根據如下的規則判斷一個光源是否爲逐像素光源:

  • Lights that have their Render Mode set to Not Important are always per-vertex or SH. 
    渲染模式被設置爲不重要(Not Important)的光源以逐頂點或球面調和的方式進行計算
  • Brightest directional light is always per-pixel. 
    最亮的方向光源爲像素光源
  • Lights that have their Render Mode set to Important are always per-pixel. 
    渲染模式被設置重要(Important)的光源爲像素光源
  • If the above results in less lights than current Pixel Light Count Quality Setting, then more lights are rendered per-pixel, in order of decreasing brightness. 
    如根據以上規則獲得的像素光源數量小於質量設置中的像素光源數量(Pixel Light Count),爲了減小亮度,會有更多的光源以逐像素的方式進行渲染

Rendering of each object happens as follows:

用如下的方法渲染每一個物體:

  • Base Pass applies one per-pixel directional light and all per-vertex/SH lights. 
    基礎通道渲染一個逐像素方向光和全部的逐頂點/球面調和光。
  • Other per-pixel lights are rendered in additional passes, one pass for each light. 
    其餘逐像素光在附加的通道中進行渲染,每一個光源都須要一個通道

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 基本通道

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 附加通道

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).

附加通道用於渲染影響物體的其餘逐像素光源。這些通道中渲染的光源沒法產生陰影(所以,前向渲染支持一個能產生陰影的方向光)。

Performance Considerations 性能注意事項

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:

球面調和光源的缺點有:

  • They are computed at object's vertices, not pixels. This means they do not support light Cookies or normal maps. 
    它們計算的是物體的頂點而不是像素。這意味着它們不支持投影遮罩和發現貼圖。
  • SH lighting is very low frequency. You can't have sharp lighting transitions with SH lights. They are also only affecting the diffuse lighting (too low frequency for specular highlights). 
    球面調和光只有很低的頻率。球面調和光不能產生鋒利的照明過渡。它們也只會影響散射光照(對高光來講,球面調和光的頻率過低了)。
  • SH lighting is is not local; point or spot SH lights close to some surface will "look wrong". 
    球面調和不是局部的,靠近曲面的球面調和點光和聚光可能會"看起來不正確"。

In summary, SH lights are often good enough for small dynamic objects.

總的來講,球面調和光的效果對小的動態物體來講已經足夠好了。

延遲光照渲染路徑的細節 Deferred Lighting Rendering Path Details

Deferred Lighting is rendering path with the most lighting and shadow fidelity:

延遲光照是一種當前最高級的能實現光線和陰影保真的渲染路徑

  • There's no limit how many lights can affect any object. 
    對於能影響任何物體的光線數量沒有上限
  • All lights are evaluated per-pixel. Which means that they all interact properly with normal maps etc. 
    徹底採用以每像素的方式評估光線,這等於意味着所有將以正常貼圖的方式正確的和物體交互
  • All lights can have Cookies. 
    全部光線都能擁有信息緩存
  • All lights can have Shadows. 
    全部的光線都能產生陰影

Deferred Lighting's advantages 延遲光照的優勢:

  • Lighting cost is proportional to light size on screen. Does not matter how many objects it shines on. Small lights = cheap! 
    光照的開銷與屏幕的光線尺寸成正比,不用擔憂光線所照射的物品的數量,少許光線 等價於 廉價的花費
  • Consistency. All lighting for all lights is computed per-pixel; there are no lighting computations that break down on large triangles etc. 
    一致性,全部的光線的光照採用按像素爲計算分割單位來計算。好比,不會有在大規模三角形狀況下光照計算使計算性能發生崩潰的狀況發生。

Disadvantages 缺點:

  • No real anti-aliasing support. 
    沒有實時抗鋸齒支持
  • Deferred Lighting can't handle semi-transparent objects. Those are rendered using Forward Rendering. 
    延遲光照不能處理半透明物體,也不能用在哪些使用前向渲染的物體之上
  • Limited lighting model support (Blinn-Phong). All lighting is computed the same way; you can't have drastically different lighting models on different objects. 
    有限的光照模式支持(Blinn-Phong)。全部光照以一樣的方式計算,你不可以在不一樣的物體上採用徹底不一樣的光照模式
  • No support for "receive shadows" flag and limited support light Culling Masks. 
    沒有對接收陰影特徵的支持和對光線遮罩剔除有限的支持

Requirements for Deferred Lighting 延時光照的需求

  • Requires Unity Pro
    須要Unity專業版
  • Graphics card with Shader Model 3.0 (or later), support for Depth render textures and two-sided stencil buffer. Most graphics cards made after 2004 support it: GeForce FX and later, Radeon X1300 and later, Intel 965 / GMA X3100 and later. 
    顯示卡支持Shader Model 3.0(或更高),深度紋理渲染和雙面模板緩衝特性。許多2004年後的顯卡都支持:如Geforce Fx或更高,Radeon X1300或更高 Intel 965/ GMA X3100 或更高
  • Currently does not work on mobile platforms. 
    目前在移動平臺不支持。

Performance Considerations 性能注意事項

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.

固然,擁有陰影的光源比沒有陰影的光源要昂貴許多。使用延遲光照,光影投射器仍然須要爲每一個陰影投射渲染一次或者屢次。並且產生陰影的光線着色器也比不產生陰影的光線着色器要昂貴許多。

Implementation Details 實現細節

When Deferred Lighting is used, rendering process in Unity happens like this:

當延遲光照生效時,在Unity中發生的渲染過程以下:

  1. Base Pass: objects are rendered, producing screen-space buffers with depth, normals, and specular power. 
    基本渲染:被渲染的對象產生帶有深度,法線,和反射量的屏幕空間緩衝
  2. Lighting pass: lighting is computed using the previous buffers. Lighting is computed into another screen-space buffer. 
    光照渲染:使用上一步的緩衝計算出光照。結果放入另外一個屏幕空間緩存
  3. Final pass: objects are rendered again. They fetch computed lighting, combine it with color textures and add any ambient/emissive lighting. 
    最後渲染:物體再次渲染。取來已經計算好的光線和顏色紋理混合在一塊兒,而後再加上環境光以及散射光照。

Objects with shaders that can't handle Deferred Lighting are rendered after this process is done, using RenderTech-ForwardRendering path.

不能採用延遲光照技術的帶陰影的物體在延遲光照渲染完後使用前向渲染路徑處理。

Base Pass 基本渲染階段

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 光照渲染階段

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 最後渲染階段

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 烘焙貼圖不在及時光的技術範圍內。烘焙燈光是經過貼圖記錄光照信息來模擬固定的光照效果,場景中自己不包含及時燈光。

 

Rendering Paths Comparison 渲染路徑比較

 

  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 -
相關文章
相關標籤/搜索