This commit is contained in:
aozhiwei 2020-08-13 20:00:52 +08:00
parent 8229584cbe
commit 0223227a47

View File

@ -297,6 +297,7 @@ void NavMeshBuilder::UnInit()
struct BuilderParams struct BuilderParams
{ {
float kCellSize = 64; float kCellSize = 64;
float kCellHeight = 64;
const float* bmin = nullptr; const float* bmin = nullptr;
const float* bmax = nullptr; const float* bmax = nullptr;
rcConfig cfg; rcConfig cfg;
@ -305,6 +306,8 @@ struct BuilderParams
LinearAllocator* talloc = nullptr; LinearAllocator* talloc = nullptr;
FastLZCompressor* tcomp = nullptr; FastLZCompressor* tcomp = nullptr;
MeshProcess* tmproc = nullptr; MeshProcess* tmproc = nullptr;
int tw = 0;
int th = 0;
}; };
dtNavMesh* NavMeshBuilder::Build(MapInstance* map_instance) dtNavMesh* NavMeshBuilder::Build(MapInstance* map_instance)
@ -348,32 +351,29 @@ dtNavMesh* NavMeshBuilder::Build(MapInstance* map_instance)
int m_cacheCompressedSize = 0; int m_cacheCompressedSize = 0;
int m_cacheRawSize = 0; int m_cacheRawSize = 0;
for (int y = 0; y < th; ++y) for (int y = 0; y < th; ++y) {
{ for (int x = 0; x < tw; ++x) {
for (int x = 0; x < tw; ++x) TileCacheData tiles[MAX_LAYERS];
{ memset(tiles, 0, sizeof(tiles));
TileCacheData tiles[MAX_LAYERS]; int ntiles = RasterizeTileLayers(x, y, builder_params.cfg, tiles, MAX_LAYERS);
memset(tiles, 0, sizeof(tiles));
int ntiles = RasterizeTileLayers(x, y, builder_params.cfg, tiles, MAX_LAYERS);
for (int i = 0; i < ntiles; ++i) for (int i = 0; i < ntiles; ++i) {
{ TileCacheData* tile = &tiles[i];
TileCacheData* tile = &tiles[i]; status = tile_cache->addTile(tile->data, tile->dataSize, DT_COMPRESSEDTILE_FREE_DATA, 0);
status = tile_cache->addTile(tile->data, tile->dataSize, DT_COMPRESSEDTILE_FREE_DATA, 0); if (dtStatusFailed(status))
if (dtStatusFailed(status)) {
{ dtFree(tile->data);
dtFree(tile->data); tile->data = 0;
tile->data = 0; continue;
continue; }
}
m_cacheLayerCount++; m_cacheLayerCount++;
m_cacheCompressedSize += tile->dataSize; m_cacheCompressedSize += tile->dataSize;
m_cacheRawSize += calcLayerBufferSize(builder_params.tcparams.width, m_cacheRawSize += calcLayerBufferSize(builder_params.tcparams.width,
builder_params.tcparams.height); builder_params.tcparams.height);
} }
} }
} }
// Build initial meshes // Build initial meshes
for (int y = 0; y < th; ++y) { for (int y = 0; y < th; ++y) {
@ -382,11 +382,6 @@ dtNavMesh* NavMeshBuilder::Build(MapInstance* map_instance)
} }
} }
#if 0
m_cacheBuildTimeMs = ctx->getAccumulatedTime(RC_TIMER_TOTAL)/1000.0f;
m_cacheBuildMemUsage = builder_params.talloc->high;
#endif
const dtNavMesh* nav = navmesh; const dtNavMesh* nav = navmesh;
int navmeshMemUsage = 0; int navmeshMemUsage = 0;
for (int i = 0; i < nav->getMaxTiles(); ++i) { for (int i = 0; i < nav->getMaxTiles(); ++i) {
@ -502,15 +497,15 @@ void NavMeshBuilder::OutputObjFile(MapInstance* map_instance)
void NavMeshBuilder::InitRcConfig(BuilderParams& builder_params) void NavMeshBuilder::InitRcConfig(BuilderParams& builder_params)
{ {
#if 0 rcConfig& cfg = builder_params.cfg;
memset(&cfg, 0, sizeof(cfg)); memset(&cfg, 0, sizeof(cfg));
cfg.cs = kCellSize; cfg.cs = builder_params.kCellSize;
cfg.ch = kCellHeight; cfg.ch = builder_params.kCellHeight;
cfg.walkableSlopeAngle = kAgentMaxSlope; cfg.walkableSlopeAngle = kAgentMaxSlope;
cfg.walkableHeight = (int)ceilf(kAgentHeight / cfg.ch); cfg.walkableHeight = (int)ceilf(kAgentHeight / cfg.ch);
cfg.walkableClimb = (int)floorf(kAgentMaxClimb / cfg.ch); cfg.walkableClimb = (int)floorf(kAgentMaxClimb / cfg.ch);
cfg.walkableRadius = (int)ceilf(kAgentRadius / cfg.cs); cfg.walkableRadius = (int)ceilf(kAgentRadius / cfg.cs);
cfg.maxEdgeLen = (int)(kEdgeMaxLen / kCellSize); cfg.maxEdgeLen = (int)(kEdgeMaxLen / builder_params.kCellSize);
cfg.maxSimplificationError = kEdgeMaxError; cfg.maxSimplificationError = kEdgeMaxError;
cfg.minRegionArea = (int)rcSqr(kRegionMinSize); // Note: area = size*size cfg.minRegionArea = (int)rcSqr(kRegionMinSize); // Note: area = size*size
cfg.mergeRegionArea = (int)rcSqr(kRegionMergeSize); // Note: area = size*size cfg.mergeRegionArea = (int)rcSqr(kRegionMergeSize); // Note: area = size*size
@ -519,30 +514,28 @@ void NavMeshBuilder::InitRcConfig(BuilderParams& builder_params)
cfg.borderSize = cfg.walkableRadius + 3; // Reserve enough padding. cfg.borderSize = cfg.walkableRadius + 3; // Reserve enough padding.
cfg.width = cfg.tileSize + cfg.borderSize*2; cfg.width = cfg.tileSize + cfg.borderSize*2;
cfg.height = cfg.tileSize + cfg.borderSize*2; cfg.height = cfg.tileSize + cfg.borderSize*2;
cfg.detailSampleDist = kDetailSampleDist < 0.9f ? 0 : kCellSize * kDetailSampleDist; cfg.detailSampleDist = kDetailSampleDist < 0.9f ? 0 : builder_params.kCellSize * kDetailSampleDist;
cfg.detailSampleMaxError = kCellHeight * kDetailSampleMaxError; cfg.detailSampleMaxError = kCellHeight * kDetailSampleMaxError;
rcVcopy(cfg.bmin, bmin); rcVcopy(cfg.bmin, builder_params.bmin);
rcVcopy(cfg.bmax, bmax); rcVcopy(cfg.bmax, builder_params.bmax);
#endif
} }
void NavMeshBuilder::InitTileCacheParams(BuilderParams& builder_params) void NavMeshBuilder::InitTileCacheParams(BuilderParams& builder_params)
{ {
#if 0 dtTileCacheParams& tcparams = builder_params.tcparams;
// Tile cache params. // Tile cache params.
memset(&tcparams, 0, sizeof(tcparams)); memset(&tcparams, 0, sizeof(tcparams));
rcVcopy(tcparams.orig, bmin); rcVcopy(tcparams.orig, builder_params.bmin);
tcparams.cs = kCellSize; tcparams.cs = builder_params.kCellSize;
tcparams.ch = kCellHeight; tcparams.ch = builder_params.kCellHeight;
tcparams.width = (int)kTileSize; tcparams.width = (int)kTileSize;
tcparams.height = (int)kTileSize; tcparams.height = (int)kTileSize;
tcparams.walkableHeight = kAgentHeight; tcparams.walkableHeight = kAgentHeight;
tcparams.walkableRadius = kAgentRadius; tcparams.walkableRadius = kAgentRadius;
tcparams.walkableClimb = kAgentMaxClimb; tcparams.walkableClimb = kAgentMaxClimb;
tcparams.maxSimplificationError = kEdgeMaxError; tcparams.maxSimplificationError = kEdgeMaxError;
tcparams.maxTiles = tw*th*EXPECTED_LAYERS_PER_TILE; tcparams.maxTiles = builder_params.tw*builder_params.th*EXPECTED_LAYERS_PER_TILE;
tcparams.maxObstacles = 128; tcparams.maxObstacles = 128;
#endif
} }
int NavMeshBuilder::RasterizeTileLayers(const int tx, const int ty, int NavMeshBuilder::RasterizeTileLayers(const int tx, const int ty,