mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-04-26 17:29:17 -04:00
Cuts navmesh memory usage by at least 5 times (about 3GB less in our benchmark scene), enabling much larger scenes to generate successfully. Small Fish’s MSC scene (~3km x 3km) now generates fine, previously ran out of memory and crashed. The new theoretical maximum for a navmeshed scene is now ~50km x 50km. - Packed `CompactCell` data into a single 32-bit field (8 bytes -> 4 bytes). - Added heightfield serialization + compression so `CompactHeightfield` can be stored/transferred as bytes, and tiles store compressed heightfield data instead of large voxel arrays. - **Heightfield serialization + compression is a prerequisite for navmesh baking.** - Tiles are decompressed when generating polymeshes (e.g., to adapt to area changes). - Compression adds a small overhead during navmesh generation. - Fixed load sequence, now properly shows "Generating Navmesh..." in scene loading screen - Reduced allocations during `PolyMesh` generation. - Reduced the permanent memory usage of `PolyMesh`. - Improved parallel tile generation; now properly limits the number of in-flight tasks to prevent OOM crashes. Overall generation will be slower due to the extra compression step, I expect roughly ~30% in our benchmarks. Basically, we're trading generation speed for drastically lower memory usage. Some of the slowdown we will see in the benchmark is also due to the increased tile size. The larger tiles don’t actually make generation slower in real use, but they show up as "slower" because of how our benchmarks are designed/measured.
96 lines
2.7 KiB
C#
96 lines
2.7 KiB
C#
using System.Buffers;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace Sandbox.Navigation.Generation;
|
|
|
|
[SkipHotload]
|
|
internal class PolyMesh : IDisposable
|
|
{
|
|
public Vector3 BMin { get; set; }
|
|
public Vector3 BMax { get; set; }
|
|
public float CellSize { get; set; }
|
|
public float CellHeight { get; set; }
|
|
public int BorderSize { get; set; }
|
|
public float MaxEdgeError { get; set; }
|
|
|
|
/// <summary>
|
|
/// The polygon vertices. [Size: 3 * #nverts]
|
|
/// </summary>
|
|
public Span<ushort> Verts => _vertsArray.AsSpan( 0, maxVertCount * 3 );
|
|
private ushort[] _vertsArray;
|
|
|
|
/// <summary>
|
|
/// The polygon indices. [Size: 2 * #npolys * nvp]
|
|
/// </summary>
|
|
public Span<ushort> Polys => _polysArray.AsSpan( 0, maxPolyCount * MaxVertsPerPoly * 2 );
|
|
private ushort[] _polysArray;
|
|
|
|
/// <summary>
|
|
/// The area id assigned to each polygon. [Size: #npolys]
|
|
/// </summary>
|
|
public Span<int> Areas => _areasArray.AsSpan( 0, maxPolyCount );
|
|
private int[] _areasArray;
|
|
|
|
public int VertCount { get; set; }
|
|
public int PolyCount { get; set; }
|
|
public int MaxPolys { get; set; }
|
|
public int MaxVertsPerPoly { get; set; }
|
|
|
|
private int maxVertCount;
|
|
private int maxPolyCount;
|
|
|
|
private PolyMesh()
|
|
{
|
|
}
|
|
|
|
internal void Init( ContourSet cset, int maxVertsPerPoly, int maxTris, int maxVertices )
|
|
{
|
|
BMin = cset.BMin;
|
|
BMax = cset.BMax;
|
|
CellSize = cset.CellSize;
|
|
CellHeight = cset.CellHeight;
|
|
BorderSize = cset.BorderSize;
|
|
MaxEdgeError = cset.MaxError;
|
|
|
|
maxVertCount = maxVertices;
|
|
maxPolyCount = maxTris;
|
|
|
|
if ( _vertsArray == null || _vertsArray.Length < maxVertices * 3 )
|
|
{
|
|
if ( _vertsArray != null ) ArrayPool<ushort>.Shared.Return( _vertsArray );
|
|
_vertsArray = ArrayPool<ushort>.Shared.Rent( maxVertices * 3 * 2 );
|
|
}
|
|
if ( _polysArray == null || _polysArray.Length < maxTris * maxVertsPerPoly * 2 )
|
|
{
|
|
if ( _polysArray != null ) ArrayPool<ushort>.Shared.Return( _polysArray );
|
|
_polysArray = ArrayPool<ushort>.Shared.Rent( maxTris * maxVertsPerPoly * 2 * 2 );
|
|
}
|
|
if ( _areasArray == null || _areasArray.Length < maxTris )
|
|
{
|
|
if ( _areasArray != null ) ArrayPool<int>.Shared.Return( _areasArray );
|
|
_areasArray = ArrayPool<int>.Shared.Rent( maxTris * 2 );
|
|
}
|
|
VertCount = 0;
|
|
PolyCount = 0;
|
|
MaxVertsPerPoly = maxVertsPerPoly;
|
|
MaxPolys = maxTris;
|
|
|
|
// We intentionally do NOT clear/fill the other arrays here here. All used elements
|
|
// are fully written during BuildPolyMesh
|
|
Polys.Fill( Constants.MESH_NULL_IDX );
|
|
}
|
|
|
|
private static ConcurrentQueue<PolyMesh> _pool = new();
|
|
|
|
public static PolyMesh GetPooled()
|
|
{
|
|
return _pool.TryDequeue( out var hf ) ? hf : new PolyMesh();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_pool.Enqueue( this );
|
|
// Arrays will get disposed on shutdown, i guess
|
|
}
|
|
}
|