In my application user can wear different clothes. Clothes for character are skinned meshes with bones, identical to character’s bones. Clothes should dress on a character through attaching clothes bones to character’s bones. The problem is that bones are attached, but clothes mesh worn “unevenly”, some parts of character’s body get off from clothes (see pic.1). Dressing should be processed at run-time, clothes are load from asset bundles. Please, see my code.
Start Download() function:
function StartDownload () {
if (url.IndexOf ("file://") == 0 || url.IndexOf ("http://") == 0)
{
download = new WWW (url);
}
yield download;
assetBundle = download.assetBundle;
if (assetBundle != null)
{
loadedObject = assetBundle.mainAsset;
if (loadedObject != null)
{
instanced = Instantiate(loadedObject);
// get all components of instanced object
numT = instanced.GetComponentsInChildren(Transform);
// get mesh from array of components
objectMesh = getMesh(numT);
// get link on SkinnedMeshRenderer
skm = objectMesh.GetComponent("SkinnedMeshRenderer") as SkinnedMeshRenderer;
// get link on sharedMesh from SkinnedMeshRenderer
m = skm.sharedMesh;
// add mesh filter
objectMeshFilter = GameObject.Find(objectMesh.name).AddComponent(MeshFilter);
// add current clothes mesh to mesh filter
objectMeshFilter.mesh = m;
// wear current clothes on girl
wearing();
// destroy instanced object
Destroy(instanced);
}
else
Debug.Log("Couldnt load resource");
}
else
{
Debug.Log("Couldnt load resource");
}}
function wearing() {
bonesArr = new Transform [numT.length-1];
// we start at [1] because transform of game object itself is at [0] position
for (i=1;i<numT.length;i++) {
bonesArr[i-1] = numT[i];
}
for (var b=0;b<bonesArr.length;b++) {
bonesArr[b].transform.parent = girl.Find(bonesArr[b].name).transform;
bonesArr[b].transform.position = girl.Find(bonesArr[b].name).transform.position;
// if game object has renderer -> it is a mesh
if (bonesArr[b].renderer != null) {
// bind mesh to root bone
bonesArr[b].transform.parent = girl.Find("root").transform;
bonesArr[b].transform.position = girl.Find("root").transform.position;
}
// change names of attched clothes bones
if (bonesArr[b].renderer == null) {
// gameObject doesnt contain mesh -> it is a bone
bonesArr[b].name = bonesArr[b].name + objectMesh.name;
}
}
}
It happened that all clothing was modeled around this character, staying in a T-pose. Now, I try to dress this clothing on a character staying in another, “default” pose. Maybe, problem in it? But, as I understand, if mesh rigged to bones and bones changed their positions, mesh should follow bones. So, please review my code and tell me, how can I solve this problem? Perhaps, I don’t understand something in mesh behaviour in Unity.
P.S. I post this problem also to answers.unity3d, but no one answered yet.