SceneWindow.cs 30 KB

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