@@ -442,6 +442,7 @@ void RecastAdapter::renderRecastNavmesh(const bool isAirgInstance) const {
442442 if (!mesh) {
443443 return ;
444444 }
445+ dtNavMeshQuery* navQuery = sample->getNavMeshQuery ();
445446 for (int tileIndex = 0 ; tileIndex < mesh->getMaxTiles (); tileIndex++) {
446447 const dtMeshTile* tile = mesh->getTile (tileIndex);
447448 if (!tile || !tile->header ) {
@@ -474,14 +475,14 @@ void RecastAdapter::renderRecastNavmesh(const bool isAirgInstance) const {
474475
475476 for (int polyIndex = 0 ; polyIndex < tile->header ->polyCount ; polyIndex++) {
476477 const dtPolyRef polyRef = getPoly (tileIndex, polyIndex);
477- auto edges = getEdges (polyRef);
478+ auto edges = getEdges (navQuery, polyRef);
478479 glBegin (GL_LINE_LOOP);
479480 glVertex3f (edges[0 ].X , edges[0 ].Y , edges[0 ].Z );
480481 glVertex3f (edges[1 ].X , edges[1 ].Y , edges[1 ].Z );
481482 glVertex3f (edges[2 ].X , edges[2 ].Y , edges[2 ].Z );
482483 glEnd ();
483- auto centroid = calculateCentroid (polyRef);
484- renderer.drawText (( " ref: " + std::to_string (polyRef) + " idx: " + std::to_string (polyIndex)). c_str ( ),
484+ auto centroid = calculateCentroid (navQuery, polyRef);
485+ renderer.drawText (" ref: " + std::to_string (polyRef) + " idx: " + std::to_string (polyIndex),
485486 {centroid.X , centroid.Y , centroid.Z }, color);
486487 }
487488 }
@@ -952,7 +953,35 @@ dtPolyRef RecastAdapter::getAdjacentPoly(const dtPolyRef polyRef, const int edge
952953 return 0 ;
953954}
954955
955- void RecastAdapter::doHitTest (const int mx, const int my) {
956+ void RecastAdapter::setMarker (const SceneMeshHitTestResult& result) {
957+ markerPositionSet = true ;
958+ markerPosition[0 ] = result.rayStart [0 ] + (result.rayEnd [0 ] - result.rayStart [0 ]) * result.hitTime ;
959+ markerPosition[1 ] = result.rayStart [1 ] + (result.rayEnd [1 ] - result.rayStart [1 ]) * result.hitTime ;
960+ markerPosition[2 ] = result.rayStart [2 ] + (result.rayEnd [2 ] - result.rayStart [2 ]) * result.hitTime ;
961+ for (auto [object, vertexRange] : SceneMesh::getInstance ().objectTriangleRanges ) {
962+ if (result.hitIndex >= vertexRange.first && result.hitIndex < vertexRange.second ) {
963+ selectedObject = object;
964+ break ;
965+ }
966+ }
967+ std::string meshNameString;
968+ std::string roomString;
969+ if (Scene::getInstance ().sceneLoaded ) {
970+ if (const auto mesh = Scene::getInstance ().findMeshByHashAndIdAndPos (
971+ selectedObject.substr (0 , 16 ), selectedObject.substr (17 , 16 ), markerPosition); mesh != nullptr ) {
972+ meshNameString = mesh->entity .name ;
973+ roomString = " Room Folder: " + mesh->roomFolderName + " Room: " + mesh->roomName ;
974+ }
975+ }
976+ Logger::log (
977+ NK_INFO,
978+ (" Selected Object: '" + meshNameString + " ' Mesh: '" + selectedObject + " ' Obj vertex: " +
979+ std::to_string (result.hitIndex ) + roomString +
980+ " . Setting marker position to: " + std::to_string (markerPosition[0 ]) + " , " +
981+ std::to_string (markerPosition[1 ]) + " , " + std::to_string (markerPosition[2 ])).c_str ());
982+ }
983+
984+ SceneMeshHitTestResult RecastAdapter::doHitTest (const int mx, const int my) {
956985 float rayStart[3 ];
957986 float rayEnd[3 ];
958987 float hitTime;
@@ -966,40 +995,23 @@ void RecastAdapter::doHitTest(const int mx, const int my) {
966995 rayEnd[0 ] = (float )x;
967996 rayEnd[1 ] = (float )y;
968997 rayEnd[2 ] = (float )z;
969- const int hit = inputGeom->raycastMesh (rayStart, rayEnd, hitTime);
970- if (hit != -1 ) {
971- // Marker
972- markerPositionSet = true ;
973- markerPosition[0 ] = rayStart[0 ] + (rayEnd[0 ] - rayStart[0 ]) * hitTime;
974- markerPosition[1 ] = rayStart[1 ] + (rayEnd[1 ] - rayStart[1 ]) * hitTime;
975- markerPosition[2 ] = rayStart[2 ] + (rayEnd[2 ] - rayStart[2 ]) * hitTime;
976- for (auto [object, vertexRange] : SceneMesh::getInstance ().objectTriangleRanges ) {
977- if (hit >= vertexRange.first && hit < vertexRange.second ) {
978- selectedObject = object;
979- break ;
980- }
981- }
982- std::string meshNameString;
983- std::string roomString;
984- if (Scene::getInstance ().sceneLoaded ) {
985- if (const auto mesh = Scene::getInstance ().findMeshByHashAndIdAndPos (
986- selectedObject.substr (0 , 16 ), selectedObject.substr (17 , 16 ), markerPosition); mesh != nullptr ) {
987- meshNameString = mesh->entity .name ;
988- roomString = " Room Folder: " + mesh->roomFolderName + " Room: " + mesh->roomName ;
989- }
990- }
991- Logger::log (
992- NK_INFO,
993- (" Selected Object: '" + meshNameString + " ' Mesh: '" + selectedObject + " ' Obj vertex: " +
994- std::to_string (hit) + roomString +
995- " . Setting marker position to: " + std::to_string (markerPosition[0 ]) + " , " +
996- std::to_string (markerPosition[1 ]) + " , " + std::to_string (markerPosition[2 ])).c_str ());
997- } else {
998- if (SDL_GetModState ()) {
999- // Marker
1000- markerPositionSet = false ;
1001- }
998+ SceneMeshHitTestResult result;
999+ if (const int hitIndex = inputGeom->raycastMesh (rayStart, rayEnd, hitTime); hitIndex != -1 ) {
1000+ result.hitIndex = hitIndex;
1001+ result.rayStart [0 ] = rayStart[0 ];
1002+ result.rayStart [1 ] = rayStart[1 ];
1003+ result.rayStart [2 ] = rayStart[2 ];
1004+ result.rayEnd [0 ] = rayEnd[0 ];
1005+ result.rayEnd [1 ] = rayEnd[1 ];
1006+ result.rayEnd [2 ] = rayEnd[2 ];
1007+ result.hitTime = hitTime;
1008+ return result;
1009+ }
1010+ result.hitIndex = -1 ;
1011+ if (SDL_GetModState ()) {
1012+ markerPositionSet = false ;
10021013 }
1014+ return result;
10031015}
10041016
10051017void RecastAdapter::loadSettings () const {
@@ -1085,8 +1097,16 @@ Vec3 RecastAdapter::convertFromRecastToNavPower(Vec3 pos) {
10851097 return {pos.X , -pos.Z , pos.Y };
10861098}
10871099
1088- std::vector<Vec3> RecastAdapter::getEdges (const dtPolyRef polyRef) const {
1089- const dtNavMesh* mesh = sample->getNavMesh ();
1100+ std::vector<Vec3> RecastAdapter::getEdges (const dtNavMeshQuery* navQuery, const dtPolyRef polyRef) {
1101+ if (!navQuery) {
1102+ return {};
1103+ }
1104+
1105+ const dtNavMesh* mesh = navQuery->getAttachedNavMesh ();
1106+ if (!mesh) {
1107+ return {};
1108+ }
1109+
10901110 unsigned int salt = 0 ;
10911111 unsigned int tileIndex = 0 ;
10921112 unsigned int polyIndex = 0 ;
@@ -1106,8 +1126,11 @@ std::vector<Vec3> RecastAdapter::getEdges(const dtPolyRef polyRef) const {
11061126 return edges;
11071127}
11081128
1109- Vec3 RecastAdapter::calculateNormal (const dtPolyRef polyRef) const {
1110- const std::vector<Vec3> edges = getEdges (polyRef);
1129+ Vec3 RecastAdapter::calculateNormal (dtNavMeshQuery* navQuery, const dtPolyRef polyRef) const {
1130+ const std::vector<Vec3> edges = getEdges (navQuery, polyRef);
1131+ if (edges.size () < 3 ) {
1132+ return {0 .0f , 1 .0f , 0 .0f };
1133+ }
11111134 const Vec3 v0 = edges.at (0 );
11121135 const Vec3 v1 = edges.at (1 );
11131136 const Vec3 v2 = edges.at (2 );
@@ -1118,9 +1141,13 @@ Vec3 RecastAdapter::calculateNormal(const dtPolyRef polyRef) const {
11181141 return cross.GetUnitVec ();
11191142}
11201143
1121- Vec3 RecastAdapter::calculateCentroid (const dtPolyRef polyRef) const {
1122- const std::vector<Vec3> edges = getEdges (polyRef);
1123- const Vec3 normal = calculateNormal (polyRef);
1144+ Vec3 RecastAdapter::calculateCentroid (dtNavMeshQuery* navQuery, const dtPolyRef polyRef) const {
1145+ const std::vector<Vec3> edges = getEdges (navQuery, polyRef);
1146+ if (edges.empty ()) {
1147+ return {0 .0f , 0 .0f , 0 .0f };
1148+ }
1149+
1150+ const Vec3 normal = calculateNormal (navQuery, polyRef);
11241151 const Vec3 v0 = edges.at (0 );
11251152 const Vec3 v1 = edges.at (1 );
11261153
0 commit comments