public static bool AutoDetectReferences(ref BipedReferences references, Transform root, BipedReferences.AutoDetectParams autoDetectParams) { if (references == null) { references = new BipedReferences(); } references.root = root; Animator component = root.GetComponent <Animator>(); if (component != null && component.isHuman) { BipedReferences.AssignHumanoidReferences(ref references, component, autoDetectParams); return(true); } BipedReferences.DetectReferencesByNaming(ref references, root, autoDetectParams); Warning.logged = false; if (!references.isFilled) { Warning.Log("BipedReferences contains one or more missing Transforms.", root, true); return(false); } string message = ""; if (BipedReferences.SetupError(references, ref message)) { Warning.Log(message, references.root, true); return(false); } if (BipedReferences.SetupWarning(references, ref message)) { Warning.Log(message, references.root, true); } return(true); }
// Check if eyes are properly set up private static bool CheckEyesError(BipedReferences references, bool log) { if (references.eyes.Length == 0) { return(true); } for (int i = 0; i < references.eyes.Length; i++) { if (references.eyes[i] == null) { if (log) { Warning.Log("BipedReferences eye bone at index " + i + " is null.", references.root, true); } return(false); } } Transform duplicate = (Transform)Hierarchy.ContainsDuplicate(references.eyes); if (duplicate != null) { if (log) { Warning.Log(duplicate.name + " is represented multiple times in BipedReferences eyes.", references.eyes[0], true); } return(false); } return(true); }
/// <summary> /// Automatically detects biped bones. Returns true if a valid biped has been referenced. /// </summary> public static bool AutoDetectReferences(ref BipedReferences references, Transform root, AutoDetectParams autoDetectParams) { if (references == null) { references = new BipedReferences(); } references.root = root; // Try with naming and hierarchy first DetectReferencesByNaming(ref references, root, autoDetectParams); if (references.isValid && CheckSetupError(references, false) && CheckSetupWarning(references, false)) { return(true); } // If that failed try the Animator AssignHumanoidReferences(ref references, root.GetComponent <Animator>(), autoDetectParams); bool isValid = references.isValid; if (!isValid) { Warning.Log("BipedReferences contains one or more missing Transforms.", root, true); } return(isValid); }
private static bool CheckSpineError(BipedReferences references, bool log) { if (references.spine.Length == 0) { return(true); } for (int i = 0; i < references.spine.Length; i++) { if (references.spine[i] == null) { if (log) { Warning.Log("BipedReferences spine bone at index " + i + " is null.", references.root, true); } return(false); } } Transform transform = (Transform)Hierarchy.ContainsDuplicate(references.spine); if (transform != null) { if (log) { Warning.Log(transform.name + " is represented multiple times in BipedReferences spine.", references.spine[0], true); } return(false); } if (!Hierarchy.HierarchyIsValid(references.spine)) { if (log) { Warning.Log("BipedReferences spine hierarchy is invalid. Bone transforms in the spine do not belong to the same ancestry. Please make sure the bones are parented to each other.", references.spine[0], true); } return(false); } for (int j = 0; j < references.spine.Length; j++) { bool flag = false; if (j == 0 && references.spine[j].position == references.pelvis.position) { flag = true; } if (j != 0 && references.spine.Length > 1 && references.spine[j].position == references.spine[j - 1].position) { flag = true; } if (flag) { if (log) { Warning.Log("Biped's spine bone nr " + j + " position is the same as it's parent spine/pelvis bone's position. Please remove this bone from the spine.", references.spine[j], true); } return(false); } } return(true); }
public static bool CheckSetupError(BipedReferences references, bool log) { if (!references.isValid) { if (log) { Warning.Log("BipedReferences contains one or more missing Transforms.", references.root, true); } return(false); } return(BipedReferences.CheckLimbError(references.leftThigh, references.leftCalf, references.leftFoot, log) && BipedReferences.CheckLimbError(references.rightThigh, references.rightCalf, references.rightFoot, log) && BipedReferences.CheckLimbError(references.leftUpperArm, references.leftForearm, references.leftHand, log) && BipedReferences.CheckLimbError(references.rightUpperArm, references.rightForearm, references.rightHand, log) && BipedReferences.CheckSpineError(references, log) && BipedReferences.CheckEyesError(references, log)); }
// Check if the limb is properly set up private static bool CheckLimbError(Transform bone1, Transform bone2, Transform bone3, bool log) { if (bone2.position == bone1.position) { if (log) { Warning.Log("Second bone's position equals first bone's position in the biped's limb.", bone2, true); } return(false); } if (bone3.position == bone2.position) { if (log) { Warning.Log("Third bone's position equals second bone's position in the biped's limb.", bone3, true); } return(false); } Transform duplicate = (Transform)Hierarchy.ContainsDuplicate(new Transform[3] { bone1, bone2, bone3 }); if (duplicate != null) { if (log) { Warning.Log(duplicate.name + " is represented multiple times in the same BipedReferences limb.", bone1, true); } return(false); } if (!Hierarchy.HierarchyIsValid(new Transform[3] { bone1, bone2, bone3 })) { if (log) { Warning.Log( "BipedReferences limb hierarchy is invalid. Bone transforms in a limb do not belong to the same ancestry. Please make sure the bones are parented to each other. " + "Bones: " + bone1.name + ", " + bone2.name + ", " + bone3.name, bone1, true); } return(false); } return(true); }
private static bool CheckFacingAxisWarning(BipedReferences references, bool log) { Vector3 vector = references.rightHand.position - references.leftHand.position; Vector3 vector2 = references.rightFoot.position - references.leftFoot.position; float num = Vector3.Dot(vector.normalized, references.root.right); float num2 = Vector3.Dot(vector2.normalized, references.root.right); if (num < 0f || num2 < 0f) { if (log) { Warning.Log("Biped does not seem to be facing it's forward axis. Please make sure that in the initial pose the character is facing towards the positive Z axis of the Biped root gameobject.", references.root, true); } return(false); } return(true); }
/// <summary> /// Automatically detects biped bones. Returns true if a valid biped has been referenced. /// </summary> public static bool AutoDetectReferences(ref BipedReferences references, Transform root, AutoDetectParams autoDetectParams) { if (references == null) { references = new BipedReferences(); } references.root = root; // If that failed try the Animator var animator = root.GetComponent <Animator>(); if (animator != null && animator.isHuman) { AssignHumanoidReferences(ref references, animator, autoDetectParams); return(true); // Assume humanoids are always valid } // Try with naming and hierarchy first DetectReferencesByNaming(ref references, root, autoDetectParams); Warning.logged = false; if (!references.isFilled) { Warning.Log("BipedReferences contains one or more missing Transforms.", root, true); return(false); } string message = ""; if (SetupError(references, ref message)) { Warning.Log(message, references.root, true); return(false); } if (SetupWarning(references, ref message)) { Warning.Log(message, references.root, true); } return(true); }
// Check if spine is properly set up private static bool CheckSpineError(BipedReferences references, bool log) { if (references.spine.Length == 0) { return(true); } Transform duplicate = (Transform)Hierarchy.ContainsDuplicate(references.spine); if (duplicate != null) { if (log) { Warning.Log(duplicate.name + " is represented multiple times in BipedReferences spine.", references.spine[0], true); } return(false); } if (!Hierarchy.HierarchyIsValid(references.spine)) { if (log) { Warning.Log("BipedReferences spine hierarchy is invalid. Bone transforms in the spine do not belong to the same ancestry. Please make sure the bones are parented to each other.", references.spine[0], true); } return(false); } for (int i = 0; i < references.spine.Length; i++) { bool matchesParentPositon = i == 0? references.spine[i].position == references.pelvis.position: references.spine[i].position == references.spine[i - 1].position; if (matchesParentPositon) { if (log) { Warning.Log("Biped's spine bone nr " + i + " position is the same as it's parent spine/pelvis bone's position. Please remove this bone from the spine.", references.spine[i], true); } return(false); } } return(true); }
private static bool CheckRootHeightWarning(BipedReferences references, bool log) { if (references.head == null) { return(true); } float verticalOffset = BipedReferences.GetVerticalOffset(references.head.position, references.leftFoot.position, references.root.rotation); float verticalOffset2 = BipedReferences.GetVerticalOffset(references.root.position, references.leftFoot.position, references.root.rotation); if (verticalOffset2 / verticalOffset > 0.2f) { if (log) { Warning.Log("Biped's root Transform's position should be at ground level relative to the character (at the character's feet not at it's pelvis).", references.root, true); } return(false); } return(true); }
// Check if the limb is properly set up private static bool CheckLimbWarning(Transform bone1, Transform bone2, Transform bone3, bool log) { Vector3 cross = Vector3.Cross(bone2.position - bone1.position, bone3.position - bone1.position); if (cross == Vector3.zero) { if (log) { Warning.Log( "BipedReferences limb is completely stretched out in the initial pose. IK solver can not calculate the default bend plane for the limb. " + "Please make sure you character's limbs are at least slightly bent in the initial pose. " + "First bone: " + bone1.name + ", second bone: " + bone2.name + ".", bone1, true); } return(false); } return(true); }
// Check if eyes are properly set up private static bool CheckEyesError(BipedReferences references, bool log) { if (references.eyes.Length == 0) { return(true); } Transform duplicate = (Transform)Hierarchy.ContainsDuplicate(references.eyes); if (duplicate != null) { if (log) { Warning.Log(duplicate.name + " is represented multiple times in BipedReferences eyes.", references.eyes[0], true); } return(false); } return(true); }
// Check if the character is facing the correct axis private static bool CheckFacingAxisWarning(BipedReferences references, bool log) { Vector3 handsLeftToRight = references.rightHand.position - references.leftHand.position; Vector3 feetLeftToRight = references.rightFoot.position - references.leftFoot.position; float dotHands = Vector3.Dot(handsLeftToRight.normalized, references.root.right); float dotFeet = Vector3.Dot(feetLeftToRight.normalized, references.root.right); if (dotHands < 0 || dotFeet < 0) { if (log) { Warning.Log( "Biped does not seem to be facing it's forward axis. " + "Please make sure that in the initial pose the character is facing towards the positive Z axis of the Biped root gameobject.", references.root, true); } return(false); } return(true); }
public static bool AutoDetectReferences(ref BipedReferences references, Transform root, BipedReferences.AutoDetectParams autoDetectParams) { if (references == null) { references = new BipedReferences(); } references.root = root; BipedReferences.DetectReferencesByNaming(ref references, root, autoDetectParams); if (references.isValid && BipedReferences.CheckSetupError(references, false) && BipedReferences.CheckSetupWarning(references, false)) { return(true); } BipedReferences.AssignHumanoidReferences(ref references, root.GetComponent <Animator>(), autoDetectParams); bool isValid = references.isValid; if (!isValid) { Warning.Log("BipedReferences contains one or more missing Transforms.", root, true); } return(isValid); }
/* * Check if the limb is properly set up * */ private static bool CheckLimb(Transform bone1, Transform bone2, Transform bone3, bool log) { if (bone2.position == bone1.position) { if (log) { Warning.Log("Second bone's position equals first bone's position in the biped's limb.", bone2, true); } return(false); } if (bone3.position == bone2.position) { if (log) { Warning.Log("Third bone's position equals second bone's position in the biped's limb.", bone3, true); } return(false); } Transform duplicate = (Transform)Hierarchy.ContainsDuplicate(new Transform[3] { bone1, bone2, bone3 }); if (duplicate != null) { if (log) { Warning.Log(duplicate.name + " is represented multiple times in the same BipedReferences limb.", bone1, true); } return(false); } if (!Hierarchy.HierarchyIsValid(new Transform[3] { bone1, bone2, bone3 })) { if (log) { Warning.Log( "BipedReferences limb hierarchy is invalid. Bone transforms in a limb do not belong to the same ancestry. Please make sure the bones are parented to each other. " + "Bones: " + bone1.name + ", " + bone2.name + ", " + bone3.name, bone1, true); } return(false); } Vector3 cross = Vector3.Cross(bone2.position - bone1.position, bone3.position - bone1.position); if (cross == Vector3.zero) { if (log) { Warning.Log( "BipedReferences limb is completely stretched out in the initial pose. IK solver can not calculate the default bend plane for the limb. " + "Please make sure you character's limbs are at least slightly bent in the initial pose. " + "First bone: " + bone1.name + ", second bone: " + bone2.name + ".", bone1, true); } return(false); } return(true); }