Ejemplo n.º 1
0
        // Determines whether a bone is valid for being added into the spine
        private static bool AddBoneToSpine(Transform bone, ref BipedReferences references, AutoDetectParams autoDetectParams)
        {
            if (bone == references.root)
            {
                return(false);
            }

            bool isLegsParent = bone == references.leftThigh.parent;

            if (isLegsParent && !autoDetectParams.legsParentInSpine)
            {
                return(false);
            }

            if (references.pelvis != null)
            {
                if (bone == references.pelvis)
                {
                    return(false);
                }
                if (Hierarchy.IsAncestor(references.pelvis, bone))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
 public static bool HierarchyIsValid(Transform[] bones)
 {
     for (int i = 1; i < bones.Length; i++)
     {
         if (!Hierarchy.IsAncestor(bones[i], bones[i - 1]))
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 3
0
 // Determines whether a Transform is above the arms
 private static bool IsNeckBone(Transform bone, Transform leftUpperArm)
 {
     if (leftUpperArm.parent != null && leftUpperArm.parent == bone)
     {
         return(false);
     }
     if (Hierarchy.IsAncestor(leftUpperArm, bone))
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 4
0
        // Determines whether a bone is valid for being added into the eyes array
        private static bool AddBoneToEyes(Transform bone, ref BipedReferences references, AutoDetectParams autoDetectParams)
        {
            if (references.head != null)
            {
                if (!Hierarchy.IsAncestor(bone, references.head))
                {
                    return(false);
                }
            }

            if (bone.GetComponent <SkinnedMeshRenderer>() != null)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 5
0
 public static bool IsCommonAncestor(Transform transform, Transform[] transforms)
 {
     if (transform == null)
     {
         Debug.LogWarning("Transform is null.");
         return(false);
     }
     for (int i = 0; i < transforms.Length; i++)
     {
         if (transforms[i] == null)
         {
             Debug.Log("Transforms[" + i + "] is null.");
             return(false);
         }
         if (!Hierarchy.IsAncestor(transforms[i], transform) && transforms[i] != transform)
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 6
0
 public static Transform GetFirstCommonAncestor(Transform t1, Transform t2)
 {
     if (t1 == null)
     {
         return(null);
     }
     if (t2 == null)
     {
         return(null);
     }
     if (t1.parent == null)
     {
         return(null);
     }
     if (t2.parent == null)
     {
         return(null);
     }
     if (Hierarchy.IsAncestor(t2, t1.parent))
     {
         return(t1.parent);
     }
     return(Hierarchy.GetFirstCommonAncestor(t1.parent, t2));
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Detects the references based on naming and hierarchy.
        /// </summary>
        public static void DetectReferencesByNaming(ref BipedReferences references, Transform root, AutoDetectParams autoDetectParams)
        {
            if (references == null)
            {
                references = new BipedReferences();
            }

            Transform[] children = root.GetComponentsInChildren <Transform>();

            // Find limbs
            DetectLimb(BipedNaming.BoneType.Arm, BipedNaming.BoneSide.Left, ref references.leftUpperArm, ref references.leftForearm, ref references.leftHand, children);
            DetectLimb(BipedNaming.BoneType.Arm, BipedNaming.BoneSide.Right, ref references.rightUpperArm, ref references.rightForearm, ref references.rightHand, children);
            DetectLimb(BipedNaming.BoneType.Leg, BipedNaming.BoneSide.Left, ref references.leftThigh, ref references.leftCalf, ref references.leftFoot, children);
            DetectLimb(BipedNaming.BoneType.Leg, BipedNaming.BoneSide.Right, ref references.rightThigh, ref references.rightCalf, ref references.rightFoot, children);

            // Find head bone
            references.head = BipedNaming.GetBone(children, BipedNaming.BoneType.Head);

            // Find Pelvis
            references.pelvis = BipedNaming.GetNamingMatch(children, BipedNaming.pelvis);

            // If pelvis is not an ancestor of a leg, it is not a valid pelvis
            if (references.pelvis == null || !Hierarchy.IsAncestor(references.leftThigh, references.pelvis))
            {
                if (references.leftThigh != null)
                {
                    references.pelvis = references.leftThigh.parent;
                }
            }

            // Find spine and head bones
            if (references.leftUpperArm != null && references.rightUpperArm != null && references.pelvis != null && references.leftThigh != null)
            {
                Transform neck = Hierarchy.GetFirstCommonAncestor(references.leftUpperArm, references.rightUpperArm);

                if (neck != null)
                {
                    Transform[] inverseSpine = new Transform[1] {
                        neck
                    };
                    Hierarchy.AddAncestors(inverseSpine[0], references.pelvis, ref inverseSpine);

                    references.spine = new Transform[0];
                    for (int i = inverseSpine.Length - 1; i > -1; i--)
                    {
                        if (AddBoneToSpine(inverseSpine[i], ref references, autoDetectParams))
                        {
                            Array.Resize(ref references.spine, references.spine.Length + 1);
                            references.spine[references.spine.Length - 1] = inverseSpine[i];
                        }
                    }

                    // Head
                    if (references.head == null)
                    {
                        for (int i = 0; i < neck.childCount; i++)
                        {
                            Transform child = neck.GetChild(i);

                            if (!Hierarchy.ContainsChild(child, references.leftUpperArm) && !Hierarchy.ContainsChild(child, references.rightUpperArm))
                            {
                                references.head = child;
                                break;
                            }
                        }
                    }
                }
            }

            // Find eye bones
            Transform[] eyes = BipedNaming.GetBonesOfType(BipedNaming.BoneType.Eye, children);
            references.eyes = new Transform[0];

            if (autoDetectParams.includeEyes)
            {
                for (int i = 0; i < eyes.Length; i++)
                {
                    if (AddBoneToEyes(eyes[i], ref references, autoDetectParams))
                    {
                        Array.Resize(ref references.eyes, references.eyes.Length + 1);
                        references.eyes[references.eyes.Length - 1] = eyes[i];
                    }
                }
            }
        }
Ejemplo n.º 8
0
 private static bool AddBoneToEyes(Transform bone, ref BipedReferences references, BipedReferences.AutoDetectParams autoDetectParams)
 {
     return((!(references.head != null) || Hierarchy.IsAncestor(bone, references.head)) && !(bone.GetComponent <SkinnedMeshRenderer>() != null));
 }
Ejemplo n.º 9
0
 private static bool IsNeckBone(Transform bone, Transform leftUpperArm)
 {
     return((!(leftUpperArm.parent != null) || !(leftUpperArm.parent == bone)) && !Hierarchy.IsAncestor(leftUpperArm, bone));
 }
Ejemplo n.º 10
0
 public static void DetectReferencesByNaming(ref BipedReferences references, Transform root, BipedReferences.AutoDetectParams autoDetectParams)
 {
     if (references == null)
     {
         references = new BipedReferences();
     }
     Transform[] componentsInChildren = root.GetComponentsInChildren <Transform>();
     BipedReferences.DetectLimb(BipedNaming.BoneType.Arm, BipedNaming.BoneSide.Left, ref references.leftUpperArm, ref references.leftForearm, ref references.leftHand, componentsInChildren);
     BipedReferences.DetectLimb(BipedNaming.BoneType.Arm, BipedNaming.BoneSide.Right, ref references.rightUpperArm, ref references.rightForearm, ref references.rightHand, componentsInChildren);
     BipedReferences.DetectLimb(BipedNaming.BoneType.Leg, BipedNaming.BoneSide.Left, ref references.leftThigh, ref references.leftCalf, ref references.leftFoot, componentsInChildren);
     BipedReferences.DetectLimb(BipedNaming.BoneType.Leg, BipedNaming.BoneSide.Right, ref references.rightThigh, ref references.rightCalf, ref references.rightFoot, componentsInChildren);
     references.head   = BipedNaming.GetBone(componentsInChildren, BipedNaming.BoneType.Head, BipedNaming.BoneSide.Center, Array.Empty <string[]>());
     references.pelvis = BipedNaming.GetNamingMatch(componentsInChildren, new string[][]
     {
         BipedNaming.pelvis
     });
     if ((references.pelvis == null || !Hierarchy.IsAncestor(references.leftThigh, references.pelvis)) && references.leftThigh != null)
     {
         references.pelvis = references.leftThigh.parent;
     }
     if (references.leftUpperArm != null && references.rightUpperArm != null && references.pelvis != null && references.leftThigh != null)
     {
         Transform firstCommonAncestor = Hierarchy.GetFirstCommonAncestor(references.leftUpperArm, references.rightUpperArm);
         if (firstCommonAncestor != null)
         {
             Transform[] array = new Transform[]
             {
                 firstCommonAncestor
             };
             Hierarchy.AddAncestors(array[0], references.pelvis, ref array);
             references.spine = new Transform[0];
             for (int i = array.Length - 1; i > -1; i--)
             {
                 if (BipedReferences.AddBoneToSpine(array[i], ref references, autoDetectParams))
                 {
                     Array.Resize <Transform>(ref references.spine, references.spine.Length + 1);
                     references.spine[references.spine.Length - 1] = array[i];
                 }
             }
             if (references.head == null)
             {
                 for (int j = 0; j < firstCommonAncestor.childCount; j++)
                 {
                     Transform child = firstCommonAncestor.GetChild(j);
                     if (!Hierarchy.ContainsChild(child, references.leftUpperArm) && !Hierarchy.ContainsChild(child, references.rightUpperArm))
                     {
                         references.head = child;
                         break;
                     }
                 }
             }
         }
     }
     Transform[] bonesOfType = BipedNaming.GetBonesOfType(BipedNaming.BoneType.Eye, componentsInChildren);
     references.eyes = new Transform[0];
     if (autoDetectParams.includeEyes)
     {
         for (int k = 0; k < bonesOfType.Length; k++)
         {
             if (BipedReferences.AddBoneToEyes(bonesOfType[k], ref references, autoDetectParams))
             {
                 Array.Resize <Transform>(ref references.eyes, references.eyes.Length + 1);
                 references.eyes[references.eyes.Length - 1] = bonesOfType[k];
             }
         }
     }
 }
Ejemplo n.º 11
0
 public static bool IsAncestor(Transform transform, Transform ancestor)
 {
     return(transform == null || ancestor == null || (!(transform.parent == null) && (transform.parent == ancestor || Hierarchy.IsAncestor(transform.parent, ancestor))));
 }