I have created procedural terrain generated in Unity using the CoherentNoise library. The generation itself works just fine, each terrain object that is added to the scene correctly generates heightmaps and applies them. The issue is that my generated noise isn't continuous from terrain object to terrain object, as seen from the image below.
The function that generates the noise for each terrain object is listed below
public void GenerateHeightMap() {
for (int z = 0; z < zRes; z++) {
for (int x = 0; x < xRes; x++) {
var xCoord = (float)((size.x * position.x) + x) / (xRes - 1);
var zCoord = (float)((size.z * position.z) + z) / (zRes - 1);
var value = settings.GetModule().GetValue(xCoord, zCoord, 0);
heights[x, z] = (float)(value / 2f) + 0.5f;
}
}
}
To quickly explain xCoord
and zCoord
:
First these x and z coordinates find their starting generation position (size * position). Then they add the loop offset (either x or z) and divide by the terrain's resolution. In theory, this should allow the noise generator to grab a neighboring terrain object's x,z coordinates and return a heightmap that lines up.
Any ideas as to why these terrain GameObjects do not line up?
If anyone would like to take a look at the project as a whole, I have uploaded the project to Github where it can be viewed and downloaded.