Example #1
0
        private void BuildHierarchy(HumanoidBone currentParentBone, Transform currentBone, string path)
        {
            HumanoidBone nextParentBone;
            string       currentPath = path;

            if (currentBone != rootBone.BoneTransform)
            {
                int indexInMap = -1;
                if (IsBoneInMap(currentBone, out indexInMap))
                {
                    currentParentBone.Childs.Add(boneMap[indexInMap]);
                    boneMap[indexInMap].Path = path + "/" + currentBone.name;

                    nextParentBone = boneMap[indexInMap];
                    currentPath    = boneMap[indexInMap].Path;
                }
                else
                {
                    nextParentBone = currentParentBone;
                }
            }
            else
            {
                nextParentBone = currentParentBone;
            }

            // Get childs
            if (currentBone.childCount > 0)
            {
                for (int i = 0; i < currentBone.childCount; i++)
                {
                    BuildHierarchy(nextParentBone, currentBone.GetChild(i), currentPath);
                }
            }

            initialized = true;
        }
Example #2
0
        public void GenerateBoneMap(Animator inAnimator)
        {
            initialized = false;

            if (inAnimator == null)
            {
                Debug.Log("<color=red>" + "[SkeletorMapper.GenerateSkeleton] Animator can't be null: " + "</color>");
                return;
            }

            animator = inAnimator;

            AddMainBodyBones();

            if (includeFingerBones)
            {
                AddFingerBones();
            }

            /// Initialize all bones, no discarded bones, this is helpful for the recorder and loader
            int nBones = (int)HumanBodyBones.LastBone;

            boneMap = new HumanoidBone[(int)HumanBodyBones.LastBone];

            for (int i = 0; i < nBones; i++)
            {
                HumanBodyBones boneIndex = (HumanBodyBones)i;
                if (boneIndex == HumanBodyBones.LastBone)
                {
                    continue;
                }

                boneMap[i]           = new HumanoidBone();
                boneMap[i].ID        = boneIndex;
                boneMap[i].IndexBone = i;
                boneMap[i].Name      = boneIndex.ToString();



                if (boneIndex == HumanBodyBones.Hips)
                {
                    rootBone = boneMap[i];
                }

                Transform boneTransform = animator.GetBoneTransform(boneIndex);

                if (boneTransform != null)
                {
                    boneMap[i].BoneTransform = boneTransform;

                    if (boneIndex == HumanBodyBones.Hips)
                    {
                        rootBone.Path = boneTransform.name;

                        if (includeRigBoneNames)
                        {
                            rootBone.Name = boneTransform.name;
                        }
                    }

                    if (includeRigBoneNames)
                    {
                        boneMap[i].Name = boneTransform.name;
                    }
                }
            }

            // Generate from the root the child list
            if ((rootBone != null) && (rootBone.BoneTransform != null))
            {
                string  path         = rootBone.Path;
                Vector3 basePosition = animator.transform.position;

                rootBone.offsetWithParent = rootBone.BoneTransform.position - basePosition;
                boneMap[rootBone.IndexBone].offsetWithParent = rootBone.offsetWithParent;

                BuildHierarchy(rootBone, rootBone.BoneTransform, path);
            }
        }
Example #3
0
        private IEnumerator Initialize()
        {
            messagesUI.message = "Preparing 3D Model: Setting bvhRecorder";

            isReadyToRecordFrame = false;

            yield return(new WaitForSeconds(1.0f));

            skeleton.GenerateBoneMap(skeletonAnimator);

            yield return(new WaitForEndOfFrame());

            bvhRecorder.targetAvatar         = skeletonAnimator;
            bvhRecorder.enforceHumanoidBones = true;
            bvhRecorder.scripted             = true;

            yield return(new WaitForSeconds(1.0f));

            // Retarget bones
            bvhRecorder.bones = new List <Transform>();
            for (int i = 0; i < skeleton.BoneNumber; i++)
            {
                HumanoidBone bone = skeleton.GetBoneByIndex(i);
                if (bone != null)
                {
                    bvhRecorder.bones.Add(bone.BoneTransform);
                }
            }

            yield return(new WaitForSeconds(1.0f));

            bvhRecorder.buildSkeleton();
            bvhRecorder.genHierarchy();

            bvhRecorder.blender = false;

            yield return(new WaitForEndOfFrame());

            bvhRecorder.targetAvatar         = skeletonAnimator;
            bvhRecorder.enforceHumanoidBones = true;
            bvhRecorder.scripted             = true;

            // Retarget bones
            bvhRecorder.bones = new List <Transform>();
            for (int i = 0; i < skeleton.BoneNumber; i++)
            {
                HumanoidBone bone = skeleton.GetBoneByIndex(i);
                if (bone != null)
                {
                    bvhRecorder.bones.Add(bone.BoneTransform);
                }
            }

            messagesUI.message = "Preparing 3D Model: Mapping bones";

            // Always record full body
            bvhRecorder.InitializeBonesToRecord(skeleton.fullBodyBones);
            bvhRecorder.buildSkeleton();
            bvhRecorder.genHierarchy();

            framesCaptured = 0;



            messagesUI.message = "Preparing 3D Model: Calibrating Final IK with 3D Model";

            finalIk.enabled = true;

            yield return(new WaitForEndOfFrame());

            settingsCalibrationData = new VRIKCalibrator.Settings();

            VRIKCalibrator.Calibrate(finalIk, settingsCalibrationData,
                                     headTracker,
                                     hipTracker,
                                     leftHandTracker,
                                     rightHandTracker,
                                     leftFootTracker,
                                     rigthFootTracker);

            yield return(new WaitForEndOfFrame());

            isReadyToRecordFrame = true;
            messagesUI.message   = "3D Model ready!";
        }