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 Sladuuch · Aug 05, 2010 at 04:04 AM · meshproceduralvertexdeletetriangle

Trouble recalculating 3D mesh's triangles after deleting verts

I'm probably doing this really inefficiently, but I'm having trouble recalculating the triangles array of a mesh I'm deleting vertices from. I can delete the vertices without issue (I think), but I'm getting hung up on the triangles array.

What I have so far works for deleting vertices, but it overflows the length of the new triangles array I'm initializing.

// First pass; null out any verts that are below the ocean in the vertex array vertices = mesh.vertices; var dist : float = 0.0; var numberOfVerticesLeft : int = vertices.length; print ("original vertices array length is " + vertices.length);

 for (var vertex in vertices) 
 { 
     dist = Vector3.Distance(Vector3.zero, vertex); 
     if (dist < planetRadius) 
     { 
         numberOfVerticesLeft--; 
         vertex = Vector3.zero; 
     } 
 } 
 print("New vertices array will be this long: " + numberOfVerticesLeft); 


 // Second pass; build up the new vertex array only from the good values 
 var newVerts = new Vector3[numberOfVerticesLeft]; 
 i = 0; 
 for (vertex in vertices) 
 { 
     if (vertex != Vector3.zero) 
     { 
         newVerts[i] = vertex; 
         i++; 
     } 
 } 
 print("Added " + i + " elements to new vertex array"); 


 // Third pass; loop through the triangle array to discover 

// which elements point to zeroed vertices, then set 'em to -1 var triangles = mesh.triangles; var numberOfTrianglesLeft : int = triangles.length; print ("original triangles array length is " + triangles.length);

 for (var tri in mesh.triangles) 
 { 
     if (vertices[tri] == Vector3.zero) 
     { 
         numberOfTrianglesLeft--; 
         tri = -1; 
     } 
 } 
 print("New triangles array will be this long: " + numberOfTrianglesLeft); 


 // Fourth pass; build up the new triangle array only from the good values 
 var newTriangles = new int[numberOfTrianglesLeft]; 
 i = 0; 
 for (tri in triangles) 
 { 
     if (tri != -1) 
     { 
         newTriangles[i] = tri; 
         i++; 
         print("Added " + i + " elements to new triangles array"); 
     } 
 } 
 // print("Added " + i + "elements to new triangles array"); 

 mesh.triangles = newTriangles; 
 mesh.vertices = newVerts;

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Wolfram · Aug 19, 2010 at 10:07 AM

Before you start messing with the vertices array of a mesh, you need to issue a mesh.Clear().

At the end of your code, you need to assign the vertices first, and the triangles last. mesh.RecalculateBounds() will be called automatically by reassigning mesh.triangles.

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
avatar image
0

Answer by StephanK · Aug 05, 2010 at 09:09 AM

I'm not sure why you get the overflow, but even if it wouldn't, your triangles would point to vertex indices that were relevant in the old vertices array and are probably irrelevant in the new one, as you deleted some vertices in between and vertices[6] now could be newVertices[3].

On which line are you getting the overflow? newTriangles[i] = tri; ?

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

No one has followed this question yet.

Related Questions

Duplicate and separate triangles from a mesh 1 Answer

Vertex count on a procedurally generated map and some related doubts 2 Answers

Procedural Cylinder Generation 2 Answers

Procedural mesh creation issue 1 Answer

Colors do not work with a mesh created in script? 3 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