- Home /
make one mesh follow the deformations of the other,adjust mesh to follow deformations of another(cloth, hair)
Hi, I usually use daz studio models in my projects, the problem is that every time I download a new morph for the main model (Genesis 3), I have to reimport all others like hair and clothes. To facilitate this process I decided to write the script below, it searches for each vertex of the clothing, the vertex closest to the main mesh and based on the distance between the two turns this vertex into a parent (this process is very slow), causing the secondary mesh follows the deformations of the main mesh, but it is not behaving as it should, the mesh gets very strange. What can I do to improve the script?
I'm sorry for bad English.
public class MorphConform : MonoBehaviour {
public GameObject TargetMesh;
Vector3[] TargetVertices;
public GameObject SourceMesh;
Vector3[] SourceVertices;
int[] VertParent;
Vector3[] LocalPos;
void Update(){
if (Input.GetKeyDown("a"))
{
GetOriginal ();
}
if (Input.GetKeyDown("b"))
{
Conform();
}
}
void GetOriginal () {
TargetVertices = TargetMesh.GetComponent<MeshFilter> ().mesh.vertices;
Mesh provmesh = new Mesh ();
SourceMesh.GetComponent<SkinnedMeshRenderer> ().BakeMesh (provmesh);
SourceVertices = provmesh.vertices;
VertParent = new int[TargetVertices.Length];
LocalPos = new Vector3[TargetVertices.Length];
for (int t = 0; t < TargetVertices.Length; t++) {
bool FirtsTime = true;
float AtDist = 0.0000000f;
float MinDist = 0.0000000f;
for (int s = 0; s < SourceVertices.Length; s++) {
Vector3 wt = TargetMesh.transform.TransformPoint (TargetVertices[t]);
Vector3 ws = SourceMesh.transform.TransformPoint (SourceVertices[s]);
AtDist = Vector3.Distance (ws, wt);
if(AtDist < MinDist || FirtsTime){
FirtsTime = false;
MinDist = AtDist;
VertParent[t] = s;
LocalPos[t] = wt - ws;
}
}
}
}
void Conform () {
TargetVertices = TargetMesh.GetComponent<MeshFilter> ().mesh.vertices;
Mesh provmesh = new Mesh ();
SourceMesh.GetComponent<SkinnedMeshRenderer> ().BakeMesh (provmesh);
SourceVertices = provmesh.vertices;
for (int t = 0; t < TargetVertices.Length; t++) {
Vector3 ws = SourceMesh.transform.TransformPoint (SourceVertices [VertParent [t]]);
Vector3 lt = TargetMesh.transform.InverseTransformPoint (ws);
TargetVertices [t] = lt + LocalPos [t];
}
TargetMesh.GetComponent<MeshFilter>().mesh.vertices = TargetVertices;
TargetMesh.GetComponent<MeshFilter> ().mesh.RecalculateBounds ();
TargetMesh.GetComponent<MeshFilter> ().mesh.RecalculateNormals ();
TargetMesh.GetComponent<MeshFilter> ().mesh.RecalculateTangents ();
}
Your answer
Follow this Question
Related Questions
How to manipulate(flatten) the Mesh of an object 0 Answers
Problem with setting vertices positions and smoothing groups. 1 Answer
mesh deformation to a shape ,how do i found that my mesh is deformed to a specific shape 0 Answers
Splitting shared vertices in a plane 0 Answers
Flawed Mesh Manipulation Question! 1 Answer