若是项目中使用GPU Instancing的话,不少人都会用底层API Graphics.DrawMeshInstanced在Update中绘制。dom
咱们先来作个极端的测试,以下代码所示。测试
void Update() { for (int i = 0; i < 1024; i++) { Graphics.DrawMeshInstanced(mesh, 0, material, m_atrix4x4s, 1023); } }
以下图所示,能看出来在Update每帧都有一个比较高的耗时。优化
using UnityEngine; using UnityEngine.Rendering; public class G1 : MonoBehaviour { //GPU instancing材质 public Material material; //GPU instancing网格 public Mesh mesh; //随便找个位置作随机 public Transform target; //是否使用cammandBuffer渲染 public bool useCommandBuffer = false; //观察摄像机 public Camera m_Camera; private Matrix4x4[] m_atrix4x4s = new Matrix4x4[1023]; void Start() { CommandBufferForDrawMeshInstanced(); } private void OnGUI() { if (GUILayout.Button("<size=50>当位置发生变化时候在更新</size>")) { CommandBufferForDrawMeshInstanced(); } } void Update() { if (!useCommandBuffer) { GraphicsForDrawMeshInstanced(); } } void SetPos() { for (int i = 0; i < m_atrix4x4s.Length; i++) { target.position = Random.onUnitSphere * 10f; m_atrix4x4s[i] = target.localToWorldMatrix; } } void GraphicsForDrawMeshInstanced() { if (!useCommandBuffer) { SetPos(); Graphics.DrawMeshInstanced(mesh, 0, material, m_atrix4x4s, m_atrix4x4s.Length); } } void CommandBufferForDrawMeshInstanced() { if (useCommandBuffer) { SetPos(); if (m_buff != null) { m_Camera.RemoveCommandBuffer(CameraEvent.AfterForwardOpaque, m_buff); CommandBufferPool.Release(m_buff); } m_buff = CommandBufferPool.Get("DrawMeshInstanced"); for (int i = 0; i < 1; i++) { m_buff.DrawMeshInstanced(mesh, 0, material, 0, m_atrix4x4s, m_atrix4x4s.Length); } m_Camera.AddCommandBuffer(CameraEvent.AfterForwardOpaque, m_buff); } } CommandBuffer m_buff = null; }
若是每帧的调用Graphics.DrawMeshInstanced的位置矩阵和MaterialPropertyBlock参数都是一致,我就在想可否进行优化。code
其实CommandBuffer也有DrawMeshInstanced方法,这样就能够不用在Update里每帧调用了。当位置矩阵或者MaterialPropertyBlock参数发生变化时在调用DrawMeshInstanced,放入CommandBuffer中渲染。以下图所示,使用CommandBuffer.DrawMeshInstanced只有当元素位置改变时在刷新,这样明显效率高了不少。orm
结合实际项目,好比以前作过的地面的草、石头、栅栏一旦建立出来之后都不会去改变位置,更适合用commandbuffer来作。固然裁剪还须要本身来作,GPU instancing不支持自动裁剪
blog