DrawAhead.BeltDraw owns the whole per-rock draw list; ZoneRenderer iterates it with no mesh choice, sort or index arithmetic
Replaces RockMeshing/AssignRockMeshes and the renderer's slot bucketing (findings scale-lookup-unpinned, renderer-index-wiring-unpinned). The build gate message names the mesh and the field, not the radii. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c576e0ac18
commit
c5ad4676f7
4 changed files with 122 additions and 82 deletions
|
|
@ -56,8 +56,8 @@ namespace Aetheria.EditorTools
|
|||
var (_, meshes, measured) = Load(scene);
|
||||
var failures = meshes.Select((m, i) => (m, i)).Where(p => !(measured[p.i] > 0f) || Mathf.Abs(p.m.Radius - measured[p.i]) > Tolerance).ToArray();
|
||||
if (failures.Length > 0)
|
||||
throw new InvalidOperationException("InstancedMesh.Radius differs from the measured one (run RockMeshRadius.Fill): " + string.Join(", ",
|
||||
failures.Select(f => $"{f.m.Mesh.name} stored {f.m.Radius} measured {measured[f.i]}")));
|
||||
throw new InvalidOperationException("InstancedMesh.Radius differs from the measured one, or is unset, on mesh (run RockMeshRadius.Fill): " + string.Join(", ",
|
||||
failures.Select(f => f.m.Mesh.name)));
|
||||
return Summary("ok", measured);
|
||||
}
|
||||
finally
|
||||
|
|
|
|||
|
|
@ -45,35 +45,53 @@ public static class DrawAhead
|
|||
public static float RockScale(float gameplayRadius, float meshRadius) =>
|
||||
meshRadius > 0f ? gameplayRadius / meshRadius : throw new System.ArgumentException("InstancedMesh.Radius is not set (zero, negative or not a number): run RockMeshRadius.Fill.");
|
||||
|
||||
// Which mesh each rock of a belt takes, and at what scale (ruling rock-meshes-by-size). meshRadii are the kept meshes' measured radii in
|
||||
// the renderer's own order; the meshes are ranked by radius here, not by the caller. Rocks ranked by unworn size split into one run per mesh,
|
||||
// smallest rocks on the smallest mesh and the last run taking the remainder; equal sizes and equal radii rank by index, so the answer is stable.
|
||||
// Unworn, so a mined rock keeps its mesh and only shrinks. The renderer builds its instance matrices from Mesh and Scale and decides neither.
|
||||
public sealed class RockMeshing
|
||||
// One rock's entry in a belt's draw list: the rock (its index in the belt) and the scale that draws it at its gameplay radius on the mesh it is listed under.
|
||||
public readonly struct RockDraw
|
||||
{
|
||||
private readonly float[] _meshRadii;
|
||||
|
||||
// Per rock: the index into meshRadii (as given) of the mesh it takes.
|
||||
public int[] Mesh { get; }
|
||||
|
||||
public RockMeshing(IReadOnlyList<float> unwornSizes, IReadOnlyList<float> meshRadii)
|
||||
{
|
||||
_meshRadii = meshRadii.ToArray();
|
||||
for (var m = 0; m < _meshRadii.Length; m++)
|
||||
if (!(_meshRadii[m] > 0f)) throw new System.ArgumentException($"InstancedMesh.Radius of mesh #{m} is not set (zero, negative or not a number): run RockMeshRadius.Fill.");
|
||||
var meshByRank = Enumerable.Range(0, _meshRadii.Length).OrderBy(m => _meshRadii[m]).ThenBy(m => m).ToArray();
|
||||
var rockByRank = Enumerable.Range(0, unwornSizes.Count).OrderBy(i => unwornSizes[i]).ThenBy(i => i).ToArray();
|
||||
var run = System.Math.Max(1, unwornSizes.Count / _meshRadii.Length);
|
||||
Mesh = new int[unwornSizes.Count];
|
||||
for (var rank = 0; rank < rockByRank.Length; rank++) Mesh[rockByRank[rank]] = meshByRank[System.Math.Min(rank / run, meshByRank.Length - 1)];
|
||||
}
|
||||
|
||||
// The scale that draws the rock at its current gameplay radius on the mesh it was given.
|
||||
public float Scale(int rock, float gameplayRadius) => RockScale(gameplayRadius, _meshRadii[Mesh[rock]]);
|
||||
public readonly int Rock;
|
||||
public readonly float Scale;
|
||||
public RockDraw(int rock, float scale) { Rock = rock; Scale = scale; }
|
||||
}
|
||||
|
||||
public static RockMeshing AssignRockMeshes(AsteroidBelt belt, PlanetSettings settings, IReadOnlyList<float> meshRadii) =>
|
||||
new(Enumerable.Range(0, belt.Data.Asteroids.Length).Select(i => belt.UndamagedSize(i, settings)).ToArray(), meshRadii);
|
||||
// The whole per-rock draw list of a belt (ruling rock-meshes-by-size); the renderer decides nothing and indexes nothing. Meshes are the kept
|
||||
// candidates ranked ascending by radius, and ByMesh[m] lists the rocks drawn on Meshes[m], so a renderer batching by mesh iterates the two together.
|
||||
// Rocks ranked by unworn size split into one run per mesh, smallest rocks on the smallest mesh and the last run taking the remainder; equal sizes and
|
||||
// equal radii rank by index, and a mesh lists its rocks by index. Unworn, so a mined rock keeps its mesh and only shrinks.
|
||||
// A list starts with every Scale NaN; Draw fills each entry's Scale from the poses (pose.w is the rock's gameplay radius) and returns the same arrays every call.
|
||||
public sealed class BeltDraw<T>
|
||||
{
|
||||
private readonly float[] _radii;
|
||||
private readonly int[][] _rocks;
|
||||
|
||||
public T[] Meshes { get; }
|
||||
public RockDraw[][] ByMesh { get; }
|
||||
|
||||
public BeltDraw(IReadOnlyList<float> unwornSizes, IReadOnlyList<T> candidates, System.Func<T, float> radius)
|
||||
{
|
||||
var radii = candidates.Select(radius).ToArray();
|
||||
for (var m = 0; m < radii.Length; m++)
|
||||
if (!(radii[m] > 0f)) throw new System.ArgumentException($"InstancedMesh.Radius of mesh #{m} is not set (zero, negative or not a number): run RockMeshRadius.Fill.");
|
||||
var meshByRank = Enumerable.Range(0, radii.Length).OrderBy(m => radii[m]).ThenBy(m => m).ToArray();
|
||||
_radii = meshByRank.Select(m => radii[m]).ToArray();
|
||||
Meshes = meshByRank.Select(m => candidates[m]).ToArray();
|
||||
var rockByRank = Enumerable.Range(0, unwornSizes.Count).OrderBy(i => unwornSizes[i]).ThenBy(i => i).ToArray();
|
||||
var run = System.Math.Max(1, unwornSizes.Count / radii.Length);
|
||||
_rocks = Enumerable.Range(0, radii.Length).Select(m => rockByRank
|
||||
.Where((_, rank) => System.Math.Min(rank / run, radii.Length - 1) == m).OrderBy(i => i).ToArray()).ToArray();
|
||||
ByMesh = _rocks.Select(r => r.Select(rock => new RockDraw(rock, float.NaN)).ToArray()).ToArray();
|
||||
}
|
||||
|
||||
public RockDraw[][] Draw(IReadOnlyList<float4> poses)
|
||||
{
|
||||
for (var m = 0; m < _rocks.Length; m++)
|
||||
for (var k = 0; k < _rocks[m].Length; k++)
|
||||
ByMesh[m][k] = new RockDraw(_rocks[m][k], RockScale(poses[_rocks[m][k]].w, _radii[m]));
|
||||
return ByMesh;
|
||||
}
|
||||
}
|
||||
|
||||
public static BeltDraw<T> DrawListOf<T>(AsteroidBelt belt, PlanetSettings settings, IReadOnlyList<T> meshes, System.Func<T, float> radius) =>
|
||||
new(Enumerable.Range(0, belt.Data.Asteroids.Length).Select(i => belt.UndamagedSize(i, settings)).ToArray(), meshes, radius);
|
||||
|
||||
// A ship in a wormhole animation has its pose scripted by the step, not integrated from Velocity and TurnRate,
|
||||
// so it is drawn where the step put it: extrapolating a scripted pose draws a streak that resets every step.
|
||||
|
|
|
|||
|
|
@ -73,11 +73,9 @@ public class ZoneRenderer : MonoBehaviour
|
|||
[HideInInspector] public Dictionary<Entity, EntityInstance> EntityInstances = new Dictionary<Entity, EntityInstance>();
|
||||
[HideInInspector] public Dictionary<CultRecordKey, PlanetObject> Planets = new Dictionary<CultRecordKey, PlanetObject>();
|
||||
|
||||
private Dictionary<CultRecordKey, InstancedMesh[]> _beltMeshes = new Dictionary<CultRecordKey, InstancedMesh[]>();
|
||||
// Ruling rock-meshes-by-size: DrawAhead owns the whole draw list (kept meshes ranked, each rock's mesh and scale); the renderer only draws it.
|
||||
private Dictionary<CultRecordKey, DrawAhead.BeltDraw<InstancedMesh>> _beltDraw = new Dictionary<CultRecordKey, DrawAhead.BeltDraw<InstancedMesh>>();
|
||||
private Dictionary<CultRecordKey, Matrix4x4[][]> _beltMatrices = new Dictionary<CultRecordKey, Matrix4x4[][]>();
|
||||
// Per rock: the mesh it takes and its slot in that mesh's matrix batch (ruling rock-meshes-by-size).
|
||||
private Dictionary<CultRecordKey, DrawAhead.RockMeshing> _beltMeshing = new Dictionary<CultRecordKey, DrawAhead.RockMeshing>();
|
||||
private Dictionary<CultRecordKey, int[]> _beltSlotOf = new Dictionary<CultRecordKey, int[]>();
|
||||
// Cut 1 (docs/mining-cut.md): the renderer's own buffer, filled from Zone.EvaluateBelt each frame. No pose
|
||||
// is stored on AsteroidBelt any more.
|
||||
private Dictionary<CultRecordKey, float4[]> _beltTransforms = new Dictionary<CultRecordKey, float4[]>();
|
||||
|
|
@ -384,10 +382,8 @@ public class ZoneRenderer : MonoBehaviour
|
|||
|
||||
Planets.Clear();
|
||||
Map.Clear();
|
||||
_beltMeshes.Clear();
|
||||
_beltDraw.Clear();
|
||||
_beltMatrices.Clear();
|
||||
_beltMeshing.Clear();
|
||||
_beltSlotOf.Clear();
|
||||
_beltTransforms.Clear();
|
||||
_tourPlanets.Clear();
|
||||
}
|
||||
|
|
@ -452,17 +448,10 @@ public class ZoneRenderer : MonoBehaviour
|
|||
var meshes = AsteroidMeshes.ToList();
|
||||
while (meshes.Count > Settings.AsteroidMeshCount)
|
||||
meshes.RemoveAt(Random.Range(0, meshes.Count));
|
||||
_beltMeshes[key] = meshes.ToArray();
|
||||
_beltTransforms[key] = new float4[beltData.Asteroids.Length];
|
||||
// Ruling rock-meshes-by-size: DrawAhead ranks the meshes by radius and the rocks by size, and refuses a mesh with no radius.
|
||||
var meshing = DrawAhead.AssignRockMeshes(Zone.AsteroidBelts[key], Zone.Settings, meshes.Select(m => m.Radius).ToArray());
|
||||
_beltMeshing[key] = meshing;
|
||||
var meshOf = meshing.Mesh;
|
||||
var slotOf = new int[meshOf.Length];
|
||||
var perMesh = new int[meshes.Count];
|
||||
for (int i = 0; i < meshOf.Length; i++) slotOf[i] = perMesh[meshOf[i]]++;
|
||||
_beltSlotOf[key] = slotOf;
|
||||
_beltMatrices[key] = perMesh.Select(n => new Matrix4x4[n]).ToArray();
|
||||
var draw = DrawAhead.DrawListOf(Zone.AsteroidBelts[key], Zone.Settings, meshes, m => m.Radius);
|
||||
_beltDraw[key] = draw;
|
||||
_beltMatrices[key] = draw.ByMesh.Select(rocks => new Matrix4x4[rocks.Length]).ToArray();
|
||||
|
||||
Map.RegisterBelt(key, _beltTransforms[key]);
|
||||
}
|
||||
|
|
@ -556,25 +545,21 @@ public class ZoneRenderer : MonoBehaviour
|
|||
new Vector3(orbitPosition.x,height,orbitPosition.y),
|
||||
new Vector3(belt.Radius * 2,100,belt.Radius * 2))))
|
||||
{
|
||||
var meshes = _beltMeshes[key];
|
||||
var draw = _beltDraw[key];
|
||||
var matrices = _beltMatrices[key];
|
||||
var meshing = _beltMeshing[key];
|
||||
var meshOf = meshing.Mesh;
|
||||
var slotOf = _beltSlotOf[key];
|
||||
for (int tx = 0; tx < transforms.Length; tx++)
|
||||
var byMesh = draw.Draw(transforms);
|
||||
for (int m = 0; m < byMesh.Length; m++)
|
||||
{
|
||||
var transform = transforms[tx];
|
||||
var i = meshOf[tx];
|
||||
matrices[i][slotOf[tx]] = Matrix4x4.TRS(DrawAhead.ChunkAt(Zone, ItemManager.GameplaySettings, transform, lead).ToUnity(),
|
||||
Quaternion.Euler(
|
||||
cos(transform.z + (float)i / meshes.Length) * 100,
|
||||
sin(transform.z + (float)i / meshes.Length) * 100,
|
||||
(float)tx / transforms.Length * 360),
|
||||
Vector3.one * meshing.Scale(tx, transform.w));
|
||||
for (int k = 0; k < byMesh[m].Length; k++)
|
||||
{
|
||||
var rock = byMesh[m][k];
|
||||
var transform = transforms[rock.Rock];
|
||||
matrices[m][k] = Matrix4x4.TRS(DrawAhead.ChunkAt(Zone, ItemManager.GameplaySettings, transform, lead).ToUnity(),
|
||||
Quaternion.Euler(cos(transform.z) * 100, sin(transform.z) * 100, (float)rock.Rock / transforms.Length * 360),
|
||||
Vector3.one * rock.Scale);
|
||||
}
|
||||
Graphics.DrawMeshInstanced(draw.Meshes[m].Mesh, 0, draw.Meshes[m].Material, matrices[m]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < meshes.Length; i++)
|
||||
Graphics.DrawMeshInstanced(meshes[i].Mesh, 0, meshes[i].Material, matrices[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue