SceneWindow.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Displays the scene view camera and various scene controls.
  9. /// </summary>
  10. internal sealed class SceneWindow : EditorWindow
  11. {
  12. internal const string ToggleProfilerOverlayBinding = "ToggleProfilerOverlay";
  13. internal const string ViewToolBinding = "ViewTool";
  14. internal const string MoveToolBinding = "MoveTool";
  15. internal const string RotateToolBinding = "RotateTool";
  16. internal const string ScaleToolBinding = "ScaleTool";
  17. internal const string DuplicateBinding = "Duplicate";
  18. internal const string DeleteBinding = "Delete";
  19. internal const string FrameBinding = "SceneFrame";
  20. private const int HeaderHeight = 20;
  21. private const float DefaultPlacementDepth = 5.0f;
  22. private static readonly Color ClearColor = new Color(83.0f/255.0f, 83.0f/255.0f, 83.0f/255.0f);
  23. private const string ProfilerOverlayActiveKey = "_Internal_ProfilerOverlayActive";
  24. private const int HandleAxesGUISize = 50;
  25. private const int HandleAxesGUIPadding = 5;
  26. private Camera camera;
  27. private SceneCamera cameraController;
  28. private RenderTexture2D renderTexture;
  29. private GUILayoutY mainLayout;
  30. private GUIPanel rtPanel;
  31. private GUIRenderTexture renderTextureGUI;
  32. private SceneGrid sceneGrid;
  33. private SceneSelection sceneSelection;
  34. private SceneGizmos sceneGizmos;
  35. private SceneHandles sceneHandles;
  36. private GUIToggle viewButton;
  37. private GUIToggle moveButton;
  38. private GUIToggle rotateButton;
  39. private GUIToggle scaleButton;
  40. private GUIToggle localCoordButton;
  41. private GUIToggle worldCoordButton;
  42. private GUIToggle pivotButton;
  43. private GUIToggle centerButton;
  44. private GUIToggle moveSnapButton;
  45. private GUIFloatField moveSnapInput;
  46. private GUIToggle rotateSnapButton;
  47. private GUIFloatField rotateSnapInput;
  48. private SceneAxesGUI sceneAxesGUI;
  49. private int editorSettingsHash = int.MaxValue;
  50. private VirtualButton duplicateKey;
  51. private VirtualButton deleteKey;
  52. private VirtualButton frameKey;
  53. // Tool shortcuts
  54. private VirtualButton viewToolKey;
  55. private VirtualButton moveToolKey;
  56. private VirtualButton rotateToolKey;
  57. private VirtualButton scaleToolKey;
  58. // Profiler overlay
  59. private ProfilerOverlay activeProfilerOverlay;
  60. private Camera profilerCamera;
  61. private VirtualButton toggleProfilerOverlayKey;
  62. // Drag & drop
  63. private bool dragActive;
  64. private SceneObject draggedSO;
  65. /// <summary>
  66. /// Returns the scene camera.
  67. /// </summary>
  68. public Camera Camera
  69. {
  70. get { return camera; }
  71. }
  72. /// <summary>
  73. /// Determines scene camera's projection type.
  74. /// </summary>
  75. internal ProjectionType ProjectionType
  76. {
  77. get { return cameraController.ProjectionType; }
  78. set { cameraController.ProjectionType = value; sceneAxesGUI.ProjectionType = value; }
  79. }
  80. /// <summary>
  81. /// Constructs a new scene window.
  82. /// </summary>
  83. internal SceneWindow()
  84. { }
  85. /// <summary>
  86. /// Opens a scene window if its not open already.
  87. /// </summary>
  88. [MenuItem("Windows/Scene", ButtonModifier.CtrlAlt, ButtonCode.S, 6000)]
  89. private static void OpenSceneWindow()
  90. {
  91. OpenWindow<SceneWindow>();
  92. }
  93. /// <summary>
  94. /// Focuses on the currently selected object.
  95. /// </summary>
  96. [MenuItem("Tools/Frame Selected", ButtonModifier.None, ButtonCode.F, 9275, true)]
  97. private static void OpenSettingsWindow()
  98. {
  99. SceneWindow window = GetWindow<SceneWindow>();
  100. if (window != null)
  101. window.cameraController.FrameSelected();
  102. }
  103. /// <summary>
  104. /// Switches the active tool to the view tool.
  105. /// </summary>
  106. [MenuItem("Tools/View", ButtonModifier.Ctrl, ButtonCode.Q, 9274, true)]
  107. private static void SetViewTool()
  108. {
  109. SceneWindow window = GetWindow<SceneWindow>();
  110. if (window != null)
  111. window.OnSceneToolButtonClicked(SceneViewTool.View);
  112. }
  113. /// <summary>
  114. /// Switches the active tool to the move tool.
  115. /// </summary>
  116. [MenuItem("Tools/Move", ButtonModifier.Ctrl, ButtonCode.W, 9273)]
  117. private static void SetMoveTool()
  118. {
  119. SceneWindow window = GetWindow<SceneWindow>();
  120. if (window != null)
  121. window.OnSceneToolButtonClicked(SceneViewTool.Move);
  122. }
  123. /// <summary>
  124. /// Switches the active tool to the rotate tool.
  125. /// </summary>
  126. [MenuItem("Tools/Rotate", ButtonModifier.Ctrl, ButtonCode.E, 9272)]
  127. private static void SetRotateTool()
  128. {
  129. SceneWindow window = GetWindow<SceneWindow>();
  130. if (window != null)
  131. window.OnSceneToolButtonClicked(SceneViewTool.Rotate);
  132. }
  133. /// <summary>
  134. /// Switches the active tool to the scale tool.
  135. /// </summary>
  136. [MenuItem("Tools/Scale", ButtonModifier.Ctrl, ButtonCode.R, 9271)]
  137. private static void SetScaleTool()
  138. {
  139. SceneWindow window = GetWindow<SceneWindow>();
  140. if (window != null)
  141. window.OnSceneToolButtonClicked(SceneViewTool.Scale);
  142. }
  143. /// <inheritdoc/>
  144. protected override LocString GetDisplayName()
  145. {
  146. return new LocEdString("Scene");
  147. }
  148. private void OnInitialize()
  149. {
  150. mainLayout = GUI.AddLayoutY();
  151. GUIContent viewIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.View),
  152. new LocEdString("View"));
  153. GUIContent moveIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Move),
  154. new LocEdString("Move"));
  155. GUIContent rotateIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Rotate),
  156. new LocEdString("Rotate"));
  157. GUIContent scaleIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Scale),
  158. new LocEdString("Scale"));
  159. GUIContent localIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Local),
  160. new LocEdString("Local"));
  161. GUIContent worldIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.World),
  162. new LocEdString("World"));
  163. GUIContent pivotIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Pivot),
  164. new LocEdString("Pivot"));
  165. GUIContent centerIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Center),
  166. new LocEdString("Center"));
  167. GUIContent moveSnapIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.MoveSnap),
  168. new LocEdString("Move snap"));
  169. GUIContent rotateSnapIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.RotateSnap),
  170. new LocEdString("Rotate snap"));
  171. GUIToggleGroup handlesTG = new GUIToggleGroup();
  172. viewButton = new GUIToggle(viewIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  173. moveButton = new GUIToggle(moveIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  174. rotateButton = new GUIToggle(rotateIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  175. scaleButton = new GUIToggle(scaleIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  176. GUIToggleGroup coordModeTG = new GUIToggleGroup();
  177. localCoordButton = new GUIToggle(localIcon, coordModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(75));
  178. worldCoordButton = new GUIToggle(worldIcon, coordModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(75));
  179. GUIToggleGroup pivotModeTG = new GUIToggleGroup();
  180. pivotButton = new GUIToggle(pivotIcon, pivotModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  181. centerButton = new GUIToggle(centerIcon, pivotModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  182. moveSnapButton = new GUIToggle(moveSnapIcon, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  183. moveSnapInput = new GUIFloatField("", GUIOption.FlexibleWidth(35));
  184. rotateSnapButton = new GUIToggle(rotateSnapIcon, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  185. rotateSnapInput = new GUIFloatField("", GUIOption.FlexibleWidth(35));
  186. viewButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.View);
  187. moveButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Move);
  188. rotateButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Rotate);
  189. scaleButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Scale);
  190. localCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.Local);
  191. worldCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.World);
  192. pivotButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Pivot);
  193. centerButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Center);
  194. moveSnapButton.OnToggled += (bool active) => OnMoveSnapToggled(active);
  195. moveSnapInput.OnChanged += (float value) => OnMoveSnapValueChanged(value);
  196. rotateSnapButton.OnToggled += (bool active) => OnRotateSnapToggled(active);
  197. rotateSnapInput.OnChanged += (float value) => OnRotateSnapValueChanged(value);
  198. GUILayout handlesLayout = mainLayout.AddLayoutX();
  199. handlesLayout.AddElement(viewButton);
  200. handlesLayout.AddElement(moveButton);
  201. handlesLayout.AddElement(rotateButton);
  202. handlesLayout.AddElement(scaleButton);
  203. handlesLayout.AddSpace(10);
  204. handlesLayout.AddElement(localCoordButton);
  205. handlesLayout.AddElement(worldCoordButton);
  206. handlesLayout.AddSpace(10);
  207. handlesLayout.AddElement(pivotButton);
  208. handlesLayout.AddElement(centerButton);
  209. handlesLayout.AddFlexibleSpace();
  210. handlesLayout.AddElement(moveSnapButton);
  211. handlesLayout.AddElement(moveSnapInput);
  212. handlesLayout.AddSpace(10);
  213. handlesLayout.AddElement(rotateSnapButton);
  214. handlesLayout.AddElement(rotateSnapInput);
  215. GUIPanel mainPanel = mainLayout.AddPanel();
  216. rtPanel = mainPanel.AddPanel();
  217. GUIPanel sceneAxesPanel = mainPanel.AddPanel(-1);
  218. sceneAxesGUI = new SceneAxesGUI(this, sceneAxesPanel, HandleAxesGUISize, HandleAxesGUISize, ProjectionType.Perspective);
  219. toggleProfilerOverlayKey = new VirtualButton(ToggleProfilerOverlayBinding);
  220. viewToolKey = new VirtualButton(ViewToolBinding);
  221. moveToolKey = new VirtualButton(MoveToolBinding);
  222. rotateToolKey = new VirtualButton(RotateToolBinding);
  223. scaleToolKey = new VirtualButton(ScaleToolBinding);
  224. duplicateKey = new VirtualButton(DuplicateBinding);
  225. deleteKey = new VirtualButton(DeleteBinding);
  226. frameKey = new VirtualButton(FrameBinding);
  227. UpdateRenderTexture(Width, Height - HeaderHeight);
  228. UpdateProfilerOverlay();
  229. }
  230. private void OnDestroy()
  231. {
  232. if (camera != null)
  233. {
  234. camera.SceneObject.Destroy();
  235. camera = null;
  236. }
  237. sceneAxesGUI.Destroy();
  238. sceneAxesGUI = null;
  239. }
  240. /// <summary>
  241. /// Orients the camera so it looks along the provided axis.
  242. /// </summary>
  243. /// <param name="axis">Axis to look along.</param>
  244. internal void LookAlong(Vector3 axis)
  245. {
  246. axis.Normalize();
  247. cameraController.LookAlong(axis);
  248. UpdateGridMode();
  249. }
  250. private void UpdateGridMode()
  251. {
  252. Vector3 forward = camera.SceneObject.Forward;
  253. if (camera.ProjectionType == ProjectionType.Perspective)
  254. sceneGrid.SetMode(GridMode.Perspective);
  255. else
  256. {
  257. float dotX = Vector3.Dot(forward, Vector3.XAxis);
  258. if (dotX >= 0.95f)
  259. sceneGrid.SetMode(GridMode.OrthoX);
  260. else if (dotX <= -0.95f)
  261. sceneGrid.SetMode(GridMode.OrthoNegX);
  262. else
  263. {
  264. float dotY = Vector3.Dot(forward, Vector3.YAxis);
  265. if (dotY >= 0.95f)
  266. sceneGrid.SetMode(GridMode.OrthoY);
  267. else if (dotY <= -0.95f)
  268. sceneGrid.SetMode(GridMode.OrthoNegY);
  269. else
  270. {
  271. float dotZ = Vector3.Dot(forward, Vector3.ZAxis);
  272. if (dotZ >= 0.95f)
  273. sceneGrid.SetMode(GridMode.OrthoZ);
  274. else if (dotZ <= -0.95f)
  275. sceneGrid.SetMode(GridMode.OrthoNegZ);
  276. else
  277. sceneGrid.SetMode(GridMode.Perspective);
  278. }
  279. }
  280. }
  281. }
  282. /// <summary>
  283. /// Converts screen coordinates into coordinates relative to the scene view render texture.
  284. /// </summary>
  285. /// <param name="screenPos">Coordinates relative to the screen.</param>
  286. /// <param name="scenePos">Output coordinates relative to the scene view texture.</param>
  287. /// <returns>True if the coordinates are within the scene view texture, false otherwise.</returns>
  288. private bool ScreenToScenePos(Vector2I screenPos, out Vector2I scenePos)
  289. {
  290. scenePos = screenPos;
  291. Vector2I windowPos = ScreenToWindowPos(screenPos);
  292. Rect2I bounds = GUILayoutUtility.CalculateBounds(renderTextureGUI, GUI);
  293. if (bounds.Contains(windowPos))
  294. {
  295. scenePos.x = windowPos.x - bounds.x;
  296. scenePos.y = windowPos.y - bounds.y;
  297. return true;
  298. }
  299. return false;
  300. }
  301. private void OnEditorUpdate()
  302. {
  303. if (HasFocus)
  304. {
  305. if (!Input.IsPointerButtonHeld(PointerButton.Right))
  306. {
  307. if (VirtualInput.IsButtonDown(toggleProfilerOverlayKey))
  308. EditorSettings.SetBool(ProfilerOverlayActiveKey, !EditorSettings.GetBool(ProfilerOverlayActiveKey));
  309. if (VirtualInput.IsButtonDown(viewToolKey))
  310. EditorApplication.ActiveSceneTool = SceneViewTool.View;
  311. if (VirtualInput.IsButtonDown(moveToolKey))
  312. EditorApplication.ActiveSceneTool = SceneViewTool.Move;
  313. if (VirtualInput.IsButtonDown(rotateToolKey))
  314. EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;
  315. if (VirtualInput.IsButtonDown(scaleToolKey))
  316. EditorApplication.ActiveSceneTool = SceneViewTool.Scale;
  317. if (VirtualInput.IsButtonDown(duplicateKey))
  318. {
  319. SceneObject[] selectedObjects = Selection.SceneObjects;
  320. CleanDuplicates(ref selectedObjects);
  321. if (selectedObjects.Length > 0)
  322. {
  323. String message;
  324. if (selectedObjects.Length == 1)
  325. message = "Duplicated " + selectedObjects[0].Name;
  326. else
  327. message = "Duplicated " + selectedObjects.Length + " elements";
  328. UndoRedo.CloneSO(selectedObjects, message);
  329. EditorApplication.SetSceneDirty();
  330. }
  331. }
  332. if (VirtualInput.IsButtonDown(deleteKey))
  333. {
  334. SceneObject[] selectedObjects = Selection.SceneObjects;
  335. CleanDuplicates(ref selectedObjects);
  336. if (selectedObjects.Length > 0)
  337. {
  338. foreach (var so in selectedObjects)
  339. {
  340. string message = "Deleted " + so.Name;
  341. UndoRedo.DeleteSO(so, message);
  342. }
  343. EditorApplication.SetSceneDirty();
  344. }
  345. }
  346. }
  347. }
  348. // Refresh GUI buttons if needed (in case someones changes the values from script)
  349. if (editorSettingsHash != EditorSettings.Hash)
  350. {
  351. UpdateButtonStates();
  352. UpdateProfilerOverlay();
  353. editorSettingsHash = EditorSettings.Hash;
  354. }
  355. // Update scene view handles and selection
  356. sceneGizmos.Draw();
  357. sceneGrid.Draw();
  358. bool handleActive = false;
  359. if (Input.IsPointerButtonUp(PointerButton.Left))
  360. {
  361. if (sceneHandles.IsActive())
  362. {
  363. sceneHandles.ClearSelection();
  364. handleActive = true;
  365. }
  366. if (sceneAxesGUI.IsActive())
  367. {
  368. sceneAxesGUI.ClearSelection();
  369. handleActive = true;
  370. }
  371. }
  372. Vector2I scenePos;
  373. bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);
  374. bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
  375. draggedOver &= inBounds && DragDrop.Type == DragDropType.Resource;
  376. if (draggedOver)
  377. {
  378. if (DragDrop.DropInProgress)
  379. {
  380. dragActive = false;
  381. if (draggedSO != null)
  382. Selection.SceneObject = draggedSO;
  383. draggedSO = null;
  384. }
  385. else
  386. {
  387. if (!dragActive)
  388. {
  389. dragActive = true;
  390. ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;
  391. string[] draggedPaths = dragData.Paths;
  392. for (int i = 0; i < draggedPaths.Length; i++)
  393. {
  394. LibraryEntry entry = ProjectLibrary.GetEntry(draggedPaths[i]);
  395. if (entry != null && entry.Type == LibraryEntryType.File)
  396. {
  397. FileEntry fileEntry = (FileEntry) entry;
  398. if (fileEntry.ResType == ResourceType.Mesh)
  399. {
  400. if (!string.IsNullOrEmpty(draggedPaths[i]))
  401. {
  402. string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
  403. draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
  404. Mesh mesh = ProjectLibrary.Load<Mesh>(draggedPaths[i]);
  405. Renderable renderable = draggedSO.AddComponent<Renderable>();
  406. renderable.Mesh = mesh;
  407. EditorApplication.SetSceneDirty();
  408. }
  409. break;
  410. }
  411. else if (fileEntry.ResType == ResourceType.Prefab)
  412. {
  413. if (!string.IsNullOrEmpty(draggedPaths[i]))
  414. {
  415. Prefab prefab = ProjectLibrary.Load<Prefab>(draggedPaths[i]);
  416. draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
  417. EditorApplication.SetSceneDirty();
  418. }
  419. break;
  420. }
  421. }
  422. }
  423. }
  424. if (draggedSO != null)
  425. {
  426. Ray worldRay = camera.ScreenToWorldRay(scenePos);
  427. draggedSO.Position = worldRay*DefaultPlacementDepth;
  428. }
  429. }
  430. return;
  431. }
  432. else
  433. {
  434. if (dragActive)
  435. {
  436. dragActive = false;
  437. if (draggedSO != null)
  438. {
  439. draggedSO.Destroy();
  440. draggedSO = null;
  441. }
  442. }
  443. }
  444. if (HasFocus)
  445. {
  446. cameraController.EnableInput(true);
  447. if (inBounds)
  448. {
  449. if (Input.IsPointerButtonDown(PointerButton.Left))
  450. {
  451. Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPadding,
  452. HandleAxesGUIPadding, HandleAxesGUISize, HandleAxesGUISize);
  453. if (sceneAxesGUIBounds.Contains(scenePos))
  454. sceneAxesGUI.TrySelect(scenePos);
  455. else
  456. sceneHandles.TrySelect(scenePos);
  457. }
  458. else if (Input.IsPointerButtonUp(PointerButton.Left))
  459. {
  460. if (!handleActive)
  461. {
  462. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  463. Input.IsButtonHeld(ButtonCode.RightControl);
  464. sceneSelection.PickObject(scenePos, ctrlHeld);
  465. }
  466. }
  467. }
  468. }
  469. else
  470. cameraController.EnableInput(false);
  471. SceneHandles.BeginInput();
  472. sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
  473. sceneHandles.Draw();
  474. sceneAxesGUI.UpdateInput(scenePos);
  475. sceneAxesGUI.Draw();
  476. SceneHandles.EndInput();
  477. sceneSelection.Draw();
  478. UpdateGridMode();
  479. if (VirtualInput.IsButtonDown(frameKey))
  480. cameraController.FrameSelected();
  481. }
  482. /// <inheritdoc/>
  483. protected override void WindowResized(int width, int height)
  484. {
  485. UpdateRenderTexture(width, height - HeaderHeight);
  486. base.WindowResized(width, height);
  487. }
  488. /// <inheritdoc/>
  489. protected override void FocusChanged(bool inFocus)
  490. {
  491. if (!inFocus)
  492. {
  493. sceneHandles.ClearSelection();
  494. }
  495. }
  496. /// <summary>
  497. /// Triggered when one of the scene tool buttons is clicked, changing the active scene handle.
  498. /// </summary>
  499. /// <param name="tool">Clicked scene tool to activate.</param>
  500. private void OnSceneToolButtonClicked(SceneViewTool tool)
  501. {
  502. EditorApplication.ActiveSceneTool = tool;
  503. editorSettingsHash = EditorSettings.Hash;
  504. }
  505. /// <summary>
  506. /// Triggered when one of the coordinate mode buttons is clicked, changing the active coordinate mode.
  507. /// </summary>
  508. /// <param name="mode">Clicked coordinate mode to activate.</param>
  509. private void OnCoordinateModeButtonClicked(HandleCoordinateMode mode)
  510. {
  511. EditorApplication.ActiveCoordinateMode = mode;
  512. editorSettingsHash = EditorSettings.Hash;
  513. }
  514. /// <summary>
  515. /// Triggered when one of the pivot buttons is clicked, changing the active pivot mode.
  516. /// </summary>
  517. /// <param name="mode">Clicked pivot mode to activate.</param>
  518. private void OnPivotModeButtonClicked(HandlePivotMode mode)
  519. {
  520. EditorApplication.ActivePivotMode = mode;
  521. editorSettingsHash = EditorSettings.Hash;
  522. }
  523. /// <summary>
  524. /// Triggered when the move snap button is toggled.
  525. /// </summary>
  526. /// <param name="active">Determins should be move snap be activated or deactivated.</param>
  527. private void OnMoveSnapToggled(bool active)
  528. {
  529. Handles.MoveHandleSnapActive = active;
  530. editorSettingsHash = EditorSettings.Hash;
  531. }
  532. /// <summary>
  533. /// Triggered when the move snap increment value changes.
  534. /// </summary>
  535. /// <param name="value">Value that determines in what increments to perform move snapping.</param>
  536. private void OnMoveSnapValueChanged(float value)
  537. {
  538. Handles.MoveSnapAmount = MathEx.Clamp(value, 0.01f, 1000.0f);
  539. editorSettingsHash = EditorSettings.Hash;
  540. }
  541. /// <summary>
  542. /// Triggered when the rotate snap button is toggled.
  543. /// </summary>
  544. /// <param name="active">Determins should be rotate snap be activated or deactivated.</param>
  545. private void OnRotateSnapToggled(bool active)
  546. {
  547. Handles.RotateHandleSnapActive = active;
  548. editorSettingsHash = EditorSettings.Hash;
  549. }
  550. /// <summary>
  551. /// Triggered when the rotate snap increment value changes.
  552. /// </summary>
  553. /// <param name="value">Value that determines in what increments to perform rotate snapping.</param>
  554. private void OnRotateSnapValueChanged(float value)
  555. {
  556. Handles.RotateSnapAmount = MathEx.Clamp(value, 0.01f, 360.0f);
  557. editorSettingsHash = EditorSettings.Hash;
  558. }
  559. /// <summary>
  560. /// Updates toggle button states according to current editor options. This is useful if tools, coordinate mode,
  561. /// pivot or other scene view options have been modified externally.
  562. /// </summary>
  563. private void UpdateButtonStates()
  564. {
  565. switch (EditorApplication.ActiveSceneTool)
  566. {
  567. case SceneViewTool.View:
  568. viewButton.Value = true;
  569. break;
  570. case SceneViewTool.Move:
  571. moveButton.Value = true;
  572. break;
  573. case SceneViewTool.Rotate:
  574. rotateButton.Value = true;
  575. break;
  576. case SceneViewTool.Scale:
  577. scaleButton.Value = true;
  578. break;
  579. }
  580. switch (EditorApplication.ActiveCoordinateMode)
  581. {
  582. case HandleCoordinateMode.Local:
  583. localCoordButton.Value = true;
  584. break;
  585. case HandleCoordinateMode.World:
  586. worldCoordButton.Value = true;
  587. break;
  588. }
  589. switch (EditorApplication.ActivePivotMode)
  590. {
  591. case HandlePivotMode.Center:
  592. centerButton.Value = true;
  593. break;
  594. case HandlePivotMode.Pivot:
  595. pivotButton.Value = true;
  596. break;
  597. }
  598. if (Handles.MoveHandleSnapActive)
  599. moveSnapButton.Value = true;
  600. else
  601. moveSnapButton.Value = false;
  602. moveSnapInput.Value = Handles.MoveSnapAmount;
  603. if (Handles.RotateHandleSnapActive)
  604. rotateSnapButton.Value = true;
  605. else
  606. rotateSnapButton.Value = false;
  607. moveSnapInput.Value = Handles.RotateSnapAmount.Degrees;
  608. }
  609. /// <summary>
  610. /// Activates or deactivates the profiler overlay according to current editor settings.
  611. /// </summary>
  612. private void UpdateProfilerOverlay()
  613. {
  614. if (EditorSettings.GetBool(ProfilerOverlayActiveKey))
  615. {
  616. if (activeProfilerOverlay == null)
  617. {
  618. SceneObject profilerSO = new SceneObject("EditorProfilerOverlay");
  619. profilerCamera = profilerSO.AddComponent<Camera>();
  620. profilerCamera.Target = renderTexture;
  621. profilerCamera.ClearFlags = ClearFlags.None;
  622. profilerCamera.Priority = 1;
  623. profilerCamera.Layers = 0;
  624. activeProfilerOverlay = profilerSO.AddComponent<ProfilerOverlay>();
  625. }
  626. }
  627. else
  628. {
  629. if (activeProfilerOverlay != null)
  630. {
  631. activeProfilerOverlay.SceneObject.Destroy();
  632. activeProfilerOverlay = null;
  633. profilerCamera = null;
  634. }
  635. }
  636. }
  637. /// <summary>
  638. /// Creates the scene camera and updates the render texture. Should be called at least once before using the
  639. /// scene view. Should be called whenever the window is resized.
  640. /// </summary>
  641. /// <param name="width">Width of the scene render target, in pixels.</param>
  642. /// <param name="height">Height of the scene render target, in pixels.</param>
  643. private void UpdateRenderTexture(int width, int height)
  644. {
  645. width = MathEx.Max(20, width);
  646. height = MathEx.Max(20, height);
  647. renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
  648. renderTexture.Priority = 1;
  649. if (camera == null)
  650. {
  651. SceneObject sceneCameraSO = new SceneObject("SceneCamera", true);
  652. camera = sceneCameraSO.AddComponent<Camera>();
  653. camera.Target = renderTexture;
  654. camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  655. sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
  656. sceneCameraSO.LookAt(new Vector3(0, 0.5f, 0));
  657. camera.Priority = 2;
  658. camera.NearClipPlane = 0.05f;
  659. camera.FarClipPlane = 2500.0f;
  660. camera.ClearColor = ClearColor;
  661. camera.Layers = UInt64.MaxValue & ~SceneAxesHandle.LAYER; // Don't draw scene axes in this camera
  662. cameraController = sceneCameraSO.AddComponent<SceneCamera>();
  663. renderTextureGUI = new GUIRenderTexture(renderTexture);
  664. rtPanel.AddElement(renderTextureGUI);
  665. sceneGrid = new SceneGrid(camera);
  666. sceneSelection = new SceneSelection(camera);
  667. sceneGizmos = new SceneGizmos(camera);
  668. sceneHandles = new SceneHandles(this, camera);
  669. }
  670. else
  671. {
  672. camera.Target = renderTexture;
  673. renderTextureGUI.RenderTexture = renderTexture;
  674. }
  675. Rect2I rtBounds = new Rect2I(0, 0, width, height);
  676. renderTextureGUI.Bounds = rtBounds;
  677. sceneAxesGUI.SetPosition(width - HandleAxesGUISize - HandleAxesGUIPadding, HandleAxesGUIPadding);
  678. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  679. // render target destroy/create cycle for every single pixel.
  680. camera.AspectRatio = width / (float)height;
  681. if (profilerCamera != null)
  682. profilerCamera.Target = renderTexture;
  683. }
  684. /// <summary>
  685. /// Parses an array of scene objects and removes elements that are children of elements that are also in the array.
  686. /// </summary>
  687. /// <param name="objects">Array containing duplicate objects as input, and array without duplicate objects as
  688. /// output.</param>
  689. private void CleanDuplicates(ref SceneObject[] objects)
  690. {
  691. List<SceneObject> cleanList = new List<SceneObject>();
  692. for (int i = 0; i < objects.Length; i++)
  693. {
  694. bool foundParent = false;
  695. for (int j = 0; j < objects.Length; j++)
  696. {
  697. SceneObject elem = objects[i];
  698. while (elem != null && elem != objects[j])
  699. elem = elem.Parent;
  700. bool isChildOf = elem == objects[j];
  701. if (i != j && isChildOf)
  702. {
  703. foundParent = true;
  704. break;
  705. }
  706. }
  707. if (!foundParent)
  708. cleanList.Add(objects[i]);
  709. }
  710. objects = cleanList.ToArray();
  711. }
  712. }
  713. }