Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Graphics.DrawMesh renders in the wrong position

Discussion in 'Scripting' started by Silly_Rollo, May 1, 2019.

  1. Silly_Rollo

    Silly_Rollo

    Joined:
    Dec 21, 2012
    Posts:
    501
    I'm trying to create a decal system in 2019.1 but Graphics.DrawMesh renders the quads in the wrong position (clustered around Vector3.Zero position).

    I create them like this
    Code (csharp):
    1.  
    2. var startSize = _decalRange.Get(Game.Random);
    3.             var decal = new Decal {
    4.                 Color = Randomize(color),
    5.                 Matrix = Matrix4x4.TRS(position, Quaternion.LookRotation(normal), new Vector3(startSize, startSize, startSize))
    6.             };
    7.  
    and display them like this
    Code (csharp):
    1.  
    2.             for (int i = 0; i < _decals.Count; i++) {
    3.                 _decalBlock.SetVector("_Color", _decals[i].Color);
    4.                 Graphics.DrawMesh(_decalMesh, _decals[i].Matrix, _decalMaterial, 0, PlayerCamera.Cam, 0, _decalBlock);
    5.             }
    6.  
    To confirm they should be displaying in the correct location I have this debug code which displays exactly where I expect
    Code (csharp):
    1.  
    2. private void OnDrawGizmosSelected() {
    3.             for (int i = 0; i < _decals.Count; i++) {
    4.                 Gizmos.matrix = _decals[i].Matrix;
    5.                 Gizmos.DrawCube(Vector3.zero, Vector3.one);
    6.             }
    7.         }
    8.  
    The gizmos are in the right places, but my quads are all rendered near origin. Any ideas what I'm doing wrong?
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,083
    You can also call DrawMesh with just a mesh/position/rotation so it might be worth trying it with that to see if it's an issue with your matrix?