SceneWindow.cs 27 KB

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