BsGizmoManager.cpp 35 KB

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