BsGizmoManager.cpp 26 KB

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