BsGizmoManager.cpp 30 KB

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