AtomDebugDisplayViewportInterface.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Serialization/SerializeContext.h>
  9. #include <AzCore/Component/ComponentApplicationBus.h>
  10. #include <AzCore/std/algorithm.h>
  11. #include <AzCore/std/containers/array.h>
  12. #include <AzCore/std/containers/span.h>
  13. #include <AzCore/Math/Obb.h>
  14. #include <AzCore/Math/Aabb.h>
  15. #include <AzCore/Math/Frustum.h>
  16. #include <AtomDebugDisplayViewportInterface.h>
  17. #include <Atom/RPI.Public/View.h>
  18. #include <Atom/RPI.Public/ViewportContextBus.h>
  19. #include <AzCore/Interface/Interface.h>
  20. #include <AzFramework/Font/FontInterface.h>
  21. namespace AZ::AtomBridge
  22. {
  23. ////////////////////////////////////////////////////////////////////////
  24. SingleColorDynamicSizeLineHelper::SingleColorDynamicSizeLineHelper(
  25. int estimatedNumLineSegments
  26. )
  27. {
  28. m_points.reserve(AZStd::abs(estimatedNumLineSegments) * 2);
  29. }
  30. void SingleColorDynamicSizeLineHelper::AddLineSegment(
  31. const AZ::Vector3& lineStart,
  32. const AZ::Vector3& lineEnd
  33. )
  34. {
  35. m_points.push_back(lineStart);
  36. m_points.push_back(lineEnd);
  37. }
  38. void SingleColorDynamicSizeLineHelper::Draw(
  39. AZ::RPI::AuxGeomDrawPtr auxGeomDrawPtr,
  40. const RenderState& rendState
  41. ) const
  42. {
  43. if (auxGeomDrawPtr && !m_points.empty())
  44. {
  45. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  46. drawArgs.m_verts = m_points.data();
  47. drawArgs.m_vertCount = aznumeric_cast<uint32_t>(m_points.size());
  48. drawArgs.m_colors = &rendState.m_color;
  49. drawArgs.m_colorCount = 1;
  50. drawArgs.m_size = rendState.m_lineWidth;
  51. drawArgs.m_opacityType = rendState.m_opacityType;
  52. drawArgs.m_depthTest = rendState.m_depthTest;
  53. drawArgs.m_depthWrite = rendState.m_depthWrite;
  54. drawArgs.m_viewProjectionOverrideIndex = rendState.m_viewProjOverrideIndex;
  55. auxGeomDrawPtr->DrawLines( drawArgs );
  56. }
  57. }
  58. void SingleColorDynamicSizeLineHelper::Draw2d(
  59. AZ::RPI::AuxGeomDrawPtr auxGeomDrawPtr,
  60. const RenderState& rendState
  61. ) const
  62. {
  63. if (auxGeomDrawPtr && !m_points.empty())
  64. {
  65. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  66. drawArgs.m_verts = m_points.data();
  67. drawArgs.m_vertCount = aznumeric_cast<uint32_t>(m_points.size());
  68. drawArgs.m_colors = &rendState.m_color;
  69. drawArgs.m_colorCount = 1;
  70. drawArgs.m_size = rendState.m_lineWidth;
  71. drawArgs.m_opacityType = rendState.m_opacityType;
  72. drawArgs.m_depthTest = rendState.m_depthTest;
  73. drawArgs.m_depthWrite = rendState.m_depthWrite;
  74. drawArgs.m_viewProjectionOverrideIndex = auxGeomDrawPtr->GetOrAdd2DViewProjOverride();
  75. auxGeomDrawPtr->DrawLines( drawArgs );
  76. }
  77. }
  78. void SingleColorDynamicSizeLineHelper::Reset()
  79. {
  80. m_points.clear();
  81. }
  82. ////////////////////////////////////////////////////////////////////////
  83. AtomDebugDisplayViewportInterface::AtomDebugDisplayViewportInterface(AZ::RPI::ViewportContextPtr viewportContextPtr)
  84. {
  85. ResetRenderState();
  86. m_viewportId = viewportContextPtr->GetId();
  87. m_defaultInstance = false;
  88. auto setupScene = [this](RPI::ScenePtr scene)
  89. {
  90. auto viewportContextManager = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
  91. AZ::RPI::ViewportContextPtr viewportContextPtr = viewportContextManager->GetViewportContextById(m_viewportId);
  92. InitInternal(scene.get(), viewportContextPtr);
  93. };
  94. setupScene(viewportContextPtr->GetRenderScene());
  95. m_sceneChangeHandler = AZ::RPI::ViewportContext::SceneChangedEvent::Handler(setupScene);
  96. viewportContextPtr->ConnectSceneChangedHandler(m_sceneChangeHandler);
  97. }
  98. AtomDebugDisplayViewportInterface::AtomDebugDisplayViewportInterface(uint32_t defaultInstanceAddress, RPI::Scene* scene)
  99. {
  100. ResetRenderState();
  101. m_viewportId = defaultInstanceAddress;
  102. m_defaultInstance = true;
  103. InitInternal(scene, nullptr);
  104. }
  105. void AtomDebugDisplayViewportInterface::UpdateAuxGeom(RPI::Scene* scene, AZ::RPI::View* view)
  106. {
  107. if (!scene)
  108. {
  109. m_auxGeomPtr = nullptr;
  110. return;
  111. }
  112. auto auxGeomFP = scene->GetFeatureProcessor<RPI::AuxGeomFeatureProcessorInterface>();
  113. if (!auxGeomFP)
  114. {
  115. m_auxGeomPtr = nullptr;
  116. return;
  117. }
  118. // default instance draws to all viewports in the default scene
  119. if (m_defaultInstance || !view)
  120. {
  121. m_auxGeomPtr = auxGeomFP->GetDrawQueue();
  122. }
  123. else
  124. {
  125. // cache the aux geom draw interface for the current view (aka camera)
  126. m_auxGeomPtr = auxGeomFP->GetOrCreateDrawQueueForView(view);
  127. }
  128. }
  129. void AtomDebugDisplayViewportInterface::InitInternal(RPI::Scene* scene, AZ::RPI::ViewportContextPtr viewportContextPtr)
  130. {
  131. AzFramework::DebugDisplayRequestBus::Handler::BusDisconnect(m_viewportId);
  132. UpdateAuxGeom(scene, viewportContextPtr ? viewportContextPtr->GetDefaultView().get() : nullptr);
  133. AzFramework::DebugDisplayRequestBus::Handler::BusConnect(m_viewportId);
  134. if (!m_defaultInstance) // only the per viewport instances need to listen for viewport changes
  135. {
  136. AZ::RPI::ViewportContextIdNotificationBus::Handler::BusConnect(viewportContextPtr->GetId());
  137. }
  138. }
  139. void AtomDebugDisplayViewportInterface::OnViewportDefaultViewChanged(AZ::RPI::ViewPtr view)
  140. {
  141. ResetRenderState();
  142. if (!m_defaultInstance)
  143. {
  144. // handle viewport update (view change, scene change, etc
  145. auto viewportContextManager = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
  146. AZ::RPI::ViewportContextPtr viewportContextPtr = viewportContextManager->GetViewportContextById(m_viewportId);
  147. UpdateAuxGeom(viewportContextPtr->GetRenderScene().get(), viewportContextPtr->GetDefaultView().get());
  148. }
  149. }
  150. AtomDebugDisplayViewportInterface::~AtomDebugDisplayViewportInterface()
  151. {
  152. AzFramework::DebugDisplayRequestBus::Handler::BusDisconnect(m_viewportId);
  153. AZ::RPI::ViewportContextIdNotificationBus::Handler::BusDisconnect();
  154. m_viewportId = AzFramework::InvalidViewportId;
  155. m_auxGeomPtr = nullptr;
  156. }
  157. void AtomDebugDisplayViewportInterface::ResetRenderState()
  158. {
  159. m_rendState = RenderState();
  160. for (int index = 0; index < RenderState::TransformStackSize; ++index)
  161. {
  162. m_rendState.m_transformStack[index] = AZ::Matrix3x4::Identity();
  163. }
  164. }
  165. void AtomDebugDisplayViewportInterface::SetColor(const AZ::Color& color)
  166. {
  167. m_rendState.m_color = color;
  168. }
  169. void AtomDebugDisplayViewportInterface::SetAlpha(float a)
  170. {
  171. m_rendState.m_color.SetA(a);
  172. m_rendState.m_opacityType = a < 1.0f ? AZ::RPI::AuxGeomDraw::OpacityType::Translucent : AZ::RPI::AuxGeomDraw::OpacityType::Opaque;
  173. }
  174. void AtomDebugDisplayViewportInterface::DrawQuad(
  175. const AZ::Vector3& p1,
  176. const AZ::Vector3& p2,
  177. const AZ::Vector3& p3,
  178. const AZ::Vector3& p4)
  179. {
  180. if (m_auxGeomPtr)
  181. {
  182. AZ::Vector3 wsPoints[4] = { ToWorldSpacePosition(p1), ToWorldSpacePosition(p2), ToWorldSpacePosition(p3), ToWorldSpacePosition(p4) };
  183. AZ::Vector3 triangles[6];
  184. triangles[0] = wsPoints[0];
  185. triangles[1] = wsPoints[1];
  186. triangles[2] = wsPoints[2];
  187. triangles[3] = wsPoints[2];
  188. triangles[4] = wsPoints[3];
  189. triangles[5] = wsPoints[0];
  190. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  191. drawArgs.m_verts = triangles;
  192. drawArgs.m_vertCount = 6;
  193. drawArgs.m_colors = &m_rendState.m_color;
  194. drawArgs.m_colorCount = 1;
  195. drawArgs.m_opacityType = m_rendState.m_opacityType;
  196. drawArgs.m_depthTest = m_rendState.m_depthTest;
  197. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  198. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  199. m_auxGeomPtr->DrawTriangles(drawArgs);
  200. }
  201. }
  202. void AtomDebugDisplayViewportInterface::DrawQuad(float width, float height, bool drawShaded)
  203. {
  204. if (!m_auxGeomPtr || width <= 0.0f || height <= 0.0f)
  205. {
  206. return;
  207. }
  208. m_auxGeomPtr->DrawQuad(
  209. width,
  210. height,
  211. GetCurrentTransform(),
  212. m_rendState.m_color,
  213. drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
  214. m_rendState.m_depthTest,
  215. m_rendState.m_depthWrite,
  216. m_rendState.m_faceCullMode,
  217. m_rendState.m_viewProjOverrideIndex);
  218. }
  219. void AtomDebugDisplayViewportInterface::DrawWireQuad(
  220. const AZ::Vector3& p1,
  221. const AZ::Vector3& p2,
  222. const AZ::Vector3& p3,
  223. const AZ::Vector3& p4)
  224. {
  225. if (m_auxGeomPtr)
  226. {
  227. AZ::Vector3 wsPoints[4] = { ToWorldSpacePosition(p1), ToWorldSpacePosition(p2), ToWorldSpacePosition(p3), ToWorldSpacePosition(p4) };
  228. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  229. drawArgs.m_verts = wsPoints;
  230. drawArgs.m_vertCount = 4;
  231. drawArgs.m_colors = &m_rendState.m_color;
  232. drawArgs.m_colorCount = 1;
  233. drawArgs.m_opacityType = m_rendState.m_opacityType;
  234. drawArgs.m_depthTest = m_rendState.m_depthTest;
  235. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  236. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  237. m_auxGeomPtr->DrawPolylines(drawArgs, AZ::RPI::AuxGeomDraw::PolylineEnd::Closed);
  238. }
  239. }
  240. void AtomDebugDisplayViewportInterface::DrawWireQuad(float width, float height)
  241. {
  242. if (!m_auxGeomPtr || width <= 0.0f || height <= 0.0f)
  243. {
  244. return;
  245. }
  246. m_auxGeomPtr->DrawQuad(
  247. width,
  248. height,
  249. GetCurrentTransform(),
  250. m_rendState.m_color,
  251. AZ::RPI::AuxGeomDraw::DrawStyle::Line,
  252. m_rendState.m_depthTest,
  253. m_rendState.m_depthWrite,
  254. m_rendState.m_faceCullMode,
  255. m_rendState.m_viewProjOverrideIndex);
  256. }
  257. void AtomDebugDisplayViewportInterface::DrawQuadGradient(
  258. const AZ::Vector3& p1,
  259. const AZ::Vector3& p2,
  260. const AZ::Vector3& p3,
  261. const AZ::Vector3& p4,
  262. const AZ::Vector4& firstColor,
  263. const AZ::Vector4& secondColor)
  264. {
  265. if (m_auxGeomPtr)
  266. {
  267. AZ::Vector3 wsPoints[4] = { ToWorldSpacePosition(p1), ToWorldSpacePosition(p2), ToWorldSpacePosition(p3), ToWorldSpacePosition(p4) };
  268. AZ::Vector3 triangles[6];
  269. AZ::Color colors[6];
  270. triangles[0] = wsPoints[0]; colors[0] = firstColor;
  271. triangles[1] = wsPoints[1]; colors[1] = firstColor;
  272. triangles[2] = wsPoints[2]; colors[2] = secondColor;
  273. triangles[3] = wsPoints[2]; colors[3] = secondColor;
  274. triangles[4] = wsPoints[3]; colors[4] = secondColor;
  275. triangles[5] = wsPoints[0]; colors[5] = firstColor;
  276. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  277. drawArgs.m_verts = triangles;
  278. drawArgs.m_vertCount = 6;
  279. drawArgs.m_colors = colors;
  280. drawArgs.m_colorCount = 6;
  281. drawArgs.m_opacityType = m_rendState.m_opacityType;
  282. drawArgs.m_depthTest = m_rendState.m_depthTest;
  283. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  284. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  285. m_auxGeomPtr->DrawTriangles(drawArgs);
  286. }
  287. }
  288. void AtomDebugDisplayViewportInterface::DrawQuad2dGradient(
  289. const Vector2& p1,
  290. const Vector2& p2,
  291. const Vector2& p3,
  292. const Vector2& p4,
  293. float z,
  294. const Color& firstColor,
  295. const Color& secondColor)
  296. {
  297. if (m_auxGeomPtr)
  298. {
  299. Vector3 points[4];
  300. points[0] = Vector3(p1.GetX(), p1.GetY(), z);
  301. points[1] = Vector3(p2.GetX(), p1.GetY(), z);
  302. points[2] = Vector3(p3.GetX(), p3.GetY(), z);
  303. points[3] = Vector3(p4.GetX(), p4.GetY(), z);
  304. Vector3 triangles[6];
  305. Color colors[6];
  306. triangles[0] = points[0]; colors[0] = firstColor;
  307. triangles[1] = points[1]; colors[1] = firstColor;
  308. triangles[2] = points[2]; colors[2] = secondColor;
  309. triangles[3] = points[2]; colors[3] = secondColor;
  310. triangles[4] = points[3]; colors[4] = secondColor;
  311. triangles[5] = points[0]; colors[5] = firstColor;
  312. RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  313. drawArgs.m_verts = triangles;
  314. drawArgs.m_vertCount = 6;
  315. drawArgs.m_colors = colors;
  316. drawArgs.m_colorCount = 6;
  317. const bool alphaBlend = firstColor.GetA() < 1.0f || secondColor.GetA() < 1.0f;
  318. drawArgs.m_opacityType = alphaBlend ? RPI::AuxGeomDraw::OpacityType::Translucent : RPI::AuxGeomDraw::OpacityType::Opaque;
  319. drawArgs.m_depthTest = m_rendState.m_depthTest;
  320. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  321. drawArgs.m_viewProjectionOverrideIndex = m_auxGeomPtr->GetOrAdd2DViewProjOverride();
  322. m_auxGeomPtr->DrawTriangles(drawArgs);
  323. }
  324. }
  325. void AtomDebugDisplayViewportInterface::DrawTri(const AZ::Vector3& p1, const AZ::Vector3& p2, const AZ::Vector3& p3)
  326. {
  327. if (m_auxGeomPtr)
  328. {
  329. AZ::Vector3 verts[3] = {ToWorldSpacePosition(p1), ToWorldSpacePosition(p2), ToWorldSpacePosition(p3)};
  330. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  331. drawArgs.m_verts = verts;
  332. drawArgs.m_vertCount = 3;
  333. drawArgs.m_colors = &m_rendState.m_color;
  334. drawArgs.m_colorCount = 1;
  335. drawArgs.m_opacityType = m_rendState.m_opacityType;
  336. drawArgs.m_depthTest = m_rendState.m_depthTest;
  337. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  338. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  339. m_auxGeomPtr->DrawTriangles(drawArgs);
  340. }
  341. }
  342. void AtomDebugDisplayViewportInterface::DrawTriangles(const AZStd::vector<AZ::Vector3>& vertices, const AZ::Color& color)
  343. {
  344. if (m_auxGeomPtr)
  345. {
  346. AZStd::vector<AZ::Vector3> transformedVertices = ToWorldSpacePosition(vertices);
  347. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  348. drawArgs.m_verts = transformedVertices.data();
  349. drawArgs.m_vertCount = aznumeric_cast<uint32_t>(transformedVertices.size());
  350. drawArgs.m_colors = &color;
  351. drawArgs.m_colorCount = 1;
  352. drawArgs.m_opacityType = m_rendState.m_opacityType;
  353. drawArgs.m_depthTest = m_rendState.m_depthTest;
  354. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  355. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  356. m_auxGeomPtr->DrawTriangles(drawArgs);
  357. }
  358. }
  359. void AtomDebugDisplayViewportInterface::DrawTrianglesIndexed(
  360. const AZStd::vector<AZ::Vector3>& vertices,
  361. const AZStd::vector<AZ::u32>& indices,
  362. const AZ::Color& color)
  363. {
  364. if (m_auxGeomPtr)
  365. {
  366. AZStd::vector<AZ::Vector3> transformedVertices = ToWorldSpacePosition(vertices);
  367. AZ::RPI::AuxGeomDraw::AuxGeomDynamicIndexedDrawArguments drawArgs;
  368. drawArgs.m_verts = transformedVertices.data();
  369. drawArgs.m_vertCount = aznumeric_cast<uint32_t>(transformedVertices.size());
  370. drawArgs.m_indices = indices.data();
  371. drawArgs.m_indexCount = aznumeric_cast<uint32_t>(indices.size());
  372. drawArgs.m_colors = &color;
  373. drawArgs.m_colorCount = 1;
  374. drawArgs.m_opacityType = m_rendState.m_opacityType;
  375. drawArgs.m_depthTest = m_rendState.m_depthTest;
  376. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  377. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  378. m_auxGeomPtr->DrawTriangles(drawArgs);
  379. }
  380. }
  381. void AtomDebugDisplayViewportInterface::DrawWireBox(const AZ::Vector3& min, const AZ::Vector3& max)
  382. {
  383. if (m_auxGeomPtr)
  384. {
  385. m_auxGeomPtr->DrawAabb(
  386. AZ::Aabb::CreateFromMinMax(min, max),
  387. GetCurrentTransform(),
  388. m_rendState.m_color,
  389. AZ::RPI::AuxGeomDraw::DrawStyle::Line,
  390. m_rendState.m_depthTest,
  391. m_rendState.m_depthWrite,
  392. m_rendState.m_faceCullMode,
  393. m_rendState.m_viewProjOverrideIndex
  394. );
  395. }
  396. }
  397. void AtomDebugDisplayViewportInterface::DrawSolidBox(const AZ::Vector3& min, const AZ::Vector3& max)
  398. {
  399. if (m_auxGeomPtr)
  400. {
  401. m_auxGeomPtr->DrawAabb(
  402. AZ::Aabb::CreateFromMinMax(min, max),
  403. GetCurrentTransform(),
  404. m_rendState.m_color,
  405. AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
  406. m_rendState.m_depthTest,
  407. m_rendState.m_depthWrite,
  408. m_rendState.m_faceCullMode,
  409. m_rendState.m_viewProjOverrideIndex);
  410. }
  411. }
  412. void AtomDebugDisplayViewportInterface::DrawWireOBB(
  413. const AZ::Vector3& center,
  414. const AZ::Vector3& axisX,
  415. const AZ::Vector3& axisY,
  416. const AZ::Vector3& axisZ,
  417. const AZ::Vector3& halfExtents)
  418. {
  419. if (m_auxGeomPtr)
  420. {
  421. AZ::Quaternion rotation = AZ::Quaternion::CreateFromMatrix3x3(AZ::Matrix3x3::CreateFromColumns(axisX, axisY, axisZ));
  422. AZ::Obb obb = AZ::Obb::CreateFromPositionRotationAndHalfLengths(center, rotation, halfExtents);
  423. m_auxGeomPtr->DrawObb(
  424. obb,
  425. AZ::Vector3::CreateZero(),
  426. m_rendState.m_color,
  427. AZ::RPI::AuxGeomDraw::DrawStyle::Line,
  428. m_rendState.m_depthTest,
  429. m_rendState.m_depthWrite,
  430. m_rendState.m_faceCullMode,
  431. m_rendState.m_viewProjOverrideIndex);
  432. }
  433. }
  434. void AtomDebugDisplayViewportInterface::DrawSolidOBB(
  435. const AZ::Vector3& center,
  436. const AZ::Vector3& axisX,
  437. const AZ::Vector3& axisY,
  438. const AZ::Vector3& axisZ,
  439. const AZ::Vector3& halfExtents)
  440. {
  441. if (m_auxGeomPtr)
  442. {
  443. AZ::Quaternion rotation = AZ::Quaternion::CreateFromMatrix3x3(AZ::Matrix3x3::CreateFromColumns(axisX, axisY, axisZ));
  444. AZ::Obb obb = AZ::Obb::CreateFromPositionRotationAndHalfLengths(center, rotation, halfExtents);
  445. m_auxGeomPtr->DrawObb(
  446. obb,
  447. AZ::Vector3::CreateZero(),
  448. m_rendState.m_color,
  449. AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
  450. m_rendState.m_depthTest,
  451. m_rendState.m_depthWrite,
  452. m_rendState.m_faceCullMode,
  453. m_rendState.m_viewProjOverrideIndex);
  454. }
  455. }
  456. void AtomDebugDisplayViewportInterface::DrawPoint(const AZ::Vector3& p, int nSize)
  457. {
  458. if (m_auxGeomPtr)
  459. {
  460. AZ::Vector3 wsPoint = ToWorldSpacePosition(p);
  461. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  462. drawArgs.m_verts = &wsPoint;
  463. drawArgs.m_vertCount = 1;
  464. drawArgs.m_colors = &m_rendState.m_color;
  465. drawArgs.m_colorCount = 1;
  466. drawArgs.m_size = aznumeric_cast<uint8_t>(nSize);
  467. drawArgs.m_opacityType = m_rendState.m_opacityType;
  468. drawArgs.m_depthTest = m_rendState.m_depthTest;
  469. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  470. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  471. m_auxGeomPtr->DrawPoints(drawArgs);
  472. }
  473. }
  474. void AtomDebugDisplayViewportInterface::DrawLine(const AZ::Vector3& p1, const AZ::Vector3& p2)
  475. {
  476. if (m_auxGeomPtr)
  477. {
  478. AZ::Vector3 verts[2] = {ToWorldSpacePosition(p1), ToWorldSpacePosition(p2)};
  479. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  480. drawArgs.m_verts = verts;
  481. drawArgs.m_vertCount = 2;
  482. drawArgs.m_colors = &m_rendState.m_color;
  483. drawArgs.m_colorCount = 1;
  484. drawArgs.m_size = m_rendState.m_lineWidth;
  485. drawArgs.m_opacityType = m_rendState.m_opacityType;
  486. drawArgs.m_depthTest = m_rendState.m_depthTest;
  487. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  488. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  489. m_auxGeomPtr->DrawLines(drawArgs);
  490. }
  491. }
  492. void AtomDebugDisplayViewportInterface::DrawLine(const AZ::Vector3& p1, const AZ::Vector3& p2, const AZ::Vector4& col1, const AZ::Vector4& col2)
  493. {
  494. if (m_auxGeomPtr)
  495. {
  496. AZ::Vector3 verts[2] = {ToWorldSpacePosition(p1), ToWorldSpacePosition(p2)};
  497. AZ::Color colors[2] = {col1, col2};
  498. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  499. drawArgs.m_verts = verts;
  500. drawArgs.m_vertCount = 2;
  501. drawArgs.m_colors = colors;
  502. drawArgs.m_colorCount = 2;
  503. drawArgs.m_size = m_rendState.m_lineWidth;
  504. drawArgs.m_opacityType = m_rendState.m_opacityType;
  505. drawArgs.m_depthTest = m_rendState.m_depthTest;
  506. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  507. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  508. m_auxGeomPtr->DrawLines(drawArgs);
  509. }
  510. }
  511. void AtomDebugDisplayViewportInterface::DrawLines(const AZStd::vector<AZ::Vector3>& lines, const AZ::Color& color)
  512. {
  513. if (m_auxGeomPtr)
  514. {
  515. AZStd::vector<AZ::Vector3> transformedLines = ToWorldSpacePosition(lines);
  516. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  517. drawArgs.m_verts = transformedLines.data();
  518. drawArgs.m_vertCount = aznumeric_cast<uint32_t>(transformedLines.size());
  519. drawArgs.m_colors = &color;
  520. drawArgs.m_colorCount = 1;
  521. drawArgs.m_size = m_rendState.m_lineWidth;
  522. drawArgs.m_opacityType = m_rendState.m_opacityType;
  523. drawArgs.m_depthTest = m_rendState.m_depthTest;
  524. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  525. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  526. m_auxGeomPtr->DrawLines(drawArgs);
  527. }
  528. }
  529. void AtomDebugDisplayViewportInterface::DrawPolyLine(AZStd::span<const AZ::Vector3> points, bool cycled)
  530. {
  531. if (m_auxGeomPtr)
  532. {
  533. AZStd::vector<AZ::Vector3> wsPoints;
  534. wsPoints.resize_no_construct(points.size());
  535. AZStd::transform(points.begin(), points.end(), wsPoints.begin(), [&](auto& pnt) {
  536. return ToWorldSpacePosition(pnt);
  537. });
  538. AZ::RPI::AuxGeomDraw::PolylineEnd polylineEnd = cycled ? AZ::RPI::AuxGeomDraw::PolylineEnd::Closed : AZ::RPI::AuxGeomDraw::PolylineEnd::Open;
  539. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  540. drawArgs.m_verts = wsPoints.data();
  541. drawArgs.m_vertCount = aznumeric_cast <uint32_t>(wsPoints.size());
  542. drawArgs.m_colors = &m_rendState.m_color;
  543. drawArgs.m_colorCount = 1;
  544. drawArgs.m_size = m_rendState.m_lineWidth;
  545. drawArgs.m_opacityType = m_rendState.m_opacityType;
  546. drawArgs.m_depthTest = m_rendState.m_depthTest;
  547. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  548. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  549. m_auxGeomPtr->DrawPolylines(drawArgs, polylineEnd);
  550. }
  551. }
  552. void AtomDebugDisplayViewportInterface::DrawPolyLine(const AZ::Vector3* pnts, int numPoints, bool cycled)
  553. {
  554. DrawPolyLine(AZStd::span<const AZ::Vector3>(pnts, numPoints), cycled);
  555. }
  556. void AtomDebugDisplayViewportInterface::DrawWireQuad2d(const AZ::Vector2& p1, const AZ::Vector2& p2, float z)
  557. {
  558. if (m_auxGeomPtr)
  559. {
  560. AZ::Vector3 points[4];
  561. points[0] = AZ::Vector3(p1.GetX(), p1.GetY(), z);
  562. points[1] = AZ::Vector3(p2.GetX(), p1.GetY(), z);
  563. points[2] = AZ::Vector3(p2.GetX(), p2.GetY(), z);
  564. points[3] = AZ::Vector3(p1.GetX(), p2.GetY(), z);
  565. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  566. drawArgs.m_verts = points;
  567. drawArgs.m_vertCount = 4;
  568. drawArgs.m_colors = &m_rendState.m_color;
  569. drawArgs.m_colorCount = 1;
  570. drawArgs.m_size = m_rendState.m_lineWidth;
  571. drawArgs.m_opacityType = m_rendState.m_opacityType;
  572. drawArgs.m_depthTest = m_rendState.m_depthTest;
  573. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  574. drawArgs.m_viewProjectionOverrideIndex = m_auxGeomPtr->GetOrAdd2DViewProjOverride();
  575. m_auxGeomPtr->DrawPolylines(drawArgs, AZ::RPI::AuxGeomDraw::PolylineEnd::Closed);
  576. }
  577. }
  578. void AtomDebugDisplayViewportInterface::DrawLine2d(const AZ::Vector2& p1, const AZ::Vector2& p2, float z)
  579. {
  580. if (m_auxGeomPtr)
  581. {
  582. AZ::Vector3 points[2];
  583. points[0] = AZ::Vector3(p1.GetX(), p1.GetY(), z);
  584. points[1] = AZ::Vector3(p2.GetX(), p2.GetY(), z);
  585. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  586. drawArgs.m_verts = points;
  587. drawArgs.m_vertCount = 2;
  588. drawArgs.m_colors = &m_rendState.m_color;
  589. drawArgs.m_colorCount = 1;
  590. drawArgs.m_size = m_rendState.m_lineWidth;
  591. drawArgs.m_opacityType = m_rendState.m_opacityType;
  592. drawArgs.m_depthTest = m_rendState.m_depthTest;
  593. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  594. drawArgs.m_viewProjectionOverrideIndex = m_auxGeomPtr->GetOrAdd2DViewProjOverride();
  595. m_auxGeomPtr->DrawLines(drawArgs);
  596. }
  597. }
  598. void AtomDebugDisplayViewportInterface::DrawLine2dGradient(const AZ::Vector2& p1, const AZ::Vector2& p2, float z, const AZ::Vector4& firstColor, const AZ::Vector4& secondColor)
  599. {
  600. if (m_auxGeomPtr)
  601. {
  602. AZ::Vector3 points[2];
  603. points[0] = AZ::Vector3(p1.GetX(), p1.GetY(), z);
  604. points[1] = AZ::Vector3(p2.GetX(), p2.GetY(), z);
  605. AZ::Color colors[2] = {firstColor, secondColor};
  606. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  607. drawArgs.m_verts = points;
  608. drawArgs.m_vertCount = 2;
  609. drawArgs.m_colors = colors;
  610. drawArgs.m_colorCount = 2;
  611. drawArgs.m_size = m_rendState.m_lineWidth;
  612. drawArgs.m_opacityType = m_rendState.m_opacityType;
  613. drawArgs.m_depthTest = m_rendState.m_depthTest;
  614. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  615. drawArgs.m_viewProjectionOverrideIndex = m_auxGeomPtr->GetOrAdd2DViewProjOverride();
  616. m_auxGeomPtr->DrawLines(drawArgs);
  617. }
  618. }
  619. void AtomDebugDisplayViewportInterface::DrawWireCircle2d(const AZ::Vector2& center, float radius, float z)
  620. {
  621. if (m_auxGeomPtr)
  622. {
  623. // Draw axis aligned arc
  624. constexpr float angularStepDegrees = 10.0f;
  625. constexpr float startAngleDegrees = 0.0f;
  626. constexpr float sweepAngleDegrees = 360.0f;
  627. const float stepAngle = DegToRad(angularStepDegrees);
  628. const float startAngle = DegToRad(startAngleDegrees);
  629. const float stopAngle = DegToRad(sweepAngleDegrees) + startAngle;
  630. SingleColorDynamicSizeLineHelper lines(1+static_cast<int>(sweepAngleDegrees/angularStepDegrees));
  631. float aspectRadius = radius / GetAspectRatio();
  632. AZ::Vector3 radiusV3 = AZ::Vector3(aspectRadius, radius, radius);
  633. AZ::Vector3 pos = AZ::Vector3(center.GetX(), center.GetY(), z);
  634. CreateAxisAlignedArc(
  635. lines,
  636. stepAngle,
  637. startAngle,
  638. stopAngle,
  639. pos,
  640. radiusV3,
  641. CircleAxis::CircleAxisZ
  642. );
  643. lines.Draw2d(m_auxGeomPtr, m_rendState);
  644. }
  645. }
  646. void AtomDebugDisplayViewportInterface::DrawArc(
  647. const AZ::Vector3& pos,
  648. float radius,
  649. float startAngleDegrees,
  650. float sweepAngleDegrees,
  651. float angularStepDegrees,
  652. int referenceAxis)
  653. {
  654. if (m_auxGeomPtr)
  655. {
  656. // Draw axis aligned arc
  657. const float stepAngle = DegToRad(angularStepDegrees);
  658. const float startAngle = DegToRad(startAngleDegrees);
  659. const float stopAngle = DegToRad(sweepAngleDegrees) + startAngle;
  660. SingleColorDynamicSizeLineHelper lines(1+static_cast<int>(sweepAngleDegrees/angularStepDegrees));
  661. AZ::Vector3 radiusV3 = AZ::Vector3(radius);
  662. CreateAxisAlignedArc(
  663. lines,
  664. stepAngle,
  665. startAngle,
  666. stopAngle,
  667. pos,
  668. radiusV3,
  669. static_cast<CircleAxis>(referenceAxis)
  670. );
  671. lines.Draw(m_auxGeomPtr, m_rendState);
  672. }
  673. }
  674. void AtomDebugDisplayViewportInterface::DrawArc(
  675. const AZ::Vector3& pos,
  676. float radius,
  677. float startAngleDegrees,
  678. float sweepAngleDegrees,
  679. float angularStepDegrees,
  680. const AZ::Vector3& fixedAxis)
  681. {
  682. if (m_auxGeomPtr)
  683. {
  684. // Draw arbitraty axis arc
  685. const float stepAngle = DegToRad(angularStepDegrees);
  686. const float startAngle = DegToRad(startAngleDegrees);
  687. const float stopAngle = DegToRad(sweepAngleDegrees) + startAngle;
  688. SingleColorDynamicSizeLineHelper lines(1+static_cast<int>(sweepAngleDegrees/angularStepDegrees));
  689. AZ::Vector3 radiusV3 = AZ::Vector3(radius);
  690. CreateArbitraryAxisArc(
  691. lines,
  692. stepAngle,
  693. startAngle,
  694. stopAngle,
  695. pos,
  696. radiusV3,
  697. fixedAxis
  698. );
  699. lines.Draw(m_auxGeomPtr, m_rendState);
  700. }
  701. }
  702. void AtomDebugDisplayViewportInterface::DrawCircle(const AZ::Vector3& pos, float radius, int nUnchangedAxis)
  703. {
  704. if (m_auxGeomPtr)
  705. {
  706. // Draw circle with default radius.
  707. const float step = DegToRad(10.0f);
  708. const float maxAngle = DegToRad(360.0f) + step;
  709. SingleColorStaticSizeLineHelper<40> lines; // hard code 40 lines until DegToRad is constexpr.
  710. AZ::Vector3 radiusV3 = AZ::Vector3(radius);
  711. CreateAxisAlignedArc(
  712. lines,
  713. step,
  714. 0.0f,
  715. maxAngle,
  716. pos,
  717. radiusV3,
  718. static_cast<CircleAxis>(nUnchangedAxis));
  719. lines.Draw(m_auxGeomPtr, m_rendState);
  720. }
  721. }
  722. void AtomDebugDisplayViewportInterface::DrawHalfDottedCircle(const AZ::Vector3& pos, float radius, const AZ::Vector3& viewPos, int nUnchangedAxis)
  723. {
  724. if (m_auxGeomPtr)
  725. {
  726. // Draw circle with single radius.
  727. const float step = DegToRad(10.0f);
  728. const float maxAngle = DegToRad(360.0f);
  729. SingleColorStaticSizeLineHelper<40> lines; // hard code 40 lines until DegToRad is constexpr.
  730. AZ::Vector3 radiusV3 = AZ::Vector3(radius);
  731. const AZ::Vector3 worldPos = ToWorldSpacePosition(pos);
  732. const AZ::Vector3 worldView = ToWorldSpacePosition(viewPos);
  733. const AZ::Vector3 worldDir = worldView - worldPos;
  734. CreateAxisAlignedArc(lines, step, 0.0f, maxAngle, pos, radiusV3, static_cast<CircleAxis>(nUnchangedAxis%CircleAxisMax),
  735. [&worldPos, &worldDir](const AZ::Vector3& lineStart, const AZ::Vector3& lineEnd, int segmentIndex)
  736. {
  737. AZ_UNUSED(lineEnd);
  738. const float dot = (lineStart - worldPos).Dot(worldDir);
  739. const bool facing = dot > 0.0f;
  740. // if so skip every other line to produce a dotted effect
  741. if (facing || segmentIndex % 2 == 0)
  742. {
  743. return true;
  744. }
  745. return false;
  746. });
  747. lines.Draw(m_auxGeomPtr, m_rendState);
  748. }
  749. }
  750. void AtomDebugDisplayViewportInterface::DrawWireCone(const AZ::Vector3& pos, const AZ::Vector3& dir, float radius, float height)
  751. {
  752. if (m_auxGeomPtr)
  753. {
  754. const AZ::Vector3 worldPos = ToWorldSpacePosition(pos);
  755. const AZ::Vector3 worldDir = ToWorldSpaceVector(dir);
  756. m_auxGeomPtr->DrawCone(
  757. worldPos,
  758. worldDir,
  759. radius,
  760. height,
  761. m_rendState.m_color,
  762. AZ::RPI::AuxGeomDraw::DrawStyle::Line,
  763. m_rendState.m_depthTest,
  764. m_rendState.m_depthWrite,
  765. m_rendState.m_faceCullMode,
  766. m_rendState.m_viewProjOverrideIndex
  767. );
  768. }
  769. }
  770. void AtomDebugDisplayViewportInterface::DrawSolidCone(const AZ::Vector3& pos, const AZ::Vector3& dir, float radius, float height, bool drawShaded)
  771. {
  772. if (m_auxGeomPtr)
  773. {
  774. const AZ::Vector3 worldPos = ToWorldSpacePosition(pos);
  775. const AZ::Vector3 worldDir = ToWorldSpaceVector(dir);
  776. m_auxGeomPtr->DrawCone(
  777. worldPos,
  778. worldDir,
  779. radius,
  780. height,
  781. m_rendState.m_color,
  782. drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
  783. m_rendState.m_depthTest,
  784. m_rendState.m_depthWrite,
  785. m_rendState.m_faceCullMode,
  786. m_rendState.m_viewProjOverrideIndex
  787. );
  788. }
  789. }
  790. void AtomDebugDisplayViewportInterface::DrawWireCylinder(const AZ::Vector3& center, const AZ::Vector3& axis, float radius, float height)
  791. {
  792. if (m_auxGeomPtr)
  793. {
  794. const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
  795. const AZ::Vector3 worldCenter = ToWorldSpacePosition(center);
  796. const AZ::Vector3 worldAxis = ToWorldSpaceVector(axis);
  797. m_auxGeomPtr->DrawCylinder(
  798. worldCenter,
  799. worldAxis,
  800. scale * radius,
  801. scale * height,
  802. m_rendState.m_color,
  803. AZ::RPI::AuxGeomDraw::DrawStyle::Line,
  804. m_rendState.m_depthTest,
  805. m_rendState.m_depthWrite,
  806. m_rendState.m_faceCullMode,
  807. m_rendState.m_viewProjOverrideIndex
  808. );
  809. }
  810. }
  811. void AtomDebugDisplayViewportInterface::DrawSolidCylinder(
  812. const AZ::Vector3& center,
  813. const AZ::Vector3& axis,
  814. float radius,
  815. float height,
  816. bool drawShaded)
  817. {
  818. if (m_auxGeomPtr)
  819. {
  820. const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
  821. const AZ::Vector3 worldCenter = ToWorldSpacePosition(center);
  822. const AZ::Vector3 worldAxis = ToWorldSpaceVector(axis);
  823. m_auxGeomPtr->DrawCylinder(
  824. worldCenter,
  825. worldAxis,
  826. scale * radius,
  827. scale * height,
  828. m_rendState.m_color,
  829. drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
  830. m_rendState.m_depthTest,
  831. m_rendState.m_depthWrite,
  832. m_rendState.m_faceCullMode,
  833. m_rendState.m_viewProjOverrideIndex
  834. );
  835. }
  836. }
  837. void AtomDebugDisplayViewportInterface::DrawWireCylinderNoEnds(const AZ::Vector3& center, const AZ::Vector3& axis, float radius, float height)
  838. {
  839. if (m_auxGeomPtr)
  840. {
  841. const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
  842. const AZ::Vector3 worldCenter = ToWorldSpacePosition(center);
  843. const AZ::Vector3 worldAxis = ToWorldSpaceVector(axis);
  844. m_auxGeomPtr->DrawCylinderNoEnds(
  845. worldCenter,
  846. worldAxis,
  847. scale * radius,
  848. scale * height,
  849. m_rendState.m_color,
  850. AZ::RPI::AuxGeomDraw::DrawStyle::Line,
  851. m_rendState.m_depthTest,
  852. m_rendState.m_depthWrite,
  853. m_rendState.m_faceCullMode,
  854. m_rendState.m_viewProjOverrideIndex
  855. );
  856. }
  857. }
  858. void AtomDebugDisplayViewportInterface::DrawSolidCylinderNoEnds(
  859. const AZ::Vector3& center,
  860. const AZ::Vector3& axis,
  861. float radius,
  862. float height,
  863. bool drawShaded)
  864. {
  865. if (m_auxGeomPtr)
  866. {
  867. const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
  868. const AZ::Vector3 worldCenter = ToWorldSpacePosition(center);
  869. const AZ::Vector3 worldAxis = ToWorldSpaceVector(axis);
  870. m_auxGeomPtr->DrawCylinderNoEnds(
  871. worldCenter,
  872. worldAxis,
  873. scale * radius,
  874. scale * height,
  875. m_rendState.m_color,
  876. drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
  877. m_rendState.m_depthTest,
  878. m_rendState.m_depthWrite,
  879. m_rendState.m_faceCullMode,
  880. m_rendState.m_viewProjOverrideIndex
  881. );
  882. }
  883. }
  884. void AtomDebugDisplayViewportInterface::DrawWireCapsule(
  885. const AZ::Vector3& center,
  886. const AZ::Vector3& axis,
  887. float radius,
  888. float heightStraightSection)
  889. {
  890. if (m_auxGeomPtr && radius > FLT_EPSILON && axis.GetLengthSq() > FLT_EPSILON)
  891. {
  892. AZ::Vector3 axisNormalized = axis.GetNormalizedEstimate();
  893. const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
  894. const AZ::Vector3 worldAxis = ToWorldSpaceVector(axis);
  895. // Draw cylinder part (if cylinder height is too small, ignore cylinder and just draw both hemispheres)
  896. if (heightStraightSection > FLT_EPSILON)
  897. {
  898. DrawWireCylinderNoEnds(center, axis, scale * radius, scale * heightStraightSection);
  899. }
  900. AZ::Vector3 centerToTopCircleCenter = axisNormalized * heightStraightSection * 0.5f;
  901. // Top hemisphere
  902. DrawWireHemisphere(center + centerToTopCircleCenter, worldAxis, scale * radius);
  903. // Bottom hemisphere
  904. DrawWireHemisphere(center - centerToTopCircleCenter, -worldAxis, scale * radius);
  905. }
  906. }
  907. void AtomDebugDisplayViewportInterface::DrawWireSphere(const AZ::Vector3& pos, float radius)
  908. {
  909. if (m_auxGeomPtr)
  910. {
  911. const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
  912. m_auxGeomPtr->DrawSphere(
  913. ToWorldSpacePosition(pos),
  914. scale * radius,
  915. m_rendState.m_color,
  916. AZ::RPI::AuxGeomDraw::DrawStyle::Line,
  917. m_rendState.m_depthTest,
  918. m_rendState.m_depthWrite,
  919. m_rendState.m_faceCullMode,
  920. m_rendState.m_viewProjOverrideIndex
  921. );
  922. }
  923. }
  924. void AtomDebugDisplayViewportInterface::DrawWireSphere(const AZ::Vector3& pos, const AZ::Vector3 radius)
  925. {
  926. if (m_auxGeomPtr)
  927. {
  928. // This matches Cry behavior, the DrawWireSphere above may need modifying to use the same approach.
  929. // Draw 3 axis aligned circles
  930. const float step = DegToRad(10.0f);
  931. const float maxAngle = DegToRad(360.0f);
  932. SingleColorStaticSizeLineHelper<40*3> lines; // hard code to 40 lines * 3 circles until DegToRad is constexpr.
  933. // Z Axis
  934. AZ::Vector3 axisRadius(radius.GetX(), radius.GetY(), 0.0f);
  935. CreateAxisAlignedArc(lines, step, 0.0f, maxAngle, pos, axisRadius, CircleAxisZ);
  936. // X Axis
  937. axisRadius = AZ::Vector3(0.0f, radius.GetY(), radius.GetZ());
  938. CreateAxisAlignedArc(lines, step, 0.0f, maxAngle, pos, axisRadius, CircleAxisX);
  939. // Y Axis
  940. axisRadius = AZ::Vector3(radius.GetX(), 0.0f, radius.GetZ());
  941. CreateAxisAlignedArc(lines, step, 0.0f, maxAngle, pos, axisRadius, CircleAxisY);
  942. lines.Draw(m_auxGeomPtr, m_rendState);
  943. }
  944. }
  945. void AtomDebugDisplayViewportInterface::DrawWireHemisphere(const AZ::Vector3& pos, const AZ::Vector3& axis, float radius)
  946. {
  947. if (m_auxGeomPtr)
  948. {
  949. const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
  950. m_auxGeomPtr->DrawHemisphere(
  951. ToWorldSpacePosition(pos),
  952. axis,
  953. scale * radius,
  954. m_rendState.m_color,
  955. AZ::RPI::AuxGeomDraw::DrawStyle::Line,
  956. m_rendState.m_depthTest,
  957. m_rendState.m_depthWrite,
  958. m_rendState.m_faceCullMode,
  959. m_rendState.m_viewProjOverrideIndex
  960. );
  961. }
  962. }
  963. void AtomDebugDisplayViewportInterface::DrawWireDisk(const AZ::Vector3& pos, const AZ::Vector3& dir, float radius)
  964. {
  965. if (m_auxGeomPtr)
  966. {
  967. // Draw 3 axis aligned circles
  968. const float stepAngle = DegToRad(11.25f);
  969. const float startAngle = DegToRad(0.0f);
  970. const float stopAngle = DegToRad(360.0f);
  971. SingleColorDynamicSizeLineHelper lines(2 + static_cast<int>(360.0f / 11.25f)); // num disk segments + 1 for axis line + 1 for spare
  972. const AZ::Vector3 radiusV3 = AZ::Vector3(radius);
  973. CreateArbitraryAxisArc(
  974. lines,
  975. stepAngle,
  976. startAngle,
  977. stopAngle,
  978. pos,
  979. radiusV3,
  980. dir
  981. );
  982. lines.AddLineSegment(ToWorldSpacePosition(pos), ToWorldSpacePosition(pos + dir * (radius * 0.2f))); // 0.2f comes from Code\Editor\Objects\DisplayContextShared.inl DisplayContext::DrawWireDisk
  983. lines.Draw(m_auxGeomPtr, m_rendState);
  984. }
  985. }
  986. void AtomDebugDisplayViewportInterface::DrawBall(const AZ::Vector3& pos, float radius, bool drawShaded)
  987. {
  988. if (m_auxGeomPtr)
  989. {
  990. // get the max scaled radius in case the transform on the stack is scaled non-uniformly
  991. const float transformedRadiusX = ToWorldSpaceVector(AZ::Vector3(radius, 0.0f, 0.0f)).GetLengthEstimate();
  992. const float transformedRadiusY = ToWorldSpaceVector(AZ::Vector3(0.0f, radius, 0.0f)).GetLengthEstimate();
  993. const float transformedRadiusZ = ToWorldSpaceVector(AZ::Vector3(0.0f, 0.0f, radius)).GetLengthEstimate();
  994. const float maxTransformedRadius =
  995. AZ::GetMax(transformedRadiusX, AZ::GetMax(transformedRadiusY, transformedRadiusZ));
  996. AZ::RPI::AuxGeomDraw::DrawStyle drawStyle = drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid;
  997. m_auxGeomPtr->DrawSphere(
  998. ToWorldSpacePosition(pos),
  999. maxTransformedRadius,
  1000. m_rendState.m_color,
  1001. drawStyle,
  1002. m_rendState.m_depthTest,
  1003. m_rendState.m_depthWrite,
  1004. m_rendState.m_faceCullMode,
  1005. m_rendState.m_viewProjOverrideIndex
  1006. );
  1007. }
  1008. }
  1009. void AtomDebugDisplayViewportInterface::DrawDisk(const AZ::Vector3& pos, const AZ::Vector3& dir, float radius, bool drawShaded)
  1010. {
  1011. if (m_auxGeomPtr)
  1012. {
  1013. const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
  1014. const AZ::Vector3 worldPos = ToWorldSpacePosition(pos);
  1015. const AZ::Vector3 worldDir = ToWorldSpaceVector(dir);
  1016. m_auxGeomPtr->DrawDisk(
  1017. worldPos,
  1018. worldDir,
  1019. scale * radius,
  1020. m_rendState.m_color,
  1021. drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
  1022. m_rendState.m_depthTest,
  1023. m_rendState.m_depthWrite,
  1024. m_rendState.m_faceCullMode,
  1025. m_rendState.m_viewProjOverrideIndex
  1026. );
  1027. }
  1028. }
  1029. void AtomDebugDisplayViewportInterface::DrawArrow(const AZ::Vector3& src, const AZ::Vector3& trg, float headScale, bool dualEndedArrow)
  1030. {
  1031. if (m_auxGeomPtr)
  1032. {
  1033. float f2dScale = 1.0f;
  1034. float arrowLen = 0.4f * headScale;
  1035. float arrowRadius = 0.1f * headScale;
  1036. // if (flags & DISPLAY_2D)
  1037. // {
  1038. // f2dScale = 1.2f * ToWorldSpaceVector(Vec3(1, 0, 0)).GetLength();
  1039. // }
  1040. AZ::Vector3 dir = trg - src;
  1041. dir = ToWorldSpaceVector(dir.GetNormalized());
  1042. AZ::Vector3 verts[2] = {ToWorldSpacePosition(src), ToWorldSpacePosition(trg)};
  1043. AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
  1044. drawArgs.m_verts = verts;
  1045. drawArgs.m_vertCount = 2;
  1046. drawArgs.m_colors = &m_rendState.m_color;
  1047. drawArgs.m_colorCount = 1;
  1048. drawArgs.m_size = m_rendState.m_lineWidth;
  1049. drawArgs.m_opacityType = m_rendState.m_opacityType;
  1050. drawArgs.m_depthTest = m_rendState.m_depthTest;
  1051. drawArgs.m_depthWrite = m_rendState.m_depthWrite;
  1052. drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
  1053. if (!dualEndedArrow)
  1054. {
  1055. verts[1] -= dir * arrowLen;
  1056. m_auxGeomPtr->DrawLines(drawArgs);
  1057. m_auxGeomPtr->DrawCone(
  1058. verts[1],
  1059. dir,
  1060. arrowRadius * f2dScale,
  1061. arrowLen * f2dScale,
  1062. m_rendState.m_color,
  1063. AZ::RPI::AuxGeomDraw::DrawStyle::Shaded,
  1064. m_rendState.m_depthTest,
  1065. m_rendState.m_depthWrite,
  1066. m_rendState.m_faceCullMode,
  1067. m_rendState.m_viewProjOverrideIndex
  1068. );
  1069. }
  1070. else
  1071. {
  1072. verts[0] += dir * arrowLen;
  1073. verts[1] -= dir * arrowLen;
  1074. m_auxGeomPtr->DrawLines(drawArgs);
  1075. m_auxGeomPtr->DrawCone(
  1076. verts[0],
  1077. -dir,
  1078. arrowRadius * f2dScale,
  1079. arrowLen * f2dScale,
  1080. m_rendState.m_color,
  1081. AZ::RPI::AuxGeomDraw::DrawStyle::Shaded,
  1082. m_rendState.m_depthTest,
  1083. m_rendState.m_depthWrite,
  1084. m_rendState.m_faceCullMode,
  1085. m_rendState.m_viewProjOverrideIndex
  1086. );
  1087. m_auxGeomPtr->DrawCone(
  1088. verts[1],
  1089. dir,
  1090. arrowRadius * f2dScale,
  1091. arrowLen * f2dScale,
  1092. m_rendState.m_color,
  1093. AZ::RPI::AuxGeomDraw::DrawStyle::Shaded,
  1094. m_rendState.m_depthTest,
  1095. m_rendState.m_depthWrite,
  1096. m_rendState.m_faceCullMode,
  1097. m_rendState.m_viewProjOverrideIndex
  1098. );
  1099. }
  1100. }
  1101. }
  1102. void AtomDebugDisplayViewportInterface::DrawTextLabel(
  1103. const AZ::Vector3& pos,
  1104. float size,
  1105. const char* text,
  1106. const bool center,
  1107. int srcOffsetX [[maybe_unused]],
  1108. int srcOffsetY [[maybe_unused]])
  1109. {
  1110. // abort draw if draw is invalid or font query interface is missing.
  1111. if (!text || size == 0.0f || !AZ::Interface<AzFramework::FontQueryInterface>::Get())
  1112. {
  1113. return;
  1114. }
  1115. AzFramework::FontDrawInterface* fontDrawInterface = AZ::Interface<AzFramework::FontQueryInterface>::Get()->GetDefaultFontDrawInterface();
  1116. // abort draw if font draw interface is missing
  1117. if (!fontDrawInterface)
  1118. {
  1119. return;
  1120. }
  1121. // if 2d draw need to project pos to screen first
  1122. AzFramework::TextDrawParameters params;
  1123. AZ::RPI::ViewportContextPtr viewportContext = GetViewportContext();
  1124. params.m_drawViewportId = viewportContext->GetId(); // get the viewport ID so default viewport works
  1125. params.m_position = pos;
  1126. params.m_color = m_rendState.m_color;
  1127. params.m_scale = AZ::Vector2(size);
  1128. params.m_hAlign = center ? AzFramework::TextHorizontalAlignment::Center : AzFramework::TextHorizontalAlignment::Left; //! Horizontal text alignment
  1129. params.m_monospace = false; //! disable character proportional spacing
  1130. params.m_depthTest = false; //! Test character against the depth buffer
  1131. params.m_virtual800x600ScreenSize = false; //! Text placement and size are scaled in viewport pixel coordinates
  1132. params.m_scaleWithWindow = false; //! Font gets bigger as the window gets bigger
  1133. params.m_multiline = true; //! text respects ascii newline characters
  1134. fontDrawInterface->DrawScreenAlignedText3d(params, text);
  1135. }
  1136. void AtomDebugDisplayViewportInterface::Draw2dTextLabel(
  1137. float x,
  1138. float y,
  1139. float size,
  1140. const char* text,
  1141. bool center)
  1142. {
  1143. // abort draw if draw is invalid or font query interface is missing.
  1144. if (!text || size == 0.0f || !AZ::Interface<AzFramework::FontQueryInterface>::Get())
  1145. {
  1146. return;
  1147. }
  1148. AzFramework::FontDrawInterface* fontDrawInterface = AZ::Interface<AzFramework::FontQueryInterface>::Get()->GetDefaultFontDrawInterface();
  1149. // abort draw if font draw interface is missing
  1150. if (!fontDrawInterface)
  1151. {
  1152. return;
  1153. }
  1154. // if 2d draw need to project pos to screen first
  1155. AzFramework::TextDrawParameters params;
  1156. AZ::RPI::ViewportContextPtr viewportContext = GetViewportContext();
  1157. const auto dpiScaleFactor = viewportContext->GetDpiScalingFactor();
  1158. params.m_drawViewportId = viewportContext->GetId(); // get the viewport ID so default viewport works
  1159. params.m_position = AZ::Vector3(x * dpiScaleFactor, y * dpiScaleFactor, 1.0f);
  1160. params.m_color = m_rendState.m_color;
  1161. params.m_scale = AZ::Vector2(size);
  1162. params.m_hAlign = center ? AzFramework::TextHorizontalAlignment::Center : AzFramework::TextHorizontalAlignment::Left; //! Horizontal text alignment
  1163. params.m_monospace = false; //! disable character proportional spacing
  1164. params.m_depthTest = false; //! Test character against the depth buffer
  1165. params.m_virtual800x600ScreenSize = false; //! Text placement and size are scaled in viewport pixel coordinates
  1166. params.m_scaleWithWindow = false; //! Font gets bigger as the window gets bigger
  1167. params.m_multiline = true; //! text respects ascii newline characters
  1168. fontDrawInterface->DrawScreenAlignedText2d(params, text);
  1169. }
  1170. void AtomDebugDisplayViewportInterface::DrawTextOn2DBox(
  1171. const AZ::Vector3& pos [[maybe_unused]],
  1172. const char* text [[maybe_unused]],
  1173. float textScale [[maybe_unused]],
  1174. const AZ::Vector4& TextColor [[maybe_unused]],
  1175. const AZ::Vector4& TextBackColor [[maybe_unused]])
  1176. {
  1177. AZ_Assert(false, "Unexpected use of legacy api, please file a feature request with the rendering team to get this implemented!");
  1178. }
  1179. void AtomDebugDisplayViewportInterface::SetLineWidth(float width)
  1180. {
  1181. AZ_Assert(width >= 0.0f && width <= 255.0f, "Width (%f) exceeds allowable range [0 - 255]", width);
  1182. m_rendState.m_lineWidth = static_cast<AZ::u8>(width);
  1183. }
  1184. bool AtomDebugDisplayViewportInterface::IsVisible(const AZ::Aabb& bounds)
  1185. {
  1186. AZ::RPI::ViewportContextPtr viewportContext = GetViewportContext();
  1187. const AZ::Matrix4x4& worldToClip = viewportContext->GetDefaultView()->GetWorldToClipMatrix();
  1188. AZ::Frustum frustum = AZ::Frustum::CreateFromMatrixColumnMajor(worldToClip, Frustum::ReverseDepth::True);
  1189. return frustum.IntersectAabb(bounds) != AZ::IntersectResult::Exterior;
  1190. }
  1191. // int AtomDebugDisplayViewportInterface::SetFillMode(int nFillMode) override;
  1192. float AtomDebugDisplayViewportInterface::GetLineWidth()
  1193. {
  1194. return m_rendState.m_lineWidth;
  1195. }
  1196. float AtomDebugDisplayViewportInterface::GetAspectRatio()
  1197. {
  1198. AZ::RPI::ViewportContextPtr viewportContext = GetViewportContext();
  1199. auto windowSize = viewportContext->GetViewportSize();
  1200. return aznumeric_cast<float>(windowSize.m_width)/aznumeric_cast<float>(windowSize.m_height);
  1201. }
  1202. void AtomDebugDisplayViewportInterface::DepthTestOff()
  1203. {
  1204. m_rendState.m_depthTest = AZ::RPI::AuxGeomDraw::DepthTest::Off;
  1205. }
  1206. void AtomDebugDisplayViewportInterface::DepthTestOn()
  1207. {
  1208. m_rendState.m_depthTest = AZ::RPI::AuxGeomDraw::DepthTest::On;
  1209. }
  1210. void AtomDebugDisplayViewportInterface::DepthWriteOff()
  1211. {
  1212. m_rendState.m_depthWrite = AZ::RPI::AuxGeomDraw::DepthWrite::Off;
  1213. }
  1214. void AtomDebugDisplayViewportInterface::DepthWriteOn()
  1215. {
  1216. m_rendState.m_depthWrite = AZ::RPI::AuxGeomDraw::DepthWrite::On;
  1217. }
  1218. void AtomDebugDisplayViewportInterface::CullOff()
  1219. {
  1220. m_rendState.m_faceCullMode = AZ::RPI::AuxGeomDraw::FaceCullMode::None;
  1221. }
  1222. void AtomDebugDisplayViewportInterface::CullOn()
  1223. {
  1224. m_rendState.m_faceCullMode = AZ::RPI::AuxGeomDraw::FaceCullMode::Back;
  1225. }
  1226. bool AtomDebugDisplayViewportInterface::SetDrawInFrontMode(bool on)
  1227. {
  1228. AZ_UNUSED(on);
  1229. return false;
  1230. }
  1231. AZ::u32 AtomDebugDisplayViewportInterface::GetState()
  1232. {
  1233. return ConvertRenderStateToCry();
  1234. }
  1235. AZ::u32 AtomDebugDisplayViewportInterface::SetState(AZ::u32 state)
  1236. {
  1237. uint32_t currentState = ConvertRenderStateToCry();
  1238. uint32_t changedState = (state & AzFramework::e_PublicParamsMask) ^ currentState;
  1239. if (changedState & AzFramework::e_Mode2D3DMask)
  1240. {
  1241. // this is the only way to turn on 2d Mode under Atom
  1242. if (state & AzFramework::e_Mode2D)
  1243. {
  1244. AZ_Assert((currentState & AzFramework::e_DrawInFrontOn) == 0 && (changedState & AzFramework::e_DrawInFrontOn) == 0, "Atom doesnt support Draw In Front and 2d at the same time");
  1245. m_rendState.m_viewProjOverrideIndex = m_auxGeomPtr->GetOrAdd2DViewProjOverride();
  1246. m_rendState.m_2dMode = true;
  1247. }
  1248. else // switch back to mode 3d
  1249. {
  1250. m_rendState.m_viewProjOverrideIndex = -1;
  1251. m_rendState.m_2dMode = false;
  1252. }
  1253. }
  1254. if (changedState & AzFramework::e_AlphaBlendingMask)
  1255. {
  1256. switch (state&AzFramework::e_AlphaBlendingMask)
  1257. {
  1258. case AzFramework::e_AlphaNone:
  1259. m_rendState.m_opacityType = AZ::RPI::AuxGeomDraw::OpacityType::Opaque;
  1260. break;
  1261. case AzFramework::e_AlphaAdditive:
  1262. [[fallthrough]]; // Additive not currently supported in Atom AuxGeom implementation
  1263. case AzFramework::e_AlphaBlended:
  1264. m_rendState.m_opacityType = AZ::RPI::AuxGeomDraw::OpacityType::Translucent;
  1265. break;
  1266. }
  1267. }
  1268. if (changedState & AzFramework::e_DrawInFrontMask)
  1269. {
  1270. AZ_Assert( // either state is turning DrawInFront off or Mode 2D has to be off
  1271. (state & AzFramework::e_DrawInFrontOn) == 0 ||
  1272. ((currentState & AzFramework::e_Mode2D) == 0 && (changedState & AzFramework::e_Mode2D) == 0),
  1273. "Atom doesnt support Draw In Front and 2d at the same time");
  1274. SetDrawInFrontMode(changedState & AzFramework::e_DrawInFrontOn);
  1275. }
  1276. if (changedState & AzFramework::e_CullModeMask)
  1277. {
  1278. switch (state & AzFramework::e_CullModeMask)
  1279. {
  1280. case AzFramework::e_CullModeNone:
  1281. CullOff();
  1282. break;
  1283. case AzFramework::e_CullModeFront:
  1284. // Currently no other way to set front face culling in DebugDisplayRequestBus
  1285. m_rendState.m_faceCullMode = AZ::RPI::AuxGeomDraw::FaceCullMode::Front;
  1286. break;
  1287. case AzFramework::e_CullModeBack:
  1288. CullOn();
  1289. break;
  1290. }
  1291. }
  1292. if (changedState & AzFramework::e_DepthWriteMask)
  1293. {
  1294. if (state & AzFramework::e_DepthWriteOff)
  1295. {
  1296. DepthWriteOff();
  1297. }
  1298. else
  1299. {
  1300. DepthWriteOn();
  1301. }
  1302. }
  1303. if (changedState & AzFramework::e_DepthTestMask)
  1304. {
  1305. if (state & AzFramework::e_DepthTestOff)
  1306. {
  1307. DepthTestOff();
  1308. }
  1309. else
  1310. {
  1311. DepthTestOn();
  1312. }
  1313. }
  1314. return currentState;
  1315. }
  1316. void AtomDebugDisplayViewportInterface::PushMatrix(const AZ::Transform& tm)
  1317. {
  1318. AZ_Assert(m_rendState.m_currentTransform < RenderState::TransformStackSize, "Exceeded AtomDebugDisplayViewportInterface matrix stack size");
  1319. if (m_rendState.m_currentTransform < RenderState::TransformStackSize)
  1320. {
  1321. m_rendState.m_currentTransform++;
  1322. m_rendState.m_transformStack[m_rendState.m_currentTransform] = m_rendState.m_transformStack[m_rendState.m_currentTransform - 1] * AZ::Matrix3x4::CreateFromTransform(tm);
  1323. }
  1324. }
  1325. void AtomDebugDisplayViewportInterface::PopMatrix()
  1326. {
  1327. AZ_Assert(m_rendState.m_currentTransform > 0, "Underflowed AtomDebugDisplayViewportInterface matrix stack");
  1328. if (m_rendState.m_currentTransform > 0)
  1329. {
  1330. m_rendState.m_currentTransform--;
  1331. }
  1332. }
  1333. void AtomDebugDisplayViewportInterface::PushPremultipliedMatrix(const AZ::Matrix3x4& matrix)
  1334. {
  1335. AZ_Assert(m_rendState.m_currentTransform < RenderState::TransformStackSize, "Exceeded AtomDebugDisplayViewportInterface matrix stack size");
  1336. if (m_rendState.m_currentTransform < RenderState::TransformStackSize)
  1337. {
  1338. m_rendState.m_currentTransform++;
  1339. m_rendState.m_transformStack[m_rendState.m_currentTransform] = matrix;
  1340. }
  1341. }
  1342. AZ::Matrix3x4 AtomDebugDisplayViewportInterface::PopPremultipliedMatrix()
  1343. {
  1344. AZ_Assert(m_rendState.m_currentTransform > 0, "Underflowed AtomDebugDisplayViewportInterface matrix stack");
  1345. if (m_rendState.m_currentTransform > 0)
  1346. {
  1347. m_rendState.m_currentTransform--;
  1348. }
  1349. return m_rendState.m_transformStack[m_rendState.m_currentTransform + 1];
  1350. }
  1351. const AZ::Matrix3x4& AtomDebugDisplayViewportInterface::GetCurrentTransform() const
  1352. {
  1353. return m_rendState.m_transformStack[m_rendState.m_currentTransform];
  1354. }
  1355. AZStd::vector<AZ::Vector3> AtomDebugDisplayViewportInterface::ToWorldSpacePosition(const AZStd::vector<AZ::Vector3>& positions) const
  1356. {
  1357. AZStd::vector<AZ::Vector3> transformedPositions;
  1358. transformedPositions.resize_no_construct(positions.size());
  1359. AZStd::transform(positions.begin(), positions.end(), transformedPositions.begin(), [this](const AZ::Vector3& position) {
  1360. return ToWorldSpacePosition(position);
  1361. });
  1362. return transformedPositions;
  1363. }
  1364. AZStd::vector<AZ::Vector3> AtomDebugDisplayViewportInterface::ToWorldSpaceVector(const AZStd::vector<AZ::Vector3>& vectors) const
  1365. {
  1366. AZStd::vector<AZ::Vector3> transformedVectors;
  1367. transformedVectors.resize_no_construct(vectors.size());
  1368. AZStd::transform(vectors.begin(), vectors.end(), transformedVectors.begin(), [this](const AZ::Vector3& vector) {
  1369. return ToWorldSpaceVector(vector);
  1370. });
  1371. return transformedVectors;
  1372. }
  1373. AZ::RPI::ViewportContextPtr AtomDebugDisplayViewportInterface::GetViewportContext() const
  1374. {
  1375. auto viewContextManager = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
  1376. if (m_defaultInstance)
  1377. {
  1378. return viewContextManager->GetViewportContextByName(viewContextManager->GetDefaultViewportContextName());
  1379. }
  1380. else
  1381. {
  1382. return viewContextManager->GetViewportContextById(m_viewportId);
  1383. }
  1384. }
  1385. uint32_t AtomDebugDisplayViewportInterface::ConvertRenderStateToCry() const
  1386. {
  1387. uint32_t result = 0;
  1388. result |= m_rendState.m_2dMode ? AzFramework::e_Mode2D : AzFramework::e_Mode3D;
  1389. result |= m_rendState.m_opacityType == AZ::RPI::AuxGeomDraw::OpacityType::Opaque ? AzFramework::e_AlphaNone : AzFramework::e_AlphaBlended;
  1390. result |= m_rendState.m_drawInFront ? AzFramework::e_DrawInFrontOn : AzFramework::e_DrawInFrontOff;
  1391. result |= m_rendState.m_depthTest == AZ::RPI::AuxGeomDraw::DepthTest::On ? AzFramework::e_DepthTestOn : AzFramework::e_DepthTestOff;
  1392. result |= m_rendState.m_depthWrite == AZ::RPI::AuxGeomDraw::DepthWrite::On ? AzFramework::e_DepthWriteOn : AzFramework::e_DepthWriteOff;
  1393. switch (m_rendState.m_faceCullMode)
  1394. {
  1395. case AZ::RPI::AuxGeomDraw::FaceCullMode::None:
  1396. result |= AzFramework::e_CullModeNone;
  1397. break;
  1398. case AZ::RPI::AuxGeomDraw::FaceCullMode::Front:
  1399. result |= AzFramework::e_CullModeFront;
  1400. break;
  1401. case AZ::RPI::AuxGeomDraw::FaceCullMode::Back:
  1402. result |= AzFramework::e_CullModeBack;
  1403. break;
  1404. default:
  1405. AZ_Assert(false, "Trying to convert an unknown culling mode to cry!");
  1406. break;
  1407. }
  1408. return result;
  1409. }
  1410. }