前言
在Unity中 同网格同材质的模型是可以合批的
动态批处理和静态批处理都可以合批 但是都有其限制
动态批处理有顶点数不能超过900的限制 只适合比较简单的模型
静态批处理的物体不能移动、旋转、缩放 并且需要消耗额外的内存来存储合并后的物体
如果动态静态批处理都无法使用 能否用其他方式合批呢?
可以尝试一下GPU Instance 虽然也有所限制 但是提供了更多可能
使用GPU Instancing的条件
1.Shader支持GPU Instancing
2.硬件支持GPI Instancing
3.代码动态绘制物体
硬件需求:
GPU Instancing is available on the following platforms and APIs:
·DirectX 11 and DirectX 12 on Windows
·OpenGL Core 4.1+/ES3.0+ on Windows, macOS, Linux, iOS and Android
·Metal on macOS and iOS
·Vulkan on Windows and Android
·PlayStation 4 and Xbox One
·WebGL (requires WebGL 2.0 API)
限制情况:
下列情况不能使用Instancing:
·使用Lightmap的物体
·受不同Light Probe / Reflection Probe影响的物体
·使用包含多个Pass的Shader的物体,只有第一个Pass可以Instancing前向渲染时,
受多个光源影响的物体只有Base Pass可以instancing,Add Passes不行
GPU Instance测试
GPU Instancing确实可以动态合批 但是需要Shader + 硬件 + 代码的支持
好处是可以解决动态批处理解决不了的问题 没900顶点的限制
ps:虽然官方的Standard Shader提供了GPU Instance的选项 但是给材质设置不同颜色后合批失败了 这里有坑 使用了其他Shader后解决
测试效果:
测试代码:
using UnityEngine;
using System.Collections.Generic;
///