BsGizmoManager.cpp 33 KB

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