BsGizmoManager.cpp 26 KB

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