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. enter image description here

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: enter image description here

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?

share|improve this question
    
I didn't figure out why using 2D noise was making a jagged result, but I found a better alternate solution of using 3D noise instead, and following the density function tutorial described in GPU Gems 3 – epitaque Jan 16 at 21:47

You need to set a positions density to

Mathf.clamp(perlin - pos.y, 0, 1);

Instead of setting the values to either 1 or 0, this makes the top a bit more smooth. The clamp is required to make range of thr values inside the mesh lie between 1 and 0.

You should always pass a value between 1 and 0 to the marching cubes algorithm

share|improve this answer
    
This doesn't really work, the terrain is still blocky/jagged. For reference on how smooth it should be [here](blob:imgur.com/a4f17869-cda0-414c-acba-b4d5ffc252da) is a mesh generated by the same MC algorithm except using a 3D perlin noise function. – epitaque Jan 16 at 18:07
    
i.imgur.com/8jgGl0a.png – epitaque Jan 16 at 18:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.