SceneWindow.cs 24 KB

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