예제 #1
0
        public static BipedRagdollReferences FromAvatar(Animator animator)
        {
            BipedRagdollReferences r = new BipedRagdollReferences();

            if (!animator.isHuman)
            {
                return(r);
            }

            r.root = animator.transform;

            r.hips  = animator.GetBoneTransform(HumanBodyBones.Hips);
            r.spine = animator.GetBoneTransform(HumanBodyBones.Spine);
            r.chest = animator.GetBoneTransform(HumanBodyBones.Chest);
            r.head  = animator.GetBoneTransform(HumanBodyBones.Head);

            r.leftUpperArm = animator.GetBoneTransform(HumanBodyBones.LeftUpperArm);
            r.leftLowerArm = animator.GetBoneTransform(HumanBodyBones.LeftLowerArm);
            r.leftHand     = animator.GetBoneTransform(HumanBodyBones.LeftHand);

            r.rightUpperArm = animator.GetBoneTransform(HumanBodyBones.RightUpperArm);
            r.rightLowerArm = animator.GetBoneTransform(HumanBodyBones.RightLowerArm);
            r.rightHand     = animator.GetBoneTransform(HumanBodyBones.RightHand);

            r.leftUpperLeg = animator.GetBoneTransform(HumanBodyBones.LeftUpperLeg);
            r.leftLowerLeg = animator.GetBoneTransform(HumanBodyBones.LeftLowerLeg);
            r.leftFoot     = animator.GetBoneTransform(HumanBodyBones.LeftFoot);

            r.rightUpperLeg = animator.GetBoneTransform(HumanBodyBones.RightUpperLeg);
            r.rightLowerLeg = animator.GetBoneTransform(HumanBodyBones.RightLowerLeg);
            r.rightFoot     = animator.GetBoneTransform(HumanBodyBones.RightFoot);

            return(r);
        }
예제 #2
0
        public static Options AutodetectOptions(BipedRagdollReferences r)
        {
            Options o = Options.Default;

            if (r.spine == null)
            {
                o.spine = false;
            }
            if (r.chest == null)
            {
                o.chest = false;
            }

            // If chest bone is too high
            if (o.chest && Vector3.Dot(r.root.up, r.chest.position - GetUpperArmCentroid(r)) > 0f)
            {
                o.chest = false;
                if (r.spine != null)
                {
                    o.spine = true;
                }
            }

            return(o);
        }
예제 #3
0
        private static void MassDistribution(BipedRagdollReferences r, Options o)
        {
            int torsoBones = 3;

            if (r.spine == null)
            {
                o.spine = false;
                torsoBones--;
            }
            if (r.chest == null)
            {
                o.chest = false;
                torsoBones--;
            }

            float torsoPrc    = 0.508f / (float)torsoBones;
            float headPrc     = 0.0732f;
            float upperArmPrc = 0.027f;
            float lowerArmPrc = 0.016f;
            float handPrc     = 0.0066f;
            float upperLegPrc = 0.0988f;
            float lowerLegPrc = 0.0465f;
            float footPrc     = 0.0145f;

            r.hips.GetComponent <Rigidbody>().mass = torsoPrc * o.weight;
            if (o.spine)
            {
                r.spine.GetComponent <Rigidbody>().mass = torsoPrc * o.weight;
            }
            if (o.chest)
            {
                r.chest.GetComponent <Rigidbody>().mass = torsoPrc * o.weight;
            }

            r.head.GetComponent <Rigidbody>().mass = headPrc * o.weight;

            r.leftUpperArm.GetComponent <Rigidbody>().mass  = upperArmPrc * o.weight;
            r.rightUpperArm.GetComponent <Rigidbody>().mass = r.leftUpperArm.GetComponent <Rigidbody>().mass;

            r.leftLowerArm.GetComponent <Rigidbody>().mass  = lowerArmPrc * o.weight;
            r.rightLowerArm.GetComponent <Rigidbody>().mass = r.leftLowerArm.GetComponent <Rigidbody>().mass;

            if (o.hands)
            {
                r.leftHand.GetComponent <Rigidbody>().mass  = handPrc * o.weight;
                r.rightHand.GetComponent <Rigidbody>().mass = r.leftHand.GetComponent <Rigidbody>().mass;
            }

            r.leftUpperLeg.GetComponent <Rigidbody>().mass  = upperLegPrc * o.weight;
            r.rightUpperLeg.GetComponent <Rigidbody>().mass = r.leftUpperLeg.GetComponent <Rigidbody>().mass;

            r.leftLowerLeg.GetComponent <Rigidbody>().mass  = lowerLegPrc * o.weight;
            r.rightLowerLeg.GetComponent <Rigidbody>().mass = r.leftLowerLeg.GetComponent <Rigidbody>().mass;

            if (o.feet)
            {
                r.leftFoot.GetComponent <Rigidbody>().mass  = footPrc * o.weight;
                r.rightFoot.GetComponent <Rigidbody>().mass = r.leftFoot.GetComponent <Rigidbody>().mass;
            }
        }
