BsGizmoManager.h 19 KB

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