Terrain in my voxel game is generated by combining 2d and 3d perlin noise.
if (y < (noise2d(x, z) + noise3d(x, y, z)) * amplitude)
{
// place block
}
To generate structures like trees, I need to find where the ground is. The simplest way is to just go through every block from min height to max height and check if there is a block with no block above it. This works, but it is slow. Is there a faster way to do this?
I tried using bisection, but it sometimes returns positions one block below the ground.
Simplified code for bisection:
float min = minHeight;
float max = maxHeight;
float mid = (min + max) / 2.0f;
while (abs(mid - combinedNoise(mid)) > 0.5f)
{
if (sign(mid - combinedNoise(min)) == sign(mid - combinedNoise(mid)))
{
min = mid;
}
else
{
max = mid;
}
mid = (min + max) / 2.0f;
}
I think removing the absolute value might fix the blocks below the ground, but I don't think I can remove it because it can create an infinite loop because of how bisection works.