- Home /
Isolation of different vertices on a mesh
Hi, I have an icosphere (imported from blender) and I made an array of its 42 vertices. My question is how can I program the mesh to where the vertices can be manipulated/programmed individually. Can this be done by using the array or is there another function used to do this. I searched the Manuel and API and didn't find much insight on this so are there any videos or other documentation that is related to this form of programming. Thank you for your time.
Answer by tanoshimi · Jan 21, 2017 at 09:12 AM
You edit the vertices as you want, and then assign the array back to Mesh.vertices.
Answer by slammin · Jan 22, 2017 at 08:13 AM
Every mesh has a MeshFilter. To access a GameObject's MeshFilter, use
MeshFilter mf = GetComponent< MeshFilter >();
Note that the code I'm providing might be incomplete depending on your circumstances.
Once you have the MeshFilter, you can access the MeshFilter's mesh's vertices and triangles:
mf.mesh.vertices
mf.mesh.triangles
It's probably best to copy these arrays into new arrays using the CopyTo command, the new arrays having respective lengths of mesh.vertices.length and mesh.triangles.length. Remember that the vertex array is a Vector3[] and the triangles array is an int[]. Let me re-iterate -- it's probably best that you create a copy of the original arrays (vertices and triangles) just so you don't lose the original data and then create one or more more set of arrays (vertices and triangles) that you can edit and assign to the mesh.
mf.mesh.vertices.CopyTo(array to copy to, 0); mf.mesh.triangles.CopyTo(array to copy to, 0);
The vertex array (mf.mesh.vertices) is all of the vertices that compose the mesh. The tricky part is the triangle array (mf.mesh.triangles) which defines the triangles by pointing to specific vertices in the vertex array. Every triangle is composed of three points (three specific vertices in the vertex array). Therefore, the first triangle in the triangle array is defined by the first three entries (0, 1, 2) the second triangle is defined by the second three entries (3, 4, 5) and so on. If the first triangle is defined by the 3rd, 9th, and 12th vertex in the vertex array, then,
triangles array = (3,9,12,...);
Note that the order in which vertices are defined (clockwise or counterclockwise) is important as it determines which way the face of the triangle will be visible.
Once you are done editing the vertex array and triangles array you have to re-assign these two arrays to the mesh:
mf.mesh.Clear();
mf.mesh.vertices = the new vertex array;
mf.mesh.triangles = the new triangle array;
mf.mesh.RecalculateBounds();
mf.mesh.RecalculateNormals();
That is just the basics of it for a non-rotating, non-moving mesh. If you actually are creating a UI that can push and pull vertices, that is on you. To select vertices when left clicking on them you might want to create invisible spheres that are on each vertex and then useRaycasts to determine which sphere you are hovering over. Use input methods to determine when you left click and then you should be able to isolate which vertex you clicked on. There's some code to it. Hope I answered your question.
Good luck!
Your answer
Follow this Question
Related Questions
how to assign mesh.vertices ToList linq extension 1 Answer
Multiple Meshes 0 Answers
How do I get rid of intersecting vertices between two gameobjects? 0 Answers
Altering specified vertices on a mesh 0 Answers
Assigning new Mesh Vertices 2 Answers