SceneWindow.cs 34 KB

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