public void Awake()
 {
     CurrentPose = SilencePose;
     lastEmotion = CurrentEmotion;
     currentLipsyncOnly = SilencePose;
     currentEmotionOnly = CurrentCompleteEmotionPose;
 }
 private void DisplayPoseOnModel(BBFacePose pose)
 {
     pose.BonePoses.ForEach(bone =>
     {
         bone.BoneTransform.localPosition = bone.LocalPosition;
         bone.BoneTransform.localRotation = bone.LocalRotation;
     });
 }
 public void SetEmotionPose(Emotion emotion, BBFacePose pose)
 {
     if (EmotionPoses.Where(lPose => lPose.AssociatedEmotion == emotion).Count() > 0)
         EmotionPoses.Where(lPose => lPose.AssociatedEmotion == emotion).First().Pose = pose;
     else
         EmotionPoses.Add(new BBLabelledEmotionPose()
         {
             AssociatedEmotion = emotion,
             Pose = pose
         });
 }
 private IEnumerator BlendEmotionsCoroutine(BBFacePose start, BBFacePose target)
 {
     TargetEmotionPose = target;
     for (float i = 0; i < BlendingTime * 3; i += BlendingStep)
     {
         currentEmotionOnly = BBFacePose.Lerp(start, target, DefaultPose, i / (BlendingTime * 3));
         CurrentPose = MixLipSyncAndEmotion(currentLipsyncOnly, currentEmotionOnly);
         DisplayPoseOnModel(CurrentPose);
         yield return new WaitForSeconds(BlendingStep);
     }
 }
        private IEnumerator BlendLipSyncCoroutine(BBFacePose one, BBFacePose two)
        {
            BlendingLipSync = true;
            TargetLipSyncPose = two;
            for (float i = 0; i < BlendingTime; i += BlendingStep)
            {
                currentLipsyncOnly = BBFacePose.Lerp(one, two, DefaultPose, i / BlendingTime);
                CurrentPose = MixLipSyncAndEmotion(currentLipsyncOnly, currentEmotionOnly);
                DisplayPoseOnModel(CurrentPose);
                yield return new WaitForSeconds(BlendingStep);
            }
            TargetLipSyncPose = null;

            currentLipsyncOnly = two;
            CurrentPose = MixLipSyncAndEmotion(two, CurrentCompleteEmotionPose);
            DisplayPoseOnModel(CurrentPose);

            BlendingLipSync = false;
        }
 private BBFacePose MixLipSyncAndEmotion(BBFacePose lipSync, BBFacePose emotion)
 {
     return BBFacePose.LerpEmotion(lipSync, emotion, DefaultPose, EmotionIntensity);
 }
        public static BBFacePose Lerp(BBFacePose one, BBFacePose two, BBFacePose defaultPose, float t)
        {
            if (one == null && two != null)
            {
                return(two);
            }
            else if (two == null && one != null)
            {
                return(one);
            }
            else if (two == null && one == null)
            {
                throw new Exception("Can't lerp the poses: they are both null.");
            }

            BBFacePose result = new BBFacePose()
            {
                BonePoses = new List <BBBonePose>()
            };

            // Map the poses
            Dictionary <Transform, PosePair> map = new Dictionary <Transform, PosePair>();

            foreach (BBBonePose boneOne in one.BonePoses)
            {
                BBBonePose defaultVal = defaultPose.BonePoses.Where(pose => pose.BoneTransform.Equals(boneOne.BoneTransform)).First();
                map.Add(boneOne.BoneTransform, new PosePair()
                {
                    PosOne      = boneOne.LocalPosition,
                    PosTwo      = defaultVal.LocalPosition,
                    RotationOne = boneOne.LocalRotation,
                    RotationTwo = defaultVal.LocalRotation,
                });
            }
            foreach (BBBonePose boneTwo in two.BonePoses)
            {
                if (map.ContainsKey(boneTwo.BoneTransform))
                {
                    PosePair pair = map[boneTwo.BoneTransform];
                    pair.PosTwo      = boneTwo.LocalPosition;
                    pair.RotationTwo = boneTwo.LocalRotation;
                }
                else
                {
                    BBBonePose defaultVal = defaultPose.BonePoses.Where(pose => pose.BoneTransform.Equals(boneTwo.BoneTransform)).First();
                    map.Add(boneTwo.BoneTransform, new PosePair()
                    {
                        PosOne      = defaultVal.LocalPosition,
                        PosTwo      = boneTwo.LocalPosition,
                        RotationOne = defaultVal.LocalRotation,
                        RotationTwo = boneTwo.LocalRotation
                    });
                }
            }

            foreach (var entry in map.ToList())
            {
                Transform  transform = entry.Key;
                Vector3    posOne    = entry.Value.PosOne;
                Vector3    posTwo    = entry.Value.PosTwo;
                Quaternion rotOne    = entry.Value.RotationOne;
                Quaternion rotTwo    = entry.Value.RotationTwo;

                Vector3    finalPos      = Vector3.LerpUnclamped(posOne, posTwo, t);
                Quaternion finalRotation = Quaternion.LerpUnclamped(rotOne, rotTwo, t);

                result.BonePoses.Add(new BBBonePose()
                {
                    BoneTransform = transform,
                    LocalPosition = finalPos,
                    LocalRotation = finalRotation
                });
            }

            //Debug.Log(sb.ToString());
            return(result);
        }
        public static BBFacePose LerpEmotion(BBFacePose lipSync, BBFacePose emotion, BBFacePose defaultPose, float t)
        {
            if (lipSync == null && emotion != null)
            {
                return(emotion);
            }
            else if (emotion == null && lipSync != null)
            {
                return(lipSync);
            }
            else if (emotion == null && lipSync == null)
            {
                throw new Exception("Can't lerp the poses: they are both null.");
            }

            BBFacePose result = new BBFacePose()
            {
                BonePoses = new List <BBBonePose>(),
                Root      = lipSync.Root // Will maybe generate troubles...
            };

            // Map the poses
            Dictionary <Transform, MappedPose> map = new Dictionary <Transform, MappedPose>();

            foreach (BBBonePose boneOne in lipSync.BonePoses)
            {
                map.Add(boneOne.BoneTransform, new MappedPose()
                {
                    Pos = boneOne.LocalPosition,
                    Rot = boneOne.LocalRotation
                });
            }
            foreach (BBBonePose boneTwo in emotion.BonePoses)
            {
                if (map.ContainsKey(boneTwo.BoneTransform))
                {
                    result.BonePoses.Add(new BBBonePose()
                    {
                        BoneTransform = boneTwo.BoneTransform,
                        LocalPosition = Vector3.Lerp(map[boneTwo.BoneTransform].Pos, boneTwo.LocalPosition, t),
                        LocalRotation = Quaternion.Lerp(map[boneTwo.BoneTransform].Rot, boneTwo.LocalRotation, t)
                    });
                    map.Remove(boneTwo.BoneTransform);
                }
                else // If there is no pose for lipsync associated to this blendshape, then use it.
                {
                    result.BonePoses.Add(new BBBonePose()
                    {
                        BoneTransform = boneTwo.BoneTransform,
                        LocalPosition = boneTwo.LocalPosition,
                        LocalRotation = boneTwo.LocalRotation
                    });
                }
            }

            foreach (KeyValuePair <Transform, MappedPose> kvp in map)
            {
                result.BonePoses.Add(new BBBonePose()
                {
                    BoneTransform = kvp.Key,
                    LocalPosition = kvp.Value.Pos,
                    LocalRotation = kvp.Value.Rot
                });
            }
            return(result);
        }