BsGizmoManager.cpp 37 KB

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