Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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: alt text After: alt text

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

367 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Procedural generating - generating trees at given mesh - vertices/height 0 Answers

Different textures on different meshes but same material 0 Answers

Mesh wont render although there are no errors! 1 Answer

Procedural Floating Island - Help Needed 1 Answer

Generate Polygon Plane from Array 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges