I have the following code trying to test out rendering mesh manually with Unity 2020.1 using URP (I commented out mesh generation since I know that works):
public class DrawMeshTestMB : MonoBehaviour {
[SerializeField]
private int _width;
[SerializeField]
private int _height;
private Material _material;
private Mesh _mesh;
private Vector3[] _vertices;
private Vector2[] _uvs;
private int[] _triangles;
private List<Vector3> _positions = new List<Vector3>();
private Matrix4x4[] _matrices;
private List<int> _t;
// attach OnEndCameraRendering event
private void Awake() {
// generate mesh / material data
for (var x = 0; x < _width; x++) {
for (var y = 0; y < _height; y++) {
var matrixIndex = x + (y * x);
var position = transform.position + new Vector3(0.45f * x, 0.7f * y);
var rotation = Quaternion.Euler(Vector3.zero);
var scale = Vector3.one;
_positions.Add(position);
_matrices[matrixIndex] = Matrix4x4.TRS(position, rotation, scale);
}
}
}
private void OnEndCameraRendering(ScriptableRenderContext context, Camera camera) {
GameLogger.LogDebug("OnEndCameraRendering");
CommandBuffer myCommandBuffer = new CommandBuffer();
// this works
for (var i = 0; i < _matrices.Length; i++) {
myCommandBuffer.DrawMesh(_mesh, _matrices[i], _material, 0);
}
// this does not
myCommandBuffer.DrawMeshInstanced(_mesh, 0, _material, -1, _matrices);
Graphics.ExecuteCommandBuffer(myCommandBuffer);
}
}
The issue I am running into in that while myCommandBuffer.DrawMesh() works, myCommandBuffer.DrawMeshInstanced() does not and I can’t find any reason why it would not. The material is GPU instancing enabled and there are no error in the console. I even look into the frame debugger and the call to render does not even show up. It is using the same data so it is not like I can creating the data differently.
Does anyone know what is wrong with my code here? Is there some weird setting that might be wrong or something not obvious?