• 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 mlechene · Mar 15, 2013 at 12:49 PM · procedural meshgarbage collector

Is there a way to assign mesh.vertices[i] directly

Hi I actually simulate a deformable surface (using mass-spring model as this used to simulate clothes for instance) and draw the mesh every frame.

To draw it i follow what's explain on this link (http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html) at section '3. Continously changing the mesh triangles and vertices'.

However, the framerate decrease a lot, and when profiling I aware it was the garbage collector (gc.collect() call) in my drawing method which takes the most of the time by frame.

Is there a way to directly modify mesh.vertices[i] = new Vector3(....) for instance instead to pass an array which is going to be copy in the mesh and so garbage collected after ?

Thanks a lot

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

Answer by Fattie · Mar 15, 2013 at 12:57 PM

  • are you asking, can you modify JUST ONE of the verts? no, you can only write the whole .vertices at the same time

I don't understand the graphics pipeline, but I believe the reason is possibly because it writes the whole thing to the graphics chips at once

  • yes you should certainly keep your own array ("yourArray")

and modify it as you wish.

then simply write that same array to the vertices (.vertices = yourArray)

  • never GET the vertices like this ... some other array of yours == mmm.vertices

never do that. it makes a copy each time.

(OK you could do it once when you launch, but generally don't it in play.)

http://answers.unity3d.com/questions/352162/optimized-vertex-manipulation.html
http://answers.unity3d.com/questions/352513/ways-of-modificating-mesh-triangles.html
http://answers.unity3d.com/questions/334473/can-i-move-just-some-of-the-verts-on-a-mesh.html

it's a bit of a gotchya in Unity, it only mentions it subtly in the doco. But you have to be super-aware of the few functions that do make a copy each time. It only took me like 5 years to figure that out :)

Comment
Add comment · Show 5 · 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 Bunny83 · Mar 15, 2013 at 01:42 PM 0
Share

Yes, it's a pity they don't offer a custom "Lock" function to write only a part of the vertex / index array.

Anyway, good explanation. +1

avatar image Fattie · Mar 15, 2013 at 01:45 PM 0
Share

dude I just copied all your previous text on the subject ;-)

yes, it would be totally incredible - a huge change in the world - if you could one way or another write faster to the mesh. everything I work on in R&D relates to this

what would be even more incredible is if I understood WTF actually happens in the pipeline after you go .vertices = ;-)

avatar image Owen-Reynolds · Mar 15, 2013 at 03:36 PM 1
Share

"it writes the whole thing to the graphics chips at once"

Yes. In the old days, only texture data was stored in the GPU. Each drawcall still sent all the vertex data, each frame.

Now, VBO's (vertex buffer objects) also store all the vertex data in the graphics card, so the drawcall just says "model 17." The final format is (probably) not the same as in the CPU, so it hates to find&change just one.

In theory, you'd turn off using VBOs if you intend to change a model each frame (but I've never tried and don't know if it's possible, how it would affect batching... .)

avatar image mlechene · Mar 15, 2013 at 03:52 PM 0
Share

Thanks for quick replies. First yes, it seems mesh.vertices = myArrays copied all array in GPU, it's why it is not an efficient way when we attempt to modify the mesh each frame. I'm gonna look at the links send above to search other possible solutions to achieve it without decrease my framerate. As I already said, the issue is not to keep my own vertices coordinates, but the gc.collect called after assigning vertices and triangles !!

avatar image Fattie · Mar 15, 2013 at 04:02 PM 0
Share

Hi @mlechene, yes if I was not clear sorry. You must never do this: yourArray = mesh.vertices. Never do that.

@Owen - sounds amazing. NOw I will use the term "VBO" everywhere to try to sound intelligent. Thanks for that! Awesome. It's incredible you used to have to send it every drawcall. NOw I want to know how to turn off the VBOs.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Procedural animated mesh 1 Answer

Generating road meshes from a spline 2 Answers

Procedural Mesh random faces generated 0 Answers

Best way to split up procedurally generated mesh 0 Answers

how to get maximum performance out of Multithreaded procedural mesh creation? 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