コード例 #1
0
    bool SetupBase()
    {
        // Only set initialized to true in the end, when we know that no errors have occurred.
        m_obj.Ready = false;

        // Find the skeleton root (child of the GameObject) if none has been set already
        if (m_obj.RootBone == null)
        {
            if (m_obj.Animator.isHuman)
            {
                m_obj.RootBone = m_obj.Animator.GetBoneTransform(HumanBodyBones.Hips);
                if (m_obj.RootBone.parent != null)
                {
                    m_obj.RootBone = m_obj.RootBone.parent;
                }
            }

            if (m_obj.RootBone == null)
            {
                Debug.LogError(name + ": Root bone Transform null.", this);
                return(false);
            }
        }

        if (m_obj.PelvisBone == null)
        {
            if (m_obj.Animator.isHuman)
            {
                m_obj.PelvisBone = m_obj.Animator.GetBoneTransform(HumanBodyBones.Hips);
            }

            if (m_obj.PelvisBone == null)
            {
                Debug.LogError(name + ": Pelvis bone Transform null.", this);
                return(false);
            }
        }

        if (m_obj.Legs == null || m_obj.Legs.Length == 0)
        {
            if (m_obj.Animator.isHuman)
            {
                m_obj.Legs = new MotionLeg[Int.Two];
                var leg = new MotionLeg()
                {
                    Hip   = m_obj.Animator.GetBoneTransform(HumanBodyBones.LeftUpperLeg),
                    Ankle = m_obj.Animator.GetBoneTransform(HumanBodyBones.LeftFoot),
                    Toe   = m_obj.Animator.GetBoneTransform(HumanBodyBones.LeftToes),
                };

                if (leg.Toe.childCount > Int.Zero)
                {
                    leg.Toe = leg.Toe.GetChild(Int.Zero);
                }

                m_obj.Legs[Int.Zero] = leg;
                leg = new MotionLeg()
                {
                    Hip   = m_obj.Animator.GetBoneTransform(HumanBodyBones.RightUpperLeg),
                    Ankle = m_obj.Animator.GetBoneTransform(HumanBodyBones.RightFoot),
                    Toe   = m_obj.Animator.GetBoneTransform(HumanBodyBones.RightToes),
                };

                if (leg.Toe.childCount > Int.Zero)
                {
                    leg.Toe = leg.Toe.GetChild(Int.Zero);
                }

                m_obj.Legs[Int.One] = leg;
            }
            else
            {
                Debug.LogError(name + ": Legs not defined.", this);
                return(false);
            }
        }

        // Calculate data for LegInfo objects
        m_obj.HipAverage = Vector3.zero;

        for (int i = 0; i < m_obj.Legs.Length; i++)
        {
            var leg = m_obj.Legs[i];
            //check all legs bones
            if (leg.Hip == null ||
                leg.Ankle == null)
            {
                Debug.LogError(name + ": Leg Transforms are null.", this);
                return(false);
            }

            // Calculate leg bone chains
            if (leg.Toe == null)
            {
                leg.Toe = leg.Ankle;
            }

            leg.LegChain  = Exts.GetTransformChain(leg.Hip, leg.Ankle);
            leg.FootChain = Exts.GetTransformChain(leg.Ankle, leg.Toe);

            // Calculate length of leg
            leg.LegLength = Float.Zero;
            for (int ci = 0; ci < leg.LegChain.Length - Int.One; ci++)
            {
                var a = leg.LegChain[ci + Int.One].position;
                var b = leg.LegChain[ci].position;
                leg.LegLength += (
                    m_obj.Transform.InverseTransformPoint(a) - m_obj.Transform.InverseTransformPoint(b)
                    ).magnitude;
            }

            m_obj.HipAverage += m_obj.Transform.InverseTransformPoint(leg.LegChain[0].position);
            SetupFoot(i);
        }

        m_obj.HipAverage        /= m_obj.Legs.Length;
        m_obj.HipAverageGround   = m_obj.HipAverage;
        m_obj.HipAverageGround.y = m_obj.GroundPlaneHeight;
        return(true);
    }