BsGizmoManager.cpp 38 KB

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