BsGizmoManager.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGizmoManager.h"
  4. #include "BsMesh.h"
  5. #include "BsAABox.h"
  6. #include "BsSphere.h"
  7. #include "BsVertexDataDesc.h"
  8. #include "BsShapeMeshes3D.h"
  9. #include "BsMeshHeap.h"
  10. #include "BsCCamera.h"
  11. #include "BsSpriteTexture.h"
  12. #include "BsCoreThread.h"
  13. #include "BsBuiltinEditorResources.h"
  14. #include "BsMaterial.h"
  15. #include "BsGpuParams.h"
  16. #include "BsRenderAPI.h"
  17. #include "BsCoreRenderer.h"
  18. #include "BsRendererUtility.h"
  19. #include "BsTransientMesh.h"
  20. #include "BsRendererManager.h"
  21. #include "BsDrawHelper.h"
  22. using namespace std::placeholders;
  23. namespace BansheeEngine
  24. {
  25. const UINT32 GizmoManager::VERTEX_BUFFER_GROWTH = 4096;
  26. const UINT32 GizmoManager::INDEX_BUFFER_GROWTH = 4096 * 2;
  27. const UINT32 GizmoManager::SPHERE_QUALITY = 1;
  28. const UINT32 GizmoManager::WIRE_SPHERE_QUALITY = 10;
  29. const float GizmoManager::MAX_ICON_RANGE = 500.0f;
  30. const UINT32 GizmoManager::OPTIMAL_ICON_SIZE = 64;
  31. const float GizmoManager::ICON_TEXEL_WORLD_SIZE = 0.05f;
  32. GizmoManager::GizmoManager()
  33. : mPickable(false), mCurrentIdx(0), mTransformDirty(false), mColorDirty(false), mDrawHelper(nullptr)
  34. , mPickingDrawHelper(nullptr), mCore(nullptr)
  35. {
  36. mTransform = Matrix4::IDENTITY;
  37. mDrawHelper = bs_new<DrawHelper>();
  38. mPickingDrawHelper = bs_new<DrawHelper>();
  39. mIconVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  40. mIconVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  41. mIconVertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD);
  42. mIconVertexDesc->addVertElem(VET_COLOR, VES_COLOR, 0);
  43. mIconVertexDesc->addVertElem(VET_COLOR, VES_COLOR, 1);
  44. mIconMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mIconVertexDesc);
  45. HMaterial solidMaterial = BuiltinEditorResources::instance().createSolidGizmoMat();
  46. HMaterial wireMaterial = BuiltinEditorResources::instance().createWireGizmoMat();
  47. HMaterial lineMaterial = BuiltinEditorResources::instance().createLineGizmoMat();
  48. HMaterial iconMaterial = BuiltinEditorResources::instance().createIconGizmoMat();
  49. HMaterial textMaterial = BuiltinEditorResources::instance().createTextGizmoMat();
  50. HMaterial pickingMaterial = BuiltinEditorResources::instance().createGizmoPickingMat();
  51. HMaterial alphaPickingMaterial = BuiltinEditorResources::instance().createAlphaGizmoPickingMat();
  52. CoreInitData initData;
  53. initData.solidMat = solidMaterial->getCore();
  54. initData.wireMat = wireMaterial->getCore();
  55. initData.lineMat = lineMaterial->getCore();
  56. initData.iconMat = iconMaterial->getCore();
  57. initData.textMat = textMaterial->getCore();
  58. initData.pickingMat = pickingMaterial->getCore();
  59. initData.alphaPickingMat = alphaPickingMaterial->getCore();
  60. mCore.store(bs_new<GizmoManagerCore>(GizmoManagerCore::PrivatelyConstuct()), std::memory_order_release);
  61. gCoreAccessor().queueCommand(std::bind(&GizmoManager::initializeCore, this, initData));
  62. }
  63. GizmoManager::~GizmoManager()
  64. {
  65. mDrawHelper->clearMeshes(mActiveMeshes);
  66. mActiveMeshes.clear();
  67. if (mIconMesh != nullptr)
  68. mIconMeshHeap->dealloc(mIconMesh);
  69. bs_delete(mDrawHelper);
  70. bs_delete(mPickingDrawHelper);
  71. gCoreAccessor().queueCommand(std::bind(&GizmoManager::destroyCore, this, mCore.load(std::memory_order_relaxed)));
  72. }
  73. void GizmoManager::initializeCore(const CoreInitData& initData)
  74. {
  75. mCore.load(std::memory_order_acquire)->initialize(initData);
  76. }
  77. void GizmoManager::destroyCore(GizmoManagerCore* core)
  78. {
  79. bs_delete(core);
  80. }
  81. void GizmoManager::startGizmo(const HSceneObject& gizmoParent)
  82. {
  83. mActiveSO = gizmoParent;
  84. if(mTransformDirty)
  85. {
  86. mTransform = Matrix4::IDENTITY;
  87. mTransformDirty = false;
  88. }
  89. if(mColorDirty)
  90. {
  91. mColor = Color();
  92. mColorDirty = false;
  93. }
  94. }
  95. void GizmoManager::endGizmo()
  96. {
  97. mActiveSO = nullptr;
  98. }
  99. void GizmoManager::setColor(const Color& color)
  100. {
  101. mDrawHelper->setColor(color);
  102. mColor = color;
  103. mColorDirty = true;
  104. }
  105. void GizmoManager::setTransform(const Matrix4& transform)
  106. {
  107. mDrawHelper->setTransform(transform);
  108. mTransform = transform;
  109. mTransformDirty = true;
  110. }
  111. void GizmoManager::drawCube(const Vector3& position, const Vector3& extents)
  112. {
  113. mSolidCubeData.push_back(CubeData());
  114. CubeData& cubeData = mSolidCubeData.back();
  115. cubeData.idx = mCurrentIdx++;
  116. cubeData.position = position;
  117. cubeData.extents = extents;
  118. cubeData.color = mColor;
  119. cubeData.transform = mTransform;
  120. cubeData.sceneObject = mActiveSO;
  121. cubeData.pickable = mPickable;
  122. mDrawHelper->cube(position, extents);
  123. mIdxToSceneObjectMap[cubeData.idx] = mActiveSO;
  124. }
  125. void GizmoManager::drawSphere(const Vector3& position, float radius)
  126. {
  127. mSolidSphereData.push_back(SphereData());
  128. SphereData& sphereData = mSolidSphereData.back();
  129. sphereData.idx = mCurrentIdx++;
  130. sphereData.position = position;
  131. sphereData.radius = radius;
  132. sphereData.color = mColor;
  133. sphereData.transform = mTransform;
  134. sphereData.sceneObject = mActiveSO;
  135. sphereData.pickable = mPickable;
  136. mDrawHelper->sphere(position, radius);
  137. mIdxToSceneObjectMap[sphereData.idx] = mActiveSO;
  138. }
  139. void GizmoManager::drawCone(const Vector3& base, const Vector3& normal, float height, float radius, const Vector2& scale)
  140. {
  141. mSolidConeData.push_back(ConeData());
  142. ConeData& sphereData = mSolidConeData.back();
  143. sphereData.idx = mCurrentIdx++;
  144. sphereData.base = base;
  145. sphereData.radius = radius;
  146. sphereData.color = mColor;
  147. sphereData.transform = mTransform;
  148. sphereData.sceneObject = mActiveSO;
  149. sphereData.pickable = mPickable;
  150. mDrawHelper->cone(base, normal, height, radius, scale);
  151. mIdxToSceneObjectMap[sphereData.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& sphereData = mWireConeData.back();
  211. sphereData.idx = mCurrentIdx++;
  212. sphereData.base = base;
  213. sphereData.radius = radius;
  214. sphereData.color = mColor;
  215. sphereData.transform = mTransform;
  216. sphereData.sceneObject = mActiveSO;
  217. sphereData.pickable = mPickable;
  218. mDrawHelper->wireCone(base, normal, height, radius, scale);
  219. mIdxToSceneObjectMap[sphereData.idx] = mActiveSO;
  220. }
  221. void GizmoManager::drawLine(const Vector3& start, const Vector3& end)
  222. {
  223. mLineData.push_back(LineData());
  224. LineData& lineData = mLineData.back();
  225. lineData.idx = mCurrentIdx++;
  226. lineData.start = start;
  227. lineData.end = end;
  228. lineData.color = mColor;
  229. lineData.transform = mTransform;
  230. lineData.sceneObject = mActiveSO;
  231. lineData.pickable = mPickable;
  232. mDrawHelper->line(start, end);
  233. mIdxToSceneObjectMap[lineData.idx] = mActiveSO;
  234. }
  235. void GizmoManager::drawLineList(const Vector<Vector3>& linePoints)
  236. {
  237. mLineListData.push_back(LineListData());
  238. LineListData& lineListData = mLineListData.back();
  239. lineListData.idx = mCurrentIdx++;
  240. lineListData.linePoints = linePoints;
  241. lineListData.color = mColor;
  242. lineListData.transform = mTransform;
  243. lineListData.sceneObject = mActiveSO;
  244. lineListData.pickable = mPickable;
  245. mDrawHelper->lineList(linePoints);
  246. mIdxToSceneObjectMap[lineListData.idx] = mActiveSO;
  247. }
  248. void GizmoManager::drawWireDisc(const Vector3& position, const Vector3& normal, float radius)
  249. {
  250. mWireDiscData.push_back(WireDiscData());
  251. WireDiscData& wireDiscData = mWireDiscData.back();
  252. wireDiscData.idx = mCurrentIdx++;
  253. wireDiscData.position = position;
  254. wireDiscData.normal = normal;
  255. wireDiscData.radius = radius;
  256. wireDiscData.color = mColor;
  257. wireDiscData.transform = mTransform;
  258. wireDiscData.sceneObject = mActiveSO;
  259. wireDiscData.pickable = mPickable;
  260. mDrawHelper->wireDisc(position, normal, radius);
  261. mIdxToSceneObjectMap[wireDiscData.idx] = mActiveSO;
  262. }
  263. void GizmoManager::drawWireArc(const Vector3& position, const Vector3& normal, float radius,
  264. Degree startAngle, Degree amountAngle)
  265. {
  266. mWireArcData.push_back(WireArcData());
  267. WireArcData& wireArcData = mWireArcData.back();
  268. wireArcData.idx = mCurrentIdx++;
  269. wireArcData.position = position;
  270. wireArcData.normal = normal;
  271. wireArcData.radius = radius;
  272. wireArcData.startAngle = startAngle;
  273. wireArcData.amountAngle = amountAngle;
  274. wireArcData.color = mColor;
  275. wireArcData.transform = mTransform;
  276. wireArcData.sceneObject = mActiveSO;
  277. wireArcData.pickable = mPickable;
  278. mDrawHelper->wireArc(position, normal, radius, startAngle, amountAngle);
  279. mIdxToSceneObjectMap[wireArcData.idx] = mActiveSO;
  280. }
  281. void GizmoManager::drawWireMesh(const MeshDataPtr& meshData)
  282. {
  283. mWireMeshData.push_back(WireMeshData());
  284. WireMeshData& wireMeshData = mWireMeshData.back();
  285. wireMeshData.idx = mCurrentIdx++;
  286. wireMeshData.meshData = meshData;
  287. wireMeshData.color = mColor;
  288. wireMeshData.transform = mTransform;
  289. wireMeshData.sceneObject = mActiveSO;
  290. wireMeshData.pickable = mPickable;
  291. mDrawHelper->wireMesh(meshData);
  292. mIdxToSceneObjectMap[wireMeshData.idx] = mActiveSO;
  293. }
  294. void GizmoManager::drawFrustum(const Vector3& position, float aspect, Degree FOV, float near, float far)
  295. {
  296. mFrustumData.push_back(FrustumData());
  297. FrustumData& frustumData = mFrustumData.back();
  298. frustumData.idx = mCurrentIdx++;
  299. frustumData.position = position;
  300. frustumData.aspect = aspect;
  301. frustumData.FOV = FOV;
  302. frustumData.near = near;
  303. frustumData.far = far;
  304. frustumData.color = mColor;
  305. frustumData.transform = mTransform;
  306. frustumData.sceneObject = mActiveSO;
  307. frustumData.pickable = mPickable;
  308. mDrawHelper->frustum(position, aspect, FOV, near, far);
  309. mIdxToSceneObjectMap[frustumData.idx] = mActiveSO;
  310. }
  311. void GizmoManager::drawIcon(Vector3 position, HSpriteTexture image, bool fixedScale)
  312. {
  313. mIconData.push_back(IconData());
  314. IconData& iconData = mIconData.back();
  315. iconData.idx = mCurrentIdx++;
  316. iconData.position = position;
  317. iconData.texture = image;
  318. iconData.fixedScale = fixedScale;
  319. iconData.color = mColor;
  320. iconData.transform = mTransform;
  321. iconData.sceneObject = mActiveSO;
  322. iconData.pickable = mPickable;
  323. mIdxToSceneObjectMap[iconData.idx] = mActiveSO;
  324. }
  325. void GizmoManager::drawText(const Vector3& position, const WString& text, const HFont& font, UINT32 fontSize)
  326. {
  327. HFont myFont = font;
  328. if (myFont == nullptr)
  329. myFont = BuiltinEditorResources::instance().getDefaultFont();
  330. mTextData.push_back(TextData());
  331. TextData& textData = mTextData.back();
  332. textData.idx = mCurrentIdx++;
  333. textData.position = position;
  334. textData.text = text;
  335. textData.font = myFont;
  336. textData.fontSize = fontSize;
  337. textData.color = mColor;
  338. textData.transform = mTransform;
  339. textData.sceneObject = mActiveSO;
  340. textData.pickable = mPickable;
  341. mDrawHelper->text(position, text, myFont, fontSize);
  342. mIdxToSceneObjectMap[textData.idx] = mActiveSO;
  343. }
  344. void GizmoManager::update(const CameraPtr& camera)
  345. {
  346. mDrawHelper->clearMeshes(mActiveMeshes);
  347. mActiveMeshes.clear();
  348. if (mIconMesh != nullptr)
  349. mIconMeshHeap->dealloc(mIconMesh);
  350. IconRenderDataVecPtr iconRenderData;
  351. mDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera->getPosition());
  352. mActiveMeshes = mDrawHelper->getMeshes();
  353. Vector<GizmoManagerCore::MeshData> proxyData;
  354. for (auto& meshData : mActiveMeshes)
  355. {
  356. SPtr<TextureCore> tex;
  357. if (meshData.texture.isLoaded())
  358. tex = meshData.texture->getCore();
  359. if (meshData.type == DrawHelper::MeshType::Solid)
  360. {
  361. proxyData.push_back(GizmoManagerCore::MeshData(
  362. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Solid));
  363. }
  364. else if(meshData.type == DrawHelper::MeshType::Wire)
  365. {
  366. proxyData.push_back(GizmoManagerCore::MeshData(
  367. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Wire));
  368. }
  369. else if (meshData.type == DrawHelper::MeshType::Line)
  370. {
  371. proxyData.push_back(GizmoManagerCore::MeshData(
  372. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Line));
  373. }
  374. else // Text
  375. {
  376. proxyData.push_back(GizmoManagerCore::MeshData(
  377. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Text));
  378. }
  379. }
  380. mIconMesh = buildIconMesh(camera, mIconData, false, iconRenderData);
  381. SPtr<MeshCoreBase> iconMesh = mIconMesh->getCore();
  382. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  383. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, core, camera->getCore(),
  384. proxyData, iconMesh, iconRenderData));
  385. }
  386. void GizmoManager::renderForPicking(const CameraPtr& camera, std::function<Color(UINT32)> idxToColorCallback)
  387. {
  388. Vector<IconData> iconData;
  389. IconRenderDataVecPtr iconRenderData;
  390. mPickingDrawHelper->clear();
  391. for (auto& cubeDataEntry : mSolidCubeData)
  392. {
  393. if (!cubeDataEntry.pickable)
  394. continue;
  395. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  396. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  397. mPickingDrawHelper->cube(cubeDataEntry.position, cubeDataEntry.extents);
  398. }
  399. for (auto& cubeDataEntry : mWireCubeData)
  400. {
  401. if (!cubeDataEntry.pickable)
  402. continue;
  403. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  404. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  405. mPickingDrawHelper->wireCube(cubeDataEntry.position, cubeDataEntry.extents);
  406. }
  407. for (auto& sphereDataEntry : mSolidSphereData)
  408. {
  409. if (!sphereDataEntry.pickable)
  410. continue;
  411. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  412. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  413. mPickingDrawHelper->sphere(sphereDataEntry.position, sphereDataEntry.radius);
  414. }
  415. for (auto& sphereDataEntry : mWireSphereData)
  416. {
  417. if (!sphereDataEntry.pickable)
  418. continue;
  419. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  420. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  421. mPickingDrawHelper->wireSphere(sphereDataEntry.position, sphereDataEntry.radius);
  422. }
  423. for (auto& coneDataEntry : mSolidConeData)
  424. {
  425. if (!coneDataEntry.pickable)
  426. continue;
  427. mPickingDrawHelper->setColor(idxToColorCallback(coneDataEntry.idx));
  428. mPickingDrawHelper->setTransform(coneDataEntry.transform);
  429. mPickingDrawHelper->cone(coneDataEntry.base, coneDataEntry.normal, coneDataEntry.radius, coneDataEntry.radius,
  430. coneDataEntry.scale);
  431. }
  432. for (auto& coneDataEntry : mWireConeData)
  433. {
  434. if (!coneDataEntry.pickable)
  435. continue;
  436. mPickingDrawHelper->setColor(idxToColorCallback(coneDataEntry.idx));
  437. mPickingDrawHelper->setTransform(coneDataEntry.transform);
  438. mPickingDrawHelper->wireCone(coneDataEntry.base, coneDataEntry.normal, coneDataEntry.radius, coneDataEntry.radius,
  439. coneDataEntry.scale);
  440. }
  441. for (auto& lineDataEntry : mLineData)
  442. {
  443. if (!lineDataEntry.pickable)
  444. continue;
  445. mPickingDrawHelper->setColor(idxToColorCallback(lineDataEntry.idx));
  446. mPickingDrawHelper->setTransform(lineDataEntry.transform);
  447. mPickingDrawHelper->line(lineDataEntry.start, lineDataEntry.end);
  448. }
  449. for (auto& lineListDataEntry : mLineListData)
  450. {
  451. if (!lineListDataEntry.pickable)
  452. continue;
  453. mPickingDrawHelper->setColor(idxToColorCallback(lineListDataEntry.idx));
  454. mPickingDrawHelper->setTransform(lineListDataEntry.transform);
  455. mPickingDrawHelper->lineList(lineListDataEntry.linePoints);
  456. }
  457. for (auto& wireDiscDataEntry : mWireDiscData)
  458. {
  459. if (!wireDiscDataEntry.pickable)
  460. continue;
  461. mPickingDrawHelper->setColor(idxToColorCallback(wireDiscDataEntry.idx));
  462. mPickingDrawHelper->setTransform(wireDiscDataEntry.transform);
  463. mPickingDrawHelper->wireDisc(wireDiscDataEntry.position, wireDiscDataEntry.normal, wireDiscDataEntry.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. mPickingDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera->getPosition());
  508. const Vector<DrawHelper::ShapeMeshData>& meshes = mPickingDrawHelper->getMeshes();
  509. TransientMeshPtr iconMesh = buildIconMesh(camera, iconData, true, iconRenderData);
  510. // Note: This must be rendered while Scene view is being rendered
  511. Matrix4 viewMat = camera->getViewMatrix();
  512. Matrix4 projMat = camera->getProjectionMatrixRS();
  513. ViewportPtr viewport = camera->getViewport();
  514. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  515. for (auto& meshData : meshes)
  516. {
  517. SPtr<TextureCore> tex;
  518. if (meshData.texture.isLoaded())
  519. tex = meshData.texture->getCore();
  520. if(meshData.type == DrawHelper::MeshType::Text)
  521. {
  522. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos, core, viewMat, projMat,
  523. camera->getForward(), meshData.mesh->getCore(), tex, GizmoMaterial::PickingAlpha));
  524. }
  525. else
  526. {
  527. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos, core, viewMat, projMat,
  528. camera->getForward(), meshData.mesh->getCore(), tex, GizmoMaterial::Picking));
  529. }
  530. }
  531. Rect2I screenArea = camera->getViewport()->getArea();
  532. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderIconGizmos,
  533. core, screenArea, iconMesh->getCore(), iconRenderData, true));
  534. mPickingDrawHelper->clearMeshes(meshes);
  535. mIconMeshHeap->dealloc(iconMesh);
  536. }
  537. void GizmoManager::clearGizmos()
  538. {
  539. mSolidCubeData.clear();
  540. mWireCubeData.clear();
  541. mSolidSphereData.clear();
  542. mWireSphereData.clear();
  543. mSolidConeData.clear();
  544. mWireConeData.clear();
  545. mLineData.clear();
  546. mLineListData.clear();
  547. mWireDiscData.clear();
  548. mWireArcData.clear();
  549. mWireMeshData.clear();
  550. mFrustumData.clear();
  551. mTextData.clear();
  552. mIconData.clear();
  553. mIdxToSceneObjectMap.clear();
  554. mDrawHelper->clear();
  555. mCurrentIdx = 0;
  556. }
  557. void GizmoManager::clearRenderData()
  558. {
  559. mDrawHelper->clearMeshes(mActiveMeshes);
  560. mActiveMeshes.clear();
  561. if (mIconMesh != nullptr)
  562. mIconMeshHeap->dealloc(mIconMesh);
  563. mIconMesh = nullptr;
  564. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  565. IconRenderDataVecPtr iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
  566. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, core,
  567. nullptr, Vector<GizmoManagerCore::MeshData>(), nullptr, iconRenderData));
  568. }
  569. TransientMeshPtr GizmoManager::buildIconMesh(const CameraPtr& camera, const Vector<IconData>& iconData,
  570. bool forPicking, GizmoManager::IconRenderDataVecPtr& iconRenderData)
  571. {
  572. mSortedIconData.clear();
  573. if (iconData.size() > mSortedIconData.size())
  574. mSortedIconData.resize(iconData.size());
  575. UINT32 i = 0;
  576. for (auto& iconEntry : iconData)
  577. {
  578. Vector3 viewPoint = camera->worldToViewPoint(iconEntry.position);
  579. float distance = -viewPoint.z;
  580. if (distance < camera->getNearClipDistance()) // Ignore behind clip plane
  581. continue;
  582. if (distance > MAX_ICON_RANGE) // Ignore too far away
  583. continue;
  584. if (!iconEntry.texture.isLoaded()) // Ignore missing texture
  585. continue;
  586. if (forPicking && !iconEntry.pickable)
  587. continue;
  588. SortedIconData& sortedIconData = mSortedIconData[i];
  589. sortedIconData.iconIdx = i;
  590. sortedIconData.distance = distance;
  591. sortedIconData.screenPosition = camera->viewToScreenPoint(viewPoint);
  592. i++;
  593. }
  594. UINT32 actualNumIcons = i;
  595. // Sort back to front first, then by texture
  596. std::sort(mSortedIconData.begin(), mSortedIconData.begin() + actualNumIcons,
  597. [&](const SortedIconData& a, const SortedIconData& b)
  598. {
  599. if (a.distance == b.distance)
  600. {
  601. HTexture texA = iconData[a.iconIdx].texture->getTexture();
  602. HTexture texB = iconData[b.iconIdx].texture->getTexture();
  603. if (texA == texB)
  604. return a.iconIdx < b.iconIdx;
  605. return texA->getInternalID() < texB->getInternalID();
  606. }
  607. else
  608. return a.distance > b.distance;
  609. });
  610. MeshDataPtr meshData = bs_shared_ptr_new<MeshData>(actualNumIcons * 4, actualNumIcons * 6, mIconVertexDesc);
  611. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  612. auto texcoordIter = meshData->getVec2DataIter(VES_TEXCOORD);
  613. auto normalColorIter = meshData->getDWORDDataIter(VES_COLOR, 0);
  614. auto fadedColorIter = meshData->getDWORDDataIter(VES_COLOR, 1);
  615. UINT32* indices = meshData->getIndices32();
  616. float cameraScale = 1.0f;
  617. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  618. cameraScale = camera->getViewport()->getHeight() / camera->getOrthoWindowHeight();
  619. else
  620. {
  621. Radian vertFOV(Math::tan(camera->getHorzFOV() * 0.5f));
  622. cameraScale = (camera->getViewport()->getHeight() * 0.5f) / vertFOV.valueRadians();
  623. }
  624. iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
  625. UINT32 lastTextureIdx = std::numeric_limits<UINT32>::max();
  626. HTexture curTexture;
  627. // Note: This assumes the meshes will be rendered using the same camera
  628. // properties as when they are created
  629. for (i = 0; i < actualNumIcons; i++)
  630. {
  631. SortedIconData& sortedIconData = mSortedIconData[i];
  632. const IconData& curIconData = iconData[sortedIconData.iconIdx];
  633. HTexture atlasTexture = curIconData.texture->getTexture();
  634. if (curTexture != atlasTexture)
  635. {
  636. UINT32 numIconsPerTexture = i - lastTextureIdx;
  637. if (numIconsPerTexture > 0)
  638. {
  639. iconRenderData->push_back(IconRenderData());
  640. IconRenderData& renderData = iconRenderData->back();
  641. renderData.count = numIconsPerTexture;
  642. renderData.texture = atlasTexture->getCore();
  643. }
  644. lastTextureIdx = i;
  645. curTexture = atlasTexture;
  646. }
  647. UINT32 iconWidth = curIconData.texture->getWidth();
  648. UINT32 iconHeight = curIconData.texture->getHeight();
  649. limitIconSize(iconWidth, iconHeight);
  650. Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, -sortedIconData.distance);
  651. Vector3 projPosition = camera->projectPoint(position);
  652. position.z = projPosition.z;
  653. float halfWidth = iconWidth * 0.5f;
  654. float halfHeight = iconHeight * 0.5f;
  655. if (!curIconData.fixedScale)
  656. {
  657. float iconScale = 1.0f;
  658. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  659. iconScale = cameraScale * ICON_TEXEL_WORLD_SIZE;
  660. else
  661. iconScale = (cameraScale * ICON_TEXEL_WORLD_SIZE) / sortedIconData.distance;
  662. halfWidth *= iconScale;
  663. halfHeight *= iconScale;
  664. }
  665. Color normalColor, fadedColor;
  666. calculateIconColors(curIconData.color, camera, (UINT32)(halfHeight * 2.0f), curIconData.fixedScale, normalColor, fadedColor);
  667. if (forPicking)
  668. {
  669. normalColor = curIconData.color;
  670. fadedColor = curIconData.color;
  671. }
  672. Vector3 positions[4];
  673. positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
  674. positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
  675. positions[2] = position + Vector3(halfWidth, halfHeight, 0.0f);
  676. positions[3] = position + Vector3(-halfWidth, halfHeight, 0.0f);
  677. Vector2 uvs[4];
  678. uvs[0] = curIconData.texture->transformUV(Vector2(0.0f, 0.0f));
  679. uvs[1] = curIconData.texture->transformUV(Vector2(1.0f, 0.0f));
  680. uvs[2] = curIconData.texture->transformUV(Vector2(1.0f, 1.0f));
  681. uvs[3] = curIconData.texture->transformUV(Vector2(0.0f, 1.0f));
  682. for (UINT32 j = 0; j < 4; j++)
  683. {
  684. positionIter.addValue(positions[j]);
  685. texcoordIter.addValue(uvs[j]);
  686. normalColorIter.addValue(normalColor.getAsRGBA());
  687. fadedColorIter.addValue(fadedColor.getAsRGBA());
  688. }
  689. UINT32 vertOffset = i * 4;
  690. indices[0] = vertOffset + 0;
  691. indices[1] = vertOffset + 1;
  692. indices[2] = vertOffset + 2;
  693. indices[3] = vertOffset + 0;
  694. indices[4] = vertOffset + 2;
  695. indices[5] = vertOffset + 3;
  696. indices += 6;
  697. }
  698. return mIconMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  699. }
  700. void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
  701. {
  702. if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
  703. return;
  704. float relWidth = OPTIMAL_ICON_SIZE / (float)width;
  705. float relHeight = OPTIMAL_ICON_SIZE / (float)height;
  706. float scale = std::min(relWidth, relHeight);
  707. width = Math::roundToInt(width * scale);
  708. height = Math::roundToInt(height * scale);
  709. }
  710. void GizmoManager::calculateIconColors(const Color& tint, const CameraPtr& camera,
  711. UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
  712. {
  713. normalColor = tint;
  714. if (!fixedScale)
  715. {
  716. float iconToScreenRatio = iconHeight / (float)camera->getViewport()->getHeight();
  717. if (iconToScreenRatio > 0.3f)
  718. {
  719. float alpha = 1.0f - Math::lerp01(iconToScreenRatio, 0.3f, 1.0f);
  720. normalColor.a *= alpha;
  721. }
  722. else if (iconToScreenRatio < 0.1f)
  723. {
  724. float alpha = Math::lerp01(iconToScreenRatio, 0.0f, 0.1f);
  725. normalColor.a *= alpha;
  726. }
  727. }
  728. fadedColor = normalColor;
  729. fadedColor.a *= 0.2f;
  730. }
  731. HSceneObject GizmoManager::getSceneObject(UINT32 gizmoIdx)
  732. {
  733. auto iterFind = mIdxToSceneObjectMap.find(gizmoIdx);
  734. if (iterFind != mIdxToSceneObjectMap.end())
  735. return iterFind->second;
  736. return HSceneObject();
  737. }
  738. const float GizmoManagerCore::PICKING_ALPHA_CUTOFF = 0.5f;
  739. GizmoManagerCore::GizmoManagerCore(const PrivatelyConstuct& dummy)
  740. {
  741. }
  742. GizmoManagerCore::~GizmoManagerCore()
  743. {
  744. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  745. if (mCamera != nullptr)
  746. activeRenderer->_unregisterRenderCallback(mCamera.get(), 20);
  747. }
  748. void GizmoManagerCore::initialize(const GizmoManager::CoreInitData& initData)
  749. {
  750. THROW_IF_NOT_CORE_THREAD;
  751. mSolidMaterial.mat = initData.solidMat;
  752. mWireMaterial.mat = initData.wireMat;
  753. mLineMaterial.mat = initData.lineMat;
  754. mTextMaterial.mat = initData.textMat;
  755. mIconMaterial.mat = initData.iconMat;
  756. mPickingMaterial.mat = initData.pickingMat;
  757. mAlphaPickingMaterial.mat = initData.alphaPickingMat;
  758. {
  759. SPtr<MaterialCore> mat = mLineMaterial.mat;
  760. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  761. vertParams->getParam("matViewProj", mLineMaterial.mViewProj);
  762. }
  763. {
  764. SPtr<MaterialCore> mat = mSolidMaterial.mat;
  765. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  766. SPtr<GpuParamsCore> fragParams = mat->getPassParameters(0)->mFragParams;
  767. vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
  768. fragParams->getParam("viewDir", mSolidMaterial.mViewDir);
  769. }
  770. {
  771. SPtr<MaterialCore> mat = mWireMaterial.mat;
  772. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  773. SPtr<GpuParamsCore> fragParams = mat->getPassParameters(0)->mFragParams;
  774. vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
  775. }
  776. {
  777. SPtr<MaterialCore> mat = mIconMaterial.mat;
  778. SPtr<PassParametersCore> pass0Params = mat->getPassParameters(0);
  779. SPtr<PassParametersCore> pass1Params = mat->getPassParameters(1);
  780. SPtr<GpuParamsCore> vertParams0 = pass0Params->mVertParams;
  781. SPtr<GpuParamsCore> vertParams1 = pass1Params->mVertParams;
  782. vertParams0->getParam("matViewProj", mIconMaterial.mViewProj[0]);
  783. vertParams1->getParam("matViewProj", mIconMaterial.mViewProj[1]);
  784. mIconMaterial.mFragParams[0] = pass0Params->mFragParams;
  785. mIconMaterial.mFragParams[1] = pass1Params->mFragParams;
  786. mIconMaterial.mFragParams[0]->getTextureParam("mainTexture", mIconMaterial.mTexture[0]);
  787. mIconMaterial.mFragParams[1]->getTextureParam("mainTexture", mIconMaterial.mTexture[1]);
  788. }
  789. {
  790. SPtr<MaterialCore> mat = mPickingMaterial.mat;
  791. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  792. vertParams->getParam("matViewProj", mPickingMaterial.mViewProj);
  793. }
  794. {
  795. SPtr<MaterialCore> mat = mAlphaPickingMaterial.mat;
  796. SPtr<PassParametersCore> passParams = mat->getPassParameters(0);
  797. SPtr<GpuParamsCore> vertParams = passParams->mVertParams;
  798. vertParams->getParam("matViewProj", mAlphaPickingMaterial.mViewProj);
  799. mAlphaPickingMaterial.mFragParams = passParams->mFragParams;
  800. mAlphaPickingMaterial.mFragParams->getTextureParam("mainTexture", mAlphaPickingMaterial.mTexture);
  801. GpuParamFloatCore alphaCutoffParam;
  802. mAlphaPickingMaterial.mFragParams->getParam("alphaCutoff", alphaCutoffParam);
  803. alphaCutoffParam.set(PICKING_ALPHA_CUTOFF);
  804. }
  805. {
  806. SPtr<MaterialCore> mat = mTextMaterial.mat;
  807. SPtr<PassParametersCore> passParams = mat->getPassParameters(0);
  808. SPtr<GpuParamsCore> vertParams = passParams->mVertParams;
  809. SPtr<GpuParamsCore> fragParams = passParams->mFragParams;
  810. vertParams->getParam("matViewProj", mTextMaterial.mViewProj);
  811. fragParams->getTextureParam("mainTexture", mTextMaterial.mTexture);
  812. }
  813. }
  814. void GizmoManagerCore::updateData(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes,
  815. const SPtr<MeshCoreBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
  816. {
  817. if (mCamera != camera)
  818. {
  819. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  820. if (mCamera != nullptr)
  821. activeRenderer->_unregisterRenderCallback(mCamera.get(), 0);
  822. if (camera != nullptr)
  823. activeRenderer->_registerRenderCallback(camera.get(), 0, std::bind(&GizmoManagerCore::render, this));
  824. }
  825. mCamera = camera;
  826. mMeshes = meshes;
  827. mIconMesh = iconMesh;
  828. mIconRenderData = iconRenderData;
  829. }
  830. void GizmoManagerCore::render()
  831. {
  832. if (mCamera == nullptr)
  833. return;
  834. SPtr<RenderTargetCore> renderTarget = mCamera->getViewport()->getTarget();
  835. float width = (float)renderTarget->getProperties().getWidth();
  836. float height = (float)renderTarget->getProperties().getHeight();
  837. Rect2 normArea = mCamera->getViewport()->getNormArea();
  838. Rect2I screenArea;
  839. screenArea.x = (int)(normArea.x * width);
  840. screenArea.y = (int)(normArea.y * height);
  841. screenArea.width = (int)(normArea.width * width);
  842. screenArea.height = (int)(normArea.height * height);
  843. for (auto& meshData : mMeshes)
  844. {
  845. GizmoManager::GizmoMaterial material = GizmoManager::GizmoMaterial::Solid;
  846. switch(meshData.type)
  847. {
  848. case MeshType::Solid:
  849. material = GizmoManager::GizmoMaterial::Solid;
  850. break;
  851. case MeshType::Wire:
  852. material = GizmoManager::GizmoMaterial::Wire;
  853. break;
  854. case MeshType::Line:
  855. material = GizmoManager::GizmoMaterial::Line;
  856. break;
  857. case MeshType::Text:
  858. material = GizmoManager::GizmoMaterial::Text;
  859. break;
  860. }
  861. renderGizmos(mCamera->getViewMatrix(), mCamera->getProjectionMatrixRS(), mCamera->getForward(),
  862. meshData.mesh, meshData.texture, material);
  863. }
  864. if (mIconMesh != nullptr)
  865. renderIconGizmos(screenArea, mIconMesh, mIconRenderData, false);
  866. }
  867. void GizmoManagerCore::renderGizmos(const Matrix4& viewMatrix, const Matrix4& projMatrix, const Vector3& viewDir,
  868. const SPtr<MeshCoreBase>& mesh, const SPtr<TextureCore>& texture, GizmoManager::GizmoMaterial material)
  869. {
  870. THROW_IF_NOT_CORE_THREAD;
  871. Matrix4 viewProjMat = projMatrix * viewMatrix;
  872. switch (material)
  873. {
  874. case GizmoManager::GizmoMaterial::Solid:
  875. mSolidMaterial.mViewProj.set(viewProjMat);
  876. mSolidMaterial.mViewDir.set((Vector4)viewDir);
  877. gRendererUtility().setPass(mSolidMaterial.mat, 0);
  878. gRendererUtility().setPassParams(mSolidMaterial.mat);
  879. break;
  880. case GizmoManager::GizmoMaterial::Wire:
  881. mWireMaterial.mViewProj.set(viewProjMat);
  882. gRendererUtility().setPass(mWireMaterial.mat, 0);
  883. gRendererUtility().setPassParams(mWireMaterial.mat);
  884. break;
  885. case GizmoManager::GizmoMaterial::Line:
  886. mLineMaterial.mViewProj.set(viewProjMat);
  887. gRendererUtility().setPass(mLineMaterial.mat, 0);
  888. gRendererUtility().setPassParams(mLineMaterial.mat);
  889. break;
  890. case GizmoManager::GizmoMaterial::Picking:
  891. mPickingMaterial.mViewProj.set(viewProjMat);
  892. gRendererUtility().setPass(mPickingMaterial.mat, 0);
  893. gRendererUtility().setPassParams(mPickingMaterial.mat);
  894. break;
  895. case GizmoManager::GizmoMaterial::PickingAlpha:
  896. mAlphaPickingMaterial.mViewProj.set(viewProjMat);
  897. mAlphaPickingMaterial.mTexture.set(texture);
  898. gRendererUtility().setPass(mAlphaPickingMaterial.mat, 0);
  899. gRendererUtility().setPassParams(mAlphaPickingMaterial.mat);
  900. break;
  901. case GizmoManager::GizmoMaterial::Text:
  902. mTextMaterial.mViewProj.set(viewProjMat);
  903. mTextMaterial.mTexture.set(texture);
  904. gRendererUtility().setPass(mTextMaterial.mat, 0);
  905. gRendererUtility().setPassParams(mTextMaterial.mat);
  906. break;
  907. }
  908. gRendererUtility().draw(mesh, mesh->getProperties().getSubMesh(0));
  909. }
  910. void GizmoManagerCore::renderIconGizmos(Rect2I screenArea, SPtr<MeshCoreBase> mesh, GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
  911. {
  912. RenderAPICore& rapi = RenderAPICore::instance();
  913. const MeshProperties& meshProps = mesh->getProperties();
  914. std::shared_ptr<VertexData> vertexData = mesh->getVertexData();
  915. rapi.setVertexDeclaration(vertexData->vertexDeclaration);
  916. auto vertexBuffers = vertexData->getBuffers();
  917. SPtr<VertexBufferCore> vertBuffers[1] = { vertexBuffers.begin()->second };
  918. rapi.setVertexBuffers(0, vertBuffers, 1);
  919. SPtr<IndexBufferCore> indexBuffer = mesh->getIndexBuffer();
  920. rapi.setIndexBuffer(indexBuffer);
  921. rapi.setDrawOperation(DOT_TRIANGLE_LIST);
  922. // Set up ortho matrix
  923. Matrix4 projMat;
  924. const RenderAPIInfo& rapiInfo = rapi.getAPIInfo();
  925. float left = screenArea.x + rapiInfo.getHorizontalTexelOffset();
  926. float right = screenArea.x + screenArea.width + rapiInfo.getHorizontalTexelOffset();
  927. float top = screenArea.y + rapiInfo.getVerticalTexelOffset();
  928. float bottom = screenArea.y + screenArea.height + rapiInfo.getVerticalTexelOffset();
  929. float near = rapiInfo.getMinimumDepthInputValue();
  930. float far = rapiInfo.getMaximumDepthInputValue();
  931. // Top/bottom have been swapped because we're moving from window coordinates (origin top left)
  932. // to normalized device coordinates (origin bottom left)
  933. // Negative near/far because Z is flipped for normalized device coordinates
  934. // (positive Z goes into screen as opposed to view space here we're looking along negative Z)
  935. projMat.makeProjectionOrtho(left, right, top, bottom, -near, -far);
  936. if (!usePickingMaterial)
  937. {
  938. mIconMaterial.mViewProj[0].set(projMat);
  939. mIconMaterial.mViewProj[1].set(projMat);
  940. for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
  941. {
  942. gRendererUtility().setPass(mIconMaterial.mat, passIdx);
  943. UINT32 curIndexOffset = mesh->getIndexOffset();
  944. for (auto curRenderData : *renderData)
  945. {
  946. mIconMaterial.mTexture[passIdx].set(curRenderData.texture);
  947. rapi.setGpuParams(GPT_FRAGMENT_PROGRAM, mIconMaterial.mFragParams[passIdx]);
  948. rapi.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  949. curIndexOffset += curRenderData.count * 6;
  950. }
  951. }
  952. }
  953. else
  954. {
  955. mAlphaPickingMaterial.mViewProj.set(projMat);
  956. gRendererUtility().setPass(mAlphaPickingMaterial.mat, 0);
  957. UINT32 curIndexOffset = 0;
  958. for (auto curRenderData : *renderData)
  959. {
  960. mAlphaPickingMaterial.mTexture.set(curRenderData.texture);
  961. rapi.setGpuParams(GPT_FRAGMENT_PROGRAM, mAlphaPickingMaterial.mFragParams);
  962. rapi.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  963. curIndexOffset += curRenderData.count * 6;
  964. }
  965. }
  966. mesh->_notifyUsedOnGPU();
  967. }
  968. }