BsGizmoManager.cpp 37 KB

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