BsGizmoManager.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. normalColor = curIconData.color;
  420. Vector3 positions[4];
  421. positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
  422. positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
  423. positions[2] = position + Vector3(-halfWidth, halfHeight, 0.0f);
  424. positions[3] = position + Vector3(halfWidth, halfHeight, 0.0f);
  425. Vector2 uvs[4];
  426. uvs[0] = curIconData.texture->transformUV(Vector2(0.0f, 0.0f));
  427. uvs[1] = curIconData.texture->transformUV(Vector2(1.0f, 0.0f));
  428. uvs[2] = curIconData.texture->transformUV(Vector2(0.0f, 1.0f));
  429. uvs[3] = curIconData.texture->transformUV(Vector2(1.0f, 1.0f));
  430. for (UINT32 j = 0; j < 4; j++)
  431. {
  432. positionIter.addValue(positions[j]);
  433. texcoordIter.addValue(uvs[j]);
  434. normalColorIter.addValue(normalColor.getAsRGBA());
  435. fadedColorIter.addValue(fadedColor.getAsRGBA());
  436. }
  437. UINT32 vertOffset = i * 4;
  438. indices[0] = vertOffset + 0;
  439. indices[1] = vertOffset + 1;
  440. indices[2] = vertOffset + 2;
  441. indices[3] = vertOffset + 1;
  442. indices[4] = vertOffset + 3;
  443. indices[5] = vertOffset + 2;
  444. indices += 6;
  445. }
  446. return mIconMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  447. }
  448. void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
  449. {
  450. if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
  451. return;
  452. float relWidth = OPTIMAL_ICON_SIZE / (float)width;
  453. float relHeight = OPTIMAL_ICON_SIZE / (float)height;
  454. float scale = std::min(relWidth, relHeight);
  455. width = Math::roundToInt(width * scale);
  456. height = Math::roundToInt(height * scale);
  457. }
  458. void GizmoManager::calculateIconColors(const Color& tint, const Camera& camera,
  459. UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
  460. {
  461. normalColor = tint;
  462. if (!fixedScale)
  463. {
  464. float iconToScreenRatio = iconHeight / (float)camera.getViewport()->getHeight();
  465. if (iconToScreenRatio > 0.3f)
  466. {
  467. float alpha = 1.0f - Math::lerp01(iconToScreenRatio, 0.3f, 1.0f);
  468. normalColor.a *= alpha;
  469. }
  470. else if (iconToScreenRatio < 0.1f)
  471. {
  472. float alpha = Math::lerp01(iconToScreenRatio, 0.0f, 0.1f);
  473. normalColor.a *= alpha;
  474. }
  475. }
  476. fadedColor = normalColor;
  477. fadedColor.a *= 0.2f;
  478. }
  479. HSceneObject GizmoManager::getSceneObject(UINT32 gizmoIdx)
  480. {
  481. auto iterFind = mIdxToSceneObjectMap.find(gizmoIdx);
  482. if (iterFind != mIdxToSceneObjectMap.end())
  483. return iterFind->second;
  484. return HSceneObject();
  485. }
  486. const float GizmoManagerCore::PICKING_ALPHA_CUTOFF = 0.5f;
  487. GizmoManagerCore::GizmoManagerCore(const PrivatelyConstuct& dummy)
  488. {
  489. }
  490. void GizmoManagerCore::initialize(const GizmoManager::CoreInitData& initData)
  491. {
  492. THROW_IF_NOT_CORE_THREAD;
  493. mSolidMaterial.proxy = initData.solidMatProxy;
  494. mWireMaterial.proxy = initData.wireMatProxy;
  495. mIconMaterial.proxy = initData.iconMatProxy;
  496. mPickingMaterial.proxy = initData.pickingMatProxy;
  497. mAlphaPickingMaterial.proxy = initData.alphaPickingMatProxy;
  498. // TODO - Make a better interface when dealing with parameters through proxies?
  499. {
  500. MaterialProxyPtr proxy = mWireMaterial.proxy;
  501. GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  502. vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
  503. }
  504. {
  505. MaterialProxyPtr proxy = mSolidMaterial.proxy;
  506. GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  507. vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
  508. }
  509. {
  510. MaterialProxyPtr proxy = mIconMaterial.proxy;
  511. GpuParamsPtr vertParams0 = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  512. GpuParamsPtr vertParams1 = proxy->params[proxy->passes[1].vertexProgParamsIdx];
  513. vertParams0->getParam("matViewProj", mIconMaterial.mViewProj[0]);
  514. vertParams1->getParam("matViewProj", mIconMaterial.mViewProj[1]);
  515. mIconMaterial.mFragParams[0] = proxy->params[proxy->passes[0].fragmentProgParamsIdx];
  516. mIconMaterial.mFragParams[1] = proxy->params[proxy->passes[1].fragmentProgParamsIdx];
  517. mIconMaterial.mFragParams[0]->getTextureParam("mainTexture", mIconMaterial.mTexture[0]);
  518. mIconMaterial.mFragParams[1]->getTextureParam("mainTexture", mIconMaterial.mTexture[1]);
  519. }
  520. {
  521. MaterialProxyPtr proxy = mPickingMaterial.proxy;
  522. GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  523. vertParams->getParam("matViewProj", mPickingMaterial.mViewProj);
  524. }
  525. {
  526. MaterialProxyPtr proxy = mAlphaPickingMaterial.proxy;
  527. GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  528. vertParams->getParam("matViewProj", mAlphaPickingMaterial.mViewProj);
  529. mAlphaPickingMaterial.mFragParams = proxy->params[proxy->passes[0].fragmentProgParamsIdx];
  530. mAlphaPickingMaterial.mFragParams->getTextureParam("mainTexture", mAlphaPickingMaterial.mTexture);
  531. GpuParamFloat alphaCutoffParam;
  532. mAlphaPickingMaterial.mFragParams->getParam("alphaCutoff", alphaCutoffParam);
  533. alphaCutoffParam.set(PICKING_ALPHA_CUTOFF);
  534. }
  535. RendererPtr activeRenderer = RendererManager::instance().getActive();
  536. activeRenderer->onCorePostRenderViewport.connect(std::bind(&GizmoManagerCore::render, this, _1));
  537. }
  538. void GizmoManagerCore::updateData(const RenderTargetPtr& rt, const MeshProxyPtr& solidMeshProxy, const MeshProxyPtr& wireMeshProxy,
  539. const MeshProxyPtr& iconMeshProxy, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
  540. {
  541. mSceneRenderTarget = rt;
  542. mSolidMeshProxy = solidMeshProxy;
  543. mWireMeshProxy = wireMeshProxy;
  544. mIconMeshProxy = iconMeshProxy;
  545. mIconRenderData = iconRenderData;
  546. }
  547. void GizmoManagerCore::render(const CameraProxy& camera)
  548. {
  549. if (camera.viewport.getTarget() != mSceneRenderTarget)
  550. return;
  551. float width = (float)mSceneRenderTarget->getCore()->getProperties().getWidth();
  552. float height = (float)mSceneRenderTarget->getCore()->getProperties().getHeight();
  553. Rect2 normArea = camera.viewport.getNormArea();
  554. Rect2I screenArea;
  555. screenArea.x = (int)(normArea.x * width);
  556. screenArea.y = (int)(normArea.y * height);
  557. screenArea.width = (int)(normArea.width * width);
  558. screenArea.height = (int)(normArea.height * height);
  559. if (mSolidMeshProxy != nullptr)
  560. renderGizmos(camera.viewMatrix, camera.projMatrix, mSolidMeshProxy, GizmoManager::GizmoMaterial::Solid);
  561. if (mWireMeshProxy != nullptr)
  562. renderGizmos(camera.viewMatrix, camera.projMatrix, mWireMeshProxy, GizmoManager::GizmoMaterial::Wire);
  563. if (mIconMeshProxy != nullptr)
  564. renderIconGizmos(screenArea, mIconMeshProxy, mIconRenderData, false);
  565. }
  566. void GizmoManagerCore::renderGizmos(Matrix4 viewMatrix, Matrix4 projMatrix, MeshProxyPtr meshProxy, GizmoManager::GizmoMaterial material)
  567. {
  568. THROW_IF_NOT_CORE_THREAD;
  569. Matrix4 viewProjMat = projMatrix * viewMatrix;
  570. switch (material)
  571. {
  572. case GizmoManager::GizmoMaterial::Solid:
  573. mSolidMaterial.mViewProj.set(viewProjMat);
  574. Renderer::setPass(*mSolidMaterial.proxy, 0);
  575. break;
  576. case GizmoManager::GizmoMaterial::Wire:
  577. mWireMaterial.mViewProj.set(viewProjMat);
  578. Renderer::setPass(*mWireMaterial.proxy, 0);
  579. break;
  580. case GizmoManager::GizmoMaterial::Picking:
  581. mPickingMaterial.mViewProj.set(viewProjMat);
  582. Renderer::setPass(*mPickingMaterial.proxy, 0);
  583. break;
  584. }
  585. Renderer::draw(*meshProxy);
  586. }
  587. void GizmoManagerCore::renderIconGizmos(Rect2I screenArea, MeshProxyPtr meshProxy, GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
  588. {
  589. RenderSystem& rs = RenderSystem::instance();
  590. MeshBasePtr mesh;
  591. // TODO: Instead of this lock consider just storing all needed data in MeshProxy and not referencing Mesh at all?
  592. if (!meshProxy->mesh.expired())
  593. mesh = meshProxy->mesh.lock();
  594. else
  595. return;
  596. std::shared_ptr<VertexData> vertexData = mesh->_getVertexData();
  597. rs.setVertexDeclaration(vertexData->vertexDeclaration);
  598. auto vertexBuffers = vertexData->getBuffers();
  599. VertexBufferPtr vertBuffers[1] = { vertexBuffers.begin()->second };
  600. rs.setVertexBuffers(0, vertBuffers, 1);
  601. IndexBufferPtr indexBuffer = mesh->_getIndexBuffer();
  602. rs.setIndexBuffer(indexBuffer);
  603. rs.setDrawOperation(DOT_TRIANGLE_LIST);
  604. // Set up ortho matrix
  605. Matrix4 projMat;
  606. float left = screenArea.x + rs.getHorizontalTexelOffset();
  607. float right = screenArea.x + screenArea.width + rs.getHorizontalTexelOffset();
  608. float top = screenArea.y + rs.getVerticalTexelOffset();
  609. float bottom = screenArea.y + screenArea.height + rs.getVerticalTexelOffset();
  610. float near = rs.getMinimumDepthInputValue();
  611. float far = rs.getMaximumDepthInputValue();
  612. projMat.makeProjectionOrtho(left, right, top, bottom, -near, -far); // Minus near/far because Z is flipped for normalized device coords
  613. if (!usePickingMaterial)
  614. {
  615. mIconMaterial.mViewProj[0].set(projMat);
  616. mIconMaterial.mViewProj[1].set(projMat);
  617. for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
  618. {
  619. Renderer::setPass(*mIconMaterial.proxy, passIdx);
  620. UINT32 curIndexOffset = 0;
  621. for (auto curRenderData : *renderData)
  622. {
  623. mIconMaterial.mTexture[passIdx].set(curRenderData.texture);
  624. rs.bindGpuParams(GPT_FRAGMENT_PROGRAM, mIconMaterial.mFragParams[passIdx]);
  625. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, 0, curRenderData.count * 4);
  626. curIndexOffset += curRenderData.count * 6;
  627. }
  628. }
  629. }
  630. else
  631. {
  632. mAlphaPickingMaterial.mViewProj.set(projMat);
  633. Renderer::setPass(*mAlphaPickingMaterial.proxy, 0);
  634. UINT32 curIndexOffset = 0;
  635. for (auto curRenderData : *renderData)
  636. {
  637. mAlphaPickingMaterial.mTexture.set(curRenderData.texture);
  638. rs.bindGpuParams(GPT_FRAGMENT_PROGRAM, mAlphaPickingMaterial.mFragParams);
  639. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, 0, curRenderData.count * 4);
  640. curIndexOffset += curRenderData.count * 6;
  641. }
  642. }
  643. mesh->_notifyUsedOnGPU();
  644. }
  645. }