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

Question Graphics.DrawMeshNow has y offset in Build

Discussion in 'General Graphics' started by SpookyCat, May 13, 2023.

  1. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,748
    I am doing a script which renders a mesh to a RenderTexture using the Graphics.DrawMeshNow(Mesh mesh, Matrix4x4 tm) method. It works perfectly in the Editor and in Play mode, but when I do a build it renders with a small Y offset.
    To test that it is the DrawMeshNow method I draw the exact same mesh with the same matrix using GL calls and that works finer in Build, but I really dont want to use that as it has the extra overhead. Is there an issue with Graphics.DrawMeshNow in builds, or is there some value I have missed? Below is the code I am using.
    This draws with an offset:
    Code (CSharp):
    1. Graphics.DrawMeshNow(_mesh, tm);
    This works fine with the same mesh and matrix values
    Code (CSharp):
    1. int[] tris = _mesh.triangles;
    2. Vector3[] verts = _mesh.vertices;
    3. Vector2[] uvs = _mesh.uv;
    4.  
    5. GL.MultMatrix(tm);
    6. GL.Begin(GL.TRIANGLES);
    7. GL.Color(Color.white);
    8.  
    9. for ( int i = 0; i < tris.Length; i += 3 )
    10. {
    11.     int tri = tris[i];
    12.     GL.MultiTexCoord(0, new Vector3(uvs[tri].x, uvs[tri].y, 0.0f));
    13.     GL.Vertex3(verts[tri].x, verts[tri].y, verts[tri].z);
    14.  
    15.     tri = tris[i + 1];
    16.     GL.MultiTexCoord(0, new Vector3(uvs[tri].x, uvs[tri].y, 0.0f));
    17.     GL.Vertex3(verts[tri].x, verts[tri].y, verts[tri].z);
    18.  
    19.     tri = tris[i + 2];
    20.     GL.MultiTexCoord(0, new Vector3(uvs[tri].x, uvs[tri].y, 0.0f));
    21.     GL.Vertex3(verts[tri].x, verts[tri].y, verts[tri].z);
    22. }
    23.  
    24. GL.End();
    25.