BsGizmoManager.cpp 26 KB

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