BsGizmoManager.cpp 26 KB

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