When building a world in square mesh chunks, is there a way to blend varied height mulitpliers so chunks' edges line up?

33 Views Asked by At

I am working on a procedurally generated world. The world is built in mesh chunks. Each chunk has its own height multiplier that is used to set the heights of the vertices. However, as having different height multipliers between chunks would, of course, cause the edges of the chunks to not line up, I need to blend these multipliers together. Currently, when a vertex is setting its height, it looks at all surrounding chunks' height multipliers (including the chunk it belongs to) and finds the weighted average of them based off the distance between the vertex and the respective chunk center. That code looks like this:

public static float GetWeightedHeightMultiplier(List<TerrainChunk> surroundingChunks, Vector3 vertexPosition, Vector2 chunkCenterPosition)
{
    //surroundingChunks is a list of the terrain chunks around the chunk that vertexPosition belongs to
    //and includes that chunk as well, so 9 chunks except when dealing with chunks at the edge of the map

    Vector2 adjustPos = new Vector3(chunkCenterPosition.x + vertexPosition.x, chunkCenterPosition.y + vertexPosition.z);

    float finalHeight = 0;
    foreach (TerrainChunk chunk in surroundingChunks)
    {
        float distance = Vector2.Distance(chunk.chunkCenterPosition, adjustPos);
        //foreach chunk, find the distance from the current vertex to that chunk's center
        float weight = 1 - (distance / TerrainGeneration.meshWorldSize);
        //meshWorldSize is the side length of one of the chunks/ the max distance a vertex can be from the center of another chunk
        if (weight > 0)
        {

            finalHeight += weight * chunk.heightMultiplier;
            //find the weighted average of the chunks' height multipliers, based off distance to the chunks
        }
    }
    return finalHeight;
}

The line: "if (weight > 0)" ensures at max only the closest 4 height multipliers will be factored in. I'm using a technique like bilinear texture sampling.

But sadly this is not quite working. The edges do indeed line up, but there are these circular artifacts in the mesh now: 15x15 chunks

Up close chunk

I feel like I am doing circle math when I should be doing square math, but I am lost here. Any help would be greatly appreciated

0

There are 0 best solutions below