BsGizmoManager.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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<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 = bs_new<GizmoManagerCore>(GizmoManagerCore::PrivatelyConstuct());
  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));
  62. }
  63. void GizmoManager::initializeCore(const CoreInitData& initData)
  64. {
  65. mCore->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. RenderTargetPtr rt = camera->getViewport()->getTarget();
  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. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, mCore, rt->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. for (auto& meshData : meshes)
  291. {
  292. if (meshData.type == DrawHelper::MeshType::Solid)
  293. {
  294. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos,
  295. mCore, viewMat, projMat, meshData.mesh->getCore(), GizmoMaterial::Picking));
  296. }
  297. else // Wire
  298. {
  299. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos,
  300. mCore, viewMat, projMat, meshData.mesh->getCore(), GizmoMaterial::Picking));
  301. }
  302. }
  303. Rect2I screenArea = camera->getViewport()->getArea();
  304. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderIconGizmos,
  305. mCore, screenArea, iconMesh->getCore(), iconRenderData, true));
  306. mPickingDrawHelper->clearMeshes();
  307. mIconMeshHeap->dealloc(iconMesh);
  308. }
  309. void GizmoManager::clearGizmos()
  310. {
  311. mSolidCubeData.clear();
  312. mWireCubeData.clear();
  313. mSolidSphereData.clear();
  314. mWireSphereData.clear();
  315. mLineData.clear();
  316. mFrustumData.clear();
  317. mIconData.clear();
  318. mIdxToSceneObjectMap.clear();
  319. mDrawHelper->clear();
  320. mCurrentIdx = 0;
  321. }
  322. void GizmoManager::clearRenderData()
  323. {
  324. mDrawHelper->clearMeshes();
  325. if (mIconMesh != nullptr)
  326. mIconMeshHeap->dealloc(mIconMesh);
  327. mIconMesh = nullptr;
  328. IconRenderDataVecPtr iconRenderData = bs_shared_ptr<IconRenderDataVec>();
  329. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, mCore, nullptr, nullptr, nullptr, nullptr, iconRenderData));
  330. }
  331. TransientMeshPtr GizmoManager::buildIconMesh(const CameraHandlerPtr& camera, const Vector<IconData>& iconData,
  332. bool forPicking, GizmoManager::IconRenderDataVecPtr& iconRenderData)
  333. {
  334. mSortedIconData.clear();
  335. if (iconData.size() > mSortedIconData.size())
  336. mSortedIconData.resize(iconData.size());
  337. UINT32 i = 0;
  338. for (auto& iconEntry : iconData)
  339. {
  340. Vector3 viewPoint = camera->worldToViewPoint(iconEntry.position);
  341. float distance = -viewPoint.z;
  342. if (distance < camera->getNearClipDistance()) // Ignore behind clip plane
  343. continue;
  344. if (distance > MAX_ICON_RANGE) // Ignore too far away
  345. continue;
  346. if (!iconEntry.texture) // Ignore missing texture
  347. continue;
  348. if (forPicking && !iconEntry.pickable)
  349. continue;
  350. SortedIconData& sortedIconData = mSortedIconData[i];
  351. sortedIconData.iconIdx = i;
  352. sortedIconData.distance = distance;
  353. sortedIconData.screenPosition = camera->viewToScreenPoint(viewPoint);
  354. i++;
  355. }
  356. UINT32 actualNumIcons = i;
  357. // Sort back to front first, then by texture
  358. std::sort(mSortedIconData.begin(), mSortedIconData.begin() + actualNumIcons,
  359. [&](const SortedIconData& a, const SortedIconData& b)
  360. {
  361. if (a.distance == b.distance)
  362. {
  363. HTexture texA = iconData[a.iconIdx].texture->getTexture();
  364. HTexture texB = iconData[b.iconIdx].texture->getTexture();
  365. if (texA == texB)
  366. return a.iconIdx < b.iconIdx;
  367. return texA->getInternalID() < texB->getInternalID();
  368. }
  369. else
  370. return a.distance > b.distance;
  371. });
  372. MeshDataPtr meshData = bs_shared_ptr<MeshData>(actualNumIcons * 4, actualNumIcons * 6, mIconVertexDesc);
  373. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  374. auto texcoordIter = meshData->getVec2DataIter(VES_TEXCOORD);
  375. auto normalColorIter = meshData->getDWORDDataIter(VES_COLOR, 0);
  376. auto fadedColorIter = meshData->getDWORDDataIter(VES_COLOR, 1);
  377. UINT32* indices = meshData->getIndices32();
  378. float cameraScale = 1.0f;
  379. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  380. cameraScale = camera->getViewport()->getHeight() / camera->getOrthoWindowHeight();
  381. else
  382. {
  383. Radian vertFOV(Math::tan(camera->getHorzFOV() * 0.5f));
  384. cameraScale = (camera->getViewport()->getHeight() * 0.5f) / vertFOV.valueRadians();
  385. }
  386. iconRenderData = bs_shared_ptr<IconRenderDataVec>();
  387. UINT32 lastTextureIdx = std::numeric_limits<UINT32>::max();
  388. HTexture curTexture;
  389. // Note: This assumes the meshes will be rendered using the same camera
  390. // properties as when they are created
  391. for (i = 0; i < actualNumIcons; i++)
  392. {
  393. SortedIconData& sortedIconData = mSortedIconData[i];
  394. const IconData& curIconData = iconData[sortedIconData.iconIdx];
  395. HTexture atlasTexture = curIconData.texture->getTexture();
  396. if (curTexture != atlasTexture)
  397. {
  398. UINT32 numIconsPerTexture = i - lastTextureIdx;
  399. if (numIconsPerTexture > 0)
  400. {
  401. iconRenderData->push_back(IconRenderData());
  402. IconRenderData& renderData = iconRenderData->back();
  403. renderData.count = numIconsPerTexture;
  404. renderData.texture = atlasTexture->getCore();
  405. }
  406. lastTextureIdx = i;
  407. curTexture = atlasTexture;
  408. }
  409. UINT32 iconWidth = curIconData.texture->getWidth();
  410. UINT32 iconHeight = curIconData.texture->getHeight();
  411. limitIconSize(iconWidth, iconHeight);
  412. Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, -sortedIconData.distance);
  413. Vector3 projPosition = camera->projectPoint(position);
  414. position.z = projPosition.z;
  415. float halfWidth = iconWidth * 0.5f;
  416. float halfHeight = iconHeight * 0.5f;
  417. if (!curIconData.fixedScale)
  418. {
  419. float iconScale = 1.0f;
  420. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  421. iconScale = cameraScale * ICON_TEXEL_WORLD_SIZE;
  422. else
  423. iconScale = (cameraScale * ICON_TEXEL_WORLD_SIZE) / sortedIconData.distance;
  424. halfWidth *= iconScale;
  425. halfHeight *= iconScale;
  426. }
  427. Color normalColor, fadedColor;
  428. calculateIconColors(curIconData.color, camera, (UINT32)(halfHeight * 2.0f), curIconData.fixedScale, normalColor, fadedColor);
  429. if (forPicking)
  430. {
  431. normalColor = curIconData.color;
  432. fadedColor = curIconData.color;
  433. }
  434. Vector3 positions[4];
  435. positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
  436. positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
  437. positions[2] = position + Vector3(halfWidth, halfHeight, 0.0f);
  438. positions[3] = position + Vector3(-halfWidth, halfHeight, 0.0f);
  439. Vector2 uvs[4];
  440. uvs[0] = curIconData.texture->transformUV(Vector2(0.0f, 0.0f));
  441. uvs[1] = curIconData.texture->transformUV(Vector2(1.0f, 0.0f));
  442. uvs[2] = curIconData.texture->transformUV(Vector2(1.0f, 1.0f));
  443. uvs[3] = curIconData.texture->transformUV(Vector2(0.0f, 1.0f));
  444. for (UINT32 j = 0; j < 4; j++)
  445. {
  446. positionIter.addValue(positions[j]);
  447. texcoordIter.addValue(uvs[j]);
  448. normalColorIter.addValue(normalColor.getAsRGBA());
  449. fadedColorIter.addValue(fadedColor.getAsRGBA());
  450. }
  451. UINT32 vertOffset = i * 4;
  452. indices[0] = vertOffset + 0;
  453. indices[1] = vertOffset + 1;
  454. indices[2] = vertOffset + 2;
  455. indices[3] = vertOffset + 0;
  456. indices[4] = vertOffset + 2;
  457. indices[5] = vertOffset + 3;
  458. indices += 6;
  459. }
  460. return mIconMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  461. }
  462. void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
  463. {
  464. if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
  465. return;
  466. float relWidth = OPTIMAL_ICON_SIZE / (float)width;
  467. float relHeight = OPTIMAL_ICON_SIZE / (float)height;
  468. float scale = std::min(relWidth, relHeight);
  469. width = Math::roundToInt(width * scale);
  470. height = Math::roundToInt(height * scale);
  471. }
  472. void GizmoManager::calculateIconColors(const Color& tint, const CameraHandlerPtr& camera,
  473. UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
  474. {
  475. normalColor = tint;
  476. if (!fixedScale)
  477. {
  478. float iconToScreenRatio = iconHeight / (float)camera->getViewport()->getHeight();
  479. if (iconToScreenRatio > 0.3f)
  480. {
  481. float alpha = 1.0f - Math::lerp01(iconToScreenRatio, 0.3f, 1.0f);
  482. normalColor.a *= alpha;
  483. }
  484. else if (iconToScreenRatio < 0.1f)
  485. {
  486. float alpha = Math::lerp01(iconToScreenRatio, 0.0f, 0.1f);
  487. normalColor.a *= alpha;
  488. }
  489. }
  490. fadedColor = normalColor;
  491. fadedColor.a *= 0.2f;
  492. }
  493. HSceneObject GizmoManager::getSceneObject(UINT32 gizmoIdx)
  494. {
  495. auto iterFind = mIdxToSceneObjectMap.find(gizmoIdx);
  496. if (iterFind != mIdxToSceneObjectMap.end())
  497. return iterFind->second;
  498. return HSceneObject();
  499. }
  500. const float GizmoManagerCore::PICKING_ALPHA_CUTOFF = 0.5f;
  501. GizmoManagerCore::GizmoManagerCore(const PrivatelyConstuct& dummy)
  502. {
  503. }
  504. void GizmoManagerCore::initialize(const GizmoManager::CoreInitData& initData)
  505. {
  506. THROW_IF_NOT_CORE_THREAD;
  507. mSolidMaterial.mat = initData.solidMat;
  508. mWireMaterial.mat = initData.wireMat;
  509. mIconMaterial.mat = initData.iconMat;
  510. mPickingMaterial.mat = initData.pickingMat;
  511. mAlphaPickingMaterial.mat = initData.alphaPickingMat;
  512. {
  513. SPtr<MaterialCore> mat = mWireMaterial.mat;
  514. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  515. vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
  516. }
  517. {
  518. SPtr<MaterialCore> mat = mSolidMaterial.mat;
  519. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  520. vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
  521. }
  522. {
  523. SPtr<MaterialCore> mat = mIconMaterial.mat;
  524. SPtr<PassParametersCore> pass0Params = mat->getPassParameters(0);
  525. SPtr<PassParametersCore> pass1Params = mat->getPassParameters(1);
  526. SPtr<GpuParamsCore> vertParams0 = pass0Params->mVertParams;
  527. SPtr<GpuParamsCore> vertParams1 = pass1Params->mVertParams;
  528. vertParams0->getParam("matViewProj", mIconMaterial.mViewProj[0]);
  529. vertParams1->getParam("matViewProj", mIconMaterial.mViewProj[1]);
  530. mIconMaterial.mFragParams[0] = pass0Params->mFragParams;
  531. mIconMaterial.mFragParams[1] = pass1Params->mFragParams;
  532. mIconMaterial.mFragParams[0]->getTextureParam("mainTexture", mIconMaterial.mTexture[0]);
  533. mIconMaterial.mFragParams[1]->getTextureParam("mainTexture", mIconMaterial.mTexture[1]);
  534. }
  535. {
  536. SPtr<MaterialCore> mat = mPickingMaterial.mat;
  537. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  538. vertParams->getParam("matViewProj", mPickingMaterial.mViewProj);
  539. }
  540. {
  541. SPtr<MaterialCore> mat = mAlphaPickingMaterial.mat;
  542. SPtr<PassParametersCore> passParams = mat->getPassParameters(0);
  543. SPtr<GpuParamsCore> vertParams = passParams->mVertParams;
  544. vertParams->getParam("matViewProj", mAlphaPickingMaterial.mViewProj);
  545. mAlphaPickingMaterial.mFragParams = passParams->mFragParams;
  546. mAlphaPickingMaterial.mFragParams->getTextureParam("mainTexture", mAlphaPickingMaterial.mTexture);
  547. GpuParamFloatCore alphaCutoffParam;
  548. mAlphaPickingMaterial.mFragParams->getParam("alphaCutoff", alphaCutoffParam);
  549. alphaCutoffParam.set(PICKING_ALPHA_CUTOFF);
  550. }
  551. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  552. activeRenderer->onCorePostRenderViewport.connect(std::bind(&GizmoManagerCore::render, this, _1));
  553. }
  554. void GizmoManagerCore::updateData(const SPtr<RenderTargetCore>& rt, const SPtr<MeshCoreBase>& solidMesh, const SPtr<MeshCoreBase>& wireMesh,
  555. const SPtr<MeshCoreBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
  556. {
  557. mSceneRenderTarget = rt;
  558. mSolidMesh = solidMesh;
  559. mWireMesh = wireMesh;
  560. mIconMesh = iconMesh;
  561. mIconRenderData = iconRenderData;
  562. }
  563. void GizmoManagerCore::render(const CameraHandlerCore& camera)
  564. {
  565. if (mSceneRenderTarget == nullptr)
  566. return;
  567. if (camera.getViewport()->getTarget() != mSceneRenderTarget)
  568. return;
  569. float width = (float)mSceneRenderTarget->getProperties().getWidth();
  570. float height = (float)mSceneRenderTarget->getProperties().getHeight();
  571. Rect2 normArea = camera.getViewport()->getNormArea();
  572. Rect2I screenArea;
  573. screenArea.x = (int)(normArea.x * width);
  574. screenArea.y = (int)(normArea.y * height);
  575. screenArea.width = (int)(normArea.width * width);
  576. screenArea.height = (int)(normArea.height * height);
  577. if (mSolidMesh != nullptr)
  578. renderGizmos(camera.getViewMatrix(), camera.getProjectionMatrixRS(), mSolidMesh, GizmoManager::GizmoMaterial::Solid);
  579. if (mWireMesh != nullptr)
  580. renderGizmos(camera.getViewMatrix(), camera.getProjectionMatrixRS(), mWireMesh, GizmoManager::GizmoMaterial::Wire);
  581. if (mIconMesh != nullptr)
  582. renderIconGizmos(screenArea, mIconMesh, mIconRenderData, false);
  583. }
  584. void GizmoManagerCore::renderGizmos(Matrix4 viewMatrix, Matrix4 projMatrix, SPtr<MeshCoreBase> mesh, GizmoManager::GizmoMaterial material)
  585. {
  586. THROW_IF_NOT_CORE_THREAD;
  587. Matrix4 viewProjMat = projMatrix * viewMatrix;
  588. switch (material)
  589. {
  590. case GizmoManager::GizmoMaterial::Solid:
  591. mSolidMaterial.mViewProj.set(viewProjMat);
  592. CoreRenderer::setPass(mSolidMaterial.mat, 0);
  593. break;
  594. case GizmoManager::GizmoMaterial::Wire:
  595. mWireMaterial.mViewProj.set(viewProjMat);
  596. CoreRenderer::setPass(mWireMaterial.mat, 0);
  597. break;
  598. case GizmoManager::GizmoMaterial::Picking:
  599. mPickingMaterial.mViewProj.set(viewProjMat);
  600. CoreRenderer::setPass(mPickingMaterial.mat, 0);
  601. break;
  602. }
  603. CoreRenderer::draw(mesh, mesh->getProperties().getSubMesh(0));
  604. }
  605. void GizmoManagerCore::renderIconGizmos(Rect2I screenArea, SPtr<MeshCoreBase> mesh, GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
  606. {
  607. RenderAPICore& rs = RenderAPICore::instance();
  608. const MeshProperties& meshProps = mesh->getProperties();
  609. std::shared_ptr<VertexData> vertexData = mesh->getVertexData();
  610. rs.setVertexDeclaration(vertexData->vertexDeclaration);
  611. auto vertexBuffers = vertexData->getBuffers();
  612. SPtr<VertexBufferCore> vertBuffers[1] = { vertexBuffers.begin()->second };
  613. rs.setVertexBuffers(0, vertBuffers, 1);
  614. SPtr<IndexBufferCore> indexBuffer = mesh->getIndexBuffer();
  615. rs.setIndexBuffer(indexBuffer);
  616. rs.setDrawOperation(DOT_TRIANGLE_LIST);
  617. // Set up ortho matrix
  618. Matrix4 projMat;
  619. float left = screenArea.x + rs.getHorizontalTexelOffset();
  620. float right = screenArea.x + screenArea.width + rs.getHorizontalTexelOffset();
  621. float top = screenArea.y + rs.getVerticalTexelOffset();
  622. float bottom = screenArea.y + screenArea.height + rs.getVerticalTexelOffset();
  623. float near = rs.getMinimumDepthInputValue();
  624. float far = rs.getMaximumDepthInputValue();
  625. // Top/bottom have been swapped because we're moving from window coordinates (origin top left)
  626. // to normalized device coordinates (origin bottom left)
  627. // Negative near/far because Z is flipped for normalized device coordinates
  628. // (positive Z goes into screen as opposed to view space here we're looking along negative Z)
  629. projMat.makeProjectionOrtho(left, right, top, bottom, -near, -far);
  630. if (!usePickingMaterial)
  631. {
  632. mIconMaterial.mViewProj[0].set(projMat);
  633. mIconMaterial.mViewProj[1].set(projMat);
  634. for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
  635. {
  636. CoreRenderer::setPass(mIconMaterial.mat, passIdx);
  637. UINT32 curIndexOffset = mesh->getIndexOffset();
  638. for (auto curRenderData : *renderData)
  639. {
  640. mIconMaterial.mTexture[passIdx].set(curRenderData.texture);
  641. rs.bindGpuParams(GPT_FRAGMENT_PROGRAM, mIconMaterial.mFragParams[passIdx]);
  642. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  643. curIndexOffset += curRenderData.count * 6;
  644. }
  645. }
  646. }
  647. else
  648. {
  649. mAlphaPickingMaterial.mViewProj.set(projMat);
  650. CoreRenderer::setPass(mAlphaPickingMaterial.mat, 0);
  651. UINT32 curIndexOffset = 0;
  652. for (auto curRenderData : *renderData)
  653. {
  654. mAlphaPickingMaterial.mTexture.set(curRenderData.texture);
  655. rs.bindGpuParams(GPT_FRAGMENT_PROGRAM, mAlphaPickingMaterial.mFragParams);
  656. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  657. curIndexOffset += curRenderData.count * 6;
  658. }
  659. }
  660. mesh->_notifyUsedOnGPU();
  661. }
  662. }