BsGizmoManager.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. #include "BsGizmoManager.h"
  2. #include "BsMesh.h"
  3. #include "BsAABox.h"
  4. #include "BsSphere.h"
  5. #include "BsVertexDataDesc.h"
  6. #include "BsDrawHelper3D.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. namespace BansheeEngine
  18. {
  19. const UINT32 GizmoManager::VERTEX_BUFFER_GROWTH = 4096;
  20. const UINT32 GizmoManager::INDEX_BUFFER_GROWTH = 4096 * 2;
  21. const UINT32 GizmoManager::SPHERE_QUALITY = 1;
  22. const float GizmoManager::MAX_ICON_RANGE = 500.0f;
  23. const UINT32 GizmoManager::OPTIMAL_ICON_SIZE = 64;
  24. const float GizmoManager::ICON_TEXEL_WORLD_SIZE = 0.05f;
  25. GizmoManager::GizmoManager(const HCamera& camera)
  26. :mTotalRequiredSolidIndices(0), mTotalRequiredSolidVertices(0),
  27. mTotalRequiredWireVertices(0), mTotalRequiredWireIndices(0), mCamera(camera), mPickable(false)
  28. {
  29. mSolidVertexDesc = bs_shared_ptr<VertexDataDesc>();
  30. mSolidVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  31. mSolidVertexDesc->addVertElem(VET_FLOAT3, VES_NORMAL);
  32. mSolidVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  33. mWireVertexDesc = bs_shared_ptr<VertexDataDesc>();
  34. mWireVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  35. mWireVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  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. mSolidMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mSolidVertexDesc);
  42. mWireMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mWireVertexDesc);
  43. mIconMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mIconVertexDesc);
  44. mSolidMaterial.material = BuiltinEditorResources::instance().createSolidGizmoMat();
  45. mWireMaterial.material = BuiltinEditorResources::instance().createWireGizmoMat();
  46. mIconMaterial.material = BuiltinEditorResources::instance().createIconGizmoMat();
  47. mSolidMaterial.proxy = mSolidMaterial.material->_createProxy();
  48. mWireMaterial.proxy = mWireMaterial.material->_createProxy();
  49. mIconMaterial.proxy = mIconMaterial.material->_createProxy();
  50. gCoreAccessor().queueCommand(std::bind(&GizmoManager::initializeCore, this));
  51. }
  52. GizmoManager::~GizmoManager()
  53. {
  54. if (mSolidMesh != nullptr)
  55. mSolidMeshHeap->dealloc(mSolidMesh);
  56. if (mWireMesh != nullptr)
  57. mWireMeshHeap->dealloc(mWireMesh);
  58. if (mIconMesh != nullptr)
  59. mIconMeshHeap->dealloc(mIconMesh);
  60. }
  61. void GizmoManager::initializeCore()
  62. {
  63. THROW_IF_NOT_CORE_THREAD;
  64. // TODO - Make a better interface when dealing with parameters through proxies?
  65. {
  66. MaterialProxyPtr proxy = mWireMaterial.proxy;
  67. GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  68. vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
  69. }
  70. {
  71. MaterialProxyPtr proxy = mSolidMaterial.proxy;
  72. GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  73. vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
  74. vertParams->getParam("matViewIT", mSolidMaterial.mViewIT);
  75. }
  76. {
  77. MaterialProxyPtr proxy = mIconMaterial.proxy;
  78. GpuParamsPtr vertParams = proxy->params[proxy->passes[0].vertexProgParamsIdx];
  79. vertParams->getParam("matViewProj", mIconMaterial.mViewProj);
  80. GpuParamsPtr fragParams = proxy->params[proxy->passes[0].fragmentProgParamsIdx];
  81. fragParams->getTextureParam("mainTexture", mIconMaterial.mTexture);
  82. }
  83. }
  84. void GizmoManager::startGizmo(const HSceneObject& gizmoParent)
  85. {
  86. mActiveSO = gizmoParent;
  87. }
  88. void GizmoManager::endGizmo()
  89. {
  90. mActiveSO = nullptr;
  91. }
  92. void GizmoManager::setColor(const Color& color)
  93. {
  94. mColor = color;
  95. }
  96. void GizmoManager::setTransform(const Matrix4& transform)
  97. {
  98. mTransform = transform;
  99. }
  100. void GizmoManager::drawCube(const Vector3& position, const Vector3& extents)
  101. {
  102. mSolidCubeData.push_back(CubeData());
  103. CubeData& cubeData = mSolidCubeData.back();
  104. cubeData.position = position;
  105. cubeData.extents = extents;
  106. cubeData.color = mColor;
  107. cubeData.transform = mTransform;
  108. cubeData.sceneObject = mActiveSO;
  109. cubeData.pickable = mPickable;
  110. mTotalRequiredSolidVertices += 24;
  111. mTotalRequiredSolidIndices += 36;
  112. }
  113. void GizmoManager::drawSphere(const Vector3& position, float radius)
  114. {
  115. mSolidSphereData.push_back(SphereData());
  116. SphereData& sphereData = mSolidSphereData.back();
  117. sphereData.position = position;
  118. sphereData.radius = radius;
  119. sphereData.color = mColor;
  120. sphereData.transform = mTransform;
  121. sphereData.sceneObject = mActiveSO;
  122. sphereData.pickable = mPickable;
  123. UINT32 numVertices, numIndices;
  124. DrawHelper3D::getNumElementsSphere(SPHERE_QUALITY, numVertices, numIndices);
  125. mTotalRequiredSolidVertices += numVertices;
  126. mTotalRequiredSolidIndices += numIndices;
  127. }
  128. void GizmoManager::drawWireCube(const Vector3& position, const Vector3& extents)
  129. {
  130. mWireCubeData.push_back(CubeData());
  131. CubeData& cubeData = mWireCubeData.back();
  132. cubeData.position = position;
  133. cubeData.extents = extents;
  134. cubeData.color = mColor;
  135. cubeData.transform = mTransform;
  136. cubeData.sceneObject = mActiveSO;
  137. cubeData.pickable = mPickable;
  138. mTotalRequiredWireVertices += 8;
  139. mTotalRequiredWireIndices += 24;
  140. }
  141. void GizmoManager::drawWireSphere(const Vector3& position, float radius)
  142. {
  143. mWireSphereData.push_back(SphereData());
  144. SphereData& sphereData = mWireSphereData.back();
  145. sphereData.position = position;
  146. sphereData.radius = radius;
  147. sphereData.color = mColor;
  148. sphereData.transform = mTransform;
  149. sphereData.sceneObject = mActiveSO;
  150. sphereData.pickable = mPickable;
  151. UINT32 numVertices, numIndices;
  152. DrawHelper3D::getNumElementsWireSphere(SPHERE_QUALITY, numVertices, numIndices);
  153. mTotalRequiredWireVertices += numVertices;
  154. mTotalRequiredWireIndices += numIndices;
  155. }
  156. void GizmoManager::drawLine(const Vector3& start, const Vector3& end)
  157. {
  158. mLineData.push_back(LineData());
  159. LineData& lineData = mLineData.back();
  160. lineData.start = start;
  161. lineData.end = end;
  162. lineData.color = mColor;
  163. lineData.transform = mTransform;
  164. lineData.sceneObject = mActiveSO;
  165. lineData.pickable = mPickable;
  166. mTotalRequiredWireVertices += 2;
  167. mTotalRequiredWireIndices += 2;
  168. }
  169. void GizmoManager::drawFrustum(const Vector3& position, float aspect, Degree FOV, float near, float far)
  170. {
  171. mFrustumData.push_back(FrustumData());
  172. FrustumData& frustumData = mFrustumData.back();
  173. frustumData.position = position;
  174. frustumData.aspect = aspect;
  175. frustumData.FOV = FOV;
  176. frustumData.near = near;
  177. frustumData.far = far;
  178. frustumData.color = mColor;
  179. frustumData.transform = mTransform;
  180. frustumData.sceneObject = mActiveSO;
  181. frustumData.pickable = mPickable;
  182. mTotalRequiredWireVertices += 8;
  183. mTotalRequiredWireIndices += 36;
  184. }
  185. void GizmoManager::drawIcon(Vector3 position, HSpriteTexture image, bool fixedScale)
  186. {
  187. mIconData.push_back(IconData());
  188. IconData& iconData = mIconData.back();
  189. iconData.position = position;
  190. iconData.texture = image;
  191. iconData.fixedScale = fixedScale;
  192. iconData.color = mColor;
  193. iconData.transform = mTransform;
  194. iconData.sceneObject = mActiveSO;
  195. iconData.pickable = mPickable;
  196. }
  197. void GizmoManager::update()
  198. {
  199. buildSolidMesh();
  200. buildWireMesh();
  201. IconRenderDataVecPtr iconRenderData = buildIconMesh();
  202. // TODO - This must be rendered while Scene view is being rendered
  203. Matrix4 viewMat = mCamera->getViewMatrix();
  204. Matrix4 projMat = mCamera->getProjectionMatrix();
  205. ViewportPtr viewport = mCamera->getViewport();
  206. gCoreAccessor().queueCommand(std::bind(&GizmoManager::coreRenderSolidGizmos,
  207. this, viewMat, projMat, mSolidMesh->_createProxy(0)));
  208. gCoreAccessor().queueCommand(std::bind(&GizmoManager::coreRenderWireGizmos,
  209. this, viewMat, projMat, mWireMesh->_createProxy(0)));
  210. RectI screenArea = mCamera->getViewport()->getArea();
  211. gCoreAccessor().queueCommand(std::bind(&GizmoManager::coreRenderIconGizmos,
  212. this, screenArea, mIconMesh->_createProxy(0), iconRenderData));
  213. }
  214. void GizmoManager::clearGizmos()
  215. {
  216. mSolidCubeData.clear();
  217. mWireCubeData.clear();
  218. mSolidSphereData.clear();
  219. mWireSphereData.clear();
  220. mLineData.clear();
  221. mFrustumData.clear();
  222. mIconData.clear();
  223. mTotalRequiredSolidVertices = 0;
  224. mTotalRequiredSolidIndices = 0;
  225. mTotalRequiredWireVertices = 0;
  226. mTotalRequiredWireIndices = 0;
  227. }
  228. void GizmoManager::buildSolidMesh()
  229. {
  230. MeshDataPtr meshData = bs_shared_ptr<MeshData>(mTotalRequiredSolidVertices, mTotalRequiredSolidIndices, mSolidVertexDesc);
  231. UINT32 curVertexOffset = 0;
  232. UINT32 curIndexOffet = 0;
  233. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  234. auto normalIter = meshData->getVec3DataIter(VES_NORMAL);
  235. auto colorIter = meshData->getDWORDDataIter(VES_COLOR);
  236. for (auto& cubeData : mSolidCubeData)
  237. {
  238. AABox box(cubeData.position - cubeData.extents, cubeData.position + cubeData.extents);
  239. DrawHelper3D::instance().solidAABox(box, meshData, curVertexOffset, curIndexOffet); // TODO - These should be static methods
  240. Matrix4 transformIT = cubeData.transform.inverseAffine().transpose();
  241. RGBA color = cubeData.color.getAsRGBA();
  242. UINT32 numVertices = 24;
  243. for (UINT32 i = 0; i < numVertices; i++)
  244. {
  245. Vector3 worldPos = cubeData.transform.multiply3x4(positionIter.getValue());
  246. Vector3 worldNormal = transformIT.multiply3x4(normalIter.getValue());
  247. positionIter.addValue(worldPos);
  248. normalIter.addValue(worldNormal);
  249. colorIter.addValue(color);
  250. }
  251. curVertexOffset += numVertices;
  252. curIndexOffet += 36;
  253. }
  254. UINT32 numSphereVertices, numSphereIndices;
  255. DrawHelper3D::getNumElementsSphere(SPHERE_QUALITY, numSphereVertices, numSphereIndices);
  256. for (auto& sphereData : mSolidSphereData)
  257. {
  258. Sphere sphere(sphereData.position, sphereData.radius);
  259. DrawHelper3D::instance().solidSphere(sphere, meshData, curVertexOffset, curIndexOffet, SPHERE_QUALITY);
  260. Matrix4 transformIT = sphereData.transform.inverseAffine().transpose();
  261. RGBA color = sphereData.color.getAsRGBA();
  262. for (UINT32 i = 0; i < numSphereVertices; i++)
  263. {
  264. Vector3 worldPos = sphereData.transform.multiply3x4(positionIter.getValue());
  265. Vector3 worldNormal = transformIT.multiply3x4(normalIter.getValue());
  266. positionIter.addValue(worldPos);
  267. normalIter.addValue(worldNormal);
  268. colorIter.addValue(color);
  269. }
  270. curVertexOffset += numSphereVertices;
  271. curIndexOffet += numSphereIndices;
  272. }
  273. if (mSolidMesh != nullptr)
  274. mSolidMeshHeap->dealloc(mSolidMesh);
  275. mSolidMesh = mSolidMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  276. }
  277. void GizmoManager::buildWireMesh()
  278. {
  279. MeshDataPtr meshData = bs_shared_ptr<MeshData>(mTotalRequiredWireVertices, mTotalRequiredWireIndices, mWireVertexDesc);
  280. UINT32 curVertexOffset = 0;
  281. UINT32 curIndexOffet = 0;
  282. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  283. auto colorIter = meshData->getDWORDDataIter(VES_COLOR);
  284. for (auto& cubeData : mWireCubeData)
  285. {
  286. AABox box(cubeData.position - cubeData.extents, cubeData.position + cubeData.extents);
  287. DrawHelper3D::instance().wireAABox(box, meshData, curVertexOffset, curIndexOffet); // TODO - These should be static methods
  288. RGBA color = cubeData.color.getAsRGBA();
  289. UINT32 numVertices = 8;
  290. for (UINT32 i = 0; i < numVertices; i++)
  291. {
  292. Vector3 worldPos = cubeData.transform.multiply3x4(positionIter.getValue());
  293. positionIter.addValue(worldPos);
  294. colorIter.addValue(color);
  295. }
  296. curVertexOffset += numVertices;
  297. curIndexOffet += 24;
  298. }
  299. UINT32 numSphereVertices, numSphereIndices;
  300. DrawHelper3D::getNumElementsWireSphere(SPHERE_QUALITY, numSphereVertices, numSphereIndices);
  301. for (auto& sphereData : mWireSphereData)
  302. {
  303. Sphere sphere(sphereData.position, sphereData.radius);
  304. DrawHelper3D::instance().wireSphere(sphere, meshData, curVertexOffset, curIndexOffet, SPHERE_QUALITY);
  305. RGBA color = sphereData.color.getAsRGBA();
  306. for (UINT32 i = 0; i < numSphereVertices; i++)
  307. {
  308. Vector3 worldPos = sphereData.transform.multiply3x4(positionIter.getValue());
  309. positionIter.addValue(worldPos);
  310. colorIter.addValue(color);
  311. }
  312. curVertexOffset += numSphereVertices;
  313. curIndexOffet += numSphereIndices;
  314. }
  315. for (auto& lineData : mLineData)
  316. {
  317. DrawHelper3D::instance().pixelLine(lineData.start, lineData.end, meshData, curVertexOffset, curIndexOffet);
  318. RGBA color = lineData.color.getAsRGBA();
  319. for (UINT32 i = 0; i < 2; i++)
  320. {
  321. Vector3 worldPos = lineData.transform.multiply3x4(positionIter.getValue());
  322. positionIter.addValue(worldPos);
  323. colorIter.addValue(color);
  324. }
  325. curVertexOffset += 2;
  326. curIndexOffet += 2;
  327. }
  328. for (auto& frustumData : mFrustumData)
  329. {
  330. DrawHelper3D::instance().wireFrustum(frustumData.aspect, frustumData.FOV, frustumData.near,
  331. frustumData.far, meshData, curVertexOffset, curIndexOffet);
  332. RGBA color = frustumData.color.getAsRGBA();
  333. for (UINT32 i = 0; i < 8; i++)
  334. {
  335. Vector3 worldPos = frustumData.transform.multiply3x4(positionIter.getValue());
  336. positionIter.addValue(worldPos);
  337. colorIter.addValue(color);
  338. }
  339. curVertexOffset += 8;
  340. curIndexOffet += 24;
  341. }
  342. if (mWireMesh != nullptr)
  343. mWireMeshHeap->dealloc(mWireMesh);
  344. mWireMesh = mWireMeshHeap->alloc(meshData, DOT_LINE_LIST);
  345. }
  346. GizmoManager::IconRenderDataVecPtr GizmoManager::buildIconMesh()
  347. {
  348. mSortedIconData.clear();
  349. if (mIconData.size() > mSortedIconData.size())
  350. mSortedIconData.resize(mIconData.size());
  351. UINT32 i = 0;
  352. for (auto& iconData : mIconData)
  353. {
  354. Vector3 viewPoint = mCamera->worldToViewPoint(iconData.position);
  355. float distance = -viewPoint.z;
  356. if (distance < mCamera->getNearClipDistance()) // Ignore behind clip plane
  357. continue;
  358. if (distance > MAX_ICON_RANGE) // Ignore too far away
  359. continue;
  360. if (!iconData.texture) // Ignore missing texture
  361. continue;
  362. SortedIconData& sortedIconData = mSortedIconData[i];
  363. sortedIconData.iconIdx = i;
  364. sortedIconData.distance = distance;
  365. sortedIconData.screenPosition = mCamera->viewToScreenPoint(viewPoint);
  366. i++;
  367. }
  368. UINT32 actualNumIcons = i;
  369. // Sort back to front first, then by texture
  370. std::sort(mSortedIconData.begin(), mSortedIconData.begin() + actualNumIcons,
  371. [&](const SortedIconData& a, const SortedIconData& b)
  372. {
  373. if (a.distance == b.distance)
  374. {
  375. HSpriteTexture texA = mIconData[a.iconIdx].texture->getTexture();
  376. HSpriteTexture texB = mIconData[b.iconIdx].texture->getTexture();
  377. if (texA == texB)
  378. return a.iconIdx < b.iconIdx;
  379. return texA->getInternalID() < texB->getInternalID();
  380. }
  381. else
  382. return a.distance > b.distance;
  383. });
  384. MeshDataPtr meshData = bs_shared_ptr<MeshData>(actualNumIcons * 4, actualNumIcons * 6, mIconVertexDesc);
  385. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  386. auto texcoordIter = meshData->getVec2DataIter(VES_TEXCOORD);
  387. auto normalColorIter = meshData->getDWORDDataIter(VES_COLOR, 0);
  388. auto fadedColorIter = meshData->getDWORDDataIter(VES_COLOR, 1);
  389. UINT32* indices = meshData->getIndices32();
  390. float cameraScale = 1.0f;
  391. if (mCamera->getProjectionType() == PT_ORTHOGRAPHIC)
  392. cameraScale = mCamera->getViewport()->getHeight() / mCamera->getOrthoWindowHeight();
  393. else
  394. {
  395. Radian vertFOV(Math::tan(mCamera->getHorzFOV() * 0.5f));
  396. cameraScale = (mCamera->getViewport()->getHeight() * 0.5f) / vertFOV.valueRadians();
  397. }
  398. IconRenderDataVecPtr iconRenderData = bs_shared_ptr<IconRenderDataVec>();
  399. UINT32 lastTextureIdx = 0;
  400. HTexture curTexture;
  401. // Note: This assumes the meshes will be rendered using the same camera
  402. // properties as when they are created
  403. for (i = 0; i < actualNumIcons; i++)
  404. {
  405. SortedIconData& sortedIconData = mSortedIconData[i];
  406. IconData& iconData = mIconData[sortedIconData.iconIdx];
  407. if (curTexture != iconData.texture)
  408. {
  409. UINT32 numIconsPerTexture = i - lastTextureIdx;
  410. if (numIconsPerTexture > 0)
  411. {
  412. iconRenderData->push_back(IconRenderData());
  413. IconRenderData& renderData = iconRenderData->back();
  414. renderData.count = numIconsPerTexture;
  415. renderData.texture = curTexture;
  416. }
  417. lastTextureIdx = i;
  418. curTexture = iconData.texture;
  419. }
  420. UINT32 iconWidth = iconData.texture->getWidth();
  421. UINT32 iconHeight = iconData.texture->getHeight();
  422. limitIconSize(iconWidth, iconHeight);
  423. Color normalColor, fadedColor;
  424. calculateIconColors(iconData.color, *mCamera.get(), iconHeight, iconData.fixedScale, normalColor, fadedColor);
  425. Vector3 position(sortedIconData.screenPosition.x, sortedIconData.screenPosition.y, sortedIconData.distance);
  426. // TODO - Does the depth need to be corrected since it was taken from a projective camera (probably)?
  427. float halfWidth = iconWidth * 0.5f;
  428. float halfHeight = iconHeight * 0.5f;
  429. if (!iconData.fixedScale)
  430. {
  431. float iconScale = 1.0f;
  432. if (mCamera->getProjectionType() == PT_ORTHOGRAPHIC)
  433. iconScale = cameraScale;
  434. else
  435. iconScale = cameraScale / sortedIconData.distance;
  436. halfWidth *= iconScale;
  437. halfHeight *= iconScale;
  438. }
  439. Vector3 positions[4];
  440. positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
  441. positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
  442. positions[2] = position + Vector3(-halfWidth, halfHeight, 0.0f);
  443. positions[3] = position + Vector3(halfWidth, halfHeight, 0.0f);
  444. Vector2 uvs[4];
  445. uvs[0] = iconData.texture->transformUV(Vector2(0.0f, 0.0f));
  446. uvs[1] = iconData.texture->transformUV(Vector2(1.0f, 0.0f));
  447. uvs[2] = iconData.texture->transformUV(Vector2(0.0f, 1.0f));
  448. uvs[3] = iconData.texture->transformUV(Vector2(1.0f, 1.0f));
  449. for (UINT32 j = 0; j < 4; j++)
  450. {
  451. positionIter.addValue(positions[j]);
  452. texcoordIter.addValue(uvs[j]);
  453. normalColorIter.addValue(normalColor.getAsRGBA());
  454. fadedColorIter.addValue(fadedColor.getAsRGBA());
  455. }
  456. UINT32 vertOffset = i * 4;
  457. indices[0] = vertOffset + 0;
  458. indices[1] = vertOffset + 1;
  459. indices[2] = vertOffset + 2;
  460. indices[3] = vertOffset + 1;
  461. indices[4] = vertOffset + 3;
  462. indices[5] = vertOffset + 2;
  463. indices += 6;
  464. }
  465. if (mIconMesh != nullptr)
  466. mIconMeshHeap->dealloc(mIconMesh);
  467. mIconMesh = mIconMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  468. return iconRenderData;
  469. }
  470. void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
  471. {
  472. if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
  473. return;
  474. float relWidth = OPTIMAL_ICON_SIZE / width;
  475. float relHeight = OPTIMAL_ICON_SIZE / height;
  476. float scale = std::min(relWidth, relHeight);
  477. width = Math::roundToInt(width * scale);
  478. height = Math::roundToInt(height * scale);
  479. }
  480. void GizmoManager::calculateIconColors(const Color& tint, const Camera& camera,
  481. UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
  482. {
  483. normalColor = tint;
  484. if (!fixedScale)
  485. {
  486. float iconToScreenRatio = iconHeight / (float)camera.getViewport()->getHeight();
  487. if (iconToScreenRatio > 0.3f)
  488. {
  489. float alpha = 1.0f - Math::lerp01(iconToScreenRatio, 0.3f, 1.0f);
  490. normalColor.a *= alpha;
  491. }
  492. else if (iconToScreenRatio < 0.1f)
  493. {
  494. float alpha = Math::lerp01(iconToScreenRatio, 0.0f, 0.1f);
  495. normalColor.a *= alpha;
  496. }
  497. }
  498. fadedColor = normalColor;
  499. fadedColor.a *= 0.2f;
  500. }
  501. void GizmoManager::coreRenderSolidGizmos(Matrix4 viewMatrix, Matrix4 projMatrix, MeshProxyPtr meshProxy)
  502. {
  503. THROW_IF_NOT_CORE_THREAD;
  504. Matrix4 viewProjMat = projMatrix * viewMatrix;
  505. Matrix4 viewIT = viewMatrix.inverse().transpose();
  506. mSolidMaterial.mViewProj.set(viewProjMat);
  507. mSolidMaterial.mViewIT.set(viewIT);
  508. Renderer::setPass(*mSolidMaterial.proxy, 0);
  509. Renderer::draw(*meshProxy);
  510. }
  511. void GizmoManager::coreRenderWireGizmos(Matrix4 viewMatrix, Matrix4 projMatrix, MeshProxyPtr meshProxy)
  512. {
  513. THROW_IF_NOT_CORE_THREAD;
  514. Matrix4 viewProjMat = projMatrix * viewMatrix;
  515. Matrix4 viewIT = viewMatrix.inverse().transpose();
  516. mWireMaterial.mViewProj.set(viewProjMat);
  517. Renderer::setPass(*mWireMaterial.proxy, 0);
  518. Renderer::draw(*meshProxy);
  519. }
  520. void GizmoManager::coreRenderIconGizmos(RectI screenArea, MeshProxyPtr meshProxy, IconRenderDataVecPtr renderData)
  521. {
  522. RenderSystem& rs = RenderSystem::instance();
  523. MeshBasePtr mesh;
  524. // TODO: Instead of this lock consider just storing all needed data in MeshProxy and not referencing Mesh at all?
  525. if (!meshProxy->mesh.expired())
  526. mesh = meshProxy->mesh.lock();
  527. else
  528. return;
  529. std::shared_ptr<VertexData> vertexData = mesh->_getVertexData();
  530. rs.setVertexDeclaration(vertexData->vertexDeclaration);
  531. auto vertexBuffers = vertexData->getBuffers();
  532. VertexBufferPtr vertBuffers[1] = { vertexBuffers.begin()->second };
  533. rs.setVertexBuffers(0, vertBuffers, 1);
  534. IndexBufferPtr indexBuffer = mesh->_getIndexBuffer();
  535. rs.setIndexBuffer(indexBuffer);
  536. rs.setDrawOperation(DOT_TRIANGLE_LIST);
  537. // Set up ortho matrix
  538. Matrix4 projMat;
  539. float left = screenArea.x + rs.getHorizontalTexelOffset();
  540. float right = screenArea.x + screenArea.width + rs.getHorizontalTexelOffset();
  541. float top = screenArea.y + rs.getVerticalTexelOffset();
  542. float bottom = screenArea.y + screenArea.height + rs.getVerticalTexelOffset();
  543. float near = rs.getMinimumDepthInputValue();
  544. float far = rs.getMaximumDepthInputValue();
  545. projMat.makeProjectionOrtho(left, right, top, bottom, near, far);
  546. mIconMaterial.mViewProj.set(projMat);
  547. for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
  548. {
  549. Renderer::setPass(*mIconMaterial.proxy, passIdx);
  550. UINT32 curIndexOffset = 0;
  551. for (auto curRenderData : *renderData)
  552. {
  553. mIconMaterial.mTexture.set(curRenderData.texture);
  554. rs.drawIndexed(curIndexOffset, curRenderData.count * 6, 0, curRenderData.count * 4);
  555. curIndexOffset += curRenderData.count * 6;
  556. }
  557. }
  558. mesh->_notifyUsedOnGPU();
  559. }
  560. }