BsGizmoManager.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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::drawFrustum(const Vector3& position, float aspect, Degree FOV, float near, float far)
  163. {
  164. mFrustumData.push_back(FrustumData());
  165. FrustumData& frustumData = mFrustumData.back();
  166. frustumData.idx = mCurrentIdx++;
  167. frustumData.position = position;
  168. frustumData.aspect = aspect;
  169. frustumData.FOV = FOV;
  170. frustumData.near = near;
  171. frustumData.far = far;
  172. frustumData.color = mColor;
  173. frustumData.transform = mTransform;
  174. frustumData.sceneObject = mActiveSO;
  175. frustumData.pickable = mPickable;
  176. mDrawHelper->frustum(position, aspect, FOV, near, far);
  177. mIdxToSceneObjectMap[frustumData.idx] = mActiveSO;
  178. }
  179. void GizmoManager::drawIcon(Vector3 position, HSpriteTexture image, bool fixedScale)
  180. {
  181. mIconData.push_back(IconData());
  182. IconData& iconData = mIconData.back();
  183. iconData.idx = mCurrentIdx++;
  184. iconData.position = position;
  185. iconData.texture = image;
  186. iconData.fixedScale = fixedScale;
  187. iconData.color = mColor;
  188. iconData.transform = mTransform;
  189. iconData.sceneObject = mActiveSO;
  190. iconData.pickable = mPickable;
  191. mIdxToSceneObjectMap[iconData.idx] = mActiveSO;
  192. }
  193. void GizmoManager::update(const CameraPtr& camera)
  194. {
  195. mDrawHelper->clearMeshes(mActiveMeshes);
  196. mActiveMeshes.clear();
  197. if (mIconMesh != nullptr)
  198. mIconMeshHeap->dealloc(mIconMesh);
  199. IconRenderDataVecPtr iconRenderData;
  200. mDrawHelper->buildMeshes();
  201. mActiveMeshes = mDrawHelper->getMeshes();
  202. SPtr<MeshCoreBase> solidMesh = nullptr;
  203. SPtr<MeshCoreBase> wireMesh = nullptr;
  204. for (auto& meshData : mActiveMeshes)
  205. {
  206. if (meshData.type == DrawHelper::MeshType::Solid)
  207. {
  208. if (solidMesh == nullptr)
  209. solidMesh = meshData.mesh->getCore();
  210. }
  211. else // Wire
  212. {
  213. if (wireMesh == nullptr)
  214. wireMesh = meshData.mesh->getCore();
  215. }
  216. }
  217. // Since there is no sorting used with draw helper meshes we only expect up to two of them,
  218. // one for solids, one for wireframe
  219. assert(mActiveMeshes.size() <= 2);
  220. mIconMesh = buildIconMesh(camera, mIconData, false, iconRenderData);
  221. SPtr<MeshCoreBase> iconMesh = mIconMesh->getCore();
  222. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  223. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, core, camera->getCore(),
  224. solidMesh, wireMesh, iconMesh, iconRenderData));
  225. }
  226. void GizmoManager::renderForPicking(const CameraPtr& camera, std::function<Color(UINT32)> idxToColorCallback)
  227. {
  228. Vector<IconData> iconData;
  229. IconRenderDataVecPtr iconRenderData;
  230. mPickingDrawHelper->clear();
  231. for (auto& cubeDataEntry : mSolidCubeData)
  232. {
  233. if (!cubeDataEntry.pickable)
  234. continue;
  235. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  236. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  237. mPickingDrawHelper->cube(cubeDataEntry.position, cubeDataEntry.extents);
  238. }
  239. for (auto& cubeDataEntry : mWireCubeData)
  240. {
  241. if (!cubeDataEntry.pickable)
  242. continue;
  243. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  244. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  245. mPickingDrawHelper->wireCube(cubeDataEntry.position, cubeDataEntry.extents);
  246. }
  247. for (auto& sphereDataEntry : mSolidSphereData)
  248. {
  249. if (!sphereDataEntry.pickable)
  250. continue;
  251. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  252. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  253. mPickingDrawHelper->sphere(sphereDataEntry.position, sphereDataEntry.radius);
  254. }
  255. for (auto& sphereDataEntry : mWireSphereData)
  256. {
  257. if (!sphereDataEntry.pickable)
  258. continue;
  259. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  260. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  261. mPickingDrawHelper->wireSphere(sphereDataEntry.position, sphereDataEntry.radius);
  262. }
  263. for (auto& lineDataEntry : mLineData)
  264. {
  265. if (!lineDataEntry.pickable)
  266. continue;
  267. mPickingDrawHelper->setColor(idxToColorCallback(lineDataEntry.idx));
  268. mPickingDrawHelper->setTransform(lineDataEntry.transform);
  269. mPickingDrawHelper->line(lineDataEntry.start, lineDataEntry.end);
  270. }
  271. for (auto& frustumDataEntry : mFrustumData)
  272. {
  273. if (!frustumDataEntry.pickable)
  274. continue;
  275. mPickingDrawHelper->setColor(idxToColorCallback(frustumDataEntry.idx));
  276. mPickingDrawHelper->setTransform(frustumDataEntry.transform);
  277. mPickingDrawHelper->frustum(frustumDataEntry.position, frustumDataEntry.aspect, frustumDataEntry.FOV,
  278. frustumDataEntry.near, frustumDataEntry.far);
  279. }
  280. for (auto& iconDataEntry : mIconData)
  281. {
  282. if (!iconDataEntry.pickable)
  283. continue;
  284. iconData.push_back(iconDataEntry);
  285. iconData.back().color = idxToColorCallback(iconDataEntry.idx);
  286. }
  287. mPickingDrawHelper->buildMeshes();
  288. const Vector<DrawHelper::ShapeMeshData>& meshes = mPickingDrawHelper->getMeshes();
  289. TransientMeshPtr iconMesh = buildIconMesh(camera, iconData, true, iconRenderData);
  290. // Note: This must be rendered while Scene view is being rendered
  291. Matrix4 viewMat = camera->getViewMatrix();
  292. Matrix4 projMat = camera->getProjectionMatrixRS();
  293. ViewportPtr viewport = camera->getViewport();
  294. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  295. for (auto& meshData : meshes)
  296. {
  297. if (meshData.type == DrawHelper::MeshType::Solid)
  298. {
  299. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos,
  300. core, viewMat, projMat, camera->getForward(), meshData.mesh->getCore(), GizmoMaterial::Picking));
  301. }
  302. else // Wire
  303. {
  304. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos,
  305. core, viewMat, projMat, camera->getForward(), meshData.mesh->getCore(), GizmoMaterial::Picking));
  306. }
  307. }
  308. Rect2I screenArea = camera->getViewport()->getArea();
  309. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderIconGizmos,
  310. core, screenArea, iconMesh->getCore(), iconRenderData, true));
  311. mPickingDrawHelper->clearMeshes(meshes);
  312. mIconMeshHeap->dealloc(iconMesh);
  313. }
  314. void GizmoManager::clearGizmos()
  315. {
  316. mSolidCubeData.clear();
  317. mWireCubeData.clear();
  318. mSolidSphereData.clear();
  319. mWireSphereData.clear();
  320. mLineData.clear();
  321. mFrustumData.clear();
  322. mIconData.clear();
  323. mIdxToSceneObjectMap.clear();
  324. mDrawHelper->clear();
  325. mCurrentIdx = 0;
  326. }
  327. void GizmoManager::clearRenderData()
  328. {
  329. mDrawHelper->clearMeshes(mActiveMeshes);
  330. mActiveMeshes.clear();
  331. if (mIconMesh != nullptr)
  332. mIconMeshHeap->dealloc(mIconMesh);
  333. mIconMesh = nullptr;
  334. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  335. IconRenderDataVecPtr iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
  336. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, core, nullptr, nullptr, nullptr, nullptr, iconRenderData));
  337. }
  338. TransientMeshPtr GizmoManager::buildIconMesh(const CameraPtr& camera, const Vector<IconData>& iconData,
  339. bool forPicking, GizmoManager::IconRenderDataVecPtr& iconRenderData)
  340. {
  341. mSortedIconData.clear();
  342. if (iconData.size() > mSortedIconData.size())
  343. mSortedIconData.resize(iconData.size());
  344. UINT32 i = 0;
  345. for (auto& iconEntry : iconData)
  346. {
  347. Vector3 viewPoint = camera->worldToViewPoint(iconEntry.position);
  348. float distance = -viewPoint.z;
  349. if (distance < camera->getNearClipDistance()) // Ignore behind clip plane
  350. continue;
  351. if (distance > MAX_ICON_RANGE) // Ignore too far away
  352. continue;
  353. if (!iconEntry.texture.isLoaded()) // Ignore missing texture
  354. continue;
  355. if (forPicking && !iconEntry.pickable)
  356. continue;
  357. SortedIconData& sortedIconData = mSortedIconData[i];
  358. sortedIconData.iconIdx = i;
  359. sortedIconData.distance = distance;
  360. sortedIconData.screenPosition = camera->viewToScreenPoint(viewPoint);
  361. i++;
  362. }
  363. UINT32 actualNumIcons = i;
  364. // Sort back to front first, then by texture
  365. std::sort(mSortedIconData.begin(), mSortedIconData.begin() + actualNumIcons,
  366. [&](const SortedIconData& a, const SortedIconData& b)
  367. {
  368. if (a.distance == b.distance)
  369. {
  370. HTexture texA = iconData[a.iconIdx].texture->getTexture();
  371. HTexture texB = iconData[b.iconIdx].texture->getTexture();
  372. if (texA == texB)
  373. return a.iconIdx < b.iconIdx;
  374. return texA->getInternalID() < texB->getInternalID();
  375. }
  376. else
  377. return a.distance > b.distance;
  378. });
  379. MeshDataPtr meshData = bs_shared_ptr_new<MeshData>(actualNumIcons * 4, actualNumIcons * 6, mIconVertexDesc);
  380. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  381. auto texcoordIter = meshData->getVec2DataIter(VES_TEXCOORD);
  382. auto normalColorIter = meshData->getDWORDDataIter(VES_COLOR, 0);
  383. auto fadedColorIter = meshData->getDWORDDataIter(VES_COLOR, 1);
  384. UINT32* indices = meshData->getIndices32();
  385. float cameraScale = 1.0f;
  386. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  387. cameraScale = camera->getViewport()->getHeight() / camera->getOrthoWindowHeight();
  388. else
  389. {
  390. Radian vertFOV(Math::tan(camera->getHorzFOV() * 0.5f));
  391. cameraScale = (camera->getViewport()->getHeight() * 0.5f) / vertFOV.valueRadians();
  392. }
  393. iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
  394. UINT32 lastTextureIdx = std::numeric_limits<UINT32>::max();
  395. HTexture curTexture;
  396. // Note: This assumes the meshes will be rendered using the same camera
  397. // properties as when they are created
  398. for (i = 0; i < actualNumIcons; i++)
  399. {
  400. SortedIconData& sortedIconData = mSortedIconData[i];
  401. const IconData& curIconData = iconData[sortedIconData.iconIdx];
  402. HTexture atlasTexture = curIconData.texture->getTexture();
  403. if (curTexture != atlasTexture)
  404. {
  405. UINT32 numIconsPerTexture = i - lastTextureIdx;
  406. if (numIconsPerTexture > 0)
  407. {
  408. iconRenderData->push_back(IconRenderData());
  409. IconRenderData& renderData = iconRenderData->back();
  410. renderData.count = numIconsPerTexture;
  411. renderData.texture = atlasTexture->getCore();
  412. }
  413. lastTextureIdx = i;
  414. curTexture = atlasTexture;
  415. }
  416. UINT32 iconWidth = curIconData.texture->getWidth();
  417. UINT32 iconHeight = curIconData.texture->getHeight();
  418. limitIconSize(iconWidth, iconHeight);
  419. Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, -sortedIconData.distance);
  420. Vector3 projPosition = camera->projectPoint(position);
  421. position.z = projPosition.z;
  422. float halfWidth = iconWidth * 0.5f;
  423. float halfHeight = iconHeight * 0.5f;
  424. if (!curIconData.fixedScale)
  425. {
  426. float iconScale = 1.0f;
  427. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  428. iconScale = cameraScale * ICON_TEXEL_WORLD_SIZE;
  429. else
  430. iconScale = (cameraScale * ICON_TEXEL_WORLD_SIZE) / sortedIconData.distance;
  431. halfWidth *= iconScale;
  432. halfHeight *= iconScale;
  433. }
  434. Color normalColor, fadedColor;
  435. calculateIconColors(curIconData.color, camera, (UINT32)(halfHeight * 2.0f), curIconData.fixedScale, normalColor, fadedColor);
  436. if (forPicking)
  437. {
  438. normalColor = curIconData.color;
  439. fadedColor = curIconData.color;
  440. }
  441. Vector3 positions[4];
  442. positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
  443. positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
  444. positions[2] = position + Vector3(halfWidth, halfHeight, 0.0f);
  445. positions[3] = position + Vector3(-halfWidth, halfHeight, 0.0f);
  446. Vector2 uvs[4];
  447. uvs[0] = curIconData.texture->transformUV(Vector2(0.0f, 0.0f));
  448. uvs[1] = curIconData.texture->transformUV(Vector2(1.0f, 0.0f));
  449. uvs[2] = curIconData.texture->transformUV(Vector2(1.0f, 1.0f));
  450. uvs[3] = curIconData.texture->transformUV(Vector2(0.0f, 1.0f));
  451. for (UINT32 j = 0; j < 4; j++)
  452. {
  453. positionIter.addValue(positions[j]);
  454. texcoordIter.addValue(uvs[j]);
  455. normalColorIter.addValue(normalColor.getAsRGBA());
  456. fadedColorIter.addValue(fadedColor.getAsRGBA());
  457. }
  458. UINT32 vertOffset = i * 4;
  459. indices[0] = vertOffset + 0;
  460. indices[1] = vertOffset + 1;
  461. indices[2] = vertOffset + 2;
  462. indices[3] = vertOffset + 0;
  463. indices[4] = vertOffset + 2;
  464. indices[5] = vertOffset + 3;
  465. indices += 6;
  466. }
  467. return mIconMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  468. }
  469. void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
  470. {
  471. if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
  472. return;
  473. float relWidth = OPTIMAL_ICON_SIZE / (float)width;
  474. float relHeight = OPTIMAL_ICON_SIZE / (float)height;
  475. float scale = std::min(relWidth, relHeight);
  476. width = Math::roundToInt(width * scale);
  477. height = Math::roundToInt(height * scale);
  478. }
  479. void GizmoManager::calculateIconColors(const Color& tint, const CameraPtr& camera,
  480. UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
  481. {
  482. normalColor = tint;
  483. if (!fixedScale)
  484. {
  485. float iconToScreenRatio = iconHeight / (float)camera->getViewport()->getHeight();
  486. if (iconToScreenRatio > 0.3f)
  487. {
  488. float alpha = 1.0f - Math::lerp01(iconToScreenRatio, 0.3f, 1.0f);
  489. normalColor.a *= alpha;
  490. }
  491. else if (iconToScreenRatio < 0.1f)
  492. {
  493. float alpha = Math::lerp01(iconToScreenRatio, 0.0f, 0.1f);
  494. normalColor.a *= alpha;
  495. }
  496. }
  497. fadedColor = normalColor;
  498. fadedColor.a *= 0.2f;
  499. }
  500. HSceneObject GizmoManager::getSceneObject(UINT32 gizmoIdx)
  501. {
  502. auto iterFind = mIdxToSceneObjectMap.find(gizmoIdx);
  503. if (iterFind != mIdxToSceneObjectMap.end())
  504. return iterFind->second;
  505. return HSceneObject();
  506. }
  507. const float GizmoManagerCore::PICKING_ALPHA_CUTOFF = 0.5f;
  508. GizmoManagerCore::GizmoManagerCore(const PrivatelyConstuct& dummy)
  509. {
  510. }
  511. GizmoManagerCore::~GizmoManagerCore()
  512. {
  513. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  514. if (mCamera != nullptr)
  515. activeRenderer->_unregisterRenderCallback(mCamera.get(), 20);
  516. }
  517. void GizmoManagerCore::initialize(const GizmoManager::CoreInitData& initData)
  518. {
  519. THROW_IF_NOT_CORE_THREAD;
  520. mSolidMaterial.mat = initData.solidMat;
  521. mWireMaterial.mat = initData.wireMat;
  522. mIconMaterial.mat = initData.iconMat;
  523. mPickingMaterial.mat = initData.pickingMat;
  524. mAlphaPickingMaterial.mat = initData.alphaPickingMat;
  525. {
  526. SPtr<MaterialCore> mat = mWireMaterial.mat;
  527. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  528. vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
  529. }
  530. {
  531. SPtr<MaterialCore> mat = mSolidMaterial.mat;
  532. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  533. SPtr<GpuParamsCore> fragParams = mat->getPassParameters(0)->mFragParams;
  534. vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
  535. fragParams->getParam("viewDir", mSolidMaterial.mViewDir);
  536. }
  537. {
  538. SPtr<MaterialCore> mat = mIconMaterial.mat;
  539. SPtr<PassParametersCore> pass0Params = mat->getPassParameters(0);
  540. SPtr<PassParametersCore> pass1Params = mat->getPassParameters(1);
  541. SPtr<GpuParamsCore> vertParams0 = pass0Params->mVertParams;
  542. SPtr<GpuParamsCore> vertParams1 = pass1Params->mVertParams;
  543. vertParams0->getParam("matViewProj", mIconMaterial.mViewProj[0]);
  544. vertParams1->getParam("matViewProj", mIconMaterial.mViewProj[1]);
  545. mIconMaterial.mFragParams[0] = pass0Params->mFragParams;
  546. mIconMaterial.mFragParams[1] = pass1Params->mFragParams;
  547. mIconMaterial.mFragParams[0]->getTextureParam("mainTexture", mIconMaterial.mTexture[0]);
  548. mIconMaterial.mFragParams[1]->getTextureParam("mainTexture", mIconMaterial.mTexture[1]);
  549. }
  550. {
  551. SPtr<MaterialCore> mat = mPickingMaterial.mat;
  552. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  553. vertParams->getParam("matViewProj", mPickingMaterial.mViewProj);
  554. }
  555. {
  556. SPtr<MaterialCore> mat = mAlphaPickingMaterial.mat;
  557. SPtr<PassParametersCore> passParams = mat->getPassParameters(0);
  558. SPtr<GpuParamsCore> vertParams = passParams->mVertParams;
  559. vertParams->getParam("matViewProj", mAlphaPickingMaterial.mViewProj);
  560. mAlphaPickingMaterial.mFragParams = passParams->mFragParams;
  561. mAlphaPickingMaterial.mFragParams->getTextureParam("mainTexture", mAlphaPickingMaterial.mTexture);
  562. GpuParamFloatCore alphaCutoffParam;
  563. mAlphaPickingMaterial.mFragParams->getParam("alphaCutoff", alphaCutoffParam);
  564. alphaCutoffParam.set(PICKING_ALPHA_CUTOFF);
  565. }
  566. }
  567. void GizmoManagerCore::updateData(const SPtr<CameraCore>& camera, const SPtr<MeshCoreBase>& solidMesh, const SPtr<MeshCoreBase>& wireMesh,
  568. const SPtr<MeshCoreBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
  569. {
  570. if (mCamera != camera)
  571. {
  572. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  573. if (mCamera != nullptr)
  574. activeRenderer->_unregisterRenderCallback(mCamera.get(), 0);
  575. if (camera != nullptr)
  576. activeRenderer->_registerRenderCallback(camera.get(), 0, std::bind(&GizmoManagerCore::render, this));
  577. }
  578. mCamera = camera;
  579. mSolidMesh = solidMesh;
  580. mWireMesh = wireMesh;
  581. mIconMesh = iconMesh;
  582. mIconRenderData = iconRenderData;
  583. }
  584. void GizmoManagerCore::render()
  585. {
  586. if (mCamera == nullptr)
  587. return;
  588. SPtr<RenderTargetCore> renderTarget = mCamera->getViewport()->getTarget();
  589. float width = (float)renderTarget->getProperties().getWidth();
  590. float height = (float)renderTarget->getProperties().getHeight();
  591. Rect2 normArea = mCamera->getViewport()->getNormArea();
  592. Rect2I screenArea;
  593. screenArea.x = (int)(normArea.x * width);
  594. screenArea.y = (int)(normArea.y * height);
  595. screenArea.width = (int)(normArea.width * width);
  596. screenArea.height = (int)(normArea.height * height);
  597. if (mSolidMesh != nullptr)
  598. renderGizmos(mCamera->getViewMatrix(), mCamera->getProjectionMatrixRS(), mCamera->getForward(), mSolidMesh, GizmoManager::GizmoMaterial::Solid);
  599. if (mWireMesh != nullptr)
  600. renderGizmos(mCamera->getViewMatrix(), mCamera->getProjectionMatrixRS(), mCamera->getForward(), mWireMesh, GizmoManager::GizmoMaterial::Wire);
  601. if (mIconMesh != nullptr)
  602. renderIconGizmos(screenArea, mIconMesh, mIconRenderData, false);
  603. }
  604. void GizmoManagerCore::renderGizmos(Matrix4 viewMatrix, Matrix4 projMatrix, Vector3 viewDir, SPtr<MeshCoreBase> mesh, GizmoManager::GizmoMaterial material)
  605. {
  606. THROW_IF_NOT_CORE_THREAD;
  607. Matrix4 viewProjMat = projMatrix * viewMatrix;
  608. switch (material)
  609. {
  610. case GizmoManager::GizmoMaterial::Solid:
  611. mSolidMaterial.mViewProj.set(viewProjMat);
  612. mSolidMaterial.mViewDir.set((Vector4)viewDir);
  613. gRendererUtility().setPass(mSolidMaterial.mat, 0);
  614. break;
  615. case GizmoManager::GizmoMaterial::Wire:
  616. mWireMaterial.mViewProj.set(viewProjMat);
  617. gRendererUtility().setPass(mWireMaterial.mat, 0);
  618. break;
  619. case GizmoManager::GizmoMaterial::Picking:
  620. mPickingMaterial.mViewProj.set(viewProjMat);
  621. gRendererUtility().setPass(mPickingMaterial.mat, 0);
  622. break;
  623. }
  624. gRendererUtility().draw(mesh, mesh->getProperties().getSubMesh(0));
  625. }
  626. void GizmoManagerCore::renderIconGizmos(Rect2I screenArea, SPtr<MeshCoreBase> mesh, GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
  627. {
  628. RenderAPICore& rs = RenderAPICore::instance();
  629. const MeshProperties& meshProps = mesh->getProperties();
  630. std::shared_ptr<VertexData> vertexData = mesh->getVertexData();
  631. rs.setVertexDeclaration(vertexData->vertexDeclaration);
  632. auto vertexBuffers = vertexData->getBuffers();
  633. SPtr<VertexBufferCore> vertBuffers[1] = { vertexBuffers.begin()->second };
  634. rs.setVertexBuffers(0, vertBuffers, 1);
  635. SPtr<IndexBufferCore> indexBuffer = mesh->getIndexBuffer();
  636. rs.setIndexBuffer(indexBuffer);
  637. rs.setDrawOperation(DOT_TRIANGLE_LIST);
  638. // Set up ortho matrix
  639. Matrix4 projMat;
  640. float left = screenArea.x + rs.getHorizontalTexelOffset();
  641. float right = screenArea.x + screenArea.width + rs.getHorizontalTexelOffset();
  642. float top = screenArea.y + rs.getVerticalTexelOffset();
  643. float bottom = screenArea.y + screenArea.height + rs.getVerticalTexelOffset();
  644. float near = rs.getMinimumDepthInputValue();
  645. float far = rs.getMaximumDepthInputValue();
  646. // Top/bottom have been swapped because we're moving from window coordinates (origin top left)
  647. // to normalized device coordinates (origin bottom left)
  648. // Negative near/far because Z is flipped for normalized device coordinates
  649. // (positive Z goes into screen as opposed to view space here we're looking along negative Z)
  650. projMat.makeProjectionOrtho(left, right, top, bottom, -near, -far);
  651. if (!usePickingMaterial)
  652. {
  653. mIconMaterial.mViewProj[0].set(projMat);
  654. mIconMaterial.mViewProj[1].set(projMat);
  655. for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
  656. {
  657. gRendererUtility().setPass(mIconMaterial.mat, passIdx);
  658. UINT32 curIndexOffset = mesh->getIndexOffset();
  659. for (auto curRenderData : *renderData)
  660. {
  661. mIconMaterial.mTexture[passIdx].set(curRenderData.texture);
  662. rs.setGpuParams(GPT_FRAGMENT_PROGRAM, mIconMaterial.mFragParams[passIdx]);
  663. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  664. curIndexOffset += curRenderData.count * 6;
  665. }
  666. }
  667. }
  668. else
  669. {
  670. mAlphaPickingMaterial.mViewProj.set(projMat);
  671. gRendererUtility().setPass(mAlphaPickingMaterial.mat, 0);
  672. UINT32 curIndexOffset = 0;
  673. for (auto curRenderData : *renderData)
  674. {
  675. mAlphaPickingMaterial.mTexture.set(curRenderData.texture);
  676. rs.setGpuParams(GPT_FRAGMENT_PROGRAM, mAlphaPickingMaterial.mFragParams);
  677. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  678. curIndexOffset += curRenderData.count * 6;
  679. }
  680. }
  681. mesh->_notifyUsedOnGPU();
  682. }
  683. }