BsGizmoManager.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsEditorPrerequisites.h"
  5. #include "BsModule.h"
  6. #include "BsColor.h"
  7. #include "BsVector2I.h"
  8. #include "BsMatrix4.h"
  9. #include "BsGpuParam.h"
  10. #include "BsDrawHelper.h"
  11. namespace BansheeEngine
  12. {
  13. /** @addtogroup Scene-Editor
  14. * @{
  15. */
  16. class GizmoManagerCore;
  17. /**
  18. * Handles the rendering and picking of gizmo elements. Gizmos are icons and 3D objects usually rendered in scene view
  19. * for various visualization purposes (for example a Camera component will have a gizmo that draws a Camera icon since
  20. * otherwise it has no visual representation). Aside from being rendered, gizmos can also be selected by the user as if
  21. * they were normal scene elements.
  22. */
  23. class BS_ED_EXPORT GizmoManager : public Module<GizmoManager>
  24. {
  25. public:
  26. GizmoManager();
  27. ~GizmoManager();
  28. /**
  29. * Starts gizmo creation. All further call will be referencing this gizmo. Must be followed by a matching
  30. * endGizmo().
  31. *
  32. * @param gizmoParent Scene object this gizmo is attached to. Selecting the gizmo will select this scene object.
  33. */
  34. void startGizmo(const HSceneObject& gizmoParent);
  35. /** Ends gizmo creation. Must be called after a matching startGizmo(). */
  36. void endGizmo();
  37. /** Changes the color of any further gizmo draw calls. */
  38. void setColor(const Color& color);
  39. /** Changes the transform that will be applied to meshes of any further gizmo draw calls. */
  40. void setTransform(const Matrix4& transform);
  41. /**
  42. * If pickable is set to true, gizmo can be selected by the user, otherwise it will be drawn but cannot be
  43. * interacted with.
  44. *
  45. * @note Must be called between startGizmo/endGizmo calls.
  46. */
  47. void setPickable(bool pickable) { mPickable = pickable; }
  48. /** Returns the currently set gizmo color. */
  49. Color getColor() const { return mColor; }
  50. /** Returns the currently set gizmo transform. */
  51. Matrix4 getTransform() const { return mTransform; }
  52. /**
  53. * Draws an axis aligned cuboid.
  54. *
  55. * @param[in] position Center of the cuboid.
  56. * @param[in] extents Radius of the cuboid in each axis.
  57. *
  58. * @note Must be called between startGizmo/endGizmo calls.
  59. */
  60. void drawCube(const Vector3& position, const Vector3& extents);
  61. /**
  62. * Draws a sphere.
  63. *
  64. * @note Must be called between startGizmo/endGizmo calls.
  65. */
  66. void drawSphere(const Vector3& position, float radius);
  67. /**
  68. * Draws a solid cone.
  69. *
  70. * @param[in] base Position of the center of the base of the cone.
  71. * @param[in] normal Orientation of the cone, pointing from center base to the tip of the cone.
  72. * @param[in] height Height of the cone (along the normal).
  73. * @param[in] radius Radius of the base of the cone.
  74. * @param[in] scale Scale applied to cone's disc width & height. Allows you to create elliptical cones.
  75. */
  76. void drawCone(const Vector3& base, const Vector3& normal, float height, float radius,
  77. const Vector2& scale = Vector2::ONE);
  78. /**
  79. * Draws a wireframe axis aligned cuboid.
  80. *
  81. * @param[in] position Center of the cuboid.
  82. * @param[in] extents Radius of the cuboid in each axis.
  83. *
  84. * @note Must be called between startGizmo/endGizmo calls.
  85. */
  86. void drawWireCube(const Vector3& position, const Vector3& extents);
  87. /**
  88. * Draws a wireframe sphere represented by three discs.
  89. *
  90. * @note Must be called between startGizmo/endGizmo calls.
  91. */
  92. void drawWireSphere(const Vector3& position, float radius);
  93. /**
  94. * Draws a wireframe capsule.
  95. *
  96. * @param[in] position World coordinates of the center of the capsule.
  97. * @param[in] height Distance between the centers of the capsule's hemispheres.
  98. * @param[in] radius Distance of each point from the capsule's center-line.
  99. */
  100. void drawWireCapsule(const Vector3& position, float height, float radius);
  101. /**
  102. * Draws a wireframe cone.
  103. *
  104. * @param[in] base Position of the center of the base of the cone.
  105. * @param[in] normal Orientation of the cone, pointing from center base to the tip of the cone.
  106. * @param[in] height Height of the cone (along the normal).
  107. * @param[in] radius Radius of the base of the cone.
  108. * @param[in] scale Scale applied to cone's disc width & height. Allows you to create elliptical cones.
  109. */
  110. void drawWireCone(const Vector3& base, const Vector3& normal, float height, float radius,
  111. const Vector2& scale = Vector2::ONE);
  112. /**
  113. * Draws a line between two points.
  114. *
  115. * @note Must be called between startGizmo/endGizmo calls.
  116. */
  117. void drawLine(const Vector3& start, const Vector3& end);
  118. /**
  119. * Draws a list of lines. Provided array must contain pairs of the line start point followed by an end point.
  120. *
  121. * @note Must be called between startGizmo/endGizmo calls.
  122. */
  123. void drawLineList(const Vector<Vector3>& linePoints);
  124. /**
  125. * Draws a wireframe disc.
  126. *
  127. * @param[in] position Center of the disc.
  128. * @param[in] normal Orientation of the disc, pointing in the direction the disc is visible in.
  129. * @param[in] radius Radius of the disc.
  130. */
  131. void drawWireDisc(const Vector3& position, const Vector3& normal, float radius);
  132. /**
  133. * Draws a wireframe arc.
  134. *
  135. * @param[in] position Center of the arc.
  136. * @param[in] normal Orientation of the arc, pointing in the direction the arc is visible in.
  137. * @param[in] radius Radius of the arc.
  138. * @param[in] startAngle Angle at which to start the arc.
  139. * @param[in] amountAngle Length of the arc.
  140. */
  141. void drawWireArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle);
  142. /**
  143. * Draws a wireframe mesh.
  144. *
  145. * @param[in] meshData Object containing mesh vertices and indices. Vertices must be Vertex3 and indices
  146. * 32-bit.
  147. */
  148. void drawWireMesh(const SPtr<MeshData>& meshData);
  149. /**
  150. * Draws a wireframe frustum.
  151. *
  152. * @param[in] position Origin of the frustum, or the eye point.
  153. * @param[in] aspect Ratio of frustum width over frustum height.
  154. * @param[in] FOV Horizontal field of view in degrees.
  155. * @param[in] near Distance to the near frustum plane.
  156. * @param[in] far Distance to the far frustum plane.
  157. *
  158. * @note Must be called between startGizmo/endGizmo calls.
  159. */
  160. void drawFrustum(const Vector3& position, float aspect, Degree FOV, float near, float far);
  161. /**
  162. * Draws an icon that always faces the camera.
  163. *
  164. * @param[in] position Position of the center of the icon.
  165. * @param[in] image Sprite image for the icon to draw.
  166. * @param[in] fixedScale If true then the icon will remain the same size regardless of the distance from camera.
  167. *
  168. * @note Must be called between startGizmo/endGizmo calls.
  169. */
  170. void drawIcon(Vector3 position, HSpriteTexture image, bool fixedScale);
  171. /**
  172. * Draws a mesh representing 2D text with the specified properties.
  173. *
  174. * @param[in] position Position to render the text at. Text will be centered around this point.
  175. * @param[in] text Text to draw.
  176. * @param[in] font Font to use for rendering the text's characters.
  177. * @param[in] fontSize Size of the characters, in points.
  178. */
  179. void drawText(const Vector3& position, const WString& text, const HFont& font, UINT32 fontSize = 16);
  180. /**
  181. * Clears all gizmo data, but doesn't update the meshes or the render data. (Calling update would create empty
  182. * meshes, but before calling update gizmos will still be drawn).
  183. */
  184. void clearGizmos();
  185. /**
  186. * Clears gizmo render data like meshes, but doesn't clear the original gizmo data (Calling update would just
  187. * recreate the render data).
  188. */
  189. void clearRenderData();
  190. /**
  191. * Returns a scene object that was attached to a specific gizmo.
  192. *
  193. * @param[in] gizmoIdx Index of the gizmo to look for.
  194. */
  195. HSceneObject getSceneObject(UINT32 gizmoIdx);
  196. /** @name Internal
  197. * @{
  198. */
  199. /**
  200. * Updates all the gizmo meshes to reflect all draw calls submitted since clearGizmos().
  201. *
  202. * @note Internal method.
  203. */
  204. void update(const SPtr<Camera>& camera);
  205. /**
  206. * Queues all gizmos to be rendered for picking. Each gizmo is draw with a separate color so we can identify them
  207. * later.
  208. *
  209. * @param[in] camera Camera to draw the gizmos on.
  210. * @param[in] idxToColorCallback Callback that assigns a unique color to each gizmo index.
  211. *
  212. * @note Internal method.
  213. */
  214. void renderForPicking(const SPtr<Camera>& camera, std::function<Color(UINT32)> idxToColorCallback);
  215. /** @} */
  216. private:
  217. friend class GizmoManagerCore;
  218. /** Supported types of gizmo materials (shaders) */
  219. enum class GizmoMaterial
  220. {
  221. Solid, Wire, Line, Picking, PickingAlpha, Text
  222. };
  223. /** Common data shared by all gizmo types. */
  224. struct CommonData
  225. {
  226. UINT32 idx;
  227. Color color;
  228. Matrix4 transform;
  229. HSceneObject sceneObject;
  230. bool pickable;
  231. };
  232. /** Data required for rendering a cuboid gizmo. */
  233. struct CubeData : CommonData
  234. {
  235. Vector3 position;
  236. Vector3 extents;
  237. };
  238. /** Data required for rendering a sphere gizmo. */
  239. struct SphereData : CommonData
  240. {
  241. Vector3 position;
  242. float radius;
  243. };
  244. /** Data required for rendering a cone gizmo. */
  245. struct ConeData : CommonData
  246. {
  247. Vector3 base;
  248. Vector3 normal;
  249. float radius;
  250. float height;
  251. Vector2 scale;
  252. };
  253. /** Data required for rendering a line gizmo. */
  254. struct LineData : CommonData
  255. {
  256. Vector3 start;
  257. Vector3 end;
  258. };
  259. /** Data required for rendering a list of lines. */
  260. struct LineListData : CommonData
  261. {
  262. Vector<Vector3> linePoints;
  263. };
  264. /** Data required for rendering a wireframe disc gizmo. */
  265. struct WireDiscData : CommonData
  266. {
  267. Vector3 position;
  268. Vector3 normal;
  269. float radius;
  270. };
  271. /** Data required for rendering a wireframe arc gizmo. */
  272. struct WireArcData : CommonData
  273. {
  274. Vector3 position;
  275. Vector3 normal;
  276. float radius;
  277. Degree startAngle;
  278. Degree amountAngle;
  279. };
  280. /** Data required for rendering a wireframe mesh gizmo. */
  281. struct WireMeshData : CommonData
  282. {
  283. SPtr<MeshData> meshData;
  284. };
  285. /** Data required for rendering a frustum gizmo. */
  286. struct FrustumData : CommonData
  287. {
  288. Vector3 position;
  289. float aspect;
  290. Degree FOV;
  291. float near;
  292. float far;
  293. };
  294. /** Data required for rendering an icon gizmo. */
  295. struct IconData : CommonData
  296. {
  297. Vector3 position;
  298. bool fixedScale;
  299. HSpriteTexture texture;
  300. };
  301. /** Data required for rendering text. */
  302. struct TextData : CommonData
  303. {
  304. Vector3 position;
  305. WString text;
  306. HFont font;
  307. UINT32 fontSize;
  308. };
  309. /** Stores how many icons use a specific texture. */
  310. struct IconRenderData
  311. {
  312. UINT32 count;
  313. SPtr<TextureCore> texture;
  314. };
  315. /** Data used for initializing the core thread equivalent of the gizmo manager. */
  316. struct CoreInitData
  317. {
  318. SPtr<MaterialCore> solidMat;
  319. SPtr<MaterialCore> wireMat;
  320. SPtr<MaterialCore> lineMat;
  321. SPtr<MaterialCore> iconMat;
  322. SPtr<MaterialCore> textMat;
  323. SPtr<MaterialCore> pickingMat;
  324. SPtr<MaterialCore> alphaPickingMat;
  325. };
  326. typedef Vector<IconRenderData> IconRenderDataVec;
  327. typedef SPtr<IconRenderDataVec> IconRenderDataVecPtr;
  328. /**
  329. * Builds a brand new mesh that can be used for rendering all icon gizmos.
  330. *
  331. * @param[in] camera Camera the mesh will be rendered to.
  332. * @param[in] iconData A list of all icons and their properties.
  333. * @param[in] forPicking Whether the icons will be rendered normally, or with a special material for picking.
  334. * @param[in] renderData Output data that outlines the structure of the returned mesh. It tells us which portions
  335. * of the mesh use which icon texture.
  336. *
  337. * @return A mesh containing all of the visible icons. Mesh is allocated using the icon mesh heap
  338. * and should be deallocated manually.
  339. */
  340. SPtr<TransientMesh> buildIconMesh(const SPtr<Camera>& camera, const Vector<IconData>& iconData, bool forPicking, IconRenderDataVecPtr& renderData);
  341. /** Resizes the icon width/height so it is always scaled to optimal size (with preserved aspect). */
  342. void limitIconSize(UINT32& width, UINT32& height);
  343. /**
  344. * Calculates colors for an icon based on its position in the camera. For example icons too close to too far might
  345. * be faded.
  346. *
  347. * @param[in] tint Primary tint for the icon.
  348. * @param[in] camera Camera in which the icon will be rendered in.
  349. * @param[in] iconHeight Height of the icon in pixels.
  350. * @param[in] fixedScale Whether the icon size changes depending on distance from the camera.
  351. * @param[in] normalColor Normal color of the icon.
  352. * @param[in] fadedColor Faded color to be used when icon is occluded by geometry.
  353. */
  354. void calculateIconColors(const Color& tint, const SPtr<Camera>& camera, UINT32 iconHeight, bool fixedScale,
  355. Color& normalColor, Color& fadedColor);
  356. /** Initializes the core thread version of the gizmo manager. */
  357. void initializeCore(const CoreInitData& initData);
  358. /** Destroys the core thread version of the gizmo manager. */
  359. void destroyCore(GizmoManagerCore* core);
  360. static const UINT32 VERTEX_BUFFER_GROWTH;
  361. static const UINT32 INDEX_BUFFER_GROWTH;
  362. static const UINT32 SPHERE_QUALITY;
  363. static const UINT32 WIRE_SPHERE_QUALITY;
  364. static const float MAX_ICON_RANGE;
  365. static const UINT32 OPTIMAL_ICON_SIZE;
  366. static const float ICON_TEXEL_WORLD_SIZE;
  367. typedef Set<IconData, std::function<bool(const IconData&, const IconData&)>> IconSet;
  368. Color mColor;
  369. Matrix4 mTransform;
  370. HSceneObject mActiveSO;
  371. bool mPickable;
  372. UINT32 mCurrentIdx;
  373. bool mTransformDirty;
  374. bool mColorDirty;
  375. DrawHelper* mDrawHelper;
  376. DrawHelper* mPickingDrawHelper;
  377. Vector<CubeData> mSolidCubeData;
  378. Vector<CubeData> mWireCubeData;
  379. Vector<SphereData> mSolidSphereData;
  380. Vector<SphereData> mWireSphereData;
  381. Vector<ConeData> mSolidConeData;
  382. Vector<ConeData> mWireConeData;
  383. Vector<LineData> mLineData;
  384. Vector<LineListData> mLineListData;
  385. Vector<WireDiscData> mWireDiscData;
  386. Vector<WireArcData> mWireArcData;
  387. Vector<WireMeshData> mWireMeshData;
  388. Vector<FrustumData> mFrustumData;
  389. Vector<IconData> mIconData;
  390. Vector<TextData> mTextData;
  391. Map<UINT32, HSceneObject> mIdxToSceneObjectMap;
  392. Vector<DrawHelper::ShapeMeshData> mActiveMeshes;
  393. SPtr<MeshHeap> mIconMeshHeap;
  394. SPtr<TransientMesh> mIconMesh;
  395. std::atomic<GizmoManagerCore*> mCore;
  396. // Immutable
  397. SPtr<VertexDataDesc> mIconVertexDesc;
  398. // Transient
  399. struct SortedIconData
  400. {
  401. float distance;
  402. Vector2I screenPosition;
  403. UINT32 iconIdx;
  404. };
  405. Vector<SortedIconData> mSortedIconData;
  406. };
  407. /** @} */
  408. /** @addtogroup Scene-Editor-Internal
  409. * @{
  410. */
  411. /**
  412. * Core thread version of the gizmo manager that handles most of the rendering of meshes provided by the gizmo manager.
  413. */
  414. class GizmoManagerCore
  415. {
  416. friend class GizmoManager;
  417. /** Solid gizmo material and parameter handles. */
  418. struct SolidMaterialData
  419. {
  420. SPtr<MaterialCore> mat;
  421. SPtr<GpuParamsSetCore> params;
  422. GpuParamMat4Core viewProj;
  423. GpuParamVec4Core viewDir;
  424. };
  425. /** Wire gizmo material and parameter handles. */
  426. struct WireMaterialData
  427. {
  428. SPtr<MaterialCore> mat;
  429. SPtr<GpuParamsSetCore> params;
  430. GpuParamMat4Core viewProj;
  431. };
  432. /** Icon gizmo material and parameter handles. */
  433. struct IconMaterialData
  434. {
  435. SPtr<MaterialCore> mat;
  436. SPtr<GpuParamsSetCore> params;
  437. GpuParamMat4Core viewProj[2];
  438. GpuParamTextureCore texture[2];
  439. };
  440. /** Text gizmo material and parameter handles. */
  441. struct TextMaterialData
  442. {
  443. SPtr<MaterialCore> mat;
  444. SPtr<GpuParamsSetCore> params;
  445. GpuParamMat4Core viewProj;
  446. GpuParamTextureCore texture;
  447. };
  448. /** Gizmo material and parameter handles used for picking. */
  449. struct PickingMaterialData
  450. {
  451. SPtr<MaterialCore> mat;
  452. SPtr<GpuParamsSetCore> params;
  453. GpuParamMat4Core viewProj;
  454. };
  455. /**
  456. * Gizmo material and parameter handles used for picking, with blending support (generally used for icon picking).
  457. */
  458. struct AlphaPickingMaterialData
  459. {
  460. SPtr<MaterialCore> mat;
  461. SPtr<GpuParamsSetCore> params;
  462. GpuParamMat4Core viewProj;
  463. GpuParamTextureCore texture;
  464. };
  465. /** Type of mesh that can be drawn. */
  466. enum class MeshType
  467. {
  468. Solid, Line, Wire, Text
  469. };
  470. /** Data about a mesh rendered by the draw manager. */
  471. struct MeshData
  472. {
  473. MeshData(const SPtr<MeshCoreBase>& mesh, SPtr<TextureCore> texture, MeshType type)
  474. :mesh(mesh), texture(texture), type(type)
  475. { }
  476. SPtr<MeshCoreBase> mesh;
  477. SPtr<TextureCore> texture;
  478. MeshType type;
  479. };
  480. struct PrivatelyConstuct { };
  481. public:
  482. GizmoManagerCore(const PrivatelyConstuct& dummy);
  483. ~GizmoManagerCore();
  484. private:
  485. /** Initializes the core gizmo manager. Must be called right after creation. */
  486. void initialize(const GizmoManager::CoreInitData& initData);
  487. /** Renders all gizmos in the parent camera. */
  488. void render();
  489. /**
  490. * Renders a non-icon gizmo mesh using the provided parameters.
  491. *
  492. * @param[in] viewMatrix View matrix of the camera we are rendering with.
  493. * @param[in] projMatrix Projection matrix of the camera we are rendering with.
  494. * @param[in] viewDir View direction of the camera we are rendering with.
  495. * @param[in] mesh Mesh to render. This is normally the solid or wireframe gizmo mesh.
  496. * @param[in] texture Texture to apply to the material, if the material supports a texture.
  497. * @param[in] material Material to use for rendering. This is normally the solid, wireframe or picking material.
  498. */
  499. void renderGizmos(const Matrix4& viewMatrix, const Matrix4& projMatrix, const Vector3& viewDir,
  500. const SPtr<MeshCoreBase>& mesh, const SPtr<TextureCore>& texture, GizmoManager::GizmoMaterial material);
  501. /**
  502. * Renders the icon gizmo mesh using the provided parameters.
  503. *
  504. * @param[in] screenArea Area of the viewport to render the gizmos in, in pixels.
  505. * @param[in] mesh Mesh containing the icons.
  506. * @param[in] renderData Icon render data outlining which parts of the icon mesh use which textures.
  507. * @param[in] usePickingMaterial Should the icons be rendered normally or for picking.
  508. */
  509. void renderIconGizmos(Rect2I screenArea, SPtr<MeshCoreBase> mesh, GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial);
  510. /**
  511. * Updates the internal data that is used for rendering. Normally you would call this after updating the camera or
  512. * meshes on the sim thread.
  513. *
  514. * @param[in] camera Sets the camera all rendering will be performed to.
  515. * @param[in] meshes Meshes to render.
  516. * @param[in] iconMesh Mesh containing icon meshes.
  517. * @param[in] iconRenderData Icon render data outlining which parts of the icon mesh use which textures.
  518. */
  519. void updateData(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes, const SPtr<MeshCoreBase>& iconMesh,
  520. const GizmoManager::IconRenderDataVecPtr& iconRenderData);
  521. static const float PICKING_ALPHA_CUTOFF;
  522. SPtr<CameraCore> mCamera;
  523. Vector<MeshData> mMeshes;
  524. SPtr<MeshCoreBase> mIconMesh;
  525. GizmoManager::IconRenderDataVecPtr mIconRenderData;
  526. // Immutable
  527. SolidMaterialData mSolidMaterial;
  528. SolidMaterialData mWireMaterial;
  529. WireMaterialData mLineMaterial;
  530. IconMaterialData mIconMaterial;
  531. TextMaterialData mTextMaterial;
  532. PickingMaterialData mPickingMaterial;
  533. AlphaPickingMaterialData mAlphaPickingMaterial;
  534. };
  535. /** @} */
  536. }