5
\$\begingroup\$

I generated a terrain using a technique from this blog post, everything worked well and I got the wanted result. This is what my generated island looks like:

enter image description here

As you can see the terrain have a polygon structure. Here is the graph that generated this mesh:

enter image description here

This is the code used to generate this mesh from the graph:

    void DrawPolygons(){
    List<Vector3> vertices = new List<Vector3>();
    List<int> triangles=new List<int>();
    List<Color32> colors = new List<Color32>();

    foreach(Center c in centers){
        float zp = zScale*c.elevation;
        vertices.Add(new Vector3(((Vector2)c.point).x,zp,((Vector2)c.point).y));
        Color c0 = getColor(c);
        colors.Add(c0);
        int centerIndex=vertices.Count-1;
        var edges = c.borders;
        int lastIndex = 0;
        int firstIndex = 0;

        for(int i =0;i<c.borders.Count;i++){
            if(edges[i].v0 == null && edges[i].v1 == null)
                break;

            //get voronoi edge
            Corner corner0 = edges[i].v0;
            Corner corner1 = edges[i].v1;

            //get vertices height
            float z0 = zScale*corner0.elevation;
            float z1 = zScale*corner1.elevation;

            //add color
            if(edges[i].river>0){
                c0 = Color.cyan;
            }else{
                c0 = getColor(c);
            }

            colors.Add(c0);
            colors.Add(c0);

            //creat voronoi edge points
            Vector3 v0 = new Vector3(((Vector2)corner0.point).x,z0,((Vector2)corner0.point).y);
            Vector3 v1 = new Vector3(((Vector2)corner1.point).x,z1,((Vector2)corner1.point).y);

            //add points to vertices
            vertices.Add(v0);
            var i2 = vertices.Count - 1;
            vertices.Add(v1);
            var i3 = vertices.Count - 1;

            //add triangles calculating surface normals so i can always add triangles clockwise correctly
            var surfaceNormal = Vector3.Cross (v0-(new Vector3(((Vector2)c.point).x,zp,((Vector2)c.point).y)), v1-(new Vector3(((Vector2)c.point).x,zp,((Vector2)c.point).y)));
            if(surfaceNormal.y>0)
                AddTriangle(triangles, centerIndex, i2, i3);
            else
                AddTriangle(triangles, centerIndex, i3, i2);

            firstIndex = i2;
            lastIndex = i3;
        }
    }

    //calculating uv's
    Vector2[] uvs = new Vector2[vertices.Count];
    for (int i = 0; i < uvs.Length; i++)
    {
        uvs[i] = new Vector2(vertices[i].x / SIZE, vertices[i].z / SIZE);
    }

    mesh.Clear();
    mesh.vertices = vertices.ToArray();
    mesh.triangles = triangles.ToArray();
    mesh.uv = uvs;
    mesh.colors32 = colors.ToArray();
    mesh.RecalculateNormals();
    mesh.RecalculateBounds();
    meshFilter.sharedMesh = mesh;
    meshCollider.sharedMesh = mesh;

}

I would like to know if it's possible to have a noisy/realistic looking terrain from this. How would I implement such a thing?

Basically I want something with no hard edges, terrain that looks smooth but not too smooth, something like this video shows:

enter image description here

\$\endgroup\$
  • 3
    \$\begingroup\$ Define "noisy/realistic". \$\endgroup\$ – Ocelot Jun 25 '19 at 19:57
  • \$\begingroup\$ Basically no hard edges, a terrain that looks smooth but not too smooth, something like this \$\endgroup\$ – Saad Moutalib Jun 25 '19 at 20:01
  • 2
    \$\begingroup\$ Use gouraud shading, tessellate mesh and run terrain erosion algorithm. \$\endgroup\$ – Ocelot Jun 25 '19 at 20:04
  • \$\begingroup\$ Thank you ithink this is exactly what i needed i'm gonna research this now and see what i come up with. \$\endgroup\$ – Saad Moutalib Jun 25 '19 at 20:08
  • 1
    \$\begingroup\$ Check Sebastian Lague on youtube, he has multiple videos on procedural terrain generation. Will help you a lot \$\endgroup\$ – Nick Jun 26 '19 at 16:10
3
\$\begingroup\$

First of all, note that Unity already has a very nice terrain system out-of-the-box. It gives you a lot of nice features for free like LOD, occlusion culling and a lot more. Also, it can look pretty neat. You can still generate terrain procedurally, but the input data it expects isn't a mesh but a height map in form of a two-dimensional array of floats. So if you want to keep using that cell-based algorithm, you will have to convert it to output a heightmap instead of a list of vertices.

But no matter which technical implementation you choose to render your map: If you want to generate a landscape which combines discernible large features with small details, then it is usually a good idea to layer multiple terrain generation algorithms on top of each other. Use a pass with a high amplitude and a low resolution to generate the basic shapes (like you already do), and then run another pass with a lower amplitude but a higher resolution to create detail.

You could, for example, apply the same algorithm you are already using again for each cell to subdivide it further. Or you could use a completely different algorithm to "rough up" each cell like Perlin Noise.

Also, don't underestimate the effect of good textures. A high-resolution texture with a good normal map could make a terrain surface look a lot less plastic-like. Keep in mind that you need to also call mesh.RecalculateTangents() on your mesh for normal maps to render correctly.

I am looking forward to exploring the procedurally generated worlds in your game.

\$\endgroup\$
  • \$\begingroup\$ i like the idea of using this to produce a height map to then implement it in the existing unity terrain system but i think that will make me loose the benefits of the graph based terrain generation wich helps me define biomes and maybe even use it for pathfinding later on. For now i will try to add more algorithmes to this like tessalation and different erosion algorithmes and see how it turns out. \$\endgroup\$ – Saad Moutalib Jun 26 '19 at 16:23
1
\$\begingroup\$

A good way to generate realistic terrain is to simulate physical processes like

  • corrosion
    • either with a "uniform" corrosion algorithm or simulation of water or both
  • simulation of interaction of multiple sediment layers, don't forget that the origin of a lot of amazing landscapes are the sediments and the interactions of them with water underneath, like stone, dirt, sand, etc.
  • plate tectonics(?) (no idea which game simulates that but it's maybe possible)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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