private void AutoDetectReferences()
 {
     this.references = new BipedReferences();
     BipedReferences.AutoDetectReferences(ref this.references, base.transform, new BipedReferences.AutoDetectParams(true, false));
     this.solver.rootNode = IKSolverFullBodyBiped.DetectRootNodeBone(this.references);
     this.solver.SetToReferences(this.references, this.solver.rootNode);
 }
Example #2
0
        public void OnEnable()
        {
            if (serializedObject == null)
            {
                return;
            }

            // Store the MonoScript for changing script execution order
            if (!Application.isPlaying)
            {
                MonoScript monoScript = MonoScript.FromMonoBehaviour(script);

                // Changing the script execution order to make sure BipedIK always executes after any other script except FullBodyBipedIK
                int executionOrder = MonoImporter.GetExecutionOrder(monoScript);
                if (executionOrder != 9998)
                {
                    MonoImporter.SetExecutionOrder(monoScript, 9998);
                }
            }

            references    = serializedObject.FindProperty("references");
            solvers       = serializedObject.FindProperty("solvers");
            solversProps  = BipedIKSolversInspector.FindProperties(solvers);
            fixTransforms = new SerializedContent(serializedObject.FindProperty("fixTransforms"), new GUIContent("Fix Transforms", "If true, will fix all the Transforms used by the solver to their initial state in each Update. This prevents potential problems with unanimated bones and animator culling with a small cost of performance."));

            // Automatically detecting references
            if (!Application.isPlaying)
            {
                if (script.references.isEmpty)
                {
                    BipedReferences.AutoDetectReferences(ref script.references, script.transform, new BipedReferences.AutoDetectParams(false, true));

                    references.isExpanded = true;
                    solvers.isExpanded    = false;
                    for (int i = 0; i < solversProps.Length; i++)
                    {
                        solversProps[i].isExpanded = false;
                    }

                    // Setting default values and initiating
                    script.InitiateBipedIK();
                    script.SetToDefaults();
                    EditorUtility.SetDirty(script);
                }
                else
                {
                    script.InitiateBipedIK();
                }

                Warning.logged = false;

                if (Application.isPlaying)
                {
                    if (ReferencesValid(true))
                    {
                        BipedReferences.CheckSetupWarning(script.references, true);
                    }
                }
            }
        }
Example #3
0
        public void OnCharacterCompleted(UMAData umaData)
        {
            // Add the BipedIK component
            BipedIK bipedIK = umaData.gameObject.AddComponent <BipedIK>() as BipedIK;

            // Setup the bone references
            if (umaData.animator != null)
            {
                BipedReferences.AssignHumanoidReferences(ref bipedIK.references, umaData.animator, BipedReferences.AutoDetectParams.Default);
            }
            else
            {
                BipedReferences.AutoDetectReferences(ref bipedIK.references, umaData.umaRoot.transform, BipedReferences.AutoDetectParams.Default);
            }

            // Zero out the initial IK weights
            bipedIK.SetLookAtWeight(0f, 0f, 0f, 0f, 0f, 0f, 0f);
            bipedIK.SetSpineWeight(0f);

            bipedIK.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 0f);
            bipedIK.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 0f);
            bipedIK.SetIKPositionWeight(AvatarIKGoal.RightFoot, 0f);
            bipedIK.SetIKRotationWeight(AvatarIKGoal.RightFoot, 0f);

            bipedIK.SetIKPositionWeight(AvatarIKGoal.LeftHand, 0f);
            bipedIK.SetIKRotationWeight(AvatarIKGoal.LeftHand, 0f);
            bipedIK.GetGoalIK(AvatarIKGoal.LeftHand).bendModifier = IKSolverLimb.BendModifier.Arm;

            bipedIK.SetIKPositionWeight(AvatarIKGoal.RightHand, 0f);
            bipedIK.SetIKRotationWeight(AvatarIKGoal.RightHand, 0f);
            bipedIK.GetGoalIK(AvatarIKGoal.RightHand).bendModifier = IKSolverLimb.BendModifier.Arm;
        }
        void AutoDetectReferences()
        {
            references = new BipedReferences();
            BipedReferences.AutoDetectReferences(ref references, transform, new BipedReferences.AutoDetectParams(true, false));

            solver.rootNode = IKSolverFullBodyBiped.DetectRootNodeBone(references);

            solver.SetToReferences(references, solver.rootNode);
        }
        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>());
                }
            }
        }
        protected override void OnEnableVirtual()
        {
            references = serializedObject.FindProperty("references");

            // Autodetecting References
            if (script.references.IsEmpty(false) && script.enabled)
            {
                BipedReferences.AutoDetectReferences(ref script.references, script.transform, new BipedReferences.AutoDetectParams(true, false));

                script.solver.rootNode = IKSolverFullBodyBiped.DetectRootNodeBone(script.references);

                Initiate();

                if (Application.isPlaying)
                {
                    Warning.Log("Biped references were auto-detected on a FullBodyBipedIK component that was added in runtime. Note that this only happens in the Editor and if the GameObject is selected (for quick and convenient debugging). If you want to add FullBodyBipedIK dynamically in runtime via script, you will have to use BipedReferences.AutodetectReferences() for automatic biped detection.", script.transform);
                }

                references.isExpanded = !script.references.isValid;
            }
        }
