- Home /
Adding vertices to procedural mesh generator
Hello everyone.
This is going to be a little rough, so stay with me :)
Here is what I have currently working:
I have a mesh (generated with script) that is built like this:
1------2
| / |
| / |
0------3
I've added 4 gameobjects around the mesh like this:
(light green are vertices, blue are gameobjects)
When I drag a gameobject, I'm rebuilding the mesh to fit the new locations of the gameobjects.
This way, I'm getting a stretched out mesh.
#######################################################
The problem is, I don't want the mesh to be stretched.
I want to add more vertices when I drag the gameobjects around.
Something like this:
(light green are vertices, drak green are double vertices, blue are gameobjects, orange is the dragged gameobject)
So the mesh should be built like this:
1-----2/5-----6
| / | / |
| / | / |
0-----3/4-----7
Imagine I'm dragging another gameobject:
(light green are vertices, drak green are double vertices, blue are gameobjects, orange is the dragged gameobject)
So now the mesh should be built like this:
9------10/13-------14
| / | / |
| / | / |
1/8----2/5/11/12----6/15
| / | / |
| / | / |
0-------3/4--------7
So here is where I'm struggling.
I have no clue how to go about managing the vertices (and triangles and uvs).
Any ideas, leads, scripts will be appreciated.
Thanks a lot!
This is more about how you organize and store your data. If you know the size of your quad grid, and every quad has 4 verts/uvs/etc, and they are stored sequentially in lists, then you can find and modify any specific quad.
say I have a mesh with a grid of 4x4 quads, and I want to change the 2nd quad on the second row, that's the 6th quad in the lists of mesh attributes, so (its 5 as lists count from zero) :
vert[ ( 5 * 4 ) ] = Vector3(...);
vert[ ( 5 * 4 ) + 1 ] = Vector3(...);
vert[ ( 5 * 4 ) + 2 ] = Vector3(...);
vert[ ( 5 * 4 ) + 3 ] = Vector3(...);
Check my answer here for the same principle : http://answers.unity3d.com/questions/475795/rendering-permanent-trails-such-as-footsteps.html
But I don't know the size of the grid, that's why I'm struggling with how to manage it.
The size of a quad is always 4verts, 4uvs and 6 tris.
But the Total size of the mesh (the quad grid) is changeable by the user (by dragging the gameobjects off the side of the mesh).
I was thinking about lists, but could not wrap my head around how to manage it correctly.
hmm, if you know the absolute maximum size of the grid, you could use a 2D array of integers, and store the type of tile (eg 0 = empty, 1 = type1, 2 = type2, etc). Then just manage your whole world like a grid. When a piece is dragged into a grid position, update the 2D array and the mesh information.
The only way I can see of doing it dynamically is to keep a list of vector tile positions and a list of tile types. (or you could use a Vector4, and use the w variable as the tile type). The order of this list would also be relative to the index position of the quad info in the mesh. Then every time you modify a tile, change the information at index position of the list, and modify the mesh data at index relative position.
I don't want to create a grid because I'm also letting the user rotate the mesh he made (only on 1 axis of curse).
Thanks for helping btw :)
Answer by Bunny83 · Aug 04, 2013 at 10:56 AM
If i got you right you want something like this
I didn't use gameobjects as handles but dragging areas around the plane. You can even grab a corner (outside of the plane) to drag two directions at once.
Keep in mind that Unity has a vertex limit of 65000 which equals roundabout 127 x 127 tiles.
The scene has only two gameobjects. One with the camera (and a modified MouseOrbit script) and one with this script.
ps: Use left mouse button to drag an edge, the middle mousebutton to rotate the camera and the mousewheel to zoom in / out
As additional information: I used the alphablended particle shader of Unity.
for writing a script :)
I'm currently at work and this is a personal project, so i'll test it when I get home and get back to you.
Thanks!
Well, I don't have time to Implant the script into my system (until the weekend).
But It works perfectly and the code doesn't seem to hard to understand, so implementing it shouldn't be too hard.
Thanks!
Hey, I finally got some time to look more closely at the script.
There are few things I don't understand.
Can you explain the CalculateUV function?
I noticed you are passing it a random value, I'm guessing that is what cussing the tiles to appear at random positions on the mesh.
How can I lose the randomness?
What I want is, tell the script what part of the atlas I want it to display, and then it will tile that part on all of the mesh tiles.
So, if compering to the example you made, I will tell the script I want it to display the "TNT" tile.
So now, the mesh will be tiled completely with TNTs.
Just saw your comment. I've updated my sample (script & webplayer).
The CalculateUV function actually just calulates an evenly distributed tile rect for a given index and x/y tile count. Dor example my sample texture has 16x16 tiles (x256). The function just takes the index and "splits" it up into x / y indices by calculate the modulo / division.
Since each tile is of equal size (1.0/16) the size of the rectangle is always the same, just the top-left corner is changing.
Answer by Paulius-Liekis · Aug 03, 2013 at 09:31 PM
Simply google "procedural mesh generation unity". There are plenty of tutorials. You simply need to create array of vertices and indices and assign them to your mesh.
Please see my comment to alucardj
I have no problem with creating meshs of any size, as long as I know the size, but in this case, I don't.
Your answer
Follow this Question
Related Questions
Flattening a mesh by deforming it 0 Answers
mesh-morph scaling badly 1 Answer
reading vertices from a mesh always returns Vector3.zero 2 Answers
Subdivide mesh? 2 Answers
Direction of a mesh 2 Answers