- Home /
Graphics.DrawMesh not working
I tried rendering a cube with Graphics.DrawMesh on Unity 4.3, but nothing seems to happen. I have an empty scene with a camera looking at the origin. The following script is attached to the camera:
using UnityEngine;
public class DrawMesh : MonoBehaviour
{ /* 5_ _ _6
/| /|
1/_|_ 2/ |
| 4|_ _|_|7
| / | /
|/_ _ _|/
0 3 */
Mesh mesh;
void Start() {
mesh = new Mesh();
Vector3[] vertices = new Vector3[] {
new Vector3(-1, -1, -1),
new Vector3(-1, +1, -1),
new Vector3(+1, +1, -1),
new Vector3(+1, -1, -1),
new Vector3(-1, -1, +1),
new Vector3(-1, +1, +1),
new Vector3(+1, +1, +1),
new Vector3(+1, -1, +1)};
int[] triangles = new int[] {
0, 1, 2, 2, 3, 0,
1, 5, 6, 6, 2, 1,
3, 2, 6, 6, 7, 3,
4, 5, 1, 1, 0, 4,
0, 3, 7, 7, 4, 0,
7, 6, 5, 5, 4, 7};
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
void Update () {
Graphics.DrawMeshNow(mesh, Vector3.zero, Quaternion.identity);
}
}
What am I doing wrong?
Answer by Bunny83 · Dec 04, 2013 at 02:28 PM
Graphics.DrawMesh should work pretty well if you:
actually use Graphics.DrawMesh and not Graphics.DrawMeshNow which is a totally different function.
have Unity pro since this is a pro feature.*It used to be a pro only feature.
DrawMeshNow will draw the mesh immediately. If you use this outside of a rendering callback it has no effect since it will get cleared when the actual rendering starts (which is at the end of each frame). DrawMesh on the other hand "registers" the mesh to be drawn this frame and it's rendered when it's time to do so.
edit
ps: You are aware of the fact that you can't draw a cube with 8 vertices? Your "cube" will look like a lowpoly sphere. Each vertex can only have one normal vector. If you share all vertices like you did, RecalculateNormals will average the normal between all 3 faces that meet at the vertex. The difference is this:
To render a cube like a cube you need at least 24 vertices or it will look like this:
In this answer i've created a frustum mesh for a cameras frustum. If you replace the 8 corners with your cube corners it should work ;) I have the same ordering as you have, but my code duplicates the vertices for each face. So ins$$anonymous$$d of my local "v" array you could use your "vertices" array and would get a cube. If you use RecalculateNormals you can also omit the normal generation ;)
I got it working once I used non-obsolete overload of the Draw$$anonymous$$esh function. Using an obsolete version gives a warining that Draw$$anonymous$$eshNow should be used ins$$anonymous$$d, which is kind of misleading.
However, Unity Pro is not required. You might want to correct that part from your answer. Thanks for your help.
@c1913219:
Well, it used to be required ;) The whole Graphics class was pro only when it has been introduced. Back then there was a note on the Graphics page that it's pro only. Now only a few things from this class requires pro. I've changed my answer. Thanks.
Can you point us to documentation that says that Draw$$anonymous$$eshNow will have no effect outside of a rendering callback?
Answer by diegzumillo · Dec 01, 2013 at 05:59 PM
I'm a javascript user but, at the end of your triangles definition you have "7[};][1]" that looks odd. Try
int[] triangles = new int[] {
0, 1, 2, 2, 3, 0,
1, 5, 6, 6, 2, 1,
3, 2, 6, 6, 7, 3,
4, 5, 1, 1, 0, 4,
0, 3, 7, 7, 4, 0,
7, 6, 5, 5, 4, 7};
$$anonymous$$ust have typoed something there. $$anonymous$$y bad, but it isn't in the actual project and is not the problem.