Ejemplo n.º 1
0
 public static Transform GetBone(Transform[] transforms, BipedNaming.BoneType boneType, BipedNaming.BoneSide boneSide = BipedNaming.BoneSide.Center, params string[][] namings)
 {
     return(BipedNaming.GetNamingMatch(BipedNaming.GetBonesOfTypeAndSide(boneType, boneSide, transforms), namings));
 }
Ejemplo n.º 2
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.º 3
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];
             }
         }
     }
 }