BsGizmoManager.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGizmoManager.h"
  4. #include "BsMesh.h"
  5. #include "BsAABox.h"
  6. #include "BsSphere.h"
  7. #include "BsVertexDataDesc.h"
  8. #include "BsShapeMeshes3D.h"
  9. #include "BsMeshHeap.h"
  10. #include "BsCCamera.h"
  11. #include "BsSpriteTexture.h"
  12. #include "BsCoreThread.h"
  13. #include "BsBuiltinEditorResources.h"
  14. #include "BsMaterial.h"
  15. #include "BsGpuParams.h"
  16. #include "BsGpuParamsSet.h"
  17. #include "BsRenderAPI.h"
  18. #include "BsCoreRenderer.h"
  19. #include "BsRendererUtility.h"
  20. #include "BsTransientMesh.h"
  21. #include "BsRendererManager.h"
  22. #include "BsDrawHelper.h"
  23. using namespace std::placeholders;
  24. namespace bs
  25. {
  26. const UINT32 GizmoManager::VERTEX_BUFFER_GROWTH = 4096;
  27. const UINT32 GizmoManager::INDEX_BUFFER_GROWTH = 4096 * 2;
  28. const UINT32 GizmoManager::SPHERE_QUALITY = 1;
  29. const UINT32 GizmoManager::WIRE_SPHERE_QUALITY = 10;
  30. const float GizmoManager::MAX_ICON_RANGE = 500.0f;
  31. const UINT32 GizmoManager::OPTIMAL_ICON_SIZE = 64;
  32. const float GizmoManager::ICON_TEXEL_WORLD_SIZE = 0.05f;
  33. GizmoManager::GizmoManager()
  34. : mPickable(false), mCurrentIdx(0), mTransformDirty(false), mColorDirty(false), mDrawHelper(nullptr)
  35. , mPickingDrawHelper(nullptr), mCore(nullptr)
  36. {
  37. mTransform = Matrix4::IDENTITY;
  38. mDrawHelper = bs_new<DrawHelper>();
  39. mPickingDrawHelper = bs_new<DrawHelper>();
  40. mIconVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  41. mIconVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  42. mIconVertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD);
  43. mIconVertexDesc->addVertElem(VET_COLOR, VES_COLOR, 0);
  44. mIconVertexDesc->addVertElem(VET_COLOR, VES_COLOR, 1);
  45. mIconMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mIconVertexDesc);
  46. HMaterial solidMaterial = BuiltinEditorResources::instance().createSolidGizmoMat();
  47. HMaterial wireMaterial = BuiltinEditorResources::instance().createWireGizmoMat();
  48. HMaterial lineMaterial = BuiltinEditorResources::instance().createLineGizmoMat();
  49. HMaterial iconMaterial = BuiltinEditorResources::instance().createIconGizmoMat();
  50. HMaterial textMaterial = BuiltinEditorResources::instance().createTextGizmoMat();
  51. HMaterial pickingMaterial = BuiltinEditorResources::instance().createGizmoPickingMat();
  52. HMaterial alphaPickingMaterial = BuiltinEditorResources::instance().createAlphaGizmoPickingMat();
  53. CoreInitData initData;
  54. initData.solidMat = solidMaterial->getCore();
  55. initData.wireMat = wireMaterial->getCore();
  56. initData.lineMat = lineMaterial->getCore();
  57. initData.iconMat = iconMaterial->getCore();
  58. initData.textMat = textMaterial->getCore();
  59. initData.pickingMat = pickingMaterial->getCore();
  60. initData.alphaPickingMat = alphaPickingMaterial->getCore();
  61. mCore.store(bs_new<GizmoManagerCore>(GizmoManagerCore::PrivatelyConstuct()), std::memory_order_release);
  62. gCoreThread().queueCommand(std::bind(&GizmoManager::initializeCore, this, initData));
  63. }
  64. GizmoManager::~GizmoManager()
  65. {
  66. mDrawHelper->clearMeshes(mActiveMeshes);
  67. mActiveMeshes.clear();
  68. if (mIconMesh != nullptr)
  69. mIconMeshHeap->dealloc(mIconMesh);
  70. bs_delete(mDrawHelper);
  71. bs_delete(mPickingDrawHelper);
  72. gCoreThread().queueCommand(std::bind(&GizmoManager::destroyCore, this, mCore.load(std::memory_order_relaxed)));
  73. }
  74. void GizmoManager::initializeCore(const CoreInitData& initData)
  75. {
  76. mCore.load(std::memory_order_acquire)->initialize(initData);
  77. }
  78. void GizmoManager::destroyCore(GizmoManagerCore* core)
  79. {
  80. bs_delete(core);
  81. }
  82. void GizmoManager::startGizmo(const HSceneObject& gizmoParent)
  83. {
  84. mActiveSO = gizmoParent;
  85. if(mTransformDirty)
  86. {
  87. mTransform = Matrix4::IDENTITY;
  88. mDrawHelper->setTransform(Matrix4::IDENTITY);
  89. mTransformDirty = false;
  90. }
  91. if(mColorDirty)
  92. {
  93. mColor = Color();
  94. mColorDirty = false;
  95. }
  96. }
  97. void GizmoManager::endGizmo()
  98. {
  99. mActiveSO = nullptr;
  100. }
  101. void GizmoManager::setColor(const Color& color)
  102. {
  103. mDrawHelper->setColor(color);
  104. mColor = color;
  105. mColorDirty = true;
  106. }
  107. void GizmoManager::setTransform(const Matrix4& transform)
  108. {
  109. mDrawHelper->setTransform(transform);
  110. mTransform = transform;
  111. mTransformDirty = true;
  112. }
  113. void GizmoManager::drawCube(const Vector3& position, const Vector3& extents)
  114. {
  115. mSolidCubeData.push_back(CubeData());
  116. CubeData& cubeData = mSolidCubeData.back();
  117. cubeData.idx = mCurrentIdx++;
  118. cubeData.position = position;
  119. cubeData.extents = extents;
  120. cubeData.color = mColor;
  121. cubeData.transform = mTransform;
  122. cubeData.sceneObject = mActiveSO;
  123. cubeData.pickable = mPickable;
  124. mDrawHelper->cube(position, extents);
  125. mIdxToSceneObjectMap[cubeData.idx] = mActiveSO;
  126. }
  127. void GizmoManager::drawSphere(const Vector3& position, float radius)
  128. {
  129. mSolidSphereData.push_back(SphereData());
  130. SphereData& sphereData = mSolidSphereData.back();
  131. sphereData.idx = mCurrentIdx++;
  132. sphereData.position = position;
  133. sphereData.radius = radius;
  134. sphereData.color = mColor;
  135. sphereData.transform = mTransform;
  136. sphereData.sceneObject = mActiveSO;
  137. sphereData.pickable = mPickable;
  138. mDrawHelper->sphere(position, radius);
  139. mIdxToSceneObjectMap[sphereData.idx] = mActiveSO;
  140. }
  141. void GizmoManager::drawCone(const Vector3& base, const Vector3& normal, float height, float radius, const Vector2& scale)
  142. {
  143. mSolidConeData.push_back(ConeData());
  144. ConeData& coneData = mSolidConeData.back();
  145. coneData.idx = mCurrentIdx++;
  146. coneData.base = base;
  147. coneData.radius = radius;
  148. coneData.color = mColor;
  149. coneData.transform = mTransform;
  150. coneData.sceneObject = mActiveSO;
  151. coneData.pickable = mPickable;
  152. coneData.scale = scale;
  153. mDrawHelper->cone(base, normal, height, radius, scale);
  154. mIdxToSceneObjectMap[coneData.idx] = mActiveSO;
  155. }
  156. void GizmoManager::drawWireCube(const Vector3& position, const Vector3& extents)
  157. {
  158. mWireCubeData.push_back(CubeData());
  159. CubeData& cubeData = mWireCubeData.back();
  160. cubeData.idx = mCurrentIdx++;
  161. cubeData.position = position;
  162. cubeData.extents = extents;
  163. cubeData.color = mColor;
  164. cubeData.transform = mTransform;
  165. cubeData.sceneObject = mActiveSO;
  166. cubeData.pickable = mPickable;
  167. mDrawHelper->wireCube(position, extents);
  168. mIdxToSceneObjectMap[cubeData.idx] = mActiveSO;
  169. }
  170. void GizmoManager::drawWireSphere(const Vector3& position, float radius)
  171. {
  172. mWireSphereData.push_back(SphereData());
  173. SphereData& sphereData = mWireSphereData.back();
  174. sphereData.idx = mCurrentIdx++;
  175. sphereData.position = position;
  176. sphereData.radius = radius;
  177. sphereData.color = mColor;
  178. sphereData.transform = mTransform;
  179. sphereData.sceneObject = mActiveSO;
  180. sphereData.pickable = mPickable;
  181. mDrawHelper->wireSphere(position, radius);
  182. mIdxToSceneObjectMap[sphereData.idx] = mActiveSO;
  183. }
  184. void GizmoManager::drawWireCapsule(const Vector3& position, float height, float radius)
  185. {
  186. float halfHeight = height * 0.5f;
  187. // Draw capsule sides
  188. Vector3 sideNegXBottom = position + Vector3(-radius, -halfHeight, 0.0f);
  189. Vector3 sideNegXTop = position + Vector3(-radius, halfHeight, 0.0f);
  190. Vector3 sidePosXBottom = position + Vector3(radius, -halfHeight, 0.0f);
  191. Vector3 sidePosXTop = position + Vector3(radius, halfHeight, 0.0f);
  192. Vector3 sideNegZBottom = position + Vector3(0.0f, -halfHeight, -radius);
  193. Vector3 sideNegZTop = position + Vector3(0.0f, halfHeight, -radius);
  194. Vector3 sidePosZBottom = position + Vector3(0.0f, -halfHeight, radius);
  195. Vector3 sidePosZTop = position + Vector3(0.0f, halfHeight, radius);
  196. drawLine(sideNegXBottom, sideNegXTop);
  197. drawLine(sidePosXBottom, sidePosXTop);
  198. drawLine(sideNegZBottom, sideNegZTop);
  199. drawLine(sidePosZBottom, sidePosZTop);
  200. // Draw capsule caps
  201. Vector3 topHemisphere = position + Vector3(0.0f, halfHeight, 0.0f);
  202. Vector3 botHemisphere = position + Vector3(0.0f, -halfHeight, 0.0f);
  203. drawWireArc(topHemisphere, Vector3::UNIT_X, radius, Degree(270.0f), -Degree(180.0f));
  204. drawWireArc(topHemisphere, Vector3::UNIT_Z, radius, Degree(0.0f), -Degree(180.0f));
  205. drawWireArc(botHemisphere, Vector3::UNIT_X, radius, Degree(90.0f), -Degree(180.0f));
  206. drawWireArc(botHemisphere, Vector3::UNIT_Z, radius, Degree(180.0f), -Degree(180.0f));
  207. drawWireDisc(topHemisphere, Vector3::UNIT_Y, radius);
  208. drawWireDisc(botHemisphere, Vector3::UNIT_Y, radius);
  209. }
  210. void GizmoManager::drawWireCone(const Vector3& base, const Vector3& normal, float height, float radius, const Vector2& scale)
  211. {
  212. mWireConeData.push_back(ConeData());
  213. ConeData& coneData = mWireConeData.back();
  214. coneData.idx = mCurrentIdx++;
  215. coneData.base = base;
  216. coneData.radius = radius;
  217. coneData.color = mColor;
  218. coneData.transform = mTransform;
  219. coneData.sceneObject = mActiveSO;
  220. coneData.pickable = mPickable;
  221. coneData.scale = scale;
  222. mDrawHelper->wireCone(base, normal, height, radius, scale);
  223. mIdxToSceneObjectMap[coneData.idx] = mActiveSO;
  224. }
  225. void GizmoManager::drawLine(const Vector3& start, const Vector3& end)
  226. {
  227. mLineData.push_back(LineData());
  228. LineData& lineData = mLineData.back();
  229. lineData.idx = mCurrentIdx++;
  230. lineData.start = start;
  231. lineData.end = end;
  232. lineData.color = mColor;
  233. lineData.transform = mTransform;
  234. lineData.sceneObject = mActiveSO;
  235. lineData.pickable = mPickable;
  236. mDrawHelper->line(start, end);
  237. mIdxToSceneObjectMap[lineData.idx] = mActiveSO;
  238. }
  239. void GizmoManager::drawLineList(const Vector<Vector3>& linePoints)
  240. {
  241. mLineListData.push_back(LineListData());
  242. LineListData& lineListData = mLineListData.back();
  243. lineListData.idx = mCurrentIdx++;
  244. lineListData.linePoints = linePoints;
  245. lineListData.color = mColor;
  246. lineListData.transform = mTransform;
  247. lineListData.sceneObject = mActiveSO;
  248. lineListData.pickable = mPickable;
  249. mDrawHelper->lineList(linePoints);
  250. mIdxToSceneObjectMap[lineListData.idx] = mActiveSO;
  251. }
  252. void GizmoManager::drawWireDisc(const Vector3& position, const Vector3& normal, float radius)
  253. {
  254. mWireDiscData.push_back(WireDiscData());
  255. WireDiscData& wireDiscData = mWireDiscData.back();
  256. wireDiscData.idx = mCurrentIdx++;
  257. wireDiscData.position = position;
  258. wireDiscData.normal = normal;
  259. wireDiscData.radius = radius;
  260. wireDiscData.color = mColor;
  261. wireDiscData.transform = mTransform;
  262. wireDiscData.sceneObject = mActiveSO;
  263. wireDiscData.pickable = mPickable;
  264. mDrawHelper->wireDisc(position, normal, radius);
  265. mIdxToSceneObjectMap[wireDiscData.idx] = mActiveSO;
  266. }
  267. void GizmoManager::drawWireArc(const Vector3& position, const Vector3& normal, float radius,
  268. Degree startAngle, Degree amountAngle)
  269. {
  270. mWireArcData.push_back(WireArcData());
  271. WireArcData& wireArcData = mWireArcData.back();
  272. wireArcData.idx = mCurrentIdx++;
  273. wireArcData.position = position;
  274. wireArcData.normal = normal;
  275. wireArcData.radius = radius;
  276. wireArcData.startAngle = startAngle;
  277. wireArcData.amountAngle = amountAngle;
  278. wireArcData.color = mColor;
  279. wireArcData.transform = mTransform;
  280. wireArcData.sceneObject = mActiveSO;
  281. wireArcData.pickable = mPickable;
  282. mDrawHelper->wireArc(position, normal, radius, startAngle, amountAngle);
  283. mIdxToSceneObjectMap[wireArcData.idx] = mActiveSO;
  284. }
  285. void GizmoManager::drawWireMesh(const SPtr<MeshData>& meshData)
  286. {
  287. mWireMeshData.push_back(WireMeshData());
  288. WireMeshData& wireMeshData = mWireMeshData.back();
  289. wireMeshData.idx = mCurrentIdx++;
  290. wireMeshData.meshData = meshData;
  291. wireMeshData.color = mColor;
  292. wireMeshData.transform = mTransform;
  293. wireMeshData.sceneObject = mActiveSO;
  294. wireMeshData.pickable = mPickable;
  295. mDrawHelper->wireMesh(meshData);
  296. mIdxToSceneObjectMap[wireMeshData.idx] = mActiveSO;
  297. }
  298. void GizmoManager::drawFrustum(const Vector3& position, float aspect, Degree FOV, float near, float far)
  299. {
  300. mFrustumData.push_back(FrustumData());
  301. FrustumData& frustumData = mFrustumData.back();
  302. frustumData.idx = mCurrentIdx++;
  303. frustumData.position = position;
  304. frustumData.aspect = aspect;
  305. frustumData.FOV = FOV;
  306. frustumData.near = near;
  307. frustumData.far = far;
  308. frustumData.color = mColor;
  309. frustumData.transform = mTransform;
  310. frustumData.sceneObject = mActiveSO;
  311. frustumData.pickable = mPickable;
  312. mDrawHelper->frustum(position, aspect, FOV, near, far);
  313. mIdxToSceneObjectMap[frustumData.idx] = mActiveSO;
  314. }
  315. void GizmoManager::drawIcon(Vector3 position, HSpriteTexture image, bool fixedScale)
  316. {
  317. mIconData.push_back(IconData());
  318. IconData& iconData = mIconData.back();
  319. iconData.idx = mCurrentIdx++;
  320. iconData.position = position;
  321. iconData.texture = image;
  322. iconData.fixedScale = fixedScale;
  323. iconData.color = mColor;
  324. iconData.transform = mTransform;
  325. iconData.sceneObject = mActiveSO;
  326. iconData.pickable = mPickable;
  327. mIdxToSceneObjectMap[iconData.idx] = mActiveSO;
  328. }
  329. void GizmoManager::drawText(const Vector3& position, const WString& text, const HFont& font, UINT32 fontSize)
  330. {
  331. HFont myFont = font;
  332. if (myFont == nullptr)
  333. myFont = BuiltinEditorResources::instance().getDefaultAAFont();
  334. mTextData.push_back(TextData());
  335. TextData& textData = mTextData.back();
  336. textData.idx = mCurrentIdx++;
  337. textData.position = position;
  338. textData.text = text;
  339. textData.font = myFont;
  340. textData.fontSize = fontSize;
  341. textData.color = mColor;
  342. textData.transform = mTransform;
  343. textData.sceneObject = mActiveSO;
  344. textData.pickable = mPickable;
  345. mDrawHelper->text(position, text, myFont, fontSize);
  346. mIdxToSceneObjectMap[textData.idx] = mActiveSO;
  347. }
  348. void GizmoManager::update(const SPtr<Camera>& camera)
  349. {
  350. mDrawHelper->clearMeshes(mActiveMeshes);
  351. mActiveMeshes.clear();
  352. if (mIconMesh != nullptr)
  353. mIconMeshHeap->dealloc(mIconMesh);
  354. IconRenderDataVecPtr iconRenderData;
  355. mDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera->getPosition());
  356. mActiveMeshes = mDrawHelper->getMeshes();
  357. Vector<GizmoManagerCore::MeshData> proxyData;
  358. for (auto& meshData : mActiveMeshes)
  359. {
  360. SPtr<TextureCore> tex;
  361. if (meshData.texture.isLoaded())
  362. tex = meshData.texture->getCore();
  363. if (meshData.type == DrawHelper::MeshType::Solid)
  364. {
  365. proxyData.push_back(GizmoManagerCore::MeshData(
  366. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Solid));
  367. }
  368. else if(meshData.type == DrawHelper::MeshType::Wire)
  369. {
  370. proxyData.push_back(GizmoManagerCore::MeshData(
  371. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Wire));
  372. }
  373. else if (meshData.type == DrawHelper::MeshType::Line)
  374. {
  375. proxyData.push_back(GizmoManagerCore::MeshData(
  376. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Line));
  377. }
  378. else // Text
  379. {
  380. proxyData.push_back(GizmoManagerCore::MeshData(
  381. meshData.mesh->getCore(), tex, GizmoManagerCore::MeshType::Text));
  382. }
  383. }
  384. mIconMesh = buildIconMesh(camera, mIconData, false, iconRenderData);
  385. SPtr<MeshCoreBase> iconMesh = mIconMesh->getCore();
  386. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  387. gCoreThread().queueCommand(std::bind(&GizmoManagerCore::updateData, core, camera->getCore(),
  388. proxyData, iconMesh, iconRenderData));
  389. }
  390. void GizmoManager::renderForPicking(const SPtr<Camera>& camera, std::function<Color(UINT32)> idxToColorCallback)
  391. {
  392. Vector<IconData> iconData;
  393. IconRenderDataVecPtr iconRenderData;
  394. mPickingDrawHelper->clear();
  395. for (auto& cubeDataEntry : mSolidCubeData)
  396. {
  397. if (!cubeDataEntry.pickable)
  398. continue;
  399. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  400. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  401. mPickingDrawHelper->cube(cubeDataEntry.position, cubeDataEntry.extents);
  402. }
  403. for (auto& cubeDataEntry : mWireCubeData)
  404. {
  405. if (!cubeDataEntry.pickable)
  406. continue;
  407. mPickingDrawHelper->setColor(idxToColorCallback(cubeDataEntry.idx));
  408. mPickingDrawHelper->setTransform(cubeDataEntry.transform);
  409. mPickingDrawHelper->wireCube(cubeDataEntry.position, cubeDataEntry.extents);
  410. }
  411. for (auto& sphereDataEntry : mSolidSphereData)
  412. {
  413. if (!sphereDataEntry.pickable)
  414. continue;
  415. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  416. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  417. mPickingDrawHelper->sphere(sphereDataEntry.position, sphereDataEntry.radius);
  418. }
  419. for (auto& sphereDataEntry : mWireSphereData)
  420. {
  421. if (!sphereDataEntry.pickable)
  422. continue;
  423. mPickingDrawHelper->setColor(idxToColorCallback(sphereDataEntry.idx));
  424. mPickingDrawHelper->setTransform(sphereDataEntry.transform);
  425. mPickingDrawHelper->wireSphere(sphereDataEntry.position, sphereDataEntry.radius);
  426. }
  427. for (auto& coneDataEntry : mSolidConeData)
  428. {
  429. if (!coneDataEntry.pickable)
  430. continue;
  431. mPickingDrawHelper->setColor(idxToColorCallback(coneDataEntry.idx));
  432. mPickingDrawHelper->setTransform(coneDataEntry.transform);
  433. mPickingDrawHelper->cone(coneDataEntry.base, coneDataEntry.normal, coneDataEntry.radius, coneDataEntry.radius,
  434. coneDataEntry.scale);
  435. }
  436. for (auto& coneDataEntry : mWireConeData)
  437. {
  438. if (!coneDataEntry.pickable)
  439. continue;
  440. mPickingDrawHelper->setColor(idxToColorCallback(coneDataEntry.idx));
  441. mPickingDrawHelper->setTransform(coneDataEntry.transform);
  442. mPickingDrawHelper->wireCone(coneDataEntry.base, coneDataEntry.normal, coneDataEntry.radius, coneDataEntry.radius,
  443. coneDataEntry.scale);
  444. }
  445. for (auto& lineDataEntry : mLineData)
  446. {
  447. if (!lineDataEntry.pickable)
  448. continue;
  449. mPickingDrawHelper->setColor(idxToColorCallback(lineDataEntry.idx));
  450. mPickingDrawHelper->setTransform(lineDataEntry.transform);
  451. mPickingDrawHelper->line(lineDataEntry.start, lineDataEntry.end);
  452. }
  453. for (auto& lineListDataEntry : mLineListData)
  454. {
  455. if (!lineListDataEntry.pickable)
  456. continue;
  457. mPickingDrawHelper->setColor(idxToColorCallback(lineListDataEntry.idx));
  458. mPickingDrawHelper->setTransform(lineListDataEntry.transform);
  459. mPickingDrawHelper->lineList(lineListDataEntry.linePoints);
  460. }
  461. for (auto& wireDiscDataEntry : mWireDiscData)
  462. {
  463. if (!wireDiscDataEntry.pickable)
  464. continue;
  465. mPickingDrawHelper->setColor(idxToColorCallback(wireDiscDataEntry.idx));
  466. mPickingDrawHelper->setTransform(wireDiscDataEntry.transform);
  467. mPickingDrawHelper->wireDisc(wireDiscDataEntry.position, wireDiscDataEntry.normal, wireDiscDataEntry.radius);
  468. }
  469. for (auto& wireArcDataEntry : mWireArcData)
  470. {
  471. if (!wireArcDataEntry.pickable)
  472. continue;
  473. mPickingDrawHelper->setColor(idxToColorCallback(wireArcDataEntry.idx));
  474. mPickingDrawHelper->setTransform(wireArcDataEntry.transform);
  475. mPickingDrawHelper->wireArc(wireArcDataEntry.position, wireArcDataEntry.normal, wireArcDataEntry.radius,
  476. wireArcDataEntry.startAngle, wireArcDataEntry.amountAngle);
  477. }
  478. for (auto& wireMeshData : mWireMeshData)
  479. {
  480. if (!wireMeshData.pickable)
  481. continue;
  482. mPickingDrawHelper->setColor(idxToColorCallback(wireMeshData.idx));
  483. mPickingDrawHelper->setTransform(wireMeshData.transform);
  484. mPickingDrawHelper->wireMesh(wireMeshData.meshData);
  485. }
  486. for (auto& frustumDataEntry : mFrustumData)
  487. {
  488. if (!frustumDataEntry.pickable)
  489. continue;
  490. mPickingDrawHelper->setColor(idxToColorCallback(frustumDataEntry.idx));
  491. mPickingDrawHelper->setTransform(frustumDataEntry.transform);
  492. mPickingDrawHelper->frustum(frustumDataEntry.position, frustumDataEntry.aspect, frustumDataEntry.FOV,
  493. frustumDataEntry.near, frustumDataEntry.far);
  494. }
  495. for (auto& textDataEntry : mTextData)
  496. {
  497. if (!textDataEntry.pickable)
  498. continue;
  499. mPickingDrawHelper->setColor(idxToColorCallback(textDataEntry.idx));
  500. mPickingDrawHelper->setTransform(textDataEntry.transform);
  501. mPickingDrawHelper->text(textDataEntry.position, textDataEntry.text, textDataEntry.font,
  502. textDataEntry.fontSize);
  503. }
  504. for (auto& iconDataEntry : mIconData)
  505. {
  506. if (!iconDataEntry.pickable)
  507. continue;
  508. iconData.push_back(iconDataEntry);
  509. iconData.back().color = idxToColorCallback(iconDataEntry.idx);
  510. }
  511. mPickingDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera->getPosition());
  512. const Vector<DrawHelper::ShapeMeshData>& meshes = mPickingDrawHelper->getMeshes();
  513. SPtr<TransientMesh> iconMesh = buildIconMesh(camera, iconData, true, iconRenderData);
  514. // Note: This must be rendered while Scene view is being rendered
  515. Matrix4 viewMat = camera->getViewMatrix();
  516. Matrix4 projMat = camera->getProjectionMatrixRS();
  517. SPtr<Viewport> viewport = camera->getViewport();
  518. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  519. for (auto& meshData : meshes)
  520. {
  521. SPtr<TextureCore> tex;
  522. if (meshData.texture.isLoaded())
  523. tex = meshData.texture->getCore();
  524. if(meshData.type == DrawHelper::MeshType::Text)
  525. {
  526. gCoreThread().queueCommand(std::bind(&GizmoManagerCore::renderGizmos, core, viewMat, projMat,
  527. camera->getForward(), meshData.mesh->getCore(), tex, GizmoMaterial::PickingAlpha));
  528. }
  529. else
  530. {
  531. gCoreThread().queueCommand(std::bind(&GizmoManagerCore::renderGizmos, core, viewMat, projMat,
  532. camera->getForward(), meshData.mesh->getCore(), tex, GizmoMaterial::Picking));
  533. }
  534. }
  535. Rect2I screenArea = camera->getViewport()->getArea();
  536. gCoreThread().queueCommand(std::bind(&GizmoManagerCore::renderIconGizmos,
  537. core, screenArea, iconMesh->getCore(), iconRenderData, true));
  538. mPickingDrawHelper->clearMeshes(meshes);
  539. mIconMeshHeap->dealloc(iconMesh);
  540. }
  541. void GizmoManager::clearGizmos()
  542. {
  543. mSolidCubeData.clear();
  544. mWireCubeData.clear();
  545. mSolidSphereData.clear();
  546. mWireSphereData.clear();
  547. mSolidConeData.clear();
  548. mWireConeData.clear();
  549. mLineData.clear();
  550. mLineListData.clear();
  551. mWireDiscData.clear();
  552. mWireArcData.clear();
  553. mWireMeshData.clear();
  554. mFrustumData.clear();
  555. mTextData.clear();
  556. mIconData.clear();
  557. mIdxToSceneObjectMap.clear();
  558. mDrawHelper->clear();
  559. mCurrentIdx = 0;
  560. }
  561. void GizmoManager::clearRenderData()
  562. {
  563. mDrawHelper->clearMeshes(mActiveMeshes);
  564. mActiveMeshes.clear();
  565. if (mIconMesh != nullptr)
  566. mIconMeshHeap->dealloc(mIconMesh);
  567. mIconMesh = nullptr;
  568. GizmoManagerCore* core = mCore.load(std::memory_order_relaxed);
  569. IconRenderDataVecPtr iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
  570. gCoreThread().queueCommand(std::bind(&GizmoManagerCore::updateData, core,
  571. nullptr, Vector<GizmoManagerCore::MeshData>(), nullptr, iconRenderData));
  572. }
  573. SPtr<TransientMesh> GizmoManager::buildIconMesh(const SPtr<Camera>& camera, const Vector<IconData>& iconData,
  574. bool forPicking, GizmoManager::IconRenderDataVecPtr& iconRenderData)
  575. {
  576. mSortedIconData.clear();
  577. if (iconData.size() > mSortedIconData.size())
  578. mSortedIconData.resize(iconData.size());
  579. UINT32 i = 0;
  580. for (auto& iconEntry : iconData)
  581. {
  582. Vector3 viewPoint = camera->worldToViewPoint(iconEntry.position);
  583. float distance = -viewPoint.z;
  584. if (distance < camera->getNearClipDistance()) // Ignore behind clip plane
  585. continue;
  586. if (distance > MAX_ICON_RANGE) // Ignore too far away
  587. continue;
  588. if (!iconEntry.texture.isLoaded()) // Ignore missing texture
  589. continue;
  590. if (forPicking && !iconEntry.pickable)
  591. continue;
  592. SortedIconData& sortedIconData = mSortedIconData[i];
  593. sortedIconData.iconIdx = i;
  594. sortedIconData.distance = distance;
  595. sortedIconData.screenPosition = camera->viewToScreenPoint(viewPoint);
  596. i++;
  597. }
  598. UINT32 actualNumIcons = i;
  599. // Sort back to front first, then by texture
  600. std::sort(mSortedIconData.begin(), mSortedIconData.begin() + actualNumIcons,
  601. [&](const SortedIconData& a, const SortedIconData& b)
  602. {
  603. if (a.distance == b.distance)
  604. {
  605. HTexture texA = iconData[a.iconIdx].texture->getTexture();
  606. HTexture texB = iconData[b.iconIdx].texture->getTexture();
  607. if (texA == texB)
  608. return a.iconIdx < b.iconIdx;
  609. return texA->getInternalID() < texB->getInternalID();
  610. }
  611. else
  612. return a.distance > b.distance;
  613. });
  614. SPtr<MeshData> meshData = bs_shared_ptr_new<MeshData>(actualNumIcons * 4, actualNumIcons * 6, mIconVertexDesc);
  615. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  616. auto texcoordIter = meshData->getVec2DataIter(VES_TEXCOORD);
  617. auto normalColorIter = meshData->getDWORDDataIter(VES_COLOR, 0);
  618. auto fadedColorIter = meshData->getDWORDDataIter(VES_COLOR, 1);
  619. UINT32* indices = meshData->getIndices32();
  620. float cameraScale = 1.0f;
  621. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  622. cameraScale = camera->getViewport()->getHeight() / camera->getOrthoWindowHeight();
  623. else
  624. {
  625. Radian vertFOV(Math::tan(camera->getHorzFOV() * 0.5f));
  626. cameraScale = (camera->getViewport()->getHeight() * 0.5f) / vertFOV.valueRadians();
  627. }
  628. iconRenderData = bs_shared_ptr_new<IconRenderDataVec>();
  629. UINT32 lastTextureIdx = std::numeric_limits<UINT32>::max();
  630. HTexture curTexture;
  631. // Note: This assumes the meshes will be rendered using the same camera
  632. // properties as when they are created
  633. for (i = 0; i < actualNumIcons; i++)
  634. {
  635. SortedIconData& sortedIconData = mSortedIconData[i];
  636. const IconData& curIconData = iconData[sortedIconData.iconIdx];
  637. HTexture atlasTexture = curIconData.texture->getTexture();
  638. if (curTexture != atlasTexture)
  639. {
  640. UINT32 numIconsPerTexture = i - lastTextureIdx;
  641. if (numIconsPerTexture > 0)
  642. {
  643. iconRenderData->push_back(IconRenderData());
  644. IconRenderData& renderData = iconRenderData->back();
  645. renderData.count = numIconsPerTexture;
  646. renderData.texture = atlasTexture->getCore();
  647. }
  648. lastTextureIdx = i;
  649. curTexture = atlasTexture;
  650. }
  651. UINT32 iconWidth = curIconData.texture->getWidth();
  652. UINT32 iconHeight = curIconData.texture->getHeight();
  653. limitIconSize(iconWidth, iconHeight);
  654. Vector3 position((float)sortedIconData.screenPosition.x, (float)sortedIconData.screenPosition.y, -sortedIconData.distance);
  655. Vector3 projPosition = camera->projectPoint(position);
  656. position.z = projPosition.z;
  657. float halfWidth = iconWidth * 0.5f;
  658. float halfHeight = iconHeight * 0.5f;
  659. if (!curIconData.fixedScale)
  660. {
  661. float iconScale = 1.0f;
  662. if (camera->getProjectionType() == PT_ORTHOGRAPHIC)
  663. iconScale = cameraScale * ICON_TEXEL_WORLD_SIZE;
  664. else
  665. iconScale = (cameraScale * ICON_TEXEL_WORLD_SIZE) / sortedIconData.distance;
  666. halfWidth *= iconScale;
  667. halfHeight *= iconScale;
  668. }
  669. Color normalColor, fadedColor;
  670. calculateIconColors(curIconData.color, camera, (UINT32)(halfHeight * 2.0f), curIconData.fixedScale, normalColor, fadedColor);
  671. if (forPicking)
  672. {
  673. normalColor = curIconData.color;
  674. fadedColor = curIconData.color;
  675. }
  676. Vector3 positions[4];
  677. positions[0] = position + Vector3(-halfWidth, -halfHeight, 0.0f);
  678. positions[1] = position + Vector3(halfWidth, -halfHeight, 0.0f);
  679. positions[2] = position + Vector3(halfWidth, halfHeight, 0.0f);
  680. positions[3] = position + Vector3(-halfWidth, halfHeight, 0.0f);
  681. Vector2 uvs[4];
  682. uvs[0] = curIconData.texture->transformUV(Vector2(0.0f, 0.0f));
  683. uvs[1] = curIconData.texture->transformUV(Vector2(1.0f, 0.0f));
  684. uvs[2] = curIconData.texture->transformUV(Vector2(1.0f, 1.0f));
  685. uvs[3] = curIconData.texture->transformUV(Vector2(0.0f, 1.0f));
  686. for (UINT32 j = 0; j < 4; j++)
  687. {
  688. positionIter.addValue(positions[j]);
  689. texcoordIter.addValue(uvs[j]);
  690. normalColorIter.addValue(normalColor.getAsRGBA());
  691. fadedColorIter.addValue(fadedColor.getAsRGBA());
  692. }
  693. UINT32 vertOffset = i * 4;
  694. indices[0] = vertOffset + 0;
  695. indices[1] = vertOffset + 1;
  696. indices[2] = vertOffset + 2;
  697. indices[3] = vertOffset + 0;
  698. indices[4] = vertOffset + 2;
  699. indices[5] = vertOffset + 3;
  700. indices += 6;
  701. }
  702. return mIconMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  703. }
  704. void GizmoManager::limitIconSize(UINT32& width, UINT32& height)
  705. {
  706. if (width <= OPTIMAL_ICON_SIZE && height <= OPTIMAL_ICON_SIZE)
  707. return;
  708. float relWidth = OPTIMAL_ICON_SIZE / (float)width;
  709. float relHeight = OPTIMAL_ICON_SIZE / (float)height;
  710. float scale = std::min(relWidth, relHeight);
  711. width = Math::roundToInt(width * scale);
  712. height = Math::roundToInt(height * scale);
  713. }
  714. void GizmoManager::calculateIconColors(const Color& tint, const SPtr<Camera>& camera,
  715. UINT32 iconHeight, bool fixedScale, Color& normalColor, Color& fadedColor)
  716. {
  717. normalColor = tint;
  718. if (!fixedScale)
  719. {
  720. float iconToScreenRatio = iconHeight / (float)camera->getViewport()->getHeight();
  721. if (iconToScreenRatio > 0.3f)
  722. {
  723. float alpha = 1.0f - Math::lerp01(iconToScreenRatio, 0.3f, 1.0f);
  724. normalColor.a *= alpha;
  725. }
  726. else if (iconToScreenRatio < 0.1f)
  727. {
  728. float alpha = Math::lerp01(iconToScreenRatio, 0.0f, 0.1f);
  729. normalColor.a *= alpha;
  730. }
  731. }
  732. fadedColor = normalColor;
  733. fadedColor.a *= 0.2f;
  734. }
  735. HSceneObject GizmoManager::getSceneObject(UINT32 gizmoIdx)
  736. {
  737. auto iterFind = mIdxToSceneObjectMap.find(gizmoIdx);
  738. if (iterFind != mIdxToSceneObjectMap.end())
  739. return iterFind->second;
  740. return HSceneObject();
  741. }
  742. const float GizmoManagerCore::PICKING_ALPHA_CUTOFF = 0.5f;
  743. GizmoManagerCore::GizmoManagerCore(const PrivatelyConstuct& dummy)
  744. {
  745. }
  746. GizmoManagerCore::~GizmoManagerCore()
  747. {
  748. SPtr<CoreRenderer> activeRenderer = RendererManager::instance().getActive();
  749. if (mCamera != nullptr)
  750. activeRenderer->unregisterRenderCallback(mCamera.get(), 20);
  751. }
  752. void GizmoManagerCore::initialize(const GizmoManager::CoreInitData& initData)
  753. {
  754. THROW_IF_NOT_CORE_THREAD;
  755. mSolidMaterial.mat = initData.solidMat;
  756. mWireMaterial.mat = initData.wireMat;
  757. mLineMaterial.mat = initData.lineMat;
  758. mTextMaterial.mat = initData.textMat;
  759. mIconMaterial.mat = initData.iconMat;
  760. mPickingMaterial.mat = initData.pickingMat;
  761. mAlphaPickingMaterial.mat = initData.alphaPickingMat;
  762. {
  763. mLineMaterial.params = mLineMaterial.mat->createParamsSet();
  764. SPtr<GpuParamsCore> params = mLineMaterial.params->getGpuParams();
  765. params->getParam(GPT_VERTEX_PROGRAM, "matViewProj", mLineMaterial.viewProj);
  766. }
  767. {
  768. mSolidMaterial.params = mSolidMaterial.mat->createParamsSet();
  769. SPtr<GpuParamsCore> params = mSolidMaterial.params->getGpuParams();
  770. params->getParam(GPT_VERTEX_PROGRAM, "matViewProj", mSolidMaterial.viewProj);
  771. params->getParam(GPT_FRAGMENT_PROGRAM, "viewDir", mSolidMaterial.viewDir);
  772. }
  773. {
  774. mWireMaterial.params = mWireMaterial.mat->createParamsSet();
  775. SPtr<GpuParamsCore> params = mWireMaterial.params->getGpuParams();
  776. params->getParam(GPT_VERTEX_PROGRAM, "matViewProj", mWireMaterial.viewProj);
  777. }
  778. {
  779. mIconMaterial.params = mIconMaterial.mat->createParamsSet();
  780. SPtr<GpuParamsCore> params0 = mIconMaterial.params->getGpuParams(0);
  781. params0->getParam(GPT_VERTEX_PROGRAM, "matViewProj", mIconMaterial.viewProj[0]);
  782. params0->getTextureParam(GPT_FRAGMENT_PROGRAM, "mainTexture", mIconMaterial.texture[0]);
  783. SPtr<GpuParamsCore> params1 = mIconMaterial.params->getGpuParams(1);
  784. params1->getParam(GPT_VERTEX_PROGRAM, "matViewProj", mIconMaterial.viewProj[1]);
  785. params1->getTextureParam(GPT_FRAGMENT_PROGRAM, "mainTexture", mIconMaterial.texture[1]);
  786. }
  787. {
  788. mPickingMaterial.params = mPickingMaterial.mat->createParamsSet();
  789. SPtr<GpuParamsCore> params = mPickingMaterial.params->getGpuParams();
  790. params->getParam(GPT_VERTEX_PROGRAM, "matViewProj", mPickingMaterial.viewProj);
  791. }
  792. {
  793. mAlphaPickingMaterial.params = mAlphaPickingMaterial.mat->createParamsSet();
  794. SPtr<GpuParamsCore> params = mAlphaPickingMaterial.params->getGpuParams();
  795. params->getParam(GPT_VERTEX_PROGRAM, "matViewProj", mAlphaPickingMaterial.viewProj);
  796. params->getTextureParam(GPT_FRAGMENT_PROGRAM, "mainTexture", mAlphaPickingMaterial.texture);
  797. GpuParamFloatCore alphaCutoffParam;
  798. params->getParam(GPT_FRAGMENT_PROGRAM, "alphaCutoff", alphaCutoffParam);
  799. alphaCutoffParam.set(PICKING_ALPHA_CUTOFF);
  800. }
  801. {
  802. mTextMaterial.params = mTextMaterial.mat->createParamsSet();
  803. SPtr<GpuParamsCore> params = mTextMaterial.params->getGpuParams();
  804. params->getParam(GPT_VERTEX_PROGRAM, "matViewProj", mTextMaterial.viewProj);
  805. params->getTextureParam(GPT_FRAGMENT_PROGRAM, "mainTexture", mTextMaterial.texture);
  806. }
  807. }
  808. void GizmoManagerCore::updateData(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes,
  809. const SPtr<MeshCoreBase>& iconMesh, const GizmoManager::IconRenderDataVecPtr& iconRenderData)
  810. {
  811. if (mCamera != camera)
  812. {
  813. SPtr<CoreRenderer> activeRenderer = RendererManager::instance().getActive();
  814. if (mCamera != nullptr)
  815. activeRenderer->unregisterRenderCallback(mCamera.get(), 0);
  816. if (camera != nullptr)
  817. activeRenderer->registerRenderCallback(camera.get(), 0, std::bind(&GizmoManagerCore::render, this));
  818. }
  819. mCamera = camera;
  820. mMeshes = meshes;
  821. mIconMesh = iconMesh;
  822. mIconRenderData = iconRenderData;
  823. }
  824. void GizmoManagerCore::render()
  825. {
  826. if (mCamera == nullptr)
  827. return;
  828. SPtr<RenderTargetCore> renderTarget = mCamera->getViewport()->getTarget();
  829. float width = (float)renderTarget->getProperties().getWidth();
  830. float height = (float)renderTarget->getProperties().getHeight();
  831. Rect2 normArea = mCamera->getViewport()->getNormArea();
  832. Rect2I screenArea;
  833. screenArea.x = (int)(normArea.x * width);
  834. screenArea.y = (int)(normArea.y * height);
  835. screenArea.width = (int)(normArea.width * width);
  836. screenArea.height = (int)(normArea.height * height);
  837. for (auto& meshData : mMeshes)
  838. {
  839. GizmoManager::GizmoMaterial material = GizmoManager::GizmoMaterial::Solid;
  840. switch(meshData.type)
  841. {
  842. case MeshType::Solid:
  843. material = GizmoManager::GizmoMaterial::Solid;
  844. break;
  845. case MeshType::Wire:
  846. material = GizmoManager::GizmoMaterial::Wire;
  847. break;
  848. case MeshType::Line:
  849. material = GizmoManager::GizmoMaterial::Line;
  850. break;
  851. case MeshType::Text:
  852. material = GizmoManager::GizmoMaterial::Text;
  853. break;
  854. }
  855. renderGizmos(mCamera->getViewMatrix(), mCamera->getProjectionMatrixRS(), mCamera->getForward(),
  856. meshData.mesh, meshData.texture, material);
  857. }
  858. if (mIconMesh != nullptr)
  859. renderIconGizmos(screenArea, mIconMesh, mIconRenderData, false);
  860. }
  861. void GizmoManagerCore::renderGizmos(const Matrix4& viewMatrix, const Matrix4& projMatrix, const Vector3& viewDir,
  862. const SPtr<MeshCoreBase>& mesh, const SPtr<TextureCore>& texture, GizmoManager::GizmoMaterial material)
  863. {
  864. THROW_IF_NOT_CORE_THREAD;
  865. Matrix4 viewProjMat = projMatrix * viewMatrix;
  866. switch (material)
  867. {
  868. case GizmoManager::GizmoMaterial::Solid:
  869. mSolidMaterial.viewProj.set(viewProjMat);
  870. mSolidMaterial.viewDir.set((Vector4)viewDir);
  871. gRendererUtility().setPass(mSolidMaterial.mat);
  872. gRendererUtility().setPassParams(mSolidMaterial.params);
  873. break;
  874. case GizmoManager::GizmoMaterial::Wire:
  875. mWireMaterial.viewProj.set(viewProjMat);
  876. gRendererUtility().setPass(mWireMaterial.mat);
  877. gRendererUtility().setPassParams(mWireMaterial.params);
  878. break;
  879. case GizmoManager::GizmoMaterial::Line:
  880. mLineMaterial.viewProj.set(viewProjMat);
  881. gRendererUtility().setPass(mLineMaterial.mat);
  882. gRendererUtility().setPassParams(mLineMaterial.params);
  883. break;
  884. case GizmoManager::GizmoMaterial::Picking:
  885. mPickingMaterial.viewProj.set(viewProjMat);
  886. gRendererUtility().setPass(mPickingMaterial.mat);
  887. gRendererUtility().setPassParams(mPickingMaterial.params);
  888. break;
  889. case GizmoManager::GizmoMaterial::PickingAlpha:
  890. mAlphaPickingMaterial.viewProj.set(viewProjMat);
  891. mAlphaPickingMaterial.texture.set(texture);
  892. gRendererUtility().setPass(mAlphaPickingMaterial.mat);
  893. gRendererUtility().setPassParams(mAlphaPickingMaterial.params);
  894. break;
  895. case GizmoManager::GizmoMaterial::Text:
  896. mTextMaterial.viewProj.set(viewProjMat);
  897. mTextMaterial.texture.set(texture);
  898. gRendererUtility().setPass(mTextMaterial.mat);
  899. gRendererUtility().setPassParams(mTextMaterial.params);
  900. break;
  901. }
  902. gRendererUtility().draw(mesh, mesh->getProperties().getSubMesh(0));
  903. }
  904. void GizmoManagerCore::renderIconGizmos(Rect2I screenArea, SPtr<MeshCoreBase> mesh, GizmoManager::IconRenderDataVecPtr renderData, bool usePickingMaterial)
  905. {
  906. RenderAPICore& rapi = RenderAPICore::instance();
  907. SPtr<VertexData> vertexData = mesh->getVertexData();
  908. rapi.setVertexDeclaration(vertexData->vertexDeclaration);
  909. auto vertexBuffers = vertexData->getBuffers();
  910. SPtr<VertexBufferCore> vertBuffers[1] = { vertexBuffers.begin()->second };
  911. rapi.setVertexBuffers(0, vertBuffers, 1);
  912. SPtr<IndexBufferCore> indexBuffer = mesh->getIndexBuffer();
  913. rapi.setIndexBuffer(indexBuffer);
  914. rapi.setDrawOperation(DOT_TRIANGLE_LIST);
  915. // Set up ortho matrix
  916. Matrix4 projMat;
  917. const RenderAPIInfo& rapiInfo = rapi.getAPIInfo();
  918. float left = screenArea.x + rapiInfo.getHorizontalTexelOffset();
  919. float right = screenArea.x + screenArea.width + rapiInfo.getHorizontalTexelOffset();
  920. float top = screenArea.y + rapiInfo.getVerticalTexelOffset();
  921. float bottom = screenArea.y + screenArea.height + rapiInfo.getVerticalTexelOffset();
  922. float near = rapiInfo.getMinimumDepthInputValue();
  923. float far = rapiInfo.getMaximumDepthInputValue();
  924. // Top/bottom have been swapped because we're moving from window coordinates (origin top left)
  925. // to normalized device coordinates (origin bottom left)
  926. // Negative near/far because Z is flipped for normalized device coordinates
  927. // (positive Z goes into screen as opposed to view space here we're looking along negative Z)
  928. projMat.makeProjectionOrtho(left, right, top, bottom, -near, -far);
  929. if (!usePickingMaterial)
  930. {
  931. mIconMaterial.viewProj[0].set(projMat);
  932. mIconMaterial.viewProj[1].set(projMat);
  933. for (UINT32 passIdx = 0; passIdx < 2; passIdx++)
  934. {
  935. gRendererUtility().setPass(mIconMaterial.mat, passIdx);
  936. gRendererUtility().setPassParams(mIconMaterial.params, passIdx);
  937. UINT32 curIndexOffset = mesh->getIndexOffset();
  938. for (auto curRenderData : *renderData)
  939. {
  940. mIconMaterial.texture[passIdx].set(curRenderData.texture);
  941. SPtr<GpuParamsCore> params = mIconMaterial.params->getGpuParams(passIdx);
  942. rapi.setGpuParams(params);
  943. rapi.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  944. curIndexOffset += curRenderData.count * 6;
  945. }
  946. }
  947. }
  948. else
  949. {
  950. mAlphaPickingMaterial.viewProj.set(projMat);
  951. gRendererUtility().setPass(mAlphaPickingMaterial.mat);
  952. gRendererUtility().setPassParams(mAlphaPickingMaterial.params);
  953. UINT32 curIndexOffset = 0;
  954. for (auto curRenderData : *renderData)
  955. {
  956. mAlphaPickingMaterial.texture.set(curRenderData.texture);
  957. SPtr<GpuParamsCore> params = mAlphaPickingMaterial.params->getGpuParams();
  958. rapi.setGpuParams(params);
  959. rapi.drawIndexed(curIndexOffset, curRenderData.count * 6, mesh->getVertexOffset(), curRenderData.count * 4);
  960. curIndexOffset += curRenderData.count * 6;
  961. }
  962. }
  963. mesh->_notifyUsedOnGPU();
  964. }
  965. }