找回密码
 立即注册
首页 业界区 业界 【光照】UnityURP[光照贴图]GPU instancing在静态动态物 ...

【光照】UnityURP[光照贴图]GPU instancing在静态动态物体上的应用

寂傧 昨天 11:34
【从UnityURP开始探索游戏渲染】专栏-直达
静态物体GPU Instancing与光照贴图

技术要点‌:


  • 静态标记‌:物体需标记为Batching Static,但需禁用静态合批以避免与GPU Instancing冲突。
  • 光照贴图绑定‌:通过LightmapIndex和LightmapScaleOffset手动绑定烘焙结果。
  • Shader适配‌:需在着色器中添加实例化支持与光照贴图采样逻辑。
示例代码‌:


  • StaticInstancingExample.cs
    1. using UnityEngine;
    2. [ExecuteAlways]
    3. public class StaticInstancingExample : MonoBehaviour {
    4.     public Mesh mesh;
    5.     public Material material;
    6.     public int instanceCount = 100;
    7.     private Matrix4x4[] matrices;
    8.     private Vector4[] lightmapOffsets;
    9.     void Start() {
    10.         // 生成实例变换矩阵
    11.         matrices = new Matrix4x4[instanceCount];
    12.         lightmapOffsets = new Vector4[instanceCount];
    13.         for (int i = 0; i < instanceCount; i++) {
    14.             Vector3 pos = Random.insideUnitSphere * 10f;
    15.             matrices[i] = Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one);
    16.             // 模拟不同光照贴图偏移(需与烘焙数据匹配)
    17.             lightmapOffsets[i] = new Vector4(1, 1, i % 2 * 0.5f, i / 2 * 0.5f);
    18.         }
    19.     }
    20.     void Update() {
    21.         MaterialPropertyBlock props = new MaterialPropertyBlock();
    22.         props.SetVectorArray("_LightmapST", lightmapOffsets); // 传递光照贴图UV偏移
    23.         Graphics.DrawMeshInstanced(mesh, 0, material, matrices, instanceCount, props);
    24.     }
    25. }
    复制代码
实现说明‌:


  • 使用MaterialPropertyBlock传递每实例的光照贴图UV偏移参数。
  • 着色器中需声明UNITY_LIGHTMAP_ON宏并采样unity_Lightmap纹理。
动态物体GPU Instancing与光照探针

技术要点‌:


  • 光照探针替代‌:动态物体依赖Light Probes获取间接光照。
  • 混合光源支持‌:光源设为Mixed模式,静态阴影烘焙到光照贴图,动态物体接收实时阴影。
  • 实例化属性扩展‌:通过MaterialPropertyBlock传递探针数据。
示例代码‌:


  • DynamicInstancingExample.cs
    1. using UnityEngine;
    2. public class DynamicInstancingExample : MonoBehaviour {
    3.     public Mesh mesh;
    4.     public Material material;
    5.     public int instanceCount = 100;
    6.     private Matrix4x4[] matrices;
    7.     void Start() {
    8.         matrices = new Matrix4x4[instanceCount];
    9.         for (int i = 0; i < instanceCount; i++) {
    10.             Vector3 pos = Random.insideUnitSphere * 10f;
    11.             matrices[i] = Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one);
    12.         }
    13.     }
    14.     void Update() {
    15.         MaterialPropertyBlock props = new MaterialPropertyBlock();
    16.         // 为每个实例设置光照探针数据
    17.         LightProbes.GetInterpolatedProbe(transform.position, null, out var probe);
    18.         props.AddVector("_LightProbeData", new Vector4(probe.occlusion, 0, 0, 0));
    19.         Graphics.DrawMeshInstanced(mesh, 0, material, matrices, instanceCount, props);
    20.     }
    21. }
    复制代码
实现说明‌:


  • 通过LightProbes.GetInterpolatedProbe获取动态物体的光照探针数据。
  • 着色器中需使用SHADERGRAPH_BAKED_LIGHT_PROBES宏处理探针数据。
Shader适配关键代码(URP Shader Graph)


  • 静态光照贴图采样‌:在Shader Graph中添加Lightmap节点,并通过Custom Function节点接入实例化的UV偏移参数。
  • 动态探针支持‌:添加Baked Light Probes节点,并与实例化属性_LightProbeData关联。
优先级注意‌:若同时启用SRP Batcher,需确保材质兼容性(禁用MaterialPropertyBlock)
<blockquote>
【从UnityURP开始探索游戏渲染】专栏-直达
(欢迎点赞留言探讨,更多人加入进来能更加完善这个探索的过程,
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册