- Home /
Assigning new Mesh Vertices
Hi Unity Community
I having issues with some code where I am trying to assign new values from Vector3 array to that of the vertices in the object's mesh.
For my needs I could just delete the existing object in scene and create a new one but that isn't really solving the issue of my logic so could anyone please provide me some pointers as to where I am going wrong?
Many thanks in advance, Ryan
function ExpVertText(userText : String)
{
var textArray = userText.Substring (1, userText.Length-2).Split ([")("], System.StringSplitOptions.RemoveEmptyEntries);
var vectArray = new Vector3[textArray.Length];
for (var i = 0; i < textArray.Length; i++) {
var numbers = textArray[i].Split(","[0]);
vectArray[i] = Vector3(parseFloat(numbers[0]), parseFloat(numbers[1]), parseFloat(numbers[2]));
}
Debug.Log("Mesh Stored");
var curObj = GameObject.Find("TestObject"); //current object
var curVertArray = curObj.GetComponent(MeshFilter).mesh.vertices;
for (var j = 0; j < vectArray.Length; j++) {
Debug.Log("Old vert: " + curVertArray[j]);
curVertArray[j] = vectArray[j];
Debug.Log("New vert: " + curVertArray[j]);
}
Debug.Log("Mesh Updated");
return curObj;
}
Answer by maccabbe · Apr 23, 2015 at 01:32 AM
Mesh.vertices returns a copy of the vertex positions or assigns a new vertex positions array. http://docs.unity3d.com/ScriptReference/Mesh-vertices.html
When you assign the vertices to curVertArray you are assigning them to a copy of Mesh.vertices. You need to assign curVertArray back to Mesh.vertices after you are done editing it, i.e. add the following line at line 20
curObj.GetComponent(MeshFilter).mesh.vertices=curVertArray;
you are a legend; this was my very problem, thank you very much!
Answer by Graham-Dunnett · Apr 22, 2015 at 04:15 PM
Try the examples provided here:
http://docs.unity3d.com/ScriptReference/Mesh.html
I believe the documentation is correct.
Hi @Graham Dunnett
Thank you very much for your prompt reply! Yes the mesh reference is great, I used it frequently to get to my current point.
The issue I'm encountering is that I need to iterate through the arrays for the length of the shorter Vector3[] array (derived from text based information) and replace some of the mesh.vertice array with its contents.
The docs suggest to clear the mesh before assigning new vert values however, I only wish to partially change the existing mesh, so calling Clear() does't seem like an option. I assumed that by iterating for the length of the short array and assigning the new vertices values would achieve this, however I can't seem to save these to the .mesh object.
It seems to work when declared as newVectArray = oldVectArray which would replace the whole array, however I can't get this to work when iterated through as newVectArray[i] = oldVectArray[i]. Can you please shed some light as to why this is the case?
Thank you very much in advance, Ryan
Your answer
Follow this Question
Related Questions
Most Optimal way of computing Delaunay Triangulation given an Array of Vector3 points 0 Answers
How to calculate normal direction for shared vertex position 1 Answer
Procedural Mesh Generation - Split Arrays into sections 1 Answer
Normalizing a cube made up of 24 meshes into a Sphere 0 Answers
Best Way to Link Vertices into Mesh 0 Answers