6

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.

Amposter
  • 787
  • 2
  • 8
  • 27

2 Answers2

3

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.)

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • 1
    Thanks for the reply. I want to actually use duplicate meshes since I want the mesh to be flat shaded. With regards to the main question then, how do you then triangulate a procedurally generated smooth terrain (well I want to make a low poly terrain something like this: http://eternityh2.tumblr.com/)? I don't want to use Unity's terrain editor as I want to actually do this in a script and generate it on the fly. I suppose depending on how I populate the array I mentioned in the question I could write some hacky code that would work specifically for my application but is there any general way? – Amposter Feb 16 '16 at 17:45
  • 1
    Are you basically asking **"how to generate terrain procedurally"?** That is a ***huge*** question man - I would encourage you to ask a new question. try to be very specific about what sort of terrain you want. (maybe include examples.) That is a whole field of endeavour, like, you might hire a team of five people to work on that aspect of a game for a year. Often it is done in shader you know? – Fattie Feb 16 '16 at 18:06
  • 1
    In the first instance, be sure to google "unity procedural terrain" .. consider things like http://answers.unity3d.com/questions/123504/procedural-terrain.html Also, **be sure to visit the asset store** and look at the state of the art from different studios on this. There are many many hugely different mathematical approaches, from voxel-related to things like this https://www.assetstore.unity3d.com/en/#!/content/48730 and of course this type of thing https://www.assetstore.unity3d.com/en/#!/content/3640 which is a whole field of endeavour iniself. – Fattie Feb 16 '16 at 18:08
  • 1
    once again, in your new question I would encourage, be specific about what you are looking for in that field you know. Google around things like "terrain perlin noise" also https://www.assetstore.unity3d.com/en/#!/content/37649 Check this shit out ... https://www.youtube.com/watch?v=FYTsB2wpnDQ impressive right? – Fattie Feb 16 '16 at 18:10
  • 1
    That is essentially what I want to do just with less polygons. But I'm assuming he used perlin noise to generate a heightmap which can then be applied to a terrain in Unity whereas I want to procedurally generate the mesh itself but I'm not sure how to generate the the triangle list. I will post a proper question with more detail soon soon. It's just that varsity has started again and so it's quite busy atm. Thanks for the help though :) – Amposter Feb 16 '16 at 19:53
  • By all means post a new question, and be specific about what you're after. what sport buddy – Fattie Feb 16 '16 at 20:10
  • Oh sorry, I meant university. In South Africa, varsity and university are essentially the same thing. I'm doing my Honors in Computer Science at the University of Cape Town which is the equivalent of the British 4th year. I reaaally want to go into games and graphics/animation as a profession but there's very little modules on those topics and so I spend a lot of my free time working on those sorts of things. – Amposter Feb 18 '16 at 09:28
1

Turns out it can be done. There are more sophisticated methods for doing this like Marching Cubes, however, they are fairly complicated to implement and won't work because they're just way too inefficient to run in real-time (EDIT: unless you offload to GPU).

Amposter
  • 787
  • 2
  • 8
  • 27
  • 1
    Not strictly true... You can offload marching cubes/marching tetrahedra to the GPU which is capable of handling it all in realtime. Of course, if you want to get that mesh back to the CPU, you have all the standard bandwidth issues. – Basic Nov 21 '17 at 14:17
  • 1
    Ah, right. I don't know what offloading to GPU entails but I suppose you could if you still felt like Marching Cubes is a feasible solution to implement given all the mentioned concerns. – Amposter Nov 22 '17 at 13:35
  • 2
    If you're interested, the information here: https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch01.html Is a great intro. In fact, th8e whole book is invaluable. – Basic Nov 22 '17 at 22:02