Skip to content

Commit

Permalink
Adding tiles to be ignored if don't have relevant information
Browse files Browse the repository at this point in the history
  • Loading branch information
bernatx committed Oct 23, 2023
1 parent 25c3768 commit 698e061
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 72 deletions.
10 changes: 5 additions & 5 deletions RecastBuilder/Include/Sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ class Sample
bool m_filterLowHangingObstacles;
bool m_filterLedgeSpans;
bool m_filterWalkableLowHeightSpans;

SampleTool* m_tool;
SampleToolState* m_toolStates[MAX_TOOLS];

BuildContext* m_ctx;

dtNavMesh* loadAll(const char* path);
Expand All @@ -130,9 +130,9 @@ class Sample
public:
Sample();
virtual ~Sample();

void setContext(BuildContext* ctx) { m_ctx = ctx; }

void setTool(SampleTool* tool);
SampleToolState* getToolState(int type) { return m_toolStates[type]; }
void setToolState(int type, SampleToolState* s) { m_toolStates[type] = s; }
Expand All @@ -157,7 +157,7 @@ class Sample
virtual float getAgentRadius() { return m_agentRadius; }
virtual float getAgentHeight() { return m_agentHeight; }
virtual float getAgentClimb() { return m_agentMaxClimb; }

void updateToolStates(const float dt);
void initToolStates(Sample* sample);
void resetToolStates();
Expand Down
26 changes: 13 additions & 13 deletions RecastBuilder/Include/Sample_TileMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class Sample_TileMesh : public Sample
rcContourSet* m_cset;
rcPolyMesh* m_pmesh;
rcPolyMeshDetail* m_dmesh;
rcConfig m_cfg;
rcConfig m_cfg;

enum DrawMode
{
DRAWMODE_NAVMESH,
Expand All @@ -58,34 +58,34 @@ class Sample_TileMesh : public Sample
DRAWMODE_BOTH_CONTOURS,
DRAWMODE_CONTOURS,
DRAWMODE_POLYMESH,
DRAWMODE_POLYMESH_DETAIL,
DRAWMODE_POLYMESH_DETAIL,
MAX_DRAWMODE
};

DrawMode m_drawMode;

int m_maxTiles;
int m_maxPolysPerTile;
float m_tileSize;

unsigned int m_tileCol;
float m_lastBuiltTileBmin[3];
float m_lastBuiltTileBmax[3];
float m_tileBuildTime;
float m_tileMemUsage;
int m_tileTriCount;

unsigned char* buildTileMesh(const int tx, const int ty, const float* bmin, const float* bmax, int& dataSize);
unsigned char* buildTileMesh(const int tx, const int ty, const float* bmin, const float* bmax, int& dataSize, bool &canBeIgnored);

void cleanup();

void saveAll(const char* path, const dtNavMesh* mesh);
dtNavMesh* loadAll(const char* path);

public:
Sample_TileMesh();
virtual ~Sample_TileMesh();

virtual void handleSettings();
virtual void handleTools();
virtual void handleDebugMode();
Expand All @@ -94,9 +94,9 @@ class Sample_TileMesh : public Sample
virtual void handleMeshChanged(class InputGeom* geom);
virtual bool handleBuild();
virtual void collectSettings(struct BuildSettings& settings);

void getTilePos(const float* pos, int& tx, int& ty);

void buildTile(const float* pos);
void removeTile(const float* pos);
void buildAllTiles();
Expand Down
68 changes: 32 additions & 36 deletions RecastBuilder/Source/InputGeom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// 3. This notice may not be removed or altered from any source distribution.
//

