BsHandleDrawManager.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. 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 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<HandleRenderer> mRenderer;
  156. DrawHelper* mDrawHelper;
  157. };
  158. /** @} */
  159. /** @addtogroup Handles-Internal
  160. * @{
  161. */
  162. BS_PARAM_BLOCK_BEGIN(HandleParamBlockDef)
  163. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatViewProj)
  164. BS_PARAM_BLOCK_ENTRY(Vector4, gViewDir)
  165. BS_PARAM_BLOCK_END
  166. extern HandleParamBlockDef gHandleParamBlockDef;
  167. /** Performs rendering of handles on the core thread, managed by the parent HandleDrawManager. */
  168. class BS_ED_EXPORT HandleRenderer : public RendererExtension
  169. {
  170. /** Type of mesh that can be drawn. */
  171. enum class MeshType
  172. {
  173. Solid, Line, Text, Count
  174. };
  175. /** Data about a mesh rendered by the draw manager. */
  176. struct MeshData
  177. {
  178. MeshData(const SPtr<MeshCoreBase>& mesh, SPtr<TextureCore> texture, MeshType type)
  179. :mesh(mesh), texture(texture), type(type)
  180. { }
  181. SPtr<MeshCoreBase> mesh;
  182. SPtr<TextureCore> texture;
  183. UINT32 paramIdx;
  184. MeshType type;
  185. };
  186. /** Data about a camera and the meshes that are queued for rendering on it */
  187. struct QueuedData
  188. {
  189. SPtr<CameraCore> camera;
  190. Vector<MeshData> meshes;
  191. };
  192. /** Data used for initializing the renderer. */
  193. struct InitData
  194. {
  195. SPtr<MaterialCore> lineMat;
  196. SPtr<MaterialCore> solidMat;
  197. SPtr<MaterialCore> textMat;
  198. SPtr<MaterialCore> clearMat;
  199. };
  200. public:
  201. HandleRenderer();
  202. private:
  203. friend class HandleDrawManager;
  204. /** @copydoc RendererExtension::initialize */
  205. void initialize(const Any& data) override;
  206. /** @copydoc RendererExtension::destroy */
  207. void destroy() override;
  208. /** @copydoc RendererExtension::check */
  209. bool check(const CameraCore& camera) override;
  210. /** @copydoc RendererExtension::render */
  211. void render(const CameraCore& camera) override;
  212. /**
  213. * Queues new data for rendering.
  214. *
  215. * @param[in] camera Camera to render to.
  216. * @param[in] meshes Meshes to render.
  217. */
  218. void queueForDraw(const SPtr<CameraCore>& camera, Vector<MeshData>& meshes);
  219. /** Deletes any meshes queued for rendering. */
  220. void clearQueued();
  221. Vector<QueuedData> mQueuedData;
  222. SPtr<GpuParamBlockBufferCore> mParamBuffer;
  223. Vector<SPtr<GpuParamsSetCore>> mParamSets[(UINT32)MeshType::Count];
  224. UINT32 mTypeCounters[(UINT32)MeshType::Count];
  225. // Immutable
  226. SPtr<MaterialCore> mMaterials[(UINT32)MeshType::Count];
  227. SPtr<MaterialCore> mClearMaterial;
  228. };
  229. /** @} */
  230. }