Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by TheJetstream · Oct 01, 2019 at 07:11 PM · meshmeshestriangle

Deleting single Triangle from Mesh

I currently try to delete a single triangle from a mesh in unity. The mesh is an Icosphere that I created using ProBuilder. Now I want to delete one triangle to have a "hole" into the sphere.


This is my code for doing so:

 private void DeleteTriangle(int id) {
     var currentMesh = _meshFilter.mesh;
 
     var triangles = currentMesh.triangles.ToList();
 
     triangles.RemoveAt(id + 0);
     triangles.RemoveAt(id + 1);
     triangles.RemoveAt(id + 2);
 
     currentMesh.Clear();
     currentMesh.vertices = vertices.ToArray();
     currentMesh.triangles = triangles.ToArray();
 
     currentMesh.RecalculateNormals();
 }


But when running this code somehow 2 triangles end up getting deleted and I cant figure out why exactly. I am running this code on id 0, so in the array only the 0th, 1st, and 2nd element should get removed but I still end up with this:


alt text

screenshot-8.png (116.5 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
2
Best Answer

Answer by OlivierHoel · Oct 02, 2019 at 04:02 AM

RemoveAt() will effectively change the size of your triangle list, so you have to be careful when calling it consecutively. Your code should be:

 triangles.RemoveAt(id);
 triangles.RemoveAt(id);
 triangles.RemoveAt(id);

or

 triangles.RemoveAt(id+2);
 triangles.RemoveAt(id+1);
 triangles.RemoveAt(id+0);

You should be able to figure out why by yourself ;)

Comment
Add comment · Show 2 · 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 TheJetstream · Oct 02, 2019 at 10:10 AM 0
Share

Oh my god I am so stupid. I overlooked that completely. Thank you!

avatar image Tristan-Moore · Nov 14, 2021 at 09:09 PM 0
Share

This answer is accurate and super helpful. One thing I noted is that I was trying to optimize a mesh by deleting triangles that had an edge length of zero, and just deleting the triangle levels the vertices in place, which is probably not what you want unless all the vertices are shared by the surrounding triangles. I found that running

 currentMesh.Optimize();

actually removes the vertices that are no longer connected to any triangle and appropriately reorders/updates the mesh data to reflect the new state. I'm not sure if this is 100% the best way to do it and there may be cases where this doesn't work, but it was pretty effective for my purposes.

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

194 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

Related Questions

Generating a plane with convex holes (at runtime via code) 0 Answers

Unexpected vertex amount when importing .blend file 0 Answers

How would I create an arbitrary mesh during runtime, copying the shape of the terrain underneath? 0 Answers

How to combine meshes for trees 0 Answers

How to remove not saved memory - meshes? 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