2
0

BsHandleDrawManager.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "BsHandleDrawManager.h"
  2. #include "BsDrawHelper.h"
  3. #include "BsMaterial.h"
  4. #include "BsBuiltinEditorResources.h"
  5. #include "BsCoreThread.h"
  6. #include "BsRendererManager.h"
  7. #include "BsCoreRenderer.h"
  8. #include "BsTransientMesh.h"
  9. #include "BsCamera.h"
  10. #include "BsRendererUtility.h"
  11. #include "BsSceneObject.h"
  12. using namespace std::placeholders;
  13. namespace BansheeEngine
  14. {
  15. const UINT32 HandleDrawManager::SPHERE_QUALITY = 1;
  16. const UINT32 HandleDrawManager::WIRE_SPHERE_QUALITY = 10;
  17. const UINT32 HandleDrawManager::ARC_QUALITY = 10;
  18. HandleDrawManager::HandleDrawManager()
  19. :mCore(nullptr)
  20. {
  21. mTransform = Matrix4::IDENTITY;
  22. mDrawHelper = bs_new<DrawHelper>();
  23. HMaterial solidMaterial = BuiltinEditorResources::instance().createSolidHandleMat();
  24. HMaterial wireMaterial = BuiltinEditorResources::instance().createWireHandleMat();
  25. SPtr<MaterialCore> solidMaterialProxy = solidMaterial->getCore();
  26. SPtr<MaterialCore> wireMaterialProxy = wireMaterial->getCore();
  27. mCore.store(bs_new<HandleDrawManagerCore>(HandleDrawManagerCore::PrivatelyConstruct()), std::memory_order_release);
  28. gCoreAccessor().queueCommand(std::bind(&HandleDrawManager::initializeCore, this, wireMaterialProxy, solidMaterialProxy));
  29. }
  30. HandleDrawManager::~HandleDrawManager()
  31. {
  32. bs_delete(mDrawHelper);
  33. gCoreAccessor().queueCommand(std::bind(&HandleDrawManager::destroyCore, this, mCore.load(std::memory_order_relaxed)));
  34. }
  35. void HandleDrawManager::initializeCore(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat)
  36. {
  37. THROW_IF_NOT_CORE_THREAD;
  38. mCore.load(std::memory_order_acquire)->initialize(wireMat, solidMat);
  39. }
  40. void HandleDrawManager::destroyCore(HandleDrawManagerCore* core)
  41. {
  42. THROW_IF_NOT_CORE_THREAD;
  43. bs_delete(core);
  44. }
  45. void HandleDrawManager::setColor(const Color& color)
  46. {
  47. mDrawHelper->setColor(color);
  48. }
  49. void HandleDrawManager::setTransform(const Matrix4& transform)
  50. {
  51. mTransform = transform;
  52. }
  53. void HandleDrawManager::drawCube(const Vector3& position, const Vector3& extents, float size)
  54. {
  55. Matrix4 scale = Matrix4::scaling(size);
  56. mDrawHelper->setTransform(mTransform * scale);
  57. mDrawHelper->cube(position, extents);
  58. }
  59. void HandleDrawManager::drawSphere(const Vector3& position, float radius, float size)
  60. {
  61. Matrix4 scale = Matrix4::scaling(size);
  62. mDrawHelper->setTransform(mTransform * scale);
  63. mDrawHelper->sphere(position, radius);
  64. }
  65. void HandleDrawManager::drawWireCube(const Vector3& position, const Vector3& extents, float size)
  66. {
  67. Matrix4 scale = Matrix4::scaling(size);
  68. mDrawHelper->setTransform(mTransform * scale);
  69. mDrawHelper->wireCube(position, extents);
  70. }
  71. void HandleDrawManager::drawWireSphere(const Vector3& position, float radius, float size)
  72. {
  73. Matrix4 scale = Matrix4::scaling(size);
  74. mDrawHelper->setTransform(mTransform * scale);
  75. mDrawHelper->wireSphere(position, radius);
  76. }
  77. void HandleDrawManager::drawCone(const Vector3& base, const Vector3& normal, float height, float radius, float size)
  78. {
  79. Matrix4 scale = Matrix4::scaling(size);
  80. mDrawHelper->setTransform(mTransform * scale);
  81. mDrawHelper->cone(base, normal, height, radius);
  82. }
  83. void HandleDrawManager::drawLine(const Vector3& start, const Vector3& end, float size)
  84. {
  85. Matrix4 scale = Matrix4::scaling(size);
  86. mDrawHelper->setTransform(mTransform * scale);
  87. mDrawHelper->line(start, end);
  88. }
  89. void HandleDrawManager::drawDisc(const Vector3& position, const Vector3& normal, float radius, float size)
  90. {
  91. Matrix4 scale = Matrix4::scaling(size);
  92. mDrawHelper->setTransform(mTransform * scale);
  93. mDrawHelper->disc(position, normal, radius);
  94. }
  95. void HandleDrawManager::drawWireDisc(const Vector3& position, const Vector3& normal, float radius, float size)
  96. {
  97. Matrix4 scale = Matrix4::scaling(size);
  98. mDrawHelper->setTransform(mTransform * scale);
  99. mDrawHelper->wireDisc(position, normal, radius);
  100. }
  101. void HandleDrawManager::drawArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size)
  102. {
  103. Matrix4 scale = Matrix4::scaling(size);
  104. mDrawHelper->setTransform(mTransform * scale);
  105. mDrawHelper->arc(position, normal, radius, startAngle, amountAngle);
  106. }
  107. void HandleDrawManager::drawWireArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size)
  108. {
  109. Matrix4 scale = Matrix4::scaling(size);
  110. mDrawHelper->setTransform(mTransform * scale);
  111. mDrawHelper->wireArc(position, normal, radius, startAngle, amountAngle);
  112. }
  113. void HandleDrawManager::drawRect(const Rect3& area, float size)
  114. {
  115. Matrix4 scale = Matrix4::scaling(size);
  116. mDrawHelper->setTransform(mTransform * scale);
  117. mDrawHelper->rectangle(area);
  118. }
  119. void HandleDrawManager::draw(const CameraPtr& camera)
  120. {
  121. mDrawHelper->clearMeshes();
  122. mDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera->getPosition());
  123. const Vector<DrawHelper::ShapeMeshData>& meshes = mDrawHelper->getMeshes();
  124. Vector<HandleDrawManagerCore::MeshData> proxyData;
  125. for (auto& meshData : meshes)
  126. {
  127. if (meshData.type == DrawHelper::MeshType::Solid)
  128. {
  129. proxyData.push_back(HandleDrawManagerCore::MeshData(
  130. meshData.mesh->getCore(), HandleDrawManagerCore::MeshType::Solid));
  131. }
  132. else // Wire
  133. {
  134. proxyData.push_back(HandleDrawManagerCore::MeshData(
  135. meshData.mesh->getCore(), HandleDrawManagerCore::MeshType::Wire));
  136. }
  137. }
  138. HandleDrawManagerCore* core = mCore.load(std::memory_order_relaxed);
  139. gCoreAccessor().queueCommand(std::bind(&HandleDrawManagerCore::updateData, core,
  140. camera->getCore(), proxyData));
  141. mDrawHelper->clear();
  142. }
  143. HandleDrawManagerCore::~HandleDrawManagerCore()
  144. {
  145. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  146. if (mCamera != nullptr)
  147. activeRenderer->_unregisterRenderCallback(mCamera.get(), 20);
  148. }
  149. void HandleDrawManagerCore::initialize(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat)
  150. {
  151. {
  152. mWireMaterial.mat = wireMat;
  153. SPtr<GpuParamsCore> vertParams = wireMat->getPassParameters(0)->mVertParams;
  154. vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
  155. }
  156. {
  157. mSolidMaterial.mat = solidMat;
  158. SPtr<GpuParamsCore> vertParams = solidMat->getPassParameters(0)->mVertParams;
  159. SPtr<GpuParamsCore> fragParams = solidMat->getPassParameters(0)->mFragParams;
  160. vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
  161. fragParams->getParam("viewDir", mSolidMaterial.mViewDir);
  162. }
  163. }
  164. void HandleDrawManagerCore::updateData(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes)
  165. {
  166. if (mCamera != camera)
  167. {
  168. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  169. if (mCamera != nullptr)
  170. activeRenderer->_unregisterRenderCallback(mCamera.get(), 20);
  171. if (camera != nullptr)
  172. activeRenderer->_registerRenderCallback(camera.get(), 20, std::bind(&HandleDrawManagerCore::render, this));
  173. }
  174. mCamera = camera;
  175. mMeshes = meshes;
  176. }
  177. void HandleDrawManagerCore::render()
  178. {
  179. THROW_IF_NOT_CORE_THREAD;
  180. if (mCamera == nullptr)
  181. return;
  182. SPtr<RenderTargetCore> renderTarget = mCamera->getViewport()->getTarget();
  183. float width = (float)renderTarget->getProperties().getWidth();
  184. float height = (float)renderTarget->getProperties().getHeight();
  185. Rect2 normArea = mCamera->getViewport()->getNormArea();
  186. Rect2I screenArea;
  187. screenArea.x = (int)(normArea.x * width);
  188. screenArea.y = (int)(normArea.y * height);
  189. screenArea.width = (int)(normArea.width * width);
  190. screenArea.height = (int)(normArea.height * height);
  191. Matrix4 viewProjMat = mCamera->getProjectionMatrixRS() * mCamera->getViewMatrix();
  192. mSolidMaterial.mViewProj.set(viewProjMat);
  193. mSolidMaterial.mViewDir.set((Vector4)mCamera->getForward());
  194. mWireMaterial.mViewProj.set(viewProjMat);
  195. MeshType currentType = MeshType::Solid;
  196. if (mMeshes.size() > 0)
  197. {
  198. currentType = mMeshes[0].type;
  199. if (currentType == MeshType::Solid)
  200. gRendererUtility().setPass(mSolidMaterial.mat, 0);
  201. else
  202. gRendererUtility().setPass(mWireMaterial.mat, 0);
  203. }
  204. for (auto& meshData : mMeshes)
  205. {
  206. if (currentType != meshData.type)
  207. {
  208. if (meshData.type == MeshType::Solid)
  209. gRendererUtility().setPass(mSolidMaterial.mat, 0);
  210. else
  211. gRendererUtility().setPass(mWireMaterial.mat, 0);
  212. currentType = meshData.type;
  213. }
  214. gRendererUtility().draw(meshData.mesh, meshData.mesh->getProperties().getSubMesh(0));
  215. }
  216. }
  217. }