1
\$\begingroup\$

I would like to draw a procedural mesh as a gizmo:

Gizmos.DrawMesh(mesh);

however this does not render anything no matter what. I can confirm that the mesh in question is a correct one by both GetComponent<MeshFilter>().mesh = mesh assigning it to Renderer and rendering it using default material and Gizmo.DrawLine drawing it edge-by-edge.
No error are logged in debug window, setting position or flipping triangle orientation has no effect neither. Version of Unity is 5.6.2f1. Am I missing something?

void Start ()
{
   GetComponent<MeshFilter>().mesh = mesh; //renders without any problems
}

void OnDrawGizmos()
{
   Gizmos.color = Color.red;
   Gizmos.DrawMesh(mesh);//nothing rendered
   //renders without any problems
   for (int i = 0; i < mesh.triangles.Length / 3; i++)
   {
      Gizmos.DrawLine(mesh.vertices[mesh.triangles[i * 3 + 2]], mesh.vertices[mesh.triangles[i * 3]]);
      Gizmos.DrawLine(mesh.vertices[mesh.triangles[i * 3]], mesh.vertices[mesh.triangles[i * 3 + 1]]);
      Gizmos.DrawLine(mesh.vertices[mesh.triangles[i * 3 + 1]], mesh.vertices[mesh.triangles[i * 3 + 2]]);
   }
}
\$\endgroup\$
  • \$\begingroup\$ I'm not able to reproduce this issue with meshes I've tried so far - they all render correctly with gizmos as expected. Can you try it with variants on the problem mesh to see if there's a particular quirk to the mesh you're using that the gizmo renderer rejects? \$\endgroup\$ – DMGregory Oct 14 '17 at 22:31
  • \$\begingroup\$ @DMGregory It appears to be broken for every mesh not just particular one, e.g. even for trivial meshes like [5.0,4.0];[-5.0,4.0];[-5.0,-4.0];5.0,-4.0] with indices 3,2,1,1,0,3. Also forgot to mention - it is 2D (ortho camera) if that ever plays role. \$\endgroup\$ – wondra Oct 15 '17 at 1:04
  • \$\begingroup\$ It's working with every mesh I've tried, including built-in primitives and triangle meshes generated at runtime. Is it possible your winding order is causing it to be rejected as a back-face, or that it's getting drawn off-screen / too close for the near plane? Otherwise this might be a Unity bug or a corruption of some kind. See if you can DrawMesh with a primitive cube in a new project to test whether there's something amiss with your editor installation. \$\endgroup\$ – DMGregory Oct 15 '17 at 1:14
  • \$\begingroup\$ @DMGregory it was the normals - not that I did not try adding them but flipping orientation was also required. Never tried combination of both. The fact that the default amterial does not require normal did not contribute to finding the problem neither. This might be a silly error, but still I feel at least some warning like "error: unassigned normals" should be produced for a mesh being rendered, making the API more user friendly. \$\endgroup\$ – wondra Oct 15 '17 at 12:35
1
\$\begingroup\$

Unity does not recalculate normals automatically, which in combination with the default material not requiring normals to render caused the confusion. Either

mesh.normals = Enumerable.Repeat(Vector3.forward, mesh.vertices.Length).ToArray();

or

mesh.RecalculateNormals();

will do the trick.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.