SceneWindow.cs 26 KB

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