Example #1
0
        void OnSceneGUI()
        {
            // TODO: add undo/redo functionality
            PolygonSkeleton skeleton = target as PolygonSkeleton;

            if (skeleton == null || !skeleton.boneReferences.IsValid)
            {
                return;
            }

            SkeletonBoneReferences bones = skeleton.boneReferences;

            if (skeleton.newSkeleton.IsValid)
            {
                bones = skeleton.newSkeleton;
            }

            if (skeleton.boneReferences.IsValid)
            {
                DrawHumanoidSkeletonBones(bones);
            }

            if (Selection.activeGameObject != skeleton.gameObject)
            {
                selectedBone = null;
            }
            if (selectedBone != null)
            {
                DrawRotationGizmo(selectedBone);
            }
        }
        // TODO: place this in another script, maybe the SkeletonBoneReferences or a new one
        #region TEMP (put in another file)

        private SkeletonBoneReferences CopySkeleton(SkeletonBoneReferences skeleton, Transform parent)
        {
            if (!skeleton.IsValid)
            {
                return(null);
            }

            SkeletonBoneReferences skeletonCopy = new SkeletonBoneReferences();

            Vector3 bodyForward =
                (Vector3.Cross(skeleton.legRight.foot.bone.position - skeleton.legLeft.foot.bone.position, Vector3.up).normalized +
                 Vector3.Cross(skeleton.armRight.shoulder.bone.position - skeleton.armLeft.shoulder.bone.position, Vector3.up).normalized) / 2f;


            Debug.DrawRay(transform.position, bodyForward, Color.red, 10);

            skeletonCopy.main = new Bone(CreateDirectionBone("main", (skeleton.legLeft.upperLeg.bone.position + skeleton.legRight.upperLeg.bone.position) / 2f, bodyForward, Vector3.up, parent));

            skeletonCopy.head.AssignBones(
                CreateDirectionBone("neck", skeleton.head.neck.bone.position, skeleton.head.head.bone.position - skeleton.head.neck.bone.position, -bodyForward, parent),
                CreateDirectionBone("head", skeleton.head.head.bone.position, bodyForward, Vector3.up, parent),
                null, null);

            skeletonCopy.body.AssignBones(
                CreateDirectionBone("hip", skeleton.body.hip.bone.position, skeleton.body.spine[0].bone.position - skeleton.body.hip.bone.position, -bodyForward, parent),
                CreateDirectionBone("spine", skeleton.body.spine[0].bone.position, (skeleton.body.spine[1]?.bone.position ?? skeleton.head.neck.bone.position) - skeleton.body.spine[0].bone.position, -bodyForward, parent),
                skeleton.body.spine[1] == null ? null : CreateDirectionBone("chest", skeleton.body.spine[1].bone.position, (skeleton.body.spine[2]?.bone.position ?? skeleton.head.neck.bone.position) - skeleton.body.spine[1].bone.position, -bodyForward, parent),
                skeleton.body.spine[2] == null ? null : CreateDirectionBone("upperchest", skeleton.body.spine[2].bone.position, skeleton.head.neck.bone.position - skeleton.body.spine[2].bone.position, -bodyForward, parent));

            skeletonCopy.armLeft.AssignBones(
                CreateDirectionBone("shoulder_left", skeleton.armLeft.shoulder.bone.position, skeleton.armLeft.upperArm.bone.position - skeleton.armLeft.shoulder.bone.position, Vector3.up, parent),
                CreateDirectionBone("upperArm_left", skeleton.armLeft.upperArm.bone.position, skeleton.armLeft.lowerArm.bone.position - skeleton.armLeft.upperArm.bone.position, Vector3.up, parent),
                CreateDirectionBone("lowerArm_left", skeleton.armLeft.lowerArm.bone.position, skeleton.armLeft.hand.wrist.bone.position - skeleton.armLeft.lowerArm.bone.position, Vector3.up, parent));

            skeletonCopy.armRight.AssignBones(
                CreateDirectionBone("shoulder_right", skeleton.armRight.shoulder.bone.position, skeleton.armRight.upperArm.bone.position - skeleton.armRight.shoulder.bone.position, Vector3.up, parent),
                CreateDirectionBone("upperArm_right", skeleton.armRight.upperArm.bone.position, skeleton.armRight.lowerArm.bone.position - skeleton.armRight.upperArm.bone.position, Vector3.up, parent),
                CreateDirectionBone("lowerArm_right", skeleton.armRight.lowerArm.bone.position, skeleton.armRight.hand.wrist.bone.position - skeleton.armRight.lowerArm.bone.position, Vector3.up, parent));

            skeletonCopy.legLeft.AssignBones(
                CreateDirectionBone("upperLeg_left", skeleton.legLeft.upperLeg.bone.position, skeleton.legLeft.lowerLeg.bone.position - skeleton.legLeft.upperLeg.bone.position, bodyForward, parent),
                CreateDirectionBone("lowerLeg_left", skeleton.legLeft.lowerLeg.bone.position, skeleton.legLeft.foot.bone.position - skeleton.legLeft.lowerLeg.bone.position, bodyForward, parent),
                CreateDirectionBone("foot_left", skeleton.legLeft.foot.bone.position, bodyForward, Vector3.up, parent),
                skeleton.legLeft.toes == null ? null : CreateDirectionBone("toes_left", skeleton.legLeft.toes.bone.position, bodyForward, Vector3.up, parent),
                skeleton.legLeft.toesEnd == null ? null : CreateDirectionBone("toesEnd_left", skeleton.legLeft.toesEnd.bone.position, bodyForward, Vector3.up, parent));

            skeletonCopy.legRight.AssignBones(
                CreateDirectionBone("upperLeg_right", skeleton.legRight.upperLeg.bone.position, skeleton.legRight.lowerLeg.bone.position - skeleton.legRight.upperLeg.bone.position, bodyForward, parent),
                CreateDirectionBone("lowerLeg_right", skeleton.legRight.lowerLeg.bone.position, skeleton.legRight.foot.bone.position - skeleton.legRight.lowerLeg.bone.position, bodyForward, parent),
                CreateDirectionBone("foot_right", skeleton.legRight.foot.bone.position, bodyForward, Vector3.up, parent),
                skeleton.legRight.toes == null ? null : CreateDirectionBone("toes_right", skeleton.legRight.toes.bone.position, bodyForward, Vector3.up, parent),
                skeleton.legRight.toesEnd == null ? null : CreateDirectionBone("toesEnd_right", skeleton.legRight.toesEnd.bone.position, bodyForward, Vector3.up, parent));

            skeletonCopy.armLeft.hand  = CopyHandSkeleton(skeleton.armLeft.hand, parent);
            skeletonCopy.armRight.hand = CopyHandSkeleton(skeleton.armRight.hand, parent);

            return(skeletonCopy);
        }
