BsGizmoManager.h 19 KB

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