BsGizmoManager.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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, solidMesh, wireMesh, iconMesh, iconRenderData));
  220. }
  221. void GizmoManager::renderForPicking(const CameraHandlerPtr& camera, std::function<Color(UINT32)> idxToColorCallback)
  222. {
  223. Vector<IconData> iconData;
  224. IconRenderDataVecPtr iconRenderData;
  225. mPickingDrawHelper->clear();
  226. for (auto& cubeDataEntry : mSolidCubeData)
  227. {
  228. if (!cubeDataEntry.pickable)
  229. continue;
  230. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  231. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  232. mPickingDrawHelper->cube(cubeDataEntry.position, cubeDataEntry.extents);
  233. }
  234. for (auto& cubeDataEntry : mWireCubeData)
  235. {
  236. if (!cubeDataEntry.pickable)
  237. continue;
  238. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  239. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  240. mPickingDrawHelper->wireCube(cubeDataEntry.position, cubeDataEntry.extents);
  241. }
  242. for (auto& sphereDataEntry : mSolidSphereData)
  243. {
  244. if (!sphereDataEntry.pickable)
  245. continue;
  246. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  247. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  248. mPickingDrawHelper->sphere(sphereDataEntry.position, sphereDataEntry.radius);
  249. }
  250. for (auto& sphereDataEntry : mWireSphereData)
  251. {
  252. if (!sphereDataEntry.pickable)
  253. continue;
  254. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  255. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  256. mPickingDrawHelper->wireSphere(sphereDataEntry.position, sphereDataEntry.radius);
  257. }
  258. for (auto& lineDataEntry : mLineData)
  259. {
  260. if (!lineDataEntry.pickable)
  261. continue;
  262. mPickingDrawHelper->setColor(idxToColorCallback(lineDataEntry.idx));
  263. mPickingDrawHelper->setTransform(lineDataEntry.transform);
  264. mPickingDrawHelper->line(lineDataEntry.start, lineDataEntry.end);
  265. }
  266. for (auto& frustumDataEntry : mFrustumData)
  267. {
  268. if (!frustumDataEntry.pickable)
  269. continue;
  270. mPickingDrawHelper->setColor(idxToColorCallback(frustumDataEntry.idx));
  271. mPickingDrawHelper->setTransform(frustumDataEntry.transform);
  272. mPickingDrawHelper->frustum(frustumDataEntry.position, frustumDataEntry.aspect, frustumDataEntry.FOV,
  273. frustumDataEntry.near, frustumDataEntry.far);
  274. }
  275. for (auto& iconDataEntry : mIconData)
  276. {
  277. if (!iconDataEntry.pickable)
  278. continue;
  279. iconData.push_back(iconDataEntry);
  280. iconData.back().color = idxToColorCallback(iconDataEntry.idx);
  281. }
  282. mPickingDrawHelper->buildMeshes();
  283. const Vector<DrawHelper::ShapeMeshData>& meshes = mPickingDrawHelper->getMeshes();
  284. TransientMeshPtr iconMesh = buildIconMesh(camera, iconData, true, iconRenderData);
  285. // Note: This must be rendered while Scene view is being rendered
  286. Matrix4 viewMat = camera->getViewMatrix();
  287. Matrix4 projMat = camera->getProjectionMatrixRS();
  288. ViewportPtr viewport = camera->getViewport();
  289. for (auto& meshData : meshes)
  290. {
  291. if (meshData.type == DrawHelper::MeshType::Solid)
  292. {
  293. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos,
  294. mCore, viewMat, projMat, meshData.mesh->getCore(), GizmoMaterial::Picking));
  295. }
  296. else // Wire
  297. {
  298. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos,
  299. mCore, viewMat, projMat, meshData.mesh->getCore(), GizmoMaterial::Picking));
  300. }
  301. }
  302. Rect2I screenArea = camera->getViewport()->getArea();
  303. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderIconGizmos,
  304. mCore, screenArea, iconMesh->getCore(), iconRenderData, true));
  305. mPickingDrawHelper->clearMeshes();
  306. mIconMeshHeap->dealloc(iconMesh);
  307. }
  308. void GizmoManager::clearGizmos()
  309. {
  310. mSolidCubeData.clear();
  311. mWireCubeData.clear();
  312. mSolidSphereData.clear();
  313. mWireSphereData.clear();
  314. mLineData.clear();
  315. mFrustumData.clear();
  316. mIconData.clear();
  317. mIdxToSceneObjectMap.clear();
  318. mDrawHelper->clear();
  319. mCurrentIdx = 0;
  320. }
  321. void GizmoManager::clearRenderData()
  322. {
  323. mDrawHelper->clearMeshes();
  324. if (mIconMesh != nullptr)
  325. mIconMeshHeap->dealloc(mIconMesh);
  326. mIconMesh = nullptr;
  327. IconRenderDataVecPtr iconRenderData = bs_shared_ptr<IconRenderDataVec>();
  328. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, mCore, nullptr, nullptr, nullptr, nullptr, iconRenderData));
  329. }
  330. TransientMeshPtr GizmoManager::buildIconMesh(const CameraHandlerPtr& camera, const Vector<IconData>& iconData,
  331. bool forPicking, GizmoManager::IconRenderDataVecPtr& iconRenderData)
  332. {
  333. mSortedIconData.clear();
  334. if (iconData.size() > mSortedIconData.size())
  335. mSortedIconData.resize(iconData.size());
  336. UINT32 i = 0;
  337. for (auto& iconEntry : iconData)
  338. {
  339. Vector3 viewPoint = camera->worldToViewPoint(iconEntry.position);
  340. float distance = -viewPoint.z;
  341. if (distance < camera->getNearClipDistance()) // Ignore behind clip plane
  342. continue;
  343. if (distance > MAX_ICON_RANGE) // Ignore too far away
  344. continue;
  345. if (!iconEntry.texture) // Ignore missing texture
  346. continue;
  347. if (forPicking && !iconEntry.pickable)
  348. continue;
  349. SortedIconData& sortedIconData = mSortedIconData[i];
  350. sortedIconData.iconIdx = i;
  351. sortedIconData.distance = distance;
  352. sortedIconData.screenPosition = camera->viewToScreenPoint(viewPoint);
  353. i++;
  354. }
  355. UINT32 actualNumIcons = i;
  356. // Sort back to front first, then by texture
  357. std::sort(mSortedIconData.begin(), mSortedIconData.begin() + actualNumIcons,
  358. [&](const SortedIconData& a, const SortedIconData& b)
  359. {
  360. if (a.distance == b.distance)
  361. {
  362. HTexture texA = iconData[a.iconIdx].texture->getTexture();
  363. HTexture texB = iconData[b.iconIdx].texture->getTexture();
  364. if (texA == texB)
  365. return a.iconIdx < b.iconIdx;
  366. return texA->getInternalID() < texB->getInternalID();
  367. }
  368. else
  369. return a.distance > b.distance;
  370. });
  371. MeshDataPtr meshData = bs_shared_ptr<MeshData>(actualNumIcons * 4, actualNumIcons * 6, mIconVertexDesc);
  372. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  373. auto texcoordIter = meshData->getVec2DataIter(VES_TEXCOORD);
  374. auto normalColorIter = meshData->getDWORDDataIter(VES_COLOR, 0);
  375. auto fadedColorIter = meshData->getDWORDDataIter(VES_COLOR, 1);
  376. UINT32* indices = meshData->getIndices32();
  377. float cameraScale = 1.0f;
  378. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  379. cameraScale = camera->getViewport()->getHeight() / camera->getOrthoWindowHeight();
  380. else
  381. {
  382. Radian vertFOV(Math::tan(camera->getHorzFOV() * 0.5f));
  383. cameraScale = (camera->getViewport()->getHeight() * 0.5f) / vertFOV.valueRadians();
  384. }
  385. iconRenderData = bs_shared_ptr<IconRenderDataVec>();
  386. UINT32 lastTextureIdx = std::numeric_limits<UINT32>::max();
  387. HTexture curTexture;
  388. // Note: This assumes the meshes will be rendered using the same camera
  389. // properties as when they are created
  390. for (i = 0; i < actualNumIcons; i++)
  391. {
  392. SortedIconData& sortedIconData = mSortedIconData[i];
  393. const IconData& curIconData = iconData[sortedIconData.iconIdx];
  394. HTexture atlasTexture = curIconData.texture->getTexture();
  395. if (curTexture != atlasTexture)
  396. {
  397. UINT32 numIconsPerTexture = i - lastTextureIdx;
  398. if (numIconsPerTexture > 0)
  399. {
  400. iconRenderData->push_back(IconRenderData());
  401. IconRenderData& renderData = iconRenderData->back();
  402. renderData.count = numIconsPerTexture;
  403. renderData.texture = atlasTexture->getCore();
  404. }
  405. lastTextureIdx = i;
  406. curTexture = atlasTexture;
  407. }
  408. UINT32 iconWidth = curIconData.texture->getWidth();
  409. UINT32 iconHeight = curIconData.texture->getHeight();
  410. limitIconSize(iconWidth, iconHeight);
  411. Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, -sortedIconData.distance);
  412. Vector3 projPosition = camera->projectPoint(position);
  413. position.z = projPosition.z;
  414. float halfWidth = iconWidth * 0.5f;
  415. float halfHeight = iconHeight * 0.5f;
  416. if (!curIconData.fixedScale)
  417. {
  418. float iconScale = 1.0f;
  419. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  420. iconScale = cameraScale * ICON_TEXEL_WORLD_SIZE;
  421. else
  422. iconScale = (cameraScale * ICON_TEXEL_WORLD_SIZE) / sortedIconData.distance;
  423. halfWidth *= iconScale;
  424. halfHeight *= iconScale;
  425. }
  426. Color normalColor, fadedColor;
  427. calculateIconColors(curIconData.color, camera, (UINT32)(halfHeight * 2.0f), curIconData.fixedScale, normalColor, fadedColor);
  428. if (forPicking)
  429. {
  430. normalColor = curIconData.color;
  431. fadedColor = curIconData.color;
  432. }
  433. Vector3 positions[4];
  434. positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
  435. positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
  436. positions[2] = position + Vector3(halfWidth, halfHeight, 0.0f);
  437. positions[3] = position + Vector3(-halfWidth, halfHeight, 0.0f);
  438. Vector2 uvs[4];
  439. uvs[0] = curIconData.texture->transformUV(Vector2(0.0f, 0.0f));
  440. uvs[1] = curIconData.texture->transformUV(Vector2(1.0f, 0.0f));
  441. uvs[2] = curIconData.texture->transformUV(Vector2(1.0f, 1.0f));
  442. uvs[3] = curIconData.texture->transformUV(Vector2(0.0f, 1.0f));
  443. for (UINT32 j = 0; j < 4; j++)
  444. {
  445. positionIter.addValue(positions[j]);
  446. texcoordIter.addValue(uvs[j]);
  447. normalColorIter.addValue(normalColor.getAsRGBA());
  448. fadedColorIter.addValue(fadedColor.getAsRGBA());
  449. }
  450. UINT32 vertOffset = i * 4;
  451. indices[0] = vertOffset + 0;
  452. indices[1] = vertOffset + 1;
  453. indices[2] = vertOffset + 2;
  454. indices[3] = vertOffset + 0;
  455. indices[4] = vertOffset + 2;
  456. indices[5] = vertOffset + 3;
  457. indices += 6;
  458. }
  459. return mIconMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  460. }
  461. void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
  462. {
  463. if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
  464. return;
  465. float relWidth = OPTIMAL_ICON_SIZE / (float)width;
  466. float relHeight = OPTIMAL_ICON_SIZE / (float)height;
  467. float scale = std::min(relWidth, relHeight);
  468. width = Math::roundToInt(width * scale);
  469. height = Math::roundToInt(height * scale);
  470. }
  471. void GizmoManager::calculateIconColors(const Color& tint, const CameraHandlerPtr& camera,
  472. UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
  473. {
  474. normalColor = tint;
  475. if (!fixedScale)
  476. {
  477. float iconToScreenRatio = iconHeight / (float)camera->getViewport()->getHeight();
  478. if (iconToScreenRatio > 0.3f)
  479. {
  480. float alpha = 1.0f - Math::lerp01(iconToScreenRatio, 0.3f, 1.0f);
  481. normalColor.a *= alpha;
  482. }
  483. else if (iconToScreenRatio < 0.1f)
  484. {
  485. float alpha = Math::lerp01(iconToScreenRatio, 0.0f, 0.1f);
  486. normalColor.a *= alpha;
  487. }
  488. }
  489. fadedColor = normalColor;
  490. fadedColor.a *= 0.2f;
  491. }
  492. HSceneObject GizmoManager::getSceneObject(UINT32 gizmoIdx)
  493. {
  494. auto iterFind = mIdxToSceneObjectMap.find(gizmoIdx);
  495. if (iterFind != mIdxToSceneObjectMap.end())
  496. return iterFind->second;
  497. return HSceneObject();
  498. }
  499. const float GizmoManagerCore::PICKING_ALPHA_CUTOFF = 0.5f;
  500. GizmoManagerCore::GizmoManagerCore(const PrivatelyConstuct& dummy)
  501. {
  502. }
  503. void GizmoManagerCore::initialize(const GizmoManager::CoreInitData& initData)
  504. {
  505. THROW_IF_NOT_CORE_THREAD;
  506. mSolidMaterial.mat = initData.solidMat;
  507. mWireMaterial.mat = initData.wireMat;
  508. mIconMaterial.mat = initData.iconMat;
  509. mPickingMaterial.mat = initData.pickingMat;
  510. mAlphaPickingMaterial.mat = initData.alphaPickingMat;
  511. {
  512. SPtr<MaterialCore> mat = mWireMaterial.mat;
  513. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  514. vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
  515. }
  516. {
  517. SPtr<MaterialCore> mat = mSolidMaterial.mat;
  518. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  519. vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
  520. }
  521. {
  522. SPtr<MaterialCore> mat = mIconMaterial.mat;
  523. SPtr<PassParametersCore> pass0Params = mat->getPassParameters(0);
  524. SPtr<PassParametersCore> pass1Params = mat->getPassParameters(1);
  525. SPtr<GpuParamsCore> vertParams0 = pass0Params->mVertParams;
  526. SPtr<GpuParamsCore> vertParams1 = pass1Params->mVertParams;
  527. vertParams0->getParam("matViewProj", mIconMaterial.mViewProj[0]);
  528. vertParams1->getParam("matViewProj", mIconMaterial.mViewProj[1]);
  529. mIconMaterial.mFragParams[0] = pass0Params->mFragParams;
  530. mIconMaterial.mFragParams[1] = pass1Params->mFragParams;
  531. mIconMaterial.mFragParams[0]->getTextureParam("mainTexture", mIconMaterial.mTexture[0]);
  532. mIconMaterial.mFragParams[1]->getTextureParam("mainTexture", mIconMaterial.mTexture[1]);
  533. }
  534. {
  535. SPtr<MaterialCore> mat = mPickingMaterial.mat;
  536. SPtr<GpuParamsCore> vertParams = mat->getPassParameters(0)->mVertParams;
  537. vertParams->getParam("matViewProj", mPickingMaterial.mViewProj);
  538. }
  539. {
  540. SPtr<MaterialCore> mat = mAlphaPickingMaterial.mat;
  541. SPtr<PassParametersCore> passParams = mat->getPassParameters(0);
  542. SPtr<GpuParamsCore> vertParams = passParams->mVertParams;
  543. vertParams->getParam("matViewProj", mAlphaPickingMaterial.mViewProj);
  544. mAlphaPickingMaterial.mFragParams = passParams->mFragParams;
  545. mAlphaPickingMaterial.mFragParams->getTextureParam("mainTexture", mAlphaPickingMaterial.mTexture);
  546. GpuParamFloatCore alphaCutoffParam;
  547. mAlphaPickingMaterial.mFragParams->getParam("alphaCutoff", alphaCutoffParam);
  548. alphaCutoffParam.set(PICKING_ALPHA_CUTOFF);
  549. }
  550. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  551. activeRenderer->onCorePostRenderViewport.connect(std::bind(&GizmoManagerCore::render, this, _1));
  552. }
  553. void GizmoManagerCore::updateData(const RenderTargetPtr& rt, const SPtr<MeshCoreBase>& solidMesh, const SPtr<MeshCoreBase>& wireMesh,
  554. const SPtr<MeshCoreBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
  555. {
  556. mSceneRenderTarget = rt;
  557. mSolidMesh = solidMesh;
  558. mWireMesh = wireMesh;
  559. mIconMesh = iconMesh;
  560. mIconRenderData = iconRenderData;
  561. }
  562. void GizmoManagerCore::render(const CameraHandlerCore& camera)
  563. {
  564. if (mSceneRenderTarget == nullptr)
  565. return;
  566. SPtr<RenderTargetCore> sceneRenderTarget = mSceneRenderTarget->getCore();
  567. if (camera.getViewport()->getTarget() != sceneRenderTarget)
  568. return;
  569. float width = (float)sceneRenderTarget->getProperties().getWidth();
  570. float height = (float)sceneRenderTarget->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. }