溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Unity3D中Rendering Paths及LightMode的示例分析

發(fā)布時(shí)間:2022-01-19 09:39:04 來源:億速云 閱讀:122 作者:小新 欄目:大數(shù)據(jù)

這篇文章將為大家詳細(xì)講解有關(guān)Unity3D中Rendering Paths及LightMode的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

簡述

Unity supports different Rendering Paths. You should choose which one you use depending on your game content and target platform / hardware. Different rendering paths have different performance characteristics that mostly affect Lights and Shadows. See render pipeline for technical details.

U3D支持不同的Rendering Path(渲染路徑),開發(fā)者應(yīng)該根據(jù)游戲內(nèi)容和目標(biāo)平臺(tái),硬件等來選擇使用哪個(gè)Rendering Path。不同的Rendering Path具有不同的表現(xiàn)效果,這些不同之處多體現(xiàn)在光照和陰影方面。查閱Render Pipeline獲取更多的細(xì)節(jié)。

The rendering Path used by your project is chosen in Player Settings. Additionally, you can override it for each Camera.

可以在Player Setting中設(shè)置默認(rèn)的Rendering Path,也可以針對(duì)單獨(dú)的Camera進(jìn)行設(shè)置。
Unity3D中Rendering Paths及LightMode的示例分析

Unity3D中Rendering Paths及LightMode的示例分析

If the graphics card can’t handle a selected rendering path, Unity will automatically use a lower fidelity one. For example, on a GPU that can’t handle Deferred Shading, Forward Rendering will be used.

如果目標(biāo)平臺(tái)的顯卡不能處理所選擇的硬件,那么Unity會(huì)自動(dòng)過渡至低保真檔次Rendering Path去。比如有的GPU不能處理Deferred Shading,那就會(huì)自動(dòng)采用Forward Rendering。


下面按從高效果至低效果的順序開始講解不同的Rendering Path。

Deferred Shading Rendering Path

概述

由于在移動(dòng)開發(fā)中并不常用Deferred Shading,故本節(jié)不細(xì)述。

Shader中使用的LightMode Pass Tag: Deferred

Deferred Shading擁有最佳的光照和陰影效果。當(dāng)場景中存在許多的實(shí)時(shí)光照時(shí),使用Deferred Shading也是最佳的方案。當(dāng)然Deferred Shading對(duì)硬件的要求稍高。

Deferred Shading是一種屏幕空間著色技術(shù),之所以叫做Deferred(延遲的,延期的),是因?yàn)镾hading的時(shí)機(jī)不是發(fā)生在第一個(gè)Pass的Vertex和Pixel/Fragment函數(shù)內(nèi),而是存在延遲,直到第二Pass的時(shí)候才執(zhí)行。

Deferred Shading對(duì)影響物體的光源數(shù)量沒有限制,所有的光源都是Per-Pixel(逐像素)光照,即所有光源都可以正確的和法線貼圖得到正確的計(jì)算結(jié)果。另外,所有的光源都可以有Light Cookie和陰影。其性能開銷與光源數(shù)量成正比,這意味著可以通過控制光照面積,減少單個(gè)物體受到光照影響的數(shù)量來獲得性能方面的提升。Deferred Shading的另一個(gè)優(yōu)點(diǎn)是結(jié)果能夠很方便的預(yù)測,被個(gè)光源都是逐像素光照,具有一致的光照計(jì)算過程。

缺點(diǎn)在于,其不能真正的支持抗鋸齒以及半透明物體(半透明物體是用 Forward Rendering 渲染路徑)。同時(shí)也不支持Unity3D中的Mesh Renderer組件的Receive Shadows功能,對(duì)Culling Masks的支持也不完善,最多能使用四個(gè)Culling Mask。

Q: 什么是Light Cookie?
A: Light Cookie又被稱作Cucoloris。是一種應(yīng)用在電影攝制時(shí)的小技巧,如下兩張圖,具體的解釋可以查看Wiki,Unity3D中的應(yīng)用可以查看Unity3D Cookies手冊(cè)或Unity3d Spot Light Cookie,Unity3D中的實(shí)現(xiàn)視頻過程可以點(diǎn)擊這里:
Unity3D中Rendering Paths及LightMode的示例分析Unity3D中Rendering Paths及LightMode的示例分析

要求

顯卡應(yīng)支持Multiple Render Targets(MRT),Shader Model 3.0(及更高),Depth Render Textures。大多數(shù)產(chǎn)于2006年以后的PC顯卡都支持本功能。
因?yàn)閷?duì)MRT功能支持的缺失,本功能在移動(dòng)端不受支持。(有的GPU支持,但是支持的程度也非常有限。)
注意:當(dāng)使用Orthographic projection(正交投影)時(shí)Deferred rendering不適用。當(dāng)使用正交投影時(shí)會(huì)降至 Forward Rendering。

