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 johnsonnikolaus · Oct 23, 2016 at 09:07 AM · c#meshverticesprocedural generation

Read Vertices in a specific order?

I need to read the vertices of a mesh in order from left to right, then backwards to forwards, but the order the vertices are read in are incredibly awkward. They seem to read clockwise around the edges then spiraling in towards the center. Sorry if it's hard to understand what i'm asking for, i've included a picture of the results i'm currently getting so hopefully that can help. My current code is listed below.


         //There is a "public int intensity" at the header to adjust the intensity (obviously) of the generation
 
         Mesh mesh = GetComponent<MeshFilter>().mesh;
         Vector3[] vertices = mesh.vertices;
         Vector3[] normals = mesh.normals;
            
         int i = 0;
         int step = 0;
         float lastY = 0;
          while (i < vertices.Length)
          {
             vertices[i] += new Vector3(vertices[i].x, Random.Range(lastY + -1,lastY + 1) * intensity, vertices[i].z);
             lastY = vertices[i].y;
             i++;
          }
          
         mesh.vertices = vertices;
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
         transform.gameObject.AddComponent<MeshCollider>();

alt text

capture.png (330.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
1

Answer by JonHUnity · Oct 25, 2016 at 12:38 PM

The order of the vertices in a Mesh are determined by the order they were assigned when created. Unless explicitly documented by your importer or by the code which generated your mesh, there are no guarantees about the order of the vertices in the array.

Your picture looks like your mesh is a procedurally generated flat plane. To accomplish this with a specific order to the vertices, I recommend generating the mesh yourself. For example, to create a grid-based mesh, you can do something like the following:

 public class CreateMesh : MonoBehaviour {
     const int width = 100;
     const int height = 100;
     // Generate the mesh with a well-known vertex order
     void Start () {
         Mesh mesh = gameObject.AddComponent<MeshFilter>().mesh;
         gameObject.AddComponent<MeshRenderer>();
         mesh.vertices = GenerateVertices(width, height);
         mesh.triangles = GenerateTriangles(width, height);
     }
 
     private int[] GenerateTriangles(int width, int height)
     {
         //generate two triangles per vertex except the last column and last row
         int[] triangles = new int[(width - 1) * (height - 1) * 6];
         for (int y = 0; y < height - 1; y++ )
         {
             for (int x = 0; x < width - 1; x++ )
             {
                 triangles[(y * (width - 1) + x) * 6    ] = y * width + x;
                 triangles[(y * (width - 1) + x) * 6 + 1] = y * width + x + 1;
                 triangles[(y * (width - 1) + x) * 6 + 2] = y * width + x + 1 + width;
                 triangles[(y * (width - 1) + x) * 6 + 3] = y * width + x;
                 triangles[(y * (width - 1) + x) * 6 + 4] = y * width + x + 1 + width;
                 triangles[(y * (width - 1) + x) * 6 + 5] = y * width + x + width;
             }
         }
         return triangles;
     }
 
     Vector3[] GenerateVertices(int width, int height)
     {
         Vector3[] vertices = new Vector3[width * height];
         for (int y = 0; y < height; y++ )
         {
             for (int x = 0; x < width; x++ )
             {
                 vertices[y * width + x] = new Vector3(x / (float)width, y / (float) height);
             }
         }
         return vertices;
     }
     
     void Update () {
         Mesh mesh = this.gameObject.GetComponent<MeshFilter>().mesh;
         Vector3[] vertices = mesh.vertices;
         SetVertex(vertices, 10, 12, .55f);//set the z component of the vertex at x=10, y=12 to .55
         mesh.vertices = vertices;
     }
     private void SetVertex(Vector3[] verts, int x, int y, float z)
     {
         verts[width * y + x].z = z; 
     }
 }
Comment
Add comment · Show 3 · 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
avatar image johnsonnikolaus · Oct 26, 2016 at 12:12 AM 0
Share

Got it working near perfect, just a couple of questions. There seems to be a max vertices of 65000, is there anyway to get around that? (I am going to be optimizing the mesh after the terrain generation, I just need it for higher resolution heightmaps) Also, there seems to be a weird spike at a specific vertex and i'm not sure what happened in the code that is causing it (see picture below). Lastly, is there any way to make the plain bigger, currently it makes a very small 1x1 plane, but I was hoping for something more like 256x256 or higher.

Thanks!alt text

capture.png (407.4 kB)
avatar image JonHUnity ♦ johnsonnikolaus · Oct 26, 2016 at 06:29 AM 0
Share

There is a limit of 65,534 vertices. If you want more, you will have to create multiple meshes and line them up so there is no gap. This will involve moving this script to another gameobject that would ins$$anonymous$$d create more gameobjects, one for each mesh.

As for the size, you can either set the scale on the transform or multiply the x,y coordinates of the Vector3s in GenerateVertices by the size you want.

avatar image JonHUnity ♦ JonHUnity ♦ · Oct 26, 2016 at 07:04 AM 0
Share

The spike you see is just my example call to SetVertex(vertices, 10, 12, .55f); in Update(). You can remove it.

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

248 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

Related Questions

Mesh generation triangles issue 0 Answers

Edit SkinnedMesh with vertices 0 Answers

Removing Tris/Verts/Polygons from an merged mesh? (C#) 0 Answers

Shared or Non-shared Vertices for mesh generation? 0 Answers

Collisions = Get colliding 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