BsHandleDrawManager.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #pragma once
  2. #include "BsEditorPrerequisites.h"
  3. #include "BsGpuParams.h"
  4. #include "BsDrawHelper.h"
  5. namespace BansheeEngine
  6. {
  7. class HandleDrawManagerCore;
  8. /**
  9. * @brief Allows you to easily draw various kinds of simple shapes, primarily
  10. * used for drawing handles in the scene view.
  11. *
  12. * Drawn elements only persist for a single draw call and need to be re-queued
  13. * after.
  14. */
  15. class BS_ED_EXPORT HandleDrawManager
  16. {
  17. public:
  18. HandleDrawManager();
  19. ~HandleDrawManager();
  20. /**
  21. * @brief Sets the color of all the following draw* calls.
  22. */
  23. void setColor(const Color& color);
  24. /**
  25. * @brief Sets the transform matrix that will be applied to all
  26. * following draw* calls.
  27. */
  28. void setTransform(const Matrix4& transform);
  29. /**
  30. * Sets the layer bitfield that controls whether a handle is considered visible in a specific camera. Handle layer
  31. * must match camera layer in order for the camera to render it
  32. */
  33. void setLayer(UINT64 layer);
  34. /**
  35. * @brief Draws a solid cuboid.
  36. *
  37. * @param position Center of the cuboid.
  38. * @param extents Radius of the cuboid in all directions.
  39. * @param size Uniform scale of the object.
  40. */
  41. void drawCube(const Vector3& position, const Vector3& extents, float size = 1.0f);
  42. /**
  43. * @brief Draws a solid sphere.
  44. *
  45. * @param position Center of the sphere.
  46. * @param radius Radius of the sphere.
  47. * @param size Uniform scale of the object.
  48. */
  49. void drawSphere(const Vector3& position, float radius, float size = 1.0f);
  50. /**
  51. * @brief Draws a wireframe cuboid.
  52. *
  53. * @param position Center of the cuboid.
  54. * @param extents Radius of the cuboid in all directions.
  55. * @param size Uniform scale of the object.
  56. */
  57. void drawWireCube(const Vector3& position, const Vector3& extents, float size = 1.0f);
  58. /**
  59. * @brief Draws a wireframe sphere.
  60. *
  61. * @param position Center of the sphere.
  62. * @param radius Radius of the sphere.
  63. * @param size Uniform scale of the object.
  64. */
  65. void drawWireSphere(const Vector3& position, float radius, float size = 1.0f);
  66. /**
  67. * @brief Draws a solid cone.
  68. *
  69. * @param base Position of the center of the base of the cone.
  70. * @param normal Orientation of the cone, pointing from center base to the tip of the cone.
  71. * @param height Height of the cone (along the normal).
  72. * @param radius Radius of the base of the cone.
  73. * @param size Uniform scale of the object.
  74. */
  75. void drawCone(const Vector3& base, const Vector3& normal, float height, float radius, float size = 1.0f);
  76. /**
  77. * @brief Draws a line.
  78. *
  79. * @param start Starting point for the line.
  80. * @param end Ending point for the line.
  81. * @param size Uniform scale of the object.
  82. */
  83. void drawLine(const Vector3& start, const Vector3& end, float size = 1.0f);
  84. /**
  85. * @brief Draws a double-sided solid disc.
  86. *
  87. * @param position Center of the disc.
  88. * @param normal Orientation of the disc, pointing in the direction the disc is visible in.
  89. * @param radius Radius of the disc.
  90. * @param size Uniform scale of the object.
  91. */
  92. void drawDisc(const Vector3& position, const Vector3& normal, float radius, float size = 1.0f);
  93. /**
  94. * @brief Draws a wireframe disc.
  95. *
  96. * @param position Center of the disc.
  97. * @param normal Orientation of the disc, pointing in the direction the disc is visible in.
  98. * @param radius Radius of the disc.
  99. * @param size Uniform scale of the object.
  100. */
  101. void drawWireDisc(const Vector3& position, const Vector3& normal, float radius, float size = 1.0f);
  102. /**
  103. * @brief Draws a double-sided solid arc.
  104. *
  105. * @param position Center of the arc.
  106. * @param normal Orientation of the arc, pointing in the direction the arc is visible in.
  107. * @param radius Radius of the arc.
  108. * @param startAngle Angle at which to start the arc.
  109. * @param amountAngle Length of the arc.
  110. * @param size Uniform scale of the object.
  111. */
  112. void drawArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size = 1.0f);
  113. /**
  114. * @brief Draws a wireframe arc.
  115. *
  116. * @param position Center of the arc.
  117. * @param normal Orientation of the arc, pointing in the direction the arc is visible in.
  118. * @param radius Radius of the arc.
  119. * @param startAngle Angle at which to start the arc.
  120. * @param amountAngle Length of the arc.
  121. * @param size Uniform scale of the object.
  122. */
  123. void drawWireArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size = 1.0f);
  124. /**
  125. * @brief Draws a double-sided solid rectangle.
  126. *
  127. * @param area Position and size of the rectangle.
  128. * @param size Uniform scale of the object.
  129. */
  130. void drawRect(const Rect3& area, float size = 1.0f);
  131. /**
  132. * Draws a mesh representing 2D text with the specified properties.
  133. *
  134. * @param[in] position Position to render the text at. Text will be centered around this point.
  135. * @param[in] text Text to draw.
  136. * @param[in] font Font to use for rendering the text's characters.
  137. * @param[in] fontSize Size of the characters, in points.
  138. * @param[in] size Uniform scale of the rendered mesh.
  139. */
  140. void drawText(const Vector3& position, const WString& text, const HFont& font, UINT32 fontSize = 16, float size = 1.0f);
  141. /** Queues all the handle draw commands queued since the last call to clear() for rendering. */
  142. void draw(const CameraPtr& camera);
  143. /** Clears all handle draw commands. */
  144. void clear();
  145. private:
  146. friend class HandleDrawManagerCore;
  147. /**
  148. * Initializes the core thread portion of the draw manager.
  149. *
  150. * @param[in] wireMat Material used for drawing the wireframe objects.
  151. * @param[in] solidMat Material used for drawing the solid objects.
  152. *@param[in] textMat Material used for drawing the text.
  153. * @param[in] clearMat Material used for clearing the alpha channel in the empty areas.
  154. */
  155. void initializeCore(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat,
  156. const SPtr<MaterialCore>& textMat, const SPtr<MaterialCore>& clearMat);
  157. /** Destroys the core thread portion of the draw manager. */
  158. void destroyCore(HandleDrawManagerCore* core);
  159. /** Destroys all meshes allocated since the last call to this method. */
  160. void clearMeshes();
  161. static const UINT32 SPHERE_QUALITY;
  162. static const UINT32 WIRE_SPHERE_QUALITY;
  163. static const UINT32 ARC_QUALITY;
  164. Vector<Vector<DrawHelper::ShapeMeshData>> mActiveMeshes;
  165. UINT64 mLastFrameIdx;
  166. Matrix4 mTransform;
  167. std::atomic<HandleDrawManagerCore*> mCore;
  168. DrawHelper* mDrawHelper;
  169. };
  170. /** Core thread specific portion of the HandleDrawManager that handles actual rendering. */
  171. class BS_ED_EXPORT HandleDrawManagerCore
  172. {
  173. /** Contains information about the material used for drawing solid objects and its parameters. */
  174. struct SolidMaterialData
  175. {
  176. SPtr<MaterialCore> mat;
  177. GpuParamMat4Core viewProj;
  178. GpuParamVec4Core viewDir;
  179. };
  180. /** Contains information about the material used for drawing wireframe objects and its parameters. */
  181. struct WireMaterialData
  182. {
  183. SPtr<MaterialCore> mat;
  184. GpuParamMat4Core viewProj;
  185. };
  186. /** Contains information about the material used for drawing text and its parameters. */
  187. struct TextMaterialData
  188. {
  189. SPtr<MaterialCore> mat;
  190. GpuParamMat4Core viewProj;
  191. GpuParamTextureCore texture;
  192. };
  193. /** Contains information about the material used for clearing the alpha channel in the empty areas. */
  194. struct ClearAlphaMaterialData
  195. {
  196. SPtr<MaterialCore> mat;
  197. };
  198. /** Type of mesh that can be drawn. */
  199. enum class MeshType
  200. {
  201. Solid, Wire, Text
  202. };
  203. /** Data about a mesh rendered by the draw manager. */
  204. struct MeshData
  205. {
  206. MeshData(const SPtr<MeshCoreBase>& mesh, SPtr<TextureCore> texture, MeshType type)
  207. :mesh(mesh), texture(texture), type(type)
  208. { }
  209. SPtr<MeshCoreBase> mesh;
  210. SPtr<TextureCore> texture;
  211. MeshType type;
  212. };
  213. /** Data about a camera and the meshes that are queued for rendering on it */
  214. struct QueuedData
  215. {
  216. SPtr<CameraCore> camera;
  217. Vector<MeshData> meshes;
  218. };
  219. struct PrivatelyConstruct { };
  220. public:
  221. HandleDrawManagerCore(const PrivatelyConstruct& dummy) { }
  222. ~HandleDrawManagerCore();
  223. private:
  224. friend class HandleDrawManager;
  225. /**
  226. * Initializes the object. Must be called right after construction.
  227. *
  228. * @param[in] wireMat Material used for drawing the wireframe objects.
  229. * @param[in] solidMat Material used for drawing the solid objects.
  230. * @param[in] textMat Material used for drawing the text.
  231. * @param[in] clearMat Material used for clearing the alpha channel in the empty areas.
  232. */
  233. void initialize(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat,
  234. const SPtr<MaterialCore>& textMat, const SPtr<MaterialCore>& clearMat);
  235. /**
  236. * Queues new data for rendering.
  237. *
  238. * @param[in] camera Camera to render to.
  239. * @param[in] meshes Meshes to render.
  240. */
  241. void queueForDraw(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes);
  242. /** Deletes any meshes queued for rendering. */
  243. void clearQueued();
  244. /** Callback triggered by the renderer. Draws all queued meshes. */
  245. void render(UINT32 queuedDataIdx);
  246. Vector<QueuedData> mQueuedData;
  247. // Immutable
  248. SolidMaterialData mSolidMaterial;
  249. WireMaterialData mWireMaterial;
  250. TextMaterialData mTextMaterial;
  251. WireMaterialData mClearMaterial;
  252. };
  253. }