#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
#include <ctype.h>
Expand All @@ -40,34 +39,34 @@ static bool intersectSegmentTriangle(const float* sp, const float* sq,
rcVsub(ab, b, a);
rcVsub(ac, c, a);
rcVsub(qp, sp, sq);

// Compute triangle normal. Can be precalculated or cached if
// intersecting multiple segments against the same triangle
rcVcross(norm, ab, ac);

// Compute denominator d. If d <= 0, segment is parallel to or points
// away from triangle, so exit early
float d = rcVdot(qp, norm);
if (d <= 0.0f) return false;

// Compute intersection t value of pq with plane of triangle. A ray
// intersects iff 0 <= t. Segment intersects iff 0 <= t <= 1. Delay
// dividing by d until intersection has been found to pierce triangle
rcVsub(ap, sp, a);
t = rcVdot(ap, norm);
if (t < 0.0f) return false;
if (t > d) return false; // For segment; exclude this code line for a ray test

// Compute barycentric coordinate components and test if within bounds
rcVcross(e, qp, ap);
v = rcVdot(ac, e);
if (v < 0.0f || v > d) return false;
w = -rcVdot(ab, e);
if (w < 0.0f || v + w > d) return false;

// Segment/ray intersects triangle. Perform delayed division
t /= d;

return true;
}

Expand Down Expand Up @@ -122,7 +121,7 @@ InputGeom::~InputGeom()
delete m_chunkyMesh;
delete m_mesh;
}

bool InputGeom::loadMesh(rcContext* ctx, const std::string& filepath)
{
if (m_mesh)
Expand All @@ -134,7 +133,7 @@ bool InputGeom::loadMesh(rcContext* ctx, const std::string& filepath)
}
m_offMeshConCount = 0;
m_volumeCount = 0;

m_mesh = new rcMeshLoaderObj;
if (!m_mesh)
{
Expand All @@ -159,7 +158,7 @@ bool InputGeom::loadMesh(rcContext* ctx, const std::string& filepath)
{
ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Failed to build chunky mesh.");
return false;
}
}

return true;
}
Expand Down Expand Up @@ -202,7 +201,7 @@ bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath)
delete[] buf;
return false;
}

m_offMeshConCount = 0;
m_volumeCount = 0;
delete m_mesh;
Expand Down Expand Up @@ -292,9 +291,9 @@ bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath)
&m_buildSettings.tileSize);
}
}

delete [] buf;

return true;
}

Expand All @@ -318,7 +317,7 @@ bool InputGeom::load(rcContext* ctx, const std::string& filepath)
bool InputGeom::saveGeomSet(const BuildSettings* settings)
{
if (!m_mesh) return false;

// Change extension
std::string filepath = m_mesh->getFileName();
size_t extPos = filepath.find_last_of('.');
Expand All @@ -329,7 +328,7 @@ bool InputGeom::saveGeomSet(const BuildSettings* settings)

FILE* fp = fopen(filepath.c_str(), "w");
if (!fp) return false;

// Store mesh filename.
fprintf(fp, "f %s\n", m_mesh->getFileName().c_str());

Expand Down Expand Up @@ -360,7 +359,7 @@ bool InputGeom::saveGeomSet(const BuildSettings* settings)
settings->navMeshBMax[2],
settings->tileSize);
}

// Store off-mesh links.
for (int i = 0; i < m_offMeshConCount; ++i)
{
Expand All @@ -381,9 +380,9 @@ bool InputGeom::saveGeomSet(const BuildSettings* settings)
for (int j = 0; j < vol->nverts; ++j)
fprintf(fp, "%f %f %f\n", vol->verts[j*3+0], vol->verts[j*3+1], vol->verts[j*3+2]);
}

fclose(fp);

return true;
}

Expand All @@ -392,14 +391,14 @@ static bool isectSegAABB(const float* sp, const float* sq,
float& tmin, float& tmax)
{
static const float EPS = 1e-6f;

float d[3];
d[0] = sq[0] - sp[0];
d[1] = sq[1] - sp[1];
d[2] = sq[2] - sp[2];
tmin = 0.0;
tmax = 1.0f;

for (int i = 0; i < 3; i++)
{
if (fabsf(d[i]) < EPS)
Expand All @@ -418,16 +417,13 @@ static bool isectSegAABB(const float* sp, const float* sq,
if (tmin > tmax) return false;
}
}

return true;
}


