BsGizmoManager.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGizmoManager.h"
  4. #include "BsMesh.h"
  5. #include "BsAABox.h"
  6. #include "BsSphere.h"
  7. #include "BsVertexDataDesc.h"
  8. #include "BsShapeMeshes3D.h"
  9. #include "BsMeshHeap.h"
  10. #include "BsCCamera.h"
  11. #include "BsSpriteTexture.h"
  12. #include "BsCoreThread.h"
  13. #include "BsBuiltinEditorResources.h"
  14. #include "BsMaterial.h"
  15. #include "BsGpuParams.h"
  16. #include "BsRenderAPI.h"
  17. #include "BsCoreRenderer.h"
  18. #include "BsRendererUtility.h"
  19. #include "BsTransientMesh.h"
  20. #include "BsRendererManager.h"
  21. #include "BsDrawHelper.h"
  22. using namespace std::placeholders;
  23. namespace BansheeEngine
  24. {
  25. const UINT32 GizmoManager::VERTEX_BUFFER_GROWTH = 4096;
  26. const UINT32 GizmoManager::INDEX_BUFFER_GROWTH = 4096 * 2;
  27. const UINT32 GizmoManager::SPHERE_QUALITY = 1;
  28. const UINT32 GizmoManager::WIRE_SPHERE_QUALITY = 10;
  29. const float GizmoManager::MAX_ICON_RANGE = 500.0f;
  30. const UINT32 GizmoManager::OPTIMAL_ICON_SIZE = 64;
  31. const float GizmoManager::ICON_TEXEL_WORLD_SIZE = 0.05f;
  32. GizmoManager::GizmoManager()
  33. :mPickable(false), mDrawHelper(nullptr), mPickingDrawHelper(nullptr), mCore(nullptr), mCurrentIdx(0)
  34. {
  35. mTransform = Matrix4::IDENTITY;
  36. mDrawHelper = bs_new<DrawHelper>();
  37. mPickingDrawHelper = bs_new<DrawHelper>();
  38. mIconVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  39. mIconVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  40. mIconVertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD);
  41. mIconVertexDesc->addVertElem(VET_COLOR, VES_COLOR, 0);
  42. mIconVertexDesc->addVertElem(VET_COLOR, VES_COLOR, 1);
  43. mIconMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mIconVertexDesc);
  44. HMaterial solidMaterial = BuiltinEditorResources::instance().createSolidGizmoMat();
  45. HMaterial wireMaterial = BuiltinEditorResources::instance().createWireGizmoMat();
  46. HMaterial iconMaterial = BuiltinEditorResources::instance().createIconGizmoMat();
  47. HMaterial textMaterial = BuiltinEditorResources::instance().createTextGizmoMat();
  48. HMaterial pickingMaterial = BuiltinEditorResources::instance().createGizmoPickingMat();
  49. HMaterial alphaPickingMaterial = BuiltinEditorResources::instance().createAlphaGizmoPickingMat();
  50. CoreInitData initData;
  51. initData.solidMat = solidMaterial->getCore();
  52. initData.wireMat = wireMaterial->getCore();
  53. initData.iconMat = iconMaterial->getCore();
  54. initData.textMat = textMaterial->getCore();
  55. initData.pickingMat = pickingMaterial->getCore();
  56. initData.alphaPickingMat = alphaPickingMaterial->getCore();
  57. mCore.store(bs_new<GizmoManagerCore>(GizmoManagerCore::PrivatelyConstuct()), std::memory_order_release);
  58. gCoreAccessor().queueCommand(std::bind(&GizmoManager::initializeCore, this, initData));
  59. }
  60. GizmoManager::~GizmoManager()
  61. {
  62. mDrawHelper->clearMeshes(mActiveMeshes);
  63. mActiveMeshes.clear();
  64. if (mIconMesh != nullptr)
  65. mIconMeshHeap->dealloc(mIconMesh);
  66. bs_delete(mDrawHelper);
  67. bs_delete(mPickingDrawHelper);
  68. gCoreAccessor().queueCommand(std::bind(&GizmoManager::destroyCore, this, mCore.load(std::memory_order_relaxed)));
  69. }
  70. void GizmoManager::initializeCore(const CoreInitData& initData)
  71. {
  72. mCore.load(std::memory_order_acquire)->initialize(initData);
  73. }
  74. void GizmoManager::destroyCore(GizmoManagerCore* core)
  75. {
  76. bs_delete(core);
  77. }
  78. void GizmoManager::startGizmo(const HSceneObject& gizmoParent)
  79. {
  80. mActiveSO = gizmoParent;
  81. }
  82. void GizmoManager::endGizmo()
  83. {
  84. mActiveSO = nullptr;
  85. }
  86. void GizmoManager::setColor(const Color& color)
  87. {
  88. mDrawHelper->setColor(color);
  89. mColor = color;
  90. }
  91. void GizmoManager::setTransform(const Matrix4& transform)
  92. {
  93. mDrawHelper->setTransform(transform);
  94. mTransform = transform;
  95. }
  96. void GizmoManager::drawCube(const Vector3& position, const Vector3& extents)
  97. {
  98. mSolidCubeData.push_back(CubeData());
  99. CubeData& cubeData = mSolidCubeData.back();
  100. cubeData.idx = mCurrentIdx++;
  101. cubeData.position = position;
  102. cubeData.extents = extents;
  103. cubeData.color = mColor;
  104. cubeData.transform = mTransform;
  105. cubeData.sceneObject = mActiveSO;
  106. cubeData.pickable = mPickable;
  107. mDrawHelper->cube(position, extents);
  108. mIdxToSceneObjectMap[cubeData.idx] = mActiveSO;
  109. }
  110. void GizmoManager::drawSphere(const Vector3& position, float radius)
  111. {
  112. mSolidSphereData.push_back(SphereData());
  113. SphereData& sphereData = mSolidSphereData.back();
  114. sphereData.idx = mCurrentIdx++;
  115. sphereData.position = position;
  116. sphereData.radius = radius;
  117. sphereData.color = mColor;
  118. sphereData.transform = mTransform;
  119. sphereData.sceneObject = mActiveSO;
  120. sphereData.pickable = mPickable;
  121. mDrawHelper->sphere(position, radius);
  122. mIdxToSceneObjectMap[sphereData.idx] = mActiveSO;
  123. }
  124. void GizmoManager::drawWireCube(const Vector3& position, const Vector3& extents)
  125. {
  126. mWireCubeData.push_back(CubeData());
  127. CubeData& cubeData = mWireCubeData.back();
  128. cubeData.idx = mCurrentIdx++;
  129. cubeData.position = position;
  130. cubeData.extents = extents;
  131. cubeData.color = mColor;
  132. cubeData.transform = mTransform;
  133. cubeData.sceneObject = mActiveSO;
  134. cubeData.pickable = mPickable;
  135. mDrawHelper->wireCube(position, extents);
  136. mIdxToSceneObjectMap[cubeData.idx] = mActiveSO;
  137. }
  138. void GizmoManager::drawWireSphere(const Vector3& position, float radius)
  139. {
  140. mWireSphereData.push_back(SphereData());
  141. SphereData& sphereData = mWireSphereData.back();
  142. sphereData.idx = mCurrentIdx++;
  143. sphereData.position = position;
  144. sphereData.radius = radius;
  145. sphereData.color = mColor;
  146. sphereData.transform = mTransform;
  147. sphereData.sceneObject = mActiveSO;
  148. sphereData.pickable = mPickable;
  149. mDrawHelper->wireSphere(position, radius);
  150. mIdxToSceneObjectMap[sphereData.idx] = mActiveSO;
  151. }
  152. void GizmoManager::drawLine(const Vector3& start, const Vector3& end)
  153. {
  154. mLineData.push_back(LineData());
  155. LineData& lineData = mLineData.back();
  156. lineData.idx = mCurrentIdx++;
  157. lineData.start = start;
  158. lineData.end = end;
  159. lineData.color = mColor;
  160. lineData.transform = mTransform;
  161. lineData.sceneObject = mActiveSO;
  162. lineData.pickable = mPickable;
  163. mDrawHelper->line(start, end);
  164. mIdxToSceneObjectMap[lineData.idx] = mActiveSO;
  165. }
  166. void GizmoManager::drawLineList(const Vector<Vector3>& linePoints)
  167. {
  168. mLineListData.push_back(LineListData());
  169. LineListData& lineListData = mLineListData.back();
  170. lineListData.idx = mCurrentIdx++;
  171. lineListData.linePoints = linePoints;
  172. lineListData.color = mColor;
  173. lineListData.transform = mTransform;
  174. lineListData.sceneObject = mActiveSO;
  175. lineListData.pickable = mPickable;
  176. mDrawHelper->lineList(linePoints);
  177. mIdxToSceneObjectMap[lineListData.idx] = mActiveSO;
  178. }
  179. void GizmoManager::drawWireDisc(const Vector3& position, const Vector3& normal, float radius)
  180. {
  181. mWireDiscData.push_back(WireDiscData());
  182. WireDiscData& wireDiscData = mWireDiscData.back();
  183. wireDiscData.idx = mCurrentIdx++;
  184. wireDiscData.position = position;
  185. wireDiscData.normal = normal;
  186. wireDiscData.radius = radius;
  187. wireDiscData.color = mColor;
  188. wireDiscData.transform = mTransform;
  189. wireDiscData.sceneObject = mActiveSO;
  190. wireDiscData.pickable = mPickable;
  191. mDrawHelper->wireDisc(position, normal, radius);
  192. mIdxToSceneObjectMap[wireDiscData.idx] = mActiveSO;
  193. }
  194. void GizmoManager::drawWireArc(const Vector3& position, const Vector3& normal, float radius,
  195. Degree startAngle, Degree amountAngle)
  196. {
  197. mWireArcData.push_back(WireArcData());
  198. WireArcData& wireArcData = mWireArcData.back();
  199. wireArcData.idx = mCurrentIdx++;
  200. wireArcData.position = position;
  201. wireArcData.normal = normal;
  202. wireArcData.radius = radius;
  203. wireArcData.startAngle = startAngle;
  204. wireArcData.amountAngle = amountAngle;
  205. wireArcData.color = mColor;
  206. wireArcData.transform = mTransform;
  207. wireArcData.sceneObject = mActiveSO;
  208. wireArcData.pickable = mPickable;
  209. mDrawHelper->wireArc(position, normal, radius, startAngle, amountAngle);
  210. mIdxToSceneObjectMap[wireArcData.idx] = mActiveSO;
  211. }
  212. void GizmoManager::drawFrustum(const Vector3& position, float aspect, Degree FOV, float near, float far)
  213. {
  214. mFrustumData.push_back(FrustumData());
  215. FrustumData& frustumData = mFrustumData.back();
  216. frustumData.idx = mCurrentIdx++;
  217. frustumData.position = position;
  218. frustumData.aspect = aspect;
  219. frustumData.FOV = FOV;
  220. frustumData.near = near;
  221. frustumData.far = far;
  222. frustumData.color = mColor;
  223. frustumData.transform = mTransform;
  224. frustumData.sceneObject = mActiveSO;
  225. frustumData.pickable = mPickable;
  226. mDrawHelper->frustum(position, aspect, FOV, near, far);
  227. mIdxToSceneObjectMap[frustumData.idx] = mActiveSO;
  228. }
  229. void GizmoManager::drawIcon(Vector3 position, HSpriteTexture image, bool fixedScale)
  230. {
  231. mIconData.push_back(IconData());
  232. IconData& iconData = mIconData.back();
  233. iconData.idx = mCurrentIdx++;
  234. iconData.position = position;
  235. iconData.texture = image;
  236. iconData.fixedScale = fixedScale;
  237. iconData.color = mColor;
  238. iconData.transform = mTransform;
  239. iconData.sceneObject = mActiveSO;
  240. iconData.pickable = mPickable;
  241. mIdxToSceneObjectMap[iconData.idx] = mActiveSO;
  242. }
  243. void GizmoManager::drawText(const Vector3& position, const WString& text, const HFont& font, UINT32 fontSize)
  244. {
  245. HFont myFont = font;
  246. if (myFont == nullptr)
  247. myFont = BuiltinEditorResources::instance().getDefaultFont();
  248. mTextData.push_back(TextData());
  249. TextData& textData = mTextData.back();
  250. textData.idx = mCurrentIdx++;
  251. textData.position = position;
  252. textData.text = text;
  253. textData.font = myFont;
  254. textData.fontSize = fontSize;
  255. textData.color = mColor;
  256. textData.transform = mTransform;
  257. textData.sceneObject = mActiveSO;
  258. textData.pickable = mPickable;
  259. mDrawHelper->text(position, text, myFont, fontSize);
  260. mIdxToSceneObjectMap[textData.idx] = mActiveSO;
  261. }
  262. void GizmoManager::update(const CameraPtr& camera)
  263. {
  264. mDrawHelper->clearMeshes(mActiveMeshes);
  265. mActiveMeshes.clear();
  266. if (mIconMesh != nullptr)
  267. mIconMeshHeap->dealloc(mIconMesh);
  268. IconRenderDataVecPtr iconRenderData;
  269. mDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera->getPosition());
  270. mActiveMeshes = mDrawHelper->getMeshes();
  271. Vector<GizmoManagerCore::MeshData> proxyData;
  272. for (auto& meshData : mActiveMeshes)
  273. {
  274. SPtr<TextureCore> tex;
  275. if (meshData.texture.isLoaded())
  276. tex = meshData.texture->getCore();
  277. if (meshData.type == DrawHelper::MeshType::Solid)
  278. {
  279. proxyData.push_back(GizmoManagerCore::MeshData(
  280. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Solid));
  281. }
  282. else if (meshData.type == DrawHelper::MeshType::Wire)
  283. {
  284. proxyData.push_back(GizmoManagerCore::MeshData(
  285. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Wire));
  286. }
  287. else // Text
  288. {
  289. proxyData.push_back(GizmoManagerCore::MeshData(
  290. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Text));
  291. }
  292. }
  293. mIconMesh = buildIconMesh(camera, mIconData, false, iconRenderData);
  294. SPtr<MeshCoreBase> iconMesh = mIconMesh->getCore();
  295. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  296. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, core, camera->getCore(),
  297. proxyData, iconMesh, iconRenderData));
  298. }
  299. void GizmoManager::renderForPicking(const CameraPtr& camera, std::function<Color(UINT32)> idxToColorCallback)
  300. {
  301. Vector<IconData> iconData;
  302. IconRenderDataVecPtr iconRenderData;
  303. mPickingDrawHelper->clear();
  304. for (auto& cubeDataEntry : mSolidCubeData)
  305. {
  306. if (!cubeDataEntry.pickable)
  307. continue;
  308. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  309. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  310. mPickingDrawHelper->cube(cubeDataEntry.position, cubeDataEntry.extents);
  311. }
  312. for (auto& cubeDataEntry : mWireCubeData)
  313. {
  314. if (!cubeDataEntry.pickable)
  315. continue;
  316. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  317. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  318. mPickingDrawHelper->wireCube(cubeDataEntry.position, cubeDataEntry.extents);
  319. }
  320. for (auto& sphereDataEntry : mSolidSphereData)
  321. {
  322. if (!sphereDataEntry.pickable)
  323. continue;
  324. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  325. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  326. mPickingDrawHelper->sphere(sphereDataEntry.position, sphereDataEntry.radius);
  327. }
  328. for (auto& sphereDataEntry : mWireSphereData)
  329. {
  330. if (!sphereDataEntry.pickable)
  331. continue;
  332. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  333. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  334. mPickingDrawHelper->wireSphere(sphereDataEntry.position, sphereDataEntry.radius);
  335. }
  336. for (auto& lineDataEntry : mLineData)
  337. {
  338. if (!lineDataEntry.pickable)
  339. continue;
  340. mPickingDrawHelper->setColor(idxToColorCallback(lineDataEntry.idx));
  341. mPickingDrawHelper->setTransform(lineDataEntry.transform);
  342. mPickingDrawHelper->line(lineDataEntry.start, lineDataEntry.end);
  343. }
  344. for (auto& lineListDataEntry : mLineListData)
  345. {
  346. if (!lineListDataEntry.pickable)
  347. continue;
  348. mPickingDrawHelper->setColor(idxToColorCallback(lineListDataEntry.idx));
  349. mPickingDrawHelper->setTransform(lineListDataEntry.transform);
  350. mPickingDrawHelper->lineList(lineListDataEntry.linePoints);
  351. }
  352. for (auto& wireDiscDataEntry : mWireDiscData)
  353. {
  354. if (!wireDiscDataEntry.pickable)
  355. continue;
  356. mPickingDrawHelper->setColor(idxToColorCallback(wireDiscDataEntry.idx));
  357. mPickingDrawHelper->setTransform(wireDiscDataEntry.transform);
  358. mPickingDrawHelper->wireDisc(wireDiscDataEntry.position, wireDiscDataEntry.normal, wireDiscDataEntry.radius);
  359. }
  360. for (auto& wireArcDataEntry : mWireArcData)
  361. {
  362. if (!wireArcDataEntry.pickable)
  363. continue;
  364. mPickingDrawHelper->setColor(idxToColorCallback(wireArcDataEntry.idx));
  365. mPickingDrawHelper->setTransform(wireArcDataEntry.transform);
  366. mPickingDrawHelper->wireArc(wireArcDataEntry.position, wireArcDataEntry.normal, wireArcDataEntry.radius,
  367. wireArcDataEntry.startAngle, wireArcDataEntry.amountAngle);
  368. }
  369. for (auto& frustumDataEntry : mFrustumData)
  370. {
  371. if (!frustumDataEntry.pickable)
  372. continue;
  373. mPickingDrawHelper->setColor(idxToColorCallback(frustumDataEntry.idx));
  374. mPickingDrawHelper->setTransform(frustumDataEntry.transform);
  375. mPickingDrawHelper->frustum(frustumDataEntry.position, frustumDataEntry.aspect, frustumDataEntry.FOV,
  376. frustumDataEntry.near, frustumDataEntry.far);
  377. }
  378. for (auto& textDataEntry : mTextData)
  379. {
  380. if (!textDataEntry.pickable)
  381. continue;
  382. mPickingDrawHelper->setColor(idxToColorCallback(textDataEntry.idx));
  383. mPickingDrawHelper->setTransform(textDataEntry.transform);
  384. mPickingDrawHelper->text(textDataEntry.position, textDataEntry.text, textDataEntry.font,
  385. textDataEntry.fontSize);
  386. }
  387. for (auto& iconDataEntry : mIconData)
  388. {
  389. if (!iconDataEntry.pickable)
  390. continue;
  391. iconData.push_back(iconDataEntry);
  392. iconData.back().color = idxToColorCallback(iconDataEntry.idx);
  393. }
  394. mPickingDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera->getPosition());
  395. const Vector<DrawHelper::ShapeMeshData>& meshes = mPickingDrawHelper->getMeshes();
  396. TransientMeshPtr iconMesh = buildIconMesh(camera, iconData, true, iconRenderData);
  397. // Note: This must be rendered while Scene view is being rendered
  398. Matrix4 viewMat = camera->getViewMatrix();
  399. Matrix4 projMat = camera->getProjectionMatrixRS();
  400. ViewportPtr viewport = camera->getViewport();
  401. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  402. for (auto& meshData : meshes)
  403. {
  404. SPtr<TextureCore> tex;
  405. if (meshData.texture.isLoaded())
  406. tex = meshData.texture->getCore();
  407. if(meshData.type == DrawHelper::MeshType::Text)
  408. {
  409. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos, core, viewMat, projMat,
  410. camera->getForward(), meshData.mesh->getCore(), tex, GizmoMaterial::PickingAlpha));
  411. }
  412. else
  413. {
  414. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos, core, viewMat, projMat,
  415. camera->getForward(), meshData.mesh->getCore(), tex, GizmoMaterial::Picking));
  416. }
  417. }
  418. Rect2I screenArea = camera->getViewport()->getArea();
  419. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderIconGizmos,
  420. core, screenArea, iconMesh->getCore(), iconRenderData, true));
  421. mPickingDrawHelper->clearMeshes(meshes);
  422. mIconMeshHeap->dealloc(iconMesh);
  423. }
  424. void GizmoManager::clearGizmos()
  425. {
  426. mSolidCubeData.clear();
  427. mWireCubeData.clear();
  428. mSolidSphereData.clear();
  429. mWireSphereData.clear();
  430. mLineData.clear();
  431. mLineListData.clear();
  432. mWireDiscData.clear();
  433. mWireArcData.clear();
  434. mFrustumData.clear();
  435. mTextData.clear();
  436. mIconData.clear();
  437. mIdxToSceneObjectMap.clear();
  438. mDrawHelper->clear();
  439. mCurrentIdx = 0;
  440. }
  441. void GizmoManager::clearRenderData()
  442. {
  443. mDrawHelper->clearMeshes(mActiveMeshes);
  444. mActiveMeshes.clear();
  445. if (mIconMesh != nullptr)
  446. mIconMeshHeap->dealloc(mIconMesh);
  447. mIconMesh = nullptr;
  448. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  449. IconRenderDataVecPtr iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
  450. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, core,
  451. nullptr, Vector<GizmoManagerCore::MeshData>(), nullptr, iconRenderData));
  452. }
  453. TransientMeshPtr GizmoManager::buildIconMesh(const CameraPtr& camera, const Vector<IconData>& iconData,
  454. bool forPicking, GizmoManager::IconRenderDataVecPtr& iconRenderData)
  455. {
  456. mSortedIconData.clear();
  457. if (iconData.size() > mSortedIconData.size())
  458. mSortedIconData.resize(iconData.size());
  459. UINT32 i = 0;
  460. for (auto& iconEntry : iconData)
  461. {
  462. Vector3 viewPoint = camera->worldToViewPoint(iconEntry.position);
  463. float distance = -viewPoint.z;
  464. if (distance < camera->getNearClipDistance()) // Ignore behind clip plane
  465. continue;
  466. if (distance > MAX_ICON_RANGE) // Ignore too far away
  467. continue;
  468. if (!iconEntry.texture.isLoaded()) // Ignore missing texture
  469. continue;
  470. if (forPicking && !iconEntry.pickable)
  471. continue;
  472. SortedIconData& sortedIconData = mSortedIconData[i];
  473. sortedIconData.iconIdx = i;
  474. sortedIconData.distance = distance;
  475. sortedIconData.screenPosition = camera->viewToScreenPoint(viewPoint);
  476. i++;
  477. }
  478. UINT32 actualNumIcons = i;
  479. // Sort back to front first, then by texture
  480. std::sort(mSortedIconData.begin(), mSortedIconData.begin() + actualNumIcons,
  481. [&](const SortedIconData& a, const SortedIconData& b)
  482. {
  483. if (a.distance == b.distance)
  484. {
  485. HTexture texA = iconData[a.iconIdx].texture->getTexture();
  486. HTexture texB = iconData[b.iconIdx].texture->getTexture();
  487. if (texA == texB)
  488. return a.iconIdx < b.iconIdx;
  489. return texA->getInternalID() < texB->getInternalID();
  490. }
  491. else
  492. return a.distance > b.distance;
  493. });
  494. MeshDataPtr meshData = bs_shared_ptr_new<MeshData>(actualNumIcons * 4, actualNumIcons * 6, mIconVertexDesc);
  495. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  496. auto texcoordIter = meshData->getVec2DataIter(VES_TEXCOORD);
  497. auto normalColorIter = meshData->getDWORDDataIter(VES_COLOR, 0);
  498. auto fadedColorIter = meshData->getDWORDDataIter(VES_COLOR, 1);
  499. UINT32* indices = meshData->getIndices32();
  500. float cameraScale = 1.0f;
  501. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  502. cameraScale = camera->getViewport()->getHeight() / camera->getOrthoWindowHeight();
  503. else
  504. {
  505. Radian vertFOV(Math::tan(camera->getHorzFOV() * 0.5f));
  506. cameraScale = (camera->getViewport()->getHeight() * 0.5f) / vertFOV.valueRadians();
  507. }
  508. iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
  509. UINT32 lastTextureIdx = std::numeric_limits<UINT32>::max();
  510. HTexture curTexture;
  511. // Note: This assumes the meshes will be rendered using the same camera
  512. // properties as when they are created
  513. for (i = 0; i < actualNumIcons; i++)
  514. {
  515. SortedIconData& sortedIconData = mSortedIconData[i];
  516. const IconData& curIconData = iconData[sortedIconData.iconIdx];
  517. HTexture atlasTexture = curIconData.texture->getTexture();
  518. if (curTexture != atlasTexture)
  519. {
  520. UINT32 numIconsPerTexture = i - lastTextureIdx;
  521. if (numIconsPerTexture > 0)
  522. {
  523. iconRenderData->push_back(IconRenderData());
  524. IconRenderData& renderData = iconRenderData->back();
  525. renderData.count = numIconsPerTexture;
  526. renderData.texture = atlasTexture->getCore();
  527. }
  528. lastTextureIdx = i;
  529. curTexture = atlasTexture;
  530. }
  531. UINT32 iconWidth = curIconData.texture->getWidth();
  532. UINT32 iconHeight = curIconData.texture->getHeight();
  533. limitIconSize(iconWidth, iconHeight);
  534. Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, -sortedIconData.distance);
  535. Vector3 projPosition = camera->projectPoint(position);
  536. position.z = projPosition.z;
  537. float halfWidth = iconWidth * 0.5f;
  538. float halfHeight = iconHeight * 0.5f;
  539. if (!curIconData.fixedScale)
  540. {
  541. float iconScale = 1.0f;
  542. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  543. iconScale = cameraScale * ICON_TEXEL_WORLD_SIZE;
  544. else
  545. iconScale = (cameraScale * ICON_TEXEL_WORLD_SIZE) / sortedIconData.distance;
  546. halfWidth *= iconScale;
  547. halfHeight *= iconScale;
  548. }
  549. Color normalColor, fadedColor;
  550. calculateIconColors(curIconData.color, camera, (UINT32)(halfHeight * 2.0f), curIconData.fixedScale, normalColor, fadedColor);
  551. if (forPicking)
  552. {
  553. normalColor = curIconData.color;
  554. fadedColor = curIconData.color;
  555. }
  556. Vector3 positions[4];
  557. positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
  558. positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
  559. positions[2] = position + Vector3(halfWidth, halfHeight, 0.0f);
  560. positions[3] = position + Vector3(-halfWidth, halfHeight, 0.0f);
  561. Vector2 uvs[4];
  562. uvs[0] = curIconData.texture->transformUV(Vector2(0.0f, 0.0f));
  563. uvs[1] = curIconData.texture->transformUV(Vector2(1.0f, 0.0f));
  564. uvs[2] = curIconData.texture->transformUV(Vector2(1.0f, 1.0f));
  565. uvs[3] = curIconData.texture->transformUV(Vector2(0.0f, 1.0f));
  566. for (UINT32 j = 0; j < 4; j++)
  567. {
  568. positionIter.addValue(positions[j]);
  569. texcoordIter.addValue(uvs[j]);
  570. normalColorIter.addValue(normalColor.getAsRGBA());
  571. fadedColorIter.addValue(fadedColor.getAsRGBA());
  572. }
  573. UINT32 vertOffset = i * 4;
  574. indices[0] = vertOffset + 0;
  575. indices[1] = vertOffset + 1;
  576. indices[2] = vertOffset + 2;
  577. indices[3] = vertOffset + 0;
  578. indices[4] = vertOffset + 2;
  579. indices[5] = vertOffset + 3;
  580. indices += 6;
  581. }
  582. return mIconMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  583. }
  584. void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
  585. {
  586. if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
  587. return;
  588. float relWidth = OPTIMAL_ICON_SIZE / (float)width;
  589. float relHeight = OPTIMAL_ICON_SIZE / (float)height;
  590. float scale = std::min(relWidth, relHeight);
  591. width = Math::roundToInt(width * scale);
  592. height = Math::roundToInt(height * scale);
  593. }
  594. void GizmoManager::calculateIconColors(const Color& tint, const CameraPtr& camera,
  595. UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
  596. {
  597. normalColor = tint;
  598. if (!fixedScale)
  599. {
  600. float iconToScreenRatio = iconHeight / (float)camera->getViewport()->getHeight();
  601. if (iconToScreenRatio > 0.3f)
  602. {
  603. float alpha = 1.0f - Math::lerp01(iconToScreenRatio, 0.3f, 1.0f);
  604. normalColor.a *= alpha;
  605. }
  606. else if (iconToScreenRatio < 0.1f)
  607. {
  608. float alpha = Math::lerp01(iconToScreenRatio, 0.0f, 0.1f);
  609. normalColor.a *= alpha;
  610. }
  611. }
  612. fadedColor = normalColor;
  613. fadedColor.a *= 0.2f;
  614. }
  615. HSceneObject GizmoManager::getSceneObject(UINT32 gizmoIdx)
  616. {
  617. auto iterFind = mIdxToSceneObjectMap.find(gizmoIdx);
  618. if (iterFind != mIdxToSceneObjectMap.end())
  619. return iterFind->second;
  620. return HSceneObject();
  621. }
  622. const float GizmoManagerCore::PICKING_ALPHA_CUTOFF = 0.5f;
  623. GizmoManagerCore::GizmoManagerCore(const PrivatelyConstuct& dummy)
  624. {
  625. }
  626. GizmoManagerCore::~GizmoManagerCore()
  627. {
  628. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  629. if (mCamera != nullptr)
  630. activeRenderer->_unregisterRenderCallback(mCamera.get(), 20);
  631. }
  632. void GizmoManagerCore::initialize(const GizmoManager::CoreInitData& initData)
  633. {
  634. THROW_IF_NOT_CORE_THREAD;
  635. mSolidMaterial.mat = initData.solidMat;
  636. mWireMaterial.mat = initData.wireMat;
  637. mTextMaterial.mat = initData.textMat;
  638. mIconMaterial.mat = initData.iconMat;
  639. mPickingMaterial.mat = initData.pickingMat;
  640. mAlphaPickingMaterial.mat = initData.alphaPickingMat;
  641. {
  642. SPtr<MaterialCore> mat = mWireMaterial.mat;
  643. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  644. vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
  645. }
  646. {
  647. SPtr<MaterialCore> mat = mSolidMaterial.mat;
  648. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  649. SPtr<GpuParamsCore> fragParams = mat->getPassParameters(0)->mFragParams;
  650. vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
  651. fragParams->getParam("viewDir", mSolidMaterial.mViewDir);
  652. }
  653. {
  654. SPtr<MaterialCore> mat = mIconMaterial.mat;
  655. SPtr<PassParametersCore> pass0Params = mat->getPassParameters(0);
  656. SPtr<PassParametersCore> pass1Params = mat->getPassParameters(1);
  657. SPtr<GpuParamsCore> vertParams0 = pass0Params->mVertParams;
  658. SPtr<GpuParamsCore> vertParams1 = pass1Params->mVertParams;
  659. vertParams0->getParam("matViewProj", mIconMaterial.mViewProj[0]);
  660. vertParams1->getParam("matViewProj", mIconMaterial.mViewProj[1]);
  661. mIconMaterial.mFragParams[0] = pass0Params->mFragParams;
  662. mIconMaterial.mFragParams[1] = pass1Params->mFragParams;
  663. mIconMaterial.mFragParams[0]->getTextureParam("mainTexture", mIconMaterial.mTexture[0]);
  664. mIconMaterial.mFragParams[1]->getTextureParam("mainTexture", mIconMaterial.mTexture[1]);
  665. }
  666. {
  667. SPtr<MaterialCore> mat = mPickingMaterial.mat;
  668. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  669. vertParams->getParam("matViewProj", mPickingMaterial.mViewProj);
  670. }
  671. {
  672. SPtr<MaterialCore> mat = mAlphaPickingMaterial.mat;
  673. SPtr<PassParametersCore> passParams = mat->getPassParameters(0);
  674. SPtr<GpuParamsCore> vertParams = passParams->mVertParams;
  675. vertParams->getParam("matViewProj", mAlphaPickingMaterial.mViewProj);
  676. mAlphaPickingMaterial.mFragParams = passParams->mFragParams;
  677. mAlphaPickingMaterial.mFragParams->getTextureParam("mainTexture", mAlphaPickingMaterial.mTexture);
  678. GpuParamFloatCore alphaCutoffParam;
  679. mAlphaPickingMaterial.mFragParams->getParam("alphaCutoff", alphaCutoffParam);
  680. alphaCutoffParam.set(PICKING_ALPHA_CUTOFF);
  681. }
  682. {
  683. SPtr<MaterialCore> mat = mTextMaterial.mat;
  684. SPtr<PassParametersCore> passParams = mat->getPassParameters(0);
  685. SPtr<GpuParamsCore> vertParams = passParams->mVertParams;
  686. SPtr<GpuParamsCore> fragParams = passParams->mFragParams;
  687. vertParams->getParam("matViewProj", mTextMaterial.mViewProj);
  688. fragParams->getTextureParam("mainTexture", mTextMaterial.mTexture);
  689. }
  690. }
  691. void GizmoManagerCore::updateData(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes,
  692. const SPtr<MeshCoreBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
  693. {
  694. if (mCamera != camera)
  695. {
  696. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  697. if (mCamera != nullptr)
  698. activeRenderer->_unregisterRenderCallback(mCamera.get(), 0);
  699. if (camera != nullptr)
  700. activeRenderer->_registerRenderCallback(camera.get(), 0, std::bind(&GizmoManagerCore::render, this));
  701. }
  702. mCamera = camera;
  703. mMeshes = meshes;
  704. mIconMesh = iconMesh;
  705. mIconRenderData = iconRenderData;
  706. }
  707. void GizmoManagerCore::render()
  708. {
  709. if (mCamera == nullptr)
  710. return;
  711. SPtr<RenderTargetCore> renderTarget = mCamera->getViewport()->getTarget();
  712. float width = (float)renderTarget->getProperties().getWidth();
  713. float height = (float)renderTarget->getProperties().getHeight();
  714. Rect2 normArea = mCamera->getViewport()->getNormArea();
  715. Rect2I screenArea;
  716. screenArea.x = (int)(normArea.x * width);
  717. screenArea.y = (int)(normArea.y * height);
  718. screenArea.width = (int)(normArea.width * width);
  719. screenArea.height = (int)(normArea.height * height);
  720. for (auto& meshData : mMeshes)
  721. {
  722. GizmoManager::GizmoMaterial material = GizmoManager::GizmoMaterial::Solid;
  723. switch(meshData.type)
  724. {
  725. case MeshType::Solid:
  726. material = GizmoManager::GizmoMaterial::Solid;
  727. break;
  728. case MeshType::Wire:
  729. material = GizmoManager::GizmoMaterial::Wire;
  730. break;
  731. case MeshType::Text:
  732. material = GizmoManager::GizmoMaterial::Text;
  733. break;
  734. }
  735. renderGizmos(mCamera->getViewMatrix(), mCamera->getProjectionMatrixRS(), mCamera->getForward(),
  736. meshData.mesh, meshData.texture, material);
  737. }
  738. if (mIconMesh != nullptr)
  739. renderIconGizmos(screenArea, mIconMesh, mIconRenderData, false);
  740. }
  741. void GizmoManagerCore::renderGizmos(const Matrix4& viewMatrix, const Matrix4& projMatrix, const Vector3& viewDir,
  742. const SPtr<MeshCoreBase>& mesh, const SPtr<TextureCore>& texture, GizmoManager::GizmoMaterial material)
  743. {
  744. THROW_IF_NOT_CORE_THREAD;
  745. Matrix4 viewProjMat = projMatrix * viewMatrix;
  746. switch (material)
  747. {
  748. case GizmoManager::GizmoMaterial::Solid:
  749. mSolidMaterial.mViewProj.set(viewProjMat);
  750. mSolidMaterial.mViewDir.set((Vector4)viewDir);
  751. gRendererUtility().setPass(mSolidMaterial.mat, 0);
  752. gRendererUtility().setPassParams(mSolidMaterial.mat);
  753. break;
  754. case GizmoManager::GizmoMaterial::Wire:
  755. mWireMaterial.mViewProj.set(viewProjMat);
  756. gRendererUtility().setPass(mWireMaterial.mat, 0);
  757. gRendererUtility().setPassParams(mWireMaterial.mat);
  758. break;
  759. case GizmoManager::GizmoMaterial::Picking:
  760. mPickingMaterial.mViewProj.set(viewProjMat);
  761. gRendererUtility().setPass(mPickingMaterial.mat, 0);
  762. gRendererUtility().setPassParams(mPickingMaterial.mat);
  763. break;
  764. case GizmoManager::GizmoMaterial::PickingAlpha:
  765. mAlphaPickingMaterial.mViewProj.set(viewProjMat);
  766. mAlphaPickingMaterial.mTexture.set(texture);
  767. gRendererUtility().setPass(mAlphaPickingMaterial.mat, 0);
  768. gRendererUtility().setPassParams(mAlphaPickingMaterial.mat);
  769. break;
  770. case GizmoManager::GizmoMaterial::Text:
  771. mTextMaterial.mViewProj.set(viewProjMat);
  772. mTextMaterial.mTexture.set(texture);
  773. gRendererUtility().setPass(mTextMaterial.mat, 0);
  774. gRendererUtility().setPassParams(mTextMaterial.mat);
  775. break;
  776. }
  777. gRendererUtility().draw(mesh, mesh->getProperties().getSubMesh(0));
  778. }
  779. void GizmoManagerCore::renderIconGizmos(Rect2I screenArea, SPtr<MeshCoreBase> mesh, GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
  780. {
  781. RenderAPICore& rs = RenderAPICore::instance();
  782. const MeshProperties& meshProps = mesh->getProperties();
  783. std::shared_ptr<VertexData> vertexData = mesh->getVertexData();
  784. rs.setVertexDeclaration(vertexData->vertexDeclaration);
  785. auto vertexBuffers = vertexData->getBuffers();
  786. SPtr<VertexBufferCore> vertBuffers[1] = { vertexBuffers.begin()->second };
  787. rs.setVertexBuffers(0, vertBuffers, 1);
  788. SPtr<IndexBufferCore> indexBuffer = mesh->getIndexBuffer();
  789. rs.setIndexBuffer(indexBuffer);
  790. rs.setDrawOperation(DOT_TRIANGLE_LIST);
  791. // Set up ortho matrix
  792. Matrix4 projMat;
  793. float left = screenArea.x + rs.getHorizontalTexelOffset();
  794. float right = screenArea.x + screenArea.width + rs.getHorizontalTexelOffset();
  795. float top = screenArea.y + rs.getVerticalTexelOffset();
  796. float bottom = screenArea.y + screenArea.height + rs.getVerticalTexelOffset();
  797. float near = rs.getMinimumDepthInputValue();
  798. float far = rs.getMaximumDepthInputValue();
  799. // Top/bottom have been swapped because we're moving from window coordinates (origin top left)
  800. // to normalized device coordinates (origin bottom left)
  801. // Negative near/far because Z is flipped for normalized device coordinates
  802. // (positive Z goes into screen as opposed to view space here we're looking along negative Z)
  803. projMat.makeProjectionOrtho(left, right, top, bottom, -near, -far);
  804. if (!usePickingMaterial)
  805. {
  806. mIconMaterial.mViewProj[0].set(projMat);
  807. mIconMaterial.mViewProj[1].set(projMat);
  808. for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
  809. {
  810. gRendererUtility().setPass(mIconMaterial.mat, passIdx);
  811. UINT32 curIndexOffset = mesh->getIndexOffset();
  812. for (auto curRenderData : *renderData)
  813. {
  814. mIconMaterial.mTexture[passIdx].set(curRenderData.texture);
  815. rs.setGpuParams(GPT_FRAGMENT_PROGRAM, mIconMaterial.mFragParams[passIdx]);
  816. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  817. curIndexOffset += curRenderData.count * 6;
  818. }
  819. }
  820. }
  821. else
  822. {
  823. mAlphaPickingMaterial.mViewProj.set(projMat);
  824. gRendererUtility().setPass(mAlphaPickingMaterial.mat, 0);
  825. UINT32 curIndexOffset = 0;
  826. for (auto curRenderData : *renderData)
  827. {
  828. mAlphaPickingMaterial.mTexture.set(curRenderData.texture);
  829. rs.setGpuParams(GPT_FRAGMENT_PROGRAM, mAlphaPickingMaterial.mFragParams);
  830. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  831. curIndexOffset += curRenderData.count * 6;
  832. }
  833. }
  834. mesh->_notifyUsedOnGPU();
  835. }
  836. }