Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

使用光照貼圖全域光照

烘焙的光照貼圖也是一種為場景新增間接光照(也叫全烘焙光照)的工作流程。與 doc_gi_probes 的方法不同,烘焙光照貼圖在低端PC和移動裝置上運作良好,因為在運作時幾乎不消耗資源。與 GIProbe 的另一個不同點是,烘焙光照貼圖還能夠用來保存直接光照,可以進一步提升性能。

烘焙光照貼圖與 GIProbe 不同,是完全靜態的,一旦被烘焙就完全不能被修改。它也不能為場景提供反射,所以如果要達到較好的畫質,在室內場景(或者是使用 Sky 的室外場景)中就需要和 反射探查 搭配使用。

因為是烘焙出來的,所以在光線滲透方面的問題就比 GIProbe 少很多,並且間接光照也會看上去更漂亮。烘焙光照貼圖的缺點是烘焙所需的時間比 GIProbe 長很多,GIProbe 幾秒鐘就能搞定的烘焙,換成烘焙光照貼圖就可能至少得花上幾分鐘。這會嚴重拖慢反覆運算速度,所以推薦只在確實有查看光照變化的需求時進行光照貼圖的烘焙。

烘焙光照貼圖還會佔用被烘焙材質的 UV2 欄位,也就是說你無法再把 UV2 用於該材質的其它用途(無論是內建的 doc_spatial_material 還是自訂著色器)。

儘管缺乏靈活性,烘焙光照貼圖通常在(大部分)靜態場景中同時提供最佳品質*和*性能。這使得光照貼圖在遊戲開發中仍然很流行,儘管光照貼圖是電玩遊戲中最古老的全域照明技術。

也參考

