From 68d9c0537735f86050e327be483cc1e66469ef3c Mon Sep 17 00:00:00 2001 From: rhhong Date: Tue, 26 Oct 2021 08:14:34 -0700 Subject: [PATCH] wireframe rendering Signed-off-by: rhhong Signed-off-by: Gene Walters --- .../Code/Source/AtomActorDebugDraw.cpp | 76 +++++++++++++++++++ .../Code/Source/AtomActorDebugDraw.h | 1 + 2 files changed, 77 insertions(+) diff --git a/Gems/AtomLyIntegration/EMotionFXAtom/Code/Source/AtomActorDebugDraw.cpp b/Gems/AtomLyIntegration/EMotionFXAtom/Code/Source/AtomActorDebugDraw.cpp index eaaf04fcf2..836e2c8822 100644 --- a/Gems/AtomLyIntegration/EMotionFXAtom/Code/Source/AtomActorDebugDraw.cpp +++ b/Gems/AtomLyIntegration/EMotionFXAtom/Code/Source/AtomActorDebugDraw.cpp @@ -88,6 +88,10 @@ namespace AZ::Render { RenderTangents(mesh, globalTM); } + if (renderWireframe) + { + RenderWireframe(mesh, globalTM); + } } } } @@ -412,4 +416,76 @@ namespace AZ::Render lineArgs.m_depthTest = RPI::AuxGeomDraw::DepthTest::Off; auxGeom->DrawLines(lineArgs); } + + // Render wireframe mesh + void AtomActorDebugDraw::RenderWireframe(EMotionFX::Mesh* mesh, const AZ::Transform& worldTM) + { + // Check if the mesh is valid and skip the node in case it's not + if (!mesh) + { + return; + } + + RPI::AuxGeomDrawPtr auxGeom = m_auxGeomFeatureProcessor->GetDrawQueue(); + if (!auxGeom) + { + return; + } + + PrepareForMesh(mesh, worldTM); + + const float scale = 0.01f; + + AZ::Vector3* normals = (AZ::Vector3*)mesh->FindVertexData(EMotionFX::Mesh::ATTRIB_NORMALS); + const AZ::Color vertexColor = AZ::Color(0.8f, 0.24f, 0.88f, 1.0f); + + const size_t numSubMeshes = mesh->GetNumSubMeshes(); + for (uint32 subMeshIndex = 0; subMeshIndex < numSubMeshes; ++subMeshIndex) + { + EMotionFX::SubMesh* subMesh = mesh->GetSubMesh(subMeshIndex); + const uint32 numTriangles = subMesh->GetNumPolygons(); + const uint32 startVertex = subMesh->GetStartVertex(); + const uint32* indices = subMesh->GetIndices(); + + m_auxVertices.clear(); + m_auxVertices.reserve(numTriangles * 6); + m_auxColors.clear(); + m_auxColors.reserve(m_auxVertices.size()); + + for (uint32 triangleIndex = 0; triangleIndex < numTriangles; ++triangleIndex) + { + const uint32 triangleStartIndex = triangleIndex * 3; + const uint32 indexA = indices[triangleStartIndex + 0] + startVertex; + const uint32 indexB = indices[triangleStartIndex + 1] + startVertex; + const uint32 indexC = indices[triangleStartIndex + 2] + startVertex; + + const AZ::Vector3 posA = m_worldSpacePositions[indexA] + normals[indexA] * scale; + const AZ::Vector3 posB = m_worldSpacePositions[indexB] + normals[indexB] * scale; + const AZ::Vector3 posC = m_worldSpacePositions[indexC] + normals[indexC] * scale; + + m_auxVertices.emplace_back(posA); + m_auxColors.emplace_back(vertexColor); + m_auxVertices.emplace_back(posB); + m_auxColors.emplace_back(vertexColor); + + m_auxVertices.emplace_back(posB); + m_auxColors.emplace_back(vertexColor); + m_auxVertices.emplace_back(posC); + m_auxColors.emplace_back(vertexColor); + + m_auxVertices.emplace_back(posC); + m_auxColors.emplace_back(vertexColor); + m_auxVertices.emplace_back(posA); + m_auxColors.emplace_back(vertexColor); + } + } + + RPI::AuxGeomDraw::AuxGeomDynamicDrawArguments lineArgs; + lineArgs.m_verts = m_auxVertices.data(); + lineArgs.m_vertCount = static_cast(m_auxVertices.size()); + lineArgs.m_colors = m_auxColors.data(); + lineArgs.m_colorCount = static_cast(m_auxColors.size()); + lineArgs.m_depthTest = RPI::AuxGeomDraw::DepthTest::Off; + auxGeom->DrawLines(lineArgs); + } } // namespace AZ::Render diff --git a/Gems/AtomLyIntegration/EMotionFXAtom/Code/Source/AtomActorDebugDraw.h b/Gems/AtomLyIntegration/EMotionFXAtom/Code/Source/AtomActorDebugDraw.h index 9f8b137f13..797de16351 100644 --- a/Gems/AtomLyIntegration/EMotionFXAtom/Code/Source/AtomActorDebugDraw.h +++ b/Gems/AtomLyIntegration/EMotionFXAtom/Code/Source/AtomActorDebugDraw.h @@ -43,6 +43,7 @@ namespace AZ::Render void RenderEMFXDebugDraw(EMotionFX::ActorInstance* instance); void RenderNormals(EMotionFX::Mesh* mesh, const AZ::Transform& worldTM, bool vertexNormals, bool faceNormals); void RenderTangents(EMotionFX::Mesh* mesh, const AZ::Transform& worldTM); + void RenderWireframe(EMotionFX::Mesh* mesh, const AZ::Transform& worldTM); EMotionFX::Mesh* m_currentMesh = nullptr; /**< A pointer to the mesh whose world space positions are in the pre-calculated positions buffer. NULL in case we haven't pre-calculated any positions yet. */