BsGizmoManager.cpp 39 KB

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