BsGizmoManager.cpp 27 KB

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