性能方面

性能方面的影響取決于物體受光源影響的數(shù)量,而不是場景的復(fù)雜度。小面積的點(diǎn)光源或聚光燈,或光源被部分或完全摭擋,可以在很好的降低性能開銷。

有陰影的光源開銷更大,在Deferred rendering中,受到投影光源影響的,且可投影的物體依然是要對(duì)影響其的每個(gè)投影光源都進(jìn)行計(jì)算。具有陰影的光照Shader也肯定比沒有陰影的開銷更大。


Forward Rendering Rendering Path

概述

Shader中使用的LightMode Pass Tag: ForwardBase和ForwardAdd
當(dāng)Forward不可以用時(shí)會(huì)以Legacy Vertex Lit Rendering Path的效果進(jìn)行渲染

Forward是傳統(tǒng)渲染路徑。支持所有的Unity圖形特性。在默認(rèn)情況下,只有少數(shù)最亮的光源得以成為Per-Pixel光源。其他的光源則視為Per-Vertex。

取決于影響物體的光源數(shù)量,F(xiàn)orward 在單個(gè)或多個(gè)Pass內(nèi)渲染物體。需要注意的是,光源根據(jù)設(shè)置或強(qiáng)度的不同,本身也會(huì)具有不同的特性。

使用指南

Forward下,一些(視Quality Setting里面的設(shè)置而定)最亮的光源渲染物體時(shí)以Per-Pixel的方式進(jìn)行。緊接著可有四個(gè)光源為Per-Vetex光源。其余的光源都以球諧函數(shù)(SH:Spherical Harmonics)進(jìn)行計(jì)算,可以高效但是粗略的得到計(jì)算結(jié)果。無論光源是不是Per-Pixel的都依賴以下規(guī)則:

  • 光源的Render Mode設(shè)為Not Important的,則不是Per-Pixel光源

  • 最亮的平行光是Per-Pixel光源

  • 光源的Render Mode設(shè)為Important的,則為Per-Pixel光源

  • 如果場景中的per-pixel光源數(shù)量少于Quality Setting中的Pixel Light Count設(shè)置的數(shù)量,則會(huì)按光照強(qiáng)度從強(qiáng)至弱的順序,將更多的光源設(shè)置為per-pixel。

渲染每個(gè)物體時(shí)會(huì)依以下規(guī)則:

  • Base Pass只會(huì)用到一個(gè)per-pixel光源,以及所有其它Per-Vertex光源或SH光源

  • 其它的per-pixel光源則在Additional Pass中渲染,并且每個(gè)Pass對(duì)應(yīng)一個(gè)per-pixel光源

例如有物體受到如下A-H光源的影響:

Unity3D中Rendering Paths及LightMode的示例分析 Unity3D中Rendering Paths及LightMode的示例分析

假設(shè)所有光源都有相同的強(qiáng)度和顏色,并且Render Mode都設(shè)為Auto。最亮的4個(gè)光源A-D則為per-pixel光源,接著是四個(gè)per-vertex光源,D-G,最后余下的光源則是SH球諧光源,G-H。

注意上圖中光源分組的重疊部份。例如最后一個(gè)既可以成為per-pixel光源也可以成為per-vertex光源,因此當(dāng)物體或光源移動(dòng)時(shí),產(chǎn)生Light Popping(因光照導(dǎo)致的畫面不正常)的可能性更小。

關(guān)于Light Popping問題的實(shí)例,點(diǎn)擊這里 ,提問者最后通過調(diào)整光照亮度得以解決。

Unity3D中Rendering Paths及LightMode的示例分析

Light Popping現(xiàn)象

Base Pass

Base Pass只用一個(gè)per-pixel光源和所有的SH/Vertex 光源來渲染物體。在這個(gè)pass內(nèi)也能通過shader加入光照貼圖(Lightmaps),環(huán)境光(ambient),自發(fā)光(Emissive)等。平行光在這個(gè)Pass內(nèi)產(chǎn)生陰影。

注意

  • 使用LightMaps的物體(烘培過的物體)不會(huì)受到SH光源的光照。

  • 當(dāng)在Shader中使用Passflag tag “OnlyDirectional”時(shí),forward base pass只會(huì)渲染主平行光,環(huán)境光照探頭(Ambient/Light Probe)和光照貼圖,SH和vertex光源沒有被包含在此pass的數(shù)據(jù)內(nèi)。

Additional Passes

Additional Passes用于渲染每個(gè)額外的per-pixel光源。在這些pass中的光源默認(rèn)是不會(huì)產(chǎn)生陰影的(也就是說,F(xiàn)orward Rendering默認(rèn)只支持一個(gè)光源可以產(chǎn)生陰影)。除非使用multi_compile_fwdadd_fullshadows宏。

