コード例 #1
0
        private static void CreateLimbJoints(Transform connectedBone, Transform bone1, Transform bone2, Transform bone3, Transform root, Vector3 defaultWorldDirection, JointType jointType, CreateJointParams.Limits limits1, CreateJointParams.Limits limits2, CreateJointParams.Limits limits3)
        {
            Quaternion bone1DefaultLocalRotation = bone1.localRotation;

            bone1.rotation = Quaternion.FromToRotation(bone1.rotation * (bone2.position - bone1.position), defaultWorldDirection) * bone1.rotation;

            Vector3 bone1Dir = (bone2.position - bone1.position).normalized;
            Vector3 bone2Dir = (bone3.position - bone2.position).normalized;

            Vector3 bendPlaneNormal      = -Vector3.Cross(bone1Dir, bone2Dir);
            float   bone2PoseAngleOffset = Vector3.Angle(bone1Dir, bone2Dir);

            bool  isVertical             = Mathf.Abs(Vector3.Dot(bone1Dir, root.up)) > 0.5f;
            float verticalAngleOffsetMlp = isVertical? 100f: 1f;             // Fixing Mixamo's inverted legs

            // Fixing straight limbs
            if (bone2PoseAngleOffset < 0.01f * verticalAngleOffsetMlp)
            {
                if (isVertical)
                {
                    bendPlaneNormal = Vector3.Dot(bone1Dir, root.up) > 0f? root.right: -root.right;
                }
                else
                {
                    bendPlaneNormal = Vector3.Dot(bone1Dir, root.right) > 0f? root.up: -root.up;
                }

                //Debug.LogWarning("Limb " + bone1.name + ", " + bone2.name + ", " + bone3.name + " appears to be completely stretched out, Ragdoll Creator can not know how to assign joint limits. Please rotate the elbow/knee bone slightly towards it's natural bending direction.");
            }

            CreateJoint(new CreateJointParams(
                            bone1.GetComponent <Rigidbody>(),
                            connectedBone.GetComponent <Rigidbody>(),
                            bone2,
                            bendPlaneNormal,
                            limits1,
                            jointType
                            ));

            CreateJoint(new CreateJointParams(
                            bone2.GetComponent <Rigidbody>(),
                            bone1.GetComponent <Rigidbody>(),
                            bone3,
                            bendPlaneNormal,
                            new CreateJointParams.Limits(limits2.minSwing - bone2PoseAngleOffset, limits2.maxSwing - bone2PoseAngleOffset, limits2.swing2, limits2.twist),
                            jointType
                            ));

            if (bone3.GetComponent <Rigidbody>() != null)
            {
                CreateJoint(new CreateJointParams(
                                bone3.GetComponent <Rigidbody>(),
                                bone2.GetComponent <Rigidbody>(),
                                null,
                                bendPlaneNormal,
                                limits3,
                                jointType
                                ));
            }

            bone1.localRotation = bone1DefaultLocalRotation;
        }
コード例 #2
0
        private static void CreateJoints(BipedRagdollReferences r, Options o)
        {
            // Torso
            if (r.spine == null)
            {
                o.spine = false;
            }
            if (r.chest == null)
            {
                o.chest = false;
            }

            float spineMinSwing = -30f * o.jointRange;
            float spineMaxSwing = 10f * o.jointRange;
            float spineSwing2   = 25f * o.jointRange;
            float spineTwist    = 25f * o.jointRange;

            CreateJoint(new CreateJointParams(
                            r.hips.GetComponent <Rigidbody>(),
                            null,
                            (o.spine? r.spine: (o.chest? r.chest: r.head)),
                            r.root.right,
                            new CreateJointParams.Limits(0f, 0f, 0f, 0f),
                            o.joints
                            ));

            if (o.spine)
            {
                CreateJoint(new CreateJointParams(
                                r.spine.GetComponent <Rigidbody>(),
                                r.hips.GetComponent <Rigidbody>(),
                                (o.chest? r.chest: r.head),
                                r.root.right,
                                new CreateJointParams.Limits(spineMinSwing, spineMaxSwing, spineSwing2, spineTwist),
                                o.joints
                                ));
            }

            if (o.chest)
            {
                CreateJoint(new CreateJointParams(
                                r.chest.GetComponent <Rigidbody>(),
                                (o.spine? r.spine.GetComponent <Rigidbody>(): r.hips.GetComponent <Rigidbody>()),
                                r.head,
                                r.root.right,
                                new CreateJointParams.Limits(spineMinSwing, spineMaxSwing, spineSwing2, spineTwist),
                                o.joints
                                ));
            }

            // Head
            Transform lastTorsoBone = o.chest? r.chest: (o.spine? r.spine: r.hips);

            CreateJoint(new CreateJointParams(
                            r.head.GetComponent <Rigidbody>(),
                            lastTorsoBone.GetComponent <Rigidbody>(),
                            null,
                            r.root.right,
                            new CreateJointParams.Limits(-30f, 30f, 30f, 85f),
                            o.joints
                            ));

            // Arms
            CreateJointParams.Limits upperArmLimits = new CreateJointParams.Limits(-35f * o.jointRange, 120f * o.jointRange, 85f * o.jointRange, 45 * o.jointRange);
            CreateJointParams.Limits lowerArmLimits = new CreateJointParams.Limits(0f, 140f * o.jointRange, 10f * o.jointRange, 45f * o.jointRange);
            CreateJointParams.Limits handLimits     = new CreateJointParams.Limits(-50f * o.jointRange, 50f * o.jointRange, 50f * o.jointRange, 25f * o.jointRange);

            // Left Arm
            CreateLimbJoints(
                lastTorsoBone,
                r.leftUpperArm,
                r.leftLowerArm,
                r.leftHand,
                r.root,
                -r.root.right,
                o.joints,
                upperArmLimits,
                lowerArmLimits,
                handLimits);

            // Right Arm
            CreateLimbJoints(
                lastTorsoBone,
                r.rightUpperArm,
                r.rightLowerArm,
                r.rightHand,
                r.root,
                r.root.right,
                o.joints,
                upperArmLimits,
                lowerArmLimits,
                handLimits);

            // Legs
            CreateJointParams.Limits upperLegLimits = new CreateJointParams.Limits(-120f * o.jointRange, 35f * o.jointRange, 85f * o.jointRange, 45 * o.jointRange);
            CreateJointParams.Limits lowerLegLimits = new CreateJointParams.Limits(0f, 140f * o.jointRange, 10f * o.jointRange, 45f * o.jointRange);
            CreateJointParams.Limits footLimits     = new CreateJointParams.Limits(-50f * o.jointRange, 50f * o.jointRange, 50f * o.jointRange, 25f * o.jointRange);

            // Left Leg
            CreateLimbJoints(
                r.hips,
                r.leftUpperLeg,
                r.leftLowerLeg,
                r.leftFoot,
                r.root,
                -r.root.up,
                o.joints,
                upperLegLimits,
                lowerLegLimits,
                footLimits);

            // Right Leg
            CreateLimbJoints(
                r.hips,
                r.rightUpperLeg,
                r.rightLowerLeg,
                r.rightFoot,
                r.root,
                -r.root.up,
                o.joints,
                upperLegLimits,
                lowerLegLimits,
                footLimits);
        }