CinemachineNewFreeLookEditor.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #if CINEMACHINE_EXPERIMENTAL_VCAM
  2. using UnityEngine;
  3. using UnityEditor;
  4. using Cinemachine.Editor;
  5. using System.Collections.Generic;
  6. namespace Cinemachine
  7. {
  8. [CustomEditor(typeof(CinemachineNewFreeLook))]
  9. sealed class CinemachineNewFreeLookEditor
  10. : CinemachineVirtualCameraBaseEditor<CinemachineNewFreeLook>
  11. {
  12. GUIContent[] m_OrbitNames = new GUIContent[]
  13. { new GUIContent("Top Rig"), new GUIContent("Main Rig"), new GUIContent("Bottom Rig") };
  14. GUIContent m_CustomizeLabel = new GUIContent(
  15. "Customize", "Custom settings for this rig. If unchecked, main rig settins will be used");
  16. VcamStageEditorPipeline m_PipelineSet = new VcamStageEditorPipeline();
  17. /// <summary>Get the property names to exclude in the inspector.</summary>
  18. /// <param name="excluded">Add the names to this list</param>
  19. protected override void GetExcludedPropertiesInInspector(List<string> excluded)
  20. {
  21. base.GetExcludedPropertiesInInspector(excluded);
  22. excluded.Add(FieldPath(x => x.m_Rigs)); // can't use HideInInspector for this
  23. excluded.Add(FieldPath(x => x.m_Orbits));
  24. excluded.Add(FieldPath(x => x.m_SplineCurvature));
  25. }
  26. protected override void OnEnable()
  27. {
  28. base.OnEnable();
  29. Undo.undoRedoPerformed += ResetTargetOnUndo;
  30. m_PipelineSet.Initialize(
  31. // GetComponent
  32. (stage, result) =>
  33. {
  34. int numNullComponents = 0;
  35. foreach (var obj in targets)
  36. {
  37. var vcam = obj as CinemachineNewVirtualCamera;
  38. if (vcam != null)
  39. {
  40. var c = vcam.GetCinemachineComponent(stage);
  41. if (c != null)
  42. result.Add(c);
  43. else
  44. ++numNullComponents;
  45. }
  46. }
  47. return numNullComponents;
  48. },
  49. // SetComponent
  50. (stage, type) =>
  51. {
  52. Undo.SetCurrentGroupName("Cinemachine pipeline change");
  53. foreach (var obj in targets)
  54. {
  55. var vcam = obj as CinemachineNewVirtualCamera;
  56. if (vcam != null)
  57. {
  58. Component c = vcam.GetCinemachineComponent(stage);
  59. if (c != null && c.GetType() == type)
  60. continue;
  61. if (c != null)
  62. {
  63. Undo.DestroyObjectImmediate(c);
  64. vcam.InvalidateComponentCache();
  65. }
  66. if (type != null)
  67. {
  68. Undo.AddComponent(vcam.gameObject, type);
  69. vcam.InvalidateComponentCache();
  70. }
  71. }
  72. }
  73. });
  74. m_PipelineSet.SetStageIsLocked(CinemachineCore.Stage.Body);
  75. for (int i = 0; i < targets.Length; ++i)
  76. (targets[i] as CinemachineNewFreeLook).UpdateInputAxisProvider();
  77. #if UNITY_2021_2_OR_NEWER
  78. CinemachineSceneToolUtility.RegisterTool(typeof(FoVTool));
  79. CinemachineSceneToolUtility.RegisterTool(typeof(FarNearClipTool));
  80. CinemachineSceneToolUtility.RegisterTool(typeof(FollowOffsetTool));
  81. #endif
  82. }
  83. protected override void OnDisable()
  84. {
  85. m_PipelineSet.Shutdown();
  86. base.OnDisable();
  87. #if UNITY_2021_2_OR_NEWER
  88. CinemachineSceneToolUtility.UnregisterTool(typeof(FoVTool));
  89. CinemachineSceneToolUtility.UnregisterTool(typeof(FarNearClipTool));
  90. CinemachineSceneToolUtility.UnregisterTool(typeof(FollowOffsetTool));
  91. #endif
  92. }
  93. void ResetTargetOnUndo()
  94. {
  95. ResetTarget();
  96. }
  97. public override void OnInspectorGUI()
  98. {
  99. // Ordinary properties
  100. BeginInspector();
  101. DrawHeaderInInspector();
  102. DrawPropertyInInspector(FindProperty(x => x.m_Priority));
  103. DrawTargetsInInspector(FindProperty(x => x.m_Follow), FindProperty(x => x.m_LookAt));
  104. DrawPropertyInInspector(FindProperty(x => x.m_StandbyUpdate));
  105. DrawLensSettingsInInspector(FindProperty(x => x.m_Lens));
  106. DrawRemainingPropertiesInInspector();
  107. // Orbits
  108. EditorGUILayout.Space();
  109. EditorGUILayout.LabelField("Orbits", EditorStyles.boldLabel);
  110. SerializedProperty orbits = FindProperty(x => x.m_Orbits);
  111. EditorGUI.BeginChangeCheck();
  112. for (int i = 0; i < 3; ++i)
  113. {
  114. var o = orbits.GetArrayElementAtIndex(i);
  115. Rect rect = EditorGUILayout.GetControlRect(true);
  116. InspectorUtility.MultiPropertyOnLine(
  117. rect, m_OrbitNames[i],
  118. new [] { o.FindPropertyRelative(() => Target.m_Orbits[i].m_Height),
  119. o.FindPropertyRelative(() => Target.m_Orbits[i].m_Radius) },
  120. null);
  121. }
  122. EditorGUILayout.PropertyField(FindProperty(x => x.m_SplineCurvature));
  123. if (EditorGUI.EndChangeCheck())
  124. serializedObject.ApplyModifiedProperties();
  125. // Pipeline Stages
  126. EditorGUILayout.Space();
  127. var selectedRig = Selection.objects.Length == 1
  128. ? GUILayout.Toolbar(GetSelectedRig(Target), s_RigNames) : 0;
  129. SetSelectedRig(Target, selectedRig);
  130. EditorGUILayout.BeginVertical(GUI.skin.box);
  131. if (selectedRig == 1)
  132. m_PipelineSet.OnInspectorGUI(false);
  133. else
  134. DrawRigEditor(selectedRig == 0 ? 0 : 1);
  135. EditorGUILayout.EndVertical();
  136. // Extensions
  137. DrawExtensionsWidgetInInspector();
  138. }
  139. static GUIContent[] s_RigNames =
  140. {
  141. new GUIContent("Top Rig"),
  142. new GUIContent("Main Rig"),
  143. new GUIContent("Bottom Rig")
  144. };
  145. static int GetSelectedRig(CinemachineNewFreeLook freelook)
  146. {
  147. return freelook.m_VerticalAxis.Value < 0.33f ? 2 : (freelook.m_VerticalAxis.Value > 0.66f ? 0 : 1);
  148. }
  149. static void SetSelectedRig(CinemachineNewFreeLook freelook, int rigIndex)
  150. {
  151. Debug.Assert(rigIndex >= 0 && rigIndex < 3);
  152. if (GetSelectedRig(freelook) != rigIndex)
  153. {
  154. var prop = new SerializedObject(freelook).FindProperty(
  155. () => freelook.m_VerticalAxis).FindPropertyRelative(() => freelook.m_VerticalAxis.Value);
  156. prop.floatValue = rigIndex == 0 ? 1 : (rigIndex == 1 ? 0.5f : 0);
  157. prop.serializedObject.ApplyModifiedProperties();
  158. }
  159. }
  160. void OnSceneGUI()
  161. {
  162. m_PipelineSet.OnSceneGUI();
  163. #if UNITY_2021_2_OR_NEWER
  164. DrawSceneTools();
  165. #endif
  166. }
  167. #if UNITY_2021_2_OR_NEWER
  168. void DrawSceneTools()
  169. {
  170. var newFreelook = Target;
  171. if (newFreelook == null || !newFreelook.IsValid)
  172. {
  173. return;
  174. }
  175. if (CinemachineSceneToolUtility.IsToolActive(typeof(FoVTool)))
  176. {
  177. CinemachineSceneToolHelpers.FovToolHandle(newFreelook,
  178. new SerializedObject(newFreelook).FindProperty(() => newFreelook.m_Lens),
  179. newFreelook.m_Lens, IsHorizontalFOVUsed());
  180. }
  181. else if (CinemachineSceneToolUtility.IsToolActive(typeof(FarNearClipTool)))
  182. {
  183. CinemachineSceneToolHelpers.NearFarClipHandle(newFreelook,
  184. new SerializedObject(newFreelook).FindProperty(() => newFreelook.m_Lens));
  185. }
  186. else if (newFreelook.Follow != null && CinemachineSceneToolUtility.IsToolActive(typeof(FollowOffsetTool)))
  187. {
  188. CinemachineSceneToolHelpers.OrbitControlHandle(newFreelook,
  189. new SerializedObject(newFreelook).FindProperty(() => newFreelook.m_Orbits));
  190. }
  191. }
  192. #endif
  193. void DrawRigEditor(int rigIndex)
  194. {
  195. const float kBoxMargin = 3;
  196. SerializedProperty rig = FindProperty(x => x.m_Rigs).GetArrayElementAtIndex(rigIndex);
  197. CinemachineNewFreeLook.Rig def = new CinemachineNewFreeLook.Rig(); // for properties
  198. EditorGUIUtility.labelWidth -= kBoxMargin;
  199. ++EditorGUI.indentLevel;
  200. var components = Target.ComponentCache;
  201. if (DrawFoldoutPropertyWithEnabledCheckbox(
  202. rig.FindPropertyRelative(() => def.m_CustomLens),
  203. rig.FindPropertyRelative(() => def.m_Lens)))
  204. {
  205. Target.m_Rigs[rigIndex].m_Lens = Target.m_Lens;
  206. }
  207. int index = (int)CinemachineCore.Stage.Body;
  208. if (components[index] is CinemachineTransposer)
  209. {
  210. if (DrawFoldoutPropertyWithEnabledCheckbox(
  211. rig.FindPropertyRelative(() => def.m_CustomBody),
  212. rig.FindPropertyRelative(() => def.m_Body)))
  213. {
  214. Target.m_Rigs[rigIndex].m_Body.PullFrom(
  215. components[index] as CinemachineTransposer);
  216. }
  217. }
  218. index = (int)CinemachineCore.Stage.Aim;
  219. if (components[index] is CinemachineComposer)
  220. {
  221. if (DrawFoldoutPropertyWithEnabledCheckbox(
  222. rig.FindPropertyRelative(() => def.m_CustomAim),
  223. rig.FindPropertyRelative(() => def.m_Aim)))
  224. {
  225. Target.m_Rigs[rigIndex].m_Aim.PullFrom(
  226. components[index] as CinemachineComposer);
  227. }
  228. }
  229. index = (int)CinemachineCore.Stage.Noise;
  230. if (components[index] is CinemachineBasicMultiChannelPerlin)
  231. {
  232. if (DrawFoldoutPropertyWithEnabledCheckbox(
  233. rig.FindPropertyRelative(() => def.m_CustomNoise),
  234. rig.FindPropertyRelative(() => def.m_Noise)))
  235. {
  236. Target.m_Rigs[rigIndex].m_Noise.PullFrom(
  237. components[index] as CinemachineBasicMultiChannelPerlin);
  238. }
  239. }
  240. --EditorGUI.indentLevel;
  241. EditorGUIUtility.labelWidth += kBoxMargin;
  242. }
  243. // Returns true if default value should be applied
  244. bool DrawFoldoutPropertyWithEnabledCheckbox(
  245. SerializedProperty enabledProperty, SerializedProperty property)
  246. {
  247. GUIContent label = new GUIContent(property.displayName, property.tooltip);
  248. Rect rect = EditorGUILayout.GetControlRect(true,
  249. (enabledProperty.boolValue && property.isExpanded)
  250. ? EditorGUI.GetPropertyHeight(property)
  251. : EditorGUIUtility.singleLineHeight);
  252. Rect r = rect; r.height = EditorGUIUtility.singleLineHeight;
  253. if (!enabledProperty.boolValue)
  254. EditorGUI.LabelField(r, label);
  255. float labelWidth = EditorGUIUtility.labelWidth;
  256. bool newValue = EditorGUI.ToggleLeft(
  257. new Rect(labelWidth, r.y, r.width - labelWidth, r.height),
  258. m_CustomizeLabel, enabledProperty.boolValue);
  259. if (newValue != enabledProperty.boolValue)
  260. {
  261. enabledProperty.boolValue = newValue;
  262. enabledProperty.serializedObject.ApplyModifiedProperties();
  263. property.isExpanded = newValue;
  264. return true;
  265. }
  266. if (newValue == true)
  267. {
  268. EditorGUI.BeginChangeCheck();
  269. EditorGUI.PropertyField(rect, property, property.isExpanded);
  270. if (EditorGUI.EndChangeCheck())
  271. enabledProperty.serializedObject.ApplyModifiedProperties();
  272. }
  273. return false;
  274. }
  275. [DrawGizmo(GizmoType.Active | GizmoType.Selected, typeof(CinemachineNewFreeLook))]
  276. private static void DrawFreeLookGizmos(CinemachineNewFreeLook vcam, GizmoType selectionType)
  277. {
  278. // Standard frustum and logo
  279. CinemachineBrainEditor.DrawVirtualCameraBaseGizmos(vcam, selectionType);
  280. Color originalGizmoColour = Gizmos.color;
  281. bool isActiveVirtualCam = CinemachineCore.Instance.IsLive(vcam);
  282. Gizmos.color = isActiveVirtualCam
  283. ? CinemachineSettings.CinemachineCoreSettings.ActiveGizmoColour
  284. : CinemachineSettings.CinemachineCoreSettings.InactiveGizmoColour;
  285. if (vcam.Follow != null)
  286. {
  287. Vector3 pos = vcam.Follow.position;
  288. Vector3 up = Vector3.up;
  289. CinemachineBrain brain = CinemachineCore.Instance.FindPotentialTargetBrain(vcam);
  290. if (brain != null)
  291. up = brain.DefaultWorldUp;
  292. var middleRig = vcam.GetComponent<CinemachineTransposer>();
  293. if (middleRig != null)
  294. {
  295. float scale = vcam.m_RadialAxis.Value;
  296. Quaternion orient = middleRig.GetReferenceOrientation(up);
  297. up = orient * Vector3.up;
  298. var orbital = middleRig as CinemachineOrbitalTransposer;
  299. if (orbital != null)
  300. {
  301. float rotation = orbital.m_XAxis.Value + orbital.m_Heading.m_Bias;
  302. orient = Quaternion.AngleAxis(rotation, up) * orient;
  303. }
  304. CinemachineOrbitalTransposerEditor.DrawCircleAtPointWithRadius(
  305. pos + up * vcam.m_Orbits[0].m_Height * scale,
  306. orient, vcam.m_Orbits[0].m_Radius * scale);
  307. CinemachineOrbitalTransposerEditor.DrawCircleAtPointWithRadius(
  308. pos + up * vcam.m_Orbits[1].m_Height * scale,
  309. orient, vcam.m_Orbits[1].m_Radius * scale);
  310. CinemachineOrbitalTransposerEditor.DrawCircleAtPointWithRadius(
  311. pos + up * vcam.m_Orbits[2].m_Height * scale,
  312. orient, vcam.m_Orbits[2].m_Radius * scale);
  313. DrawCameraPath(pos, orient, vcam);
  314. }
  315. }
  316. Gizmos.color = originalGizmoColour;
  317. }
  318. private static void DrawCameraPath(
  319. Vector3 atPos, Quaternion orient, CinemachineNewFreeLook vcam)
  320. {
  321. Matrix4x4 prevMatrix = Gizmos.matrix;
  322. Gizmos.matrix = Matrix4x4.TRS(atPos, orient, Vector3.one);
  323. const int kNumSteps = 20;
  324. Vector3 currPos = vcam.GetLocalPositionForCameraFromInput(0f);
  325. for (int i = 1; i < kNumSteps + 1; ++i)
  326. {
  327. float t = (float)i / (float)kNumSteps;
  328. Vector3 nextPos = vcam.GetLocalPositionForCameraFromInput(t);
  329. Gizmos.DrawLine(currPos, nextPos);
  330. currPos = nextPos;
  331. }
  332. Gizmos.matrix = prevMatrix;
  333. }
  334. }
  335. }
  336. #endif