news 2026/9/25 8:50:27

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【光照】UnityURP[光照贴图]GPU instancing在静态动态物体上的应用

静态物体GPU Instancing与光照贴图

‌技术要点‌:

‌静态标记‌:物体需标记为Batching Static,但需禁用静态合批以避免与GPU Instancing冲突。

‌光照贴图绑定‌:通过LightmapIndex和LightmapScaleOffset手动绑定烘焙结果。

‌Shader适配‌:需在着色器中添加实例化支持与光照贴图采样逻辑。

‌示例代码‌:

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() {

// 生成实例变换矩阵

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

// 模拟不同光照贴图偏移(需与烘焙数据匹配)

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

}

}

‌实现说明‌:

使用MaterialPropertyBlock传递每实例的光照贴图UV偏移参数。

着色器中需声明UNITY_LIGHTMAP_ON宏并采样unity_Lightmap纹理。

动态物体GPU Instancing与光照探针

‌技术要点‌:

‌光照探针替代‌:动态物体依赖Light Probes获取间接光照。

‌混合光源支持‌:光源设为Mixed模式,静态阴影烘焙到光照贴图,动态物体接收实时阴影。

‌实例化属性扩展‌:通过MaterialPropertyBlock传递探针数据。

‌示例代码‌:

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();

// 为每个实例设置光照探针数据

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

}

}

‌实现说明‌:

通过LightProbes.GetInterpolatedProbe获取动态物体的光照探针数据。

着色器中需使用SHADERGRAPH_BAKED_LIGHT_PROBES宏处理探针数据。

Shader适配关键代码(URP Shader Graph)

‌静态光照贴图采样‌:在Shader Graph中添加Lightmap节点,并通过Custom Function节点接入实例化的UV偏移参数。

‌动态探针支持‌:添加Baked Light Probes节点,并与实例化属性_LightProbeData关联。

‌优先级注意‌:若同时启用SRP Batcher,需确保材质兼容性(禁用MaterialPropertyBlock)

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/31 15:55:14

测试左移与测试右移

1、前言 测试左移以及测试右移&#xff0c;能够让测试拥有更多的主动权&#xff0c;有更充足的时间进行测试&#xff0c;同时不会像之前因为质量差风险高每次都延期上线&#xff0c;并且产品的线上质量也能有保证。 不管是测试左移还是测试右移&#xff0c;都是为产品质量服务…

作者头像 李华
网站建设 2026/9/22 15:21:51

CANN软件栈 昇腾AI计算神经网络计算架构全景解读

目录 &#x1f3af; 摘要 &#x1f3d7;️ 第一章 CANN设计哲学 从专用芯片到全栈生态的系统思考 1.1 计算范式的历史转折点 1.2 全栈协同的真正含义 &#x1f527; 第二章 CANN架构全景 五层设计的协同奥秘 2.1 整体架构&#xff1a;不只是分层&#xff0c;而是微分与积…

作者头像 李华
网站建设 2026/9/25 4:34:30

Netbox拓扑视图:让复杂的网络关系一目了然

Netbox拓扑视图&#xff1a;让复杂的网络关系一目了然 【免费下载链接】netbox-topology-views A netbox plugin that draws topology views 项目地址: https://gitcode.com/gh_mirrors/ne/netbox-topology-views 在网络运维工作中&#xff0c;你是否曾经面对密密麻麻的…

作者头像 李华
网站建设 2026/9/25 0:06:30

5步掌握veScale:从单机到分布式大模型训练的终极指南

5步掌握veScale&#xff1a;从单机到分布式大模型训练的终极指南 【免费下载链接】veScale A PyTorch Native LLM Training Framework 项目地址: https://gitcode.com/gh_mirrors/ve/veScale 你是否曾为训练大语言模型时遇到的内存不足、训练速度慢、扩展困难等问题而烦…

作者头像 李华