bool InputGeom::raycastMesh(float* src, float* dst, float& tmin)
{
float dir[3];
rcVsub(dir, dst, src);

// Prune hit ray.
float btmin, btmax;
if (!isectSegAABB(src, dst, m_meshBMin, m_meshBMax, btmin, btmax))
Expand All @@ -437,16 +433,16 @@ bool InputGeom::raycastMesh(float* src, float* dst, float& tmin)
p[1] = src[2] + (dst[2]-src[2])*btmin;
q[0] = src[0] + (dst[0]-src[0])*btmax;
q[1] = src[2] + (dst[2]-src[2])*btmax;

int cid[512];
const int ncid = rcGetChunksOverlappingSegment(m_chunkyMesh, p, q, cid, 512);
if (!ncid)
return false;

tmin = 1.0f;
bool hit = false;
const float* verts = m_mesh->getVerts();

for (int i = 0; i < ncid; ++i)
{
const rcChunkyTriMeshNode& node = m_chunkyMesh->nodes[cid[i]];
Expand All @@ -467,7 +463,7 @@ bool InputGeom::raycastMesh(float* src, float* dst, float& tmin)
}
}
}

return hit;
}

Expand Down Expand Up @@ -512,10 +508,10 @@ void InputGeom::drawOffMeshConnections(duDebugDraw* dd, bool hilight)

dd->vertex(v[0],v[1],v[2], baseColor);
dd->vertex(v[0],v[1]+0.2f,v[2], baseColor);

dd->vertex(v[3],v[4],v[5], baseColor);
dd->vertex(v[3],v[4]+0.2f,v[5], baseColor);

duAppendCircle(dd, v[0],v[1]+0.1f,v[2], m_offMeshConRads[i], baseColor);
duAppendCircle(dd, v[3],v[4]+0.1f,v[5], m_offMeshConRads[i], baseColor);

Expand All @@ -524,7 +520,7 @@ void InputGeom::drawOffMeshConnections(duDebugDraw* dd, bool hilight)
duAppendArc(dd, v[0],v[1],v[2], v[3],v[4],v[5], 0.25f,
(m_offMeshConDirs[i]&1) ? 0.6f : 0.0f, 0.6f, conColor);
}
}
}
dd->end();

dd->depthMask(true);
Expand Down Expand Up @@ -554,7 +550,7 @@ void InputGeom::drawConvexVolumes(struct duDebugDraw* dd, bool /*hilight*/)
dd->depthMask(false);

dd->begin(DU_DRAW_TRIS);

for (int i = 0; i < m_volumeCount; ++i)
{
const ConvexVolume* vol = &m_volumes[i];
Expand All @@ -567,7 +563,7 @@ void InputGeom::drawConvexVolumes(struct duDebugDraw* dd, bool /*hilight*/)
dd->vertex(vol->verts[0],vol->hmax,vol->verts[2], col);
dd->vertex(vb[0],vol->hmax,vb[2], col);
dd->vertex(va[0],vol->hmax,va[2], col);

dd->vertex(va[0],vol->hmin,va[2], duDarkenCol(col));
dd->vertex(va[0],vol->hmax,va[2], col);
dd->vertex(vb[0],vol->hmax,vb[2], col);
Expand All @@ -577,7 +573,7 @@ void InputGeom::drawConvexVolumes(struct duDebugDraw* dd, bool /*hilight*/)
dd->vertex(vb[0],vol->hmin,vb[2], duDarkenCol(col));
}
}

dd->end();

dd->begin(DU_DRAW_LINES, 2.0f);
Expand Down Expand Up @@ -612,7 +608,7 @@ void InputGeom::drawConvexVolumes(struct duDebugDraw* dd, bool /*hilight*/)
}
}
dd->end();


dd->depthMask(true);
}
4 changes: 2 additions & 2 deletions RecastBuilder/Source/Sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ void Sample::collectSettings(BuildSettings& settings)

void Sample::resetCommonSettings()
{
m_cellSize = 0.3f;
m_cellSize = 0.2f;
m_cellHeight = 0.01f;
m_agentHeight = 1.8f;
m_agentRadius = 0.4f;
m_agentRadius = 0.2f;
m_agentMaxClimb = 0.3f;
m_agentMaxSlope = 45.0f;
m_regionMinSize = 8;
Expand Down
Loading

0 comments on commit 698e061

Please sign in to comment.