性能相關(guān)

Spherical Harmonics lights(球諧光照)非常高效。對(duì)CPU的消耗極其微小,對(duì)于GPU來說不會(huì)有額外的負(fù)擔(dān)(因?yàn)閎ase pass原本就會(huì)一直計(jì)算SH Lighting;和球諧光照的機(jī)制有光,無論有多少球諧光的存在,消耗都是一樣的)。

擴(kuò)展閱讀,Enlighten的簡化球諧光照文檔,《球諧照明:細(xì)節(jié)》,《球諧照明:細(xì)節(jié)》講義,球諧照明原理

SH光照的缺點(diǎn):

  • 利用物體的vertices(Vertex單詞的復(fù)數(shù)形式,Vertex的復(fù)數(shù)有三種寫法: vertexes,vertices ,vertexs)進(jìn)行計(jì)算,而不是pixels。因此不支持法線貼圖和Light Cookies。

  • SH光照函數(shù)是種非常低頻的照明函數(shù),不會(huì)有非常銳利的照明過渡,另外也只會(huì)響應(yīng)Diffuse Lighting(漫反射),對(duì)于Specular Highlight(鏡面高光)而言其頻率太低了。

  • SH lighting is not local; point or spot SH lights close to some surface will “l(fā)ook wrong”. 球諧照明不是局部的(?局部坐標(biāo)系?),點(diǎn)光源或聚光燈形式的SH Light在靠近一些表面時(shí)看上去會(huì)不正常。

總結(jié),SH Light適用于小型動(dòng)態(tài)物體。


Legacy Deferred Lighting Rendering Path

概述

Shader中使用的LightMode Pass Tag: PrepassBase和PrepassFinal

Legacy Deferred (light prepass) 類似于Deferred Shading,但是和其運(yùn)用了不同的技術(shù),有不同的側(cè)重。不支持Unity5的Physically Based Standard Shader。關(guān)于Legacy Deferred的原理可以查看RTR(Real-Time Rendering)網(wǎng)站的文章《Deferred lighting approaches》

Deferred Lighting從Unity 5.0起被視為傳統(tǒng)特性,因?yàn)槠錈o法支持諸如Stander Shader,Reflection Probes等特性。新工程可以考慮直接使用 Deferred Shading(不適用于移動(dòng)平臺(tái))。

注意:當(dāng)使用Orthographic projection時(shí)Deferred Rendering Path 不適用。當(dāng)使用正交投影時(shí)會(huì)降至 Forward Rendering Path。
注意:Legacy Deferred Lighting Rendering Path 和 Deferred Shading Rendering Path 都是Deferred rendering。

Deferred Lighting對(duì)影響物體的光源數(shù)量沒有限制,所有的光源都是Per-Pixel光照,即所有光源都可以正確的和法線貼圖得到正確的計(jì)算結(jié)果。另外,所有的光源都可以有Light Cookie和Shadow。其性能開銷與光源數(shù)量成正比,這意味著可以通過控制光照面積,減少單個(gè)物體受到光照影響的數(shù)量來獲得性能方面的提升。Deferred Lighting的另一個(gè)優(yōu)點(diǎn)是結(jié)果能夠很方便的預(yù)測,被個(gè)光源都是逐像素光照,具有一致的光照計(jì)算過程。

其缺點(diǎn)在于不能真正的支持抗鋸齒以及半透明物體(半透明物體采用 Forward Rendering 渲染路徑)。同時(shí)也不支持Unity3D中的Mesh Renderer組件的Receive Shadows功能,對(duì)Culling Masks的支持也不完善,最多能使用四個(gè)Culling Mask。

要求

顯卡應(yīng)支持Shader Model 3.0(或更新的版本),Depth Render Textures,Two-sided Stencil Buffers。大多數(shù)產(chǎn)于2004年以后的PC顯卡都支持本功能。
在手機(jī)端,所有支持OpenGL ES 3.0r GPU都支持Deferred Lighting. 部份OpenGL ES 2.0的GPU也能支持(因?yàn)橹С諨epth Textures)。
注意:當(dāng)使用Orthographic projection(正交投影)時(shí)Deferred rendering不適用。當(dāng)使用正交投影時(shí)會(huì)降至 Forward Rendering。

性能方面

性能方面的影響取決于物體受光源影響的數(shù)量,而不是場景的復(fù)雜度。小面積的點(diǎn)光源或聚光燈,或光源被部分或完全摭擋,可以在很好的降低性能開銷。

有陰影的光源開銷更大,在Deferred Rendering中,受到投影光源影響的,且可投影的物體依然是要對(duì)影響其的每個(gè)投影光源都進(jìn)行計(jì)算。具有陰影的光照Shader也肯定比沒有陰影的開銷更大。

