- Home /
Flat Low Poly Terrain / Script a terrain
i want to build design an low poly terrain in unity like http://i.ytimg.com/vi/-Yrb7mYhV9U/maxresdefault.jpg or this or other nice looking pictures ...
I dont find an solution,datwhy i set up my own Script (its not finised)
 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
 public class Map : MonoBehaviour {
 
     public Vector3[] vertices;
     public int[] triangles;
     public Vector2[] uv;
     public Vector3[] normals;
 
     int numberOfVertices = 0;
     int numberOfTriangles = 0;
 
     public float segmentWidth = 50f;
     public float segmentHeight = 50f;
 
     public int numberOfSegmentsAtXAxis = 10;
     public int numberOfSegmentsAtZAxis = 10;
 
     // Use this for initialization
     void Start () {
 
         MeshFilter meshfilter = GetComponent<MeshFilter>();
         Mesh mesh = new Mesh ();
         meshfilter.mesh = mesh;
 
         SetUpMesh ();
 
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         mesh.normals = normals;
 //        mesh.uv = uv;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void SetUpMesh(){
 
         numberOfVertices    = (1 + numberOfSegmentsAtXAxis)
                                         * (1 + numberOfSegmentsAtZAxis);
 
         numberOfTriangles    = numberOfSegmentsAtXAxis
                                         * numberOfSegmentsAtZAxis * 2;
 
         vertices = new Vector3[numberOfVertices];
         SetVertices ();
         
         triangles = new int[numberOfTriangles * 3];
         SetTriangles ();
         
         normals = new Vector3[numberOfVertices];
         SetNormals (Vector3.down);
 //
 //        uv = new Vector2[numberOfVertices];
 //        SetUv (uv);
     
     }
 
     void SetVertices (){
 
         for (int sz = 0; sz <= numberOfSegmentsAtZAxis; sz ++) {
                 
             for (int sx = 0; sx <= numberOfSegmentsAtXAxis; sx++){
 
                 int vertex = sx + sz * (numberOfSegmentsAtXAxis + 1);
 
                 vertices[vertex] = new Vector3(sx * segmentWidth,0,sz * segmentHeight);
 
             }
 
         }
 
     }
 
     void SetTriangles (){
 
         int trianglesIndex = 0;
 
         for (int sz = 0; sz < numberOfSegmentsAtZAxis; sz ++) {
             
             for (int sx = 0; sx < numberOfSegmentsAtXAxis; sx++){
 
                 int vertex = sx + sz * (numberOfSegmentsAtXAxis + 1);
 
                 triangles[trianglesIndex+0] = vertex;
                 triangles[trianglesIndex+1] = vertex + numberOfSegmentsAtXAxis + 1;
                 triangles[trianglesIndex+2] = vertex + 1;
 
                 triangles[trianglesIndex+3] = vertex + numberOfSegmentsAtXAxis + 1;
                 triangles[trianglesIndex+4] = vertex + numberOfSegmentsAtXAxis + 2;
                 triangles[trianglesIndex+5] = vertex + 1;
 
                 trianglesIndex += 6;
                 
             }
             
         }
 
     }
 
     void SetNormals (Vector3 direction){
 
         for (int n = 0; n < numberOfVertices; n++) {
         
             normals[n] = direction;
 
         }
 
     }
 
     void SetUv (ref Vector2[] uv){
 
 
 
     }
 }
till now i build an plane ... i want to move the vertecies to positions where they can build an cool terrain.And how to apply other colors to the mesh(for example for mountens or rivers - now its only grren). Is there an algorithem or something simular. Is the code accurate ?
pls help me
thx a guy how want to become an developer
Answer by Josh1231 · Aug 25, 2014 at 04:49 PM
You can have a premade mesh and move the vertices around, you may also recalculate normals after that, that way there is a lower chance of the triangles joining with vertices you don't want it to since it's preset
 mesh.RecalculateNormals();
You don't have to do it though.
I cant use this method because it makes the mesh smoother :(
Answer by NeverEndingPrjct · Aug 25, 2014 at 06:23 PM
I saw an other problem ... if i use 100 segments at the x axis adn 100 at the z axis the performece goes down,everything is lagging and so on ... How can i solve this ?
That's because the object is beco$$anonymous$$g too high poly, try reducing it to something like 20.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                