[Unity]CommandBuffer.DrawInstance

2020/01 03 17:01
public class DrawInstance : MonoBehaviour
{
    private CommandBuffer mBuff = null;
    public Material mMat;
    public Mesh mMesh;
    private Matrix4x4[] mMatrix44;
    void Start()
    {
        mMatrix44 = new Matrix4x4[100];
        Vector3 pos = Vector3.zero;
        for(int i=0; i<mMatrix44.Length; ++i)
        {
            pos.x = (i / 10) * 1.3f - 5 ;
            pos.y = (i % 10) * 1.3f - 5 ;
            mMatrix44[i] = Matrix4x4.TRS( pos, Quaternion.Euler(0, 90, 0), Vector3.one);            
        }
        mBuff = new CommandBuffer();
        mBuff.name = "MyCube";
        mBuff.DrawMeshInstanced(mMesh, 0, mMat, 0, mMatrix44, mMatrix44.Length);
        Camera.main.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, mBuff);
    }
}