BsGizmoManager.cpp 40 KB

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