- Home /
Generating a mesh with unique vertices per quad
Hello there, this is my first post on here!
I am currently working on a game that requires me to use a tile map using a mesh. I am currently generating a mesh successfully with shared vertices per quad, but my use case requires me to generate a mesh with unique vertices per quad.
How do I alter my current algorithm to work with this new use case?
Current Code:
public int SizeX = 100;
public int SizeZ = 50;
public float TileSize = 1.0f;
// Use this for initialization
void Start ()
{
BuildMesh();
}
public void BuildMesh()
{
int numTiles = SizeX * SizeZ;
int numTris = numTiles * 2;
int vertsSizeX = SizeX + 1;
int vertsSizeZ = SizeZ + 1;
int numVerts = vertsSizeX * vertsSizeZ;
//Generate mesh data
Vector3[] vertices = new Vector3[numVerts];
Vector3[] normals = new Vector3[numVerts];
Vector2[] uv = new Vector2[numVerts];
int[] triangles = new int[numTris * 3];
int x;
int z;
for (z = 0; z < vertsSizeZ; z++)
{
for (x = 0; x < vertsSizeX; x++)
{
vertices[z * vertsSizeX + x] = new Vector3(x * TileSize, 0, z * TileSize);
normals[z * vertsSizeX + x] = Vector3.up;
uv[z * vertsSizeX + x] = new Vector2((float)x / SizeX, (float)z / SizeZ);
}
}
Debug.Log("Done verts!");
for (z = 0; z < SizeZ; z++)
{
for (x = 0; x < SizeX; x++)
{
int squareIndex = z * SizeX + x;
int triOffset = squareIndex * 6;
//this makes the 2 triangles that make up a square tile
triangles[triOffset + 0] = z * vertsSizeX + x + 0;
triangles[triOffset + 1] = z * vertsSizeX + x + vertsSizeX + 0;
triangles[triOffset + 2] = z * vertsSizeX + x + vertsSizeX + 1;
triangles[triOffset + 3] = z * vertsSizeX + x + 0;
triangles[triOffset + 4] = z * vertsSizeX + x + vertsSizeX + 1;
triangles[triOffset + 5] = z * vertsSizeX + x + 1;
}
}
//Create a new mesh
Mesh mesh = new Mesh();
//Populate mesh data
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uv;
//Apply mesh to filter, renderer and collider
MeshFilter meshFilter = GetComponent<MeshFilter>();
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
MeshCollider meshCollider = GetComponent<MeshCollider>();
meshFilter.mesh = mesh;
meshCollider.sharedMesh = mesh;
//BuildTexture();
}
Answer by Bunny83 · Sep 04, 2018 at 03:28 PM
Some time ago i made an example script how to create a mesh / plane with individual quads. I've created a webplayer example that everyone could try. Though since the webplayer is no longer supported I've just ported my example to WebGL. In the past i had hosted all my webplayer example on my dropbox. Though DP doesn't support this anymore so i have moved most of them to a free webspace. You can see the example here. The relevant script can be found here.
Note that this example contains several different concepts in one example. Since you are just interested in creating the mesh, just have a look at the UpdateMesh method. The rect that is passed as parameter just defines the size of the whole mesh.
In the example you can "grab" the border of the mesh by clicking outside the plane and drag the mouse to extend / shrink the plane. The actual tile that is assigned to each quad in this example is either created randomly or the same for every quad. You can use the right mouse button to let the directional light "orbit" the plane or use the middle mouse button to let the camera orbit the plane. Use the mouse wheel to zoom in / out. Keep in mind that the mesh is a 16bit index buffer mesh so there's a vertex limit of 64k. So don't create a too large plane (max is about 127x127 tiles).
All used scripts and the used shader are linked at the bottom of the WebGL page.
Hello! Unfortunately the link you referenced is dead. It would be very much appreciated if you could reupload the sample!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Algorithm for automatic mesh creation 1 Answer
Using compute shader, how to take every vertex of a mesh and make a cube spawn and move to each one? 0 Answers
Box collider 2d detecting collisions while not actually colliding 1 Answer