Inheritance: RootMotion.FinalIK.IKMapping
 protected override void OnInitiate()
 {
     for (int i = 0; i < this.chain.Length; i++)
     {
         this.chain[i].Initiate(this, this.chain);
     }
     IKEffector[] array = this.effectors;
     for (int j = 0; j < array.Length; j++)
     {
         IKEffector iKEffector = array[j];
         iKEffector.Initiate(this);
     }
     this.spineMapping.Initiate(this);
     IKMappingBone[] array2 = this.boneMappings;
     for (int k = 0; k < array2.Length; k++)
     {
         IKMappingBone iKMappingBone = array2[k];
         iKMappingBone.Initiate(this);
     }
     IKMappingLimb[] array3 = this.limbMappings;
     for (int l = 0; l < array3.Length; l++)
     {
         IKMappingLimb iKMappingLimb = array3[l];
         iKMappingLimb.Initiate(this);
     }
 }
 public override void Execute()
 {
     Head = Solver.headMapping;
     LeftArmLimbMapping = Solver.leftArmMapping;
     RightArmLimbMapping = Solver.rightArmMapping;
     LeftLegLimbMapping = Solver.leftLegMapping;
     RightLegLimbMapping = Solver.rightLegMapping;
     SpineMapping = Solver.spineMapping;
 }
 public override bool IsValid(bool log)
 {
     if (this.chain == null)
     {
         if (log)
         {
             base.LogWarning("FBIK chain is null, can't initiate solver.");
         }
         return(false);
     }
     if (this.chain.Length == 0)
     {
         if (log)
         {
             base.LogWarning("FBIK chain length is 0, can't initiate solver.");
         }
         return(false);
     }
     for (int i = 0; i < this.chain.Length; i++)
     {
         if (log)
         {
             if (!this.chain[i].IsValid(new Warning.Logger(base.LogWarning)))
             {
                 return(false);
             }
         }
         else if (!this.chain[i].IsValid(null))
         {
             return(false);
         }
     }
     IKEffector[] array = this.effectors;
     for (int j = 0; j < array.Length; j++)
     {
         IKEffector iKEffector = array[j];
         if (!iKEffector.IsValid(this, new Warning.Logger(base.LogWarning)))
         {
             return(false);
         }
     }
     if (log)
     {
         if (!this.spineMapping.IsValid(this, new Warning.Logger(base.LogWarning)))
         {
             return(false);
         }
         IKMappingLimb[] array2 = this.limbMappings;
         for (int k = 0; k < array2.Length; k++)
         {
             IKMappingLimb iKMappingLimb = array2[k];
             if (!iKMappingLimb.IsValid(this, new Warning.Logger(base.LogWarning)))
             {
                 return(false);
             }
         }
         IKMappingBone[] array3 = this.boneMappings;
         for (int l = 0; l < array3.Length; l++)
         {
             IKMappingBone iKMappingBone = array3[l];
             if (!iKMappingBone.IsValid(this, new Warning.Logger(base.LogWarning)))
             {
                 return(false);
             }
         }
     }
     else
     {
         if (!this.spineMapping.IsValid(this, null))
         {
             return(false);
         }
         IKMappingLimb[] array4 = this.limbMappings;
         for (int m = 0; m < array4.Length; m++)
         {
             IKMappingLimb iKMappingLimb2 = array4[m];
             if (!iKMappingLimb2.IsValid(this, null))
             {
                 return(false);
             }
         }
         IKMappingBone[] array5 = this.boneMappings;
         for (int n = 0; n < array5.Length; n++)
         {
             IKMappingBone iKMappingBone2 = array5[n];
             if (!iKMappingBone2.IsValid(this, null))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Exemple #4
0
        /// <summary>
        /// Sets up the solver to BipedReferences and reinitiates (if in runtime).
        /// </summary>
        /// <param name="references">Biped references.</param>
        /// <param name="rootNode">Root node (optional). if null, will try to detect the root node bone automatically. </param>
        public void SetToReferences(BipedReferences references, Transform rootNode = null)
        {
            if (rootNode == null)
            {
                rootNode = DetectRootNodeBone(references);
            }
            this.rootNode = rootNode;

            // Root Node
            if (chain == null)
            {
                chain = new FBIKChain();
            }
            chain.pin = 0f;
            chain.SetNodes(rootNode);

            if (chain.children.Length != 4)
            {
                chain.children = new FBIKChain[4];
            }

            // Left Arm
            if (chain.children[0] == null)
            {
                chain.children[0]       = new FBIKChain();
                chain.children[0].reach = 0.05f;
            }
            chain.children[0].SetNodes(references.leftUpperArm, references.leftForearm, references.leftHand);

            // Right Arm
            if (chain.children[1] == null)
            {
                chain.children[1]       = new FBIKChain();
                chain.children[1].reach = 0.05f;
            }
            chain.children[1].SetNodes(references.rightUpperArm, references.rightForearm, references.rightHand);

            // Left Leg
            if (chain.children[2] == null)
            {
                chain.children[2]       = new FBIKChain();
                chain.children[2].reach = 0.05f;
            }
            chain.children[2].SetNodes(references.leftThigh, references.leftCalf, references.leftFoot);

            // Right Leg
            if (chain.children[3] == null)
            {
                chain.children[3]       = new FBIKChain();
                chain.children[3].reach = 0.05f;
            }
            chain.children[3].SetNodes(references.rightThigh, references.rightCalf, references.rightFoot);

            // Effectors
            if (effectors.Length != 9)
            {
                effectors = new IKEffector[9] {
                    new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector()
                }
            }
            ;

            effectors[0].bone       = rootNode;
            effectors[0].childBones = new Transform[2] {
                references.leftThigh, references.rightThigh
            };

            effectors[1].bone = references.leftUpperArm;
            effectors[2].bone = references.rightUpperArm;
            effectors[3].bone = references.leftThigh;
            effectors[4].bone = references.rightThigh;
            effectors[5].bone = references.leftHand;
            effectors[6].bone = references.rightHand;
            effectors[7].bone = references.leftFoot;
            effectors[8].bone = references.rightFoot;

            effectors[5].planeBone1 = references.leftUpperArm;
            effectors[5].planeBone2 = references.rightUpperArm;
            effectors[5].planeBone3 = rootNode;

            effectors[6].planeBone1 = references.rightUpperArm;
            effectors[6].planeBone2 = references.leftUpperArm;
            effectors[6].planeBone3 = rootNode;

            effectors[7].planeBone1 = references.leftThigh;
            effectors[7].planeBone2 = references.rightThigh;
            effectors[7].planeBone3 = rootNode;
            effectors[7].mode       = IKEffector.Mode.MaintainAnimatedPosition;

            effectors[8].planeBone1 = references.rightThigh;
            effectors[8].planeBone2 = references.leftThigh;
            effectors[8].planeBone3 = rootNode;
            effectors[8].mode       = IKEffector.Mode.MaintainAnimatedPosition;

            effectors[5].isEndEffector = true;
            effectors[6].isEndEffector = true;
            effectors[7].isEndEffector = true;
            effectors[8].isEndEffector = true;

            // Child Constraints
            chain.childConstraints = new FBIKChain.ChildConstraint[4] {
                new FBIKChain.ChildConstraint(references.leftUpperArm, references.rightThigh, 0f, 1f),
                new FBIKChain.ChildConstraint(references.rightUpperArm, references.leftThigh, 0f, 1f),
                new FBIKChain.ChildConstraint(references.leftUpperArm, references.rightUpperArm),
                new FBIKChain.ChildConstraint(references.leftThigh, references.rightThigh)
            };

            // Bend Constraints
            if (bendConstraints.Length != 4)
            {
                bendConstraints = new IKConstraintBend[4] {
                    new IKConstraintBend(), new IKConstraintBend(), new IKConstraintBend(), new IKConstraintBend()
                };
            }

            bendConstraints[0].SetBones(references.leftUpperArm, references.leftForearm, references.leftHand);
            bendConstraints[1].SetBones(references.rightUpperArm, references.rightForearm, references.rightHand);
            bendConstraints[2].SetBones(references.leftThigh, references.leftCalf, references.leftFoot);
            bendConstraints[3].SetBones(references.rightThigh, references.rightCalf, references.rightFoot);

            // IKMappingSpine
            Transform[] spineBones = new Transform[references.spine.Length + 1];
            spineBones[0] = references.pelvis;
            for (int i = 0; i < references.spine.Length; i++)
            {
                spineBones[i + 1] = references.spine[i];
            }

            if (spineMapping == null)
            {
                spineMapping            = new IKMappingSpine();
                spineMapping.iterations = 3;
            }
            spineMapping.SetBones(spineBones, references.leftUpperArm, references.rightUpperArm, references.leftThigh, references.rightThigh);

            // IKMappingBone
            int boneMappingsCount = references.head != null? 1: 0;

            if (boneMappings.Length != boneMappingsCount)
            {
                boneMappings = new IKMappingBone[boneMappingsCount];
                for (int i = 0; i < boneMappings.Length; i++)
                {
                    boneMappings[i] = new IKMappingBone();
                }
                if (boneMappingsCount == 1)
                {
                    boneMappings[0].maintainRotationWeight = 0f;
                }
            }

            if (boneMappings.Length > 0)
            {
                boneMappings[0].bone = references.head;
            }

            // IKMappingLimb
            if (limbMappings.Length != 4)
            {
                limbMappings = new IKMappingLimb[4] {
                    new IKMappingLimb(), new IKMappingLimb(), new IKMappingLimb(), new IKMappingLimb()
                };

                limbMappings[2].maintainRotationWeight = 1f;
                limbMappings[3].maintainRotationWeight = 1f;
            }

            limbMappings[0].SetBones(references.leftUpperArm, references.leftForearm, references.leftHand, GetLeftClavicle(references));
            limbMappings[1].SetBones(references.rightUpperArm, references.rightForearm, references.rightHand, GetRightClavicle(references));
            limbMappings[2].SetBones(references.leftThigh, references.leftCalf, references.leftFoot);
            limbMappings[3].SetBones(references.rightThigh, references.rightCalf, references.rightFoot);

            if (Application.isPlaying)
            {
                Initiate(references.root);
            }
        }
		/// <summary>
		/// Sets up the solver to BipedReferences and reinitiates (if in runtime).
		/// </summary>
		/// <param name="references">Biped references.</param>
		/// <param name="rootNode">Root node (optional). if null, will try to detect the root node bone automatically. </param>
		public void SetToReferences(BipedReferences references, Transform rootNode = null) {
			root = references.root;

			if (rootNode == null) rootNode = DetectRootNodeBone(references);
			this.rootNode = rootNode;
			
			// Root Node
			if (chain == null || chain.Length != 5) chain = new FBIKChain[5];
			for (int i = 0; i < chain.Length; i++) {
				if (chain[i] == null) {
					chain[i] = new FBIKChain();
				}
			}

			chain[0].pin = 0f;
			chain[0].SetNodes(rootNode);
			chain[0].children = new int[4] { 1, 2, 3, 4 };
			
			// Left Arm
			chain[1].SetNodes(references.leftUpperArm, references.leftForearm, references.leftHand);
			
			// Right Arm
			chain[2].SetNodes(references.rightUpperArm, references.rightForearm, references.rightHand);
			
			// Left Leg
			chain[3].SetNodes(references.leftThigh, references.leftCalf, references.leftFoot);
			
			// Right Leg
			chain[4].SetNodes(references.rightThigh, references.rightCalf, references.rightFoot);
			
			// Effectors
			if (effectors.Length != 9) effectors = new IKEffector[9] {
				new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector(), new IKEffector()
			};
			
			effectors[0].bone = rootNode;
			effectors[0].childBones = new Transform[2] { references.leftThigh, references.rightThigh };
			
			effectors[1].bone = references.leftUpperArm;
			effectors[2].bone = references.rightUpperArm;
			effectors[3].bone = references.leftThigh;
			effectors[4].bone = references.rightThigh;
			effectors[5].bone = references.leftHand;
			effectors[6].bone = references.rightHand;
			effectors[7].bone = references.leftFoot;
			effectors[8].bone = references.rightFoot;
			
			effectors[5].planeBone1 = references.leftUpperArm;
			effectors[5].planeBone2 = references.rightUpperArm;
			effectors[5].planeBone3 = rootNode;

			effectors[6].planeBone1 = references.rightUpperArm;
			effectors[6].planeBone2 = references.leftUpperArm;
			effectors[6].planeBone3 = rootNode;

			effectors[7].planeBone1 = references.leftThigh;
			effectors[7].planeBone2 = references.rightThigh;
			effectors[7].planeBone3 = rootNode;

			effectors[8].planeBone1 = references.rightThigh;
			effectors[8].planeBone2 = references.leftThigh;
			effectors[8].planeBone3 = rootNode;
			
			// Child Constraints
			chain[0].childConstraints = new FBIKChain.ChildConstraint[4] {
				new FBIKChain.ChildConstraint(references.leftUpperArm, references.rightThigh, 0f, 1f),
				new FBIKChain.ChildConstraint(references.rightUpperArm, references.leftThigh, 0f, 1f),
				new FBIKChain.ChildConstraint(references.leftUpperArm, references.rightUpperArm),
				new FBIKChain.ChildConstraint(references.leftThigh, references.rightThigh)
				
			};
			
			// IKMappingSpine
			Transform[] spineBones = new Transform[references.spine.Length + 1];
			spineBones[0] = references.pelvis;
			for (int i = 0; i < references.spine.Length; i++) {
				spineBones[i + 1] = references.spine[i];
			}
			
			if (spineMapping == null) {
				spineMapping = new IKMappingSpine();
				spineMapping.iterations = 3;
			}
			spineMapping.SetBones(spineBones, references.leftUpperArm, references.rightUpperArm, references.leftThigh, references.rightThigh);
			
			// IKMappingBone
			int boneMappingsCount = references.head != null? 1: 0;
			
			if (boneMappings.Length != boneMappingsCount) {
				boneMappings = new IKMappingBone[boneMappingsCount];
				for (int i = 0; i < boneMappings.Length; i++) {
					boneMappings[i] = new IKMappingBone();
				}
				if (boneMappingsCount == 1) boneMappings[0].maintainRotationWeight = 0f;
			}
			
			if (boneMappings.Length > 0) boneMappings[0].bone = references.head;
			
			// IKMappingLimb
			if (limbMappings.Length != 4) {
				limbMappings = new IKMappingLimb[4] {
					new IKMappingLimb(), new IKMappingLimb(), new IKMappingLimb(), new IKMappingLimb()
				};
				
				limbMappings[2].maintainRotationWeight = 1f;
				limbMappings[3].maintainRotationWeight = 1f;
			}
			
			limbMappings[0].SetBones(references.leftUpperArm, references.leftForearm, references.leftHand, GetLeftClavicle(references));
			limbMappings[1].SetBones(references.rightUpperArm, references.rightForearm, references.rightHand, GetRightClavicle(references));
			limbMappings[2].SetBones(references.leftThigh, references.leftCalf, references.leftFoot);
			limbMappings[3].SetBones(references.rightThigh, references.rightCalf, references.rightFoot);

			if (Application.isPlaying) Initiate(references.root);
		}