SceneWindow.cs 29 KB

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