使用指南

使用Deferred Lighting, 渲染過程發(fā)生在三種Pass內(nèi)。

  1. Base Pass:物體產(chǎn)生screen-space buffers, 包含depth, normals, specular power等信息。

  2. Lighting Pass:前一階段生成的Scree-space buffers用來計(jì)算光照,并放入另一個(gè)screen-space buffers。

  3. Final Pass:物體再次被處理,取得前一階段計(jì)算的光照信息,結(jié)合色彩紋理,并添加ambient/emissive Lighting。

如果物體使用了不適用于Deferred Lighting Rendering Path的Shader,會(huì)使用Forward rendering path

Base Pass

Base pass會(huì)對(duì)物體進(jìn)行一次處理。View sapce normals(視野空間法線)和Specular Power(高光強(qiáng)度)被處理為一張ARGB32格式的Render Texture(RGB值為normals, A為Specular Power)。如果目標(biāo)平臺(tái)和硬件允許Z buffer以紋理的形式讀取,則不用再進(jìn)行額外的渲染流程。反之,則要在另一個(gè)Pass使用Shader Replacement的Shader中進(jìn)行處理。

Lighting Pass

lighting passs根據(jù)Depth,Normal和Specular power來計(jì)算光照。光照計(jì)算發(fā)生在Screen Space(屏幕空間)中,因此時(shí)間消耗取決與場景的復(fù)雜程度。Lighting Buffer是一張ARGB32的Render Texture,Diffuse Lighting使用其RGB通道,Specular Lighting使用其A通道。

Final Pass

處理紋理,自發(fā)光等其它內(nèi)容,另外,光照貼圖也在這里完成。


Legacy Vertex Lit Rendering Path

Shader中使用的 使用的LightMode Pass Tag: Vertex,VertexLMRGBM和VertexLM

效果最差,不支持實(shí)時(shí)陰影。屬于Forward Rendering Path的子集。是性能最高,受支持度最多的Rendering Path(但是主機(jī)上不適用)。
所有的光照計(jì)算在Vertex層面完成,因此無法支持大多數(shù)的per-pixel效果,比如,陰影,法線貼圖,Light Cookies,精細(xì)的高光效果。


LightMode Tags

LightMode tag defines Pass’ role in the lighting pipeline. See render pipeline for details. These tags are rarely used manually; most often shaders that need to interact with lighting are written as Surface Shaders and then all those details are taken care of.

Possible values for LightMode tag are:

Always: Always rendered; no lighting is applied.
ForwardBase: Used in Forward rendering, ambient, main directional light, vertex/SH lights and lightmaps are applied.
ForwardAdd: Used in Forward rendering; additive per-pixel lights are applied, one pass per light.
Deferred: Used in Deferred Shading; renders g-buffer.
ShadowCaster: Renders object depth into the shadowmap or a depth texture.
PrepassBase: Used in legacy Deferred Lighting, renders normals and specular exponent.
PrepassFinal: Used in legacy Deferred Lighting, renders final color by combining textures, lighting and emission.
Vertex: Used in legacy Vertex Lit rendering when object is not lightmapped; all vertex lights are applied.
VertexLMRGBM: Used in legacy Vertex Lit rendering when object is lightmapped; on platforms where lightmap is RGBM encoded (PC & console).
VertexLM: Used in legacy Vertex Lit rendering when object is lightmapped; on platforms where lightmap is double-LDR encoded (mobile platforms).


Rendering Paths Comparison

-DeferredForwardLegacy DeferredVertex Lit
Features



Per-pixel lighting (normal maps, light cookies)YesYesYes-
Realtime shadowsYesWith caveatsYes-
Reflection ProbesYesYes--
Depth&Normals BuffersYesAdditional render passesYes-
Soft ParticlesYes-Yes-
Semitransparent objects-Yes-Yes
Anti-Aliasing-Yes-Yes
Light Culling MasksLimitedYesLimitedYes
Lighting FidelityAll per-pixelSome per-pixelAll per-pixelAll per-vertex
Performance



Cost of a per-pixel LightNumber of pixels it illuminatesNumber of pixels * Number of objects it illuminatesNumber of pixels it illuminates-
Number of times objects are normally rendered1Number of per-pixel lights21
Overhead for simple scenesHighNoneMediumNone
Platform Support



PC (Windows/Mac)Shader Model 3.0+ & MRTAllShader Model 3.0+All
Mobile (iOS/Android)OpenGL ES 3.0 & MRTAllOpenGL ES 2.0All
ConsolesXB1, PS4AllXB1, PS4, 360-

關(guān)于“Unity3D中Rendering Paths及LightMode的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI