- Home /
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 ?
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!
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
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.
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.
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 :)
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;
}
}
Answer by aakwewaanaqa · Mar 22, 2017 at 10:57 AM
BUT... BUT...
mesh.vertices[mesh.triangles[INDEX]] still get a Vector3 of local coordination.
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
Follow this Question
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