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 PatrickStas · Nov 22, 2012 at 01:12 PM · meshoptimizationruntimeverticestriangles

Ways of modificating mesh triangles

Recently I have been asking about how to modify single vertex or triangle in mesh in topic http://answers.unity3d.com/questions/352162/optimized-vertex-manipulation.html . As it turned out, there are basically 2 reasonable ways how to do it showed in manual http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html , specifically #2 and #3. I don't clearly get when is using #3 better then #2. In manual is written "Continously changing the mesh triangles and vertices ". I did some experiments and from my sample observation, manipulating triangles using #2 approach works as well and seems to be faster.

 int[] triangles = _objectMeshFilter.triangles;
 triangles[GetTriangleLastIndex(nthTriangle)]--;
 _objectMeshFilter.triangles = triangles;

It seems to me that #3 is only good if I want to change number of vertices or triangles in mesh, other it just creating unnecessary work to garbage collector. Should I be aware of something using #2 approach ? In what situations should I rather use #3 ?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Fattie · Nov 22, 2012 at 01:51 PM

you're right, the doco is often badly written! but thank goodness it's there

what they are getting at:

use 2 if you want to CHANGE the values. so you have 100 verts. later you will still have 100 verts. but you just want to MOVE some of them.

you are GETTING the EXISTING values

var vertices : Vector3[] = mesh.vertices; // MAKES A COPY

changing them a bit, and then writing them back to the mesh

mesh.vertices = vertices;

In example three, they mean you are MAKING A WHOLE NEW MESH totally. (it could be an utterly different object.)

where they say "Do some calculations..." they should have written "create an entirely new object in mesh, however you happen to want to do that"

NOTE in example 3, putting it inside Update() is just silly. it is very unlikely (not impossible, but unlikely) a program would come out like that. you're not going to be building a whole new dragon r whatever in every frame. so that's just silly.

indeed, in example 2, it is somewhat silly -- it's only in pretty unusual situations, with small objects, you would deform the mesh like that EVERY FRAME.

don't forget too that COLLDIER MESH is totally different, it is way slow.

and never forget that whenever you type .vertices, it makes A WHOLE COPY ... it's just Special, it's not like other properties.

hope it helps!

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 PatrickStas · Nov 22, 2012 at 02:48 PM 1
Share

Thanks Fattie, you are the mesh master :-)

avatar image Fattie · Nov 22, 2012 at 02:52 PM 0
Share

LOL, I wish !

I know how to calculate pi, but I know almost nothing, at all, about Javascript of the Unity system! But I hope it helps! however I can use ALL CAPS to make points clearly! :)

there are some really critical points about mesh (in UNity) on this doiscussion...

http://answers.unity3d.com/questions/193695/in-unity-is-there-a-fast-way-to-find-nearby-triang.html

eg, "There appears to be a common misconception that it is "normal for" models to have shared verts where possible, or that verts "should be shared" or "are always" shared where possible. This is quite wrong, the sharedness of verts in 3D models is random, and is just an artefact of how the model happened to be made. You definitely cannot rely on verts being shared where possible, in 3D."

watch for misconceptions with mesh !! :O

avatar image PatrickStas · Nov 22, 2012 at 02:57 PM 0
Share

However you wrote that var vertices : Vector3[] = mesh.vertices; makes a copy. Isn't it possible to get by reference or something ? Why is it not possible to directly modify vertices of mesh in first place, as I was trying in previous topic ? I mean mesh.vertes[23] = SomeVector3; If I have a mesh and I often need to change few vertices and few triangles...and I use var vertices : Vector3[] = mesh.vertices; ... and that makes a copy of whole array ... sounds like not going to be very fast.

avatar image Fattie · Nov 22, 2012 at 03:07 PM 2
Share

mesh.vertices; makes a copy

Isn't it possible to get by reference or something?

IT IS ABSOLUTELY NOT POSSIBLE !!

fucking weird huh ?

