Unity3D-程序性地创建骨骼的问题(非类人动物)

时间:2019-06-29 17:41:53

标签: unity3d procedural skeletal-mesh

我想将一本书(仅覆盖,没有页面)制作为一个可以操纵以打开和关闭的网格。 但是我想通过使用其他游戏对象通过代码添加骨骼来指导程序。因此,书脊,前盖和后盖之间的间距很小。计划是通过代码进行绑定,然后像平常使用前一样将书的各个部分一起移动。 我目前的解决方案没有任何结果,所以我想知道我是否完全误解了该如何完成?首先,我找不到有关Unity骨量系统的太多信息。

单本书网格:

Book mesh

现在,为了选择应该受打开/关闭运动影响的顶点,我在此帮助对象中选择了这些顶点(在列表m_AffectedVertices中):

相交的顶点应该是绑定的灵活部分:

Vertices selectors

蒙皮的网格渲染器已附加到“新建主体”对象。

Hierarchy

这是我用来向要影响的顶点添加骨骼权重以及代表骨骼的GameObjects的代码。

private void _AddBones()
{
    if (m_SkinnedRenderer != null && (m_AffectedVertices != null && m_AffectedVertices.Count > 1))
    {
        var mesh            = m_SkinnedRenderer.sharedMesh;
        var parentObject    = m_NewBody;
        bool useBindPoses   = false;

        // Make bone weight for vertices
        BoneWeight[]    weights     = new BoneWeight[mesh.vertexCount];
        Transform[]     bones       = new Transform[2];
        Matrix4x4[]     bindPoses   = new Matrix4x4[2];

        for (int i = 0; i < weights.Length; i++)
        {
            if(m_AffectedVertices.Contains(mesh.vertices[i]))
            {
                weights[i].boneIndex0 = 0;
                weights[i].weight0    = 1;
            } 
            else
            {
                weights[i].boneIndex0 = 0;
                weights[i].weight0    = 0;
            }
        }
        mesh.boneWeights = weights;

        // Root bone
        bones[0]                = new GameObject("Root Bone").transform;
        bones[0].parent         = parentObject.transform;
        bones[0].localRotation  = Quaternion.identity;
        bones[0].localPosition  = Vector3.zero;     
        if(useBindPoses) { bindPoses[0] = bones[0].worldToLocalMatrix * parentObject.transform.localToWorldMatrix; }

        // Page bone
        bones[1]                = new GameObject("Left Bone").transform;
        bones[1].parent         = bones[0].transform;
        bones[1].localRotation  = Quaternion.identity;
        var pos                 = Hint.transform.localPosition;
        pos.y                   = pos.y * 5f;
        bones[1].localPosition  = pos;          
        if(useBindPoses) { bindPoses[1] = bones[1].worldToLocalMatrix * parentObject.transform.localToWorldMatrix; }

        if(useBindPoses) { mesh.bindposes = bindPoses; }
        m_SkinnedRenderer.sharedMesh = mesh;
        m_SkinnedRenderer.bones = bones;
        m_SkinnedRenderer.rootBone = bones[0];
    }
}

我已经确认要受影响的顶点列表包含由蓝色相交框选择的顶点。骨骼变换游戏对象已成功添加,但移动过程中没有任何反应。

0 个答案:

没有答案