MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / EnsureCapacity

Method EnsureCapacity

Source/Engine/Navigation/NavMeshRuntime.cpp:324–384  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

322}
323
324void NavMeshRuntime::EnsureCapacity(int32 tilesToAddCount)
325{
326 ScopeLock lock(Locker);
327 const int32 newTilesCount = _tiles.Count() + tilesToAddCount;
328 const int32 capacity = GetTilesCapacity();
329 if (newTilesCount <= capacity)
330 return;
331 PROFILE_CPU_NAMED("NavMeshRuntime.EnsureCapacity");
332 PROFILE_MEM(NavigationMesh);
333
334 // Navmesh tiles capacity growing rule
335 int32 newCapacity = capacity ? capacity : 32;
336 while (newCapacity < newTilesCount)
337 newCapacity = Math::RoundUpToPowerOf2(newCapacity + 1);
338
339 LOG(Info, "Resizing navmesh {2} from {0} to {1} tiles capacity", capacity, newCapacity, Properties.Name);
340
341 // Ensure to have size assigned
342 ASSERT(_tileSize != 0);
343
344 // Prepare parameters
345 dtNavMeshParams params;
346 params.orig[0] = 0.0f;
347 params.orig[1] = 0.0f;
348 params.orig[2] = 0.0f;
349 params.tileWidth = _tileSize;
350 params.tileHeight = _tileSize;
351 params.maxTiles = newCapacity;
352 const int32 tilesBits = (int32)Math::Log2((float)Math::RoundUpToPowerOf2(params.maxTiles));
353 params.maxPolys = 1 << (22 - tilesBits);
354
355 // Initialize nav mesh
356 if (!_navMesh)
357 _navMesh = dtAllocNavMesh();
358 if (dtStatusFailed(_navMesh->init(&params)))
359 {
360 LOG(Error, "Navmesh {0} init failed", Properties.Name);
361 return;
362 }
363 if (dtStatusFailed(_navMeshQuery->init(_navMesh, MAX_NODES)))
364 {
365 LOG(Error, "Navmesh query {0} init failed", Properties.Name);
366 }
367
368 // Prepare tiles container
369 _tiles.EnsureCapacity(newCapacity);
370
371 // Restore previous tiles
372 for (auto& tile : _tiles)
373 {
374 GET_NAV_TILE_DATA(tile);
375 const auto result = _navMesh->addTile(data, dataSize, flags, 0, nullptr);
376 if (dtStatusFailed(result))
377 {
378 LOG(Warning, "Could not add tile ({2}x{3}, layer {4}) to navmesh {0} (error: {1})", Properties.Name, result & ~DT_FAILURE, tile.X, tile.Y, tile.Layer);
379#if USE_NAV_MESH_ALLOC
380 dtFree(data);
381#endif

Callers 1

BuildDirtyBoundsFunction · 0.45

Calls 8

dtAllocNavMeshFunction · 0.85
dtStatusFailedFunction · 0.85
dtFreeFunction · 0.85
RoundUpToPowerOf2Function · 0.50
Log2Function · 0.50
CountMethod · 0.45
initMethod · 0.45
addTileMethod · 0.45

Tested by

no test coverage detected