SpherePointKeyframeGroup.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Funly.SkyStudio
  6. {
  7. [Serializable]
  8. public class SpherePointKeyframeGroup : KeyframeGroup<SpherePointKeyframe>
  9. {
  10. public SpherePointKeyframeGroup(string name) : base(name) { }
  11. public const float MinHorizontalRotation = -Mathf.PI;
  12. public const float MaxHorizontalRotation = Mathf.PI;
  13. public const float MinVerticalRotation = -Mathf.PI / 2.0f;
  14. public const float MaxVerticalRotation = Mathf.PI / 2.0f;
  15. public SpherePointKeyframeGroup(string name, SpherePointKeyframe keyframe) : base(name)
  16. {
  17. AddKeyFrame(keyframe);
  18. }
  19. public SpherePoint SpherePointForTime(float time)
  20. {
  21. int beforeIndex;
  22. int afterIndex;
  23. // Shortcut and skip a calculations.
  24. if (keyframes.Count == 1)
  25. {
  26. return keyframes[0].spherePoint;
  27. }
  28. if (!GetSurroundingKeyFrames(time, out beforeIndex, out afterIndex)) {
  29. Debug.LogError("Failed to get surrounding sphere point for time: " + time);
  30. return null;
  31. }
  32. time = time - (int)time;
  33. SpherePointKeyframe beforeKeyframe = GetKeyframe(beforeIndex);
  34. SpherePointKeyframe afterKeyframe = GetKeyframe(afterIndex);
  35. float progressBetweenFrames = ProgressBetweenSurroundingKeyframes(time, beforeKeyframe.time, afterKeyframe.time);
  36. float curvedTime = CurveAdjustedBlendingTime(beforeKeyframe.interpolationCurve, progressBetweenFrames);
  37. Vector3 point = Vector3.Slerp(
  38. beforeKeyframe.spherePoint.GetWorldDirection(),
  39. afterKeyframe.spherePoint.GetWorldDirection(),
  40. curvedTime);
  41. return new SpherePoint(point.normalized);
  42. }
  43. }
  44. }