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 /
avatar image
0
Question by tuf91497 · Jun 19, 2019 at 07:27 PM · scripting problemmeshverticestriangles

Does mesh.triangles also return a copy of the triangle array?

So mesh.vertices returns a copy of the vertex array, so if you are accessing it more than a couple of times you should really store it in a local array to avoid excessive garbage being created. So if I have some code like this:

 for(int i = 0; i < mesh.triangles.length; i+=3){
     Vector3 v0Vec = mesh.vertices[mesh.triangles[i]];
     Vector3 v1Vec = mesh.vertices[mesh.triangles[i + 1]];
     Vector3 v2Vec = mesh.vertices[mesh.triangles[i + 2]];
     ...
 }


Then I should really convert it to something like this:

 Vector3[] verts = mesh.vertices;
 for(int i = 0; i < mesh.triangles.length; i+=3){
     Vector3 v0Vec = verts[mesh.triangles[i]];
     Vector3 v1Vec = verts[mesh.triangles[i + 1]];
     Vector3 v2Vec = verts[mesh.triangles[i + 2]];
     ...
 }


But does mesh.triangles do the same thing? Should my code really be more like this?:

 Vector3[] verts = mesh.vertices;
 int[] tris = mesh.triangles;
 for(int i = 0; i < tris.length; i+=3){
     Vector3 v0Vec = verts[tris[i]];
     Vector3 v1Vec = verts[tris[i + 1]];
     Vector3 v2Vec = verts[tris[i + 2]];
     ...
 }

Thanks for your help.

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
Best Answer

Answer by Bunny83 · Jun 19, 2019 at 07:55 PM

Yes, it does return a copy as well. Keep in mind that the Untiy engine core is written in native C++. All the built-in component data is stored on the native side. You can't get direct access to native code memory within a managed environment without using unsafe code. Since Unity uses multiple threads in it's core it can not ensure thread safety if they actually provide you direct access to internal memory structures.


If you need to do this more often you may want to use a cached List and use GetIncides instead. The same is true for GetVertices. If you just clear the lists and not recreating them all the time there will be no memory allocated for the subsequental calls.

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 tuf91497 · Jun 19, 2019 at 08:18 PM 0
Share

I'm confused by the usage of GetVertices. So I make List vertList = new List(mesh.vertexCount); mesh.GetVertices(vertList); Then I have all of the vertices in a list? Do I need to reassign the vertex list to mesh.vertices when I'm done with it?

avatar image Bunny83 tuf91497 · Jun 19, 2019 at 08:31 PM 1
Share

If you want to set the vertices back you would use SetVertices. Note that the usage would be more like this:

 private List<Vector3> vertices = new List<Vector3>();
 
 void Some$$anonymous$$ethod()
 {
     vertices.Clear();
     mesh.GetVertices(vertices);
     // [ ... ]
     mesh.SetVertices(vertices);
 }

Of course the capacity of the list would still occupy the memory, but that's the point when reusing the list is to avoid constant creation and collection of memory. GetVertices will automatically resize the List if necessary. Though resizing is only required the first time it's used or when you add more vertices at the [...]

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

197 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

Related Questions

How to find connected mesh triangles? 2 Answers

Why does the default Unity sphere have duplicate vertices? 1 Answer

Earth mesh rendering 1 Answer

Problem creating submeshes based on raycast. 0 Answers

Accessing Mesh: Triangle and Vertex array fields directly? 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