- Home /
 
Procedurally generated mesh acting weird.
I follow a tutorial on how to make a procedurally generated mesh. I managed to get through it but I wanted to texture it by face instead of by vertex so I changed the code to have 6 vertexes instead of one, so adjacent faces could be different shades. Now my mesh looks like this: (nvm, it says error parsing the file when I try to upload) It generates the mesh for me but there's other random bits as well. Like there's a flat plane in the middle and it's also visible from underneath, which it wasn't meant to do before. On top of that I can't get it to render colour per face even with six times the vertices. I'll try to put in the code.
Any advice would be appreciated.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 
 [RequireComponent(typeof(MeshFilter))]
 public class MeshGenerator : MonoBehaviour
 {
     Mesh mesh;
     private int MESH_SCALE = 100;
     public AnimationCurve heightCurve;
     public VertArray[] verticies;
     public List<int> triangles;
     public HeightColor[] cols;
 
     public int xSize;
     public int zSize;
     public float frequency = 1;
     public float amplitude = 12;
 
     private List<Color> colors;
     private float minHeight;
     private float maxHeight;
     private Vector3[] points;
 
     public float scale;
     public int octaves;
     public float lacunarity;
 
     public int highnum;
 
     [SerializeField]
     private Gradient gradient;
 
     public int seed;
     
 
     void Start()
     {
         Debug.Log("START");
         mesh = new Mesh();
         GetComponent<MeshFilter>().mesh = mesh;
         StartCoroutine(GenerateMap());
     }
 
     IEnumerator GenerateMap()
     {
         CreateMapShape();
         Debug.Log("CREATED MAP SHAPE");
         yield return new WaitForEndOfFrame();
         CreateTriangles();
         Debug.Log("CREATED TRIANGLES");
         yield return new WaitForEndOfFrame();
         ColorMap();
         Debug.Log("MAP COLORED");
         yield return new WaitForEndOfFrame();
         UpdateMesh();
         Debug.Log("DONE");
     }
 
     void CreateMapShape()
     {
         Vector2[] octaveOffsets = GetOffsetSeed();
 
         if (scale <= 0)
             scale = 0.0001f;
 
         foreach(VertArray v in verticies)
         {
             v.verticies = new Vector3[xSize * zSize];
             for (int o = 0; o < 1; o++)
             {
                 for (int i = 0, z = v.z; z < zSize + v.z; z++) 
                 {
                     for (int x = v.x; x < xSize + v.x; x++)
                     {
                         float noiseHeight = GenerateNoiseHeight(z, x, octaveOffsets);
                         SetMinMaxHeights(noiseHeight);
                         v.verticies[i] = new Vector3(x, noiseHeight, z);
                         i++;
                     }
                 }
             }
         }
     }
 
     Vector2[] GetOffsetSeed()
     {
         System.Random prng = new System.Random(seed);
         Vector2[] octaveOffsets = new Vector2[octaves];
 
         for (int o = 0; o < octaves; o++)
         {
             float offsetX = prng.Next(-100000, 100000);
             float offsetY = prng.Next(-100000, 100000);
             octaveOffsets[o] = new Vector2(offsetX, offsetY);
         }
         return octaveOffsets;
     }
 
     private float GenerateNoiseHeight(int z, int x, Vector2[] offsets)
     {
         amplitude = 12;
         float persistance = 0.5f;
         float freq = frequency;
         float noiseHeight = 0;
 
         for(int y = 0; y < octaves; y++)
         {
             float MapZ = z / scale * freq + offsets[y].y;
             float MapX = x / scale * freq + offsets[y].x;
 
             float perlinValue = (Mathf.PerlinNoise(MapZ, MapX)) * 2 - 1;
             noiseHeight += heightCurve.Evaluate(perlinValue) * amplitude;
             freq *= lacunarity;
             amplitude *= persistance;
         }
         return noiseHeight;
     }
 
     private void SetMinMaxHeights(float noiseHeight)
     {
         if(noiseHeight > maxHeight)
             maxHeight = noiseHeight;
         if (noiseHeight < minHeight)
             minHeight = noiseHeight;
     }
 
     void CreateTriangles()
     {
         triangles = new List<int>();
         int vert = 0;
         int tris = 0;
 
         for(int z = 0; z < zSize; z++)
         {
             for(int x = 0; x < xSize; x++)
             {
                 foreach(VertArray v in verticies)
                 {
                     Debug.Log($"{vert} + {v.x} + ({v.z} * {xSize}) = {vert + v.x + (v.z * xSize)}");
                     triangles.Add(vert + v.x + (v.z * xSize));
                 }
                 vert++;
                 tris++;
             }
 
         }
 /*
         for (int z = 0; z < xSize; z++)
         {
 
             for(int x = 0; x < xSize; x++)
             {
                 triangles[tris + 0] = vert + 0;
                 triangles[tris + 1] = vert + xSize + 1;
                 triangles[tris + 2] = vert + 1;
                 triangles[tris + 3] = vert + 1;
                 triangles[tris + 4] = vert + xSize + 1;
                 triangles[tris + 5] = vert + xSize + 2;
 
                 vert++;
                 tris += 6;
             }
             vert++;
         }
 */
 
     }
 
     void ColorMap()
     {
         colors = new List<Color>();
         float change = Random.Range(-0.2f, 0.2f);
         for (int v = 0; v < 6;)
         {
             
             for (int i = 0, z = 0; z < verticies[v].verticies.Length; z++)
             {
                 foreach (HeightColor h in cols)
                 {
                     //if (verticies[0].verticies[i].y > 2)
                         //Debug.Log($"Max is: {h.max}, Y level is: {verticies[0].verticies[i].y}");
                     if (verticies[v].verticies[i].y < h.max)
                     {
                         colors.Add(new Color(h.color.r + change, h.color.g + change, h.color.b + change));
                         break;
                     }
                 }
                 i++;
             }
             v++;
             Debug.Log("V = " + v);
 
         }
         /*
         List<Color> cool = colors;
         colors = new List<Color>();
         for (int i = 0; i < 6; i++)
         {
             foreach (Color c in cool)
             {
                 colors.Add(c);
             }
         }
         */
     }
 
     void UpdateMesh()
     {
         List <Vector3> verts = new List<Vector3>();
         foreach(VertArray vert in verticies)
         {
             foreach (Vector3 v in vert.verticies)
                 verts.Add(v);
         }
         points = verts.ToArray();
         
         mesh.Clear();
         mesh.vertices = verts.ToArray();
         mesh.triangles = triangles.ToArray();
         mesh.colors = colors.ToArray();
         mesh.name = "Terrain Mesh";
         mesh.RecalculateNormals();
         mesh.RecalculateTangents();
 
         GetComponent<MeshCollider>().sharedMesh = mesh;
         gameObject.transform.localScale = new Vector3(MESH_SCALE, MESH_SCALE, MESH_SCALE);
     }
 
     public void OnDrawGizmos()
     {
         /*
        // Mesh meshy = GetComponent<MeshCollider>().sharedMesh;
         if (points.Length>0)
         {
             //Debug.Log("Should be working");
             
             for(int i = 0; i < triangles.Count;)
             {
                 if(triangles[i] == highnum)
                 {
                     Gizmos.color = Color.red;
                     Vector3 v1 = points[triangles[i]] * MESH_SCALE;
                     Gizmos.DrawSphere(v1, 10);
                     Gizmos.color = Color.green;
                     Vector3 v2 = points[triangles[i+1]] * MESH_SCALE;
                     Gizmos.DrawSphere(v2, 10);
                     Vector3 v3 = points[triangles[i+2]] * MESH_SCALE;
                     Gizmos.DrawSphere(v3, 10);
                     Vector3 v4 = points[triangles[i+3]] * MESH_SCALE;
                     Gizmos.DrawSphere(v4, 10);
                     Vector3 v5 = points[triangles[i+4]] * MESH_SCALE;
                     Gizmos.DrawSphere(v5, 10);
                     Vector3 v6 = points[triangles[i+5]] * MESH_SCALE;
                     Gizmos.DrawSphere(v6, 10);
                     Gizmos.color = Color.blue;
                     
                     Gizmos.DrawLine(v1, v2);
                     Gizmos.DrawLine(v2, v3);
                     Gizmos.DrawLine(v3, v4);
                     Gizmos.DrawLine(v4, v5);
                     Gizmos.DrawLine(v5, v6);
                 }
                 i+=6;
             }
         }
     */
         
         
     }
 }
 
 
              Your answer
 
             Follow this Question
Related Questions
Procedurally generated mesh acting weird. 0 Answers
How do i make an object move around a 3D mesh? 1 Answer
Get all vertices in gameObject to array. 1 Answer
how to make a planet 1 Answer
How do i modify a mesh Filter(C#)? 2 Answers