Question by 
               Creeper9207 · Aug 11, 2017 at 06:51 AM · 
                c#meshprocedural meshprocedural-generationprocedural-terrain  
              
 
              Procedural Mesh issues after trying to clean up code.
I had a simple procedural mesh generator working until i tried to make it more modular, now only one tri seems to render.
I'm not really sure what I've done wrong, I've been trying to work it out for a couple hours now.
Before: 
 After: 
Sect.cs using UnityEngine; using System.Collections;
 public class Sect
 {
 
     public MeshRenderer rend;
     public MeshFilter mf;
 
     public Vector2 position;
     public Texture2D x16;
     public Texture2D x64;
     public Texture2D x256;
     public Texture2D x512;
     public Texture2D x1024;
 
     public int noiseScale;
     public int scale;
 
     public GameObject obj;
 
     
 
 
 
     public void init()
     {
 
         obj = new GameObject("SECT " + position.x + "_" + position.y);
 
         obj.AddComponent(typeof(MeshFilter));
         obj.AddComponent(typeof(MeshRenderer));
         obj.AddComponent(typeof(MeshCollider));
 
         mf = obj.GetComponent(typeof(MeshFilter)) as MeshFilter;
         rend = obj.GetComponent<MeshRenderer>();
 
         
 
         MTable mtable = SectLib.calcVert(128,25,scale,position);
 
         Debug.Log(SectLib.calcTri(128).Length);
 
         
 
         Mesh m = new Mesh();
         m.name = "Scripted_Plane_New_Mesh";
         m.vertices = mtable.vert;
         m.uv = mtable.uv;
         m.triangles = SectLib.calcTri(128);
         rend.material.shader = Shader.Find("Standard");
         rend.material.mainTexture = SectLib.calcPix(1024, 25, scale, new Vector2(position.x, position.y));
         m.RecalculateNormals();
 
 
         
         mf.mesh = m;
 
         //obj.transform.Rotate(180, 0, 0);
         //obj.transform.localScale = new Vector3(50, 50, 50);
         //obj.transform.localPosition = new Vector3(-250, 0, -250);
 
     }
 
 }
 
 public static class SectLib
 {
     public static int[] tri;
 
     public static Vector2[] uv;
 
     public static Vector3[] vert;
 
     public static Color[] pix;
 
     public static int vcounter;
 
     public static int[] calcTri(int size)
     {
         vcounter = 0;
         float y = 0.0F;
         float x = 0.0F;
         tri = new int[(size - 1) * (size - 1) * 6];
 
         while (y < size - 1)
         {
             
             while (x < size - 1)
             {
                 tri[vcounter] = (int)(y * size + x);
                 vcounter++;
                 tri[vcounter] = (int)((y + 1) * size + x);
                 vcounter++;
                 tri[vcounter] = (int)(y * size + (1 + x));
                 vcounter++;
 
                 tri[vcounter] = (int)((y + 1) * size + (x + 1));
                 vcounter++;
                 tri[vcounter] = (int)(y * size + (1 + x));
                 vcounter++;
                 tri[vcounter] = (int)((y + 1) * size + x);
                 vcounter++;
 
                 x++;
             }
             y++;
         }
         return tri;
     }
     public static MTable calcVert(int size, int noiseScale, int scale, Vector2 origin)
     {
         vcounter = 0;
         float y = 0.0F;
         float x = 0.0F;
         vert = new Vector3[size * size];
         uv = new Vector2[size * size];
 
         while (y < size)
         {
             
             while (x < size)
             {
 
                 float xCoord = origin.x*scale + x / size * noiseScale;
                 float yCoord = origin.y*scale + y / size * noiseScale;
                 float sample = 1;//(Mathf.PerlinNoise(xCoord, yCoord) - (Mathf.Sqrt(Mathf.Pow(x - size/2, 2) + Mathf.Pow(y - size/2, 2)) / 35)) / 4;
 
                 /*math issue zone*/
                 vert[vcounter] = new Vector3(x/10, sample, y/10);
                 uv[vcounter] = new Vector2(x / size, y / size);
 
                 vcounter++;
 
                 x++;
             }
             y++;
         }
 
         return new MTable(vert,uv);
     }
     public static Texture2D calcPix(int size, int noiseScale, int scale, Vector2 origin)
     {
        
 
         Texture2D tex = new Texture2D(size, size);
         tex.filterMode = FilterMode.Bilinear;
         pix = new Color[tex.width * tex.height];
 
         float y = 0.0F;
         
         while (y < tex.height)
         {
             float x = 0.0F;
             while (x < tex.width)
             {
                 float xCoord = origin.x*scale + x / tex.width * noiseScale;
                 float yCoord = origin.y*scale + y / tex.height * noiseScale;
                 float sample = (Mathf.PerlinNoise(xCoord, yCoord) - (Mathf.Sqrt(Mathf.Pow(x - (tex.width / 2), 2) + Mathf.Pow(y - (tex.height / 2), 2)) / 35)) / 4;
                 pix[(int)(y * tex.width + x)] = WorldPainter.paint(sample);
 
 
                 x++;
             }
             y++;
         }
 
         tex.SetPixels(pix);
         tex.Apply();
 
         return tex;
 
 
         
         
         
     }
 }
 public class MTable
 {
     public Vector3[] vert;
     public Vector2[] uv;
     public MTable(Vector3[] x,Vector2[] y)
     {
         vert = x;
         uv = y;
     }
 }
 
               MeshGen.cs
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class MeshGen : MonoBehaviour
 {
 
     public int pixWidth = 100;
     public int pixHeight = 100;
     public float xOrg = 0f;
     public float yOrg = 0f;
     public float scale = .25F;
     private Texture2D noiseTex;
     private Color[] pix;
     private Renderer rend;
 
     public Vector3[] vertices;
     public Vector2[] uv;
     public int[] tri;
     void Start()
     {
         pixWidth = 1000;
         pixHeight = 1000;
         xOrg = 0f;
         yOrg = 0f;
         scale = 25F;
 
         Sect sect = new Sect();
         sect.position = new Vector2(1, 1);
         sect.scale = 20;
         sect.init();
 
         /*GameObject obj = new GameObject("New_Plane_Fom_Script");
 
         //float size = 10;
 
         //obj.AddComponent(typeof(MeshFilter));
         obj.AddComponent(typeof(MeshRenderer));
         obj.AddComponent(typeof(MeshCollider));
 
         vertices = new Vector3[100 * 100];
         uv = new Vector2[100 * 100];
         tri = new int[99 * 99 * 6];
 
         
         */
         rend = GetComponent<MeshRenderer>();
         rend.material.mainTexture = SectLib.calcPix(1024, 25, 100, new Vector2(1, 1));
         
         /* WARNING :: CODE BEYOND THIS POINT IS VERY NOT PRETTY */
         
         /*
         noiseTex = new Texture2D(pixWidth, pixHeight);
 
         noiseTex.filterMode = FilterMode.Bilinear;
 
         
         
         rend.material.mainTexture = noiseTex;
 
         Mesh m = new Mesh();
         m.name = "Scripted_Plane_New_Mesh";
         m.vertices = vertices;
         m.uv = uv;
         m.triangles = tri;
         rend.material.shader = Shader.Find("Standard");
         m.RecalculateNormals();
 
         
         MeshFilter mf = obj.GetComponent(typeof(MeshFilter)) as MeshFilter;
         mf.mesh = m;
 
         //obj.transform.Rotate(180, 0, 0);
         obj.transform.localScale = new Vector3(50, 50, 50);
         obj.transform.localPosition = new Vector3(-250, 0, -250);*/
 
     }
     void CalcNoise()
     {
         int vcounter = 0;
         float y = 0.0F;
         float x = 0.0F;
         while (y < noiseTex.height)
         {
             x = 0;
             while (x < noiseTex.width)
             {
                 float xCoord = xOrg + x / noiseTex.width * scale;
                 float yCoord = yOrg + y / noiseTex.height * scale;
                 float sample = ( Mathf.PerlinNoise(xCoord,yCoord) - (Mathf.Sqrt(Mathf.Pow(x-(noiseTex.width/2),2) + Mathf.Pow(y-(noiseTex.height/2),2))/35) )/4;
                 pix[(int)(y * noiseTex.width + x)] = WorldPainter.paint(sample);
 
                 
                 x++;
             }
             y++;
         }
         vcounter = 0;
         y = 0.0F;
         while (y < 100-1)
         {
             x = 0.0F;
             while (x < 100-1)
             {
                 tri[vcounter] = (int)(y * 100 + x);
                 vcounter++;
                 tri[vcounter] = (int)((y+1) * 100 + x);
                 vcounter++;
                 tri[vcounter] = (int)(y * 100 + (1+x));
                 vcounter++;
 
                 tri[vcounter] = (int)((y+1) * 100 + (x+1));
                 vcounter++;
                 tri[vcounter] = (int)(y * 100 + (1 + x));
                 vcounter++;
                 tri[vcounter] = (int)((y + 1) * 100 + x);
                 vcounter++;
                 
                 x++;
             }
             y++;
         }
         vcounter = 0;
         y = 0.0F;
         while (y < 100)
         {
             x = 0.0F;
             while (x < 100)
             {
 
                 float xCoord = xOrg + x / 100 * scale;
                 float yCoord = yOrg + y / 100 * scale;
                 float sample = (Mathf.PerlinNoise(xCoord, yCoord) - (Mathf.Sqrt(Mathf.Pow(x - 50f, 2) + Mathf.Pow(y - 50f, 2)) / 35)) / 4;
 
                 vertices[vcounter] = new Vector3(x / 10, sample, y / 10);
                 uv[vcounter] = new Vector2(x / 100, y / 100);
 
                 vcounter++;
 
                 x++;
             }
             y++;
         }
         noiseTex.SetPixels(pix);
         noiseTex.Apply();
     }
     // Update is called once per frame
     void Update () {
         
     }
 }
 
 
              
               Comment
              
 
               
              Your answer