BsGizmoManager.cpp 24 KB

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