Search Unity

Question Copying vertices of pre-made mesh for procedural generation

Discussion in 'Scripting' started by Psyworks, Jun 20, 2020.

  1. Psyworks

    Psyworks

    Joined:
    Jul 11, 2016
    Posts:
    5
    Hi all,

    I'm working on a procedural world generator that I would like to essentially be made of large cubes (something that would be the equivalent of like 5x5x5 meters) where instead of the sides of the cubes being flat quads they are made out of a mesh that I've made as an asset (basically just a cliff face). I have got my generator creating a world that is rendered as one large mesh made of the top-face quads (i.e. there is only a top face of the cubes at various heights on a grid, no side or bottom) but I'm really struggling with how to add my cliff mesh as the side faces.

    Basically I need to copy the vertices/triangles of my pre-made mesh, and 'paste' copies of the vertices in different positions on the grid as one big mesh (I don't mind it being a second mesh from the top faces). My problem is that I don't know where to start with copying the existing mesh data and then translating/rotating the new vertices relative to the world. It was easy enough when I was creating the vertices from scratch to define their relative positions, but I'm struggling with how copy and move/rotate vertices that are part of a pre-defined mesh; I could really use some help setting this part up.

    Any help or pointers would be greatly appreciated!

    -Chris
     
  2. Well, it really depends how you made the pre-made mesh and how did you generate your cubes.
    You can start in general way and fix the problems you notice.

    https://docs.unity3d.com/ScriptReference/Mesh.GetVertices.html
    To retrieve the vertices from the mesh and SetVertices to copy into the new one.
    Also similar to the Triangles. Also you need to RecalculateNormals at the end.

    Just place one generated cube and try to copy the vertices into it and see what happens. Probably you will need to fix a lot of things. Like adjust the positions and such.
     
  3. Psyworks

    Psyworks

    Joined:
    Jul 11, 2016
    Posts:
    5
    Thanks for the reply!

    How do the triangles work in this? I think I've got enough of an understanding to get/set the vertices to a new position (just adjusting their individual vector3's), but I don't understand how that would work with the triangles - would it just be copying and appending them to the triangles array? How do I 'translate' the triangles?

    As I play with this I'm wondering if it even makes sense to do it this way. In your opinion, would it make more sense to simply generate them as GameObjects so that they are easy to position (as you say, there will be a LOT of tweaking to position them nicely) since there would only be maybe ~100 or so low-poly cliffs active at any given time (i.e. I would make sure they aren't rendered if not within a certain range of the player). Performance wise, how much does it matter if it is one giant complicated mesh vs many separate GameObjects?

    Thanks again, much appreciated!
     
  4. - before you append the new vertices you record how many vertices your cube has
    - then append the vertices
    - then translate the triangles from the asset by incrementing the vertex-indexes by the amount you saved above
    - finally you find any leaks and stuff, you generate extra triangles to patch the holes

    Be careful, a mesh in Unity only could have 65535 vertices unless you set the index-format: https://docs.unity3d.com/ScriptReference/Mesh-indexFormat.html

    If you're lucky, this will work. If not, then you will need to examine the asset you made and write a translator where you query the vertieces and the triangles. Or try a combine mesh solution from the asset store. :D