Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity 5: Automatically specifying the triangle list for the vertices of a mesh

Is there a way or algorithm for automatically specifying how a list of vertices should be connected?

For example lets say I have this (in 2D for simplicity):

     *(0,0)     *(2,0)

*(-1,-1)   *(1,-1)

So I add the vertices in the order (-1,-1),(0,0),(1,-1),(2,0) and then to the triangle list I add the indices 0,1,2 and 2,1,3 and this should generate a mesh with two triangles.

However, as the number of vertices get larger the process of defining the triangles get more tedious, unless of course I add the vertices in a sort of sorted order so that I can keep track of which triangle i'm and just add an offset each time (which I don't want to do).

Where i'm going with this is this (again in 2D for simplicity): I want to define an array, say:

[0 , 0 , 1 , 0 , 1 , 0]

[0 , 0 , 0 , 0 , 0 , 0]

[0 , 1 , 0 , 1 , 0 , 0]

which would represent the xy-plane and points in that array are vertices (1) or contain nothing (0). And so, array[0,0] would be x=0, y=0 (nothing) and array[2,1] (a vertex) would be x=1, y=2. Using this, I'd then like to generate a mesh.

Is this possible? Any help would be appreciated.

Thanks

EDIT: I know there's a triangulate script but it only works for 2D.

like image 828
Amposter Avatar asked Feb 14 '16 17:02

Amposter


People also ask

How many triangles can a mesh have in Unity?

The size of the triangle array must always be a multiple of 3. Vertices can be shared by simply indexing into the same vertex. If the Mesh contains multiple sub-meshes (Materials), the triangle list will contain all the triangles belonging to all its sub-meshes.

How do you find the triangle in Unity?

Calculate the number of vertices: Indices go from 0 to triangleCount + 1 , so there are triangleCount + 2 (there are triangleCount + 1 perimeter vertices, and the center vertex). Calculate the triangles: Each triangle is made from vertex 0 , i + 1 and i + 2 , where i ranges from 0 to triangleCount - 1 .

How many triangles are in a mesh?

In a large mesh, there could be eight or more triangles meeting at a single vertex - by processing those vertices just once, it is possible to do a fraction of the work and achieve an identical effect. In many computer graphics applications it is necessary to manage a mesh of triangles.


1 Answers

No, you simply do that manually.

That's that.

Note that with a routine with one line of code you can "automate" that process.

If you are learning about mesh, it's critical to understand that you DO NOT HAVE TO use duplicated vertices, if you don't happen to want to.

Good luck and congratulations on asking the only worthwhile question in the Unity3D tag all week :O

(1) I urge you to be familiar with tis "gotchya" when weaving mesh in Unity

http://answers.unity3d.com/answers/417487/view.html

Note that the same question gets asked on here over and over.

(2) I urge you the study the three linked answers in there.

(3) Be sure to know about this ... http://answers.unity3d.com/answers/352167/view.html

(4) Don't forget to remember the chirality of Unity .. http://answers.unity3d.com/answers/267076/view.html


One small point. It goes without saying you do not use mesh colliders, for almost any reason ever in video games. It is extremely confusing that Unity included the concept, and it endlessly confuses new and hobbyist programmers. (If you are building your own mesh, and even adding dynamic colliders, all you do is use a box collider that is a good size.)

like image 160
Fattie Avatar answered Sep 28 '22 12:09

Fattie