1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AzCore/Serialization/SerializeContext.h>
- #include <AzCore/Component/ComponentApplicationBus.h>
- #include <AzCore/std/algorithm.h>
- #include <AzCore/std/containers/array.h>
- #include <AzCore/std/containers/span.h>
- #include <AzCore/Math/Obb.h>
- #include <AzCore/Math/Aabb.h>
- #include <AzCore/Math/Frustum.h>
- #include <AtomDebugDisplayViewportInterface.h>
- #include <Atom/RPI.Public/View.h>
- #include <Atom/RPI.Public/ViewportContextBus.h>
- #include <AzCore/Interface/Interface.h>
- #include <AzFramework/Font/FontInterface.h>
- namespace AZ::AtomBridge
- {
- ////////////////////////////////////////////////////////////////////////
- SingleColorDynamicSizeLineHelper::SingleColorDynamicSizeLineHelper(
- int estimatedNumLineSegments
- )
- {
- m_points.reserve(AZStd::abs(estimatedNumLineSegments) * 2);
- }
- void SingleColorDynamicSizeLineHelper::AddLineSegment(
- const AZ::Vector3& lineStart,
- const AZ::Vector3& lineEnd
- )
- {
- m_points.push_back(lineStart);
- m_points.push_back(lineEnd);
- }
- void SingleColorDynamicSizeLineHelper::Draw(
- AZ::RPI::AuxGeomDrawPtr auxGeomDrawPtr,
- const RenderState& rendState
- ) const
- {
- if (auxGeomDrawPtr && !m_points.empty())
- {
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = m_points.data();
- drawArgs.m_vertCount = aznumeric_cast<uint32_t>(m_points.size());
- drawArgs.m_colors = &rendState.m_color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_size = rendState.m_lineWidth;
- drawArgs.m_opacityType = rendState.m_opacityType;
- drawArgs.m_depthTest = rendState.m_depthTest;
- drawArgs.m_depthWrite = rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = rendState.m_viewProjOverrideIndex;
- auxGeomDrawPtr->DrawLines( drawArgs );
- }
- }
- void SingleColorDynamicSizeLineHelper::Draw2d(
- AZ::RPI::AuxGeomDrawPtr auxGeomDrawPtr,
- const RenderState& rendState
- ) const
- {
- if (auxGeomDrawPtr && !m_points.empty())
- {
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = m_points.data();
- drawArgs.m_vertCount = aznumeric_cast<uint32_t>(m_points.size());
- drawArgs.m_colors = &rendState.m_color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_size = rendState.m_lineWidth;
- drawArgs.m_opacityType = rendState.m_opacityType;
- drawArgs.m_depthTest = rendState.m_depthTest;
- drawArgs.m_depthWrite = rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = auxGeomDrawPtr->GetOrAdd2DViewProjOverride();
- auxGeomDrawPtr->DrawLines( drawArgs );
- }
- }
- void SingleColorDynamicSizeLineHelper::Reset()
- {
- m_points.clear();
- }
- ////////////////////////////////////////////////////////////////////////
- AtomDebugDisplayViewportInterface::AtomDebugDisplayViewportInterface(AZ::RPI::ViewportContextPtr viewportContextPtr)
- {
- ResetRenderState();
- m_viewportId = viewportContextPtr->GetId();
- m_defaultInstance = false;
- auto setupScene = [this](RPI::ScenePtr scene)
- {
- auto viewportContextManager = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
- AZ::RPI::ViewportContextPtr viewportContextPtr = viewportContextManager->GetViewportContextById(m_viewportId);
- InitInternal(scene.get(), viewportContextPtr);
- };
- setupScene(viewportContextPtr->GetRenderScene());
- m_sceneChangeHandler = AZ::RPI::ViewportContext::SceneChangedEvent::Handler(setupScene);
- viewportContextPtr->ConnectSceneChangedHandler(m_sceneChangeHandler);
- }
- AtomDebugDisplayViewportInterface::AtomDebugDisplayViewportInterface(uint32_t defaultInstanceAddress, RPI::Scene* scene)
- {
- ResetRenderState();
- m_viewportId = defaultInstanceAddress;
- m_defaultInstance = true;
- InitInternal(scene, nullptr);
- }
- void AtomDebugDisplayViewportInterface::UpdateAuxGeom(RPI::Scene* scene, AZ::RPI::View* view)
- {
- if (!scene)
- {
- m_auxGeomPtr = nullptr;
- return;
- }
- auto auxGeomFP = scene->GetFeatureProcessor<RPI::AuxGeomFeatureProcessorInterface>();
- if (!auxGeomFP)
- {
- m_auxGeomPtr = nullptr;
- return;
- }
- // default instance draws to all viewports in the default scene
- if (m_defaultInstance || !view)
- {
- m_auxGeomPtr = auxGeomFP->GetDrawQueue();
- }
- else
- {
- // cache the aux geom draw interface for the current view (aka camera)
- m_auxGeomPtr = auxGeomFP->GetOrCreateDrawQueueForView(view);
- }
- }
- void AtomDebugDisplayViewportInterface::InitInternal(RPI::Scene* scene, AZ::RPI::ViewportContextPtr viewportContextPtr)
- {
- AzFramework::DebugDisplayRequestBus::Handler::BusDisconnect(m_viewportId);
- UpdateAuxGeom(scene, viewportContextPtr ? viewportContextPtr->GetDefaultView().get() : nullptr);
- AzFramework::DebugDisplayRequestBus::Handler::BusConnect(m_viewportId);
- if (!m_defaultInstance) // only the per viewport instances need to listen for viewport changes
- {
- AZ::RPI::ViewportContextIdNotificationBus::Handler::BusConnect(viewportContextPtr->GetId());
- }
- }
- void AtomDebugDisplayViewportInterface::OnViewportDefaultViewChanged(AZ::RPI::ViewPtr view)
- {
- ResetRenderState();
- if (!m_defaultInstance)
- {
- // handle viewport update (view change, scene change, etc
- auto viewportContextManager = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
- AZ::RPI::ViewportContextPtr viewportContextPtr = viewportContextManager->GetViewportContextById(m_viewportId);
- UpdateAuxGeom(viewportContextPtr->GetRenderScene().get(), viewportContextPtr->GetDefaultView().get());
- }
- }
- AtomDebugDisplayViewportInterface::~AtomDebugDisplayViewportInterface()
- {
- AzFramework::DebugDisplayRequestBus::Handler::BusDisconnect(m_viewportId);
- AZ::RPI::ViewportContextIdNotificationBus::Handler::BusDisconnect();
- m_viewportId = AzFramework::InvalidViewportId;
- m_auxGeomPtr = nullptr;
- }
- void AtomDebugDisplayViewportInterface::ResetRenderState()
- {
- m_rendState = RenderState();
- for (int index = 0; index < RenderState::TransformStackSize; ++index)
- {
- m_rendState.m_transformStack[index] = AZ::Matrix3x4::Identity();
- }
- }
- void AtomDebugDisplayViewportInterface::SetColor(const AZ::Color& color)
- {
- m_rendState.m_color = color;
- }
- void AtomDebugDisplayViewportInterface::SetAlpha(float a)
- {
- m_rendState.m_color.SetA(a);
- m_rendState.m_opacityType = a < 1.0f ? AZ::RPI::AuxGeomDraw::OpacityType::Translucent : AZ::RPI::AuxGeomDraw::OpacityType::Opaque;
- }
- void AtomDebugDisplayViewportInterface::DrawQuad(
- const AZ::Vector3& p1,
- const AZ::Vector3& p2,
- const AZ::Vector3& p3,
- const AZ::Vector3& p4)
- {
- if (m_auxGeomPtr)
- {
- AZ::Vector3 wsPoints[4] = { ToWorldSpacePosition(p1), ToWorldSpacePosition(p2), ToWorldSpacePosition(p3), ToWorldSpacePosition(p4) };
- AZ::Vector3 triangles[6];
- triangles[0] = wsPoints[0];
- triangles[1] = wsPoints[1];
- triangles[2] = wsPoints[2];
- triangles[3] = wsPoints[2];
- triangles[4] = wsPoints[3];
- triangles[5] = wsPoints[0];
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = triangles;
- drawArgs.m_vertCount = 6;
- drawArgs.m_colors = &m_rendState.m_color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- m_auxGeomPtr->DrawTriangles(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawQuad(float width, float height, bool drawShaded)
- {
- if (!m_auxGeomPtr || width <= 0.0f || height <= 0.0f)
- {
- return;
- }
- m_auxGeomPtr->DrawQuad(
- width,
- height,
- GetCurrentTransform(),
- m_rendState.m_color,
- drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex);
- }
- void AtomDebugDisplayViewportInterface::DrawWireQuad(
- const AZ::Vector3& p1,
- const AZ::Vector3& p2,
- const AZ::Vector3& p3,
- const AZ::Vector3& p4)
- {
- if (m_auxGeomPtr)
- {
- AZ::Vector3 wsPoints[4] = { ToWorldSpacePosition(p1), ToWorldSpacePosition(p2), ToWorldSpacePosition(p3), ToWorldSpacePosition(p4) };
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = wsPoints;
- drawArgs.m_vertCount = 4;
- drawArgs.m_colors = &m_rendState.m_color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- m_auxGeomPtr->DrawPolylines(drawArgs, AZ::RPI::AuxGeomDraw::PolylineEnd::Closed);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireQuad(float width, float height)
- {
- if (!m_auxGeomPtr || width <= 0.0f || height <= 0.0f)
- {
- return;
- }
- m_auxGeomPtr->DrawQuad(
- width,
- height,
- GetCurrentTransform(),
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Line,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex);
- }
- void AtomDebugDisplayViewportInterface::DrawQuadGradient(
- const AZ::Vector3& p1,
- const AZ::Vector3& p2,
- const AZ::Vector3& p3,
- const AZ::Vector3& p4,
- const AZ::Vector4& firstColor,
- const AZ::Vector4& secondColor)
- {
- if (m_auxGeomPtr)
- {
- AZ::Vector3 wsPoints[4] = { ToWorldSpacePosition(p1), ToWorldSpacePosition(p2), ToWorldSpacePosition(p3), ToWorldSpacePosition(p4) };
- AZ::Vector3 triangles[6];
- AZ::Color colors[6];
- triangles[0] = wsPoints[0]; colors[0] = firstColor;
- triangles[1] = wsPoints[1]; colors[1] = firstColor;
- triangles[2] = wsPoints[2]; colors[2] = secondColor;
- triangles[3] = wsPoints[2]; colors[3] = secondColor;
- triangles[4] = wsPoints[3]; colors[4] = secondColor;
- triangles[5] = wsPoints[0]; colors[5] = firstColor;
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = triangles;
- drawArgs.m_vertCount = 6;
- drawArgs.m_colors = colors;
- drawArgs.m_colorCount = 6;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- m_auxGeomPtr->DrawTriangles(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawQuad2dGradient(
- const Vector2& p1,
- const Vector2& p2,
- const Vector2& p3,
- const Vector2& p4,
- float z,
- const Color& firstColor,
- const Color& secondColor)
- {
- if (m_auxGeomPtr)
- {
- Vector3 points[4];
- points[0] = Vector3(p1.GetX(), p1.GetY(), z);
- points[1] = Vector3(p2.GetX(), p1.GetY(), z);
- points[2] = Vector3(p3.GetX(), p3.GetY(), z);
- points[3] = Vector3(p4.GetX(), p4.GetY(), z);
- Vector3 triangles[6];
- Color colors[6];
- triangles[0] = points[0]; colors[0] = firstColor;
- triangles[1] = points[1]; colors[1] = firstColor;
- triangles[2] = points[2]; colors[2] = secondColor;
- triangles[3] = points[2]; colors[3] = secondColor;
- triangles[4] = points[3]; colors[4] = secondColor;
- triangles[5] = points[0]; colors[5] = firstColor;
- RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = triangles;
- drawArgs.m_vertCount = 6;
- drawArgs.m_colors = colors;
- drawArgs.m_colorCount = 6;
- const bool alphaBlend = firstColor.GetA() < 1.0f || secondColor.GetA() < 1.0f;
- drawArgs.m_opacityType = alphaBlend ? RPI::AuxGeomDraw::OpacityType::Translucent : RPI::AuxGeomDraw::OpacityType::Opaque;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_auxGeomPtr->GetOrAdd2DViewProjOverride();
- m_auxGeomPtr->DrawTriangles(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawTri(const AZ::Vector3& p1, const AZ::Vector3& p2, const AZ::Vector3& p3)
- {
- if (m_auxGeomPtr)
- {
- AZ::Vector3 verts[3] = {ToWorldSpacePosition(p1), ToWorldSpacePosition(p2), ToWorldSpacePosition(p3)};
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = verts;
- drawArgs.m_vertCount = 3;
- drawArgs.m_colors = &m_rendState.m_color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- m_auxGeomPtr->DrawTriangles(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawTriangles(const AZStd::vector<AZ::Vector3>& vertices, const AZ::Color& color)
- {
- if (m_auxGeomPtr)
- {
- AZStd::vector<AZ::Vector3> transformedVertices = ToWorldSpacePosition(vertices);
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = transformedVertices.data();
- drawArgs.m_vertCount = aznumeric_cast<uint32_t>(transformedVertices.size());
- drawArgs.m_colors = &color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- m_auxGeomPtr->DrawTriangles(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawTrianglesIndexed(
- const AZStd::vector<AZ::Vector3>& vertices,
- const AZStd::vector<AZ::u32>& indices,
- const AZ::Color& color)
- {
- if (m_auxGeomPtr)
- {
- AZStd::vector<AZ::Vector3> transformedVertices = ToWorldSpacePosition(vertices);
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicIndexedDrawArguments drawArgs;
- drawArgs.m_verts = transformedVertices.data();
- drawArgs.m_vertCount = aznumeric_cast<uint32_t>(transformedVertices.size());
- drawArgs.m_indices = indices.data();
- drawArgs.m_indexCount = aznumeric_cast<uint32_t>(indices.size());
- drawArgs.m_colors = &color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- m_auxGeomPtr->DrawTriangles(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireBox(const AZ::Vector3& min, const AZ::Vector3& max)
- {
- if (m_auxGeomPtr)
- {
- m_auxGeomPtr->DrawAabb(
- AZ::Aabb::CreateFromMinMax(min, max),
- GetCurrentTransform(),
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Line,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- void AtomDebugDisplayViewportInterface::DrawSolidBox(const AZ::Vector3& min, const AZ::Vector3& max)
- {
- if (m_auxGeomPtr)
- {
- m_auxGeomPtr->DrawAabb(
- AZ::Aabb::CreateFromMinMax(min, max),
- GetCurrentTransform(),
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireOBB(
- const AZ::Vector3& center,
- const AZ::Vector3& axisX,
- const AZ::Vector3& axisY,
- const AZ::Vector3& axisZ,
- const AZ::Vector3& halfExtents)
- {
- if (m_auxGeomPtr)
- {
- AZ::Quaternion rotation = AZ::Quaternion::CreateFromMatrix3x3(AZ::Matrix3x3::CreateFromColumns(axisX, axisY, axisZ));
- AZ::Obb obb = AZ::Obb::CreateFromPositionRotationAndHalfLengths(center, rotation, halfExtents);
- m_auxGeomPtr->DrawObb(
- obb,
- AZ::Vector3::CreateZero(),
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Line,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawSolidOBB(
- const AZ::Vector3& center,
- const AZ::Vector3& axisX,
- const AZ::Vector3& axisY,
- const AZ::Vector3& axisZ,
- const AZ::Vector3& halfExtents)
- {
- if (m_auxGeomPtr)
- {
- AZ::Quaternion rotation = AZ::Quaternion::CreateFromMatrix3x3(AZ::Matrix3x3::CreateFromColumns(axisX, axisY, axisZ));
- AZ::Obb obb = AZ::Obb::CreateFromPositionRotationAndHalfLengths(center, rotation, halfExtents);
- m_auxGeomPtr->DrawObb(
- obb,
- AZ::Vector3::CreateZero(),
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawPoint(const AZ::Vector3& p, int nSize)
- {
- if (m_auxGeomPtr)
- {
- AZ::Vector3 wsPoint = ToWorldSpacePosition(p);
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = &wsPoint;
- drawArgs.m_vertCount = 1;
- drawArgs.m_colors = &m_rendState.m_color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_size = aznumeric_cast<uint8_t>(nSize);
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- m_auxGeomPtr->DrawPoints(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawLine(const AZ::Vector3& p1, const AZ::Vector3& p2)
- {
- if (m_auxGeomPtr)
- {
- AZ::Vector3 verts[2] = {ToWorldSpacePosition(p1), ToWorldSpacePosition(p2)};
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = verts;
- drawArgs.m_vertCount = 2;
- drawArgs.m_colors = &m_rendState.m_color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_size = m_rendState.m_lineWidth;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- m_auxGeomPtr->DrawLines(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawLine(const AZ::Vector3& p1, const AZ::Vector3& p2, const AZ::Vector4& col1, const AZ::Vector4& col2)
- {
- if (m_auxGeomPtr)
- {
- AZ::Vector3 verts[2] = {ToWorldSpacePosition(p1), ToWorldSpacePosition(p2)};
- AZ::Color colors[2] = {col1, col2};
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = verts;
- drawArgs.m_vertCount = 2;
- drawArgs.m_colors = colors;
- drawArgs.m_colorCount = 2;
- drawArgs.m_size = m_rendState.m_lineWidth;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- m_auxGeomPtr->DrawLines(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawLines(const AZStd::vector<AZ::Vector3>& lines, const AZ::Color& color)
- {
- if (m_auxGeomPtr)
- {
- AZStd::vector<AZ::Vector3> transformedLines = ToWorldSpacePosition(lines);
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = transformedLines.data();
- drawArgs.m_vertCount = aznumeric_cast<uint32_t>(transformedLines.size());
- drawArgs.m_colors = &color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_size = m_rendState.m_lineWidth;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- m_auxGeomPtr->DrawLines(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawPolyLine(AZStd::span<const AZ::Vector3> points, bool cycled)
- {
- if (m_auxGeomPtr)
- {
- AZStd::vector<AZ::Vector3> wsPoints;
- wsPoints.resize_no_construct(points.size());
- AZStd::transform(points.begin(), points.end(), wsPoints.begin(), [&](auto& pnt) {
- return ToWorldSpacePosition(pnt);
- });
- AZ::RPI::AuxGeomDraw::PolylineEnd polylineEnd = cycled ? AZ::RPI::AuxGeomDraw::PolylineEnd::Closed : AZ::RPI::AuxGeomDraw::PolylineEnd::Open;
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = wsPoints.data();
- drawArgs.m_vertCount = aznumeric_cast <uint32_t>(wsPoints.size());
- drawArgs.m_colors = &m_rendState.m_color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_size = m_rendState.m_lineWidth;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- m_auxGeomPtr->DrawPolylines(drawArgs, polylineEnd);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawPolyLine(const AZ::Vector3* pnts, int numPoints, bool cycled)
- {
- DrawPolyLine(AZStd::span<const AZ::Vector3>(pnts, numPoints), cycled);
- }
- void AtomDebugDisplayViewportInterface::DrawWireQuad2d(const AZ::Vector2& p1, const AZ::Vector2& p2, float z)
- {
- if (m_auxGeomPtr)
- {
- AZ::Vector3 points[4];
- points[0] = AZ::Vector3(p1.GetX(), p1.GetY(), z);
- points[1] = AZ::Vector3(p2.GetX(), p1.GetY(), z);
- points[2] = AZ::Vector3(p2.GetX(), p2.GetY(), z);
- points[3] = AZ::Vector3(p1.GetX(), p2.GetY(), z);
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = points;
- drawArgs.m_vertCount = 4;
- drawArgs.m_colors = &m_rendState.m_color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_size = m_rendState.m_lineWidth;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_auxGeomPtr->GetOrAdd2DViewProjOverride();
- m_auxGeomPtr->DrawPolylines(drawArgs, AZ::RPI::AuxGeomDraw::PolylineEnd::Closed);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawLine2d(const AZ::Vector2& p1, const AZ::Vector2& p2, float z)
- {
- if (m_auxGeomPtr)
- {
- AZ::Vector3 points[2];
- points[0] = AZ::Vector3(p1.GetX(), p1.GetY(), z);
- points[1] = AZ::Vector3(p2.GetX(), p2.GetY(), z);
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = points;
- drawArgs.m_vertCount = 2;
- drawArgs.m_colors = &m_rendState.m_color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_size = m_rendState.m_lineWidth;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_auxGeomPtr->GetOrAdd2DViewProjOverride();
- m_auxGeomPtr->DrawLines(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawLine2dGradient(const AZ::Vector2& p1, const AZ::Vector2& p2, float z, const AZ::Vector4& firstColor, const AZ::Vector4& secondColor)
- {
- if (m_auxGeomPtr)
- {
- AZ::Vector3 points[2];
- points[0] = AZ::Vector3(p1.GetX(), p1.GetY(), z);
- points[1] = AZ::Vector3(p2.GetX(), p2.GetY(), z);
- AZ::Color colors[2] = {firstColor, secondColor};
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = points;
- drawArgs.m_vertCount = 2;
- drawArgs.m_colors = colors;
- drawArgs.m_colorCount = 2;
- drawArgs.m_size = m_rendState.m_lineWidth;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_auxGeomPtr->GetOrAdd2DViewProjOverride();
- m_auxGeomPtr->DrawLines(drawArgs);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireCircle2d(const AZ::Vector2& center, float radius, float z)
- {
- if (m_auxGeomPtr)
- {
- // Draw axis aligned arc
- constexpr float angularStepDegrees = 10.0f;
- constexpr float startAngleDegrees = 0.0f;
- constexpr float sweepAngleDegrees = 360.0f;
- const float stepAngle = DegToRad(angularStepDegrees);
- const float startAngle = DegToRad(startAngleDegrees);
- const float stopAngle = DegToRad(sweepAngleDegrees) + startAngle;
- SingleColorDynamicSizeLineHelper lines(1+static_cast<int>(sweepAngleDegrees/angularStepDegrees));
- float aspectRadius = radius / GetAspectRatio();
- AZ::Vector3 radiusV3 = AZ::Vector3(aspectRadius, radius, radius);
- AZ::Vector3 pos = AZ::Vector3(center.GetX(), center.GetY(), z);
- CreateAxisAlignedArc(
- lines,
- stepAngle,
- startAngle,
- stopAngle,
- pos,
- radiusV3,
- CircleAxis::CircleAxisZ
- );
- lines.Draw2d(m_auxGeomPtr, m_rendState);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawArc(
- const AZ::Vector3& pos,
- float radius,
- float startAngleDegrees,
- float sweepAngleDegrees,
- float angularStepDegrees,
- int referenceAxis)
- {
- if (m_auxGeomPtr)
- {
- // Draw axis aligned arc
- const float stepAngle = DegToRad(angularStepDegrees);
- const float startAngle = DegToRad(startAngleDegrees);
- const float stopAngle = DegToRad(sweepAngleDegrees) + startAngle;
- SingleColorDynamicSizeLineHelper lines(1+static_cast<int>(sweepAngleDegrees/angularStepDegrees));
- AZ::Vector3 radiusV3 = AZ::Vector3(radius);
- CreateAxisAlignedArc(
- lines,
- stepAngle,
- startAngle,
- stopAngle,
- pos,
- radiusV3,
- static_cast<CircleAxis>(referenceAxis)
- );
- lines.Draw(m_auxGeomPtr, m_rendState);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawArc(
- const AZ::Vector3& pos,
- float radius,
- float startAngleDegrees,
- float sweepAngleDegrees,
- float angularStepDegrees,
- const AZ::Vector3& fixedAxis)
- {
- if (m_auxGeomPtr)
- {
- // Draw arbitraty axis arc
- const float stepAngle = DegToRad(angularStepDegrees);
- const float startAngle = DegToRad(startAngleDegrees);
- const float stopAngle = DegToRad(sweepAngleDegrees) + startAngle;
- SingleColorDynamicSizeLineHelper lines(1+static_cast<int>(sweepAngleDegrees/angularStepDegrees));
- AZ::Vector3 radiusV3 = AZ::Vector3(radius);
- CreateArbitraryAxisArc(
- lines,
- stepAngle,
- startAngle,
- stopAngle,
- pos,
- radiusV3,
- fixedAxis
- );
- lines.Draw(m_auxGeomPtr, m_rendState);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawCircle(const AZ::Vector3& pos, float radius, int nUnchangedAxis)
- {
- if (m_auxGeomPtr)
- {
- // Draw circle with default radius.
- const float step = DegToRad(10.0f);
- const float maxAngle = DegToRad(360.0f) + step;
- SingleColorStaticSizeLineHelper<40> lines; // hard code 40 lines until DegToRad is constexpr.
- AZ::Vector3 radiusV3 = AZ::Vector3(radius);
- CreateAxisAlignedArc(
- lines,
- step,
- 0.0f,
- maxAngle,
- pos,
- radiusV3,
- static_cast<CircleAxis>(nUnchangedAxis));
- lines.Draw(m_auxGeomPtr, m_rendState);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawHalfDottedCircle(const AZ::Vector3& pos, float radius, const AZ::Vector3& viewPos, int nUnchangedAxis)
- {
- if (m_auxGeomPtr)
- {
- // Draw circle with single radius.
- const float step = DegToRad(10.0f);
- const float maxAngle = DegToRad(360.0f);
- SingleColorStaticSizeLineHelper<40> lines; // hard code 40 lines until DegToRad is constexpr.
- AZ::Vector3 radiusV3 = AZ::Vector3(radius);
- const AZ::Vector3 worldPos = ToWorldSpacePosition(pos);
- const AZ::Vector3 worldView = ToWorldSpacePosition(viewPos);
- const AZ::Vector3 worldDir = worldView - worldPos;
- CreateAxisAlignedArc(lines, step, 0.0f, maxAngle, pos, radiusV3, static_cast<CircleAxis>(nUnchangedAxis%CircleAxisMax),
- [&worldPos, &worldDir](const AZ::Vector3& lineStart, const AZ::Vector3& lineEnd, int segmentIndex)
- {
- AZ_UNUSED(lineEnd);
- const float dot = (lineStart - worldPos).Dot(worldDir);
- const bool facing = dot > 0.0f;
- // if so skip every other line to produce a dotted effect
- if (facing || segmentIndex % 2 == 0)
- {
- return true;
- }
- return false;
- });
- lines.Draw(m_auxGeomPtr, m_rendState);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireCone(const AZ::Vector3& pos, const AZ::Vector3& dir, float radius, float height)
- {
- if (m_auxGeomPtr)
- {
- const AZ::Vector3 worldPos = ToWorldSpacePosition(pos);
- const AZ::Vector3 worldDir = ToWorldSpaceVector(dir);
- m_auxGeomPtr->DrawCone(
- worldPos,
- worldDir,
- radius,
- height,
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Line,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- void AtomDebugDisplayViewportInterface::DrawSolidCone(const AZ::Vector3& pos, const AZ::Vector3& dir, float radius, float height, bool drawShaded)
- {
- if (m_auxGeomPtr)
- {
- const AZ::Vector3 worldPos = ToWorldSpacePosition(pos);
- const AZ::Vector3 worldDir = ToWorldSpaceVector(dir);
- m_auxGeomPtr->DrawCone(
- worldPos,
- worldDir,
- radius,
- height,
- m_rendState.m_color,
- drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireCylinder(const AZ::Vector3& center, const AZ::Vector3& axis, float radius, float height)
- {
- if (m_auxGeomPtr)
- {
- const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
- const AZ::Vector3 worldCenter = ToWorldSpacePosition(center);
- const AZ::Vector3 worldAxis = ToWorldSpaceVector(axis);
- m_auxGeomPtr->DrawCylinder(
- worldCenter,
- worldAxis,
- scale * radius,
- scale * height,
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Line,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- void AtomDebugDisplayViewportInterface::DrawSolidCylinder(
- const AZ::Vector3& center,
- const AZ::Vector3& axis,
- float radius,
- float height,
- bool drawShaded)
- {
- if (m_auxGeomPtr)
- {
- const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
- const AZ::Vector3 worldCenter = ToWorldSpacePosition(center);
- const AZ::Vector3 worldAxis = ToWorldSpaceVector(axis);
- m_auxGeomPtr->DrawCylinder(
- worldCenter,
- worldAxis,
- scale * radius,
- scale * height,
- m_rendState.m_color,
- drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireCylinderNoEnds(const AZ::Vector3& center, const AZ::Vector3& axis, float radius, float height)
- {
- if (m_auxGeomPtr)
- {
- const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
- const AZ::Vector3 worldCenter = ToWorldSpacePosition(center);
- const AZ::Vector3 worldAxis = ToWorldSpaceVector(axis);
- m_auxGeomPtr->DrawCylinderNoEnds(
- worldCenter,
- worldAxis,
- scale * radius,
- scale * height,
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Line,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- void AtomDebugDisplayViewportInterface::DrawSolidCylinderNoEnds(
- const AZ::Vector3& center,
- const AZ::Vector3& axis,
- float radius,
- float height,
- bool drawShaded)
- {
- if (m_auxGeomPtr)
- {
- const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
- const AZ::Vector3 worldCenter = ToWorldSpacePosition(center);
- const AZ::Vector3 worldAxis = ToWorldSpaceVector(axis);
- m_auxGeomPtr->DrawCylinderNoEnds(
- worldCenter,
- worldAxis,
- scale * radius,
- scale * height,
- m_rendState.m_color,
- drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireCapsule(
- const AZ::Vector3& center,
- const AZ::Vector3& axis,
- float radius,
- float heightStraightSection)
- {
- if (m_auxGeomPtr && radius > FLT_EPSILON && axis.GetLengthSq() > FLT_EPSILON)
- {
- AZ::Vector3 axisNormalized = axis.GetNormalizedEstimate();
- const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
- const AZ::Vector3 worldAxis = ToWorldSpaceVector(axis);
- // Draw cylinder part (if cylinder height is too small, ignore cylinder and just draw both hemispheres)
- if (heightStraightSection > FLT_EPSILON)
- {
- DrawWireCylinderNoEnds(center, axis, scale * radius, scale * heightStraightSection);
- }
- AZ::Vector3 centerToTopCircleCenter = axisNormalized * heightStraightSection * 0.5f;
- // Top hemisphere
- DrawWireHemisphere(center + centerToTopCircleCenter, worldAxis, scale * radius);
- // Bottom hemisphere
- DrawWireHemisphere(center - centerToTopCircleCenter, -worldAxis, scale * radius);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireSphere(const AZ::Vector3& pos, float radius)
- {
- if (m_auxGeomPtr)
- {
- const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
- m_auxGeomPtr->DrawSphere(
- ToWorldSpacePosition(pos),
- scale * radius,
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Line,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireSphere(const AZ::Vector3& pos, const AZ::Vector3 radius)
- {
- if (m_auxGeomPtr)
- {
- // This matches Cry behavior, the DrawWireSphere above may need modifying to use the same approach.
- // Draw 3 axis aligned circles
- const float step = DegToRad(10.0f);
- const float maxAngle = DegToRad(360.0f);
- SingleColorStaticSizeLineHelper<40*3> lines; // hard code to 40 lines * 3 circles until DegToRad is constexpr.
- // Z Axis
- AZ::Vector3 axisRadius(radius.GetX(), radius.GetY(), 0.0f);
- CreateAxisAlignedArc(lines, step, 0.0f, maxAngle, pos, axisRadius, CircleAxisZ);
- // X Axis
- axisRadius = AZ::Vector3(0.0f, radius.GetY(), radius.GetZ());
- CreateAxisAlignedArc(lines, step, 0.0f, maxAngle, pos, axisRadius, CircleAxisX);
- // Y Axis
- axisRadius = AZ::Vector3(radius.GetX(), 0.0f, radius.GetZ());
- CreateAxisAlignedArc(lines, step, 0.0f, maxAngle, pos, axisRadius, CircleAxisY);
- lines.Draw(m_auxGeomPtr, m_rendState);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireHemisphere(const AZ::Vector3& pos, const AZ::Vector3& axis, float radius)
- {
- if (m_auxGeomPtr)
- {
- const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
- m_auxGeomPtr->DrawHemisphere(
- ToWorldSpacePosition(pos),
- axis,
- scale * radius,
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Line,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- void AtomDebugDisplayViewportInterface::DrawWireDisk(const AZ::Vector3& pos, const AZ::Vector3& dir, float radius)
- {
- if (m_auxGeomPtr)
- {
- // Draw 3 axis aligned circles
- const float stepAngle = DegToRad(11.25f);
- const float startAngle = DegToRad(0.0f);
- const float stopAngle = DegToRad(360.0f);
- SingleColorDynamicSizeLineHelper lines(2 + static_cast<int>(360.0f / 11.25f)); // num disk segments + 1 for axis line + 1 for spare
- const AZ::Vector3 radiusV3 = AZ::Vector3(radius);
- CreateArbitraryAxisArc(
- lines,
- stepAngle,
- startAngle,
- stopAngle,
- pos,
- radiusV3,
- dir
- );
- lines.AddLineSegment(ToWorldSpacePosition(pos), ToWorldSpacePosition(pos + dir * (radius * 0.2f))); // 0.2f comes from Code\Editor\Objects\DisplayContextShared.inl DisplayContext::DrawWireDisk
- lines.Draw(m_auxGeomPtr, m_rendState);
- }
- }
- void AtomDebugDisplayViewportInterface::DrawBall(const AZ::Vector3& pos, float radius, bool drawShaded)
- {
- if (m_auxGeomPtr)
- {
- // get the max scaled radius in case the transform on the stack is scaled non-uniformly
- const float transformedRadiusX = ToWorldSpaceVector(AZ::Vector3(radius, 0.0f, 0.0f)).GetLengthEstimate();
- const float transformedRadiusY = ToWorldSpaceVector(AZ::Vector3(0.0f, radius, 0.0f)).GetLengthEstimate();
- const float transformedRadiusZ = ToWorldSpaceVector(AZ::Vector3(0.0f, 0.0f, radius)).GetLengthEstimate();
- const float maxTransformedRadius =
- AZ::GetMax(transformedRadiusX, AZ::GetMax(transformedRadiusY, transformedRadiusZ));
- AZ::RPI::AuxGeomDraw::DrawStyle drawStyle = drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid;
- m_auxGeomPtr->DrawSphere(
- ToWorldSpacePosition(pos),
- maxTransformedRadius,
- m_rendState.m_color,
- drawStyle,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- void AtomDebugDisplayViewportInterface::DrawDisk(const AZ::Vector3& pos, const AZ::Vector3& dir, float radius, bool drawShaded)
- {
- if (m_auxGeomPtr)
- {
- const float scale = GetCurrentTransform().RetrieveScale().GetMaxElement();
- const AZ::Vector3 worldPos = ToWorldSpacePosition(pos);
- const AZ::Vector3 worldDir = ToWorldSpaceVector(dir);
- m_auxGeomPtr->DrawDisk(
- worldPos,
- worldDir,
- scale * radius,
- m_rendState.m_color,
- drawShaded ? AZ::RPI::AuxGeomDraw::DrawStyle::Shaded : AZ::RPI::AuxGeomDraw::DrawStyle::Solid,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- void AtomDebugDisplayViewportInterface::DrawArrow(const AZ::Vector3& src, const AZ::Vector3& trg, float headScale, bool dualEndedArrow)
- {
- if (m_auxGeomPtr)
- {
- float f2dScale = 1.0f;
- float arrowLen = 0.4f * headScale;
- float arrowRadius = 0.1f * headScale;
- // if (flags & DISPLAY_2D)
- // {
- // f2dScale = 1.2f * ToWorldSpaceVector(Vec3(1, 0, 0)).GetLength();
- // }
- AZ::Vector3 dir = trg - src;
- dir = ToWorldSpaceVector(dir.GetNormalized());
- AZ::Vector3 verts[2] = {ToWorldSpacePosition(src), ToWorldSpacePosition(trg)};
- AZ::RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments drawArgs;
- drawArgs.m_verts = verts;
- drawArgs.m_vertCount = 2;
- drawArgs.m_colors = &m_rendState.m_color;
- drawArgs.m_colorCount = 1;
- drawArgs.m_size = m_rendState.m_lineWidth;
- drawArgs.m_opacityType = m_rendState.m_opacityType;
- drawArgs.m_depthTest = m_rendState.m_depthTest;
- drawArgs.m_depthWrite = m_rendState.m_depthWrite;
- drawArgs.m_viewProjectionOverrideIndex = m_rendState.m_viewProjOverrideIndex;
- if (!dualEndedArrow)
- {
- verts[1] -= dir * arrowLen;
- m_auxGeomPtr->DrawLines(drawArgs);
- m_auxGeomPtr->DrawCone(
- verts[1],
- dir,
- arrowRadius * f2dScale,
- arrowLen * f2dScale,
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Shaded,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- else
- {
- verts[0] += dir * arrowLen;
- verts[1] -= dir * arrowLen;
- m_auxGeomPtr->DrawLines(drawArgs);
- m_auxGeomPtr->DrawCone(
- verts[0],
- -dir,
- arrowRadius * f2dScale,
- arrowLen * f2dScale,
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Shaded,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- m_auxGeomPtr->DrawCone(
- verts[1],
- dir,
- arrowRadius * f2dScale,
- arrowLen * f2dScale,
- m_rendState.m_color,
- AZ::RPI::AuxGeomDraw::DrawStyle::Shaded,
- m_rendState.m_depthTest,
- m_rendState.m_depthWrite,
- m_rendState.m_faceCullMode,
- m_rendState.m_viewProjOverrideIndex
- );
- }
- }
- }
- void AtomDebugDisplayViewportInterface::DrawTextLabel(
- const AZ::Vector3& pos,
- float size,
- const char* text,
- const bool center,
- int srcOffsetX [[maybe_unused]],
- int srcOffsetY [[maybe_unused]])
- {
- // abort draw if draw is invalid or font query interface is missing.
- if (!text || size == 0.0f || !AZ::Interface<AzFramework::FontQueryInterface>::Get())
- {
- return;
- }
- AzFramework::FontDrawInterface* fontDrawInterface = AZ::Interface<AzFramework::FontQueryInterface>::Get()->GetDefaultFontDrawInterface();
- // abort draw if font draw interface is missing
- if (!fontDrawInterface)
- {
- return;
- }
- // if 2d draw need to project pos to screen first
- AzFramework::TextDrawParameters params;
- AZ::RPI::ViewportContextPtr viewportContext = GetViewportContext();
- params.m_drawViewportId = viewportContext->GetId(); // get the viewport ID so default viewport works
- params.m_position = pos;
- params.m_color = m_rendState.m_color;
- params.m_scale = AZ::Vector2(size);
- params.m_hAlign = center ? AzFramework::TextHorizontalAlignment::Center : AzFramework::TextHorizontalAlignment::Left; //! Horizontal text alignment
- params.m_monospace = false; //! disable character proportional spacing
- params.m_depthTest = false; //! Test character against the depth buffer
- params.m_virtual800x600ScreenSize = false; //! Text placement and size are scaled in viewport pixel coordinates
- params.m_scaleWithWindow = false; //! Font gets bigger as the window gets bigger
- params.m_multiline = true; //! text respects ascii newline characters
- fontDrawInterface->DrawScreenAlignedText3d(params, text);
- }
- void AtomDebugDisplayViewportInterface::Draw2dTextLabel(
- float x,
- float y,
- float size,
- const char* text,
- bool center)
- {
- // abort draw if draw is invalid or font query interface is missing.
- if (!text || size == 0.0f || !AZ::Interface<AzFramework::FontQueryInterface>::Get())
- {
- return;
- }
- AzFramework::FontDrawInterface* fontDrawInterface = AZ::Interface<AzFramework::FontQueryInterface>::Get()->GetDefaultFontDrawInterface();
- // abort draw if font draw interface is missing
- if (!fontDrawInterface)
- {
- return;
- }
- // if 2d draw need to project pos to screen first
- AzFramework::TextDrawParameters params;
- AZ::RPI::ViewportContextPtr viewportContext = GetViewportContext();
- const auto dpiScaleFactor = viewportContext->GetDpiScalingFactor();
- params.m_drawViewportId = viewportContext->GetId(); // get the viewport ID so default viewport works
- params.m_position = AZ::Vector3(x * dpiScaleFactor, y * dpiScaleFactor, 1.0f);
- params.m_color = m_rendState.m_color;
- params.m_scale = AZ::Vector2(size);
- params.m_hAlign = center ? AzFramework::TextHorizontalAlignment::Center : AzFramework::TextHorizontalAlignment::Left; //! Horizontal text alignment
- params.m_monospace = false; //! disable character proportional spacing
- params.m_depthTest = false; //! Test character against the depth buffer
- params.m_virtual800x600ScreenSize = false; //! Text placement and size are scaled in viewport pixel coordinates
- params.m_scaleWithWindow = false; //! Font gets bigger as the window gets bigger
- params.m_multiline = true; //! text respects ascii newline characters
- fontDrawInterface->DrawScreenAlignedText2d(params, text);
- }
- void AtomDebugDisplayViewportInterface::DrawTextOn2DBox(
- const AZ::Vector3& pos [[maybe_unused]],
- const char* text [[maybe_unused]],
- float textScale [[maybe_unused]],
- const AZ::Vector4& TextColor [[maybe_unused]],
- const AZ::Vector4& TextBackColor [[maybe_unused]])
- {
- AZ_Assert(false, "Unexpected use of legacy api, please file a feature request with the rendering team to get this implemented!");
- }
- void AtomDebugDisplayViewportInterface::SetLineWidth(float width)
- {
- AZ_Assert(width >= 0.0f && width <= 255.0f, "Width (%f) exceeds allowable range [0 - 255]", width);
- m_rendState.m_lineWidth = static_cast<AZ::u8>(width);
- }
- bool AtomDebugDisplayViewportInterface::IsVisible(const AZ::Aabb& bounds)
- {
- AZ::RPI::ViewportContextPtr viewportContext = GetViewportContext();
- const AZ::Matrix4x4& worldToClip = viewportContext->GetDefaultView()->GetWorldToClipMatrix();
- AZ::Frustum frustum = AZ::Frustum::CreateFromMatrixColumnMajor(worldToClip, Frustum::ReverseDepth::True);
- return frustum.IntersectAabb(bounds) != AZ::IntersectResult::Exterior;
- }
- // int AtomDebugDisplayViewportInterface::SetFillMode(int nFillMode) override;
- float AtomDebugDisplayViewportInterface::GetLineWidth()
- {
- return m_rendState.m_lineWidth;
- }
- float AtomDebugDisplayViewportInterface::GetAspectRatio()
- {
- AZ::RPI::ViewportContextPtr viewportContext = GetViewportContext();
- auto windowSize = viewportContext->GetViewportSize();
- return aznumeric_cast<float>(windowSize.m_width)/aznumeric_cast<float>(windowSize.m_height);
- }
- void AtomDebugDisplayViewportInterface::DepthTestOff()
- {
- m_rendState.m_depthTest = AZ::RPI::AuxGeomDraw::DepthTest::Off;
- }
- void AtomDebugDisplayViewportInterface::DepthTestOn()
- {
- m_rendState.m_depthTest = AZ::RPI::AuxGeomDraw::DepthTest::On;
- }
- void AtomDebugDisplayViewportInterface::DepthWriteOff()
- {
- m_rendState.m_depthWrite = AZ::RPI::AuxGeomDraw::DepthWrite::Off;
- }
- void AtomDebugDisplayViewportInterface::DepthWriteOn()
- {
- m_rendState.m_depthWrite = AZ::RPI::AuxGeomDraw::DepthWrite::On;
- }
- void AtomDebugDisplayViewportInterface::CullOff()
- {
- m_rendState.m_faceCullMode = AZ::RPI::AuxGeomDraw::FaceCullMode::None;
- }
- void AtomDebugDisplayViewportInterface::CullOn()
- {
- m_rendState.m_faceCullMode = AZ::RPI::AuxGeomDraw::FaceCullMode::Back;
- }
- bool AtomDebugDisplayViewportInterface::SetDrawInFrontMode(bool on)
- {
- AZ_UNUSED(on);
- return false;
- }
- AZ::u32 AtomDebugDisplayViewportInterface::GetState()
- {
- return ConvertRenderStateToCry();
- }
- AZ::u32 AtomDebugDisplayViewportInterface::SetState(AZ::u32 state)
- {
- uint32_t currentState = ConvertRenderStateToCry();
- uint32_t changedState = (state & AzFramework::e_PublicParamsMask) ^ currentState;
- if (changedState & AzFramework::e_Mode2D3DMask)
- {
- // this is the only way to turn on 2d Mode under Atom
- if (state & AzFramework::e_Mode2D)
- {
- AZ_Assert((currentState & AzFramework::e_DrawInFrontOn) == 0 && (changedState & AzFramework::e_DrawInFrontOn) == 0, "Atom doesnt support Draw In Front and 2d at the same time");
- m_rendState.m_viewProjOverrideIndex = m_auxGeomPtr->GetOrAdd2DViewProjOverride();
- m_rendState.m_2dMode = true;
- }
- else // switch back to mode 3d
- {
- m_rendState.m_viewProjOverrideIndex = -1;
- m_rendState.m_2dMode = false;
- }
- }
- if (changedState & AzFramework::e_AlphaBlendingMask)
- {
- switch (state&AzFramework::e_AlphaBlendingMask)
- {
- case AzFramework::e_AlphaNone:
- m_rendState.m_opacityType = AZ::RPI::AuxGeomDraw::OpacityType::Opaque;
- break;
- case AzFramework::e_AlphaAdditive:
- [[fallthrough]]; // Additive not currently supported in Atom AuxGeom implementation
- case AzFramework::e_AlphaBlended:
- m_rendState.m_opacityType = AZ::RPI::AuxGeomDraw::OpacityType::Translucent;
- break;
- }
- }
- if (changedState & AzFramework::e_DrawInFrontMask)
- {
- AZ_Assert( // either state is turning DrawInFront off or Mode 2D has to be off
- (state & AzFramework::e_DrawInFrontOn) == 0 ||
- ((currentState & AzFramework::e_Mode2D) == 0 && (changedState & AzFramework::e_Mode2D) == 0),
- "Atom doesnt support Draw In Front and 2d at the same time");
- SetDrawInFrontMode(changedState & AzFramework::e_DrawInFrontOn);
- }
- if (changedState & AzFramework::e_CullModeMask)
- {
- switch (state & AzFramework::e_CullModeMask)
- {
- case AzFramework::e_CullModeNone:
- CullOff();
- break;
- case AzFramework::e_CullModeFront:
- // Currently no other way to set front face culling in DebugDisplayRequestBus
- m_rendState.m_faceCullMode = AZ::RPI::AuxGeomDraw::FaceCullMode::Front;
- break;
- case AzFramework::e_CullModeBack:
- CullOn();
- break;
- }
- }
- if (changedState & AzFramework::e_DepthWriteMask)
- {
- if (state & AzFramework::e_DepthWriteOff)
- {
- DepthWriteOff();
- }
- else
- {
- DepthWriteOn();
- }
- }
- if (changedState & AzFramework::e_DepthTestMask)
- {
- if (state & AzFramework::e_DepthTestOff)
- {
- DepthTestOff();
- }
- else
- {
- DepthTestOn();
- }
- }
- return currentState;
- }
- void AtomDebugDisplayViewportInterface::PushMatrix(const AZ::Transform& tm)
- {
- AZ_Assert(m_rendState.m_currentTransform < RenderState::TransformStackSize, "Exceeded AtomDebugDisplayViewportInterface matrix stack size");
- if (m_rendState.m_currentTransform < RenderState::TransformStackSize)
- {
- m_rendState.m_currentTransform++;
- m_rendState.m_transformStack[m_rendState.m_currentTransform] = m_rendState.m_transformStack[m_rendState.m_currentTransform - 1] * AZ::Matrix3x4::CreateFromTransform(tm);
- }
- }
- void AtomDebugDisplayViewportInterface::PopMatrix()
- {
- AZ_Assert(m_rendState.m_currentTransform > 0, "Underflowed AtomDebugDisplayViewportInterface matrix stack");
- if (m_rendState.m_currentTransform > 0)
- {
- m_rendState.m_currentTransform--;
- }
- }
- void AtomDebugDisplayViewportInterface::PushPremultipliedMatrix(const AZ::Matrix3x4& matrix)
- {
- AZ_Assert(m_rendState.m_currentTransform < RenderState::TransformStackSize, "Exceeded AtomDebugDisplayViewportInterface matrix stack size");
- if (m_rendState.m_currentTransform < RenderState::TransformStackSize)
- {
- m_rendState.m_currentTransform++;
- m_rendState.m_transformStack[m_rendState.m_currentTransform] = matrix;
- }
- }
- AZ::Matrix3x4 AtomDebugDisplayViewportInterface::PopPremultipliedMatrix()
- {
- AZ_Assert(m_rendState.m_currentTransform > 0, "Underflowed AtomDebugDisplayViewportInterface matrix stack");
- if (m_rendState.m_currentTransform > 0)
- {
- m_rendState.m_currentTransform--;
- }
- return m_rendState.m_transformStack[m_rendState.m_currentTransform + 1];
- }
- const AZ::Matrix3x4& AtomDebugDisplayViewportInterface::GetCurrentTransform() const
- {
- return m_rendState.m_transformStack[m_rendState.m_currentTransform];
- }
- AZStd::vector<AZ::Vector3> AtomDebugDisplayViewportInterface::ToWorldSpacePosition(const AZStd::vector<AZ::Vector3>& positions) const
- {
- AZStd::vector<AZ::Vector3> transformedPositions;
- transformedPositions.resize_no_construct(positions.size());
- AZStd::transform(positions.begin(), positions.end(), transformedPositions.begin(), [this](const AZ::Vector3& position) {
- return ToWorldSpacePosition(position);
- });
- return transformedPositions;
- }
- AZStd::vector<AZ::Vector3> AtomDebugDisplayViewportInterface::ToWorldSpaceVector(const AZStd::vector<AZ::Vector3>& vectors) const
- {
- AZStd::vector<AZ::Vector3> transformedVectors;
- transformedVectors.resize_no_construct(vectors.size());
- AZStd::transform(vectors.begin(), vectors.end(), transformedVectors.begin(), [this](const AZ::Vector3& vector) {
- return ToWorldSpaceVector(vector);
- });
- return transformedVectors;
- }
- AZ::RPI::ViewportContextPtr AtomDebugDisplayViewportInterface::GetViewportContext() const
- {
- auto viewContextManager = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
- if (m_defaultInstance)
- {
- return viewContextManager->GetViewportContextByName(viewContextManager->GetDefaultViewportContextName());
- }
- else
- {
- return viewContextManager->GetViewportContextById(m_viewportId);
- }
- }
- uint32_t AtomDebugDisplayViewportInterface::ConvertRenderStateToCry() const
- {
- uint32_t result = 0;
- result |= m_rendState.m_2dMode ? AzFramework::e_Mode2D : AzFramework::e_Mode3D;
- result |= m_rendState.m_opacityType == AZ::RPI::AuxGeomDraw::OpacityType::Opaque ? AzFramework::e_AlphaNone : AzFramework::e_AlphaBlended;
- result |= m_rendState.m_drawInFront ? AzFramework::e_DrawInFrontOn : AzFramework::e_DrawInFrontOff;
- result |= m_rendState.m_depthTest == AZ::RPI::AuxGeomDraw::DepthTest::On ? AzFramework::e_DepthTestOn : AzFramework::e_DepthTestOff;
- result |= m_rendState.m_depthWrite == AZ::RPI::AuxGeomDraw::DepthWrite::On ? AzFramework::e_DepthWriteOn : AzFramework::e_DepthWriteOff;
- switch (m_rendState.m_faceCullMode)
- {
- case AZ::RPI::AuxGeomDraw::FaceCullMode::None:
- result |= AzFramework::e_CullModeNone;
- break;
- case AZ::RPI::AuxGeomDraw::FaceCullMode::Front:
- result |= AzFramework::e_CullModeFront;
- break;
- case AZ::RPI::AuxGeomDraw::FaceCullMode::Back:
- result |= AzFramework::e_CullModeBack;
- break;
- default:
- AZ_Assert(false, "Trying to convert an unknown culling mode to cry!");
- break;
- }
- return result;
- }
- }
|