BsGizmoManager.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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. UINT32 lastTextureIdx = std::numeric_limits<UINT32>::max();
  629. HTexture curTexture;
  630. // Note: This assumes the meshes will be rendered using the same camera
  631. // properties as when they are created
  632. for (i = 0; i < actualNumIcons; i++)
  633. {
  634. SortedIconData& sortedIconData = mSortedIconData[i];
  635. const IconData& curIconData = iconData[sortedIconData.iconIdx];
  636. HTexture atlasTexture = curIconData.texture->getTexture();
  637. if (curTexture != atlasTexture)
  638. {
  639. UINT32 numIconsPerTexture = i - lastTextureIdx;
  640. if (numIconsPerTexture > 0)
  641. {
  642. iconRenderData->push_back(IconRenderData());
  643. IconRenderData& renderData = iconRenderData->back();
  644. renderData.count = numIconsPerTexture;
  645. renderData.texture = atlasTexture->getCore();
  646. }
  647. lastTextureIdx = i;
  648. curTexture = atlasTexture;
  649. }
  650. UINT32 iconWidth = curIconData.texture->getWidth();
  651. UINT32 iconHeight = curIconData.texture->getHeight();
  652. limitIconSize(iconWidth, iconHeight);
  653. Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, -sortedIconData.distance);
  654. Vector3 projPosition = camera->projectPoint(position);
  655. position.z = projPosition.z;
  656. float halfWidth = iconWidth * 0.5f;
  657. float halfHeight = iconHeight * 0.5f;
  658. if (!curIconData.fixedScale)
  659. {
  660. float iconScale = 1.0f;
  661. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  662. iconScale = cameraScale * ICON_TEXEL_WORLD_SIZE;
  663. else
  664. iconScale = (cameraScale * ICON_TEXEL_WORLD_SIZE) / sortedIconData.distance;
  665. halfWidth *= iconScale;
  666. halfHeight *= iconScale;
  667. }
  668. Color normalColor, fadedColor;
  669. calculateIconColors(curIconData.color, camera, (UINT32)(halfHeight * 2.0f), curIconData.fixedScale, normalColor, fadedColor);
  670. if (forPicking)
  671. {
  672. normalColor = curIconData.color;
  673. fadedColor = curIconData.color;
  674. }
  675. Vector3 positions[4];
  676. positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
  677. positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
  678. positions[2] = position + Vector3(halfWidth, halfHeight, 0.0f);
  679. positions[3] = position + Vector3(-halfWidth, halfHeight, 0.0f);
  680. Vector2 uvs[4];
  681. uvs[0] = curIconData.texture->transformUV(Vector2(0.0f, 0.0f));
  682. uvs[1] = curIconData.texture->transformUV(Vector2(1.0f, 0.0f));
  683. uvs[2] = curIconData.texture->transformUV(Vector2(1.0f, 1.0f));
  684. uvs[3] = curIconData.texture->transformUV(Vector2(0.0f, 1.0f));
  685. for (UINT32 j = 0; j < 4; j++)
  686. {
  687. positionIter.addValue(positions[j]);
  688. texcoordIter.addValue(uvs[j]);
  689. normalColorIter.addValue(normalColor.getAsRGBA());
  690. fadedColorIter.addValue(fadedColor.getAsRGBA());
  691. }
  692. UINT32 vertOffset = i * 4;
  693. indices[0] = vertOffset + 0;
  694. indices[1] = vertOffset + 1;
  695. indices[2] = vertOffset + 2;
  696. indices[3] = vertOffset + 0;
  697. indices[4] = vertOffset + 2;
  698. indices[5] = vertOffset + 3;
  699. indices += 6;
  700. }
  701. if(actualNumIcons > 0)
  702. return Mesh::_createPtr(meshData);
  703. return nullptr;
  704. }
  705. void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
  706. {
  707. if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
  708. return;
  709. float relWidth = OPTIMAL_ICON_SIZE / (float)width;
  710. float relHeight = OPTIMAL_ICON_SIZE / (float)height;
  711. float scale = std::min(relWidth, relHeight);
  712. width = Math::roundToInt(width * scale);
  713. height = Math::roundToInt(height * scale);
  714. }
  715. void GizmoManager::calculateIconColors(const Color& tint, const SPtr<Camera>& camera,
  716. UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
  717. {
  718. normalColor = tint;
  719. if (!fixedScale)
  720. {
  721. float iconToScreenRatio = iconHeight / (float)camera->getViewport()->getPixelArea().height;
  722. if (iconToScreenRatio > 0.3f)
  723. {
  724. float alpha = 1.0f - Math::invLerp(iconToScreenRatio, 0.3f, 1.0f);
  725. normalColor.a *= alpha;
  726. }
  727. else if (iconToScreenRatio < 0.1f)
  728. {
  729. float alpha = Math::invLerp(iconToScreenRatio, 0.0f, 0.1f);
  730. normalColor.a *= alpha;
  731. }
  732. }
  733. fadedColor = normalColor;
  734. fadedColor.a *= 0.2f;
  735. }
  736. HSceneObject GizmoManager::getSceneObject(UINT32 gizmoIdx)
  737. {
  738. auto iterFind = mIdxToSceneObjectMap.find(gizmoIdx);
  739. if (iterFind != mIdxToSceneObjectMap.end())
  740. return iterFind->second;
  741. return HSceneObject();
  742. }
  743. namespace ct
  744. {
  745. GizmoParamBlockDef gGizmoParamBlockDef;
  746. GizmoPickingParamBlockDef gGizmoPickingParamBlockDef;
  747. const float GizmoRenderer::PICKING_ALPHA_CUTOFF = 0.5f;
  748. GizmoRenderer::GizmoRenderer()
  749. :RendererExtension(RenderLocation::PostLightPass, 0)
  750. {
  751. }
  752. void GizmoRenderer::initialize(const Any& data)
  753. {
  754. THROW_IF_NOT_CORE_THREAD;
  755. const GizmoManager::CoreInitData& initData = any_cast_ref<GizmoManager::CoreInitData>(data);
  756. const auto getAndCompile = [](const SPtr<Material>& material) { material->getTechnique(0)->compile(); return material; };
  757. mMeshMaterials[(UINT32)GizmoMeshType::Solid] = getAndCompile(initData.solidMat);
  758. mMeshMaterials[(UINT32)GizmoMeshType::Wire] = getAndCompile(initData.wireMat);
  759. mMeshMaterials[(UINT32)GizmoMeshType::Line] = getAndCompile(initData.lineMat);
  760. mMeshMaterials[(UINT32)GizmoMeshType::Text] = getAndCompile(initData.textMat);
  761. mIconMaterial = getAndCompile(initData.iconMat);
  762. mPickingMaterials[0] = getAndCompile(initData.pickingMat);
  763. mPickingMaterials[1] = getAndCompile(initData.alphaPickingMat);
  764. mMeshGizmoBuffer = gGizmoParamBlockDef.createBuffer();
  765. mIconGizmoBuffer = gGizmoParamBlockDef.createBuffer();
  766. mMeshPickingParamBuffer = gGizmoPickingParamBlockDef.createBuffer();
  767. mIconPickingParamBuffer = gGizmoPickingParamBlockDef.createBuffer();
  768. }
  769. void GizmoRenderer::updateData(const SPtr<Camera>& camera, const Vector<GizmoManager::MeshRenderData>& meshes,
  770. const SPtr<MeshBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
  771. {
  772. mCamera = camera;
  773. mMeshes = meshes;
  774. mIconMesh = iconMesh;
  775. mIconRenderData = iconRenderData;
  776. // Allocate and assign GPU program parameter objects
  777. UINT32 meshCounters[(UINT32)GizmoMeshType::Count];
  778. bs_zero_out(meshCounters);
  779. for (auto& meshData : mMeshes)
  780. {
  781. UINT32 typeIdx = (UINT32)meshData.type;
  782. UINT32 paramsIdx = meshCounters[typeIdx];
  783. meshData.paramsIdx = paramsIdx;
  784. SPtr<GpuParamsSet> paramsSet;
  785. if (paramsIdx >= mMeshParamSets[typeIdx].size())
  786. {
  787. paramsSet = mMeshMaterials[typeIdx]->createParamsSet();
  788. mMeshMaterials[typeIdx]->updateParamsSet(paramsSet, true);
  789. paramsSet->setParamBlockBuffer("Uniforms", mMeshGizmoBuffer, true);
  790. mMeshParamSets[typeIdx].push_back(paramsSet);
  791. }
  792. else
  793. paramsSet = mMeshParamSets[typeIdx][paramsIdx];
  794. if(meshData.type == GizmoMeshType::Text)
  795. {
  796. SPtr<GpuParams> params = paramsSet->getGpuParams();
  797. GpuParamTexture textureParam;
  798. params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam);
  799. textureParam.set(meshData.texture);
  800. }
  801. meshCounters[typeIdx]++;
  802. }
  803. UINT32 iconMeshIdx = 0;
  804. for(auto& iconData : *mIconRenderData)
  805. {
  806. iconData.paramsIdx = iconMeshIdx;
  807. SPtr<GpuParamsSet> paramsSet;
  808. if (iconMeshIdx >= mIconParamSets.size())
  809. {
  810. paramsSet = mIconMaterial->createParamsSet();
  811. paramsSet->setParamBlockBuffer("Uniforms", mIconGizmoBuffer, true);
  812. mIconParamSets.push_back(paramsSet);
  813. }
  814. else
  815. paramsSet = mIconParamSets[iconMeshIdx];
  816. SPtr<GpuParams> params0 = paramsSet->getGpuParams(0);
  817. SPtr<GpuParams> params1 = paramsSet->getGpuParams(1);
  818. GpuParamTexture textureParam0;
  819. GpuParamTexture textureParam1;
  820. params0->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam0);
  821. params1->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam1);
  822. textureParam0.set(iconData.texture);
  823. textureParam1.set(iconData.texture);
  824. iconMeshIdx++;
  825. }
  826. }
  827. bool GizmoRenderer::check(const Camera& camera)
  828. {
  829. return &camera == mCamera.get();
  830. }
  831. void GizmoRenderer::render(const Camera& camera)
  832. {
  833. renderData(mCamera, mMeshes, mIconMesh, mIconRenderData, false);
  834. }
  835. void GizmoRenderer::renderData(const SPtr<Camera>& camera, Vector<GizmoManager::MeshRenderData>& meshes,
  836. const SPtr<MeshBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData, bool usePickingMaterial)
  837. {
  838. if (camera == nullptr)
  839. return;
  840. SPtr<RenderTarget> renderTarget = camera->getViewport()->getTarget();
  841. if (renderTarget == nullptr)
  842. return;
  843. Rect2I screenArea = camera->getViewport()->getPixelArea();
  844. Matrix4 viewMatrix = camera->getViewMatrix();
  845. Matrix4 projMatrix = camera->getProjectionMatrixRS();
  846. Matrix4 viewProjMat = projMatrix * viewMatrix;
  847. float invViewportWidth = 1.0f / (camera->getViewport()->getPixelArea().width * 0.5f);
  848. float invViewportHeight = 1.0f / (camera->getViewport()->getPixelArea().height * 0.5f);
  849. float viewportYFlip = (gCaps().conventions.ndcYAxis == Conventions::Axis::Down) ? -1.0f : 1.0f;
  850. if (!usePickingMaterial)
  851. {
  852. gGizmoParamBlockDef.gMatViewProj.set(mMeshGizmoBuffer, viewProjMat);
  853. gGizmoParamBlockDef.gViewDir.set(mMeshGizmoBuffer, (Vector4)camera->getTransform().getForward());
  854. gGizmoParamBlockDef.gInvViewportWidth.set(mMeshGizmoBuffer, invViewportWidth);
  855. gGizmoParamBlockDef.gInvViewportHeight.set(mMeshGizmoBuffer, invViewportHeight);
  856. gGizmoParamBlockDef.gViewportYFlip.set(mMeshGizmoBuffer, viewportYFlip);
  857. for (auto& entry : meshes)
  858. {
  859. UINT32 typeIdx = (UINT32)entry.type;
  860. gRendererUtility().setPass(mMeshMaterials[typeIdx]);
  861. gRendererUtility().setPassParams(mMeshParamSets[typeIdx][entry.paramsIdx]);
  862. gRendererUtility().draw(entry.mesh, entry.subMesh);
  863. }
  864. }
  865. else
  866. {
  867. // Allocate and assign GPU program parameter objects
  868. UINT32 pickingCounters[2];
  869. bs_zero_out(pickingCounters);
  870. for (auto& entry : meshes)
  871. {
  872. UINT32 typeIdx = entry.type == GizmoMeshType::Text ? 1 : 0;
  873. UINT32 paramsIdx = pickingCounters[typeIdx];
  874. entry.paramsIdx = paramsIdx;
  875. if (paramsIdx >= mPickingParamSets[typeIdx].size())
  876. {
  877. SPtr<GpuParamsSet> paramsSet = mPickingMaterials[typeIdx]->createParamsSet();
  878. paramsSet->setParamBlockBuffer("Uniforms", mMeshPickingParamBuffer, true);
  879. mPickingParamSets[typeIdx].push_back(paramsSet);
  880. }
  881. pickingCounters[typeIdx]++;
  882. }
  883. for (auto& iconData : *iconRenderData)
  884. {
  885. iconData.paramsIdx = pickingCounters[1];
  886. if (iconData.paramsIdx >= mPickingParamSets[1].size())
  887. {
  888. SPtr<GpuParamsSet> paramsSet = mPickingMaterials[1]->createParamsSet();
  889. paramsSet->setParamBlockBuffer("Uniforms", mIconPickingParamBuffer, true);
  890. mPickingParamSets[1].push_back(paramsSet);
  891. }
  892. pickingCounters[1]++;
  893. }
  894. gGizmoPickingParamBlockDef.gMatViewProj.set(mMeshPickingParamBuffer, viewProjMat);
  895. gGizmoPickingParamBlockDef.gAlphaCutoff.set(mMeshPickingParamBuffer, PICKING_ALPHA_CUTOFF);
  896. for (auto& entry : meshes)
  897. {
  898. UINT32 typeIdx = entry.type == GizmoMeshType::Text ? 1 : 0;
  899. gRendererUtility().setPass(mPickingMaterials[typeIdx]);
  900. gRendererUtility().setPassParams(mPickingParamSets[typeIdx][entry.paramsIdx]);
  901. gRendererUtility().draw(entry.mesh, entry.subMesh);
  902. }
  903. }
  904. if (iconMesh != nullptr)
  905. renderIconGizmos(screenArea, iconMesh, iconRenderData, usePickingMaterial);
  906. }
  907. void GizmoRenderer::renderIconGizmos(Rect2I screenArea, SPtr<MeshBase> mesh,
  908. GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
  909. {
  910. RenderAPI& rapi = RenderAPI::instance();
  911. SPtr<VertexData> vertexData = mesh->getVertexData();
  912. rapi.setVertexDeclaration(vertexData->vertexDeclaration);
  913. auto vertexBuffers = vertexData->getBuffers();
  914. SPtr<VertexBuffer> vertBuffers[1] = { vertexBuffers.begin()->second };
  915. rapi.setVertexBuffers(0, vertBuffers, 1);
  916. SPtr<IndexBuffer> indexBuffer = mesh->getIndexBuffer();
  917. rapi.setIndexBuffer(indexBuffer);
  918. rapi.setDrawOperation(DOT_TRIANGLE_LIST);
  919. // Set up ortho matrix
  920. Matrix4 projMat;
  921. const RenderAPICapabilities& caps = gCaps();
  922. float left = screenArea.x + caps.horizontalTexelOffset;
  923. float right = screenArea.x + screenArea.width + caps.horizontalTexelOffset;
  924. float top = screenArea.y + caps.verticalTexelOffset;
  925. float bottom = screenArea.y + screenArea.height + caps.verticalTexelOffset;
  926. float near = caps.minDepth;
  927. float far = caps.maxDepth;
  928. // Top/bottom have been swapped because we're moving from window coordinates (origin top left)
  929. // to normalized device coordinates (origin bottom left)
  930. // Negative near/far because Z is flipped for normalized device coordinates
  931. // (positive Z goes into screen as opposed to view space here we're looking along negative Z)
  932. projMat.makeProjectionOrtho(left, right, top, bottom, -near, -far);
  933. rapi.convertProjectionMatrix(projMat, projMat);
  934. if (!usePickingMaterial)
  935. {
  936. gGizmoParamBlockDef.gMatViewProj.set(mIconGizmoBuffer, projMat);
  937. gGizmoParamBlockDef.gViewDir.set(mIconGizmoBuffer, Vector4::ZERO);
  938. for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
  939. {
  940. gRendererUtility().setPass(mIconMaterial, passIdx);
  941. UINT32 curIndexOffset = mesh->getIndexOffset();
  942. for (auto curRenderData : *renderData)
  943. {
  944. gRendererUtility().setPassParams(mIconParamSets[curRenderData.paramsIdx], passIdx);
  945. rapi.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  946. curIndexOffset += curRenderData.count * 6;
  947. }
  948. }
  949. }
  950. else
  951. {
  952. gGizmoPickingParamBlockDef.gMatViewProj.set(mIconPickingParamBuffer, projMat);
  953. gGizmoPickingParamBlockDef.gAlphaCutoff.set(mIconPickingParamBuffer, PICKING_ALPHA_CUTOFF);
  954. for (auto& iconData : *renderData)
  955. {
  956. SPtr<GpuParamsSet> paramsSet = mPickingParamSets[1][iconData.paramsIdx];
  957. SPtr<GpuParams> params = paramsSet->getGpuParams();
  958. GpuParamTexture textureParam;
  959. params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam);
  960. textureParam.set(iconData.texture);
  961. }
  962. gRendererUtility().setPass(mPickingMaterials[1]);
  963. UINT32 curIndexOffset = mesh->getIndexOffset();
  964. for (auto curRenderData : *renderData)
  965. {
  966. gRendererUtility().setPassParams(mPickingParamSets[1][curRenderData.paramsIdx]);
  967. rapi.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  968. curIndexOffset += curRenderData.count * 6;
  969. }
  970. }
  971. mesh->_notifyUsedOnGPU();
  972. }
  973. }
  974. }