SceneWindow.cs 32 KB

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