BsGizmoManager.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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), mDrawHelper(nullptr), mPickingDrawHelper(nullptr), mCore(nullptr), mCurrentIdx(0)
  34. {
  35. mTransform = Matrix4::IDENTITY;
  36. mDrawHelper = bs_new<DrawHelper>();
  37. mPickingDrawHelper = bs_new<DrawHelper>();
  38. mIconVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  39. mIconVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  40. mIconVertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD);
  41. mIconVertexDesc->addVertElem(VET_COLOR, VES_COLOR, 0);
  42. mIconVertexDesc->addVertElem(VET_COLOR, VES_COLOR, 1);
  43. mIconMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mIconVertexDesc);
  44. HMaterial solidMaterial = BuiltinEditorResources::instance().createSolidGizmoMat();
  45. HMaterial wireMaterial = BuiltinEditorResources::instance().createWireGizmoMat();
  46. HMaterial iconMaterial = BuiltinEditorResources::instance().createIconGizmoMat();
  47. HMaterial textMaterial = BuiltinEditorResources::instance().createTextGizmoMat();
  48. HMaterial pickingMaterial = BuiltinEditorResources::instance().createGizmoPickingMat();
  49. HMaterial alphaPickingMaterial = BuiltinEditorResources::instance().createAlphaGizmoPickingMat();
  50. CoreInitData initData;
  51. initData.solidMat = solidMaterial->getCore();
  52. initData.wireMat = wireMaterial->getCore();
  53. initData.iconMat = iconMaterial->getCore();
  54. initData.textMat = textMaterial->getCore();
  55. initData.pickingMat = pickingMaterial->getCore();
  56. initData.alphaPickingMat = alphaPickingMaterial->getCore();
  57. mCore.store(bs_new<GizmoManagerCore>(GizmoManagerCore::PrivatelyConstuct()), std::memory_order_release);
  58. gCoreAccessor().queueCommand(std::bind(&GizmoManager::initializeCore, this, initData));
  59. }
  60. GizmoManager::~GizmoManager()
  61. {
  62. mDrawHelper->clearMeshes(mActiveMeshes);
  63. mActiveMeshes.clear();
  64. if (mIconMesh != nullptr)
  65. mIconMeshHeap->dealloc(mIconMesh);
  66. bs_delete(mDrawHelper);
  67. bs_delete(mPickingDrawHelper);
  68. gCoreAccessor().queueCommand(std::bind(&GizmoManager::destroyCore, this, mCore.load(std::memory_order_relaxed)));
  69. }
  70. void GizmoManager::initializeCore(const CoreInitData& initData)
  71. {
  72. mCore.load(std::memory_order_acquire)->initialize(initData);
  73. }
  74. void GizmoManager::destroyCore(GizmoManagerCore* core)
  75. {
  76. bs_delete(core);
  77. }
  78. void GizmoManager::startGizmo(const HSceneObject& gizmoParent)
  79. {
  80. mActiveSO = gizmoParent;
  81. }
  82. void GizmoManager::endGizmo()
  83. {
  84. mActiveSO = nullptr;
  85. }
  86. void GizmoManager::setColor(const Color& color)
  87. {
  88. mDrawHelper->setColor(color);
  89. mColor = color;
  90. }
  91. void GizmoManager::setTransform(const Matrix4& transform)
  92. {
  93. mDrawHelper->setTransform(transform);
  94. mTransform = transform;
  95. }
  96. void GizmoManager::drawCube(const Vector3& position, const Vector3& extents)
  97. {
  98. mSolidCubeData.push_back(CubeData());
  99. CubeData& cubeData = mSolidCubeData.back();
  100. cubeData.idx = mCurrentIdx++;
  101. cubeData.position = position;
  102. cubeData.extents = extents;
  103. cubeData.color = mColor;
  104. cubeData.transform = mTransform;
  105. cubeData.sceneObject = mActiveSO;
  106. cubeData.pickable = mPickable;
  107. mDrawHelper->cube(position, extents);
  108. mIdxToSceneObjectMap[cubeData.idx] = mActiveSO;
  109. }
  110. void GizmoManager::drawSphere(const Vector3& position, float radius)
  111. {
  112. mSolidSphereData.push_back(SphereData());
  113. SphereData& sphereData = mSolidSphereData.back();
  114. sphereData.idx = mCurrentIdx++;
  115. sphereData.position = position;
  116. sphereData.radius = radius;
  117. sphereData.color = mColor;
  118. sphereData.transform = mTransform;
  119. sphereData.sceneObject = mActiveSO;
  120. sphereData.pickable = mPickable;
  121. mDrawHelper->sphere(position, radius);
  122. mIdxToSceneObjectMap[sphereData.idx] = mActiveSO;
  123. }
  124. void GizmoManager::drawWireCube(const Vector3& position, const Vector3& extents)
  125. {
  126. mWireCubeData.push_back(CubeData());
  127. CubeData& cubeData = mWireCubeData.back();
  128. cubeData.idx = mCurrentIdx++;
  129. cubeData.position = position;
  130. cubeData.extents = extents;
  131. cubeData.color = mColor;
  132. cubeData.transform = mTransform;
  133. cubeData.sceneObject = mActiveSO;
  134. cubeData.pickable = mPickable;
  135. mDrawHelper->wireCube(position, extents);
  136. mIdxToSceneObjectMap[cubeData.idx] = mActiveSO;
  137. }
  138. void GizmoManager::drawWireSphere(const Vector3& position, float radius)
  139. {
  140. mWireSphereData.push_back(SphereData());
  141. SphereData& sphereData = mWireSphereData.back();
  142. sphereData.idx = mCurrentIdx++;
  143. sphereData.position = position;
  144. sphereData.radius = radius;
  145. sphereData.color = mColor;
  146. sphereData.transform = mTransform;
  147. sphereData.sceneObject = mActiveSO;
  148. sphereData.pickable = mPickable;
  149. mDrawHelper->wireSphere(position, radius);
  150. mIdxToSceneObjectMap[sphereData.idx] = mActiveSO;
  151. }
  152. void GizmoManager::drawLine(const Vector3& start, const Vector3& end)
  153. {
  154. mLineData.push_back(LineData());
  155. LineData& lineData = mLineData.back();
  156. lineData.idx = mCurrentIdx++;
  157. lineData.start = start;
  158. lineData.end = end;
  159. lineData.color = mColor;
  160. lineData.transform = mTransform;
  161. lineData.sceneObject = mActiveSO;
  162. lineData.pickable = mPickable;
  163. mDrawHelper->line(start, end);
  164. mIdxToSceneObjectMap[lineData.idx] = mActiveSO;
  165. }
  166. void GizmoManager::drawWireDisc(const Vector3& position, const Vector3& normal, float radius)
  167. {
  168. mWireDiscData.push_back(WireDiscData());
  169. WireDiscData& wireDiscData = mWireDiscData.back();
  170. wireDiscData.idx = mCurrentIdx++;
  171. wireDiscData.position = position;
  172. wireDiscData.normal = normal;
  173. wireDiscData.radius = radius;
  174. wireDiscData.color = mColor;
  175. wireDiscData.transform = mTransform;
  176. wireDiscData.sceneObject = mActiveSO;
  177. wireDiscData.pickable = mPickable;
  178. mDrawHelper->wireDisc(position, normal, radius);
  179. mIdxToSceneObjectMap[wireDiscData.idx] = mActiveSO;
  180. }
  181. void GizmoManager::drawWireArc(const Vector3& position, const Vector3& normal, float radius,
  182. Degree startAngle, Degree amountAngle)
  183. {
  184. mWireArcData.push_back(WireArcData());
  185. WireArcData& wireArcData = mWireArcData.back();
  186. wireArcData.idx = mCurrentIdx++;
  187. wireArcData.position = position;
  188. wireArcData.normal = normal;
  189. wireArcData.radius = radius;
  190. wireArcData.startAngle = startAngle;
  191. wireArcData.amountAngle = amountAngle;
  192. wireArcData.color = mColor;
  193. wireArcData.transform = mTransform;
  194. wireArcData.sceneObject = mActiveSO;
  195. wireArcData.pickable = mPickable;
  196. mDrawHelper->wireArc(position, normal, radius, startAngle, amountAngle);
  197. mIdxToSceneObjectMap[wireArcData.idx] = mActiveSO;
  198. }
  199. void GizmoManager::drawFrustum(const Vector3& position, float aspect, Degree FOV, float near, float far)
  200. {
  201. mFrustumData.push_back(FrustumData());
  202. FrustumData& frustumData = mFrustumData.back();
  203. frustumData.idx = mCurrentIdx++;
  204. frustumData.position = position;
  205. frustumData.aspect = aspect;
  206. frustumData.FOV = FOV;
  207. frustumData.near = near;
  208. frustumData.far = far;
  209. frustumData.color = mColor;
  210. frustumData.transform = mTransform;
  211. frustumData.sceneObject = mActiveSO;
  212. frustumData.pickable = mPickable;
  213. mDrawHelper->frustum(position, aspect, FOV, near, far);
  214. mIdxToSceneObjectMap[frustumData.idx] = mActiveSO;
  215. }
  216. void GizmoManager::drawIcon(Vector3 position, HSpriteTexture image, bool fixedScale)
  217. {
  218. mIconData.push_back(IconData());
  219. IconData& iconData = mIconData.back();
  220. iconData.idx = mCurrentIdx++;
  221. iconData.position = position;
  222. iconData.texture = image;
  223. iconData.fixedScale = fixedScale;
  224. iconData.color = mColor;
  225. iconData.transform = mTransform;
  226. iconData.sceneObject = mActiveSO;
  227. iconData.pickable = mPickable;
  228. mIdxToSceneObjectMap[iconData.idx] = mActiveSO;
  229. }
  230. void GizmoManager::drawText(const Vector3& position, const WString& text, const HFont& font, UINT32 fontSize)
  231. {
  232. HFont myFont = font;
  233. if (myFont == nullptr)
  234. myFont = BuiltinEditorResources::instance().getDefaultFont();
  235. mTextData.push_back(TextData());
  236. TextData& textData = mTextData.back();
  237. textData.idx = mCurrentIdx++;
  238. textData.position = position;
  239. textData.text = text;
  240. textData.font = myFont;
  241. textData.fontSize = fontSize;
  242. textData.color = mColor;
  243. textData.transform = mTransform;
  244. textData.sceneObject = mActiveSO;
  245. textData.pickable = mPickable;
  246. mDrawHelper->text(position, text, myFont, fontSize);
  247. mIdxToSceneObjectMap[textData.idx] = mActiveSO;
  248. }
  249. void GizmoManager::update(const CameraPtr& camera)
  250. {
  251. mDrawHelper->clearMeshes(mActiveMeshes);
  252. mActiveMeshes.clear();
  253. if (mIconMesh != nullptr)
  254. mIconMeshHeap->dealloc(mIconMesh);
  255. IconRenderDataVecPtr iconRenderData;
  256. mDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera->getPosition());
  257. mActiveMeshes = mDrawHelper->getMeshes();
  258. Vector<GizmoManagerCore::MeshData> proxyData;
  259. for (auto& meshData : mActiveMeshes)
  260. {
  261. SPtr<TextureCore> tex;
  262. if (meshData.texture.isLoaded())
  263. tex = meshData.texture->getCore();
  264. if (meshData.type == DrawHelper::MeshType::Solid)
  265. {
  266. proxyData.push_back(GizmoManagerCore::MeshData(
  267. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Solid));
  268. }
  269. else if (meshData.type == DrawHelper::MeshType::Wire)
  270. {
  271. proxyData.push_back(GizmoManagerCore::MeshData(
  272. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Wire));
  273. }
  274. else // Text
  275. {
  276. proxyData.push_back(GizmoManagerCore::MeshData(
  277. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Text));
  278. }
  279. }
  280. mIconMesh = buildIconMesh(camera, mIconData, false, iconRenderData);
  281. SPtr<MeshCoreBase> iconMesh = mIconMesh->getCore();
  282. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  283. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, core, camera->getCore(),
  284. proxyData, iconMesh, iconRenderData));
  285. }
  286. void GizmoManager::renderForPicking(const CameraPtr& camera, std::function<Color(UINT32)> idxToColorCallback)
  287. {
  288. Vector<IconData> iconData;
  289. IconRenderDataVecPtr iconRenderData;
  290. mPickingDrawHelper->clear();
  291. for (auto& cubeDataEntry : mSolidCubeData)
  292. {
  293. if (!cubeDataEntry.pickable)
  294. continue;
  295. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  296. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  297. mPickingDrawHelper->cube(cubeDataEntry.position, cubeDataEntry.extents);
  298. }
  299. for (auto& cubeDataEntry : mWireCubeData)
  300. {
  301. if (!cubeDataEntry.pickable)
  302. continue;
  303. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  304. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  305. mPickingDrawHelper->wireCube(cubeDataEntry.position, cubeDataEntry.extents);
  306. }
  307. for (auto& sphereDataEntry : mSolidSphereData)
  308. {
  309. if (!sphereDataEntry.pickable)
  310. continue;
  311. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  312. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  313. mPickingDrawHelper->sphere(sphereDataEntry.position, sphereDataEntry.radius);
  314. }
  315. for (auto& sphereDataEntry : mWireSphereData)
  316. {
  317. if (!sphereDataEntry.pickable)
  318. continue;
  319. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  320. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  321. mPickingDrawHelper->wireSphere(sphereDataEntry.position, sphereDataEntry.radius);
  322. }
  323. for (auto& lineDataEntry : mLineData)
  324. {
  325. if (!lineDataEntry.pickable)
  326. continue;
  327. mPickingDrawHelper->setColor(idxToColorCallback(lineDataEntry.idx));
  328. mPickingDrawHelper->setTransform(lineDataEntry.transform);
  329. mPickingDrawHelper->line(lineDataEntry.start, lineDataEntry.end);
  330. }
  331. for (auto& wireDiscDataEntry : mWireDiscData)
  332. {
  333. if (!wireDiscDataEntry.pickable)
  334. continue;
  335. mPickingDrawHelper->setColor(idxToColorCallback(wireDiscDataEntry.idx));
  336. mPickingDrawHelper->setTransform(wireDiscDataEntry.transform);
  337. mPickingDrawHelper->wireDisc(wireDiscDataEntry.position, wireDiscDataEntry.normal, wireDiscDataEntry.radius);
  338. }
  339. for (auto& wireArcDataEntry : mWireArcData)
  340. {
  341. if (!wireArcDataEntry.pickable)
  342. continue;
  343. mPickingDrawHelper->setColor(idxToColorCallback(wireArcDataEntry.idx));
  344. mPickingDrawHelper->setTransform(wireArcDataEntry.transform);
  345. mPickingDrawHelper->wireArc(wireArcDataEntry.position, wireArcDataEntry.normal, wireArcDataEntry.radius,
  346. wireArcDataEntry.startAngle, wireArcDataEntry.amountAngle);
  347. }
  348. for (auto& frustumDataEntry : mFrustumData)
  349. {
  350. if (!frustumDataEntry.pickable)
  351. continue;
  352. mPickingDrawHelper->setColor(idxToColorCallback(frustumDataEntry.idx));
  353. mPickingDrawHelper->setTransform(frustumDataEntry.transform);
  354. mPickingDrawHelper->frustum(frustumDataEntry.position, frustumDataEntry.aspect, frustumDataEntry.FOV,
  355. frustumDataEntry.near, frustumDataEntry.far);
  356. }
  357. for (auto& textDataEntry : mTextData)
  358. {
  359. if (!textDataEntry.pickable)
  360. continue;
  361. mPickingDrawHelper->setColor(idxToColorCallback(textDataEntry.idx));
  362. mPickingDrawHelper->setTransform(textDataEntry.transform);
  363. mPickingDrawHelper->text(textDataEntry.position, textDataEntry.text, textDataEntry.font,
  364. textDataEntry.fontSize);
  365. }
  366. for (auto& iconDataEntry : mIconData)
  367. {
  368. if (!iconDataEntry.pickable)
  369. continue;
  370. iconData.push_back(iconDataEntry);
  371. iconData.back().color = idxToColorCallback(iconDataEntry.idx);
  372. }
  373. mPickingDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera->getPosition());
  374. const Vector<DrawHelper::ShapeMeshData>& meshes = mPickingDrawHelper->getMeshes();
  375. TransientMeshPtr iconMesh = buildIconMesh(camera, iconData, true, iconRenderData);
  376. // Note: This must be rendered while Scene view is being rendered
  377. Matrix4 viewMat = camera->getViewMatrix();
  378. Matrix4 projMat = camera->getProjectionMatrixRS();
  379. ViewportPtr viewport = camera->getViewport();
  380. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  381. for (auto& meshData : meshes)
  382. {
  383. SPtr<TextureCore> tex;
  384. if (meshData.texture.isLoaded())
  385. tex = meshData.texture->getCore();
  386. if(meshData.type == DrawHelper::MeshType::Text)
  387. {
  388. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos, core, viewMat, projMat,
  389. camera->getForward(), meshData.mesh->getCore(), tex, GizmoMaterial::PickingAlpha));
  390. }
  391. else
  392. {
  393. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos, core, viewMat, projMat,
  394. camera->getForward(), meshData.mesh->getCore(), tex, GizmoMaterial::Picking));
  395. }
  396. }
  397. Rect2I screenArea = camera->getViewport()->getArea();
  398. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderIconGizmos,
  399. core, screenArea, iconMesh->getCore(), iconRenderData, true));
  400. mPickingDrawHelper->clearMeshes(meshes);
  401. mIconMeshHeap->dealloc(iconMesh);
  402. }
  403. void GizmoManager::clearGizmos()
  404. {
  405. mSolidCubeData.clear();
  406. mWireCubeData.clear();
  407. mSolidSphereData.clear();
  408. mWireSphereData.clear();
  409. mLineData.clear();
  410. mWireDiscData.clear();
  411. mWireArcData.clear();
  412. mFrustumData.clear();
  413. mTextData.clear();
  414. mIconData.clear();
  415. mIdxToSceneObjectMap.clear();
  416. mDrawHelper->clear();
  417. mCurrentIdx = 0;
  418. }
  419. void GizmoManager::clearRenderData()
  420. {
  421. mDrawHelper->clearMeshes(mActiveMeshes);
  422. mActiveMeshes.clear();
  423. if (mIconMesh != nullptr)
  424. mIconMeshHeap->dealloc(mIconMesh);
  425. mIconMesh = nullptr;
  426. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  427. IconRenderDataVecPtr iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
  428. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, core,
  429. nullptr, Vector<GizmoManagerCore::MeshData>(), nullptr, iconRenderData));
  430. }
  431. TransientMeshPtr GizmoManager::buildIconMesh(const CameraPtr& camera, const Vector<IconData>& iconData,
  432. bool forPicking, GizmoManager::IconRenderDataVecPtr& iconRenderData)
  433. {
  434. mSortedIconData.clear();
  435. if (iconData.size() > mSortedIconData.size())
  436. mSortedIconData.resize(iconData.size());
  437. UINT32 i = 0;
  438. for (auto& iconEntry : iconData)
  439. {
  440. Vector3 viewPoint = camera->worldToViewPoint(iconEntry.position);
  441. float distance = -viewPoint.z;
  442. if (distance < camera->getNearClipDistance()) // Ignore behind clip plane
  443. continue;
  444. if (distance > MAX_ICON_RANGE) // Ignore too far away
  445. continue;
  446. if (!iconEntry.texture.isLoaded()) // Ignore missing texture
  447. continue;
  448. if (forPicking && !iconEntry.pickable)
  449. continue;
  450. SortedIconData& sortedIconData = mSortedIconData[i];
  451. sortedIconData.iconIdx = i;
  452. sortedIconData.distance = distance;
  453. sortedIconData.screenPosition = camera->viewToScreenPoint(viewPoint);
  454. i++;
  455. }
  456. UINT32 actualNumIcons = i;
  457. // Sort back to front first, then by texture
  458. std::sort(mSortedIconData.begin(), mSortedIconData.begin() + actualNumIcons,
  459. [&](const SortedIconData& a, const SortedIconData& b)
  460. {
  461. if (a.distance == b.distance)
  462. {
  463. HTexture texA = iconData[a.iconIdx].texture->getTexture();
  464. HTexture texB = iconData[b.iconIdx].texture->getTexture();
  465. if (texA == texB)
  466. return a.iconIdx < b.iconIdx;
  467. return texA->getInternalID() < texB->getInternalID();
  468. }
  469. else
  470. return a.distance > b.distance;
  471. });
  472. MeshDataPtr meshData = bs_shared_ptr_new<MeshData>(actualNumIcons * 4, actualNumIcons * 6, mIconVertexDesc);
  473. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  474. auto texcoordIter = meshData->getVec2DataIter(VES_TEXCOORD);
  475. auto normalColorIter = meshData->getDWORDDataIter(VES_COLOR, 0);
  476. auto fadedColorIter = meshData->getDWORDDataIter(VES_COLOR, 1);
  477. UINT32* indices = meshData->getIndices32();
  478. float cameraScale = 1.0f;
  479. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  480. cameraScale = camera->getViewport()->getHeight() / camera->getOrthoWindowHeight();
  481. else
  482. {
  483. Radian vertFOV(Math::tan(camera->getHorzFOV() * 0.5f));
  484. cameraScale = (camera->getViewport()->getHeight() * 0.5f) / vertFOV.valueRadians();
  485. }
  486. iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
  487. UINT32 lastTextureIdx = std::numeric_limits<UINT32>::max();
  488. HTexture curTexture;
  489. // Note: This assumes the meshes will be rendered using the same camera
  490. // properties as when they are created
  491. for (i = 0; i < actualNumIcons; i++)
  492. {
  493. SortedIconData& sortedIconData = mSortedIconData[i];
  494. const IconData& curIconData = iconData[sortedIconData.iconIdx];
  495. HTexture atlasTexture = curIconData.texture->getTexture();
  496. if (curTexture != atlasTexture)
  497. {
  498. UINT32 numIconsPerTexture = i - lastTextureIdx;
  499. if (numIconsPerTexture > 0)
  500. {
  501. iconRenderData->push_back(IconRenderData());
  502. IconRenderData& renderData = iconRenderData->back();
  503. renderData.count = numIconsPerTexture;
  504. renderData.texture = atlasTexture->getCore();
  505. }
  506. lastTextureIdx = i;
  507. curTexture = atlasTexture;
  508. }
  509. UINT32 iconWidth = curIconData.texture->getWidth();
  510. UINT32 iconHeight = curIconData.texture->getHeight();
  511. limitIconSize(iconWidth, iconHeight);
  512. Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, -sortedIconData.distance);
  513. Vector3 projPosition = camera->projectPoint(position);
  514. position.z = projPosition.z;
  515. float halfWidth = iconWidth * 0.5f;
  516. float halfHeight = iconHeight * 0.5f;
  517. if (!curIconData.fixedScale)
  518. {
  519. float iconScale = 1.0f;
  520. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  521. iconScale = cameraScale * ICON_TEXEL_WORLD_SIZE;
  522. else
  523. iconScale = (cameraScale * ICON_TEXEL_WORLD_SIZE) / sortedIconData.distance;
  524. halfWidth *= iconScale;
  525. halfHeight *= iconScale;
  526. }
  527. Color normalColor, fadedColor;
  528. calculateIconColors(curIconData.color, camera, (UINT32)(halfHeight * 2.0f), curIconData.fixedScale, normalColor, fadedColor);
  529. if (forPicking)
  530. {
  531. normalColor = curIconData.color;
  532. fadedColor = curIconData.color;
  533. }
  534. Vector3 positions[4];
  535. positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
  536. positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
  537. positions[2] = position + Vector3(halfWidth, halfHeight, 0.0f);
  538. positions[3] = position + Vector3(-halfWidth, halfHeight, 0.0f);
  539. Vector2 uvs[4];
  540. uvs[0] = curIconData.texture->transformUV(Vector2(0.0f, 0.0f));
  541. uvs[1] = curIconData.texture->transformUV(Vector2(1.0f, 0.0f));
  542. uvs[2] = curIconData.texture->transformUV(Vector2(1.0f, 1.0f));
  543. uvs[3] = curIconData.texture->transformUV(Vector2(0.0f, 1.0f));
  544. for (UINT32 j = 0; j < 4; j++)
  545. {
  546. positionIter.addValue(positions[j]);
  547. texcoordIter.addValue(uvs[j]);
  548. normalColorIter.addValue(normalColor.getAsRGBA());
  549. fadedColorIter.addValue(fadedColor.getAsRGBA());
  550. }
  551. UINT32 vertOffset = i * 4;
  552. indices[0] = vertOffset + 0;
  553. indices[1] = vertOffset + 1;
  554. indices[2] = vertOffset + 2;
  555. indices[3] = vertOffset + 0;
  556. indices[4] = vertOffset + 2;
  557. indices[5] = vertOffset + 3;
  558. indices += 6;
  559. }
  560. return mIconMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  561. }
  562. void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
  563. {
  564. if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
  565. return;
  566. float relWidth = OPTIMAL_ICON_SIZE / (float)width;
  567. float relHeight = OPTIMAL_ICON_SIZE / (float)height;
  568. float scale = std::min(relWidth, relHeight);
  569. width = Math::roundToInt(width * scale);
  570. height = Math::roundToInt(height * scale);
  571. }
  572. void GizmoManager::calculateIconColors(const Color& tint, const CameraPtr& camera,
  573. UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
  574. {
  575. normalColor = tint;
  576. if (!fixedScale)
  577. {
  578. float iconToScreenRatio = iconHeight / (float)camera->getViewport()->getHeight();
  579. if (iconToScreenRatio > 0.3f)
  580. {
  581. float alpha = 1.0f - Math::lerp01(iconToScreenRatio, 0.3f, 1.0f);
  582. normalColor.a *= alpha;
  583. }
  584. else if (iconToScreenRatio < 0.1f)
  585. {
  586. float alpha = Math::lerp01(iconToScreenRatio, 0.0f, 0.1f);
  587. normalColor.a *= alpha;
  588. }
  589. }
  590. fadedColor = normalColor;
  591. fadedColor.a *= 0.2f;
  592. }
  593. HSceneObject GizmoManager::getSceneObject(UINT32 gizmoIdx)
  594. {
  595. auto iterFind = mIdxToSceneObjectMap.find(gizmoIdx);
  596. if (iterFind != mIdxToSceneObjectMap.end())
  597. return iterFind->second;
  598. return HSceneObject();
  599. }
  600. const float GizmoManagerCore::PICKING_ALPHA_CUTOFF = 0.5f;
  601. GizmoManagerCore::GizmoManagerCore(const PrivatelyConstuct& dummy)
  602. {
  603. }
  604. GizmoManagerCore::~GizmoManagerCore()
  605. {
  606. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  607. if (mCamera != nullptr)
  608. activeRenderer->_unregisterRenderCallback(mCamera.get(), 20);
  609. }
  610. void GizmoManagerCore::initialize(const GizmoManager::CoreInitData& initData)
  611. {
  612. THROW_IF_NOT_CORE_THREAD;
  613. mSolidMaterial.mat = initData.solidMat;
  614. mWireMaterial.mat = initData.wireMat;
  615. mTextMaterial.mat = initData.textMat;
  616. mIconMaterial.mat = initData.iconMat;
  617. mPickingMaterial.mat = initData.pickingMat;
  618. mAlphaPickingMaterial.mat = initData.alphaPickingMat;
  619. {
  620. SPtr<MaterialCore> mat = mWireMaterial.mat;
  621. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  622. vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
  623. }
  624. {
  625. SPtr<MaterialCore> mat = mSolidMaterial.mat;
  626. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  627. SPtr<GpuParamsCore> fragParams = mat->getPassParameters(0)->mFragParams;
  628. vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
  629. fragParams->getParam("viewDir", mSolidMaterial.mViewDir);
  630. }
  631. {
  632. SPtr<MaterialCore> mat = mIconMaterial.mat;
  633. SPtr<PassParametersCore> pass0Params = mat->getPassParameters(0);
  634. SPtr<PassParametersCore> pass1Params = mat->getPassParameters(1);
  635. SPtr<GpuParamsCore> vertParams0 = pass0Params->mVertParams;
  636. SPtr<GpuParamsCore> vertParams1 = pass1Params->mVertParams;
  637. vertParams0->getParam("matViewProj", mIconMaterial.mViewProj[0]);
  638. vertParams1->getParam("matViewProj", mIconMaterial.mViewProj[1]);
  639. mIconMaterial.mFragParams[0] = pass0Params->mFragParams;
  640. mIconMaterial.mFragParams[1] = pass1Params->mFragParams;
  641. mIconMaterial.mFragParams[0]->getTextureParam("mainTexture", mIconMaterial.mTexture[0]);
  642. mIconMaterial.mFragParams[1]->getTextureParam("mainTexture", mIconMaterial.mTexture[1]);
  643. }
  644. {
  645. SPtr<MaterialCore> mat = mPickingMaterial.mat;
  646. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  647. vertParams->getParam("matViewProj", mPickingMaterial.mViewProj);
  648. }
  649. {
  650. SPtr<MaterialCore> mat = mAlphaPickingMaterial.mat;
  651. SPtr<PassParametersCore> passParams = mat->getPassParameters(0);
  652. SPtr<GpuParamsCore> vertParams = passParams->mVertParams;
  653. vertParams->getParam("matViewProj", mAlphaPickingMaterial.mViewProj);
  654. mAlphaPickingMaterial.mFragParams = passParams->mFragParams;
  655. mAlphaPickingMaterial.mFragParams->getTextureParam("mainTexture", mAlphaPickingMaterial.mTexture);
  656. GpuParamFloatCore alphaCutoffParam;
  657. mAlphaPickingMaterial.mFragParams->getParam("alphaCutoff", alphaCutoffParam);
  658. alphaCutoffParam.set(PICKING_ALPHA_CUTOFF);
  659. }
  660. {
  661. SPtr<MaterialCore> mat = mTextMaterial.mat;
  662. SPtr<PassParametersCore> passParams = mat->getPassParameters(0);
  663. SPtr<GpuParamsCore> vertParams = passParams->mVertParams;
  664. SPtr<GpuParamsCore> fragParams = passParams->mFragParams;
  665. vertParams->getParam("matViewProj", mTextMaterial.mViewProj);
  666. fragParams->getTextureParam("mainTexture", mTextMaterial.mTexture);
  667. }
  668. }
  669. void GizmoManagerCore::updateData(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes,
  670. const SPtr<MeshCoreBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
  671. {
  672. if (mCamera != camera)
  673. {
  674. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  675. if (mCamera != nullptr)
  676. activeRenderer->_unregisterRenderCallback(mCamera.get(), 0);
  677. if (camera != nullptr)
  678. activeRenderer->_registerRenderCallback(camera.get(), 0, std::bind(&GizmoManagerCore::render, this));
  679. }
  680. mCamera = camera;
  681. mMeshes = meshes;
  682. mIconMesh = iconMesh;
  683. mIconRenderData = iconRenderData;
  684. }
  685. void GizmoManagerCore::render()
  686. {
  687. if (mCamera == nullptr)
  688. return;
  689. SPtr<RenderTargetCore> renderTarget = mCamera->getViewport()->getTarget();
  690. float width = (float)renderTarget->getProperties().getWidth();
  691. float height = (float)renderTarget->getProperties().getHeight();
  692. Rect2 normArea = mCamera->getViewport()->getNormArea();
  693. Rect2I screenArea;
  694. screenArea.x = (int)(normArea.x * width);
  695. screenArea.y = (int)(normArea.y * height);
  696. screenArea.width = (int)(normArea.width * width);
  697. screenArea.height = (int)(normArea.height * height);
  698. for (auto& meshData : mMeshes)
  699. {
  700. GizmoManager::GizmoMaterial material = GizmoManager::GizmoMaterial::Solid;
  701. switch(meshData.type)
  702. {
  703. case MeshType::Solid:
  704. material = GizmoManager::GizmoMaterial::Solid;
  705. break;
  706. case MeshType::Wire:
  707. material = GizmoManager::GizmoMaterial::Wire;
  708. break;
  709. case MeshType::Text:
  710. material = GizmoManager::GizmoMaterial::Text;
  711. break;
  712. }
  713. renderGizmos(mCamera->getViewMatrix(), mCamera->getProjectionMatrixRS(), mCamera->getForward(),
  714. meshData.mesh, meshData.texture, material);
  715. }
  716. if (mIconMesh != nullptr)
  717. renderIconGizmos(screenArea, mIconMesh, mIconRenderData, false);
  718. }
  719. void GizmoManagerCore::renderGizmos(const Matrix4& viewMatrix, const Matrix4& projMatrix, const Vector3& viewDir,
  720. const SPtr<MeshCoreBase>& mesh, const SPtr<TextureCore>& texture, GizmoManager::GizmoMaterial material)
  721. {
  722. THROW_IF_NOT_CORE_THREAD;
  723. Matrix4 viewProjMat = projMatrix * viewMatrix;
  724. switch (material)
  725. {
  726. case GizmoManager::GizmoMaterial::Solid:
  727. mSolidMaterial.mViewProj.set(viewProjMat);
  728. mSolidMaterial.mViewDir.set((Vector4)viewDir);
  729. gRendererUtility().setPass(mSolidMaterial.mat, 0);
  730. gRendererUtility().setPassParams(mSolidMaterial.mat);
  731. break;
  732. case GizmoManager::GizmoMaterial::Wire:
  733. mWireMaterial.mViewProj.set(viewProjMat);
  734. gRendererUtility().setPass(mWireMaterial.mat, 0);
  735. gRendererUtility().setPassParams(mWireMaterial.mat);
  736. break;
  737. case GizmoManager::GizmoMaterial::Picking:
  738. mPickingMaterial.mViewProj.set(viewProjMat);
  739. gRendererUtility().setPass(mPickingMaterial.mat, 0);
  740. gRendererUtility().setPassParams(mPickingMaterial.mat);
  741. break;
  742. case GizmoManager::GizmoMaterial::PickingAlpha:
  743. mAlphaPickingMaterial.mViewProj.set(viewProjMat);
  744. mAlphaPickingMaterial.mTexture.set(texture);
  745. gRendererUtility().setPass(mAlphaPickingMaterial.mat, 0);
  746. gRendererUtility().setPassParams(mAlphaPickingMaterial.mat);
  747. break;
  748. case GizmoManager::GizmoMaterial::Text:
  749. mTextMaterial.mViewProj.set(viewProjMat);
  750. mTextMaterial.mTexture.set(texture);
  751. gRendererUtility().setPass(mTextMaterial.mat, 0);
  752. gRendererUtility().setPassParams(mTextMaterial.mat);
  753. break;
  754. }
  755. gRendererUtility().draw(mesh, mesh->getProperties().getSubMesh(0));
  756. }
  757. void GizmoManagerCore::renderIconGizmos(Rect2I screenArea, SPtr<MeshCoreBase> mesh, GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
  758. {
  759. RenderAPICore& rs = RenderAPICore::instance();
  760. const MeshProperties& meshProps = mesh->getProperties();
  761. std::shared_ptr<VertexData> vertexData = mesh->getVertexData();
  762. rs.setVertexDeclaration(vertexData->vertexDeclaration);
  763. auto vertexBuffers = vertexData->getBuffers();
  764. SPtr<VertexBufferCore> vertBuffers[1] = { vertexBuffers.begin()->second };
  765. rs.setVertexBuffers(0, vertBuffers, 1);
  766. SPtr<IndexBufferCore> indexBuffer = mesh->getIndexBuffer();
  767. rs.setIndexBuffer(indexBuffer);
  768. rs.setDrawOperation(DOT_TRIANGLE_LIST);
  769. // Set up ortho matrix
  770. Matrix4 projMat;
  771. float left = screenArea.x + rs.getHorizontalTexelOffset();
  772. float right = screenArea.x + screenArea.width + rs.getHorizontalTexelOffset();
  773. float top = screenArea.y + rs.getVerticalTexelOffset();
  774. float bottom = screenArea.y + screenArea.height + rs.getVerticalTexelOffset();
  775. float near = rs.getMinimumDepthInputValue();
  776. float far = rs.getMaximumDepthInputValue();
  777. // Top/bottom have been swapped because we're moving from window coordinates (origin top left)
  778. // to normalized device coordinates (origin bottom left)
  779. // Negative near/far because Z is flipped for normalized device coordinates
  780. // (positive Z goes into screen as opposed to view space here we're looking along negative Z)
  781. projMat.makeProjectionOrtho(left, right, top, bottom, -near, -far);
  782. if (!usePickingMaterial)
  783. {
  784. mIconMaterial.mViewProj[0].set(projMat);
  785. mIconMaterial.mViewProj[1].set(projMat);
  786. for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
  787. {
  788. gRendererUtility().setPass(mIconMaterial.mat, passIdx);
  789. UINT32 curIndexOffset = mesh->getIndexOffset();
  790. for (auto curRenderData : *renderData)
  791. {
  792. mIconMaterial.mTexture[passIdx].set(curRenderData.texture);
  793. rs.setGpuParams(GPT_FRAGMENT_PROGRAM, mIconMaterial.mFragParams[passIdx]);
  794. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  795. curIndexOffset += curRenderData.count * 6;
  796. }
  797. }
  798. }
  799. else
  800. {
  801. mAlphaPickingMaterial.mViewProj.set(projMat);
  802. gRendererUtility().setPass(mAlphaPickingMaterial.mat, 0);
  803. UINT32 curIndexOffset = 0;
  804. for (auto curRenderData : *renderData)
  805. {
  806. mAlphaPickingMaterial.mTexture.set(curRenderData.texture);
  807. rs.setGpuParams(GPT_FRAGMENT_PROGRAM, mAlphaPickingMaterial.mFragParams);
  808. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  809. curIndexOffset += curRenderData.count * 6;
  810. }
  811. }
  812. mesh->_notifyUsedOnGPU();
  813. }
  814. }