SceneWindow.cs 26 KB

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