• 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 /
avatar image
Question by v0id1sm · Feb 03, 2022 at 08:59 PM · meshrenderingprocedural meshprocedural generation

Procedurally Generated mesh not rendering all triangles,Procedurally generated mesh isn't rendering all triangles?

I'm working on making a planet generator, and to start it off I'm trying to see if I can make a plane of procedurally generates squares. I have it print out each time a vertex is made, and every time a square is made out of 2 triangles, as well as the coordinates, and even though the console prints out everything properly, only the top row of squares appear.

If it helps, this is my spaghetti-looking code: (resolution is the amount of squares each side, so a res of 2 would make a 2 by 2 plane, 3 would make a 3 by 3 plane, etc)

 int resolution=10;
         int numVert = (resolution+1)*(resolution+1)+1;
         int numTri = resolution*resolution*6;
 
         Debug.Log(numVert);
         Debug.Log(numTri);
         Mesh mesh = new Mesh();
         Debug.Log(resolution);
         Vector3[] vertices = new Vector3[numVert];
         int[] triangles = new int[numTri];
 
         int i = 0;
         int i2 = 0;
         int x = 0;
         int y = 0;
         for (y = 0; y <= resolution; y++)
         {
             for (x = 0; x <= resolution; x++)
             {
                 vertices[i+1] = new Vector3(x*2,-y*2,0);
                 i=i+1;
                 Debug.Log("Vertex made at "+x+","+y);
             }
         }
         for (y = 1; y <= resolution; y++)
         {
             for (x = 1; x <= resolution; x++)
             {
                 triangles[i2] = x+resolution+1;
                 triangles[i2+1] = x;
                 triangles[i2+2] = x+1;
                 triangles[i2+3] = x+resolution+1;
                 triangles[i2+4] = x+1;
                 triangles[i2+5] = x+resolution+2;
                 i2=i2+6;
                 Debug.Log("Square made at "+x+","+y);
             }
         }
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         GetComponent<MeshFilter>().mesh = mesh;
         MeshCollider meshc = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
         meshc.sharedMesh = mesh;
     }
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by amabone · Feb 04, 2022 at 10:04 AM

Ok, Mr. you start from a 0 indexing vertex list, u grow up of maxX (resolution) for each y, that for the entire first row is 0.

          for (y = 1; y <= resolution; y++)
          {
              for (x = 1; x <= resolution; x++)
 {
 ....

you start here for 1 while doing calculation. resulting in index 0 never being mentioned, and thats what i would do (i did). you take count of the whole square while triangulating. so u need to check only for the square in a

resolution-1 * resolution-1 grid

those are the number of square in a grid of resolution*resolution vertices so u do

          for (y = 0; y <= resolution-1; y++)
          {
              for (x = 0; x <= resolution-1; x++)
              {


and then when selecting the corresponding vertex index u start from the point 0, 0 of the grid and count from there

 x---------x+1
 |          |
 x+X------x+X+1



entering the indexing like this

                      triangles[i2] = x;
                      triangles[i2+1] = x+1;
                      triangles[i2+2] = x+resolution;
                      triangles[i2+3] = x+resolution;
                      triangles[i2+4] = x+1;
                      triangles[i2+5] = x+resolution+1;
                      i2=i2+6;


im actually not sure by the orther, but u can figure it out i hope. Good Luck!

edit: i re-read the answer that i posted and pointed out that x, must be accessed linearly so, u can add another counter, like int index, and then when the last x occur u can call index++

 triangles[i2] = index;
 triangles[i2+1] = index+1;
 triangles[i2+2] = index+resolution;
 triangles[i2+3] = index+resolution;
 triangles[i2+4] = index+1;
 triangles[i2+5] = (index++)+resolution+1
 i2=i2+6;
 
 u can rewrite the whole thing as a single for loop int i = 0; i<((resolution-1)*(resolution-1));i++

the y would be i divided / by resolution

removing i2 at all stepping i for 6 like this

     for(int i = 0 ;i<((resolution-1)*(resolution-1));i++){
 int triangle_index = i*6;
     int y = i / resolution;
     //triangle[triangle_index+0] = i;//i+1,i+resolution
     //blabla...
     
     //i2 no more
     }
     
 


Comment

People who like this

0 Show 0 · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

172 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

Related Questions

Shadow artifects on mesh with Standard shader 1 Answer

Splitting vertices in a mesh specifically a plane 0 Answers

C# Proceducal Mesh terrain 2 Answers

How do I optimize loads of quads generated as a Tile map for my procedurally generated tileSet? 1 Answer

Manual culling of voxel faces? 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