protected override void OnEnableVirtual() { // 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; content[5].prop.isExpanded = false; content[content.Length - 3].prop.isExpanded = false; } }
private FullBodyBipedIK SetUpBiped(Human member) { var ik = member.gameObject.AddComponent <ObservableFullBodyBipedIK>(); ik.IKPass += delegate { if (member.neckLook != null) { member.neckLook.Calc(); } if (member.eyeLook != null) { member.eyeLook.Calc(); } }; var root = member.transform.FindChild("cm_body_01"); if (root == null) { root = member.transform.FindChild("cf_body_01"); } if (root == null) { Console.WriteLine("No entry point found: {0}", member.name); } ik.references = new BipedReferences(); ik.references.root = root; var spines = new List <Transform>(); foreach (var transform in root.GetComponentsInChildren <Transform>()) { string name = transform.name; if (name.Length > 5) { name = name.Substring(5); if (name == IK_MAPPING[HumanBodyBones.Head]) { ik.references.head = transform; } // LEGS else if (name == IK_MAPPING[HumanBodyBones.LeftUpperLeg]) { ik.references.leftThigh = transform; } else if (name == IK_MAPPING[HumanBodyBones.LeftLowerLeg]) { ik.references.leftCalf = transform; } else if (name == IK_MAPPING[HumanBodyBones.LeftFoot]) { ik.references.leftFoot = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightUpperLeg]) { ik.references.rightThigh = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightLowerLeg]) { ik.references.rightCalf = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightFoot]) { ik.references.rightFoot = transform; } // ARMS else if (name == IK_MAPPING[HumanBodyBones.LeftUpperArm]) { ik.references.leftUpperArm = transform; } else if (name == IK_MAPPING[HumanBodyBones.LeftLowerArm]) { ik.references.leftForearm = transform; } else if (name == IK_MAPPING[HumanBodyBones.LeftHand]) { ik.references.leftHand = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightUpperArm]) { ik.references.rightUpperArm = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightLowerArm]) { ik.references.rightForearm = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightHand]) { ik.references.rightHand = transform; } // HIPS else if (name == IK_MAPPING[HumanBodyBones.Hips]) { ik.references.pelvis = transform; } else if (name == "Spine01" || name == "Spine02" || name == "Spine03") { spines.Add(transform); } } } ik.references.spine = spines.ToArray(); //Console.WriteLine("1: " + ik.references.head); //Console.WriteLine("2: " + ik.references.leftCalf); //Console.WriteLine("3: " + ik.references.leftFoot); //Console.WriteLine("4: " + ik.references.leftForearm); //Console.WriteLine("5: " + ik.references.leftHand); //Console.WriteLine("6: " + ik.references.leftThigh); //Console.WriteLine("7: " + ik.references.leftUpperArm); //Console.WriteLine("8: " + ik.references.pelvis); //Console.WriteLine("9: " + ik.references.rightCalf); //Console.WriteLine("10: " + ik.references.rightFoot); //Console.WriteLine("11: " + ik.references.rightForearm); //Console.WriteLine("12: " + ik.references.rightHand); //Console.WriteLine("13: " + ik.references.rightThigh); //Console.WriteLine("14: " + ik.references.rightUpperArm); ik.solver.rootNode = IKSolverFullBodyBiped.DetectRootNodeBone(ik.references); ik.solver.SetToReferences(ik.references, ik.solver.rootNode); foreach (var effector in ik.solver.effectors) { effector.positionWeight = 0; effector.rotationWeight = 0; } return(ik); }
private FullBodyBipedIK SetUpBiped(Transform root) { var ik = root.gameObject.AddComponent <MyBipedIK>(); ik.references = new BipedReferences(); ik.references.root = root; var spines = new List <Transform>(); foreach (var transform in root.GetComponentsInChildren <Transform>()) { string name = transform.name; if (name.Length > 5) { name = name.Substring(5); if (name == IK_MAPPING[HumanBodyBones.Head]) { ik.references.head = transform; } // LEGS else if (name == IK_MAPPING[HumanBodyBones.LeftUpperLeg]) { ik.references.leftThigh = transform; } else if (name == IK_MAPPING[HumanBodyBones.LeftLowerLeg]) { ik.references.leftCalf = transform; } else if (name == IK_MAPPING[HumanBodyBones.LeftFoot]) { ik.references.leftFoot = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightUpperLeg]) { ik.references.rightThigh = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightLowerLeg]) { ik.references.rightCalf = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightFoot]) { ik.references.rightFoot = transform; } // ARMS else if (name == IK_MAPPING[HumanBodyBones.LeftUpperArm]) { ik.references.leftUpperArm = transform; } else if (name == IK_MAPPING[HumanBodyBones.LeftLowerArm]) { ik.references.leftForearm = transform; } else if (name == IK_MAPPING[HumanBodyBones.LeftHand]) { ik.references.leftHand = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightUpperArm]) { ik.references.rightUpperArm = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightLowerArm]) { ik.references.rightForearm = transform; } else if (name == IK_MAPPING[HumanBodyBones.RightHand]) { ik.references.rightHand = transform; } // HIPS else if (name == IK_MAPPING[HumanBodyBones.Hips]) { ik.references.pelvis = transform; } else if (name == "Spine01" || name == "Spine02" || name == "Spine03") { spines.Add(transform); } } } ik.references.spine = spines.ToArray(); Console.WriteLine("1: " + ik.references.head); Console.WriteLine("2: " + ik.references.leftCalf); Console.WriteLine("3: " + ik.references.leftFoot); Console.WriteLine("4: " + ik.references.leftForearm); Console.WriteLine("5: " + ik.references.leftHand); Console.WriteLine("6: " + ik.references.leftThigh); Console.WriteLine("7: " + ik.references.leftUpperArm); Console.WriteLine("8: " + ik.references.pelvis); Console.WriteLine("9: " + ik.references.rightCalf); Console.WriteLine("10: " + ik.references.rightFoot); Console.WriteLine("11: " + ik.references.rightForearm); Console.WriteLine("12: " + ik.references.rightHand); Console.WriteLine("13: " + ik.references.rightThigh); Console.WriteLine("14: " + ik.references.rightUpperArm); ik.solver.rootNode = IKSolverFullBodyBiped.DetectRootNodeBone(ik.references); ik.solver.SetToReferences(ik.references, ik.solver.rootNode); return(ik); }
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 }