BsGizmoManager.cpp 39 KB

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