10,000 Cubes or a Mesh that looked like 10,000 Cubes

I’m creating 10,000 cubes programatically. I’ll be moving these around as 1 giant “monster”. I will not be moving them individually.

To keep the code clean and elegant, I’d like to create 10,000 cube primitives rather than the complicated code to create vertices and triangles for a mesh that looks like 10,000 cubes. Is the extra performance of the mesh worth the trouble? I suspect yes, but was hoping for an answer before attempting the mesh code.

On a related note, the cube generated by “CreatePrimitive(PrimitiveType.Cube)” has 24 vertices (4 each side) and 12 triangles. Since many of the vertices are the same, could you create a cube that only has 8 vertices (1 for each corner of the cube) and 12 triangles (that re-use many of the same vertices)?

Take a look at Mesh.CombineMeshes which is new for 2.6 and may be faster, I dont know yet.

You could also look at the CombineChildren script.

You can’t feasibly have 10,000 draw calls on even the best computer and expect to have a decent framerate. Not sure if dynamic batching in Unity 3 would help that enough, with such a large amount of objects, but in any case, drawing it as one mesh (actually it would have to be 4 meshes because of the 64K vertex limit) will still be faster.

Not really, since vertices have to be split because of UV seams and normals. Theoretically you can make an untextured cube with 8 vertices, but the lighting looks weird.

–Eric

Thanks so much for the quick replies. My questions are completely answered. Appreciate no-one giving me a hard-time about them being newbie. I built my first code generated triangle yesterday, and I’ve got big dreams.

Eric5h5 - One last related question. Why does Unity need you to define the triangles? "Mesh.triangles = "

Since vertices should not be shared between triangles (due to the aforementioned UV and normal issues), can’t Unity just assume the first 3 vertices are a triangle, and the next 3 vertices are another triangle? I’m not knocking Unity whatsoever, just curious what experts use .triangles for.

No, not at all. Vertices can’t be feasibly shared between different faces on a cube, but they can be shared in many other cases, such as spheres, the terrain, a plane, etc. etc. And even on a cube, you do have vertices shared between the two triangles on each face. You absolutely need to tell Unity which vertices are used for which triangles.

–Eric

3 Vertexes doesn’t always mean a Triangle in a 3D Mesh.
Triangle Strips/Fans just need 1 more Vertex to define each new Triangle, etc.

You guys are the best!

If you ever have database design or website html/javascript/dom questions, I’m your man.