Example #3
0
        public void InitializeIK(Transform root, Animator animator, SkeletonBoneReferences bones, IKTargets_TMP targets)
        {
            if (isInitialized)
            {
                return;
            }
            isInitialized = true;

            ikContainer = new GameObject("IK");
            ikContainer.transform.SetParent(root);
            ikContainer.transform.localPosition = Vector3.zero;
            ikContainer.transform.localRotation = Quaternion.identity;

            this.animator = animator;
            this.bones    = bones;
            this.targets  = targets;
        }
Example #4
0
        private void DrawHumanoidSkeletonBones(SkeletonBoneReferences bones)
        {
            // Draw Skeleton
            ConnectBones(bones.main, bones.legLeft.upperLeg);
            ConnectBones(bones.main, bones.legRight.upperLeg);

            // Draw Bones
            DrawBone(bones.main, size);


            // Draw whole skeleton
            DrawBodyAndHead(bones.body, bones.head);

            DrawLeg(bones.legLeft, bones.body);
            DrawLeg(bones.legRight, bones.body);

            DrawArm(bones.armLeft, bones.body);
            DrawArm(bones.armRight, bones.body);
        }
        public void PopulateBoneReferences()
        {
            if (IsAnimatorValid())
            {
                boneReferences.Populate(animator);
            }

            if (boneReferences.IsValid)
            {
                if (newSkeletonParent != null)
                {
                    DestroyImmediate(newSkeletonParent.gameObject);
                }

                newSkeletonParent = new GameObject("newSkeleton").transform;
                newSkeletonParent.SetParent(transform);

                newSkeleton = CopySkeleton(boneReferences, newSkeletonParent);
            }
        }
        // Parent Skeleton to another skeleton
        public void ParentSkeletonToAnotherSkeleton(SkeletonBoneReferences parent, SkeletonBoneReferences child)
        {
            InsertBoneParent(child.main.bone, parent.main.bone);
            InsertBoneParent(child.body.hip.bone, parent.body.hip.bone);
            for (var index = 0; index < child.body.spine.Length; index++)
            {
                InsertBoneParent(child.body.spine[index].bone, parent.body.spine[index].bone);
            }

            InsertBoneParent(child.head.neck.bone, parent.head.neck.bone);
            InsertBoneParent(child.head.head.bone, parent.head.head.bone);

            ParentArmToArm(parent.armLeft, child.armLeft);
            ParentArmToArm(parent.armRight, child.armRight);

            ParentLegToLeg(parent.legLeft, child.legLeft);
            ParentLegToLeg(parent.legRight, child.legRight);

            ParentHandSkeletonToAnotherHandSkeleton(parent.armLeft.hand, child.armLeft.hand);
            ParentHandSkeletonToAnotherHandSkeleton(parent.armRight.hand, child.armRight.hand);
        }
        // Fix parenting of a single skeleton
        public void ReparentSkeleton(SkeletonBoneReferences skeleton)
        {
            skeleton.body.hip.bone.SetParent(skeleton.body.hip.bone);

            skeleton.body.spine[0].bone.SetParent(skeleton.body.hip.bone);
            for (int i = 0; i < skeleton.body.spine.Length; i++)
            {
                skeleton.body.spine[i].bone.SetParent(i == 0 ? skeleton.body.hip.bone : skeleton.body.spine[i - 1].bone);
            }
            skeleton.armLeft.shoulder.bone.SetParent(skeleton.body.spine[skeleton.body.spine.Length - 1].bone);

            skeleton.head.neck.bone.SetParent(skeleton.body.spine[skeleton.body.spine.Length - 1].bone);
            skeleton.head.head.bone.SetParent(skeleton.head.neck.bone);

            ReparentArm(skeleton.armLeft, skeleton.body);
            ReparentArm(skeleton.armRight, skeleton.body);

            ReparentLeg(skeleton.legLeft, skeleton.body);
            ReparentLeg(skeleton.legRight, skeleton.body);

            ReparentHand(skeleton.armLeft.hand);
            ReparentHand(skeleton.armRight.hand);
        }
        public void GenerateScalerBonesForBody(SkeletonBoneReferences bones, SkeletonBoneReferences boneRotations)
        {
            AddScalerBone(HumanBodyBones.Hips, bones.body.hip.bone, boneRotations.body.hip.bone.rotation, new[] { boneRotations.legLeft.upperLeg.bone, boneRotations.legRight.upperLeg.bone, boneRotations.body.spine[0].bone });
            AddScalerBone(HumanBodyBones.Spine, bones.body.spine[0].bone, boneRotations.body.spine[0].bone.rotation, (bones.body.spine[1]?.bone != null) ? new [] { boneRotations.body.spine[1].bone } : new [] { boneRotations.head.neck.bone, boneRotations.armLeft.shoulder.bone, boneRotations.armRight.shoulder.bone });
            if (bones.body.spine[1]?.bone != null)
            {
                AddScalerBone(HumanBodyBones.Chest, bones.body.spine[1].bone, boneRotations.body.spine[1].bone.rotation, (bones.body.spine[2]?.bone != null) ? new[] { boneRotations.body.spine[2].bone } : new[] { boneRotations.head.neck.bone, boneRotations.armLeft.shoulder.bone, boneRotations.armRight.shoulder.bone });
            }
            if (bones.body.spine[2]?.bone != null)
            {
                AddScalerBone(HumanBodyBones.UpperChest, bones.body.spine[2].bone, boneRotations.body.spine[2].bone.rotation, new[] { boneRotations.head.neck.bone, boneRotations.armLeft.shoulder.bone, boneRotations.armRight.shoulder.bone });
            }

            AddScalerBone(HumanBodyBones.Neck, bones.head.neck.bone, boneRotations.head.neck.bone.rotation, new[] { boneRotations.head.head.bone });
            AddScalerBone(HumanBodyBones.Head, bones.head.head.bone, boneRotations.head.head.bone.rotation);

            // Arms
            AddScalerBone(HumanBodyBones.LeftShoulder, bones.armLeft.shoulder.bone, boneRotations.armLeft.shoulder.bone.rotation, new[] { boneRotations.armLeft.upperArm.bone });
            AddScalerBone(HumanBodyBones.LeftUpperArm, bones.armLeft.upperArm.bone, boneRotations.armLeft.upperArm.bone.rotation, new[] { boneRotations.armLeft.lowerArm.bone });
            AddScalerBone(HumanBodyBones.LeftLowerArm, bones.armLeft.lowerArm.bone, boneRotations.armLeft.lowerArm.bone.rotation, new[] { boneRotations.armLeft.hand.wrist.bone });

            AddScalerBone(HumanBodyBones.RightShoulder, bones.armRight.shoulder.bone, boneRotations.armRight.shoulder.bone.rotation, new[] { boneRotations.armRight.upperArm.bone });
            AddScalerBone(HumanBodyBones.RightUpperArm, bones.armRight.upperArm.bone, boneRotations.armRight.upperArm.bone.rotation, new[] { boneRotations.armRight.lowerArm.bone });
            AddScalerBone(HumanBodyBones.RightLowerArm, bones.armRight.lowerArm.bone, boneRotations.armRight.lowerArm.bone.rotation, new[] { boneRotations.armRight.hand.wrist.bone });

            // Legs
            AddScalerBone(HumanBodyBones.LeftUpperLeg, bones.legLeft.upperLeg.bone, boneRotations.legLeft.upperLeg.bone.rotation, new[] { boneRotations.legLeft.lowerLeg.bone });
            AddScalerBone(HumanBodyBones.LeftLowerLeg, bones.legLeft.lowerLeg.bone, boneRotations.legLeft.lowerLeg.bone.rotation, new[] { boneRotations.legLeft.foot.bone });
            AddScalerBone(HumanBodyBones.LeftFoot, bones.legLeft.foot.bone, boneRotations.legLeft.foot.bone.rotation);
            //if (bones.legLeft.toes?.bone != null) AddScalerBone(HumanBodyBones.LeftToes, bones.legLeft.toes.bone, boneRotations.legLeft.toes.bone.rotation);

            AddScalerBone(HumanBodyBones.RightUpperLeg, bones.legRight.upperLeg.bone, boneRotations.legRight.upperLeg.bone.rotation, new[] { boneRotations.legRight.lowerLeg.bone });
            AddScalerBone(HumanBodyBones.RightLowerLeg, bones.legRight.lowerLeg.bone, boneRotations.legRight.lowerLeg.bone.rotation, new[] { boneRotations.legRight.foot.bone });
            AddScalerBone(HumanBodyBones.RightFoot, bones.legRight.foot.bone, boneRotations.legRight.foot.bone.rotation);
            //if (bones.legRight.toes?.bone != null) AddScalerBone(HumanBodyBones.RightToes, bones.legRight.toes.bone, boneRotations.legRight.toes.bone.rotation);

            // Hands
            AddScalerBone(HumanBodyBones.LeftHand, bones.armLeft.hand.wrist.bone, boneRotations.armLeft.hand.wrist.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftIndexProximal, bones.armLeft.hand.index.proximal.bone, boneRotations.armLeft.hand.index.proximal.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftIndexIntermediate, bones.armLeft.hand.index.middle.bone, boneRotations.armLeft.hand.index.middle.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftIndexDistal, bones.armLeft.hand.index.distal.bone, boneRotations.armLeft.hand.index.distal.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftMiddleProximal, bones.armLeft.hand.middle.proximal.bone, boneRotations.armLeft.hand.middle.proximal.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftMiddleIntermediate, bones.armLeft.hand.middle.middle.bone, boneRotations.armLeft.hand.middle.middle.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftMiddleDistal, bones.armLeft.hand.middle.distal.bone, boneRotations.armLeft.hand.middle.distal.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftRingProximal, bones.armLeft.hand.ring.proximal.bone, boneRotations.armLeft.hand.ring.proximal.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftRingIntermediate, bones.armLeft.hand.ring.middle.bone, boneRotations.armLeft.hand.ring.middle.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftRingDistal, bones.armLeft.hand.ring.distal.bone, boneRotations.armLeft.hand.ring.distal.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftLittleProximal, bones.armLeft.hand.pinky.proximal.bone, boneRotations.armLeft.hand.pinky.proximal.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftLittleIntermediate, bones.armLeft.hand.pinky.middle.bone, boneRotations.armLeft.hand.pinky.middle.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftLittleDistal, bones.armLeft.hand.pinky.distal.bone, boneRotations.armLeft.hand.pinky.distal.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftThumbProximal, bones.armLeft.hand.thumb.proximal.bone, boneRotations.armLeft.hand.thumb.proximal.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftThumbIntermediate, bones.armLeft.hand.thumb.middle.bone, boneRotations.armLeft.hand.thumb.middle.bone.rotation);
            //AddScalerBone(HumanBodyBones.LeftThumbDistal, bones.armLeft.hand.thumb.distal.bone, boneRotations.armLeft.hand.thumb.distal.bone.rotation);

            AddScalerBone(HumanBodyBones.RightHand, bones.armRight.hand.wrist.bone, boneRotations.armRight.hand.wrist.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightIndexProximal, bones.armRight.hand.index.proximal.bone, boneRotations.armRight.hand.index.proximal.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightIndexIntermediate, bones.armRight.hand.index.middle.bone, boneRotations.armRight.hand.index.middle.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightIndexDistal, bones.armRight.hand.index.distal.bone, boneRotations.armRight.hand.index.distal.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightMiddleProximal, bones.armRight.hand.middle.proximal.bone, boneRotations.armRight.hand.middle.proximal.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightMiddleIntermediate, bones.armRight.hand.middle.middle.bone, boneRotations.armRight.hand.middle.middle.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightMiddleDistal, bones.armRight.hand.middle.distal.bone, boneRotations.armRight.hand.middle.distal.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightRingProximal, bones.armRight.hand.ring.proximal.bone, boneRotations.armRight.hand.ring.proximal.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightRingIntermediate, bones.armRight.hand.ring.middle.bone, boneRotations.armRight.hand.ring.middle.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightRingDistal, bones.armRight.hand.ring.distal.bone, boneRotations.armRight.hand.ring.distal.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightLittleProximal, bones.armRight.hand.pinky.proximal.bone, boneRotations.armRight.hand.pinky.proximal.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightLittleIntermediate, bones.armRight.hand.pinky.middle.bone, boneRotations.armRight.hand.pinky.middle.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightLittleDistal, bones.armRight.hand.pinky.distal.bone, boneRotations.armRight.hand.pinky.distal.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightThumbProximal, bones.armRight.hand.thumb.proximal.bone, boneRotations.armRight.hand.thumb.proximal.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightThumbIntermediate, bones.armRight.hand.thumb.middle.bone, boneRotations.armRight.hand.thumb.middle.bone.rotation);
            //AddScalerBone(HumanBodyBones.RightThumbDistal, bones.armRight.hand.thumb.distal.bone, boneRotations.armRight.hand.thumb.distal.bone.rotation);
        }