BsGizmoManager.cpp 38 KB

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