Update Random Genrator

Hello guys, my name is Alex and this is my first Terrain Generator.

My question is… how can i achive a terrain like that:


This is my actuell Chunk Script:

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class Chunk : MonoBehaviour {

    //Public Variables
    public float perlinHeight;
    public float xFrequency, yFrequency;
    public float borderline;
    public bool allowBorderCut = false;
    public bool allowNoiseAdd = false;

    [HideInInspector]
    public int resolution;

    [HideInInspector]
    public float segmentSize;

    //Private Variables
    private WorldData world = new WorldData();
    private Mesh mesh;

    private Vector3[] vertices;
    private int[] triangles;
    private List<int> indices;
    private Vector3[] normals;


    private void Awake ()
    {
        resolution = world.ChunkWidth;
        segmentSize = world.SegmentSize;
    }

    private void Start ()
    {
        triangles = new int[resolution * resolution * 6];

        if (mesh == null)
        {
            mesh = new Mesh();
            mesh.name = "Chunk";
            GetComponent<MeshFilter>().mesh = mesh;
        }

        CreateGrid ();

        transform.rotation = Quaternion.Euler(-90, 0, 0);
    }

    private void CreateGrid ()
    {
        //Vertices & Normals & UV's
        vertices = new Vector3[(resolution + 1) * (resolution + 1)];
        normals = new Vector3[vertices.Length];
        Vector2[] uv = new Vector2[vertices.Length];
        float stepSize = segmentSize;
        for (int v = 0, z = 0; z <= resolution; z++)
        {
            for (int x = 0; x <= resolution; x++, v++)
            {
                vertices[v] = new Vector3(x * stepSize - 0.5f, 0f, z * stepSize - 0.5f);
                normals[v] = Vector3.up;
                uv[v] = new Vector2(x * stepSize, z * stepSize);
            }
        }

        //Calculate Triangles over all
        for (int t = 0, v = 0, y = 0; y < resolution; y++, v++)
        {
            for (int x = 0; x < resolution; x++, v++, t += 6)
            {
                triangles[t] = v;
                triangles[t + 1] = v + resolution + 1;
                triangles[t + 2] = v + 1;
                triangles[t + 3] = v + 1;
                triangles[t + 4] = v + resolution + 1;
                triangles[t + 5] = v + resolution + 2;
            }
        }

        //Assign Values
        mesh.Clear();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.normals = normals;
        mesh.uv = uv;

        //Add some Noise to the Terrain
        if (allowNoiseAdd)
        {
            for (int v = 0, y = 0; y <= resolution; y++)
            {
                for (int x = 0; x <= resolution; x++, v++)
                {
                    float noiseY = world.InitNoiseY(x, y, perlinHeight, xFrequency, yFrequency, transform.position.x, transform.position.y);

                    if (noiseY > borderline)
                    {
                        vertices[v] = new Vector3(vertices[v].x, borderline + 1, vertices[v].z);
                    }
                    else
                    {
                        //world.setToFreeSpace(vertices[v]);
                    }
                }
            }
        }

        //Remove Triangles who are under the Borderline
        if (allowBorderCut)
        {
            indices = new List<int>(mesh.triangles);
            int count = indices.Count / 3;
            for (int i = count - 1; i >= 0; i--)
            {
                Vector3 V1 = vertices[indices[i * 3 + 0]];
                Vector3 V2 = vertices[indices[i * 3 + 1]];
                Vector3 V3 = vertices[indices[i * 3 + 2]];
                if (V1.y < borderline && V2.y < borderline && V3.y < borderline)
                {
                    indices.RemoveRange(i * 3, 3);
                }
            }

            mesh.triangles = indices.ToArray();
        }

        //Add Verts to Collider


        //Re-Assign Values
        mesh.vertices = vertices;
        mesh.RecalculateNormals();
    }

}

Wow this is actually really freaking cool

Terrain like that is based on ‘Marching Squares’, but not quite the same. So basically you get an array of heights, and you construct a mesh based on it.

If you look at the first wireframe gif you can see that each tile in the terrain is treated separately, which makes calculating this very easy. Basically you need to go through each tile and calculate the shape of it based on the 4 surrounding heights. So if all 4 heights are 1.0, then the shape of the tile is just a square. If 3 of the heights are 0.0, and one is 1.0, then the shape of the tile is a triangle covering just one corner. Once you do this for every situation, you just need to adjust the vertex positions based on the height ratio between each side, and you can build the mesh.