Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by GoodKingJohn · May 30, 2015 at 05:06 PM · meshgenerationtriangles

Plane mesh generation help

I have made a method to produce a plane mesh that has options for tiling on both axis as well as size. I have written code to produce the vertices, but I am having trouble calculating the triangles. I cannot see what I have done wrong, I have my code here along with the result it makes:

     public Mesh GeneratePlaneMesh (int width, int height,
             int widthTile, int heightTile) {
 
         List<Vector3> vertices = new List<Vector3> ();
         List<Vector3> normals = new List<Vector3> ();
         List<int> triangles = new List<int> ();
         
         float singleWidth = width / widthTile;
         float singleHeight = height / heightTile;
         int triangleCount = 0;
 
         for (int i2 = 0; i2 < heightTile; i2 ++) {
 
             for (int i = 0; i < widthTile; i ++) {
 
                 vertices.Add(new Vector3(singleWidth * i, 0, i2 * singleHeight));
                 normals.Add(Vector3.up);
 
                 Debug.Log("Added new vertice at (" + singleWidth * i + ", 0, " + i2 * singleHeight + ") with index " + vertices.IndexOf(new Vector3(singleWidth * i, 0, i2 * singleHeight)));
 
             }
 
         }
 
         Debug.Log ("Added a total of " + vertices.Count + " vertices");
 
         // Single plane vertex indice expressions:
         /*
          * 
          * 0 = (i2 * widthTile) + i;
          * 1 = (i2 * widthTile + 1) + i;
          * 2 = ((i2 + 1) * widthTile + 1) + i;
          * 3 = ((i2 + 1) * widthTile) + i;
          * 
          */
 
         // Single plane triangle vertex arangement:
         /*
          * 
          * 2,3,0,
          * 0,1,2
          * 
          */
 
         bool stop = false;
 
         for (int i2 = 0; i2 < heightTile; i2 ++) {
 
             for (int i = 0; i < widthTile; i ++) {
 
                 int t0 = (i2 * widthTile) + i;
                 int t1 = (i2 * widthTile + 1) + i;
                 int t2 = ((i2 + 1) * widthTile + 1) + i;
                 int t3 = ((i2 + 1) * widthTile) + i;
 
                 //if (triangleCount % 4 == 0) {
 
                 triangles.Add (t2);
                 triangles.Add (t3);
                 triangles.Add (t0);
                 triangles.Add (t0);
                 triangles.Add (t1);
                 triangles.Add (t2);
 
                 Debug.Log ("Indexes: 0 = " + t0 + ", 1 = " + t1 + ", 2 = " + t2 + ", 3 = " + t3);
                 Debug.Log("Added 2 triangles with indexes (" + t2 + ", " + t3 + ", " + t0 + ") and (" + t0 + ", " + t1 + ", " + t2 + ")");
 
                 if (t2 == vertices.Count - 1) {
 
                     stop = true;
                     break;
 
                 }
 
                 //}
 
                 //triangleCount ++;
 
             }
 
             if (stop) {
 
                 break;
 
             }
 
         }
 
         Mesh m = new Mesh ();
         m.vertices = vertices.ToArray ();
         m.triangles = triangles.ToArray ();
         m.normals = normals.ToArray ();
 
         return m;
 
     }


Result (shaded and wirframe): alt text

alt text

Note: the planes are currently being generated as 6x5 with widthTiling as 6 and heightTiling as 5

probswireframe.png (50.6 kB)
probsshaded.png (48.2 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by GoodKingJohn · Jun 02, 2015 at 05:37 PM

Nevermind, I just had my triangles wrong. The correct order of indexes was: 2,1,0,0,3,2.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Generating Triangles Given a Set of Points 1 Answer

Voxel mesh generation not working. 1 Answer

automatically creating triangles 2 Answers

Setting triangles failing 0 Answers

Connecting flatshaded vertices 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