SceneWindow.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. /** @addtogroup Scene-Editor
  10. * @{
  11. */
  12. /// <summary>
  13. /// Displays the scene view camera and various scene controls.
  14. /// </summary>
  15. internal sealed class SceneWindow : EditorWindow, IGlobalShortcuts
  16. {
  17. internal const string ToggleProfilerOverlayBinding = "ToggleProfilerOverlay";
  18. internal const string ViewToolBinding = "ViewTool";
  19. internal const string MoveToolBinding = "MoveTool";
  20. internal const string RotateToolBinding = "RotateTool";
  21. internal const string ScaleToolBinding = "ScaleTool";
  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 const int HandleAxesGUISize = 50;
  28. private const int HandleAxesGUIPaddingX = 10;
  29. private const int HandleAxesGUIPaddingY = 5;
  30. private Camera camera;
  31. private SceneCamera cameraController;
  32. private RenderTexture2D renderTexture;
  33. private GUILayoutY mainLayout;
  34. private GUIPanel rtPanel;
  35. private GUIButton focusCatcher;
  36. private GUIRenderTexture renderTextureGUI;
  37. private SceneGrid sceneGrid;
  38. private SceneSelection sceneSelection;
  39. private SceneGizmos sceneGizmos;
  40. private SceneHandles sceneHandles;
  41. private GUIToggle viewButton;
  42. private GUIToggle moveButton;
  43. private GUIToggle rotateButton;
  44. private GUIToggle scaleButton;
  45. private GUIToggle localCoordButton;
  46. private GUIToggle worldCoordButton;
  47. private GUIToggle pivotButton;
  48. private GUIToggle centerButton;
  49. private GUIToggle moveSnapButton;
  50. private GUIFloatField moveSnapInput;
  51. private GUIToggle rotateSnapButton;
  52. private GUIFloatField rotateSnapInput;
  53. private SceneAxesGUI sceneAxesGUI;
  54. private bool hasContentFocus = false;
  55. private bool HasContentFocus { get { return HasFocus && hasContentFocus; } }
  56. private int editorSettingsHash = int.MaxValue;
  57. private VirtualButton frameKey;
  58. // Tool shortcuts
  59. private VirtualButton viewToolKey;
  60. private VirtualButton moveToolKey;
  61. private VirtualButton rotateToolKey;
  62. private VirtualButton scaleToolKey;
  63. // Profiler overlay
  64. private ProfilerOverlay activeProfilerOverlay;
  65. private Camera profilerCamera;
  66. private VirtualButton toggleProfilerOverlayKey;
  67. // Drag & drop
  68. private bool dragActive;
  69. private SceneObject draggedSO;
  70. private Vector3 draggedSOOffset;
  71. private Vector2I dragBegin;
  72. /// <summary>
  73. /// Returns the scene camera.
  74. /// </summary>
  75. public Camera Camera
  76. {
  77. get { return camera; }
  78. }
  79. /// <summary>
  80. /// Determines scene camera's projection type.
  81. /// </summary>
  82. internal ProjectionType ProjectionType
  83. {
  84. get { return cameraController.ProjectionType; }
  85. set { cameraController.ProjectionType = value; sceneAxesGUI.ProjectionType = value; }
  86. }
  87. /// <summary>
  88. /// Constructs a new scene window.
  89. /// </summary>
  90. internal SceneWindow()
  91. { }
  92. /// <summary>
  93. /// Opens a scene window if its not open already.
  94. /// </summary>
  95. [MenuItem("Windows/Scene", ButtonModifier.CtrlAlt, ButtonCode.S, 6000)]
  96. private static void OpenSceneWindow()
  97. {
  98. OpenWindow<SceneWindow>();
  99. }
  100. /// <summary>
  101. /// Focuses on the currently selected object.
  102. /// </summary>
  103. [MenuItem("Tools/Frame Selected", ButtonModifier.None, ButtonCode.F, 9275, true)]
  104. private static void OpenSettingsWindow()
  105. {
  106. SceneWindow window = GetWindow<SceneWindow>();
  107. if (window != null)
  108. window.cameraController.FrameSelected();
  109. }
  110. /// <summary>
  111. /// Switches the active tool to the view tool.
  112. /// </summary>
  113. [MenuItem("Tools/View", ButtonModifier.Ctrl, ButtonCode.Q, 9274, true)]
  114. private static void SetViewTool()
  115. {
  116. SceneWindow window = GetWindow<SceneWindow>();
  117. if (window != null)
  118. window.OnSceneToolButtonClicked(SceneViewTool.View);
  119. }
  120. /// <summary>
  121. /// Switches the active tool to the move tool.
  122. /// </summary>
  123. [MenuItem("Tools/Move", ButtonModifier.Ctrl, ButtonCode.W, 9273)]
  124. private static void SetMoveTool()
  125. {
  126. SceneWindow window = GetWindow<SceneWindow>();
  127. if (window != null)
  128. window.OnSceneToolButtonClicked(SceneViewTool.Move);
  129. }
  130. /// <summary>
  131. /// Switches the active tool to the rotate tool.
  132. /// </summary>
  133. [MenuItem("Tools/Rotate", ButtonModifier.Ctrl, ButtonCode.E, 9272)]
  134. private static void SetRotateTool()
  135. {
  136. SceneWindow window = GetWindow<SceneWindow>();
  137. if (window != null)
  138. window.OnSceneToolButtonClicked(SceneViewTool.Rotate);
  139. }
  140. /// <summary>
  141. /// Switches the active tool to the scale tool.
  142. /// </summary>
  143. [MenuItem("Tools/Scale", ButtonModifier.Ctrl, ButtonCode.R, 9271)]
  144. private static void SetScaleTool()
  145. {
  146. SceneWindow window = GetWindow<SceneWindow>();
  147. if (window != null)
  148. window.OnSceneToolButtonClicked(SceneViewTool.Scale);
  149. }
  150. /// <inheritdoc/>
  151. protected override LocString GetDisplayName()
  152. {
  153. return new LocEdString("Scene");
  154. }
  155. private void OnInitialize()
  156. {
  157. mainLayout = GUI.AddLayoutY();
  158. GUIContent viewIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.View),
  159. new LocEdString("View"));
  160. GUIContent moveIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Move),
  161. new LocEdString("Move"));
  162. GUIContent rotateIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Rotate),
  163. new LocEdString("Rotate"));
  164. GUIContent scaleIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Scale),
  165. new LocEdString("Scale"));
  166. GUIContent localIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Local),
  167. new LocEdString("Local"));
  168. GUIContent worldIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.World),
  169. new LocEdString("World"));
  170. GUIContent pivotIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Pivot),
  171. new LocEdString("Pivot"));
  172. GUIContent centerIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Center),
  173. new LocEdString("Center"));
  174. GUIContent moveSnapIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.MoveSnap),
  175. new LocEdString("Move snap"));
  176. GUIContent rotateSnapIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.RotateSnap),
  177. new LocEdString("Rotate snap"));
  178. GUIToggleGroup handlesTG = new GUIToggleGroup();
  179. viewButton = new GUIToggle(viewIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  180. moveButton = new GUIToggle(moveIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  181. rotateButton = new GUIToggle(rotateIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  182. scaleButton = new GUIToggle(scaleIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  183. GUIToggleGroup coordModeTG = new GUIToggleGroup();
  184. localCoordButton = new GUIToggle(localIcon, coordModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(75));
  185. worldCoordButton = new GUIToggle(worldIcon, coordModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(75));
  186. GUIToggleGroup pivotModeTG = new GUIToggleGroup();
  187. pivotButton = new GUIToggle(pivotIcon, pivotModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  188. centerButton = new GUIToggle(centerIcon, pivotModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  189. moveSnapButton = new GUIToggle(moveSnapIcon, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  190. moveSnapInput = new GUIFloatField("", GUIOption.FlexibleWidth(35));
  191. rotateSnapButton = new GUIToggle(rotateSnapIcon, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  192. rotateSnapInput = new GUIFloatField("", GUIOption.FlexibleWidth(35));
  193. viewButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.View);
  194. moveButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Move);
  195. rotateButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Rotate);
  196. scaleButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Scale);
  197. localCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.Local);
  198. worldCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.World);
  199. pivotButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Pivot);
  200. centerButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Center);
  201. moveSnapButton.OnToggled += (bool active) => OnMoveSnapToggled(active);
  202. moveSnapInput.OnChanged += (float value) => OnMoveSnapValueChanged(value);
  203. rotateSnapButton.OnToggled += (bool active) => OnRotateSnapToggled(active);
  204. rotateSnapInput.OnChanged += (float value) => OnRotateSnapValueChanged(value);
  205. GUILayout handlesLayout = mainLayout.AddLayoutX();
  206. handlesLayout.AddElement(viewButton);
  207. handlesLayout.AddElement(moveButton);
  208. handlesLayout.AddElement(rotateButton);
  209. handlesLayout.AddElement(scaleButton);
  210. handlesLayout.AddSpace(10);
  211. handlesLayout.AddElement(localCoordButton);
  212. handlesLayout.AddElement(worldCoordButton);
  213. handlesLayout.AddSpace(10);
  214. handlesLayout.AddElement(pivotButton);
  215. handlesLayout.AddElement(centerButton);
  216. handlesLayout.AddFlexibleSpace();
  217. handlesLayout.AddElement(moveSnapButton);
  218. handlesLayout.AddElement(moveSnapInput);
  219. handlesLayout.AddSpace(10);
  220. handlesLayout.AddElement(rotateSnapButton);
  221. handlesLayout.AddElement(rotateSnapInput);
  222. GUIPanel mainPanel = mainLayout.AddPanel();
  223. rtPanel = mainPanel.AddPanel();
  224. GUIPanel sceneAxesPanel = mainPanel.AddPanel(-1);
  225. sceneAxesGUI = new SceneAxesGUI(this, sceneAxesPanel, HandleAxesGUISize, HandleAxesGUISize, ProjectionType.Perspective);
  226. focusCatcher = new GUIButton("", EditorStyles.Blank);
  227. focusCatcher.OnFocusGained += () => hasContentFocus = true;
  228. focusCatcher.OnFocusLost += () => hasContentFocus = false;
  229. GUIPanel focusPanel = GUI.AddPanel(-2);
  230. focusPanel.AddElement(focusCatcher);
  231. toggleProfilerOverlayKey = new VirtualButton(ToggleProfilerOverlayBinding);
  232. viewToolKey = new VirtualButton(ViewToolBinding);
  233. moveToolKey = new VirtualButton(MoveToolBinding);
  234. rotateToolKey = new VirtualButton(RotateToolBinding);
  235. scaleToolKey = new VirtualButton(ScaleToolBinding);
  236. frameKey = new VirtualButton(FrameBinding);
  237. UpdateRenderTexture(Width, Height - HeaderHeight);
  238. UpdateProfilerOverlay();
  239. }
  240. private void OnDestroy()
  241. {
  242. if (camera != null)
  243. {
  244. camera.SceneObject.Destroy();
  245. camera = null;
  246. }
  247. sceneAxesGUI.Destroy();
  248. sceneAxesGUI = null;
  249. }
  250. /// <summary>
  251. /// Deletes all currently selected objects.
  252. /// </summary>
  253. private void DeleteSelection()
  254. {
  255. SceneObject[] selectedObjects = Selection.SceneObjects;
  256. CleanDuplicates(ref selectedObjects);
  257. if (selectedObjects.Length > 0)
  258. {
  259. foreach (var so in selectedObjects)
  260. {
  261. string message = "Deleted " + so.Name;
  262. UndoRedo.DeleteSO(so, message);
  263. }
  264. EditorApplication.SetSceneDirty();
  265. }
  266. }
  267. /// <summary>
  268. /// Duplicates all currently selected objects.
  269. /// </summary>
  270. private void DuplicateSelection()
  271. {
  272. SceneObject[] selectedObjects = Selection.SceneObjects;
  273. CleanDuplicates(ref selectedObjects);
  274. if (selectedObjects.Length > 0)
  275. {
  276. string message;
  277. if (selectedObjects.Length == 1)
  278. message = "Duplicated " + selectedObjects[0].Name;
  279. else
  280. message = "Duplicated " + selectedObjects.Length + " elements";
  281. UndoRedo.CloneSO(selectedObjects, message);
  282. EditorApplication.SetSceneDirty();
  283. }
  284. }
  285. /// <inheritdoc/>
  286. void IGlobalShortcuts.OnRenamePressed()
  287. {
  288. // Do nothing
  289. }
  290. /// <inheritdoc/>
  291. void IGlobalShortcuts.OnDuplicatePressed()
  292. {
  293. DuplicateSelection();
  294. }
  295. /// <inheritdoc/>
  296. void IGlobalShortcuts.OnDeletePressed()
  297. {
  298. DeleteSelection();
  299. }
  300. /// <inheritdoc/>
  301. void IGlobalShortcuts.OnCopyPressed()
  302. {
  303. // Do nothing
  304. }
  305. /// <inheritdoc/>
  306. void IGlobalShortcuts.OnCutPressed()
  307. {
  308. // Do nothing
  309. }
  310. /// <inheritdoc/>
  311. void IGlobalShortcuts.OnPastePressed()
  312. {
  313. // Do nothing
  314. }
  315. /// <summary>
  316. /// Orients the camera so it looks along the provided axis.
  317. /// </summary>
  318. /// <param name="axis">Axis to look along.</param>
  319. internal void LookAlong(Vector3 axis)
  320. {
  321. axis.Normalize();
  322. cameraController.LookAlong(axis);
  323. UpdateGridMode();
  324. }
  325. private void UpdateGridMode()
  326. {
  327. Vector3 forward = camera.SceneObject.Forward;
  328. if (camera.ProjectionType == ProjectionType.Perspective)
  329. sceneGrid.SetMode(GridMode.Perspective);
  330. else
  331. {
  332. float dotX = Vector3.Dot(forward, Vector3.XAxis);
  333. if (dotX >= 0.95f)
  334. sceneGrid.SetMode(GridMode.OrthoX);
  335. else if (dotX <= -0.95f)
  336. sceneGrid.SetMode(GridMode.OrthoNegX);
  337. else
  338. {
  339. float dotY = Vector3.Dot(forward, Vector3.YAxis);
  340. if (dotY >= 0.95f)
  341. sceneGrid.SetMode(GridMode.OrthoY);
  342. else if (dotY <= -0.95f)
  343. sceneGrid.SetMode(GridMode.OrthoNegY);
  344. else
  345. {
  346. float dotZ = Vector3.Dot(forward, Vector3.ZAxis);
  347. if (dotZ >= 0.95f)
  348. sceneGrid.SetMode(GridMode.OrthoZ);
  349. else if (dotZ <= -0.95f)
  350. sceneGrid.SetMode(GridMode.OrthoNegZ);
  351. else
  352. sceneGrid.SetMode(GridMode.Perspective);
  353. }
  354. }
  355. }
  356. }
  357. /// <summary>
  358. /// Converts screen coordinates into coordinates relative to the scene view render texture.
  359. /// </summary>
  360. /// <param name="screenPos">Coordinates relative to the screen.</param>
  361. /// <param name="scenePos">Output coordinates relative to the scene view texture.</param>
  362. /// <returns>True if the coordinates are within the scene view texture, false otherwise.</returns>
  363. private bool ScreenToScenePos(Vector2I screenPos, out Vector2I scenePos)
  364. {
  365. scenePos = screenPos;
  366. Vector2I windowPos = ScreenToWindowPos(screenPos);
  367. Rect2I bounds = GUIUtility.CalculateBounds(renderTextureGUI, GUI);
  368. if (bounds.Contains(windowPos))
  369. {
  370. scenePos.x = windowPos.x - bounds.x;
  371. scenePos.y = windowPos.y - bounds.y;
  372. return true;
  373. }
  374. return false;
  375. }
  376. private void OnEditorUpdate()
  377. {
  378. if (HasFocus)
  379. {
  380. if (!Input.IsPointerButtonHeld(PointerButton.Right))
  381. {
  382. if (VirtualInput.IsButtonDown(EditorApplication.DuplicateKey))
  383. DuplicateSelection();
  384. else if (VirtualInput.IsButtonDown(EditorApplication.DeleteKey))
  385. DeleteSelection();
  386. else if (VirtualInput.IsButtonDown(toggleProfilerOverlayKey))
  387. EditorSettings.SetBool(ProfilerOverlayActiveKey, !EditorSettings.GetBool(ProfilerOverlayActiveKey));
  388. else if(VirtualInput.IsButtonDown(viewToolKey))
  389. EditorApplication.ActiveSceneTool = SceneViewTool.View;
  390. else if(VirtualInput.IsButtonDown(moveToolKey))
  391. EditorApplication.ActiveSceneTool = SceneViewTool.Move;
  392. else if(VirtualInput.IsButtonDown(rotateToolKey))
  393. EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;
  394. else if(VirtualInput.IsButtonDown(scaleToolKey))
  395. EditorApplication.ActiveSceneTool = SceneViewTool.Scale;
  396. }
  397. }
  398. // Refresh GUI buttons if needed (in case someones changes the values from script)
  399. if (editorSettingsHash != EditorSettings.Hash)
  400. {
  401. UpdateButtonStates();
  402. UpdateProfilerOverlay();
  403. editorSettingsHash = EditorSettings.Hash;
  404. }
  405. // Update scene view handles and selection
  406. sceneGizmos.Draw();
  407. sceneGrid.Draw();
  408. bool handleActive = false;
  409. if (Input.IsPointerButtonUp(PointerButton.Left))
  410. {
  411. if (sceneHandles.IsActive())
  412. {
  413. sceneHandles.ClearSelection();
  414. handleActive = true;
  415. }
  416. if (sceneAxesGUI.IsActive())
  417. {
  418. sceneAxesGUI.ClearSelection();
  419. handleActive = true;
  420. }
  421. }
  422. Vector2I scenePos;
  423. bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);
  424. bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
  425. draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource;
  426. if (draggedOver)
  427. {
  428. if (DragDrop.DropInProgress)
  429. {
  430. dragActive = false;
  431. if (draggedSO != null)
  432. {
  433. Selection.SceneObject = draggedSO;
  434. EditorApplication.SetSceneDirty();
  435. }
  436. else
  437. {
  438. //Selection.SceneObjects = sceneSelection.PickObjects(dragBegin, scenePos - dragBegin,
  439. // Input.IsButtonHeld(ButtonCode.LeftControl));
  440. }
  441. draggedSO = null;
  442. }
  443. else
  444. {
  445. if (!dragActive)
  446. {
  447. dragActive = true;
  448. dragBegin = scenePos;
  449. ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;
  450. string[] draggedPaths = dragData.Paths;
  451. for (int i = 0; i < draggedPaths.Length; i++)
  452. {
  453. ResourceMeta meta = ProjectLibrary.GetMeta(draggedPaths[i]);
  454. if (meta != null)
  455. {
  456. if (meta.ResType == ResourceType.Mesh)
  457. {
  458. if (!string.IsNullOrEmpty(draggedPaths[i]))
  459. {
  460. string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
  461. draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
  462. Mesh mesh = ProjectLibrary.Load<Mesh>(draggedPaths[i]);
  463. Renderable renderable = draggedSO.AddComponent<Renderable>();
  464. renderable.Mesh = mesh;
  465. if (mesh != null)
  466. draggedSOOffset = mesh.Bounds.Box.Center;
  467. else
  468. draggedSOOffset = Vector3.Zero;
  469. }
  470. break;
  471. }
  472. else if (meta.ResType == ResourceType.Prefab)
  473. {
  474. if (!string.IsNullOrEmpty(draggedPaths[i]))
  475. {
  476. Prefab prefab = ProjectLibrary.Load<Prefab>(draggedPaths[i]);
  477. draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
  478. if (draggedSO != null)
  479. {
  480. AABox draggedObjBounds = EditorUtility.CalculateBounds(draggedSO);
  481. draggedSOOffset = draggedObjBounds.Center;
  482. }
  483. else
  484. draggedSOOffset = Vector3.Zero;
  485. }
  486. break;
  487. }
  488. }
  489. }
  490. }
  491. if (draggedSO != null)
  492. {
  493. if (Input.IsButtonHeld(ButtonCode.Space))
  494. {
  495. SnapData snapData = sceneSelection.Snap(scenePos, new SceneObject[] {draggedSO});
  496. Debug.Log(snapData.normal);
  497. Debug.Log(snapData.position);
  498. draggedSO.Position = snapData.position;
  499. draggedSO.Rotation = Quaternion.FromAxisAngle(snapData.normal, new Degree(0));
  500. }
  501. else
  502. {
  503. Ray worldRay = camera.ViewportToWorldRay(scenePos);
  504. draggedSO.Position = worldRay * DefaultPlacementDepth - draggedSOOffset;
  505. }
  506. }
  507. }
  508. return;
  509. }
  510. else
  511. {
  512. if (dragActive)
  513. {
  514. dragActive = false;
  515. if (draggedSO != null)
  516. {
  517. draggedSO.Destroy();
  518. draggedSO = null;
  519. }
  520. }
  521. }
  522. if (HasContentFocus)
  523. {
  524. cameraController.EnableInput(true);
  525. if (inBounds)
  526. {
  527. if (Input.IsPointerButtonDown(PointerButton.Left))
  528. {
  529. Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX,
  530. HandleAxesGUIPaddingY, HandleAxesGUISize, HandleAxesGUISize);
  531. if (sceneAxesGUIBounds.Contains(scenePos))
  532. sceneAxesGUI.TrySelect(scenePos);
  533. else
  534. sceneHandles.TrySelect(scenePos);
  535. }
  536. else if (Input.IsPointerButtonUp(PointerButton.Left))
  537. {
  538. if (!handleActive)
  539. {
  540. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  541. Input.IsButtonHeld(ButtonCode.RightControl);
  542. sceneSelection.PickObject(scenePos, ctrlHeld, new SceneObject[] { draggedSO });
  543. }
  544. }
  545. }
  546. }
  547. else
  548. cameraController.EnableInput(false);
  549. SceneHandles.BeginInput();
  550. sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
  551. sceneHandles.Draw();
  552. sceneAxesGUI.UpdateInput(scenePos);
  553. sceneAxesGUI.Draw();
  554. SceneHandles.EndInput();
  555. sceneSelection.Draw();
  556. UpdateGridMode();
  557. if (VirtualInput.IsButtonDown(frameKey))
  558. cameraController.FrameSelected();
  559. }
  560. /// <inheritdoc/>
  561. protected override void WindowResized(int width, int height)
  562. {
  563. UpdateRenderTexture(width, height - HeaderHeight);
  564. base.WindowResized(width, height);
  565. }
  566. /// <inheritdoc/>
  567. protected override void FocusChanged(bool inFocus)
  568. {
  569. if (!inFocus)
  570. {
  571. sceneHandles.ClearSelection();
  572. }
  573. }
  574. /// <summary>
  575. /// Triggered when one of the scene tool buttons is clicked, changing the active scene handle.
  576. /// </summary>
  577. /// <param name="tool">Clicked scene tool to activate.</param>
  578. private void OnSceneToolButtonClicked(SceneViewTool tool)
  579. {
  580. EditorApplication.ActiveSceneTool = tool;
  581. editorSettingsHash = EditorSettings.Hash;
  582. }
  583. /// <summary>
  584. /// Triggered when one of the coordinate mode buttons is clicked, changing the active coordinate mode.
  585. /// </summary>
  586. /// <param name="mode">Clicked coordinate mode to activate.</param>
  587. private void OnCoordinateModeButtonClicked(HandleCoordinateMode mode)
  588. {
  589. EditorApplication.ActiveCoordinateMode = mode;
  590. editorSettingsHash = EditorSettings.Hash;
  591. }
  592. /// <summary>
  593. /// Triggered when one of the pivot buttons is clicked, changing the active pivot mode.
  594. /// </summary>
  595. /// <param name="mode">Clicked pivot mode to activate.</param>
  596. private void OnPivotModeButtonClicked(HandlePivotMode mode)
  597. {
  598. EditorApplication.ActivePivotMode = mode;
  599. editorSettingsHash = EditorSettings.Hash;
  600. }
  601. /// <summary>
  602. /// Triggered when the move snap button is toggled.
  603. /// </summary>
  604. /// <param name="active">Determins should be move snap be activated or deactivated.</param>
  605. private void OnMoveSnapToggled(bool active)
  606. {
  607. Handles.MoveHandleSnapActive = active;
  608. editorSettingsHash = EditorSettings.Hash;
  609. }
  610. /// <summary>
  611. /// Triggered when the move snap increment value changes.
  612. /// </summary>
  613. /// <param name="value">Value that determines in what increments to perform move snapping.</param>
  614. private void OnMoveSnapValueChanged(float value)
  615. {
  616. Handles.MoveSnapAmount = MathEx.Clamp(value, 0.01f, 1000.0f);
  617. editorSettingsHash = EditorSettings.Hash;
  618. }
  619. /// <summary>
  620. /// Triggered when the rotate snap button is toggled.
  621. /// </summary>
  622. /// <param name="active">Determins should be rotate snap be activated or deactivated.</param>
  623. private void OnRotateSnapToggled(bool active)
  624. {
  625. Handles.RotateHandleSnapActive = active;
  626. editorSettingsHash = EditorSettings.Hash;
  627. }
  628. /// <summary>
  629. /// Triggered when the rotate snap increment value changes.
  630. /// </summary>
  631. /// <param name="value">Value that determines in what increments to perform rotate snapping.</param>
  632. private void OnRotateSnapValueChanged(float value)
  633. {
  634. Handles.RotateSnapAmount = (Degree)MathEx.Clamp(value, 0.01f, 360.0f);
  635. editorSettingsHash = EditorSettings.Hash;
  636. }
  637. /// <summary>
  638. /// Updates toggle button states according to current editor options. This is useful if tools, coordinate mode,
  639. /// pivot or other scene view options have been modified externally.
  640. /// </summary>
  641. private void UpdateButtonStates()
  642. {
  643. switch (EditorApplication.ActiveSceneTool)
  644. {
  645. case SceneViewTool.View:
  646. viewButton.Value = true;
  647. break;
  648. case SceneViewTool.Move:
  649. moveButton.Value = true;
  650. break;
  651. case SceneViewTool.Rotate:
  652. rotateButton.Value = true;
  653. break;
  654. case SceneViewTool.Scale:
  655. scaleButton.Value = true;
  656. break;
  657. }
  658. switch (EditorApplication.ActiveCoordinateMode)
  659. {
  660. case HandleCoordinateMode.Local:
  661. localCoordButton.Value = true;
  662. break;
  663. case HandleCoordinateMode.World:
  664. worldCoordButton.Value = true;
  665. break;
  666. }
  667. switch (EditorApplication.ActivePivotMode)
  668. {
  669. case HandlePivotMode.Center:
  670. centerButton.Value = true;
  671. break;
  672. case HandlePivotMode.Pivot:
  673. pivotButton.Value = true;
  674. break;
  675. }
  676. if (Handles.MoveHandleSnapActive)
  677. moveSnapButton.Value = true;
  678. else
  679. moveSnapButton.Value = false;
  680. moveSnapInput.Value = Handles.MoveSnapAmount;
  681. if (Handles.RotateHandleSnapActive)
  682. rotateSnapButton.Value = true;
  683. else
  684. rotateSnapButton.Value = false;
  685. moveSnapInput.Value = Handles.RotateSnapAmount.Degrees;
  686. }
  687. /// <summary>
  688. /// Activates or deactivates the profiler overlay according to current editor settings.
  689. /// </summary>
  690. private void UpdateProfilerOverlay()
  691. {
  692. if (EditorSettings.GetBool(ProfilerOverlayActiveKey))
  693. {
  694. if (activeProfilerOverlay == null)
  695. {
  696. SceneObject profilerSO = new SceneObject("EditorProfilerOverlay");
  697. profilerCamera = profilerSO.AddComponent<Camera>();
  698. profilerCamera.Target = renderTexture;
  699. profilerCamera.ClearFlags = ClearFlags.None;
  700. profilerCamera.Priority = 1;
  701. profilerCamera.Layers = 0;
  702. profilerCamera.HDR = false;
  703. activeProfilerOverlay = profilerSO.AddComponent<ProfilerOverlay>();
  704. }
  705. }
  706. else
  707. {
  708. if (activeProfilerOverlay != null)
  709. {
  710. activeProfilerOverlay.SceneObject.Destroy();
  711. activeProfilerOverlay = null;
  712. profilerCamera = null;
  713. }
  714. }
  715. }
  716. /// <summary>
  717. /// Creates the scene camera and updates the render texture. Should be called at least once before using the
  718. /// scene view. Should be called whenever the window is resized.
  719. /// </summary>
  720. /// <param name="width">Width of the scene render target, in pixels.</param>
  721. /// <param name="height">Height of the scene render target, in pixels.</param>
  722. private void UpdateRenderTexture(int width, int height)
  723. {
  724. width = MathEx.Max(20, width);
  725. height = MathEx.Max(20, height);
  726. // Note: Depth buffer is required because ScenePicking uses it
  727. renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height, 1, false, true);
  728. renderTexture.Priority = 1;
  729. if (camera == null)
  730. {
  731. SceneObject sceneCameraSO = new SceneObject("SceneCamera", true);
  732. camera = sceneCameraSO.AddComponent<Camera>();
  733. camera.Target = renderTexture;
  734. camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  735. sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
  736. sceneCameraSO.LookAt(new Vector3(0, 0.5f, 0));
  737. camera.Priority = 2;
  738. camera.NearClipPlane = 0.05f;
  739. camera.FarClipPlane = 2500.0f;
  740. camera.ClearColor = ClearColor;
  741. camera.Layers = UInt64.MaxValue & ~SceneAxesHandle.LAYER; // Don't draw scene axes in this camera
  742. cameraController = sceneCameraSO.AddComponent<SceneCamera>();
  743. renderTextureGUI = new GUIRenderTexture(renderTexture);
  744. rtPanel.AddElement(renderTextureGUI);
  745. sceneGrid = new SceneGrid(camera);
  746. sceneSelection = new SceneSelection(camera);
  747. sceneGizmos = new SceneGizmos(camera);
  748. sceneHandles = new SceneHandles(this, camera);
  749. }
  750. else
  751. {
  752. camera.Target = renderTexture;
  753. renderTextureGUI.RenderTexture = renderTexture;
  754. }
  755. Rect2I rtBounds = new Rect2I(0, 0, width, height);
  756. renderTextureGUI.Bounds = rtBounds;
  757. focusCatcher.Bounds = GUIUtility.CalculateBounds(rtPanel, GUI);
  758. sceneAxesGUI.SetPosition(width - HandleAxesGUISize - HandleAxesGUIPaddingX, HandleAxesGUIPaddingY);
  759. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  760. // render target destroy/create cycle for every single pixel.
  761. camera.AspectRatio = width / (float)height;
  762. if (profilerCamera != null)
  763. profilerCamera.Target = renderTexture;
  764. }
  765. /// <summary>
  766. /// Parses an array of scene objects and removes elements that are children of elements that are also in the array.
  767. /// </summary>
  768. /// <param name="objects">Array containing duplicate objects as input, and array without duplicate objects as
  769. /// output.</param>
  770. private void CleanDuplicates(ref SceneObject[] objects)
  771. {
  772. List<SceneObject> cleanList = new List<SceneObject>();
  773. for (int i = 0; i < objects.Length; i++)
  774. {
  775. bool foundParent = false;
  776. for (int j = 0; j < objects.Length; j++)
  777. {
  778. SceneObject elem = objects[i];
  779. while (elem != null && elem != objects[j])
  780. elem = elem.Parent;
  781. bool isChildOf = elem == objects[j];
  782. if (i != j && isChildOf)
  783. {
  784. foundParent = true;
  785. break;
  786. }
  787. }
  788. if (!foundParent)
  789. cleanList.Add(objects[i]);
  790. }
  791. objects = cleanList.ToArray();
  792. }
  793. }
  794. /** @} */
  795. }