SceneWindow.cs 28 KB

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