BsGizmoManager.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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. mMeshMaterials[(UINT32)GizmoMeshType::Solid] = initData.solidMat;
  734. mMeshMaterials[(UINT32)GizmoMeshType::Wire] = initData.wireMat;
  735. mMeshMaterials[(UINT32)GizmoMeshType::Line] = initData.lineMat;
  736. mMeshMaterials[(UINT32)GizmoMeshType::Text] = initData.textMat;
  737. mIconMaterial = initData.iconMat;
  738. mPickingMaterials[0] = initData.pickingMat;
  739. mPickingMaterials[1] = initData.alphaPickingMat;
  740. mMeshGizmoBuffer = gGizmoParamBlockDef.createBuffer();
  741. mIconGizmoBuffer = gGizmoParamBlockDef.createBuffer();
  742. mMeshPickingParamBuffer = gGizmoPickingParamBlockDef.createBuffer();
  743. mIconPickingParamBuffer = gGizmoPickingParamBlockDef.createBuffer();
  744. }
  745. void GizmoRenderer::updateData(const SPtr<Camera>& camera, const Vector<GizmoManager::MeshRenderData>& meshes,
  746. const SPtr<MeshBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
  747. {
  748. mCamera = camera;
  749. mMeshes = meshes;
  750. mIconMesh = iconMesh;
  751. mIconRenderData = iconRenderData;
  752. // Allocate and assign GPU program parameter objects
  753. UINT32 meshCounters[(UINT32)GizmoMeshType::Count];
  754. bs_zero_out(meshCounters);
  755. for (auto& meshData : mMeshes)
  756. {
  757. UINT32 typeIdx = (UINT32)meshData.type;
  758. UINT32 paramsIdx = meshCounters[typeIdx];
  759. meshData.paramsIdx = paramsIdx;
  760. SPtr<GpuParamsSet> paramsSet;
  761. if (paramsIdx >= mMeshParamSets[typeIdx].size())
  762. {
  763. paramsSet = mMeshMaterials[typeIdx]->createParamsSet();
  764. mMeshMaterials[typeIdx]->updateParamsSet(paramsSet, true);
  765. paramsSet->setParamBlockBuffer("Uniforms", mMeshGizmoBuffer, true);
  766. mMeshParamSets[typeIdx].push_back(paramsSet);
  767. }
  768. else
  769. paramsSet = mMeshParamSets[typeIdx][paramsIdx];
  770. if(meshData.type == GizmoMeshType::Text)
  771. {
  772. SPtr<GpuParams> params = paramsSet->getGpuParams();
  773. GpuParamTexture textureParam;
  774. params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam);
  775. textureParam.set(meshData.texture);
  776. }
  777. meshCounters[typeIdx]++;
  778. }
  779. UINT32 iconMeshIdx = 0;
  780. for(auto& iconData : *mIconRenderData)
  781. {
  782. iconData.paramsIdx = iconMeshIdx;
  783. SPtr<GpuParamsSet> paramsSet;
  784. if (iconMeshIdx >= mIconParamSets.size())
  785. {
  786. mIconMaterial->createParamsSet();
  787. paramsSet->setParamBlockBuffer("Uniforms", mIconGizmoBuffer, true);
  788. mIconParamSets.push_back(paramsSet);
  789. }
  790. else
  791. paramsSet = mIconParamSets[iconMeshIdx];
  792. SPtr<GpuParams> params0 = paramsSet->getGpuParams(0);
  793. SPtr<GpuParams> params1 = paramsSet->getGpuParams(1);
  794. GpuParamTexture textureParam0;
  795. GpuParamTexture textureParam1;
  796. params0->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam0);
  797. params1->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam1);
  798. textureParam0.set(iconData.texture);
  799. textureParam1.set(iconData.texture);
  800. iconMeshIdx++;
  801. }
  802. }
  803. bool GizmoRenderer::check(const Camera& camera)
  804. {
  805. return &camera == mCamera.get();
  806. }
  807. void GizmoRenderer::render(const Camera& camera)
  808. {
  809. renderData(mCamera, mMeshes, mIconMesh, mIconRenderData, false);
  810. }
  811. void GizmoRenderer::renderData(const SPtr<Camera>& camera, Vector<GizmoManager::MeshRenderData>& meshes,
  812. const SPtr<MeshBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData, bool usePickingMaterial)
  813. {
  814. if (camera == nullptr)
  815. return;
  816. SPtr<RenderTarget> renderTarget = camera->getViewport()->getTarget();
  817. if (renderTarget == nullptr)
  818. return;
  819. Rect2I screenArea = camera->getViewport()->getPixelArea();
  820. Matrix4 viewMatrix = camera->getViewMatrix();
  821. Matrix4 projMatrix = camera->getProjectionMatrixRS();
  822. Matrix4 viewProjMat = projMatrix * viewMatrix;
  823. float invViewportWidth = 1.0f / (camera->getViewport()->getPixelArea().width * 0.5f);
  824. float invViewportHeight = 1.0f / (camera->getViewport()->getPixelArea().height * 0.5f);
  825. float viewportYFlip = bs::RenderAPI::getAPIInfo().isFlagSet(RenderAPIFeatureFlag::NDCYAxisDown) ? -1.0f : 1.0f;
  826. if (!usePickingMaterial)
  827. {
  828. gGizmoParamBlockDef.gMatViewProj.set(mMeshGizmoBuffer, viewProjMat);
  829. gGizmoParamBlockDef.gViewDir.set(mMeshGizmoBuffer, (Vector4)camera->getTransform().getForward());
  830. gGizmoParamBlockDef.gInvViewportWidth.set(mMeshGizmoBuffer, invViewportWidth);
  831. gGizmoParamBlockDef.gInvViewportHeight.set(mMeshGizmoBuffer, invViewportHeight);
  832. gGizmoParamBlockDef.gViewportYFlip.set(mMeshGizmoBuffer, viewportYFlip);
  833. for (auto& entry : meshes)
  834. {
  835. UINT32 typeIdx = (UINT32)entry.type;
  836. gRendererUtility().setPass(mMeshMaterials[typeIdx]);
  837. gRendererUtility().setPassParams(mMeshParamSets[typeIdx][entry.paramsIdx]);
  838. gRendererUtility().draw(entry.mesh, entry.subMesh);
  839. }
  840. }
  841. else
  842. {
  843. // Allocate and assign GPU program parameter objects
  844. UINT32 pickingCounters[2];
  845. bs_zero_out(pickingCounters);
  846. for (auto& entry : meshes)
  847. {
  848. UINT32 typeIdx = entry.type == GizmoMeshType::Text ? 1 : 0;
  849. UINT32 paramsIdx = pickingCounters[typeIdx];
  850. entry.paramsIdx = paramsIdx;
  851. if (paramsIdx >= mPickingParamSets[typeIdx].size())
  852. {
  853. SPtr<GpuParamsSet> paramsSet = mPickingMaterials[typeIdx]->createParamsSet();
  854. paramsSet->setParamBlockBuffer("Uniforms", mMeshPickingParamBuffer, true);
  855. mPickingParamSets[typeIdx].push_back(paramsSet);
  856. }
  857. pickingCounters[typeIdx]++;
  858. }
  859. for (auto& iconData : *iconRenderData)
  860. {
  861. iconData.paramsIdx = pickingCounters[1];
  862. if (iconData.paramsIdx >= mPickingParamSets[1].size())
  863. {
  864. SPtr<GpuParamsSet> paramsSet = mPickingMaterials[1]->createParamsSet();
  865. paramsSet->setParamBlockBuffer("Uniforms", mIconPickingParamBuffer, true);
  866. mPickingParamSets[1].push_back(paramsSet);
  867. }
  868. pickingCounters[1]++;
  869. }
  870. gGizmoPickingParamBlockDef.gMatViewProj.set(mMeshPickingParamBuffer, viewProjMat);
  871. gGizmoPickingParamBlockDef.gAlphaCutoff.set(mMeshPickingParamBuffer, PICKING_ALPHA_CUTOFF);
  872. for (auto& entry : meshes)
  873. {
  874. UINT32 typeIdx = entry.type == GizmoMeshType::Text ? 1 : 0;
  875. gRendererUtility().setPass(mPickingMaterials[typeIdx]);
  876. gRendererUtility().setPassParams(mPickingParamSets[typeIdx][entry.paramsIdx]);
  877. gRendererUtility().draw(entry.mesh, entry.subMesh);
  878. }
  879. }
  880. if (iconMesh != nullptr)
  881. renderIconGizmos(screenArea, iconMesh, iconRenderData, usePickingMaterial);
  882. }
  883. void GizmoRenderer::renderIconGizmos(Rect2I screenArea, SPtr<MeshBase> mesh,
  884. GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
  885. {
  886. RenderAPI& rapi = RenderAPI::instance();
  887. SPtr<VertexData> vertexData = mesh->getVertexData();
  888. rapi.setVertexDeclaration(vertexData->vertexDeclaration);
  889. auto vertexBuffers = vertexData->getBuffers();
  890. SPtr<VertexBuffer> vertBuffers[1] = { vertexBuffers.begin()->second };
  891. rapi.setVertexBuffers(0, vertBuffers, 1);
  892. SPtr<IndexBuffer> indexBuffer = mesh->getIndexBuffer();
  893. rapi.setIndexBuffer(indexBuffer);
  894. rapi.setDrawOperation(DOT_TRIANGLE_LIST);
  895. // Set up ortho matrix
  896. Matrix4 projMat;
  897. const RenderAPIInfo& rapiInfo = rapi.getAPIInfo();
  898. float left = screenArea.x + rapiInfo.getHorizontalTexelOffset();
  899. float right = screenArea.x + screenArea.width + rapiInfo.getHorizontalTexelOffset();
  900. float top = screenArea.y + rapiInfo.getVerticalTexelOffset();
  901. float bottom = screenArea.y + screenArea.height + rapiInfo.getVerticalTexelOffset();
  902. float near = rapiInfo.getMinimumDepthInputValue();
  903. float far = rapiInfo.getMaximumDepthInputValue();
  904. // Top/bottom have been swapped because we're moving from window coordinates (origin top left)
  905. // to normalized device coordinates (origin bottom left)
  906. // Negative near/far because Z is flipped for normalized device coordinates
  907. // (positive Z goes into screen as opposed to view space here we're looking along negative Z)
  908. projMat.makeProjectionOrtho(left, right, top, bottom, -near, -far);
  909. if (!usePickingMaterial)
  910. {
  911. gGizmoParamBlockDef.gMatViewProj.set(mIconGizmoBuffer, projMat);
  912. gGizmoParamBlockDef.gViewDir.set(mIconGizmoBuffer, Vector4::ZERO);
  913. for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
  914. {
  915. gRendererUtility().setPass(mIconMaterial, passIdx);
  916. UINT32 curIndexOffset = mesh->getIndexOffset();
  917. for (auto curRenderData : *renderData)
  918. {
  919. gRendererUtility().setPassParams(mIconParamSets[curRenderData.paramsIdx], passIdx);
  920. rapi.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  921. curIndexOffset += curRenderData.count * 6;
  922. }
  923. }
  924. }
  925. else
  926. {
  927. gGizmoPickingParamBlockDef.gMatViewProj.set(mIconPickingParamBuffer, projMat);
  928. gGizmoPickingParamBlockDef.gAlphaCutoff.set(mIconPickingParamBuffer, PICKING_ALPHA_CUTOFF);
  929. for (auto& iconData : *renderData)
  930. {
  931. SPtr<GpuParamsSet> paramsSet = mPickingParamSets[1][iconData.paramsIdx];
  932. SPtr<GpuParams> params = paramsSet->getGpuParams();
  933. GpuParamTexture textureParam;
  934. params->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", textureParam);
  935. textureParam.set(iconData.texture);
  936. }
  937. gRendererUtility().setPass(mPickingMaterials[1]);
  938. UINT32 curIndexOffset = mesh->getIndexOffset();
  939. for (auto curRenderData : *renderData)
  940. {
  941. gRendererUtility().setPassParams(mPickingParamSets[1][curRenderData.paramsIdx]);
  942. rapi.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  943. curIndexOffset += curRenderData.count * 6;
  944. }
  945. }
  946. mesh->_notifyUsedOnGPU();
  947. }
  948. }
  949. }