- Home /
Get an specific vertices of a mesh
Hey guys, in my game I'm creating a grid with procedural generated tiles (a cube without the bottom face). Since the grid will be 256x256 I'll need to combine all my tiles in a one single mesh after created, for perfomance reasons. However, one of the features on my grid is the ability to change tile's verts positions and after combining to that one single mesh I'm not sure on how to know exactly the vertices that I need to change when I want to, for example, change the height of the upper face of the tile 180,180. Is there any way to know the specific vertice(s)[x] of this new combine mesh?
Answer by Bunny83 · 5 days ago
Is there any way to know the specific vertice(s)[x] of this new combine mesh?
Sure, just remember where you put what. The structure of your vertex buffer is completely up to you. So you have to come up with a strategy how you want to lay out your vertices so you know where they are.
Note: What you're describing is actually a "folded" plane mesh. Though such a mesh, if stretched out would have square holes in it in a regular pattern like this:
left is the "unfolded" / flattened view while on the right is the folded view. Now you just have to generate the vertices / triangles in a logical way so it's easy to access / find the position of one of those quads. Since each quad needs its own set of 4 vertices (in order to have a flat shaded surface), I would probably first lay out all the "red" (top) quads, just the way they are seen in the folded version. Now if you look closely at just the horizontal connecting green quads, there are just as many rows as there are rows in the folded mesh. However in my case one column is missing (since they are only in between the top faces). You probably also want the outermost faces as well, so just imagine another column at the left side and another on the right side. So if we have a grid that is 7x5, we need 8x5 horizontal connecting quads. Likewise the vertical connecting quads we have 7x6 of them.
Overall if you want to change the top face of a cell, you would just take the row and column index and use the usual (col + row*xCount)
to get the "cell index". Since each cell top face requires 4 vertices multiply the cell index by 4 any you get the vertex offset (of course the 4 vertices would come one after the other in a fix pattern as well). To get the horizontal / vertical connecting quads, the same cell index can be used, but of course with an offset of xCount*yCount*4 which is the end of the top faces.
Now you just have to access the connecting quads adjacent to the cell you want to change and modify only the two adjacent vertices. Since the layout of the vertices of a quad should always be the same, it's just a matter of picking the right offsets.
Note in my case it's assumed that the "walls" between the cells are always the same. This layout has the advantage that unnecessary faces are already removed and the connecting faces automatically flip over if necessary. So if a cell is first at a lower position than the neighbors, the walls would face inwards forming the walls of the 4 neighboring cells. When moving the cell upwards, those walls would flip over and form the outside of the now higher cell.
if you actually need the possibility for the walls to have a different texture for each cell, or the wall texture has an "orientation" this technique is probably to complicated as adjusting the UVs accordingly would make things quite complicated. In this case it makes more sense to store "cube by cube" (so all 5 faces just right next to each other). I would still start with the top face and then the 4 wall quads in a fix order (something like top, left, bottom, right). So in this case we would have 20 vertices per cell. The calculation with the cell index would still work the same way, though we now multiply by 20 instead of 4. Such a layout requires about 66% more quads (if the grid is relatively large) than the first method.
Note in general when doing procedural meshing, you don't wnat to combine already existing meshes. Instead you should create the mesh completely procedurally. That way you have full control over where the vertices are.
Answer by jubahhh · 5 days ago
Of course!! It makes so much more sense to create the whole mesh, and it really is way better to just "fold" the side faces, only showing them when there is a higher tile. Thank you, for taking the time to show me!
Your answer
Follow this Question
Related Questions
Null Reference Exception Assigning Vertices to Mesh 0 Answers
How to find connected mesh triangles? 2 Answers
Lighting up all triangles between two points in a mesh? 1 Answer
How to return from shader the indexes of triangles/vertices that was actually rendered? 1 Answer
Making Different vertices,triangle,uv maps in the same mesh 1 Answer