Understanding How Unity's Mesh Object Groups Triangles from Vertices

I’m creating a 2D grid of points like such:

[
    [0, 0, 0], [1, 0, 0], [2, 0, 0],    // bottom row
    [0, 1, 0], [1, 1, 0], [2, 1, 0]     // top row
   
]

Ignoring syntax, consider it as a 3 x 2 grid space on which I’m trying to create a Mesh grid that is made of a series of triangles, like such:

This is a much bigger grid obviously, but the points creating this triangle are found within the above array example: t = [[0, 0, 0], [0, 1, 0], [1, 0, 0]]

Question
My question is how Unity’s Mesh object interprets a list of vertices as triangles. After reading the documentation, I’ve gathered that Mesh.vertices is an array of Vector3's and Mesh.triangles is a list of indexes into the Mesh.vertices array.

Mesh.vertices = [ [0, 0, 0], [1, 0, 0], [2, 0, 0] , [0, 1, 0], [1, 1, 0], [2, 1, 0] ]
Mesh.triangles = [0, 3, 1]

So does Unity’s Mesh object interpret triangles simply by taking every group of three, starting with the beginning? So mesh.triangles[0], mesh.triangles[1], mesh.triangles[2] is always the first triangle and so on, so forth such that mesh.triangles[x+0], mesh.triangles[x+1, mesh.triangles[x+2] is always the xth triangle?

I don’t know how else it would be, it’s just been hard to wrap my head around it.

Yup, just like OpenGL did… it’s just a stream of indices grouped in 3s.

Welcome to procedural geometry generation… it’s SUPER easy to do in Unity, best engine ever for it.

I have a little project I call MakeGeo with some random examples in it if you care to peruse.

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

https://github.com/kurtdekker/makegeo

https://gitlab.com/kurtdekker/makegeo

https://sourceforge.net/p/makegeo

1 Like

Thanks for the confirmation! I’ve done a little bit in unity, but my next project is integrating some procgen (mostly as a learning experience at this point).

The makegeo resource you linked to looks super helpful! Thanks for that, I’m sure it’ll keep me busy for hours!

1 Like

Awesome: just remember winding order: Unity is left-hand, which means if you wind the triangle vertices going clockwise, you can see the triangle. If you wind them counter-clockwise, it will be facing the opposite direction.

Lighting normal and UV are all contained at the vertex level, so if you share vertices between triangles, you will have a smooth look (no faceting). If you want faceting, put the vertex in twice, but with different normals.

For auto normal calculation there’s a recalculate normals call you can do on the mesh, save a lot of hassle.

if you want custom face weighted normals, I can help, it’s not that hard … but as Kurt-Dekker said, it’s a hassle.
if you just want it done, use mesh.RecalculateNormals().
dont forget mesh.RecalculateBounds() if you’re modifying positions of the vertices which do affect the axis-aligned box around the model. I think this is important mainly for having proper frustum culling, but can’t remember what else depends on it.

2 Likes

I just ran into this when trying to go from Unity to Revit. Revit has an example of importing json data with the triangles/facets formatted like that.

Will this help me format json like that?

JSON is just a data format. I don’t use it in MakeGeo.

Look at your Revit code and see what POCOs shape it is using to import the JSON, and that is the POCO you would also use to write it out.

https://en.wikipedia.org/wiki/Plain_old_CLR_object

2 Likes

I’m a bit confused by your post. Unity does not use json in any way when it comes to meshes and you said you go “from Unity to Revit”? What does that even mean? If you talk about Audodesk Revit I’m a bit confused. Unity is not a modelling software but a game engine.

Of course you can use json in Unity to roll your own mesh format and I’m sure there are already a few pseudo standards out there. Over here at the end of my answer I’ve linked to my own very simple json mesh loader which is used to load that CC turtle mesh from my own json format. Not counting my SimpleJSON parser, the mesh loader itself only has about 100 lines of code ^^. It builds the mesh out of triangle fans. So each sub array in the “faces” array is a triangle fan. In this particular case I always build quads.

@Kurt-Dekker @Bunny83

I’m using the json to store the mesh vertices and triangles for serializing.

Bunny, Revit has an example for the API that enables importing json data to create a model.

Although I am probably going another route converting the models to Revit elements.

My issue was getting the triangles grouped in the brackets by three. That’s how Revits json data is formatted like in their example.