-2

I am trying to implement a biomes system into a procedural terrain generation system. I made it so that the height multiplier of each terrain chunk changes based on which biome it is, but that results in the player being able to go under the map where two biomes meet. I am trying to make the edges of each chunk have a height of 0, so that they will connect properly. The issue is, the for loops don't seem to be starting at 0. I used to have the if statement that contains x <= 8 to be x == 0, but then nothing with the mesh would change.

for (int y = 0; y < borderedSize; y += meshSimplificationIncrement) {
    for (int x = 0; x < borderedSize; x += meshSimplificationIncrement) {
        float height;
        if (x <= 8 || x >= 233) {
            height = 5.0f;
            //Debug.Log(height);
            //Debug.Log("x" + x);
        } else if (y <= 8 || y >= 233) {
            height = 5.0f;
            //Debug.Log(height);
            //Debug.Log("y" + y);
        } else if (y <= 32 || y >= 225) {
            height = heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier;
            height = height / 1.5f;
        } else if (x <= 32 || x >= 225) {
            height = heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier;
            height = height / 1.5f;
        } else {
            height = heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier;
        }
        int vertexIndex = vertexIndicesMap[x, y];
        Vector2 percent = new Vector2((x - meshSimplificationIncrement) / (float)meshSize, (y - meshSimplificationIncrement) / (float)meshSize);
        Vector3 vertexPosition = new Vector3(topLeftX + percent.x * meshSizeUnsimplified, height, topLeftZ - percent.y * meshSizeUnsimplified);

        meshData.AddVertex(vertexPosition, percent, vertexIndex);

        if (x < borderedSize - 1 && y < borderedSize - 1) {
            int a = vertexIndicesMap[x, y];
            int b = vertexIndicesMap[x + meshSimplificationIncrement, y];
            int c = vertexIndicesMap[x, y + meshSimplificationIncrement];
            int d = vertexIndicesMap[x + meshSimplificationIncrement, y + meshSimplificationIncrement];
            meshData.AddTriangle(a, d, c);
            meshData.AddTriangle(d, a, b);
        }

        vertexIndex++;
    }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Nwott
  • 29
  • 3
  • 4
    `x` and `y` do start at 0. If you like you can print them out or use a debugger to see. – Retired Ninja Aug 16 '21 at 15:26
  • Asuming the value of a variable is a surefire way to fail at debugging. Always *verify* the value of a variable and that even triggers as the first and foremost thing. The ability to output values is the one thing you learn with the "Hello World" examples. – Christopher Aug 16 '21 at 15:30
  • You have a complex if/else chain where you sometimes check x and sometimes y. Step through the code using the debugger to verify that ( if x==0, you never will check the y) – Hans Kesting Aug 16 '21 at 15:46
  • Is it normal that `Vector2 percent` can have negative values for x=0 or y=0? – Dialecticus Aug 16 '21 at 15:59

1 Answers1

1

Belive me, the loops do start at 0.

I would create a ramp falling down to 0 at the edges.

const int rampSize = 32;

for (int y = 0; y < borderedSize; y += meshSimplificationIncrement)
{
    for (int x = 0; x < borderedSize; x += meshSimplificationIncrement)
    {
        float height = heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier;
        int min = Math.Min(x, y);
        int max = Math.Max(x, y);
        int borderDist = Math.Min(min, borderedSize - max);
        if (borderDist < rampSize) {
            height = height * (float)borderDist / rampSize;
        }
        ...
    }
}

This creates a ramp looking like a truncated pyramid.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188