Example #7
0
        public void OnEnable()
        {
            // Store the MonoScript for changing script execution order
            if (!Application.isPlaying)
            {
                MonoScript monoScript = MonoScript.FromMonoBehaviour(script);

                // Changing the script execution order to make sure BipedIK always executes after any other script except FullBodyBipedIK
                int executionOrder = MonoImporter.GetExecutionOrder(monoScript);
                if (executionOrder != 9998)
                {
                    MonoImporter.SetExecutionOrder(monoScript, 9998);
                }
            }

            references   = serializedObject.FindProperty("references");
            solvers      = serializedObject.FindProperty("solvers");
            solversProps = BipedIKSolversInspector.FindProperties(solvers);

            // Caching Solver Contents
            leftFootContent  = IKSolverLimbInspector.FindContent(solversProps[0]);
            rightFootContent = IKSolverLimbInspector.FindContent(solversProps[1]);
            leftHandContent  = IKSolverLimbInspector.FindContent(solversProps[2]);
            rightHandContent = IKSolverLimbInspector.FindContent(solversProps[3]);
            spineContent     = IKSolverHeuristicInspector.FindContent(solversProps[4]);
            aimContent       = IKSolverAimInspector.FindContent(solversProps[5]);
            lookAtContent    = IKSolverLookAtInspector.FindContent(solversProps[6]);
            pelvisContent    = ConstraintsInspector.FindContent(solversProps[7]);

            solverContents = new SerializedContent[8][] {
                leftFootContent, rightFootContent, leftHandContent, rightHandContent, spineContent, aimContent, lookAtContent, pelvisContent
            };

            // Automatically detecting references
            if (!Application.isPlaying)
            {
                if (script.references.isEmpty)
                {
                    BipedReferences.AutoDetectReferences(ref script.references, script.transform, new BipedReferences.AutoDetectParams(false, true));

                    references.isExpanded = true;
                    solvers.isExpanded    = false;
                    for (int i = 0; i < solversProps.Length; i++)
                    {
                        solversProps[i].isExpanded = false;
                    }

                    // Setting default values and initiating
                    script.InitiateBipedIK();
                    script.SetToDefaults();
                    EditorUtility.SetDirty(script);
                }
                else
                {
                    script.InitiateBipedIK();
                }

                Warning.logged = false;
                BipedReferences.CheckSetup(script.references);
            }
        }
