BsHandleDrawManager.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. /** Queues all the handle draw commands queued since the last call to clear() for rendering. */
  132. void draw(const CameraPtr& camera);
  133. /** Clears all handle draw commands. */
  134. void clear();
  135. private:
  136. friend class HandleDrawManagerCore;
  137. /**
  138. * Initializes the core thread portion of the draw manager.
  139. *
  140. * @param[in] wireMat Material used for drawing the wireframe objects.
  141. * @param[in] solidMat Material used for drawing the solid objects.
  142. * @param[in] clearMat Material used for clearing the alpha channel in the empty areas.
  143. */
  144. void initializeCore(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat,
  145. const SPtr<MaterialCore>& clearMat);
  146. /** Destroys the core thread portion of the draw manager. */
  147. void destroyCore(HandleDrawManagerCore* core);
  148. /** Destroys all meshes allocated since the last call to this method. */
  149. void clearMeshes();
  150. static const UINT32 SPHERE_QUALITY;
  151. static const UINT32 WIRE_SPHERE_QUALITY;
  152. static const UINT32 ARC_QUALITY;
  153. Vector<Vector<DrawHelper::ShapeMeshData>> mActiveMeshes;
  154. UINT64 mLastFrameIdx;
  155. Matrix4 mTransform;
  156. std::atomic<HandleDrawManagerCore*> mCore;
  157. DrawHelper* mDrawHelper;
  158. };
  159. /** Core thread specific portion of the HandleDrawManager that handles actual rendering. */
  160. class BS_ED_EXPORT HandleDrawManagerCore
  161. {
  162. /** Contains information about the material used for drawing solid objects and its parameters. */
  163. struct SolidMaterialData
  164. {
  165. SPtr<MaterialCore> mat;
  166. GpuParamMat4Core mViewProj;
  167. GpuParamVec4Core mViewDir;
  168. };
  169. /** Contains information about the material used for drawing wireframe objects and its parameters. */
  170. struct WireMaterialData
  171. {
  172. SPtr<MaterialCore> mat;
  173. GpuParamMat4Core mViewProj;
  174. };
  175. /** Contains information about the material used for clearing the alpha channel in the empty areas. */
  176. struct ClearAlphaMaterialData
  177. {
  178. SPtr<MaterialCore> mat;
  179. };
  180. /** Type of mesh that can be drawn. */
  181. enum class MeshType
  182. {
  183. Solid, Wire
  184. };
  185. /** Data about a mesh rendered by the draw manager. */
  186. struct MeshData
  187. {
  188. MeshData(const SPtr<MeshCoreBase>& mesh, MeshType type)
  189. :mesh(mesh), type(type)
  190. { }
  191. SPtr<MeshCoreBase> mesh;
  192. MeshType type;
  193. };
  194. /** Data about a camera and the meshes that are queued for rendering on it */
  195. struct QueuedData
  196. {
  197. SPtr<CameraCore> camera;
  198. Vector<MeshData> meshes;
  199. };
  200. struct PrivatelyConstruct { };
  201. public:
  202. HandleDrawManagerCore(const PrivatelyConstruct& dummy) { }
  203. ~HandleDrawManagerCore();
  204. private:
  205. friend class HandleDrawManager;
  206. /**
  207. * Initializes the object. Must be called right after construction.
  208. *
  209. * @param[in] wireMat Material used for drawing the wireframe objects.
  210. * @param[in] solidMat Material used for drawing the solid objects.
  211. * @param[in] clearMat Material used for clearing the alpha channel in the empty areas.
  212. */
  213. void initialize(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat,
  214. const SPtr<MaterialCore>& clearMat);
  215. /**
  216. * Queues new data for rendering.
  217. *
  218. * @param[in] camera Camera to render to.
  219. * @param[in] meshes Meshes to render.
  220. */
  221. void queueForDraw(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes);
  222. /** Deletes any meshes queued for rendering. */
  223. void clearQueued();
  224. /** Callback triggered by the renderer. Draws all queued meshes. */
  225. void render(UINT32 queuedDataIdx);
  226. Vector<QueuedData> mQueuedData;
  227. // Immutable
  228. SolidMaterialData mSolidMaterial;
  229. WireMaterialData mWireMaterial;
  230. WireMaterialData mClearMaterial;
  231. };
  232. }