- Home /
procedural mesh grid alignment using chunks
Im creating a custom terrain out of chunks, these chunks are square , made out of a grid of vertices, then these vertices are displaced in the Y Axis(World UP). Finally the vertices are connected using triangles, to create the mesh.
Each Chunk needs to know all its own vertices so it can then take the vertices that make up its top and right sides, get the height value of those vertices ( position.y
) and copy them to the next chunks Left and /Or Bottom sides depending on the chunkID
(or its position in the grid of all chunks.
Everything works fine, but my vertices that Im setting from the last chunk so both chunks connect Seamlessly at the edges are not being applied.
I triple checked all my logic and everything is correct but when I generate a grid of chunks they don't line up at all!
My code:
vertices = new Vector3[totalVertices];
foreach (Vertice vert in Vertices)
chunk.Vertices.Add(vert);
if (chunkID > 1 && chunkID <= chunksWidthCount)// ONLY LEFT L L L L
{
for (int i = 0; i < chunkSizeX + 1; i++)
{
chunk.Vertices[leftIndexes[i]].position.y =
chunks[chunkID-1].Vertices[rightIndexes[i]].position.y;
chunk.Vertices[leftIndexes[i]].centerPosition.y =
chunks[chunkID-1].Vertices[rightIndexes[i]].centerPosition.y;
}
}
else if (chunkID != 1 && (chunkID-1) % chunksWidthCount == 0)
{
for (int i = 0; i < chunkSizeX + 1; i++)
{
chunk.Vertices[bottomIndexes[i]].position.y =
chunks[chunkID-chunksWidthCount].Vertices[bottomIndexes[i]].position.y;
chunk.Vertices[bottomIndexes[i]].centerPosition.y =
chunks[chunkID-chunksWidthCount].Vertices[bottomIndexes[i]].centerPosition.y;
}
}
else if ((chunkID-1) % chunksWidthCount < chunksWidthCount && chunkID != 1)
{
for (int i = 0; i < chunkSizeX + 1; i++)
{
chunk.Vertices[leftIndexes[i]].position.y =
chunks[chunkID-1].Vertices[rightIndexes[i]].position.y;
chunk.Vertices[leftIndexes[i]].centerPosition.y =
chunks[chunkID-1].Vertices[rightIndexes[i]].centerPosition.y;
chunk.Vertices[bottomIndexes[i]].position.y =
chunks[chunkID-chunksWidthCount].Vertices[bottomIndexes[i]].position.y;
chunk.Vertices[bottomIndexes[i]].centerPosition.y =
chunks[chunkID-chunksWidthCount].Vertices[bottomIndexes[i]].centerPosition.y;
}
}
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = new Vector3(
chunk.Vertices[i].position.x,
chunk.Vertices[i].position.y,
chunk.Vertices[i].position.z
);
}
chunk.mesh.vertices = vertices;
chunkID
starts at 1!! Not at 0. Otherwise the calculations will be more complicated to find the chunks position in the grid (wether it needs to change its left edge, bottom edge or maybe both).
Everytime a chunk is created it will check its own ID, then either take the previous chunks right edge and set its leftedge heights to match, or it will take the chunk below it, and match its own bottom side heights with the below chunks top side heights. In the third case it will copy the bottom heights form the chunk below and the leftside heights from the previous chunks right side.
Every chunk stores its own vertices for this reason, and I use one array for each sides's indexes, using these indexes to switch out heights.
Since all chunks are the same size, all the sides will have the same indexes, so the generator holds those index values and then just take the correct elements from the chunks Vertices Array using those same indexes that are calculated for each side.
void GenerateVertexGridObject()
{
Vertices = new Vertice[totalVertices];
for (int i = 0, z = 0; z <= chunkSizeZ; z++)
for (int x = 0; x <= chunkSizeX; x++, i++)
{
Vertice vert = new Vertice();
vert.index = i;
vert.position = new Vector3(
chunkStartPosition.x + x,
chunkStartPosition.y,
chunkStartPosition.z + z
);
vert.centerPosition = new Vector3(
vert.position.x + 0.5f,
vert.position.y,
vert.position.z + 0.5f
);
vert.centerPos2 = new Vector2(
vert.position.x + 0.5f,
vert.position.z + 0.5f
);
Vertices[i] = vert;
if (chunkID == 1)
{
if (i <= chunkSizeX)
{
bottomIndexes.Add(i);
if (i == 0)
leftIndexes.Add(i);
}
if (i % (chunkSizeX + 1) == 0 && i != 0)
{
leftIndexes.Add(i);
rightIndexes.Add(i - 1);
}
if (i >= (totalVertices - (chunkSizeX + 1)))
{
topIndexes.Add(i);
}
}
}
rightIndexes.Add(totalVertices - 1);
}
The above code creates the grid, this is before everything else.
Your answer
Follow this Question
Related Questions
How do I make a rotatable procedural circle in 3d space? 1 Answer
Holes in procedural mesh 0 Answers
Make a high poly count plane with code 0 Answers
How can I remove redundant vertices in custom mesh? 0 Answers
[Solved] Issues mapping procedurally generated cube without repeating vertices 0 Answers