diff --git a/Assets/Scripts/Editor/SpringRigVerify.cs b/Assets/Scripts/Editor/SpringRigVerify.cs index d65eed25d..131e4fcb5 100644 --- a/Assets/Scripts/Editor/SpringRigVerify.cs +++ b/Assets/Scripts/Editor/SpringRigVerify.cs @@ -62,6 +62,7 @@ namespace Aetheria.EditorTools int failed = 0; foreach (var (name, body) in checks) { + ("reentry-is-continuous", ReentryIsContinuous), string verdict; try { verdict = body(); } catch (Exception e) { verdict = "exception: " + e; } @@ -302,7 +303,7 @@ namespace Aetheria.EditorTools if (snap > 0.15) return $"the first frozen frame moved a tip {snap:P0} of its chain (a snap, not an ease)"; t += Dt; w.Swim.AmplitudeDegrees = 15; - for (int i = 0; i < 30; i++, t += Dt) w.Present(t, new Vector3(Mathf.Sin((float)t * 5), 0, 0), Quaternion.identity, 1); // the ease ends at BlendSeconds + for (int i = 0, eased = (int)math.ceil(w.BlendSeconds / Dt) + 2; i < eased; i++, t += Dt) w.Present(t, new Vector3(Mathf.Sin((float)t * 5), 0, 0), Quaternion.identity, 1); // the ease ends at BlendSeconds int jobs = w.SolveJobsScheduled; double rest = 0; for (int i = 0; i < 10; i++, t += Dt) @@ -619,8 +620,11 @@ namespace Aetheria.EditorTools return jumped.Resets == 3 ? null : $"a jump of 500 u at scale 4 reset {jumped.Resets - 1} extra times, expected 2 (out and back)"; } - // Bounds follow the pose: every skinned vertex stays inside the renderer's bounds at a swim amplitude that moves the body. - static string BoundsFollowTheSwim() + // Bounds follow the pose: every skinned vertex stays inside the renderer's bounds at a swim amplitude that moves the body, at a + // scale above 1 (a bounds job that ignores scale is too large, which still holds every vertex) and below it (too small). + static string BoundsFollowTheSwim() => BoundsCase(1.5f) ?? BoundsCase(0.6f); + + static string BoundsCase(float scale) { var s = new Synth { Gravity = 0.5f, Stiffness = 2, Colliders = new JArray() }; var go = new GameObject("boundsTest"); @@ -636,7 +640,7 @@ namespace Aetheria.EditorTools double worst = 0; Bounds first = default, last = default; for (int f = 0; f < 300; f++) { - rig.Present(f * Dt, new Vector3(0, 0, f * 0.05f), Quaternion.Euler(0, f * 0.5f, 0), 1.5f); + rig.Present(f * Dt, new Vector3(0, 0, f * 0.05f), Quaternion.Euler(0, f * 0.5f, 0), scale); var b = rig.Renderer.localBounds; if (f == 0) first = b; last = b; for (int v = 0; v < verts.Count; v++) @@ -647,8 +651,52 @@ namespace Aetheria.EditorTools worst = math.max(worst, math.cmax(outside)); } } - if (worst > 1e-3) return $"a skinned vertex lies {worst} outside the bounds"; - return first.size != last.size ? null : "the bounds never changed (vacuous)"; + if (worst > 1e-3) return $"at scale {scale} a skinned vertex lies {worst} outside the bounds"; + return first.size != last.size ? null : $"at scale {scale} the bounds never changed (vacuous)"; + } + + // Returning to Full part way through the ease continues from the pose last drawn: the returning frame moves a tip, relative to its + // chain root, a small part of the chain; a reset to rest moves it by nearly all of the distance the tip had left (Soul read 96% of + // the chain after 3 frames out with the old quarter-second ease). Readings with the 0.75 s ease, the largest over the two cases + // (3 and 8 frames out): shipped 0.172; the reset-to-rest mutant least 0.970 (0.970 and 1.207). The bound is + // lodur:ruling:deterministic-bound-margin's geometric mean sqrt(0.172 x 0.970) = 0.41, a separation of 2.4 on each side. + const double ReentryBound = 0.41; + + static string ReentryIsContinuous() + { + var shortOut = ReentryCase(3); + var longOut = ReentryCase(8); // both run, so both readings are logged + return shortOut ?? longOut; + } + + static string ReentryCase(int framesOut) + { + var cam = new GameObject("lodCam").AddComponent(); + Scrap.Add(cam.gameObject); + cam.fieldOfView = 60; + var s = new Synth { Gravity = 0, Stiffness = 4 }; + var w = Whale(s, out var data); + w.LodCamera = cam; + w.Swim.AmplitudeDegrees = 0; + var chains = Enumerable.Range(0, data.ChainCount).Select(c => data.ChainBones(c)).ToArray(); + float3[] Rel() => chains.Select(b => w.BonePosition(b[b.Count - 1]) - w.BonePosition(b[0])).ToArray(); + double chainLength = (s.Joints - 1) * math.length(s.Seg); + cam.transform.position = new Vector3(0, 0, -50); + double t = 0; + Vector3 At() => new Vector3(Mathf.Sin((float)t * 5) * 3.2f, 0, 0); + for (; t < 3; t += Dt) w.Present(t, At(), Quaternion.identity, 1); + if (w.Level != SpringLod.Full) return "near whale is not Full"; + cam.transform.position = new Vector3(0, 0, -300); + for (int i = 0; i < framesOut; i++, t += Dt) w.Present(t, At(), Quaternion.identity, 1); + if (w.Level != SpringLod.Frozen) return "far whale is not Frozen"; + var drawn = Rel(); + cam.transform.position = new Vector3(0, 0, -50); + if (w.Present(t, At(), Quaternion.identity, 1) != SpringLod.Full) return "near whale did not return to Full"; + var back = Rel(); + double moved = 0; + for (int c = 0; c < drawn.Length; c++) moved = math.max(moved, math.distance(back[c], drawn[c]) / chainLength); + Debug.Log($"{Tag} reentry after {framesOut} frames out: the returning frame moved a tip {moved:P1} of its chain (bound {ReentryBound:P1})"); + return moved <= ReentryBound ? null : $"returning after {framesOut} frames out moved a tip {moved:P0} of its chain in one frame (a reset to rest, not a continuation)"; } // Hovering at the Full threshold must not flip the level every frame. diff --git a/Assets/Scripts/Fauna Display/SpringRig.cs b/Assets/Scripts/Fauna Display/SpringRig.cs index 7d3b12d3b..fb33ab0a2 100644 --- a/Assets/Scripts/Fauna Display/SpringRig.cs +++ b/Assets/Scripts/Fauna Display/SpringRig.cs @@ -26,6 +26,8 @@ namespace Aetheria.FaunaDisplay public const int SubstepRate = 120; public const int MaxSubsteps = 4; + [Tooltip("The rig's GLB (a .springrig the importer wraps). Awake initializes from it when set; callers that Initialize by hand leave it null.")] + public TextAsset Rig; public MeshRenderer Renderer; public SwimSettings Swim = SwimSettings.Default; [Tooltip("Projected height of the whale as a fraction of the viewport at or above which the tendrils are solved.")] @@ -34,8 +36,8 @@ namespace Aetheria.FaunaDisplay public float FrozenAbove = 0.015f; [Tooltip("A level is left only once the projected height is this fraction below the threshold that entered it.")] [Range(0f, 0.5f)] public float Hysteresis = 0.15f; - [Tooltip("Drawn seconds over which the tendrils ease to rest when the level leaves Full.")] - public float BlendSeconds = 0.25f; + [Tooltip("Drawn seconds over which the tendrils ease to rest when the level leaves Full: 0.5 to 1 reads as settling, a quarter second as a flare.")] + public float BlendSeconds = 0.75f; public Camera LodCamera; public SpringRigData Data { get; private set; } @@ -49,7 +51,7 @@ namespace Aetheria.FaunaDisplay public quaternion BoneRotation(int bone) => _boneRot[bone]; NativeArray _order, _parent, _spine, _chainStart, _chainLen, _chainBone, _chainColStart, _chainColLen, _chainCol; - NativeArray _restPos, _bonePos, _simPos, _simPrev; + NativeArray _restPos, _bonePos, _simPos, _simPrev, _drawn; NativeArray _restRot, _boneRot; NativeArray _bind, _skin; NativeArray _param; @@ -90,6 +92,7 @@ namespace Aetheria.FaunaDisplay _bonePos = Own(n); _simPos = Own(n); _simPrev = Own(n); + _drawn = Own(n); _boneRot = Own(n); _skin = Own(n); _minMax = Own(2); @@ -133,11 +136,14 @@ namespace Aetheria.FaunaDisplay // Leaving Full the tendrils ease to rest over BlendSeconds of drawn time (no snap); the solver still draws, with no steps. double dt = _started ? math.max(drawnTime - _lastTime, 0) : 0; _lastTime = drawnTime; + bool midEase = _everFull && _restWeight < 1f; // a return to Full from here starts from the pose last drawn, not from rest if (full) { _restWeight = 0f; _everFull = true; _lastFrac = frac; } else if (_everFull) _restWeight = (float)math.min(1.0, _restWeight + dt / math.max(BlendSeconds, 1e-4f)); bool easing = !full && _everFull && _restWeight < 1f; - bool reset = !_started || (full && !_wasFull) || math.distance(_lastPosition, position) > Data.MaxChainLength * scale; + bool jumped = math.distance(_lastPosition, position) > Data.MaxChainLength * scale; + bool reset = !_started || (full && !_wasFull) || jumped; + bool seedDrawn = reset && _started && !jumped && full && !_wasFull && midEase; _started = true; _wasFull = full; _lastPosition = position; @@ -159,6 +165,7 @@ namespace Aetheria.FaunaDisplay chainStart = _chainStart, chainLen = _chainLen, chainBone = _chainBone, chainColStart = _chainColStart, chainColLen = _chainColLen, chainCol = _chainCol, restPos = _restPos, restRot = _restRot, param = _param, colliders = _colliders, bonePos = _bonePos, boneRot = _boneRot, simPos = _simPos, simPrev = _simPrev, + drawn = _drawn, seedDrawn = seedDrawn, substeps = steps, step = 1f / SubstepRate, scale = scale, frac = full ? frac : _lastFrac, blend = full ? 0f : _restWeight, reset = reset, }.Schedule(Data.ChainCount, 4, pose); } @@ -168,6 +175,7 @@ namespace Aetheria.FaunaDisplay }.Schedule(Data.BoneCount, 64, pose); new BoundsJob { bonePos = _bonePos, rootPos = pos, rootRot = rot, scale = scale, minMax = _minMax }.Schedule(skinned).Complete(); SkinBuffer.SetData(_skin); + _drawn.CopyFrom(_bonePos); var t = Renderer.transform; t.SetPositionAndRotation(position, rotation); @@ -199,6 +207,15 @@ namespace Aetheria.FaunaDisplay SkinBuffer = null; } + // From the serialized Rig and this object's mesh. Awake calls it in play; an editor tool that instantiates the asset outside play + // (Awake does not run there) calls it itself. + public void Initialize() => Initialize(SpringRigData.FromGlb(Rig.bytes), GetComponent().sharedMesh.bounds); + + void Awake() + { + if (Rig != null && Data == null) Initialize(); + } + void OnDestroy() => Release(); } } diff --git a/Assets/Scripts/Fauna Display/SpringSolver.cs b/Assets/Scripts/Fauna Display/SpringSolver.cs index 79dd2e0d1..c576a343c 100644 --- a/Assets/Scripts/Fauna Display/SpringSolver.cs +++ b/Assets/Scripts/Fauna Display/SpringSolver.cs @@ -50,6 +50,9 @@ namespace Aetheria.FaunaDisplay [ReadOnly] public NativeArray restRot; [ReadOnly] public NativeArray param; [ReadOnly] public NativeArray colliders; + // The bone positions drawn last frame; a reset seeds from them when seedDrawn (a return to Full part way through the ease), from rest otherwise. + [ReadOnly] public NativeArray drawn; + public bool seedDrawn; // Bone-indexed. A chain writes only its own bones; colliders read body bones only. [NativeDisableParallelForRestriction] public NativeArray bonePos, simPos, simPrev; [NativeDisableParallelForRestriction] public NativeArray boneRot; @@ -64,7 +67,7 @@ namespace Aetheria.FaunaDisplay for (int k = 0; k < len; k++) { int bone = chainBone[start + k]; - simPos[bone] = simPrev[bone] = bonePos[bone]; + simPos[bone] = simPrev[bone] = seedDrawn ? drawn[bone] : bonePos[bone]; } int root = chainBone[start]; float3 headPos0 = bonePos[root];