it's a HUGE issue. it has to do with the graphics chips and stuff. it absolutely drives me nuts. often i just need the verts purely "to look at the data" - and it's a damnedable copy.

"Why is it not possible to directly modify vertices of mesh in first place"

very simple the verts "array" IS ONE HUGE "THING".. it's like one "word" on the computer that is 100s (or 10000s .. whatever) of bits long. you can only push the whole thing in at once.

we are dealing here with unbelievably complex hardware issues -- I don't understand it at all, I am only an abstract topologist.

"I often need to change few vertices and few triangles"

you simply can't. it's like saying "oh I wish I had voice recognition". we don't, at this stage. the achievements of GPUs in our era are epic - we have to live with it

don't forget it's FAIRLY FAST to write in a new verts -- the speed problem IS WITH COLLIDER $$anonymous$$ESH

run some tests. check out the "mega fiers" thingy on the app store - it wibbles mesh quite quickly (not HUGE objects)

you must "get with" how it works. don't forget in all situations YOU CAN $$anonymous$$EEP YOUR OWN PARALLEL COPY of the verts (just get it between scenes or whatever) which you can use to look at stuff.

(there is a very similar although much more trivial gotchya in Unity. whenevr you change the color, for hell's sake, on a renderer, it .. makes a whole new material! if you think about it it's obvious, it has to do that, each obejct w/ a different color needs its own. btu it's a gotchya in no-trivial projects)

i would urge yo to press on and "get with" the system as you find it - it's pretty good!

when you say innocuous stuff like changing the innocent looking verts property, you are melting the chipset you know, it is scifi processing going on. think of what it's doing! it's showing you lit living objects in real time, swinging them around .. it's nuts really. you need only pla an y common opengl game .. ACDC pinball say - to have you r$$anonymous$$d blown at the hardware.

avatar image PatrickStas · Nov 22, 2012 at 03:22 PM 0
Share

I see... Well at least I am glad to learned something new. I should probably study little bit more about graphic chips, it's interesting topic. So in the end seems like I have to make some workaround.

Thank you once again, you are really helpful :)

avatar image
2

Answer by Lev-Lukomskyi · Apr 22, 2014 at 11:16 PM

You can modify example #2 from docs to avoid garbage:

 using UnityEngine;
 
 public class Example : MonoBehaviour
 {
     Mesh mesh;
     Vector3[] vertices;
     Vector3[] normals;
     
     void Start()
     {
         mesh = GetComponent<MeshFilter>().mesh;
         vertices = mesh.vertices;
         normals = mesh.normals;
     }
     
     void Update()
     {
         for (var i = 0; i < vertices.Length; ++i)
             vertices[i] += normals[i] * Mathf.Sin(Time.time);
         mesh.vertices = vertices;
     }
 }
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
-1

Answer by aakwewaanaqa · Mar 22, 2017 at 10:57 AM

BUT... BUT...

mesh.vertices[mesh.triangles[INDEX]] still get a Vector3 of local coordination.

Comment
Add comment · Show 1 · 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 22, 2017 at 11:09 AM 1
Share

First of all, this is not an answer. If you want to ask a question, ask a question. If you really think your question is directly related to this question, post a comment. A Q&A site is not a forum.

About your question:

Whenever you "read" mesh.vertices Unity will create a copy of the internal data structure and returns that array. If you do things like

 mesh.vertices[index] = new Vector3(1,2,3);

It has no effect at all. Because you're getting a copy of the array, you change an element in that copy of the array but that array is just being garbage collected since it isn't stored anywhere, To actually change the vertices you have to set the vertices property by assinging an array to that property.

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

15 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

Related Questions

Best way to transition one mesh to another with the option to change the target mesh 0 Answers

Remove unnecessary vertices from plane mesh 1 Answer

Lighting up all triangles between two points in a mesh? 1 Answer

Best Way to Link Vertices into Mesh 0 Answers

Raycast - Add tris (script inside) 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