CinemachineOrbitalTransposerEditor.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. namespace Cinemachine.Editor
  5. {
  6. [CustomEditor(typeof(CinemachineOrbitalTransposer))]
  7. internal class CinemachineOrbitalTransposerEditor : BaseEditor<CinemachineOrbitalTransposer>
  8. {
  9. /// <summary>Get the property names to exclude in the inspector.</summary>
  10. /// <param name="excluded">Add the names to this list</param>
  11. protected override void GetExcludedPropertiesInInspector(List<string> excluded)
  12. {
  13. base.GetExcludedPropertiesInInspector(excluded);
  14. if (Target.m_HeadingIsSlave)
  15. {
  16. excluded.Add(FieldPath(x => x.m_FollowOffset));
  17. excluded.Add(FieldPath(x => x.m_BindingMode));
  18. excluded.Add(FieldPath(x => x.m_Heading));
  19. excluded.Add(FieldPath(x => x.m_XAxis));
  20. excluded.Add(FieldPath(x => x.m_RecenterToTargetHeading));
  21. }
  22. if (Target.HideOffsetInInspector)
  23. excluded.Add(FieldPath(x => x.m_FollowOffset));
  24. switch (Target.m_BindingMode)
  25. {
  26. default:
  27. case CinemachineTransposer.BindingMode.LockToTarget:
  28. if (Target.m_AngularDampingMode == CinemachineTransposer.AngularDampingMode.Euler)
  29. excluded.Add(FieldPath(x => x.m_AngularDamping));
  30. else
  31. {
  32. excluded.Add(FieldPath(x => x.m_PitchDamping));
  33. excluded.Add(FieldPath(x => x.m_YawDamping));
  34. excluded.Add(FieldPath(x => x.m_RollDamping));
  35. }
  36. break;
  37. case CinemachineTransposer.BindingMode.LockToTargetNoRoll:
  38. excluded.Add(FieldPath(x => x.m_RollDamping));
  39. excluded.Add(FieldPath(x => x.m_AngularDamping));
  40. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  41. break;
  42. case CinemachineTransposer.BindingMode.LockToTargetWithWorldUp:
  43. excluded.Add(FieldPath(x => x.m_PitchDamping));
  44. excluded.Add(FieldPath(x => x.m_RollDamping));
  45. excluded.Add(FieldPath(x => x.m_AngularDamping));
  46. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  47. break;
  48. case CinemachineTransposer.BindingMode.LockToTargetOnAssign:
  49. case CinemachineTransposer.BindingMode.WorldSpace:
  50. excluded.Add(FieldPath(x => x.m_PitchDamping));
  51. excluded.Add(FieldPath(x => x.m_YawDamping));
  52. excluded.Add(FieldPath(x => x.m_RollDamping));
  53. excluded.Add(FieldPath(x => x.m_AngularDamping));
  54. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  55. break;
  56. case CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp:
  57. excluded.Add(FieldPath(x => x.m_XDamping));
  58. excluded.Add(FieldPath(x => x.m_PitchDamping));
  59. excluded.Add(FieldPath(x => x.m_YawDamping));
  60. excluded.Add(FieldPath(x => x.m_RollDamping));
  61. excluded.Add(FieldPath(x => x.m_AngularDamping));
  62. excluded.Add(FieldPath(x => x.m_AngularDampingMode));
  63. excluded.Add(FieldPath(x => x.m_Heading));
  64. excluded.Add(FieldPath(x => x.m_RecenterToTargetHeading));
  65. break;
  66. }
  67. }
  68. private void OnEnable()
  69. {
  70. Target.UpdateInputAxisProvider();
  71. }
  72. public override void OnInspectorGUI()
  73. {
  74. BeginInspector();
  75. if (Target.FollowTarget == null)
  76. EditorGUILayout.HelpBox(
  77. "Orbital Transposer requires a Follow target.",
  78. MessageType.Warning);
  79. Target.m_XAxis.ValueRangeLocked
  80. = (Target.m_BindingMode == CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp);
  81. DrawRemainingPropertiesInInspector();
  82. }
  83. /// Process a position drag from the user.
  84. /// Called "magically" by the vcam editor, so don't change the signature.
  85. private void OnVcamPositionDragged(Vector3 delta)
  86. {
  87. if (Target.FollowTarget != null)
  88. {
  89. Undo.RegisterCompleteObjectUndo(Target, "Camera drag");
  90. Quaternion targetOrientation = Target.GetReferenceOrientation(Target.VcamState.ReferenceUp);
  91. targetOrientation = targetOrientation * Quaternion.Euler(0, Target.m_Heading.m_Bias, 0);
  92. Vector3 localOffset = Quaternion.Inverse(targetOrientation) * delta;
  93. localOffset.x = 0;
  94. Target.m_FollowOffset += localOffset;
  95. Target.m_FollowOffset = Target.EffectiveOffset;
  96. }
  97. }
  98. [DrawGizmo(GizmoType.Active | GizmoType.Selected, typeof(CinemachineOrbitalTransposer))]
  99. static void DrawTransposerGizmos(CinemachineOrbitalTransposer target, GizmoType selectionType)
  100. {
  101. if (target.IsValid && !target.HideOffsetInInspector)
  102. {
  103. Color originalGizmoColour = Gizmos.color;
  104. Gizmos.color = CinemachineCore.Instance.IsLive(target.VirtualCamera)
  105. ? CinemachineSettings.CinemachineCoreSettings.ActiveGizmoColour
  106. : CinemachineSettings.CinemachineCoreSettings.InactiveGizmoColour;
  107. Vector3 up = target.VirtualCamera.State.ReferenceUp;
  108. Vector3 pos = target.FollowTargetPosition;
  109. Quaternion orient = target.GetReferenceOrientation(up);
  110. up = orient * Vector3.up;
  111. DrawCircleAtPointWithRadius
  112. (pos + up * target.m_FollowOffset.y, orient, target.m_FollowOffset.z);
  113. Gizmos.color = originalGizmoColour;
  114. }
  115. }
  116. public static void DrawCircleAtPointWithRadius(Vector3 point, Quaternion orient, float radius)
  117. {
  118. Matrix4x4 prevMatrix = Gizmos.matrix;
  119. Gizmos.matrix = Matrix4x4.TRS(point, orient, radius * Vector3.one);
  120. const int kNumPoints = 25;
  121. Vector3 currPoint = Vector3.forward;
  122. Quaternion rot = Quaternion.AngleAxis(360f / (float)kNumPoints, Vector3.up);
  123. for (int i = 0; i < kNumPoints + 1; ++i)
  124. {
  125. Vector3 nextPoint = rot * currPoint;
  126. Gizmos.DrawLine(currPoint, nextPoint);
  127. currPoint = nextPoint;
  128. }
  129. Gizmos.matrix = prevMatrix;
  130. }
  131. }
  132. }