【问题标题】:Swapping animations of mecanim bones交换机械骨骼的动画
【发布时间】:2016-02-26 23:56:18
【问题描述】:

我编写了一个函数来交换应用于 mecanim 骨骼的旋转;例如左右手:

private void swapLimbsRots(Quaternion[] rotationsArray)
{
    Quaternion bone1Rot = rotationsArray [(int)XsAnimationSegment.LeftHand];    // 16
    Quaternion bone2Rot = rotationsArray [(int)XsAnimationSegment.RightHand];   // 20

    Quaternion temp = bone1Rot;
    bone1Rot = bone2Rot;
    bone2Rot = temp;

    Debug.Log("I am called");
}

我只交换轮换,因为我认为这样就足够了。是这样吗,还是我也应该调换位置?

下面是我的更新方法,我相信交换左右手的旋转,然后使用新的旋转数组来更新我的统一字符:

void Update()
{
    Vector3[] latestPositions;
    Quaternion[] latestOrientations;

    if (mvnActors.getLatestPose(actorID, out latestPositions, out latestOrientations))
            {

    if (swapLimbs)
    {
        swapLimbsRots(latestOrientations);
    }

    updateMvnActor(currentPose, latestPositions, latestOrientations);
    updateModel(currentPose, targetModel);
}

当然,我的类变量包含必要的声明,如下所示。这就是函数中提到1620的原因:

public bool swapLimbs = false;              

private Dictionary<XsAnimationSegment, HumanBodyBones> mecanimBones;

        /// The 23 segments numbers for the animation. The first part of the dictionary pairs.
        public enum XsAnimationSegment
        {
            Pelvis = 0, // Hips

            RightUpperLeg = 1,
            RightLowerLeg = 2,
            RightFoot = 3,
            RightToe = 4,

            LeftUpperLeg = 5,
            LeftLowerLeg = 6,
            LeftFoot = 7,
            LeftToe = 8,

            L5 = 9,     // not used
            L3 = 10,    // Spine
            T12 = 11,   // not used
            T8 = 12,    // Chest

            LeftShoulder = 13,
            LeftUpperArm = 14,
            LeftLowerArm = 15,
            LeftHand = 16,

            RightShoulder = 17,
            RightUpperArm = 18,
            RightLowerArm = 19,
            RightHand = 20,

            Neck = 21,
            Head = 22
        }


        /// The segments order.
        int[] segmentOrder =
        {
                    (int)XsAnimationSegment.Pelvis,

                    (int)XsAnimationSegment.RightUpperLeg,
                    (int)XsAnimationSegment.RightLowerLeg,
                    (int)XsAnimationSegment.RightFoot,
                    (int)XsAnimationSegment.RightToe,

                    (int)XsAnimationSegment.LeftUpperLeg,
                    (int)XsAnimationSegment.LeftLowerLeg,
                    (int)XsAnimationSegment.LeftFoot,
                    (int)XsAnimationSegment.LeftToe,

                    (int)XsAnimationSegment.L5,
                    (int)XsAnimationSegment.L3,
                    (int)XsAnimationSegment.T12,
                    (int)XsAnimationSegment.T8,

                    (int)XsAnimationSegment.LeftShoulder,
                    (int)XsAnimationSegment.LeftUpperArm,
                    (int)XsAnimationSegment.LeftLowerArm,
                    (int)XsAnimationSegment.LeftHand,

                    (int)XsAnimationSegment.RightShoulder,
                    (int)XsAnimationSegment.RightUpperArm,
                    (int)XsAnimationSegment.RightLowerArm,
                    (int)XsAnimationSegment.RightHand,

                    (int)XsAnimationSegment.Neck,
                    (int)XsAnimationSegment.Head
        };


        /// Map the mecanim bones to xsens segments. 23 bones/segments
        protected void mapMecanimBones()
        {
            mecanimBones = new Dictionary<XsAnimationSegment, HumanBodyBones>();

            mecanimBones.Add(XsAnimationSegment.Pelvis,         HumanBodyBones.Hips);
            mecanimBones.Add(XsAnimationSegment.LeftUpperLeg,   HumanBodyBones.LeftUpperLeg);
            mecanimBones.Add(XsAnimationSegment.LeftLowerLeg,   HumanBodyBones.LeftLowerLeg);
            mecanimBones.Add(XsAnimationSegment.LeftFoot,       HumanBodyBones.LeftFoot);
            mecanimBones.Add(XsAnimationSegment.LeftToe,        HumanBodyBones.LeftToes);
            mecanimBones.Add(XsAnimationSegment.RightUpperLeg,  HumanBodyBones.RightUpperLeg);
            mecanimBones.Add(XsAnimationSegment.RightLowerLeg,  HumanBodyBones.RightLowerLeg);
            mecanimBones.Add(XsAnimationSegment.RightFoot,      HumanBodyBones.RightFoot);
            mecanimBones.Add(XsAnimationSegment.RightToe,       HumanBodyBones.RightToes);
            mecanimBones.Add(XsAnimationSegment.L5,             HumanBodyBones.LastBone);
            mecanimBones.Add(XsAnimationSegment.L3,             HumanBodyBones.Spine);
            mecanimBones.Add(XsAnimationSegment.T12,            HumanBodyBones.LastBone);
            mecanimBones.Add(XsAnimationSegment.T8,             HumanBodyBones.Chest);
            mecanimBones.Add(XsAnimationSegment.LeftShoulder,   HumanBodyBones.LeftShoulder);
            mecanimBones.Add(XsAnimationSegment.LeftUpperArm,   HumanBodyBones.LeftUpperArm);
            mecanimBones.Add(XsAnimationSegment.LeftLowerArm,   HumanBodyBones.LeftLowerArm);
            mecanimBones.Add(XsAnimationSegment.LeftHand,       HumanBodyBones.LeftHand);
            mecanimBones.Add(XsAnimationSegment.RightShoulder,  HumanBodyBones.RightShoulder);
            mecanimBones.Add(XsAnimationSegment.RightUpperArm,  HumanBodyBones.RightUpperArm);
            mecanimBones.Add(XsAnimationSegment.RightLowerArm,  HumanBodyBones.RightLowerArm);
            mecanimBones.Add(XsAnimationSegment.RightHand,      HumanBodyBones.RightHand);
            mecanimBones.Add(XsAnimationSegment.Neck,           HumanBodyBones.Neck);
            mecanimBones.Add(XsAnimationSegment.Head,           HumanBodyBones.Head);
        }

但是,左右手的动画方式显然不会发生交换。我错过了什么或做错了什么?

【问题讨论】:

    标签: unity3d unity3d-mecanim


    【解决方案1】:

    动画是更新中的事情,如果你想改变它的任何内容,你应该在LateUpdate() 中进行

    只需将您的 Update() 更改为 LateUpdate() 即可修复它;

    【讨论】:

    • 谢谢。你是对的,但是在LateUpdate() 中我不会有latestOrientations,这是我的swapLimbsRots 方法的唯一参数...你是建议我应该在LateUpdate 中再次调用mvnActors.getLatestPose(actorID, out latestPositions, out latestOrientations) 吗?
    • 我建议您将 Update() 更改为 LateUpdate(),如果这不起作用,您可以在课堂上设置 latestOrientationslatestPositions 全局,然后只使用 @987654331 @ 在LateUpdate() 中,其余的留在更新中
    • 谢谢。我将 2 个变量设为全局变量。
    • 既然我正在处理全局变量,Update 的计算结果不应该在LateUpdate 中可用吗?我的latestOrientations 变量(它是一个四元数数组)在Update 中接收数据,但是当我使用Debug.LogLateUpdate 中仅打印其中一个元素时,我收到NullReferenceException: Object reference not set to an instance of an object 错误消息。我现在很困惑!
    • 哪一行触发了 NRE?
    猜你喜欢
    • 2017-01-30
    • 2012-10-17
    • 2013-08-19
    • 2014-10-16
    • 2014-09-23
    • 2017-05-05
    • 2011-04-02
    • 2021-06-09
    • 2017-05-03
    相关资源
    最近更新 更多