Smoother drives across the meadow: no more hitches opening sectors

- Sector maps are put away on a worker thread, and trimmed in batches
- A far sector's texture only needs its look; at most four new ones a frame
- Fresh maps fill rows between their borders instead of testing every cell
- Rocks are marked with plain numbers; marks come back on unpacked maps
- Worked-overlay sort uses float keys; idle belts look on staggered steps
- Chase camera cast no longer allocates; perf demo drives and runs too

Drive phase on a late save: p99 33 -> 16 ms, frames over 20 ms 242 -> 7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiemandKG
2026-11-10 12:59:16 +06:00
parent 29485df9e1
commit 172c3b978d
6 changed files with 271 additions and 76 deletions

View File

@ -95,6 +95,8 @@ namespace GrassCutter
readonly List<MaterialPropertyBlock> _mpbs = new List<MaterialPropertyBlock>();
readonly List<HexGrid.Sector> _near = new List<HexGrid.Sector>();
readonly List<HexGrid.Sector> _worked = new List<HexGrid.Sector>();
float[] _workedKey = new float[256];
HexGrid.Sector[] _workedBy = new HexGrid.Sector[256];
readonly Plane[] _frustum = new Plane[6];
int _mpbUsed, _here = -1;
/// <summary>Grass draws issued last frame (for -demogpu).</summary>
@ -269,7 +271,8 @@ namespace GrassCutter
if (!GeometryUtility.TestPlanesAABB(_frustum, s.bounds)) continue;
float arc = (s.c - dir).magnitude * Planet.Radius;
float reachable = Mathf.Max(arc - s.radius, alt);
var mask = MowMap.TextureFor(s);
var mask = MowMap.TextureFor(s, arc - s.radius < 45f);
if (!mask) continue;
// mown to the last blade: only the floor shows (the pile's shells and every plant are cut away in the
// shader anyway), one draw instead of up to six. Near the base that is most of what is in view.
if (mask == MowMap.Full && mask)
@ -303,20 +306,24 @@ namespace GrassCutter
}
if (furMat)
foreach (var s in _worked)
if (GeometryUtility.TestPlanesAABB(_frustum, s.bounds)) DrawPile(FurFloor, s, MowMap.TextureFor(s), true);
if (GeometryUtility.TestPlanesAABB(_frustum, s.bounds) && MowMap.TextureFor(s, false) is Texture2D worked && worked) DrawPile(FurFloor, s, worked, true);
DrawBatches();
Draws = _mpbUsed + _drawn;
}
int _drawn;
readonly HashSet<int> _seen = new HashSet<int>();
/// <summary>Every sector the rings can reach from here, the one underfoot first, and the worked ones further out.</summary>
void GatherNear(int here)
{
_near.Clear();
var from = HexGrid.Get(here);
_near.Add(from);
var seen = new HashSet<int> { here };
var seen = _seen;
seen.Clear();
seen.Add(here);
float reach = Mathf.Max(Hemp.fadeB, FurFar.outer) + 40f;
for (int i = 0; i < _near.Count; i++)
foreach (int n in _near[i].neighbours)
@ -327,13 +334,27 @@ namespace GrassCutter
}
MowMap.Trim(from.c);
_worked.Clear();
// a well-worked planet has thousands of these: they are sorted by a plain number, not by a comparison that
// works out two distances every time (that took 4 ms at every step into a new sector)
float far = OverlayReach / Planet.Radius;
far *= far;
int count = 0;
foreach (var m in MowMap.All)
if (m.keep && !seen.Contains(m.s.id) && (m.s.c - from.c).magnitude * Planet.Radius < OverlayReach) _worked.Add(m.s);
if (_worked.Count > OverlayMax)
{
_worked.Sort((a, b) => (a.c - from.c).sqrMagnitude.CompareTo((b.c - from.c).sqrMagnitude));
_worked.RemoveRange(OverlayMax, _worked.Count - OverlayMax);
if (!m.keep || seen.Contains(m.s.id)) continue;
float d = (m.s.c - from.c).sqrMagnitude;
if (d >= far) continue;
if (count == _workedKey.Length)
{
System.Array.Resize(ref _workedKey, count * 2);
System.Array.Resize(ref _workedBy, count * 2);
}
_workedKey[count] = d;
_workedBy[count++] = m.s;
}
if (count > OverlayMax) System.Array.Sort(_workedKey, _workedBy, 0, count);
for (int i = 0; i < count && i < OverlayMax; i++) _worked.Add(_workedBy[i]);
System.Array.Clear(_workedBy, 0, count);
}
static void Flat(HexGrid.Sector s, Vector3 dir, out float u, out float v) => MowMap.Flat(s, dir, out u, out v);