将骨架绑定到Unity中的新网格

时间:2016-06-10 13:05:31

标签: unity3d mesh renderer

我正在尝试将骨架添加到场景中的新网格中。原始网格包含骨架,模型根据骨骼位置和旋转移动。我现在有一个没有骨架的新网格。我想将这个新网格设计为现有骨架。下面是我的代码,我复制骨骼权重并添加蒙皮网格渲染器。

它不起作用。它使我的新网格变形非常糟糕。仅仅为了背景,我的新网格只是旧网格的x缩放版本,没有附加骨架。

// Use this for initialization
void Start () 
{
    vertices = mesh.vertices;
    int n = mesh.vertexCount;
    Debug.Log ("vertex count" + n);

    //Bone Weights
    weights=new BoneWeight[mesh.vertexCount];
    weights = mesh.boneWeights;
    Debug.Log ("vertex 2:" + weights [2].weight2 + "vertex 100:" + weights [100].weight0);

    //Renderer
    top = this.transform.Find("Tops").gameObject;
    SkinnedMeshRenderer meshRend = top.GetComponent<SkinnedMeshRenderer> ();

    //Skeleton
    Transform[] originalMeshBones;
    originalMeshBones = meshRend.bones;
    Matrix4x4[] bindPoses = mesh.bindposes;
    for(int i = 0; i < 16; i++){
        bindPoses[i] = originalMeshBones[i].worldToLocalMatrix * transform.localToWorldMatrix;
    }

    //Blend Mesh setting
    tshirt.AddComponent<Animator>();
    tshirt.GetComponent<Animator> ().avatar = humanAvatar;
    tshirt.GetComponent<Animator> ().applyRootMotion = true;


    tshirt = tshirt.transform.Find ("Tops").gameObject;

    //blendMeshRend = tshirt.AddComponent<SkinnedMeshRenderer> ();
    blendMeshRend = tshirt.GetComponent<SkinnedMeshRenderer> ();

    BoneWeight[] blendMeshWeights=new BoneWeight[mesh.vertexCount];
    blendMesh.boneWeights = weights;
    Debug.Log ("vertex 2: " + blendMesh.boneWeights [2].weight2 + "vertex 100:" + blendMesh.boneWeights [100].weight0);

    blendMesh.bindposes = bindPoses;
    Debug.Log ("bind poses 0"+blendMesh.bindposes [7].m00);
    blendMeshRend.bones = originalMeshBones;

    //tshirt.transform.parent = this.transform;
    blendMeshRend.sharedMesh = blendMesh;
    blendMeshRend.rootBone = meshRend.rootBone;
}

0 个答案:

没有答案