Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 biebelbrott · May 04 at 08:42 PM · arrayverticesprocedural meshedges

procedural mesh grid alignment using chunks


Im creating a custom terrain out of chunks, these chunks are square , made out of a grid of vertices, then these vertices are displaced in the Y Axis(World UP). Finally the vertices are connected using triangles, to create the mesh.


Each Chunk needs to know all its own vertices so it can then take the vertices that make up its top and right sides, get the height value of those vertices ( position.y) and copy them to the next chunks Left and /Or Bottom sides depending on the chunkID (or its position in the grid of all chunks.


Everything works fine, but my vertices that Im setting from the last chunk so both chunks connect Seamlessly at the edges are not being applied.


I triple checked all my logic and everything is correct but when I generate a grid of chunks they don't line up at all!


enter image description here


My code:

 vertices = new Vector3[totalVertices];
 
 foreach (Vertice vert in Vertices)
     chunk.Vertices.Add(vert);
 
 if (chunkID > 1 && chunkID <= chunksWidthCount)// ONLY LEFT    L   L   L   L
 {
     for (int i = 0; i < chunkSizeX + 1; i++)
     {
         chunk.Vertices[leftIndexes[i]].position.y =
             chunks[chunkID-1].Vertices[rightIndexes[i]].position.y;
         chunk.Vertices[leftIndexes[i]].centerPosition.y =
             chunks[chunkID-1].Vertices[rightIndexes[i]].centerPosition.y;
     }
 }
 else if (chunkID != 1 && (chunkID-1) % chunksWidthCount == 0)
 {
     for (int i = 0; i < chunkSizeX + 1; i++)
     {
         chunk.Vertices[bottomIndexes[i]].position.y =
             chunks[chunkID-chunksWidthCount].Vertices[bottomIndexes[i]].position.y;
         chunk.Vertices[bottomIndexes[i]].centerPosition.y =
             chunks[chunkID-chunksWidthCount].Vertices[bottomIndexes[i]].centerPosition.y;
     }
 }
 else if ((chunkID-1) % chunksWidthCount < chunksWidthCount && chunkID != 1)
 {
     for (int i = 0; i < chunkSizeX + 1; i++)
     {
         chunk.Vertices[leftIndexes[i]].position.y =
             chunks[chunkID-1].Vertices[rightIndexes[i]].position.y;
         chunk.Vertices[leftIndexes[i]].centerPosition.y =
             chunks[chunkID-1].Vertices[rightIndexes[i]].centerPosition.y;
         chunk.Vertices[bottomIndexes[i]].position.y =
             chunks[chunkID-chunksWidthCount].Vertices[bottomIndexes[i]].position.y;
         chunk.Vertices[bottomIndexes[i]].centerPosition.y =
             chunks[chunkID-chunksWidthCount].Vertices[bottomIndexes[i]].centerPosition.y;
     }
 }
 
 for (int i = 0; i < vertices.Length; i++)
 {
     vertices[i] = new Vector3(
         chunk.Vertices[i].position.x,
         chunk.Vertices[i].position.y,
         chunk.Vertices[i].position.z
     );
 }
 
 chunk.mesh.vertices = vertices;


chunkID starts at 1!! Not at 0. Otherwise the calculations will be more complicated to find the chunks position in the grid (wether it needs to change its left edge, bottom edge or maybe both).


Everytime a chunk is created it will check its own ID, then either take the previous chunks right edge and set its leftedge heights to match, or it will take the chunk below it, and match its own bottom side heights with the below chunks top side heights. In the third case it will copy the bottom heights form the chunk below and the leftside heights from the previous chunks right side.


Every chunk stores its own vertices for this reason, and I use one array for each sides's indexes, using these indexes to switch out heights.


Since all chunks are the same size, all the sides will have the same indexes, so the generator holds those index values and then just take the correct elements from the chunks Vertices Array using those same indexes that are calculated for each side.


 void GenerateVertexGridObject()
 {
     Vertices = new Vertice[totalVertices];
     for (int i = 0, z = 0; z <= chunkSizeZ; z++)
     for (int x = 0; x <= chunkSizeX; x++, i++)
     {
         Vertice vert = new Vertice();
         vert.index = i;
 
         vert.position = new Vector3(
             chunkStartPosition.x + x,
             chunkStartPosition.y,
             chunkStartPosition.z + z
         );
         vert.centerPosition = new Vector3(
             vert.position.x + 0.5f,
             vert.position.y,
             vert.position.z + 0.5f
         );
         vert.centerPos2 = new Vector2(
             vert.position.x + 0.5f,
             vert.position.z + 0.5f
         );
 
         Vertices[i] = vert;
         if (chunkID == 1)
         {
             if (i <= chunkSizeX)
             {
                 bottomIndexes.Add(i);
                 if (i == 0)
                     leftIndexes.Add(i);
             }
             if (i % (chunkSizeX + 1) == 0 && i != 0)
             {
                 leftIndexes.Add(i);
                 rightIndexes.Add(i - 1);
             }
             if (i >= (totalVertices - (chunkSizeX + 1)))
             {
                 topIndexes.Add(i);
             }
         }
     }
     rightIndexes.Add(totalVertices - 1);
 }


The above code creates the grid, this is before everything else.


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

193 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

Related Questions

Mesh Collider not work with procedural mesh 0 Answers

Performance increase by unused Vertices? 1 Answer

Best Way to Link Vertices into Mesh 0 Answers

Multiple Meshes 0 Answers

How could I detect the outer edges of a mesh? 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