CinemachineRecomposer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using UnityEngine;
  2. using Cinemachine;
  3. /// <summary>
  4. /// An add-on module for Cinemachine Virtual Camera that adds a final tweak to the camera
  5. /// comnposition. It is intended for use in a Timeline context, where you want to hand-adjust
  6. /// the output of procedural or recorded camera aiming.
  7. /// </summary>
  8. [AddComponentMenu("")] // Hide in menu
  9. [ExecuteAlways]
  10. [SaveDuringPlay]
  11. [HelpURL(Documentation.BaseURL + "manual/CinemachineRecomposer.html")]
  12. public class CinemachineRecomposer : CinemachineExtension
  13. {
  14. /// <summary>
  15. /// When to apply the adjustment
  16. /// </summary>
  17. [Tooltip("When to apply the adjustment")]
  18. public CinemachineCore.Stage m_ApplyAfter;
  19. /// <summary>
  20. /// Tilt the camera by this much
  21. /// </summary>
  22. [Tooltip("Tilt the camera by this much")]
  23. public float m_Tilt;
  24. /// <summary>
  25. /// Pan the camera by this much
  26. /// </summary>
  27. [Tooltip("Pan the camera by this much")]
  28. public float m_Pan;
  29. /// <summary>
  30. /// Roll the camera by this much
  31. /// </summary>
  32. [Tooltip("Roll the camera by this much")]
  33. public float m_Dutch;
  34. /// <summary>
  35. /// Scale the zoom by this amount (normal = 1)
  36. /// </summary>
  37. [Tooltip("Scale the zoom by this amount (normal = 1)")]
  38. public float m_ZoomScale;
  39. /// <summary>
  40. /// Lowering this value relaxes the camera's attention to the Follow target (normal = 1)
  41. /// </summary>
  42. [Range(0, 1)]
  43. [Tooltip("Lowering this value relaxes the camera's attention to the Follow target (normal = 1)")]
  44. public float m_FollowAttachment;
  45. /// <summary>
  46. /// Lowering this value relaxes the camera's attention to the LookAt target (normal = 1)
  47. /// </summary>
  48. [Range(0, 1)]
  49. [Tooltip("Lowering this value relaxes the camera's attention to the LookAt target (normal = 1)")]
  50. public float m_LookAtAttachment;
  51. private void Reset()
  52. {
  53. m_ApplyAfter = CinemachineCore.Stage.Finalize;
  54. m_Tilt = 0;
  55. m_Pan = 0;
  56. m_Dutch = 0;
  57. m_ZoomScale = 1;
  58. m_FollowAttachment = 1;
  59. m_LookAtAttachment = 1;
  60. }
  61. private void OnValidate()
  62. {
  63. m_ZoomScale = Mathf.Max(0.01f, m_ZoomScale);
  64. m_FollowAttachment = Mathf.Clamp01(m_FollowAttachment);
  65. m_LookAtAttachment = Mathf.Clamp01(m_LookAtAttachment);
  66. }
  67. /// <summary>Callback to set the target attachment</summary>
  68. /// <param name="vcam">The virtual camera being processed</param>
  69. /// <param name="curState">Input state that must be mutated</param>
  70. /// <param name="deltaTime">The current applicable deltaTime</param>
  71. public override void PrePipelineMutateCameraStateCallback(
  72. CinemachineVirtualCameraBase vcam, ref CameraState curState, float deltaTime)
  73. {
  74. vcam.FollowTargetAttachment = m_FollowAttachment;
  75. vcam.LookAtTargetAttachment = m_LookAtAttachment;
  76. }
  77. /// <summary>Callback to tweak the settings</summary>
  78. /// <param name="vcam">The virtual camera being processed</param>
  79. /// <param name="stage">The current pipeline stage</param>
  80. /// <param name="state">The current virtual camera state</param>
  81. /// <param name="deltaTime">The current applicable deltaTime</param>
  82. protected override void PostPipelineStageCallback(
  83. CinemachineVirtualCameraBase vcam,
  84. CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
  85. {
  86. if (stage == m_ApplyAfter)
  87. {
  88. var lens = state.Lens;
  89. // Tilt by local X
  90. var qTilted = state.RawOrientation * Quaternion.AngleAxis(m_Tilt, Vector3.right);
  91. // Pan in world space
  92. var qDesired = Quaternion.AngleAxis(m_Pan, state.ReferenceUp) * qTilted;
  93. state.OrientationCorrection = Quaternion.Inverse(state.CorrectedOrientation) * qDesired;
  94. // And dutch at the end
  95. lens.Dutch += m_Dutch;
  96. // Finally zoom
  97. if (m_ZoomScale != 1)
  98. {
  99. lens.OrthographicSize *= m_ZoomScale;
  100. lens.FieldOfView *= m_ZoomScale;
  101. }
  102. state.Lens = lens;
  103. }
  104. }
  105. }