From d6ae135fd67551a82363b88331c39f3006f1a338 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Wed, 14 Aug 2024 01:28:16 +0200 Subject: [PATCH] Recast: draw height point offset in traverse links Show the actual kink from raycast point to actual link point. --- src/naveditor/Editor.cpp | 5 +++- .../DebugUtils/Include/DetourDebugDraw.h | 7 ++++- .../DebugUtils/Source/DetourDebugDraw.cpp | 28 +++++++++++++++++-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/naveditor/Editor.cpp b/src/naveditor/Editor.cpp index 091ecafe..7c3fb5c3 100644 --- a/src/naveditor/Editor.cpp +++ b/src/naveditor/Editor.cpp @@ -175,6 +175,7 @@ void Editor::resetCommonSettings() m_cellSize = 16.0f; m_cellHeight = 5.85f; + m_traverseLinkParams.cellHeight = m_cellHeight; // todo(amos): check if this applies for all hulls, and check if this is the // actual value used by the game. This seems to generate slopes very close @@ -213,7 +214,9 @@ void Editor::handleCommonSettings() ImGui::Text("Rasterization"); ImGui::SliderFloat("Cell Size", &m_cellSize, 12.1f, 100.0f); - ImGui::SliderFloat("Cell Height", &m_cellHeight, 0.4f, 100.0f); + + if (ImGui::SliderFloat("Cell Height", &m_cellHeight, 0.4f, 100.0f)) + m_traverseLinkParams.cellHeight = m_cellHeight; if (m_geom) { diff --git a/src/thirdparty/recast/DebugUtils/Include/DetourDebugDraw.h b/src/thirdparty/recast/DebugUtils/Include/DetourDebugDraw.h index 184cc4ed..1927b33a 100644 --- a/src/thirdparty/recast/DebugUtils/Include/DetourDebugDraw.h +++ b/src/thirdparty/recast/DebugUtils/Include/DetourDebugDraw.h @@ -49,7 +49,8 @@ struct duDrawTraverseLinkParams duDrawTraverseLinkParams() : traverseLinkType(-1), traverseLinkDistance(-1), - traverseAnimType(-2) + traverseAnimType(-2), + cellHeight(0.0f) {} int traverseLinkType; @@ -58,6 +59,10 @@ struct duDrawTraverseLinkParams // -2 means all, -1 means disjoint poly groups only, anything above // refers to an actual anim type and indexes into the traverse tables. int traverseAnimType; + + // Used to determine the max LOS angle, this information is lost after + // the mesh tile has been build so we have to cache it from the editor. + float cellHeight; }; void duDebugDrawNavMesh(struct duDebugDraw* dd, const dtNavMesh& mesh, const float* offset, unsigned int flags, const duDrawTraverseLinkParams& traverseLinkParams); diff --git a/src/thirdparty/recast/DebugUtils/Source/DetourDebugDraw.cpp b/src/thirdparty/recast/DebugUtils/Source/DetourDebugDraw.cpp index 90fcce58..f07ff553 100644 --- a/src/thirdparty/recast/DebugUtils/Source/DetourDebugDraw.cpp +++ b/src/thirdparty/recast/DebugUtils/Source/DetourDebugDraw.cpp @@ -212,13 +212,37 @@ static void drawTraverseLinks(duDebugDraw* dd, const dtNavMesh& mesh, const dtNa query->getEdgeMidPoint(basePolyRef, link->ref, startPos); query->getEdgeMidPoint(link->ref, basePolyRef, endPos); + const float slopeAngle = rdMathFabsf(rdCalcSlopeAngle(startPos, endPos)); + const float offsetAmount = rdCalcLedgeSpanOffsetAmount(tile->header->walkableRadius, + slopeAngle, rdCalcMaxLOSAngle(tile->header->walkableRadius, traverseLinkParams.cellHeight)); + + const bool startPointHighest = startPos[2] > endPos[2]; + float* highestPos = startPointHighest ? startPos : endPos; + + const dtPolyRef lowPolyRef = startPointHighest ? link->ref : basePolyRef; + const dtPolyRef highPolyRef = startPointHighest ? basePolyRef : link->ref; + + float normal[3]; + query->getEdgeNormal(highPolyRef, lowPolyRef, normal); + + // The offset between the height point and the ray point + // used to account for the ledge span. + const float offsetEndPos[3] = { + highestPos[0] + normal[0] * offsetAmount, + highestPos[1] + normal[1] * offsetAmount, + highestPos[2] + }; + // Unique color for each type. const int col = duIntToCol(link->traverseType, 128); dd->begin(DU_DRAW_LINES, 2.0f, offset); - dd->vertex(startPos, col); - dd->vertex(endPos, col); + const float* targetStartPos = startPointHighest ? offsetEndPos : startPos; + const float* targetEndPos = startPointHighest ? startPos : offsetEndPos; + + dd->vertex(targetStartPos, col); + dd->vertex(targetEndPos, col); const bool hasReverseLink = link->reverseLink != DT_NULL_TRAVERSE_REVERSE_LINK;