예제 #4
0
        public static void ClearBipedRagdoll(BipedRagdollReferences r)
        {
            var transforms = r.GetRagdollTransforms();

            foreach (Transform t in transforms)
            {
                ClearTransform(t);
            }
        }
        void OnEnable()
        {
            if (script == null)
            {
                return;
            }
            if (Application.isPlaying)
            {
                return;
            }

            // Autodetection
            if (script.references.IsEmpty(false))
            {
                Animator animator = script.gameObject.GetComponent <Animator>();

                if (animator == null && script.references.root != null)
                {
                    animator = script.references.root.GetComponentInChildren <Animator>();
                    if (animator == null)
                    {
                        animator = GetAnimatorInParents(script.references.root);
                    }
                }

                if (animator != null)
                {
                    script.references = BipedRagdollReferences.FromAvatar(animator);
                }
                else
                {
                    BipedReferences r = new BipedReferences();
                    BipedReferences.AutoDetectReferences(ref r, script.transform, BipedReferences.AutoDetectParams.Default);
                    if (r.isFilled)
                    {
                        script.references = BipedRagdollReferences.FromBipedReferences(r);
                    }
                }

                if (!OnRoot())
                {
                    Debug.LogWarning("BipedRagdollCreator must be added to the root of the character. Destroying the component.");
                    DestroyImmediate(script);
                    return;
                }

                string msg = string.Empty;
                if (script.references.IsValid(ref msg))
                {
                    script.options = BipedRagdollCreator.AutodetectOptions(script.references);
                    //BipedRagdollCreator.Create(script.references, script.options);

                    //if (animator != null) DestroyImmediate(animator);
                    //if (script.GetComponent<Animation>() != null) DestroyImmediate(script.GetComponent<Animation>());
                }
            }
        }
예제 #6
0
        public static bool IsClear(BipedRagdollReferences r)
        {
            var transforms = r.GetRagdollTransforms();

            foreach (Transform t in transforms)
            {
                if (t.GetComponent <Rigidbody>() != null)
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #7
0
        public static void Create(BipedRagdollReferences r, Options options)
        {
            if (!r.IsValid())
            {
                return;
            }

            // Clean up
            ClearAll(r.root);

            // Colliders
            CreateColliders(r, options);

            // Rigidbodies
            MassDistribution(r, options);

            // Joints
            CreateJoints(r, options);
        }
예제 #8
0
        public static void Create(BipedRagdollReferences r, Options options)
        {
            string msg = string.Empty;

            if (!r.IsValid(ref msg))
            {
                Debug.LogWarning(msg);
                return;
            }

            // Clean up
            ClearAll(r.root);

            // Colliders
            CreateColliders(r, options);

            // Rigidbodies
            MassDistribution(r, options);

            // Joints
            CreateJoints(r, options);
        }
예제 #9
0
        public static BipedRagdollReferences FromBipedReferences(BipedReferences biped)
        {
            BipedRagdollReferences r = new BipedRagdollReferences();

            r.root = biped.root;

            r.hips = biped.pelvis;

            if (biped.spine != null && biped.spine.Length > 0)
            {
                r.spine = biped.spine[0];
                if (biped.spine.Length > 1)
                {
                    r.chest = biped.spine[biped.spine.Length - 1];
                }
            }

            r.head = biped.head;

            r.leftUpperArm = biped.leftUpperArm;
            r.leftLowerArm = biped.leftForearm;
            r.leftHand     = biped.leftHand;

            r.rightUpperArm = biped.rightUpperArm;
            r.rightLowerArm = biped.rightForearm;
            r.rightHand     = biped.rightHand;

            r.leftUpperLeg = biped.leftThigh;
            r.leftLowerLeg = biped.leftCalf;
            r.leftFoot     = biped.leftFoot;

            r.rightUpperLeg = biped.rightThigh;
            r.rightLowerLeg = biped.rightCalf;
            r.rightFoot     = biped.rightFoot;

            return(r);
        }
예제 #10
0
 private static Vector3 GetUpperArmCentroid(BipedRagdollReferences r)
 {
     return(Vector3.Lerp(r.leftUpperArm.position, r.rightUpperArm.position, 0.5f));
 }
예제 #11
0
 private static Vector3 GetUpperArmToHeadCentroid(BipedRagdollReferences r)
 {
     return(Vector3.Lerp(GetUpperArmCentroid(r), r.head.position, 0.5f));
 }
예제 #12
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);
        }
