BsGizmoManager.cpp 29 KB

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