CinemachineFollowZoom.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine;
  2. using Cinemachine.Utility;
  3. namespace Cinemachine
  4. {
  5. /// <summary>
  6. /// An add-on module for Cinemachine Virtual Camera that adjusts
  7. /// the FOV of the lens to keep the target object at a constant size on the screen,
  8. /// regardless of camera and target position.
  9. /// </summary>
  10. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  11. [AddComponentMenu("")] // Hide in menu
  12. [SaveDuringPlay]
  13. [ExecuteAlways]
  14. [DisallowMultipleComponent]
  15. [HelpURL(Documentation.BaseURL + "manual/CinemachineFollowZoom.html")]
  16. public class CinemachineFollowZoom : CinemachineExtension
  17. {
  18. /// <summary>The shot width to maintain, in world units, at target distance.
  19. /// FOV will be adusted as far as possible to maintain this width at the
  20. /// target distance from the camera.</summary>
  21. [Tooltip("The shot width to maintain, in world units, at target distance.")]
  22. public float m_Width = 2f;
  23. /// <summary>Increase this value to soften the aggressiveness of the follow-zoom.
  24. /// Small numbers are more responsive, larger numbers give a more heavy slowly responding camera. </summary>
  25. [Range(0f, 20f)]
  26. [Tooltip("Increase this value to soften the aggressiveness of the follow-zoom. Small numbers are more responsive, larger numbers give a more heavy slowly responding camera.")]
  27. public float m_Damping = 1f;
  28. /// <summary>Will not generate an FOV smaller than this.</summary>
  29. [Range(1f, 179f)]
  30. [Tooltip("Lower limit for the FOV that this behaviour will generate.")]
  31. public float m_MinFOV = 3f;
  32. /// <summary>Will not generate an FOV larget than this.</summary>
  33. [Range(1f, 179f)]
  34. [Tooltip("Upper limit for the FOV that this behaviour will generate.")]
  35. public float m_MaxFOV = 60f;
  36. private void OnValidate()
  37. {
  38. m_Width = Mathf.Max(0, m_Width);
  39. m_MaxFOV = Mathf.Clamp(m_MaxFOV, 1, 179);
  40. m_MinFOV = Mathf.Clamp(m_MinFOV, 1, m_MaxFOV);
  41. }
  42. class VcamExtraState
  43. {
  44. public float m_previousFrameZoom = 0;
  45. }
  46. /// <summary>
  47. /// Report maximum damping time needed for this component.
  48. /// </summary>
  49. /// <returns>Highest damping setting in this component</returns>
  50. public override float GetMaxDampTime()
  51. {
  52. return m_Damping;
  53. }
  54. /// <summary>Callback to preform the zoom adjustment</summary>
  55. /// <param name="vcam">The virtual camera being processed</param>
  56. /// <param name="stage">The current pipeline stage</param>
  57. /// <param name="state">The current virtual camera state</param>
  58. /// <param name="deltaTime">The current applicable deltaTime</param>
  59. protected override void PostPipelineStageCallback(
  60. CinemachineVirtualCameraBase vcam,
  61. CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
  62. {
  63. VcamExtraState extra = GetExtraState<VcamExtraState>(vcam);
  64. if (deltaTime < 0 || !VirtualCamera.PreviousStateIsValid)
  65. extra.m_previousFrameZoom = state.Lens.FieldOfView;
  66. // Set the zoom after the body has been positioned, but before the aim,
  67. // so that composer can compose using the updated fov.
  68. if (stage == CinemachineCore.Stage.Body)
  69. {
  70. // Try to reproduce the target width
  71. float targetWidth = Mathf.Max(m_Width, 0);
  72. float fov = 179f;
  73. float d = Vector3.Distance(state.CorrectedPosition, state.ReferenceLookAt);
  74. if (d > UnityVectorExtensions.Epsilon)
  75. {
  76. // Clamp targetWidth to FOV min/max
  77. float minW = d * 2f * Mathf.Tan(m_MinFOV * Mathf.Deg2Rad / 2f);
  78. float maxW = d * 2f * Mathf.Tan(m_MaxFOV * Mathf.Deg2Rad / 2f);
  79. targetWidth = Mathf.Clamp(targetWidth, minW, maxW);
  80. // Apply damping
  81. if (deltaTime >= 0 && m_Damping > 0 && VirtualCamera.PreviousStateIsValid)
  82. {
  83. float currentWidth = d * 2f * Mathf.Tan(extra.m_previousFrameZoom * Mathf.Deg2Rad / 2f);
  84. float delta = targetWidth - currentWidth;
  85. delta = VirtualCamera.DetachedLookAtTargetDamp(delta, m_Damping, deltaTime);
  86. targetWidth = currentWidth + delta;
  87. }
  88. fov = 2f * Mathf.Atan(targetWidth / (2 * d)) * Mathf.Rad2Deg;
  89. }
  90. LensSettings lens = state.Lens;
  91. lens.FieldOfView = extra.m_previousFrameZoom = Mathf.Clamp(fov, m_MinFOV, m_MaxFOV);
  92. state.Lens = lens;
  93. }
  94. }
  95. }
  96. }