SpringRigVerify: VerifyGlb is a thin wrapper over one file run, GlbFile, that VerifyGlbCases also runs; the bind and rest thresholds pinned

Finding cut-whale-rig-glb-check.s2.exit-and-thresholds-prose.
- GlbFile(path, done): a missing or unreadable path is exit 1, a refusal or a failed comparison is 1, a pass is 0. VerifyGlb checks the
  device, logs and exits with that code; nothing else chooses an exit code.
- VerifyGlbCases adds the file cases that need no asset: a text file named .springrig and a missing path are exit 1.
- A Disturbance has a phase (Imported: before the bind and rest poses are compared; Posed: before the bake). Two cases move one bone's
  bind pose, and one bone's rest node, by 0.01 and expect the named refusal.
- BindTolerance and PoseTolerance 1e-4 -> 7.8e-5, the geometric mean of the shipped worst reading (rest 6.0e-7 over the Synth cases and
  the leviathan; bind exactly 0, so the rest reading stands in) and the disturbed case's 1.0e-2: separation 129 and 128. The comparison
  logs both readings on every run.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nibu-hands 2026-12-01 11:00:39 +01:00 • committed by soul
commit eb8b92a797

View file

@ -23,8 +23,9 @@ namespace Aetheria.EditorTools
/// Needs a graphics device (the skin check renders): run WITHOUT -nographics, and without -quit (Run exits itself, 0 or 1).
/// Unity -batchmode -projectPath &lt;repo&gt; -executeMethod Aetheria.EditorTools.SpringRigVerify.Run -logFile &lt;path&gt;
/// VerifyGlb runs the rig contract's own parse (SpringRigData.FromGlb) and the glTFast skin comparison on the GLB at
/// SPRINGRIG_GLB (same flags, method VerifyGlb); it adds no rule of its own.
/// VerifyGlbCases pins its verdicts on edited Synth GLBs (same flags); its exit code is pinned by running VerifyGlb on a refused GLB (exit 1).
/// SPRINGRIG_GLB (same flags, method VerifyGlb); it adds no rule of its own. .springrig files are GLBs. It is a thin wrapper
/// over GlbFile, the file run (a missing path, a refusal or a failed comparison is exit 1, a pass 0) that VerifyGlbCases also runs.
/// VerifyGlbCases pins GlbFile's verdicts and exit codes on edited Synth GLBs, the committed asset, a text file and a missing path (same flags).
/// Capture renders a short swim to PNG frames in SPRINGRIG_CAPTURE_OUT (same flags, method Capture).
/// </summary>
public static class SpringRigVerify
@ -58,11 +59,11 @@ namespace Aetheria.EditorTools
("lengths-hold-at-any-rate", LengthsHoldAtAnyRate), ("steady-motion-is-smooth", SteadyMotionIsSmooth), ("skin-matches-fk", SkinMatchesFk),
("capsule-is-finite", CapsuleIsFinite), ("gravity-sags", GravitySags), ("reset-honours-scale", ResetHonoursScale),
("bounds-follow-the-swim", BoundsFollowTheSwim), ("lod-hysteresis", LodHysteresis), ("inertia-carries", InertiaCarries),
("reentry-is-continuous", ReentryIsContinuous),
};
int failed = 0;
foreach (var (name, body) in checks)
{
("reentry-is-continuous", ReentryIsContinuous),
string verdict;
try { verdict = body(); }
catch (Exception e) { verdict = "exception: " + e; }
@ -731,7 +732,8 @@ namespace Aetheria.EditorTools
ImportAndCompare(glb, SpringRigData.FromGlb(glb), done);
}
// The rig contract's own parse of a GLB file, then the same glTFast comparison. SpringRigData.FromGlb owns every refusal.
// The rig contract's own parse of a GLB file, then the same glTFast comparison. SpringRigData.FromGlb owns every refusal; the exit
// code is GlbFile's, the one function VerifyGlbCases also runs: this method only checks the device, logs and exits with it.
public static void VerifyGlb()
{
if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null)
@ -740,31 +742,27 @@ namespace Aetheria.EditorTools
EditorApplication.Exit(1);
return;
}
var path = Environment.GetEnvironmentVariable("SPRINGRIG_GLB");
if (string.IsNullOrEmpty(path) || !File.Exists(path))
GlbFile(Environment.GetEnvironmentVariable("SPRINGRIG_GLB"), (code, verdict) =>
{
Debug.LogError($"{Tag} FAIL glb: set SPRINGRIG_GLB to a readable GLB path (got '{path}').");
EditorApplication.Exit(1);
return;
}
byte[] glb;
try { glb = File.ReadAllBytes(path); }
catch (Exception e)
{
Debug.LogError($"{Tag} FAIL glb: {e.Message}");
EditorApplication.Exit(1);
return;
}
CheckGlb(glb, verdict =>
{
if (verdict == null) Debug.Log($"{Tag} PASS glb");
if (code == 0) Debug.Log($"{Tag} PASS glb");
else Debug.LogError($"{Tag} FAIL glb: {verdict}");
EditorApplication.Exit(verdict == null ? 0 : 1);
EditorApplication.Exit(code);
});
}
// The file run: a missing or unreadable path is 1, a refusal or a failed comparison is 1, a pass is 0. done gets the exit code
// and the verdict (null on a pass). Any file (.glb, .springrig) is a GLB container.
static void GlbFile(string path, Action<int, string> done, Disturbance? disturb = null)
{
if (string.IsNullOrEmpty(path) || !File.Exists(path)) { done(1, $"set SPRINGRIG_GLB to a readable GLB path (got '{path}')"); return; }
byte[] glb;
try { glb = File.ReadAllBytes(path); }
catch (Exception e) { done(1, e.Message); return; }
CheckGlb(glb, verdict => done(verdict == null ? 0 : 1, verdict), disturb);
}
// FromGlb, then the glTFast comparison: null is a pass, else the owner's refusal or the comparison's verdict.
static void CheckGlb(byte[] glb, Action<string> done, Action<SkinnedMeshRenderer> disturb = null)
static void CheckGlb(byte[] glb, Action<string> done, Disturbance? disturb = null)
{
SpringRigData data;
try { data = SpringRigData.FromGlb(glb); }
@ -791,20 +789,49 @@ namespace Aetheria.EditorTools
return;
}
Synth Base() => new Synth { Spine = 4, Chains = 3, Joints = 4, Rot = true };
var cases = new (string name, Synth rig, string expect, Action<SkinnedMeshRenderer> disturb)[]
var cases = new (string name, Synth rig, string expect, Disturbance? disturb)[]
{
("accepts-synth", Base(), null, null),
("refuses-by-owner", new Synth { Spine = 4, Chains = 3, Joints = 4, Rot = true, Edit = j => j["nodes"][1]["scale"] = new JArray(2, 1, 1) }, "non-unit scale", null),
("refuses-unknown-required-extension", new Synth { Spine = 4, Chains = 3, Joints = 4, Rot = true, Edit = j => j["extensionsRequired"] = new JArray("KHR_not_a_real_extension") }, "glTFast did not import", null),
("refuses-joint-past-bones", new Synth { Spine = 4, Chains = 3, Joints = 4, Rot = true, BadJoint = true }, "exception", null),
("refuses-skin-off-by-a-known-amount", new Synth { Spine = 4, Chains = 3, Joints = 4, Rot = true }, "Unity skins a vertex 0.02", smr => smr.bones[1].position += new Vector3(0.02f, 0, 0)),
("refuses-skin-off-by-a-known-amount", new Synth { Spine = 4, Chains = 3, Joints = 4, Rot = true }, "Unity skins a vertex 0.02", new Disturbance(Phase.Posed, smr => smr.bones[1].position += new Vector3(0.02f, 0, 0))),
("refuses-bind-pose-off-by-a-known-amount", Base(), "bind poses differ", new Disturbance(Phase.Imported, smr =>
{
var bind = smr.sharedMesh.bindposes;
bind[1] = Matrix4x4.Translate(new Vector3(0.01f, 0, 0)) * bind[1];
smr.sharedMesh.bindposes = bind;
})),
("refuses-rest-node-off-by-a-known-amount", Base(), "rest bone poses differ", new Disturbance(Phase.Imported, smr => smr.bones[1].position += new Vector3(0.01f, 0, 0))),
("refuses-vacuous-mesh", new Synth { Spine = 4, Chains = 3, Joints = 4, Rot = true, Tiny = true }, "vacuous", null),
};
int failed = 0, at = 0;
// The file run itself, by path: a text file named .springrig and a missing path are exit 1.
var text = Path.Combine(Path.GetTempPath(), "springrig-not-a-glb.springrig");
File.WriteAllText(text, "this is not a GLB");
var files = new (string name, string path, int code)[]
{
("refuses-a-text-file", text, 1), ("refuses-a-missing-path", Path.Combine(Path.GetTempPath(), "springrig-missing.springrig"), 1),
};
int fileAt = 0;
void Next()
{
if (fileAt < files.Length)
{
var (fileName, path, code) = files[fileAt++];
GlbFile(path, (got, verdict) =>
{
if (got == code) Debug.Log($"{Tag} PASS glb file {fileName}: exit {got}");
else { Debug.LogError($"{Tag} FAIL glb file {fileName}: expected exit {code}, got {got} ({verdict})"); failed++; }
foreach (var go in Scrap) if (go != null) UnityEngine.Object.DestroyImmediate(go);
Scrap.Clear();
Next();
});
return;
}
if (at == cases.Length)
{
File.Delete(text);
Debug.Log(failed == 0 ? $"{Tag} PASS all glb cases" : $"{Tag} FAIL {failed} glb case(s)");
EditorApplication.Exit(failed == 0 ? 0 : 1);
return;
@ -821,8 +848,25 @@ namespace Aetheria.EditorTools
Next();
}
// disturb moves the imported skeleton after it is posed: only VerifyGlbCases passes one, to give the comparison a known skin error.
static void ImportAndCompare(byte[] glb, SpringRigData data, Action<string> done, Action<SkinnedMeshRenderer> disturb = null)
// A Disturbance moves what glTFast imported at a named point of the comparison: only VerifyGlbCases passes one, to give a check a
// known error. Imported: before the bind and rest poses are compared (a bind pose or a rest node moved). Posed: after the rig
// posed the bones and before Unity bakes the skin (a bone moved).
enum Phase { Imported, Posed }
readonly struct Disturbance
{
public readonly Phase When;
public readonly Action<SkinnedMeshRenderer> Act;
public Disturbance(Phase when, Action<SkinnedMeshRenderer> act) { When = when; Act = act; }
}
// Tolerances of the glTFast comparison (lodur:ruling:deterministic-bound-margin): the geometric mean of the shipped worst reading and
// the reading of the case that moves one bone by 0.01 (bind 1.0e-2, rest 1.0e-2). Shipped, over the Synth cases and the committed
// leviathan: rest bone poses 6.0e-7 (separation 129 and 128), bind poses exactly 0, so the rest reading, the same float noise, stands
// in for it (the mean of 0 is 0).
const double BindTolerance = 7.8e-5, PoseTolerance = 7.8e-5;
// disturb moves the imported skeleton: only VerifyGlbCases passes one.
static void ImportAndCompare(byte[] glb, SpringRigData data, Action<string> done, Disturbance? disturb = null)
{
var root = new GameObject("imported");
Scrap.Add(root);
@ -846,11 +890,12 @@ namespace Aetheria.EditorTools
EditorApplication.update += poll;
}
static string HandednessCompare(SpringRigData data, GameObject root, bool instantiated, Action<SkinnedMeshRenderer> disturb)
static string HandednessCompare(SpringRigData data, GameObject root, bool instantiated, Disturbance? disturb)
{
if (!instantiated) return "glTFast did not instantiate the scene";
var smr = root.GetComponentInChildren<SkinnedMeshRenderer>();
if (smr == null || smr.bones.Length != data.BoneCount) return "glTFast made no skinned mesh with the rig's bones";
if (disturb is { When: Phase.Imported } early) early.Act(smr);
var mesh = smr.sharedMesh;
double bind = 0, pose = 0;
Fk(data, 0, new SwimSettings { AmplitudeDegrees = 0, PeriodSeconds = 1, Axis = new float3(1, 0, 0) }, default, quaternion.identity, 1, out var rp, out var rr);
@ -860,8 +905,9 @@ namespace Aetheria.EditorTools
bind = math.max(bind, math.max(math.cmax(math.abs(a.c0 - b.c0)), math.max(math.cmax(math.abs(a.c1 - b.c1)), math.max(math.cmax(math.abs(a.c2 - b.c2)), math.cmax(math.abs(a.c3 - b.c3))))));
pose = math.max(pose, math.distance(smr.bones[i].position, (Vector3)rp[i]) + Quaternion.Angle(smr.bones[i].rotation, (Quaternion)rr[i]) * 0.01);
}
if (bind > 1e-4) return $"glTFast's bind poses differ from the rig's by {bind}";
if (pose > 1e-4) return $"glTFast's rest bone poses differ from the rig's by {pose}";
Debug.Log($"{Tag} glb readings: bind poses differ by {bind:E2} (tolerance {BindTolerance:E1}), rest bone poses by {pose:E2} (tolerance {PoseTolerance:E1})");
if (bind > BindTolerance) return $"glTFast's bind poses differ from the rig's by {bind}";
if (pose > PoseTolerance) return $"glTFast's rest bone poses differ from the rig's by {pose}";
var holder = new GameObject("rig");
Scrap.Add(holder);
@ -871,7 +917,7 @@ namespace Aetheria.EditorTools
rig.Initialize(data, mesh.bounds);
for (int f = 0; f < 90; f++) rig.Present(f * Dt, Vector3.zero, Quaternion.identity, 1);
for (int i = 0; i < data.BoneCount; i++) smr.bones[i].SetPositionAndRotation((Vector3)rig.BonePosition(i), (Quaternion)rig.BoneRotation(i));
disturb?.Invoke(smr);
if (disturb is { When: Phase.Posed } late) late.Act(smr);
var baked = new Mesh();
smr.BakeMesh(baked, true);
var bakedVerts = baked.vertices; var vertices = mesh.vertices; var weights = mesh.boneWeights;