예제 #13
0
        private static void CreateColliders(BipedRagdollReferences r, Options options)
        {
            // Torso
            Vector3 upperArmToHeadCentroid = GetUpperArmToHeadCentroid(r);

            if (r.spine == null)
            {
                options.spine = false;
            }
            if (r.chest == null)
            {
                options.chest = false;
            }

            Vector3 shoulderDirection     = r.rightUpperArm.position - r.leftUpperArm.position;
            float   torsoWidth            = shoulderDirection.magnitude;
            float   torsoProportionAspect = 0.6f;

            // Hips
            Vector3 hipsStartPoint = r.hips.position;

            // Making sure the hip bone is not at the feet
            float toHead = Vector3.Distance(r.head.position, r.root.position);
            float toHips = Vector3.Distance(r.hips.position, r.root.position);

            if (toHips < toHead * 0.2f)
            {
                hipsStartPoint = Vector3.Lerp(r.leftUpperLeg.position, r.rightUpperLeg.position, 0.5f);
            }

            Vector3 lastEndPoint = options.spine? r.spine.position: (options.chest? r.chest.position: upperArmToHeadCentroid);

            hipsStartPoint += (hipsStartPoint - upperArmToHeadCentroid) * 0.1f;
            float hipsWidth = options.spine || options.chest? torsoWidth * 0.8f: torsoWidth;

            CreateCollider(r.hips, hipsStartPoint, lastEndPoint, options.torsoColliders, options.colliderLengthOverlap, hipsWidth, torsoProportionAspect, shoulderDirection);

            // Spine
            if (options.spine)
            {
                Vector3 spineStartPoint = lastEndPoint;
                lastEndPoint = options.chest? r.chest.position: upperArmToHeadCentroid;

                float spineWidth = options.chest? torsoWidth * 0.75f: torsoWidth;

                CreateCollider(r.spine, spineStartPoint, lastEndPoint, options.torsoColliders, options.colliderLengthOverlap, spineWidth, torsoProportionAspect, shoulderDirection);
            }

            if (options.chest)
            {
                Vector3 chestStartPoint = lastEndPoint;
                lastEndPoint = upperArmToHeadCentroid;

                CreateCollider(r.chest, chestStartPoint, lastEndPoint, options.torsoColliders, options.colliderLengthOverlap, torsoWidth, torsoProportionAspect, shoulderDirection);
            }

            // Head
            Vector3 headStartPoint = lastEndPoint;
            Vector3 headEndPoint   = headStartPoint + (headStartPoint - hipsStartPoint) * 0.45f;
            Vector3 axis           = r.head.TransformVector(AxisTools.GetAxisVectorToDirection(r.head, headEndPoint - headStartPoint));

            headEndPoint = headStartPoint + Vector3.Project(headEndPoint - headStartPoint, axis).normalized *(headEndPoint - headStartPoint).magnitude;

            CreateCollider(r.head, headStartPoint, headEndPoint, options.headCollider, options.colliderLengthOverlap, Vector3.Distance(headStartPoint, headEndPoint) * 0.8f);

            // Arms
            float armWidthAspect = 0.4f;

            float leftArmWidth = Vector3.Distance(r.leftUpperArm.position, r.leftLowerArm.position) * armWidthAspect;

            CreateCollider(r.leftUpperArm, r.leftUpperArm.position, r.leftLowerArm.position, options.armColliders, options.colliderLengthOverlap, leftArmWidth);
            CreateCollider(r.leftLowerArm, r.leftLowerArm.position, r.leftHand.position, options.armColliders, options.colliderLengthOverlap, leftArmWidth * 0.9f);

            float rightArmWidth = Vector3.Distance(r.rightUpperArm.position, r.rightLowerArm.position) * armWidthAspect;

            CreateCollider(r.rightUpperArm, r.rightUpperArm.position, r.rightLowerArm.position, options.armColliders, options.colliderLengthOverlap, rightArmWidth);
            CreateCollider(r.rightLowerArm, r.rightLowerArm.position, r.rightHand.position, options.armColliders, options.colliderLengthOverlap, rightArmWidth * 0.9f);

            // Legs
            float legWidthAspect = 0.3f;

            float leftLegWidth = Vector3.Distance(r.leftUpperLeg.position, r.leftLowerLeg.position) * legWidthAspect;

            CreateCollider(r.leftUpperLeg, r.leftUpperLeg.position, r.leftLowerLeg.position, options.legColliders, options.colliderLengthOverlap, leftLegWidth);
            CreateCollider(r.leftLowerLeg, r.leftLowerLeg.position, r.leftFoot.position, options.legColliders, options.colliderLengthOverlap, leftLegWidth * 0.9f);

            float rightLegWidth = Vector3.Distance(r.rightUpperLeg.position, r.rightLowerLeg.position) * legWidthAspect;

            CreateCollider(r.rightUpperLeg, r.rightUpperLeg.position, r.rightLowerLeg.position, options.legColliders, options.colliderLengthOverlap, rightLegWidth);
            CreateCollider(r.rightLowerLeg, r.rightLowerLeg.position, r.rightFoot.position, options.legColliders, options.colliderLengthOverlap, rightLegWidth * 0.9f);

            // Hands
            if (options.hands)
            {
                CreateHandCollider(r.leftHand, r.leftLowerArm, r.root, options);
                CreateHandCollider(r.rightHand, r.rightLowerArm, r.root, options);
            }

            // Feet
            if (options.feet)
            {
                CreateFootCollider(r.leftFoot, r.leftLowerLeg, r.leftUpperLeg, r.root, options);
                CreateFootCollider(r.rightFoot, r.rightLowerLeg, r.rightUpperLeg, r.root, options);
            }
        }
        public static BipedRagdollReferences FromBipedReferences(BipedReferences biped)
        {
            BipedRagdollReferences r = new BipedRagdollReferences();

            r.root = biped.root;

            r.hips = biped.pelvis;

            if (biped.spine != null && biped.spine.Length > 0) {
                r.spine = biped.spine[0];
                if (biped.spine.Length > 1) r.chest = biped.spine[biped.spine.Length - 1];
            }

            r.head = biped.head;

            r.leftUpperArm = biped.leftUpperArm;
            r.leftLowerArm = biped.leftForearm;
            r.leftHand = biped.leftHand;

            r.rightUpperArm = biped.rightUpperArm;
            r.rightLowerArm = biped.rightForearm;
            r.rightHand = biped.rightHand;

            r.leftUpperLeg = biped.leftThigh;
            r.leftLowerLeg = biped.leftCalf;
            r.leftFoot = biped.leftFoot;

            r.rightUpperLeg = biped.rightThigh;
            r.rightLowerLeg = biped.rightCalf;
            r.rightFoot = biped.rightFoot;

            return r;
        }
        public static BipedRagdollReferences FromAvatar(Animator animator)
        {
            BipedRagdollReferences r = new BipedRagdollReferences();

            if (!animator.isHuman) return r;

            r.root = animator.transform;

            r.hips = animator.GetBoneTransform(HumanBodyBones.Hips);
            r.spine = animator.GetBoneTransform(HumanBodyBones.Spine);
            r.chest = animator.GetBoneTransform(HumanBodyBones.Chest);
            r.head = animator.GetBoneTransform(HumanBodyBones.Head);

            r.leftUpperArm = animator.GetBoneTransform(HumanBodyBones.LeftUpperArm);
            r.leftLowerArm = animator.GetBoneTransform(HumanBodyBones.LeftLowerArm);
            r.leftHand = animator.GetBoneTransform(HumanBodyBones.LeftHand);

            r.rightUpperArm = animator.GetBoneTransform(HumanBodyBones.RightUpperArm);
            r.rightLowerArm = animator.GetBoneTransform(HumanBodyBones.RightLowerArm);
            r.rightHand = animator.GetBoneTransform(HumanBodyBones.RightHand);

            r.leftUpperLeg = animator.GetBoneTransform(HumanBodyBones.LeftUpperLeg);
            r.leftLowerLeg = animator.GetBoneTransform(HumanBodyBones.LeftLowerLeg);
            r.leftFoot = animator.GetBoneTransform(HumanBodyBones.LeftFoot);

            r.rightUpperLeg = animator.GetBoneTransform(HumanBodyBones.RightUpperLeg);
            r.rightLowerLeg = animator.GetBoneTransform(HumanBodyBones.RightLowerLeg);
            r.rightFoot = animator.GetBoneTransform(HumanBodyBones.RightFoot);

            return r;
        }