🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Unity CommandBuffer.DrawMeshInstanced Does Not Work With URP

Started by
0 comments, last by 3dmodelerguy 3 years, 6 months ago

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;

      void OnEnable() {
        RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
      }
      
      void OnDisable() {
        RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
      }

      private void Awake() {
        _mesh = new Mesh();
        _material = Resources.Load<Material>("Materials/TestMaterial");
        _vertices = new Vector3[4];
        _uvs = new Vector2[4];
        _triangles = new int[6];
        _matrices = new Matrix4x4[_width * _height];

        _vertices[0] = new Vector3(0, 1);
        _vertices[1] = new Vector3(1, 1);
        _vertices[2] = new Vector3(0, 0);
        _vertices[3] = new Vector3(1, 0);
        
        _uvs[0] = new Vector2(0, 1);
        _uvs[1] = new Vector2(1, 1);
        _uvs[2] = new Vector2(0, 0);
        _uvs[3] = new Vector2(1, 0);
        
        _triangles[0] = 0;
        _triangles[1] = 1;
        _triangles[2] = 2;
        _triangles[3] = 2;
        _triangles[4] = 1;
        _triangles[5] = 3;
        
        _mesh.vertices = _vertices;
        _mesh.uv = _uvs;
        _mesh.triangles = _triangles;

        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
        foreach (var position in _positions) {
          var rotation = Quaternion.Euler(Vector3.zero);
          var scale = Vector3.one;
          
          myCommandBuffer.DrawMesh(_mesh, Matrix4x4.TRS(position, rotation, scale), _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.

Does anyone know what is wrong with my code here? Is there some weird setting that might be wrong or something not obvious?

This topic is closed to new replies.

Advertisement