BsHandleDrawManager.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "Renderer/BsRendererExtension.h"
  6. #include "RenderAPI/BsGpuParams.h"
  7. #include "Utility/BsDrawHelper.h"
  8. #include "Renderer/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. */
  139. void drawText(const Vector3& position, const String& text, const HFont& font, UINT32 fontSize = 16);
  140. /** Queues all the handle draw commands queued since the last call to clear() for rendering. */
  141. void draw(const SPtr<Camera>& camera);
  142. /** Clears all handle draw commands. */
  143. void clear();
  144. private:
  145. friend class ct::HandleRenderer;
  146. /** Destroys all meshes allocated since the last call to this method. */
  147. void clearMeshes();
  148. static const UINT32 SPHERE_QUALITY;
  149. static const UINT32 WIRE_SPHERE_QUALITY;
  150. static const UINT32 ARC_QUALITY;
  151. Vector<Vector<DrawHelper::ShapeMeshData>> mActiveMeshes;
  152. UINT64 mLastFrameIdx;
  153. Matrix4 mTransform;
  154. SPtr<ct::HandleRenderer> mRenderer;
  155. DrawHelper* mDrawHelper;
  156. };
  157. /** @} */
  158. namespace ct
  159. {
  160. /** @addtogroup Handles-Internal
  161. * @{
  162. */
  163. BS_PARAM_BLOCK_BEGIN(HandleParamBlockDef)
  164. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatViewProj)
  165. BS_PARAM_BLOCK_ENTRY(Vector4, gViewDir)
  166. BS_PARAM_BLOCK_ENTRY(float, gInvViewportWidth)
  167. BS_PARAM_BLOCK_ENTRY(float, gInvViewportHeight)
  168. BS_PARAM_BLOCK_ENTRY(float, gViewportYFlip)
  169. BS_PARAM_BLOCK_END
  170. extern HandleParamBlockDef gHandleParamBlockDef;
  171. /** Performs rendering of handles on the core thread, managed by the parent HandleDrawManager. */
  172. class BS_ED_EXPORT HandleRenderer : public RendererExtension
  173. {
  174. /** Type of mesh that can be drawn. */
  175. enum class MeshType
  176. {
  177. Solid, Line, Text, Count
  178. };
  179. /** Data about a mesh rendered by the draw manager. */
  180. struct MeshData
  181. {
  182. MeshData(const SPtr<Mesh>& mesh, const SubMesh& subMesh, SPtr<Texture> texture, MeshType type)
  183. :mesh(mesh), subMesh(subMesh), texture(texture), type(type)
  184. { }
  185. SPtr<Mesh> mesh;
  186. SubMesh subMesh;
  187. SPtr<Texture> texture;
  188. UINT32 paramIdx;
  189. MeshType type;
  190. };
  191. /** Data about a camera and the meshes that are queued for rendering on it */
  192. struct QueuedData
  193. {
  194. SPtr<Camera> camera;
  195. Vector<MeshData> meshes;
  196. };
  197. /** Data used for initializing the renderer. */
  198. struct InitData
  199. {
  200. SPtr<Material> lineMat;
  201. SPtr<Material> solidMat;
  202. SPtr<Material> textMat;
  203. SPtr<Material> clearMat;
  204. };
  205. public:
  206. HandleRenderer();
  207. private:
  208. friend class bs::HandleDrawManager;
  209. /** @copydoc RendererExtension::initialize */
  210. void initialize(const Any& data) override;
  211. /** @copydoc RendererExtension::destroy */
  212. void destroy() override;
  213. /** @copydoc RendererExtension::check */
  214. bool check(const Camera& camera) override;
  215. /** @copydoc RendererExtension::render */
  216. void render(const Camera& camera) override;
  217. /**
  218. * Queues new data for rendering.
  219. *
  220. * @param[in] camera Camera to render to.
  221. * @param[in] meshes Meshes to render.
  222. */
  223. void queueForDraw(const SPtr<Camera>& camera, Vector<MeshData>& meshes);
  224. /** Deletes any meshes queued for rendering. */
  225. void clearQueued();
  226. Vector<QueuedData> mQueuedData;
  227. SPtr<GpuParamBlockBuffer> mParamBuffer;
  228. Vector<SPtr<GpuParamsSet>> mParamSets[(UINT32)MeshType::Count];
  229. UINT32 mTypeCounters[(UINT32)MeshType::Count];
  230. // Immutable
  231. SPtr<Material> mMaterials[(UINT32)MeshType::Count];
  232. SPtr<Material> mClearMaterial;
  233. };
  234. /** @} */
  235. }
  236. }