I need some way to transform my mesh into another by adding or subtracting triangles and vertices and changing their position.
My goal is to transform my "Monster"(that is a single mesh) into a human(that is a single mesh too) smoothly inside Unity. Something that looks like the "Crumple Mesh Modifier" that you can find in this project here
sorry about my english, I`m not an English speeker and thanks in advance.
Dynamic creation of a mesh:
MeshFilter meshFilter = GetComponent();
Mesh mesh = meshFilter.sharedMesh;
if (mesh == null){
meshFilter.mesh = new Mesh();
mesh = meshFilter.sharedMesh;
}
Vector3 p0 = new Vector3(0,0,0);
Vector3 p1 = new Vector3(1,0,0);
Vector3 p2 = new Vector3(0,1,0);
Vector3 p3 = new Vector3(0,0,1);
// clear mesh of current data
mesh.Clear();
// set vertices
mesh.vertices = new Vector3[]{p0,p1,p2,p3};
// set
mesh.triangles = new int[]{
0,1,2,
0,2,3,
2,1,3,
0,3,1
};
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
Modifying existing vertices (would work similarly for faces):
// mesh.vertices returns a copy
Vector3[] vert_copy = mesh.vertices;
vert_copy[0] = new Vector3(10,11,12);
vert_copy[1] = new Vector3(13,14,15);
// reassign new vertices to mesh
mesh.vertices = vert_copy;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
These calls get expensive when you recalculate the normals and the bounds. If your mesh will not physically interact with other meshes while "transforming" you can defer RecalculateBounds() until after the "transformation" is complete. Similarly, if you anticipate only small per-frame transformations to your mesh you could limit RecalculateNormals() to every other frame or every 300ms.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With