Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Problem with clothing

Discussion in 'Editor & General Support' started by korotkevich-md, Jun 10, 2011.

  1. korotkevich-md

    korotkevich-md

    Joined:
    Jun 10, 2011
    Posts:
    21
    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:
    Code (csharp):
    1.  
    2. function StartDownload () {
    3.  if (url.IndexOf ("file://") == 0 || url.IndexOf ("http://") == 0)
    4.  {
    5.  download = new WWW (url);
    6.  }
    7.  yield download;
    8.  assetBundle = download.assetBundle;
    9. if (assetBundle != null)
    10. {
    11.     loadedObject = assetBundle.mainAsset;
    12.  
    13.     if (loadedObject != null)
    14.     {
    15.     instanced = Instantiate(loadedObject);
    16.     // get all components of instanced object
    17.     numT = instanced.GetComponentsInChildren(Transform);
    18.     // get mesh from array of components
    19.     objectMesh = getMesh(numT);
    20.     // get link on SkinnedMeshRenderer
    21.     skm = objectMesh.GetComponent("SkinnedMeshRenderer") as SkinnedMeshRenderer;
    22.     // get link on sharedMesh from SkinnedMeshRenderer
    23.     m = skm.sharedMesh;
    24.     // add mesh filter
    25.     objectMeshFilter = GameObject.Find(objectMesh.name).AddComponent(MeshFilter);
    26.     // add current clothes mesh to mesh filter
    27.     objectMeshFilter.mesh = m;
    28.     // wear current clothes on girl
    29.     wearing();
    30.     // destroy instanced object
    31.     Destroy(instanced);
    32.     }
    33.     else
    34.         Debug.Log("Couldnt load resource");
    35.         }
    36. else
    37. {
    38.     Debug.Log("Couldnt load resource");
    39. }}
    40.  
    41. function wearing() {
    42.  bonesArr = new Transform [numT.length-1];
    43.  // we start at [1] because transform of game object itself is at [0] position
    44.  for (i=1;i<numT.length;i++) {
    45.  bonesArr[i-1] = numT[i];
    46.  }
    47.  for (var b=0;b<bonesArr.length;b++) {
    48.  bonesArr[b].transform.parent = girl.Find(bonesArr[b].name).transform;
    49.  bonesArr[b].transform.position = girl.Find(bonesArr[b].name).transform.position;
    50.  // if game object has renderer -> it is a mesh
    51.  if (bonesArr[b].renderer != null) {
    52.  // bind mesh to root bone
    53.  bonesArr[b].transform.parent = girl.Find("root").transform;
    54.  bonesArr[b].transform.position = girl.Find("root").transform.position;
    55.  }
    56.  // change names of attched clothes bones
    57.  if (bonesArr[b].renderer == null) {
    58.  // gameObject doesnt contain mesh -> it is a bone
    59.  bonesArr[b].name = bonesArr[b].name + objectMesh.name;
    60.  }
    61.  }
    62. }
    63.  
    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.
    $girl&skirt.jpg
    P.S. I post this problem also to answers.unity3d, but no one answered yet.
     
    Last edited: Jun 10, 2011
  2. kablammyman

    kablammyman

    Joined:
    Nov 22, 2010
    Posts:
    507
    i gave you some advice to your question in unity answers. If you solved this issue, please share what you did.
     
unityunity