【光照】UnityURP[光照貼圖]GPU instancing在靜態(tài)動(dòng)態(tài)物體上的應(yīng)用
【從UnityURP開始探索游戲渲染】專欄-直達(dá)
靜態(tài)物體GPU Instancing與光照貼圖
?技術(shù)要點(diǎn)?:
- ?靜態(tài)標(biāo)記?:物體需標(biāo)記為
Batching Static,但需禁用靜態(tài)合批以避免與GPU Instancing沖突。 - ?光照貼圖綁定?:通過
LightmapIndex和LightmapScaleOffset手動(dòng)綁定烘焙結(jié)果。 - ?Shader適配?:需在著色器中添加實(shí)例化支持與光照貼圖采樣邏輯。
?示例代碼?:
-
StaticInstancingExample.cs
using UnityEngine; [ExecuteAlways] public class StaticInstancingExample : MonoBehaviour { public Mesh mesh; public Material material; public int instanceCount = 100; private Matrix4x4[] matrices; private Vector4[] lightmapOffsets; void Start() { // 生成實(shí)例變換矩陣 matrices = new Matrix4x4[instanceCount]; lightmapOffsets = new Vector4[instanceCount]; for (int i = 0; i < instanceCount; i++) { Vector3 pos = Random.insideUnitSphere * 10f; matrices[i] = Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one); // 模擬不同光照貼圖偏移(需與烘焙數(shù)據(jù)匹配) lightmapOffsets[i] = new Vector4(1, 1, i % 2 * 0.5f, i / 2 * 0.5f); } } void Update() { MaterialPropertyBlock props = new MaterialPropertyBlock(); props.SetVectorArray("_LightmapST", lightmapOffsets); // 傳遞光照貼圖UV偏移 Graphics.DrawMeshInstanced(mesh, 0, material, matrices, instanceCount, props); } }
?實(shí)現(xiàn)說明?:
- 使用
MaterialPropertyBlock傳遞每實(shí)例的光照貼圖UV偏移參數(shù)。 - 著色器中需聲明
UNITY_LIGHTMAP_ON宏并采樣unity_Lightmap紋理。
動(dòng)態(tài)物體GPU Instancing與光照探針
?技術(shù)要點(diǎn)?:
- ?光照探針替代?:動(dòng)態(tài)物體依賴
Light Probes獲取間接光照。 - ?混合光源支持?:光源設(shè)為
Mixed模式,靜態(tài)陰影烘焙到光照貼圖,動(dòng)態(tài)物體接收實(shí)時(shí)陰影。 - ?實(shí)例化屬性擴(kuò)展?:通過
MaterialPropertyBlock傳遞探針數(shù)據(jù)。
?示例代碼?:
-
DynamicInstancingExample.cs
using UnityEngine; public class DynamicInstancingExample : MonoBehaviour { public Mesh mesh; public Material material; public int instanceCount = 100; private Matrix4x4[] matrices; void Start() { matrices = new Matrix4x4[instanceCount]; for (int i = 0; i < instanceCount; i++) { Vector3 pos = Random.insideUnitSphere * 10f; matrices[i] = Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one); } } void Update() { MaterialPropertyBlock props = new MaterialPropertyBlock(); // 為每個(gè)實(shí)例設(shè)置光照探針數(shù)據(jù) LightProbes.GetInterpolatedProbe(transform.position, null, out var probe); props.AddVector("_LightProbeData", new Vector4(probe.occlusion, 0, 0, 0)); Graphics.DrawMeshInstanced(mesh, 0, material, matrices, instanceCount, props); } }
?實(shí)現(xiàn)說明?:
- 通過
LightProbes.GetInterpolatedProbe獲取動(dòng)態(tài)物體的光照探針數(shù)據(jù)。 - 著色器中需使用
SHADERGRAPH_BAKED_LIGHT_PROBES宏處理探針數(shù)據(jù)。
Shader適配關(guān)鍵代碼(URP Shader Graph)
- ?靜態(tài)光照貼圖采樣?:在Shader Graph中添加
Lightmap節(jié)點(diǎn),并通過Custom Function節(jié)點(diǎn)接入實(shí)例化的UV偏移參數(shù)。 - ?動(dòng)態(tài)探針支持?:添加
Baked Light Probes節(jié)點(diǎn),并與實(shí)例化屬性_LightProbeData關(guān)聯(lián)。
?優(yōu)先級(jí)注意?:若同時(shí)啟用SRP Batcher,需確保材質(zhì)兼容性(禁用MaterialPropertyBlock)
【從UnityURP開始探索游戲渲染】專欄-直達(dá)
(歡迎點(diǎn)贊留言探討,更多人加入進(jìn)來能更加完善這個(gè)探索的過程,??)

該專欄探討了Unity URP中靜態(tài)和動(dòng)態(tài)物體的GPU實(shí)例化技術(shù)要點(diǎn)。對于靜態(tài)物體,需標(biāo)記為BatchingStatic并禁用靜態(tài)合批,通過LightmapIndex綁定光照貼圖,在著色器中添加實(shí)例化支持。動(dòng)態(tài)物體則依賴光照探針獲取間接光照,使用MaterialPropertyBlock傳遞探針數(shù)據(jù)。文章提供了StaticInstancingExample和DynamicInstancingExample兩個(gè)示例代碼,展示如何實(shí)現(xiàn)矩陣變換和光照數(shù)據(jù)傳遞。在Shader適配方面,需處理光照貼圖采樣和探針數(shù)據(jù),
浙公網(wǎng)安備 33010602011771號(hào)