Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Making a procedural low-poly terrain

Discussion in 'Scripting' started by AlexGorcea, Apr 3, 2016.

  1. AlexGorcea

    AlexGorcea

    Joined:
    Aug 20, 2013
    Posts:
    22
    Hey everyone.

    I've been banging my head on this for the last few days, and I can't seem to get anywhere.

    I'm trying to do a random low-poly terrain (that has a few properties). Kind of like this one:


    I've tried making it with the default Unity terrain and then setting very low values on the heightmap and detail resolution, but it still retains the smoothing. I've also tried to manipulate a plane manually by shifting vertices up and down, but I lost my way when I wanted to move quads as opposed to triangles.

    Any ideas would be really appreciated.
     
    Tsproggy likes this.
  2. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    looks pretty neat here is some 30 min code maby it helps you get started all you need to do is add this to a standart unity plane

    Code (CSharp):
    1.     void Start ()
    2.     {
    3.         int terrainSize = 10;
    4.  
    5.         Vector3[] vertices = this.transform.GetComponent<MeshFilter>().mesh.vertices;
    6.  
    7.  
    8.         //Generate some random terrain
    9.         for (int i = 0; i < vertices.Length; i ++)
    10.             vertices [i] = new Vector3 (vertices [i].x,    Mathf.PerlinNoise(vertices [i].x / 3f ,vertices [i].z  / 3f) * 3f, vertices [i].z);
    11.         //you could start work with tiles while terrain generation and edit 4 vertices
    12.  
    13.         int tileCount = 5;
    14.         float tileheigt;
    15.  
    16.         //for better results we ceep track of already placed tiles
    17.         List<int> usedVerts = new List<int> ();
    18.  
    19.         for (int i = 0; i < tileCount; i ++)
    20.         {
    21.             //get a start point
    22.             int vertIndex = Random.Range (0,vertices.Length);
    23.  
    24.             // array out of range ?
    25.             if(vertIndex - (tileCount + 2) < 0)
    26.                 continue;
    27.  
    28.             //set new high
    29.             tileheigt = vertices[vertIndex].y * 3;
    30.             //if not already in use by a other tile
    31.             for(int v = 0; v < usedVerts.Count; v ++)
    32.             {
    33.                 if(vertIndex == usedVerts[v])
    34.                 {
    35.                     tileheigt = vertices[usedVerts[v]].y;
    36.                     break;
    37.                 }
    38.             }
    39.  
    40.             //add used verts to the list and update the mesh
    41.             usedVerts.Add(vertIndex);
    42.             vertices[vertIndex] = new Vector3(vertices [vertIndex].x,tileheigt, vertices [vertIndex].z);
    43.             vertIndex -= 1;
    44.             usedVerts.Add(vertIndex);
    45.             vertices[vertIndex] = new Vector3(vertices [vertIndex].x,tileheigt, vertices [vertIndex].z);
    46.             vertIndex -= terrainSize;
    47.             usedVerts.Add(vertIndex);
    48.             vertices[vertIndex] = new Vector3(vertices [vertIndex].x,tileheigt, vertices [vertIndex].z);
    49.             vertIndex -= 1;
    50.             usedVerts.Add(vertIndex);
    51.             vertices[vertIndex] = new Vector3(vertices [vertIndex].x,tileheigt, vertices [vertIndex].z);
    52.         }
    53.         //bake
    54.         this.transform.GetComponent<MeshFilter> ().mesh.vertices = vertices;
    55.     }
    the result is pretty basic but its more about how you could to it not how its done

    test.jpg
     
    Tsproggy likes this.
  3. AlexGorcea

    AlexGorcea

    Joined:
    Aug 20, 2013
    Posts:
    22
    That looks awesome. I'll try it as soon as I get home.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,903
    Tsproggy likes this.
  5. YourFutureHero

    YourFutureHero

    Joined:
    Aug 6, 2014
    Posts:
    2
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Sorry to necro this thread, but I claim it's only mostly dead, which is a lot better than all dead...

    I've been thinking a lot about low-poly terrain generation today, and searching for resources on it. But it appears that everything I've found uses the same approach of having a regular grid in X and Z, and simply setting the elevation of each point in Y.

    I find this unsatisfying. Good low-poly assets generally use larger triangles where things are fairly smooth, and smaller triangles where the shape is complex. There ought to be a way to do that for a height map (or procedural height function), too — placing the vertices not on a regular grid, but wherever they're needed most.

    But I'm a bit stuck on how to do that... possibly something involving Delaunay triangulation? Are there any higher-level geometry geeks here who can help me slay this monster?
     
  7. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    [OFFTOP]I started to make my terrain because the more love in the form of the coordinates when X,Y is the plane of the earth and Z is the height of the peaks and dents. But the standard terrain cannot be rotated. In addition i found other some things that are not liked for me.[/OFFTOP]

    To Pavlon youre code looks pretty small and compact. Good work but im think AlexGorcea mean each thing another.

    To JoeStrout I think all polygons of the terrain are scanned and in each area is determined by the angle between neighbors edges(or vertices), and if this angle is obtuse, then maked optimization of the triangles in a determined area.

    To AlexGorcea you want terrain with sharp edges youre need to write special code where each triangle has its own vertices (the standard terrain triangles connected by common vertices) Except the void created by the abrupt(sharp) transitions will need to fill in patches. This is a difficult algorithm. i'm modeling in Blender how you make it.

    picture 1: editor terrain model (im show it to you, for compare)
    picture 2: terrain with independet quads (1st step)
    picture 3: terrain with independet quads with patched holes (2nd step)
     

    Attached Files:

    Last edited: Jan 13, 2018
    someguywithamouse likes this.
  8. someguywithamouse

    someguywithamouse

    Joined:
    Mar 25, 2018
    Posts:
    9



    I'm liking that one. I assume you can easily configure the height, etc? I need biomes and most of my terrain is slightly flatter than that. Also can we make it higher poly instead of cubes, I don't want my game to look like a Minecraft clone

    Like, the terrain has a realistic shape but its made up of tris/polygons. (maybe shaped somewhat like the level terrain seen in Clustertruck, except just a few more polygons and they're smaller too.)

    I wonder how long that would take
     
    Last edited: Jun 29, 2018
  9. Soulice

    Soulice

    Joined:
    Aug 18, 2014
    Posts:
    69
    @JoeStrout You ever have any luck? I have been looking at (too many to count) tutorials on low poly procedural land masses ( more islands ) with colliders and one series appears to be missing some in the middle (the code change jump was greater than the available vids and the other got too complex once it went beyond low-poly.
    Really looking for something that can build a large island (not continent size) that I can test play an fps on, so needs some terrain, flat places for a base or two and surrounded by water to act as the end edge.
    Dont want an never ending, or 20 chunks, just one big island, maybe 1500 unit units in diameter.
    Sorry to necro the thread....just found this and reaching out.
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    No, I never found a solution for this.
     
    Soulice likes this.