BsHandleDrawManager.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "BsRendererExtension.h"
  6. #include "BsGpuParams.h"
  7. #include "BsDrawHelper.h"
  8. #include "BsParamBlocks.h"
  9. namespace bs
  10. {
  11. /** @addtogroup Handles
  12. * @{
  13. */
  14. namespace ct { class HandleRenderer; }
  15. /**
  16. * Allows you to easily draw various kinds of simple shapes, primarily used for drawing handles in the scene view.
  17. *
  18. * Drawn elements only persist for a single draw() call and need to be re-queued after.
  19. */
  20. class BS_ED_EXPORT HandleDrawManager
  21. {
  22. public:
  23. HandleDrawManager();
  24. ~HandleDrawManager();
  25. /** Sets the color of all the following draw* calls. */
  26. void setColor(const Color& color);
  27. /** Sets the transform matrix that will be applied to all following draw* calls. */
  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. * Draws a solid cuboid.
  36. *
  37. * @param[in] position Center of the cuboid.
  38. * @param[in] extents Radius of the cuboid in all directions.
  39. * @param[in] size Uniform scale of the object.
  40. */
  41. void drawCube(const Vector3& position, const Vector3& extents, float size = 1.0f);
  42. /**
  43. * Draws a solid sphere.
  44. *
  45. * @param[in] position Center of the sphere.
  46. * @param[in] radius Radius of the sphere.
  47. * @param[in] size Uniform scale of the object.
  48. */
  49. void drawSphere(const Vector3& position, float radius, float size = 1.0f);
  50. /**
  51. * Draws a wireframe cuboid.
  52. *
  53. * @param[in] position Center of the cuboid.
  54. * @param[in] extents Radius of the cuboid in all directions.
  55. * @param[in] size Uniform scale of the object.
  56. */
  57. void drawWireCube(const Vector3& position, const Vector3& extents, float size = 1.0f);
  58. /**
  59. * Draws a wireframe sphere.
  60. *
  61. * @param[in] position Center of the sphere.
  62. * @param[in] radius Radius of the sphere.
  63. * @param[in] size Uniform scale of the object.
  64. */
  65. void drawWireSphere(const Vector3& position, float radius, float size = 1.0f);
  66. /**
  67. * Draws a solid cone.
  68. *
  69. * @param[in] base Position of the center of the base of the cone.
  70. * @param[in] normal Orientation of the cone, pointing from center base to the tip of the cone.
  71. * @param[in] height Height of the cone (along the normal).
  72. * @param[in] radius Radius of the base of the cone.
  73. * @param[in] 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. * Draws a line.
  78. *
  79. * @param[in] start Starting point for the line.
  80. * @param[in] end Ending point for the line.
  81. * @param[in] size Uniform scale of the object.
  82. */
  83. void drawLine(const Vector3& start, const Vector3& end, float size = 1.0f);
  84. /**
  85. * Draws a double-sided solid disc.
  86. *
  87. * @param[in] position Center of the disc.
  88. * @param[in] normal Orientation of the disc, pointing in the direction the disc is visible in.
  89. * @param[in] radius Radius of the disc.
  90. * @param[in] size Uniform scale of the object.
  91. */
  92. void drawDisc(const Vector3& position, const Vector3& normal, float radius, float size = 1.0f);
  93. /**
  94. * Draws a wireframe disc.
  95. *
  96. * @param[in] position Center of the disc.
  97. * @param[in] normal Orientation of the disc, pointing in the direction the disc is visible in.
  98. * @param[in] radius Radius of the disc.
  99. * @param[in] size Uniform scale of the object.
  100. */
  101. void drawWireDisc(const Vector3& position, const Vector3& normal, float radius, float size = 1.0f);
  102. /**
  103. * Draws a double-sided solid arc.
  104. *
  105. * @param[in] position Center of the arc.
  106. * @param[in] normal Orientation of the arc, pointing in the direction the arc is visible in.
  107. * @param[in] radius Radius of the arc.
  108. * @param[in] startAngle Angle at which to start the arc.
  109. * @param[in] amountAngle Length of the arc.
  110. * @param[in] 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. * Draws a wireframe arc.
  115. *
  116. * @param[in] position Center of the arc.
  117. * @param[in] normal Orientation of the arc, pointing in the direction the arc is visible in.
  118. * @param[in] radius Radius of the arc.
  119. * @param[in] startAngle Angle at which to start the arc.
  120. * @param[in] amountAngle Length of the arc.
  121. * @param[in] 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. * Draws a double-sided solid rectangle.
  126. *
  127. * @param[in] area Position and size of the rectangle.
  128. * @param[in] 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 SPtr<Camera>& camera);
  143. /** Clears all handle draw commands. */
  144. void clear();
  145. private:
  146. friend class ct::HandleRenderer;
  147. /** Destroys all meshes allocated since the last call to this method. */
  148. void clearMeshes();
  149. static const UINT32 SPHERE_QUALITY;
  150. static const UINT32 WIRE_SPHERE_QUALITY;
  151. static const UINT32 ARC_QUALITY;
  152. Vector<Vector<DrawHelper::ShapeMeshData>> mActiveMeshes;
  153. UINT64 mLastFrameIdx;
  154. Matrix4 mTransform;
  155. SPtr<ct::HandleRenderer> mRenderer;
  156. DrawHelper* mDrawHelper;
  157. };
  158. /** @} */
  159. namespace ct
  160. {
  161. /** @addtogroup Handles-Internal
  162. * @{
  163. */
  164. BS_PARAM_BLOCK_BEGIN(HandleParamBlockDef)
  165. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatViewProj)
  166. BS_PARAM_BLOCK_ENTRY(Vector4, gViewDir)
  167. BS_PARAM_BLOCK_END
  168. extern HandleParamBlockDef gHandleParamBlockDef;
  169. /** Performs rendering of handles on the core thread, managed by the parent HandleDrawManager. */
  170. class BS_ED_EXPORT HandleRenderer : public RendererExtension
  171. {
  172. /** Type of mesh that can be drawn. */
  173. enum class MeshType
  174. {
  175. Solid, Line, Text, Count
  176. };
  177. /** Data about a mesh rendered by the draw manager. */
  178. struct MeshData
  179. {
  180. MeshData(const SPtr<MeshBase>& mesh, SPtr<Texture> texture, MeshType type)
  181. :mesh(mesh), texture(texture), type(type)
  182. { }
  183. SPtr<MeshBase> mesh;
  184. SPtr<Texture> texture;
  185. UINT32 paramIdx;
  186. MeshType type;
  187. };
  188. /** Data about a camera and the meshes that are queued for rendering on it */
  189. struct QueuedData
  190. {
  191. SPtr<Camera> camera;
  192. Vector<MeshData> meshes;
  193. };
  194. /** Data used for initializing the renderer. */
  195. struct InitData
  196. {
  197. SPtr<Material> lineMat;
  198. SPtr<Material> solidMat;
  199. SPtr<Material> textMat;
  200. SPtr<Material> clearMat;
  201. };
  202. public:
  203. HandleRenderer();
  204. private:
  205. friend class HandleDrawManager;
  206. /** @copydoc RendererExtension::initialize */
  207. void initialize(const Any& data) override;
  208. /** @copydoc RendererExtension::destroy */
  209. void destroy() override;
  210. /** @copydoc RendererExtension::check */
  211. bool check(const Camera& camera) override;
  212. /** @copydoc RendererExtension::render */
  213. void render(const Camera& camera) override;
  214. /**
  215. * Queues new data for rendering.
  216. *
  217. * @param[in] camera Camera to render to.
  218. * @param[in] meshes Meshes to render.
  219. */
  220. void queueForDraw(const SPtr<Camera>& camera, Vector<MeshData>& meshes);
  221. /** Deletes any meshes queued for rendering. */
  222. void clearQueued();
  223. Vector<QueuedData> mQueuedData;
  224. SPtr<GpuParamBlockBuffer> mParamBuffer;
  225. Vector<SPtr<GpuParamsSet>> mParamSets[(UINT32)MeshType::Count];
  226. UINT32 mTypeCounters[(UINT32)MeshType::Count];
  227. // Immutable
  228. SPtr<Material> mMaterials[(UINT32)MeshType::Count];
  229. SPtr<Material> mClearMaterial;
  230. };
  231. /** @} */
  232. }
  233. }