The Marching Cubes (MC) algorithm works by taking 8 points of a cube with different density values and converting them into vertices and triangles based on a given iso level (threshold above which is considered solid area). It can create quite a smooth result. For example the following picture shows MC generating polygons using a 3D noise perlin function.
However 3D perlin noise is not very useful for generating terrain. 2D perlin noise is much better. One could use the following function to generate the density values at each corner of each cube sent through the marching cubes algorithm with an iso level of 0:
float IsoValue(Vector3 pos) {
float gap = maxHeight - minHeight;
float perlin = (Mathf.PerlinNoise(pos.x * frequency, pos.z * frequency) * gap) + minHeight;
if(pos.y > perlin) {
return -1f;
}
else {
return 1f;
}
}
It produces a jagged-looking result:
This is because unlike the 3d-noise function, the density values go straight from 0 to 1, and MC always generates vertices along a grid instead of at different heights in between the grid. How do I modify the above IsoValue function to not always give values between 0 and 1 (to make smoother looking terrain) depending on how close the generated noise is to the grid?