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 "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(const CameraHandlerPtr& camera)
  192. {
  193. mDrawHelper->clearMeshes();
  194. if (mIconMesh != nullptr)
  195. mIconMeshHeap->dealloc(mIconMesh);
  196. RenderTargetPtr rt;
  197. SceneEditorWidget* sceneView = SceneViewLocator::instance();
  198. if (sceneView != nullptr)
  199. {
  200. rt = camera->getViewport()->getTarget();
  201. IconRenderDataVecPtr iconRenderData;
  202. mDrawHelper->buildMeshes();
  203. const Vector<DrawHelper::ShapeMeshData>& meshes = mDrawHelper->getMeshes();
  204. MeshProxyPtr solidMeshProxy = nullptr;
  205. MeshProxyPtr wireMeshProxy = nullptr;
  206. for (auto& meshData : meshes)
  207. {
  208. if (meshData.type == DrawHelper::MeshType::Solid)
  209. {
  210. if (solidMeshProxy == nullptr)
  211. solidMeshProxy = meshData.mesh->_createProxy(0);
  212. }
  213. else // Wire
  214. {
  215. if (wireMeshProxy == nullptr)
  216. wireMeshProxy = meshData.mesh->_createProxy(0);
  217. }
  218. }
  219. // Since there is no sorting used with draw helper meshes we only expect up to two of them,
  220. // one for solids, one for wireframe
  221. assert(meshes.size() <= 2);
  222. mIconMesh = buildIconMesh(camera, mIconData, false, iconRenderData);
  223. MeshProxyPtr iconMeshProxy = mIconMesh->_createProxy(0);
  224. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, mCore, rt, solidMeshProxy, wireMeshProxy, iconMeshProxy, iconRenderData));
  225. }
  226. else
  227. {
  228. mIconMesh = nullptr;
  229. IconRenderDataVecPtr iconRenderData = bs_shared_ptr<IconRenderDataVec>();
  230. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::updateData, mCore, nullptr, nullptr, nullptr, nullptr, iconRenderData));
  231. }
  232. }
  233. void GizmoManager::renderForPicking(const CameraHandlerPtr& camera, std::function<Color(UINT32)> idxToColorCallback)
  234. {
  235. Vector<IconData> iconData;
  236. IconRenderDataVecPtr iconRenderData;
  237. mPickingDrawHelper->clear();
  238. for (auto& cubeDataEntry : mSolidCubeData)
  239. {
  240. if (!cubeDataEntry.pickable)
  241. continue;
  242. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  243. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  244. mPickingDrawHelper->cube(cubeDataEntry.position, cubeDataEntry.extents);
  245. }
  246. for (auto& cubeDataEntry : mWireCubeData)
  247. {
  248. if (!cubeDataEntry.pickable)
  249. continue;
  250. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  251. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  252. mPickingDrawHelper->wireCube(cubeDataEntry.position, cubeDataEntry.extents);
  253. }
  254. for (auto& sphereDataEntry : mSolidSphereData)
  255. {
  256. if (!sphereDataEntry.pickable)
  257. continue;
  258. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  259. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  260. mPickingDrawHelper->sphere(sphereDataEntry.position, sphereDataEntry.radius);
  261. }
  262. for (auto& sphereDataEntry : mWireSphereData)
  263. {
  264. if (!sphereDataEntry.pickable)
  265. continue;
  266. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  267. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  268. mPickingDrawHelper->wireSphere(sphereDataEntry.position, sphereDataEntry.radius);
  269. }
  270. for (auto& lineDataEntry : mLineData)
  271. {
  272. if (!lineDataEntry.pickable)
  273. continue;
  274. mPickingDrawHelper->setColor(idxToColorCallback(lineDataEntry.idx));
  275. mPickingDrawHelper->setTransform(lineDataEntry.transform);
  276. mPickingDrawHelper->line(lineDataEntry.start, lineDataEntry.end);
  277. }
  278. for (auto& frustumDataEntry : mFrustumData)
  279. {
  280. if (!frustumDataEntry.pickable)
  281. continue;
  282. mPickingDrawHelper->setColor(idxToColorCallback(frustumDataEntry.idx));
  283. mPickingDrawHelper->setTransform(frustumDataEntry.transform);
  284. mPickingDrawHelper->frustum(frustumDataEntry.position, frustumDataEntry.aspect, frustumDataEntry.FOV,
  285. frustumDataEntry.near, frustumDataEntry.far);
  286. }
  287. for (auto& iconDataEntry : mIconData)
  288. {
  289. if (!iconDataEntry.pickable)
  290. continue;
  291. iconData.push_back(iconDataEntry);
  292. iconData.back().color = idxToColorCallback(iconDataEntry.idx);
  293. }
  294. mPickingDrawHelper->buildMeshes();
  295. const Vector<DrawHelper::ShapeMeshData>& meshes = mPickingDrawHelper->getMeshes();
  296. TransientMeshPtr iconMesh = buildIconMesh(camera, iconData, true, iconRenderData);
  297. // Note: This must be rendered while Scene view is being rendered
  298. Matrix4 viewMat = camera->getViewMatrix();
  299. Matrix4 projMat = camera->getProjectionMatrixRS();
  300. ViewportPtr viewport = camera->getViewport();
  301. for (auto& meshData : meshes)
  302. {
  303. if (meshData.type == DrawHelper::MeshType::Solid)
  304. {
  305. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos,
  306. mCore, viewMat, projMat, meshData.mesh->_createProxy(0), GizmoMaterial::Picking));
  307. }
  308. else // Wire
  309. {
  310. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderGizmos,
  311. mCore, viewMat, projMat, meshData.mesh->_createProxy(0), GizmoMaterial::Picking));
  312. }
  313. }
  314. Rect2I screenArea = camera->getViewport()->getArea();
  315. gCoreAccessor().queueCommand(std::bind(&GizmoManagerCore::renderIconGizmos,
  316. mCore, screenArea, iconMesh->_createProxy(0), iconRenderData, true));
  317. mPickingDrawHelper->clearMeshes();
  318. mIconMeshHeap->dealloc(iconMesh);
  319. }
  320. void GizmoManager::clearGizmos()
  321. {
  322. mSolidCubeData.clear();
  323. mWireCubeData.clear();
  324. mSolidSphereData.clear();
  325. mWireSphereData.clear();
  326. mLineData.clear();
  327. mFrustumData.clear();
  328. mIconData.clear();
  329. mIdxToSceneObjectMap.clear();
  330. mDrawHelper->clear();
  331. mCurrentIdx = 0;
  332. }
  333. TransientMeshPtr GizmoManager::buildIconMesh(const CameraHandlerPtr& camera, const Vector<IconData>& iconData,
  334. bool forPicking, GizmoManager::IconRenderDataVecPtr& iconRenderData)
  335. {
  336. mSortedIconData.clear();
  337. if (iconData.size() > mSortedIconData.size())
  338. mSortedIconData.resize(iconData.size());
  339. UINT32 i = 0;
  340. for (auto& iconEntry : iconData)
  341. {
  342. Vector3 viewPoint = camera->worldToViewPoint(iconEntry.position);
  343. float distance = -viewPoint.z;
  344. if (distance < camera->getNearClipDistance()) // Ignore behind clip plane
  345. continue;
  346. if (distance > MAX_ICON_RANGE) // Ignore too far away
  347. continue;
  348. if (!iconEntry.texture) // Ignore missing texture
  349. continue;
  350. if (forPicking && !iconEntry.pickable)
  351. continue;
  352. SortedIconData& sortedIconData = mSortedIconData[i];
  353. sortedIconData.iconIdx = i;
  354. sortedIconData.distance = distance;
  355. sortedIconData.screenPosition = camera->viewToScreenPoint(viewPoint);
  356. i++;
  357. }
  358. UINT32 actualNumIcons = i;
  359. // Sort back to front first, then by texture
  360. std::sort(mSortedIconData.begin(), mSortedIconData.begin() + actualNumIcons,
  361. [&](const SortedIconData& a, const SortedIconData& b)
  362. {
  363. if (a.distance == b.distance)
  364. {
  365. HTexture texA = iconData[a.iconIdx].texture->getTexture();
  366. HTexture texB = iconData[b.iconIdx].texture->getTexture();
  367. if (texA == texB)
  368. return a.iconIdx < b.iconIdx;
  369. return texA->getInternalID() < texB->getInternalID();
  370. }
  371. else
  372. return a.distance > b.distance;
  373. });
  374. MeshDataPtr meshData = bs_shared_ptr<MeshData>(actualNumIcons * 4, actualNumIcons * 6, mIconVertexDesc);
  375. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  376. auto texcoordIter = meshData->getVec2DataIter(VES_TEXCOORD);
  377. auto normalColorIter = meshData->getDWORDDataIter(VES_COLOR, 0);
  378. auto fadedColorIter = meshData->getDWORDDataIter(VES_COLOR, 1);
  379. UINT32* indices = meshData->getIndices32();
  380. float cameraScale = 1.0f;
  381. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  382. cameraScale = camera->getViewport()->getHeight() / camera->getOrthoWindowHeight();
  383. else
  384. {
  385. Radian vertFOV(Math::tan(camera->getHorzFOV() * 0.5f));
  386. cameraScale = (camera->getViewport()->getHeight() * 0.5f) / vertFOV.valueRadians();
  387. }
  388. iconRenderData = bs_shared_ptr<IconRenderDataVec>();
  389. UINT32 lastTextureIdx = std::numeric_limits<UINT32>::max();
  390. HTexture curTexture;
  391. // Note: This assumes the meshes will be rendered using the same camera
  392. // properties as when they are created
  393. for (i = 0; i < actualNumIcons; i++)
  394. {
  395. SortedIconData& sortedIconData = mSortedIconData[i];
  396. const IconData& curIconData = iconData[sortedIconData.iconIdx];
  397. HTexture atlasTexture = curIconData.texture->getTexture();
  398. if (curTexture != atlasTexture)
  399. {
  400. UINT32 numIconsPerTexture = i - lastTextureIdx;
  401. if (numIconsPerTexture > 0)
  402. {
  403. iconRenderData->push_back(IconRenderData());
  404. IconRenderData& renderData = iconRenderData->back();
  405. renderData.count = numIconsPerTexture;
  406. renderData.texture = atlasTexture;
  407. }
  408. lastTextureIdx = i;
  409. curTexture = atlasTexture;
  410. }
  411. UINT32 iconWidth = curIconData.texture->getWidth();
  412. UINT32 iconHeight = curIconData.texture->getHeight();
  413. limitIconSize(iconWidth, iconHeight);
  414. Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, -sortedIconData.distance);
  415. Vector3 projPosition = camera->projectPoint(position);
  416. position.z = projPosition.z;
  417. float halfWidth = iconWidth * 0.5f;
  418. float halfHeight = iconHeight * 0.5f;
  419. if (!curIconData.fixedScale)
  420. {
  421. float iconScale = 1.0f;
  422. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  423. iconScale = cameraScale * ICON_TEXEL_WORLD_SIZE;
  424. else
  425. iconScale = (cameraScale * ICON_TEXEL_WORLD_SIZE) / sortedIconData.distance;
  426. halfWidth *= iconScale;
  427. halfHeight *= iconScale;
  428. }
  429. Color normalColor, fadedColor;
  430. calculateIconColors(curIconData.color, camera, (UINT32)(halfHeight * 2.0f), curIconData.fixedScale, normalColor, fadedColor);
  431. if (forPicking)
  432. {
  433. normalColor = curIconData.color;
  434. fadedColor = curIconData.color;
  435. }
  436. Vector3 positions[4];
  437. positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
  438. positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
  439. positions[2] = position + Vector3(halfWidth, halfHeight, 0.0f);
  440. positions[3] = position + Vector3(-halfWidth, halfHeight, 0.0f);
  441. Vector2 uvs[4];
  442. uvs[0] = curIconData.texture->transformUV(Vector2(0.0f, 0.0f));
  443. uvs[1] = curIconData.texture->transformUV(Vector2(1.0f, 0.0f));
  444. uvs[2] = curIconData.texture->transformUV(Vector2(1.0f, 1.0f));
  445. uvs[3] = curIconData.texture->transformUV(Vector2(0.0f, 1.0f));
  446. for (UINT32 j = 0; j < 4; j++)
  447. {
  448. positionIter.addValue(positions[j]);
  449. texcoordIter.addValue(uvs[j]);
  450. normalColorIter.addValue(normalColor.getAsRGBA());
  451. fadedColorIter.addValue(fadedColor.getAsRGBA());
  452. }
  453. UINT32 vertOffset = i * 4;
  454. indices[0] = vertOffset + 0;
  455. indices[1] = vertOffset + 1;
  456. indices[2] = vertOffset + 2;
  457. indices[3] = vertOffset + 0;
  458. indices[4] = vertOffset + 2;
  459. indices[5] = vertOffset + 3;
  460. indices += 6;
  461. }
  462. return mIconMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  463. }
  464. void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
  465. {
  466. if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
  467. return;
  468. float relWidth = OPTIMAL_ICON_SIZE / (float)width;
  469. float relHeight = OPTIMAL_ICON_SIZE / (float)height;
  470. float scale = std::min(relWidth, relHeight);
  471. width = Math::roundToInt(width * scale);
  472. height = Math::roundToInt(height * scale);
  473. }
  474. void GizmoManager::calculateIconColors(const Color& tint, const CameraHandlerPtr& camera,
  475. UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
  476. {
  477. normalColor = tint;
  478. if (!fixedScale)
  479. {
  480. float iconToScreenRatio = iconHeight / (float)camera->getViewport()->getHeight();
  481. if (iconToScreenRatio > 0.3f)
  482. {
  483. float alpha = 1.0f - Math::lerp01(iconToScreenRatio, 0.3f, 1.0f);
  484. normalColor.a *= alpha;
  485. }
  486. else if (iconToScreenRatio < 0.1f)
  487. {
  488. float alpha = Math::lerp01(iconToScreenRatio, 0.0f, 0.1f);
  489. normalColor.a *= alpha;
  490. }
  491. }
  492. fadedColor = normalColor;
  493. fadedColor.a *= 0.2f;
  494. }
  495. HSceneObject GizmoManager::getSceneObject(UINT32 gizmoIdx)
  496. {
  497. auto iterFind = mIdxToSceneObjectMap.find(gizmoIdx);
  498. if (iterFind != mIdxToSceneObjectMap.end())
  499. return iterFind->second;
  500. return HSceneObject();
  501. }
  502. const float GizmoManagerCore::PICKING_ALPHA_CUTOFF = 0.5f;
  503. GizmoManagerCore::GizmoManagerCore(const PrivatelyConstuct& dummy)
  504. {
  505. }
  506. void GizmoManagerCore::initialize(const GizmoManager::CoreInitData& initData)
  507. {
  508. THROW_IF_NOT_CORE_THREAD;
  509. mSolidMaterial.proxy = initData.solidMatProxy;
  510. mWireMaterial.proxy = initData.wireMatProxy;
  511. mIconMaterial.proxy = initData.iconMatProxy;
  512. mPickingMaterial.proxy = initData.pickingMatProxy;
  513. mAlphaPickingMaterial.proxy = initData.alphaPickingMatProxy;
  514. // TODO - Make a better interface when dealing with parameters through proxies?
  515. {
  516. MaterialProxyPtr proxy = mWireMaterial.proxy;
  517. GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  518. vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
  519. }
  520. {
  521. MaterialProxyPtr proxy = mSolidMaterial.proxy;
  522. GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  523. vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
  524. }
  525. {
  526. MaterialProxyPtr proxy = mIconMaterial.proxy;
  527. GpuParamsPtr vertParams0 = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  528. GpuParamsPtr vertParams1 = proxy->params[proxy->passes[1].vertexProgParamsIdx];
  529. vertParams0->getParam("matViewProj", mIconMaterial.mViewProj[0]);
  530. vertParams1->getParam("matViewProj", mIconMaterial.mViewProj[1]);
  531. mIconMaterial.mFragParams[0] = proxy->params[proxy->passes[0].fragmentProgParamsIdx];
  532. mIconMaterial.mFragParams[1] = proxy->params[proxy->passes[1].fragmentProgParamsIdx];
  533. mIconMaterial.mFragParams[0]->getTextureParam("mainTexture", mIconMaterial.mTexture[0]);
  534. mIconMaterial.mFragParams[1]->getTextureParam("mainTexture", mIconMaterial.mTexture[1]);
  535. }
  536. {
  537. MaterialProxyPtr proxy = mPickingMaterial.proxy;
  538. GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  539. vertParams->getParam("matViewProj", mPickingMaterial.mViewProj);
  540. }
  541. {
  542. MaterialProxyPtr proxy = mAlphaPickingMaterial.proxy;
  543. GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  544. vertParams->getParam("matViewProj", mAlphaPickingMaterial.mViewProj);
  545. mAlphaPickingMaterial.mFragParams = proxy->params[proxy->passes[0].fragmentProgParamsIdx];
  546. mAlphaPickingMaterial.mFragParams->getTextureParam("mainTexture", mAlphaPickingMaterial.mTexture);
  547. GpuParamFloat alphaCutoffParam;
  548. mAlphaPickingMaterial.mFragParams->getParam("alphaCutoff", alphaCutoffParam);
  549. alphaCutoffParam.set(PICKING_ALPHA_CUTOFF);
  550. }
  551. RendererPtr activeRenderer = RendererManager::instance().getActive();
  552. activeRenderer->onCorePostRenderViewport.connect(std::bind(&GizmoManagerCore::render, this, _1));
  553. }
  554. void GizmoManagerCore::updateData(const RenderTargetPtr& rt, const MeshProxyPtr& solidMeshProxy, const MeshProxyPtr& wireMeshProxy,
  555. const MeshProxyPtr& iconMeshProxy, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
  556. {
  557. mSceneRenderTarget = rt;
  558. mSolidMeshProxy = solidMeshProxy;
  559. mWireMeshProxy = wireMeshProxy;
  560. mIconMeshProxy = iconMeshProxy;
  561. mIconRenderData = iconRenderData;
  562. }
  563. void GizmoManagerCore::render(const CameraProxy& camera)
  564. {
  565. if (camera.viewport.getTarget() != mSceneRenderTarget)
  566. return;
  567. float width = (float)mSceneRenderTarget->getCore()->getProperties().getWidth();
  568. float height = (float)mSceneRenderTarget->getCore()->getProperties().getHeight();
  569. Rect2 normArea = camera.viewport.getNormArea();
  570. Rect2I screenArea;
  571. screenArea.x = (int)(normArea.x * width);
  572. screenArea.y = (int)(normArea.y * height);
  573. screenArea.width = (int)(normArea.width * width);
  574. screenArea.height = (int)(normArea.height * height);
  575. if (mSolidMeshProxy != nullptr)
  576. renderGizmos(camera.viewMatrix, camera.projMatrix, mSolidMeshProxy, GizmoManager::GizmoMaterial::Solid);
  577. if (mWireMeshProxy != nullptr)
  578. renderGizmos(camera.viewMatrix, camera.projMatrix, mWireMeshProxy, GizmoManager::GizmoMaterial::Wire);
  579. if (mIconMeshProxy != nullptr)
  580. renderIconGizmos(screenArea, mIconMeshProxy, mIconRenderData, false);
  581. }
  582. void GizmoManagerCore::renderGizmos(Matrix4 viewMatrix, Matrix4 projMatrix, MeshProxyPtr meshProxy, GizmoManager::GizmoMaterial material)
  583. {
  584. THROW_IF_NOT_CORE_THREAD;
  585. Matrix4 viewProjMat = projMatrix * viewMatrix;
  586. switch (material)
  587. {
  588. case GizmoManager::GizmoMaterial::Solid:
  589. mSolidMaterial.mViewProj.set(viewProjMat);
  590. Renderer::setPass(*mSolidMaterial.proxy, 0);
  591. break;
  592. case GizmoManager::GizmoMaterial::Wire:
  593. mWireMaterial.mViewProj.set(viewProjMat);
  594. Renderer::setPass(*mWireMaterial.proxy, 0);
  595. break;
  596. case GizmoManager::GizmoMaterial::Picking:
  597. mPickingMaterial.mViewProj.set(viewProjMat);
  598. Renderer::setPass(*mPickingMaterial.proxy, 0);
  599. break;
  600. }
  601. Renderer::draw(*meshProxy);
  602. }
  603. void GizmoManagerCore::renderIconGizmos(Rect2I screenArea, MeshProxyPtr meshProxy, GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
  604. {
  605. RenderSystem& rs = RenderSystem::instance();
  606. MeshBasePtr mesh;
  607. // TODO: Instead of this lock consider just storing all needed data in MeshProxy and not referencing Mesh at all?
  608. if (!meshProxy->mesh.expired())
  609. mesh = meshProxy->mesh.lock();
  610. else
  611. return;
  612. std::shared_ptr<VertexData> vertexData = mesh->_getVertexData();
  613. rs.setVertexDeclaration(vertexData->vertexDeclaration);
  614. auto vertexBuffers = vertexData->getBuffers();
  615. SPtr<VertexBufferCore> vertBuffers[1] = { vertexBuffers.begin()->second };
  616. rs.setVertexBuffers(0, vertBuffers, 1);
  617. SPtr<IndexBufferCore> indexBuffer = mesh->_getIndexBuffer();
  618. rs.setIndexBuffer(indexBuffer);
  619. rs.setDrawOperation(DOT_TRIANGLE_LIST);
  620. // Set up ortho matrix
  621. Matrix4 projMat;
  622. float left = screenArea.x + rs.getHorizontalTexelOffset();
  623. float right = screenArea.x + screenArea.width + rs.getHorizontalTexelOffset();
  624. float top = screenArea.y + rs.getVerticalTexelOffset();
  625. float bottom = screenArea.y + screenArea.height + rs.getVerticalTexelOffset();
  626. float near = rs.getMinimumDepthInputValue();
  627. float far = rs.getMaximumDepthInputValue();
  628. // Top/bottom have been swapped because we're moving from window coordinates (origin top left)
  629. // to normalized device coordinates (origin bottom left)
  630. // Negative near/far because Z is flipped for normalized device coordinates
  631. // (positive Z goes into screen as opposed to view space here we're looking along negative Z)
  632. projMat.makeProjectionOrtho(left, right, top, bottom, -near, -far);
  633. if (!usePickingMaterial)
  634. {
  635. mIconMaterial.mViewProj[0].set(projMat);
  636. mIconMaterial.mViewProj[1].set(projMat);
  637. for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
  638. {
  639. Renderer::setPass(*mIconMaterial.proxy, passIdx);
  640. UINT32 curIndexOffset = mesh->_getIndexOffset();
  641. for (auto curRenderData : *renderData)
  642. {
  643. mIconMaterial.mTexture[passIdx].set(curRenderData.texture);
  644. rs.bindGpuParams(GPT_FRAGMENT_PROGRAM, mIconMaterial.mFragParams[passIdx]);
  645. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->_getVertexOffset(), curRenderData.count * 4);
  646. curIndexOffset += curRenderData.count * 6;
  647. }
  648. }
  649. }
  650. else
  651. {
  652. mAlphaPickingMaterial.mViewProj.set(projMat);
  653. Renderer::setPass(*mAlphaPickingMaterial.proxy, 0);
  654. UINT32 curIndexOffset = 0;
  655. for (auto curRenderData : *renderData)
  656. {
  657. mAlphaPickingMaterial.mTexture.set(curRenderData.texture);
  658. rs.bindGpuParams(GPT_FRAGMENT_PROGRAM, mAlphaPickingMaterial.mFragParams);
  659. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->_getVertexOffset(), curRenderData.count * 4);
  660. curIndexOffset += curRenderData.count * 6;
  661. }
  662. }
  663. mesh->_notifyUsedOnGPU();
  664. }
  665. }