不確定 LightmapGI 是否適合您的需求?請參閱 doc_introduction_to_global_Illumination_comparison` 以了解 Godot 4 中可用的 GI 技術的比較。

視覺比較

LightmapGI 已停用。

LightmapGI 已停用。

LightmapGI 啟用(僅使用間接光烘焙)。

LightmapGI 啟用(僅使用間接光烘焙)。直射光仍然是即時的,允許在遊戲過程中發生微妙的變化。

LightmapGI 已啟用(直接和間接光照烘焙)。

LightmapGI 已啟用(直接和間接光照烘焙)。最佳性能,但視覺品質較低。請注意右上角較模糊的太陽陰影。

視覺比較

以下是 BakedLightmap 和 GIProbe 的一些顯示效果比較。可以看到光照貼圖更精確,但由於使用的是展開後的紋理,所以過渡以及解析度可能就沒有那麼理想。GIProbe 看上去沒有那麼精確(因為是近似估算),但總體上更平滑。

../../../_images/lightmap_gi_comparison.png

與 LightmapGI 相比,SDFGI 的準確度也較低。然而,SDFGI 可以支援大型開放世界,而不需要任何烘焙。

設定

首先,在光照貼圖器可以執行任何操作之前,要烘焙的物件需要 UV2 圖層和紋理大小。UV2 圖層是一組輔助紋理座標,可確保物件中的任何面在 UV 貼圖中都有自己的位置。面與面之間不得共用紋理中的像素。

這裡有幾種方法可以確保您的物件具有唯一的 UV2 層和紋理大小:

使用 Godot 進行展開

警告

如果在匯入的 3D 場景上使用此 Mesh 選單操作,則重新載入場景時產生的 UV2 將會遺失。

Godot可以選擇打開網格並視覺化UV通道. 它可以在Mesh功能表中找到:

../../../_images/lightmap_gi_mesh_menu.png

這將生成第二組UV2座標, 可用於烘焙, 並且還將自動設定紋理大小.

使用 3D DCC 展開

最後一種方法是在你喜歡的 3D 應用程式中進行操作。通常不推薦這種做法,但為了讓你知道它的存在,這裡還是解釋一下。這種做法的主要優勢在於,針對可能要經常重新匯入的複雜物件,在 Godot 中進行紋理生成的代價可能相當高,所以在匯入前展開可以提高速度。

只需在第二個UV2層上進行展開即可。

../../../_images/lightmap_gi_blender.png

然後正常匯入 3D 場景。記得在匯入後為網格設定紋理大小。

../../../_images/lightmap_gi_lmsize.png

如果在匯入時使用外部網格, 則將保留大小. 請注意,3D DCC中的大多數解包器都不是面向品質的, 因為它們可以快速工作. 您通常需要使用接縫或其他技術來建立更好的展開.

將 Sprite 轉換為 2D 網格

備註

此選項僅適用於基本網格體,例如 class_BoxMesh、class_CylinderMesh、class_PlaneMesh 等。

在原始網格體上啟用 UV2 可以使它們接收並貢獻烘焙光照。這可以用於某些照明設定。例如,您可以在烘焙光貼圖後隱藏具有發光材質的圓環,以建立遵循圓環形狀的區域光。

預設情況下,原始網格體不會產生 UV2 以節省資源(因為這些網格體可能在遊戲過程中建立)。您可以在屬性面板中編輯基本網格並啟用**新增 UV2** 以使引擎按程式為基本網格產生 UV2。預設的 UV2 填充 值經過調整以避免大多數光照貼圖滲色,而不會在邊緣上浪費太多空間。如果您發現光照貼圖僅在特定基元網格上滲色,則可能需要增加 UV2 填充

光照貼圖大小提示 表示光照貼圖紋理上單一網格所佔據的大小,該大小會根據網格的大小屬性和 UV2 填充 值而變化。 光照貼圖大小提示 不應手動更改,因為重新載入場景時任何修改都會遺失。

檢查 UV2

在前面提到的網格功能表中, 可以顯示UV2紋理座標. 如果出現問題, 請確保檢查網格是否具有以下UV2座標:

../../../_images/lightmap_gi_uvchannel.png

設定場景

首先需要在場景中新增一個 BakedLightmap 節點。新增後,場景中的所有節點(和子節點)就都可以進行光照烘焙了,甚至產生實體的場景也可以。

../../../_images/lightmap_gi_scene.png

烘焙器支援同一子場景存在多個實例,它們會有各自獨立的光照貼圖(前提是你得遵守之前提過的關於縮放的規則)。

設定網格

如果需要讓 MeshInstance 節點參與烘焙,需要啟用 Use in Baked Light 屬性。

../../../_images/lightmap_gi_use.png

在場景匯入時自動生成光照貼圖時, 會自動啟用此功能.

設定燈光

預設情況下, 燈光採用間接燈光烘焙. 這意味著陰影貼圖和光照仍然是動態的並影響移動的物體, 但是光線反射的光將被烘焙.

燈可以禁用(不烘焙)或完全烘焙(直接和間接). 這可以通過燈光中的 烘焙模式 功能表進行控制:

../../../_images/lightmap_gi_bake_mode.png

模式有:

已停用的專案

The light is ignored when baking lightmaps. This is the mode to use for dynamic lighting effects such as explosions and weapon effects.

警告

Hiding a light has no effect on the resulting lightmap bake. This means you must use the Disabled bake mode instead of hiding the Light node by disabling its Visible property.

動態語言:

這是預設的模式,是性能與即時友好性的折衷。只會烘焙間接光照。直接燈光和陰影仍舊是即時的,與不使用 BakedLightmap 時一致。

這個模式可以在保持顯示效果相對正確的同時,允許進行燈光顏色、能量、以及位置的 微調 。例如,你可以借此實作靜態火把的閃爍,它的間接光照仍然是烘焙的。

靜態語言:

間接和直接光照都會被烘焙。因為靜態表面可以完全跳過光照和陰影的計算,所以這個模式可以提供最佳的性能,並且實作不隨距離衰減的平滑的陰影。即時燈光不會再影響烘焙的表面,但仍然可以影響動態的物件。在光源上使用 All (全部)烘焙模式時,動態物件不會在烘焙表面上投射陰影,所以你需要使用別的方法,比如軟陰影。軟陰影既可以通過 Sprite3D + RayCast 實作,也可以通過把朝下的反 SpotLight 的烘焙模式設定成 禁用 實作。

遊戲過程中無法調整燈光。燈光的移動、變色、能量調整不會對靜態表面產生影響。

因為烘焙模式可以分光源調整,所以可以設定出混合的烘焙光照。一個比較流行的做法是使用即時 DirectionalLight 並把它的烘焙模式設定成 Indirect,然後把 OmniLight 和 SpotLight 的烘焙模式都設定成 All。這樣做的性能不錯,同時也允許動態物件在室外投射即時陰影。

完全烘焙的燈光還可以利用燈光節點的**大小**(全向/聚光)或**角度距離**(方向)屬性。這允許陰影具有真實的半影,隨著施法者和陰影之間距離的增加,半影的尺寸也會增加。與即時 PCSS 陰影相比,這也具有較低的效能成本,因為只有動態物件才會在其上渲染即時陰影。

../../../_images/lightmap_gi_omnilight_size.png

烘焙

開始烘焙只需在選中 BakedLightmap 節點後點擊上方的 烘焙光照貼圖 按鈕:

../../../_images/lightmap_gi_bake.png

根據場景大小、所選烘焙方法以及品質的不同,其過程可能花費幾秒鐘到幾分鐘不等(也可能是幾小時)。

警告

烘焙光照貼圖是一個可能需要大量視訊記憶體的過程,尤其是當產生的紋理很大時。由於內部限制,如果生成的紋理尺寸太大(即使在具有大量視訊記憶體的系統上),引擎也可能當機。

為了避免當機,請確保匯入停靠列中的光照貼圖紋理像素大小設定為足夠高的值。

調整

  • Quality(品質):提供了四種烘焙品質:Low(低級)、Medium(中級)、High(高級)、Ultra(超級)。品質越高所需的時間越長,但最終光照貼圖的顯示效果越好、噪點也越少。針對發光材質或者幾乎沒有直接光照的地方,不同品質之間的區別尤為顯著。

  • Bounces: The number of bounces to use for indirect lighting. The default value (3) is a good compromise between bake times and quality. Higher values will make light bounce around more times before it stops, which makes indirect lighting look smoother (but also possibly brighter depending on materials and geometry).

  • Bounce Indirect Energy: The global multiplier to use when baking lights' indirect energy. This multiplies each light's own Indirect Energy value. Values different from 1.0 are not physically accurate, but can be used for artistic effect.

  • 定向: 如果啟用,則儲存光照貼圖的方向資訊。這改善了烘焙表面的法線貼圖材質的外觀,尤其是在完全烘焙的燈光下(因為它們也有直接光線烘焙)。缺點是定向光照貼圖的渲染成本稍高。它們還需要更多的時間來烘烤並導致檔案大小更大。

  • 內部: 若啟用,將不會取得環境照明。將其用於純室內場景以避免漏光。

  • Use Texture for Bounces: If enabled, a texture with the lighting information will be generated to speed up the generation of indirect lighting at the cost of some accuracy. The geometry might exhibit extra light leak artifacts when using low resolution lightmaps or UVs that stretch the lightmap significantly across surfaces. Leave this enabled if unsure.

  • Use Denoiser: If enabled, uses a denoising algorithm to make the lightmap significantly less noisy. This increases bake times and can occasionally introduce artifacts, but the result is often worth it. See Denoising for more information.

  • Denoiser Strength: The strength of denoising step applied to the generated lightmaps. Higher values are more effective at removing noise, but can reduce shadow detail for static shadows. Only effective if denoising is enabled and the denoising method is JNLM (OIDN does not have a denoiser strength setting).

  • Bias(偏置) :陰影的偏移量,使用 3D 單位。除非你遇到了光線滲透問題或者光照貼圖在烘焙後存在暗區,否則通常情況下不需要修改這個值。該選項不會影響投射在烘焙表面上的即時陰影。

  • 最大紋理尺寸: 產生的紋理合集的最大紋理尺寸。較高的值將導致產生的切片較少,但由於硬體對紋理大小的限制,可能無法在所有硬體上工作。如果不確定,請將其保留為預設值“16384”。

  • 環境 > 模式: 控制烘焙光照貼圖時環境光照的來源方式。 場景 的預設值適用於具有可見外部元件的關卡。對於純室內場景,將其設定為**停用**以避免漏光並加快烘烤速度。也可以設定為 自訂天空自訂顏色 以使用與實際場景的環境天空不同的環境照明。

  • 產生探查 > 細分: 請參閱 動態物件

  • 資料 > 光資料: 請參閱 光照貼圖資料

平衡烘焙時間和品質

因為高品質的烘焙可能花費非常長的時間(大型複雜場景可能需要若干小時),推薦首先設定成較低品質,將場景中的燈光佈置成滿意的效果後再改成較高的品質,在匯出專案前進行“終極”烘焙。

透過增加匯入的 3D 場景上的 光照貼圖紋理尺寸 來降低光照貼圖解析度也會顯著加快烘焙速度。但是,這將要求您重新匯入所有光照貼圖 3D 場景,然後才能再次烘焙光照貼圖。

Denoising

Since baking lightmaps relies on raytracing, there will always be visible noise in the "raw" baked lightmap. Noise is especially visible in areas that are difficult to reach by bounced light, such as indoor areas with small openings where the sunlight can enter. Noise can be reduced by increasing bake quality, but doing so will increase bake times significantly.

Comparison between denoising disabled and enabled

Comparison between denoising disabled and enabled (with the default JNLM denoiser).

To combat noise without increasing bake times too much, a denoiser can be used. A denoiser is an algorithm that runs on the final baked lightmap, detects patterns of noise and softens them while attempting to best preseve detail. Godot offers two denoising algorithms:

JNLM (Non-Local Means with Joint Filtering)

JNLM is the default denoising method and is included in Godot. It uses a simple but efficient denoising algorithm known as non-local means. JNLM runs on the GPU using a compute shader, and is compatible with any GPU that can run Godot 4's Vulkan-based rendering methods. No additional setup is required.

JNLM's denoising can be adjusted using the Denoiser Strength property that is visible when Use Denoiser enabled. Higher values can be more effective at removing noise, at the cost of suppressing shadow detail for static shadows.

Comparison between JNLM denoiser strength values

Comparison between JNLM denoiser strength values. Higher values can reduce detail.

OIDN (Open Image Denoise)

Unlike JNLM, OIDN uses a machine learning approach to denoising lightmaps. It features a model specifically trained to remove noise from lightmaps while preserving more shadow detail in most scenes compared to JNLM.

OIDN can run on the GPU if hardware acceleration is configured. With a modern high-end GPU, this can provide a speedup of over 50× over CPU-based denoising:

  • On AMD GPUs, HIP must be installed and configured.

  • On NVIDIA GPUs, CUDA must be installed and configured. This may automatically be done by the NVIDIA installer, but on Linux, CUDA libraries may not be installed by default. Double-check that the CUDA packages from your Linux distribution are installed.

  • On Intel GPUs, SYCL must be installed and configured.

If hardware acceleration is not available, OIDN will fall back to multithreaded CPU-based denoising. To confirm whether GPU-based denoising is working, use a GPU utilization monitor while baking lightmaps and look at the GPU utilization percentage and VRAM utilization while the denoising step is shown in the Godot editor. The nvidia-smi command line tool can be useful for this.

OIDN is not included with Godot due to its relatively large download size. You can download precompiled OIDN binary packages from its website. Extract the package to a location on your PC, then specify the path to the oidnDenoise executable in the Editor Settings (FileSystem > Tools > OIDN > OIDN Denoise Path). This executable is located within the bin folder of the binary package you extracted.

After specifying the path to the OIDN denoising executable, change the denoising method in the project settings by setting Rendering > Lightmapping > Denoiser to OIDN. This will affect all lightmap bakes on this project after the setting is changed.

備註

The denoising method is configured in the project settings instead of the editor settings. This is done so that different team members working on the same project are assured to be using the same denoising method for consistent results.

Comparison between JNLM and OIDN denoisers

Comparison between JNLM and OIDN denoisers. Notice how OIDN better preserves detail and reduces seams across different objects.

動態物件

與 VoxelGI 和 SDFGI 不同,動態物件接收間接光照的方式與靜態物件不同。這是因為光照貼圖僅在靜態物件上執行。

為了顯示動態物件上的間接照明,使用了 3D 探查系統,其中光探查遍布整個場景。烘焙光照貼圖時,光照貼圖器將計算探頭接收到的*間接*光量。直射光不會儲存在光探查中,即使對於將烘焙模式設定為**靜態**的光源(因為動態物件繼續即時點亮)。

有兩種方法可以為伺服器匯出專案:

  • 自動:Gen Probes > Subdiv 設定為 Disabled 以外的值,然後烘焙光照貼圖。預設值為“8”,但您可以選擇更大的值來提高精確度,但代價是烘焙時間更長且輸出檔案大小更大。

  • 手動: 除了自動產生探查之外或作為自動產生探查的替代方案,您可以透過向場景新增:ref:class_LightmapProbe 節點來手動新增光照探查。這可用於改善動態物體經常經過的區域的照明細節。在場景中放置 LightmapProbe 節點後,必須再次烘焙光照貼圖才能使其生效。

備註

烘焙光照貼圖後,您會注意到 3D 場景中的白色球體代表烘焙光照將如何影響動態物件。這些球體**不會**出現在正在運作的專案中。

如果要在編輯器中隱藏這些球體,請在 3D 編輯器頂部切換 **View > Gizmos > LightmapGI**(「閉眼」圖示表示 Gizmo 已隱藏)。

光照貼圖資料

Light Data(光照資料) :烘焙後包含光照烘焙資料。紋理會被保存到磁片上,但這裡還會包含動態物件的捕獲資料,資料量可能非常大。如果你使用的是 .tscn 格式的場景,應該將此資源保存成外部的二進位 .lmbake 檔,否則 .tscn 場景可能因為使用 Base64 編碼二進位資料而變得巨大。

小訣竅

如果有後期處理的需要,可以使用圖像編輯器查看並編輯所生成的 EXR 檔。不過請注意,重新烘焙貼圖會覆蓋你對 EXR 檔的修改。