SceneWindow.cs 34 KB

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