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 NaahL · Jan 07, 2021 at 10:34 PM · meshnot workingtriangles

Triangles on my mesh don't show

I am trying to fill in a plane of vertices with triangles but the triangles do not show on my mesh. Is there anything I am doing wrong? Here is the code that does that:

 private void Generate()
     {
         for (int z = 0; z < chunkWidth; z++)
         {
             for (int x = 0; x < chunkWidth; x++)
             {
                 float simplex1 = noise.GetSimplex(x * 0.8f, z * 0.8f) * 10;
                 float simplex2 = noise.GetSimplex(x * 3f, z * 3f) * 10 * (noise.GetSimplex(x * 0.3f, z * 0.3f) + 0.5f);
 
                 float y = simplex1 + simplex2;
 
                 // Instantiate(cube, new Vector3(x, y, z), Quaternion.identity);
                 verts.Add(new Vector3(x, 0, z));
                 
             }
         }
 
         int v = 0;
         int t = 0;
         for (int z = 0; z < chunkWidth; z++)
         {
             for (int x = 0; x < chunkWidth; x++)
             {
                 tris.Add(v + 0);
                 tris.Add(v + chunkWidth + 1);
                 tris.Add(v + 1);
                 tris.Add(v + 1);
                 tris.Add(v + chunkWidth + 1);
                 tris.Add(v + chunkWidth + 2);
 
                 v++;
                 t += 6;
             }
 
             v++;
         }
 
         UpdateMesh();
     }
 
     private void UpdateMesh()
     {
         mesh.vertices = verts.ToArray();
         mesh.triangles = tris.ToArray();
         mesh.RecalculateNormals();
 
         MeshFilter filter = GetComponent<MeshFilter>();
         filter.mesh = mesh;
     }

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
1

Answer by Bunny83 · Jan 08, 2021 at 02:00 AM

Your triangles are wrong your indices in the next row are off by 1. You should not add 1 and 2 but instead add 0 and 1. You also create too many quads. If you have a field of x x vertices you only have (x-1) (x-1) quads between them. It should look something like this:

      for (int z = 0; z < chunkWidth-1; z++)
      {
          for (int x = 0; x < chunkWidth-1; x++)
          {
              tris.Add(v);
              tris.Add(v + chunkWidth);
              tris.Add(v + 1);
              
              tris.Add(v + 1);
              tris.Add(v + chunkWidth);
              tris.Add(v + chunkWidth + 1);
 
              v++;
              t += 6;
          }
          v++;
      }


chunkWidth is the number of vertices in one row. So adding this count to an index will shift the row. Just imagine a small case, say a 3x3 vertex grid. It means the width and height are both 3. There are 3 vertices in each row. The indices of the first row are 0, 1 and 2. The second row has 3, 4 and 5. So if you are at index 0 you have to add 3 to get to the same index in the next row, not 4. It should also be clear that a 3x3 vertex field has only 4 quads between those vertices. The quads are in a 2x2 area between the vertices. If you can not imagine such things in your head, I highly recommend to grab pen and paper and draw the arrangement. It really helps visualising the problem. Keep in mind that triangles in Unity need clockwise winding in order to be visible.


If it's still not visible, are you sure you have a MeshRenderer on your gameobject and a proper material assigned?

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

127 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

Related Questions

Color triangle on mesh 3 Answers

How to know if triangles in a mesh are occluded or illuminated by directional light? 0 Answers

Procedural mesh creation issue 1 Answer

clarification about mesh triangles - vertices order 1 Answer

Seemingly random results winding triangles? 2 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