SceneWindow.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. draggedSO = null;
  282. }
  283. else
  284. {
  285. if (!dragActive)
  286. {
  287. dragActive = true;
  288. ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;
  289. string draggedMeshPath = "";
  290. string[] draggedPaths = dragData.Paths;
  291. for (int i = 0; i < draggedPaths.Length; i++)
  292. {
  293. LibraryEntry entry = ProjectLibrary.GetEntry(draggedPaths[i]);
  294. if (entry != null && entry.Type == LibraryEntryType.File)
  295. {
  296. FileEntry fileEntry = (FileEntry) entry;
  297. if (fileEntry.ResType == ResourceType.Mesh)
  298. {
  299. draggedMeshPath = draggedPaths[i];
  300. break;
  301. }
  302. }
  303. }
  304. if (!string.IsNullOrEmpty(draggedMeshPath))
  305. {
  306. string meshName = Path.GetFileNameWithoutExtension(draggedMeshPath);
  307. draggedSO = new SceneObject(meshName);
  308. Mesh mesh = ProjectLibrary.Load<Mesh>(draggedMeshPath);
  309. Renderable renderable = draggedSO.AddComponent<Renderable>();
  310. renderable.Mesh = mesh;
  311. EditorApplication.SetSceneDirty();
  312. }
  313. }
  314. if (draggedSO != null)
  315. {
  316. Ray worldRay = camera.ScreenToWorldRay(scenePos);
  317. draggedSO.Position = worldRay*DefaultPlacementDepth;
  318. }
  319. }
  320. return;
  321. }
  322. else
  323. {
  324. if (dragActive)
  325. {
  326. dragActive = false;
  327. if (draggedSO != null)
  328. {
  329. draggedSO.Destroy();
  330. draggedSO = null;
  331. }
  332. }
  333. }
  334. if (HasFocus)
  335. {
  336. cameraController.EnableInput(true);
  337. if (inBounds)
  338. {
  339. if (Input.IsPointerButtonDown(PointerButton.Left))
  340. {
  341. sceneHandles.TrySelect(scenePos);
  342. }
  343. else if (Input.IsPointerButtonUp(PointerButton.Left))
  344. {
  345. if (!handleActive)
  346. {
  347. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  348. Input.IsButtonHeld(ButtonCode.RightControl);
  349. sceneSelection.PickObject(scenePos, ctrlHeld);
  350. }
  351. }
  352. }
  353. }
  354. else
  355. cameraController.EnableInput(false);
  356. sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
  357. sceneHandles.Draw();
  358. sceneSelection.Draw();
  359. if (VirtualInput.IsButtonDown(frameKey))
  360. {
  361. cameraController.FrameSelected();
  362. }
  363. }
  364. /// <inheritdoc/>
  365. protected override void WindowResized(int width, int height)
  366. {
  367. UpdateRenderTexture(width, height - HeaderHeight);
  368. base.WindowResized(width, height);
  369. }
  370. /// <inheritdoc/>
  371. protected override void FocusChanged(bool inFocus)
  372. {
  373. if (!inFocus)
  374. {
  375. sceneHandles.ClearSelection();
  376. }
  377. }
  378. /// <summary>
  379. /// Triggered when one of the scene tool buttons is clicked, changing the active scene handle.
  380. /// </summary>
  381. /// <param name="tool">Clicked scene tool to activate.</param>
  382. private void OnSceneToolButtonClicked(SceneViewTool tool)
  383. {
  384. EditorApplication.ActiveSceneTool = tool;
  385. editorSettingsHash = EditorSettings.Hash;
  386. }
  387. /// <summary>
  388. /// Triggered when one of the coordinate mode buttons is clicked, changing the active coordinate mode.
  389. /// </summary>
  390. /// <param name="mode">Clicked coordinate mode to activate.</param>
  391. private void OnCoordinateModeButtonClicked(HandleCoordinateMode mode)
  392. {
  393. EditorApplication.ActiveCoordinateMode = mode;
  394. editorSettingsHash = EditorSettings.Hash;
  395. }
  396. /// <summary>
  397. /// Triggered when one of the pivot buttons is clicked, changing the active pivot mode.
  398. /// </summary>
  399. /// <param name="mode">Clicked pivot mode to activate.</param>
  400. private void OnPivotModeButtonClicked(HandlePivotMode mode)
  401. {
  402. EditorApplication.ActivePivotMode = mode;
  403. editorSettingsHash = EditorSettings.Hash;
  404. }
  405. /// <summary>
  406. /// Triggered when the move snap button is toggled.
  407. /// </summary>
  408. /// <param name="active">Determins should be move snap be activated or deactivated.</param>
  409. private void OnMoveSnapToggled(bool active)
  410. {
  411. Handles.MoveHandleSnapActive = active;
  412. editorSettingsHash = EditorSettings.Hash;
  413. }
  414. /// <summary>
  415. /// Triggered when the move snap increment value changes.
  416. /// </summary>
  417. /// <param name="value">Value that determines in what increments to perform move snapping.</param>
  418. private void OnMoveSnapValueChanged(float value)
  419. {
  420. Handles.MoveSnapAmount = MathEx.Clamp(value, 0.01f, 1000.0f);
  421. editorSettingsHash = EditorSettings.Hash;
  422. }
  423. /// <summary>
  424. /// Triggered when the rotate snap button is toggled.
  425. /// </summary>
  426. /// <param name="active">Determins should be rotate snap be activated or deactivated.</param>
  427. private void OnRotateSnapToggled(bool active)
  428. {
  429. Handles.RotateHandleSnapActive = active;
  430. editorSettingsHash = EditorSettings.Hash;
  431. }
  432. /// <summary>
  433. /// Triggered when the rotate snap increment value changes.
  434. /// </summary>
  435. /// <param name="value">Value that determines in what increments to perform rotate snapping.</param>
  436. private void OnRotateSnapValueChanged(float value)
  437. {
  438. Handles.RotateSnapAmount = MathEx.Clamp(value, 0.01f, 360.0f);
  439. editorSettingsHash = EditorSettings.Hash;
  440. }
  441. /// <summary>
  442. /// Updates toggle button states according to current editor options. This is useful if tools, coordinate mode,
  443. /// pivot or other scene view options have been modified externally.
  444. /// </summary>
  445. private void UpdateButtonStates()
  446. {
  447. switch (EditorApplication.ActiveSceneTool)
  448. {
  449. case SceneViewTool.View:
  450. viewButton.Value = true;
  451. break;
  452. case SceneViewTool.Move:
  453. moveButton.Value = true;
  454. break;
  455. case SceneViewTool.Rotate:
  456. rotateButton.Value = true;
  457. break;
  458. case SceneViewTool.Scale:
  459. scaleButton.Value = true;
  460. break;
  461. }
  462. switch (EditorApplication.ActiveCoordinateMode)
  463. {
  464. case HandleCoordinateMode.Local:
  465. localCoordButton.Value = true;
  466. break;
  467. case HandleCoordinateMode.World:
  468. worldCoordButton.Value = true;
  469. break;
  470. }
  471. switch (EditorApplication.ActivePivotMode)
  472. {
  473. case HandlePivotMode.Center:
  474. centerButton.Value = true;
  475. break;
  476. case HandlePivotMode.Pivot:
  477. pivotButton.Value = true;
  478. break;
  479. }
  480. if (Handles.MoveHandleSnapActive)
  481. moveSnapButton.Value = true;
  482. else
  483. moveSnapButton.Value = false;
  484. moveSnapInput.Value = Handles.MoveSnapAmount;
  485. if (Handles.RotateHandleSnapActive)
  486. rotateSnapButton.Value = true;
  487. else
  488. rotateSnapButton.Value = false;
  489. moveSnapInput.Value = Handles.RotateSnapAmount.Degrees;
  490. }
  491. /// <summary>
  492. /// Activates or deactivates the profiler overlay according to current editor settings.
  493. /// </summary>
  494. private void UpdateProfilerOverlay()
  495. {
  496. if (EditorSettings.GetBool(ProfilerOverlayActiveKey))
  497. {
  498. if (activeProfilerOverlay == null)
  499. {
  500. SceneObject profilerSO = new SceneObject("EditorProfilerOverlay");
  501. profilerCamera = profilerSO.AddComponent<Camera>();
  502. profilerCamera.Target = renderTexture;
  503. profilerCamera.ClearFlags = ClearFlags.None;
  504. profilerCamera.Priority = 1;
  505. profilerCamera.Layers = 0;
  506. activeProfilerOverlay = profilerSO.AddComponent<ProfilerOverlay>();
  507. }
  508. }
  509. else
  510. {
  511. if (activeProfilerOverlay != null)
  512. {
  513. activeProfilerOverlay.SceneObject.Destroy();
  514. activeProfilerOverlay = null;
  515. profilerCamera = null;
  516. }
  517. }
  518. }
  519. /// <summary>
  520. /// Creates the scene camera and updates the render texture. Should be called at least once before using the
  521. /// scene view. Should be called whenever the window is resized.
  522. /// </summary>
  523. /// <param name="width">Width of the scene render target, in pixels.</param>
  524. /// <param name="height">Height of the scene render target, in pixels.</param>
  525. private void UpdateRenderTexture(int width, int height)
  526. {
  527. width = MathEx.Max(20, width);
  528. height = MathEx.Max(20, height);
  529. renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
  530. renderTexture.Priority = 1;
  531. if (camera == null)
  532. {
  533. SceneObject sceneCameraSO = new SceneObject("SceneCamera", true);
  534. camera = sceneCameraSO.AddComponent<Camera>();
  535. camera.Target = renderTexture;
  536. camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  537. sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
  538. sceneCameraSO.LookAt(new Vector3(0, 0.5f, 0));
  539. camera.Priority = 2;
  540. camera.NearClipPlane = 0.05f;
  541. camera.FarClipPlane = 2500.0f;
  542. camera.ClearColor = ClearColor;
  543. cameraController = sceneCameraSO.AddComponent<SceneCamera>();
  544. renderTextureGUI = new GUIRenderTexture(renderTexture);
  545. rtPanel.AddElement(renderTextureGUI);
  546. sceneGrid = new SceneGrid(camera);
  547. sceneSelection = new SceneSelection(camera);
  548. sceneGizmos = new SceneGizmos(camera);
  549. sceneHandles = new SceneHandles(this, camera);
  550. }
  551. else
  552. {
  553. camera.Target = renderTexture;
  554. renderTextureGUI.RenderTexture = renderTexture;
  555. }
  556. Rect2I rtBounds = new Rect2I(0, 0, width, height);
  557. renderTextureGUI.Bounds = rtBounds;
  558. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  559. // render target destroy/create cycle for every single pixel.
  560. camera.AspectRatio = width / (float)height;
  561. if (profilerCamera != null)
  562. profilerCamera.Target = renderTexture;
  563. }
  564. /// <summary>
  565. /// Parses an array of scene objects and removes elements that are children of elements that are also in the array.
  566. /// </summary>
  567. /// <param name="objects">Array containing duplicate objects as input, and array without duplicate objects as
  568. /// output.</param>
  569. private void CleanDuplicates(ref SceneObject[] objects)
  570. {
  571. List<SceneObject> cleanList = new List<SceneObject>();
  572. for (int i = 0; i < objects.Length; i++)
  573. {
  574. bool foundParent = false;
  575. for (int j = 0; j < objects.Length; j++)
  576. {
  577. SceneObject elem = objects[i];
  578. while (elem != null && elem != objects[j])
  579. elem = elem.Parent;
  580. bool isChildOf = elem == objects[j];
  581. if (i != j && isChildOf)
  582. {
  583. foundParent = true;
  584. break;
  585. }
  586. }
  587. if (!foundParent)
  588. cleanList.Add(objects[i]);
  589. }
  590. objects = cleanList.ToArray();
  591. }
  592. }
  593. }