Example #8
0
        void SetupIK(GameObject modelGO, GameObject leftHandGoal, GameObject rightHandGoal, HandPoser leftHandPoser, HandPoser rightHandPoser)
        {
            var ikController = modelGO.AddComponent <HandIKController>();

            ikController.useFinalIK     = useFinalIK;
            ikController.leftHandGoal   = leftHandGoal.transform;
            ikController.rightHandGoal  = rightHandGoal.transform;
            ikController.leftHandPoser  = leftHandPoser;
            ikController.rightHandPoser = rightHandPoser;

            if (!useFinalIK)
            {
                return;
            }


#if FINALIK
            var fbbik = modelGO.AddComponent <FullBodyBipedIK>();

            // force auto detect of fbbik
            if (fbbik.references.isEmpty)
            {
                BipedReferences.AutoDetectReferences(ref fbbik.references, fbbik.transform, new BipedReferences.AutoDetectParams(true, false));
                fbbik.solver.rootNode = IKSolverFullBodyBiped.DetectRootNodeBone(fbbik.references);
                fbbik.solver.SetToReferences(fbbik.references, fbbik.solver.rootNode);
            }


            Transform goalContainer = new GameObject("ik_goals").transform;
            goalContainer.parent = _containerObject;

            leftHandGoal.transform.parent  = goalContainer;
            rightHandGoal.transform.parent = goalContainer;

            // Add pole targets for the knees
            Transform elbowGoalLeft  = CopyTransform(_animator.GetBoneTransform(HumanBodyBones.LeftLowerArm), "elbow_L").transform;
            Transform elbowGoalRight = CopyTransform(_animator.GetBoneTransform(HumanBodyBones.RightLowerArm), "elbow_R").transform;

            elbowGoalLeft.SetParent(goalContainer);
            elbowGoalRight.SetParent(goalContainer);

            // offset the goals in the bend direction of the arm
            elbowGoalLeft.position  = elbowGoalLeft.position + GetChainBendDirection(HumanBodyBones.LeftUpperArm, HumanBodyBones.LeftLowerArm, HumanBodyBones.LeftHand);
            elbowGoalRight.position = elbowGoalRight.position + GetChainBendDirection(HumanBodyBones.RightUpperArm, HumanBodyBones.RightLowerArm, HumanBodyBones.RightHand);



            fbbik.solver.leftArmChain.bendConstraint.bendGoal  = elbowGoalLeft;
            fbbik.solver.leftArmChain.bendConstraint.weight    = 0.5f;
            fbbik.solver.rightArmChain.bendConstraint.bendGoal = elbowGoalRight;
            fbbik.solver.rightArmChain.bendConstraint.weight   = 0.5f;


            Transform kneeGoalLeft  = CopyTransform(_animator.GetBoneTransform(HumanBodyBones.LeftLowerLeg), "knee_L").transform;
            Transform kneeGoalRight = CopyTransform(_animator.GetBoneTransform(HumanBodyBones.RightLowerLeg), "knee_R").transform;

            kneeGoalLeft.SetParent(goalContainer);
            kneeGoalRight.SetParent(goalContainer);

            // offset the goals in the bend direction of the arm
            kneeGoalLeft.position  = kneeGoalLeft.position + GetChainBendDirection(HumanBodyBones.LeftUpperLeg, HumanBodyBones.LeftLowerLeg, HumanBodyBones.LeftFoot);
            kneeGoalRight.position = kneeGoalRight.position + GetChainBendDirection(HumanBodyBones.RightUpperLeg, HumanBodyBones.RightLowerLeg, HumanBodyBones.RightFoot);

            fbbik.solver.leftLegChain.bendConstraint.bendGoal  = kneeGoalLeft;
            fbbik.solver.leftLegChain.bendConstraint.weight    = 0.5f;
            fbbik.solver.rightLegChain.bendConstraint.bendGoal = kneeGoalRight;
            fbbik.solver.rightLegChain.bendConstraint.weight   = 0.5f;

            //
            Transform footGoalLeft  = CopyTransform(_animator.GetBoneTransform(HumanBodyBones.LeftFoot), "foot_L");
            Transform footGoalRight = CopyTransform(_animator.GetBoneTransform(HumanBodyBones.RightFoot), "foot_R");

            footGoalLeft.SetParent(goalContainer);
            footGoalRight.SetParent(goalContainer);

            // We set the target in the FBBIK but we leave the weights at zero for the feet.
            fbbik.solver.SetEffectorWeights(FullBodyBipedEffector.LeftFoot, 0.0f, 0.0f);
            fbbik.solver.leftFootEffector.target = footGoalLeft;
            fbbik.solver.SetEffectorWeights(FullBodyBipedEffector.RightFoot, 0.0f, 0.0f);
            fbbik.solver.rightFootEffector.target = footGoalRight;

            // Setup head effector
            Transform headEffector = CopyTransform(_animator.GetBoneTransform(HumanBodyBones.Head).transform, "headEffector");
            headEffector.gameObject.AddComponent <FBBIKHeadEffector>().ik = fbbik;

            headEffector.SetParent(goalContainer);

            // Setup hand goals
            fbbik.solver.leftHandEffector.target         = leftHandGoal.transform;
            fbbik.solver.leftHandEffector.positionWeight = 1.0f;
            fbbik.solver.leftHandEffector.rotationWeight = 1.0f;

            fbbik.solver.rightHandEffector.target         = rightHandGoal.transform;
            fbbik.solver.rightHandEffector.positionWeight = 1.0f;
            fbbik.solver.rightHandEffector.rotationWeight = 1.0f;
#endif
        }