Esempio n. 1
0
        public void loadAnimation(bool addToAnimation = true)
        {
            getTargetAvatar();

            if (bp == null)
            {
                throw new InvalidOperationException("No BVH file has been parsed.");
            }

            if (nameMap == null)
            {
                if (standardBoneNames)
                {
                    Dictionary <Transform, string> boneMap;
                    BVHRecorder.populateBoneMap(out boneMap, targetAvatar, UseRigBoneNames);
                    nameMap = boneMap.ToDictionary(kp => flexibleName(kp.Value), kp => kp.Key);
                }
                else
                {
                    nameMap = new Dictionary <string, Transform>();
                }
            }

            renamingMap = new Dictionary <string, string>();
            foreach (FakeDictionary entry in boneRenamingMap)
            {
                if (entry.bvhName != "" && entry.targetName != "")
                {
                    renamingMap.Add(flexibleName(entry.bvhName), flexibleName(entry.targetName));
                }
            }

            Queue <Transform> transforms = new Queue <Transform>();

            transforms.Enqueue(targetAvatar.transform);
            string targetName = flexibleName(bp.root.name);

            if (renamingMap.ContainsKey(targetName))
            {
                targetName = flexibleName(renamingMap[targetName]);
            }
            while (transforms.Any())
            {
                Transform transform = transforms.Dequeue();
                if (flexibleName(transform.name) == targetName)
                {
                    rootBone = transform;
                    break;
                }
                if (nameMap.ContainsKey(targetName) && nameMap[targetName] == transform)
                {
                    rootBone = transform;
                    break;
                }
                for (int i = 0; i < transform.childCount; i++)
                {
                    transforms.Enqueue(transform.GetChild(i));
                }
            }
            if (rootBone == null)
            {
                rootBone = BVHRecorder.getRootBone(targetAvatar);
                Debug.LogWarning("Using \"" + rootBone.name + "\" as the root bone.");
            }
            if (rootBone == null)
            {
                throw new InvalidOperationException("No root bone \"" + bp.root.name + "\" found.");
            }

            frames    = bp.frames;
            clip      = new AnimationClip();
            clip.name = "BVHClip (" + (clipCount++) + ")";
            if (clipName != "")
            {
                clip.name = clipName;
            }

            clip.legacy = true;

            prefix = getPathBetween(rootBone, targetAvatar.transform, true, true);

            Vector3    targetAvatarPosition = targetAvatar.transform.position;
            Quaternion targetAvatarRotation = targetAvatar.transform.rotation;

            targetAvatar.transform.position = new Vector3(0.0f, 0.0f, 0.0f);
            targetAvatar.transform.rotation = Quaternion.identity;

            getCurves(prefix, bp.root, rootBone, true);

            targetAvatar.transform.position = targetAvatarPosition;
            targetAvatar.transform.rotation = targetAvatarRotation;

            clip.EnsureQuaternionContinuity();

            if (addToAnimation)
            {
                if (anim == null)
                {
                    anim = targetAvatar.gameObject.GetComponent <Animation>();
                    if (anim == null)
                    {
                        anim = targetAvatar.gameObject.AddComponent <Animation>();
                    }
                }
                anim.AddClip(clip, clip.name);
                anim.clip = clip;
                anim.playAutomatically = autoPlay;

                if (autoPlay)
                {
                    anim.Play(clip.name);
                }
            }
        }