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
-1
Question by GamesDeveloper12 · Jan 10, 2015 at 05:35 PM · mesharrayproceduralverticestriangles

Procedural Mesh Generation - Split Arrays into sections

I am using a modified version of a procedural generation asset which generates a mesh at runtime, but for performance i am trying to split the mesh into pieces.

Currently im dividing the arrays for the vertices, uv's and triangles into 6 as thats how many pieces i want and then looping six times and each time copying the section from the arrays that i need and using it to make a mesh which would then be applied to a gameobject. However when i run the code i get the following error:

"Failed setting triangles: Some indices are referencing out of bounds vertices. UnityEngine.Mesh:set_Triangles(Int32[])"

Any help resolving this issue would be greatly appreciated.

This is the code im currently using:

         for (int i = 0; i < 6; i++)
         {
             //UnityEngine.Debug.Log("i is: "+i+" "+(i*vertsChunk));
             Array.Copy(vectorArray, (i * vertsChunk), chunkverts, 0, vertsChunk);
             Array.Copy(vectorArray2, (i * uvchunk), chunkUv, 0, uvchunk);
             Array.Copy(vectorArray3, (i * uvchunk2), chunkUv2, 0, uvchunk2);
             Array.Copy(numArray, (i*triChunk), chunktris, 0, triChunk);

             Mesh m = new Mesh
             {
                 vertices = chunkverts,
                 uv = chunkUv,
                 tangents = new Vector4[vertsChunk],
                 uv2 = chunkUv2,
                 triangles = chunktris
             };

             m.RecalculateNormals();
          }


Comment
Add comment · Show 2
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 Cherno · Jan 11, 2015 at 03:36 AM 0
Share

Sounds like either one of the triangles doesn't have exactly three vertex indices, or one of the triangles' vertex indices is higher than the vertex count.

avatar image GamesDeveloper12 · Jan 11, 2015 at 01:12 PM 0
Share

i have checked both of those and they are both set correctly.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by incorrect · Jan 11, 2015 at 01:27 PM

That's probably because in triangles[] you just enumerate vertices by their indexes in vertices[] and you can not just divide those arrays because that mathematical coherence will be broken. Also note that in triangles[] polygons can be enumerated in any order, so if you take first half of vertices that does not mean that polygons that use those vertices will be in first half of triangles[]. You have to do it smarter: for each vertice in separate part you have to check through triangles[] and copy those polygons to that part's triangles[]. And you still have to solve the problem wich will occur with polygons that have vertices in different parts. Try reading about meshes and how they work here and there.

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 incorrect · Jan 11, 2015 at 01:36 PM 0
Share

Oh, btw, the error appears because renderer picks an int from triangles[] that is greater than vertices.Length. That's what I mean by saying 'mathematical coherency will be broken'. :)

avatar image GamesDeveloper12 · Jan 11, 2015 at 02:55 PM 0
Share

your suggestion of looping through each vertice in a section and copy those polygons. How would i do that, a simple for loop or would i have to try and work out which polygons match the vertice.

sudo code would be really helpful in understanding your idea, if possible ?

avatar image incorrect · Jan 11, 2015 at 04:39 PM 0
Share

Well, it's not very straightforward for a 'for loop'.

  1. You have to divide vertices[] on several parts.

  2. You have to run through all of those vertices and check their presence in triangles[], picking each separate triangle and checking if all other vertices are in this part of vertices[] division. If some vertices appear to be in another part of division (and there will be many of them), you have to add them in that part. (Btw, that will cause duplication of vertices, because some of them have to be in more than one mesh after division)

  3. Then you have to recalculate indexes and make new meshes with resulting arrays. Remember that indexes of vertices[], uv[], triangles[] and all other arrays that form mesh are the core of how mesh works.

The reason why meshes are made index-based is in the efficiency of this method, but it makes working with meshes quite complicated. Anyway, if you can avoid getting deep into meshes, try to make it, else it's not a question for Unity Answers and you have to start a discussion on Forums.

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

27 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

Related Questions

Highmap on a cube. 0 Answers

Best Way to Link Vertices into Mesh 0 Answers

Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer

Procedural cube code 0 Answers

Mesh extrusion overlapping triangles problem 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