fauna: interpolated drawn pose, bounds from the pose, LOD hysteresis, one x-mirror owner, two more refusals (whale-rig-s1 fix batch)
The solver draws the last two fixed steps blended by the leftover fraction, pushed out of colliders and length-held from this frame's head, so a frame that owes no substep still moves and keeps segment length. Renderer bounds follow every bone each frame. A level is left only past a hysteresis margin. SpringRigData mirrors x as glTFast does (one owner). Colliders on spring joints and zero-length segments are refused. The promise below 30 fps is the clamp, stated. Stiffness is documented as a dimensionless rate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7a982d2a0d
commit
348ed0798f
3 changed files with 108 additions and 37 deletions
|
|
@ -19,7 +19,10 @@ namespace Aetheria.FaunaDisplay
|
|||
// skin matrices (SpringSkin.cginc) from the mesh's BLENDINDICES and BLENDWEIGHTS.
|
||||
public sealed class SpringRig : MonoBehaviour
|
||||
{
|
||||
// The solver's fixed substep, in drawn seconds, and the most substeps one frame runs (a hitch drops the rest).
|
||||
// The solver's fixed substep, in drawn seconds, and the most substeps one frame runs. The two cannot both hold below
|
||||
// MaxSubsteps x SubstepRate = 30 frames a second: there a frame owes more than 4 and the tendrils run slow (24 fps: at
|
||||
// 80% of drawn time), which is the clamp's purpose (a hitch or a slow machine must not spiral). At 30 fps and above
|
||||
// the count over any span is floor(span x 120) up to rounding at a boundary, which the interpolated pose hides.
|
||||
public const int SubstepRate = 120;
|
||||
public const int MaxSubsteps = 4;
|
||||
|
||||
|
|
@ -29,6 +32,8 @@ namespace Aetheria.FaunaDisplay
|
|||
public float FullAbove = 0.06f;
|
||||
[Tooltip("Below FullAbove and at or above this the tendrils hold their rest pose on the swimming body; below it the caller hides them.")]
|
||||
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;
|
||||
public Camera LodCamera;
|
||||
|
||||
public SpringRigData Data { get; private set; }
|
||||
|
|
@ -50,6 +55,8 @@ namespace Aetheria.FaunaDisplay
|
|||
bool _started, _wasFull;
|
||||
long _lastIndex;
|
||||
float3 _lastPosition;
|
||||
float _pad;
|
||||
float _lodDiameter;
|
||||
|
||||
readonly System.Collections.Generic.List<IDisposable> _owned = new System.Collections.Generic.List<IDisposable>();
|
||||
|
||||
|
|
@ -84,9 +91,20 @@ namespace Aetheria.FaunaDisplay
|
|||
var block = new MaterialPropertyBlock();
|
||||
block.SetBuffer("_SpringBones", SkinBuffer);
|
||||
Renderer.SetPropertyBlock(block);
|
||||
// Tendrils swing beyond the rest mesh by at most their length.
|
||||
meshBounds.Expand(data.MaxChainLength * 2f);
|
||||
// The mesh reaches past its bones by the rest mesh's margin; each frame's bounds are the bones' reach plus that margin.
|
||||
var restPos = new float3[n]; var restRot = new quaternion[n];
|
||||
float3 lo = new float3(float.MaxValue), hi = new float3(float.MinValue);
|
||||
foreach (int i in data._order)
|
||||
{
|
||||
int p = data._parent[i];
|
||||
restPos[i] = (p < 0 ? default : restPos[p]) + math.mul(p < 0 ? quaternion.identity : restRot[p], data._restPos[i]);
|
||||
restRot[i] = math.mul(p < 0 ? quaternion.identity : restRot[p], data._restRot[i]);
|
||||
lo = math.min(lo, restPos[i]); hi = math.max(hi, restPos[i]);
|
||||
}
|
||||
_lodDiameter = meshBounds.extents.magnitude * 2f;
|
||||
Renderer.localBounds = meshBounds;
|
||||
// One radius all round (the mesh turns with its bones): the rest mesh's largest margin past its bones, per axis, as a length.
|
||||
_pad = math.length(math.max(math.max(lo - (float3)meshBounds.min, (float3)meshBounds.max - hi), 0f));
|
||||
_started = false;
|
||||
}
|
||||
|
||||
|
|
@ -102,6 +120,7 @@ namespace Aetheria.FaunaDisplay
|
|||
long index = (long)math.floor(drawnTime * SubstepRate);
|
||||
int steps = _started && full ? (int)math.clamp(index - _lastIndex, 0, MaxSubsteps) : 0;
|
||||
_lastIndex = index;
|
||||
float frac = (float)(drawnTime * SubstepRate - index);
|
||||
|
||||
bool reset = !_started || (full && !_wasFull) || math.distance(_lastPosition, position) > Data.MaxChainLength * scale;
|
||||
_started = true;
|
||||
|
|
@ -125,7 +144,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,
|
||||
substeps = steps, step = 1f / SubstepRate, scale = scale, reset = reset,
|
||||
substeps = steps, step = 1f / SubstepRate, scale = scale, frac = frac, reset = reset,
|
||||
}.Schedule(Data.ChainCount, 4, pose);
|
||||
}
|
||||
new SkinJob
|
||||
|
|
@ -137,6 +156,17 @@ namespace Aetheria.FaunaDisplay
|
|||
var t = Renderer.transform;
|
||||
t.SetPositionAndRotation(position, rotation);
|
||||
t.localScale = new Vector3(scale, scale, scale);
|
||||
|
||||
// Bounds follow the pose: every bone in object space, plus the rest mesh's margin around the bones.
|
||||
var inverse = math.inverse(rot);
|
||||
float3 min = new float3(float.MaxValue), max = new float3(float.MinValue);
|
||||
for (int i = 0; i < _bonePos.Length; i++)
|
||||
{
|
||||
float3 local = math.mul(inverse, _bonePos[i] - pos) / scale;
|
||||
min = math.min(min, local); max = math.max(max, local);
|
||||
}
|
||||
min -= _pad; max += _pad;
|
||||
Renderer.localBounds = new Bounds((min + max) * 0.5f, max - min);
|
||||
return Level;
|
||||
}
|
||||
|
||||
|
|
@ -145,10 +175,11 @@ namespace Aetheria.FaunaDisplay
|
|||
{
|
||||
var cam = LodCamera;
|
||||
if (cam == null) return SpringLod.Full;
|
||||
float diameter = Renderer.localBounds.extents.magnitude * 2f * scale;
|
||||
float diameter = _lodDiameter * scale;
|
||||
float visible = cam.orthographic ? cam.orthographicSize * 2f : 2f * Vector3.Distance(cam.transform.position, position) * Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
|
||||
float height = diameter / Mathf.Max(visible, 1e-6f);
|
||||
return height >= FullAbove ? SpringLod.Full : height >= FrozenAbove ? SpringLod.Frozen : SpringLod.Hidden;
|
||||
float height = diameter / Mathf.Max(visible, 1e-6f), stay = 1f - Hysteresis;
|
||||
if (height >= FullAbove * (Level == SpringLod.Full ? stay : 1f)) return SpringLod.Full;
|
||||
return height >= FrozenAbove * (Level != SpringLod.Hidden ? stay : 1f) ? SpringLod.Frozen : SpringLod.Hidden;
|
||||
}
|
||||
|
||||
void Release()
|
||||
|
|
|
|||
|
|
@ -13,6 +13,11 @@ using Unity.Mathematics;
|
|||
namespace Aetheria.FaunaDisplay
|
||||
{
|
||||
// One spring joint's VRMC_springBone parameters. They drive the segment that ends at the NEXT joint of the chain.
|
||||
// Stiffness is a RATE in 1/s and dimensionless in length: each substep the joint closes saturate(Stiffness x step) of its
|
||||
// distance to the rest target, so the same number moves a 0.1 u tendril and a 10 u one by the same fraction. VRMC_springBone
|
||||
// gives stiffness a length-per-second unit (a fixed-magnitude nudge along the rest axis), so a rig authored by a VRM tool
|
||||
// reads this field with a different meaning; the Blender builder (whale-rig-builder) authors it as a rate. GravityPower is
|
||||
// a displacement per second in rig units and DragForce a fraction of velocity lost per substep, as in VRM.
|
||||
public readonly struct SpringJointParams
|
||||
{
|
||||
public readonly float HitRadius, Stiffness, GravityPower, DragForce;
|
||||
|
|
@ -35,6 +40,10 @@ namespace Aetheria.FaunaDisplay
|
|||
// The parsed rig: a glTF skin plus VRMC_springBone 1.0 (the seam with the Blender builder). Bone index = skin joint
|
||||
// index, so the mesh's BLENDINDICES address the skin matrices directly. Skin joints no spring names are body bones.
|
||||
// Immutable after FromGlbJson; presentation only (nothing in ServerShared reads it).
|
||||
// HANDEDNESS: glTF is right-handed, Unity left-handed, and glTFast mirrors x on everything it imports (positions, normals,
|
||||
// node translations (-x,y,z), rotations (x,-y,-z,w), bind poses S M S with S = diag(-1,1,1)). FromGlbJson is the one owner
|
||||
// of the same mirror for the rig: every vector, rotation, matrix, collider offset and gravity direction it returns is in
|
||||
// Unity space, matching a glTFast-imported mesh and node tree. Nothing downstream converts.
|
||||
public sealed class SpringRigData
|
||||
{
|
||||
public int BoneCount => _parent.Length;
|
||||
|
|
@ -182,6 +191,9 @@ namespace Aetheria.FaunaDisplay
|
|||
}
|
||||
springBones.Add(bones);
|
||||
}
|
||||
for (int ci = 0; ci < d._colliders.Length; ci++)
|
||||
if (!d._isBody[d._colliders[ci].Bone])
|
||||
throw new InvalidDataException($"Collider {ci} sits on node {joints[d._colliders[ci].Bone]}, a spring joint; colliders ride body bones.");
|
||||
for (int si = 0; si < springBones.Count; si++)
|
||||
{
|
||||
var bones = springBones[si];
|
||||
|
|
@ -191,6 +203,8 @@ namespace Aetheria.FaunaDisplay
|
|||
int parent = d._parent[bones[k]];
|
||||
if (k == 0 && (parent < 0 || !d._isBody[parent]))
|
||||
throw new InvalidDataException($"Spring '{name}': first joint (node {joints[bones[0]]}) must hang from a body bone.");
|
||||
if (k > 0 && math.lengthsq(d._restPos[bones[k]]) < 1e-12f)
|
||||
throw new InvalidDataException($"Spring '{name}': joint {k} (node {joints[bones[k]]}) sits on its predecessor; a segment needs a length.");
|
||||
if (k > 0 && parent != bones[k - 1])
|
||||
throw new InvalidDataException($"Spring '{name}': joint {k} (node {joints[bones[k]]}) has parent {(parent < 0 ? "none" : "node " + joints[parent])}, not its predecessor node {joints[bones[k - 1]]}.");
|
||||
}
|
||||
|
|
@ -265,7 +279,8 @@ namespace Aetheria.FaunaDisplay
|
|||
return d;
|
||||
}
|
||||
|
||||
static float3 Vec3(JToken t, float3 fallback) => t is JArray a && a.Count == 3 ? new float3((float)a[0], (float)a[1], (float)a[2]) : fallback;
|
||||
// A glTF vector read into Unity space (x mirrored).
|
||||
static float3 Vec3(JToken t, float3 fallback) => t is JArray a && a.Count == 3 ? new float3(-(float)a[0], (float)a[1], (float)a[2]) : fallback;
|
||||
|
||||
static (float3, quaternion) LocalRest(JArray nodes, int node)
|
||||
{
|
||||
|
|
@ -275,7 +290,7 @@ namespace Aetheria.FaunaDisplay
|
|||
throw new InvalidDataException($"Node {node} has a non-unit scale; bake it before export.");
|
||||
float3 t = Vec3(nd["translation"], default);
|
||||
var r = nd["rotation"] as JArray;
|
||||
return (t, r != null && r.Count == 4 ? math.normalize(new quaternion((float)r[0], (float)r[1], (float)r[2], (float)r[3])) : quaternion.identity);
|
||||
return (t, r != null && r.Count == 4 ? math.normalize(new quaternion((float)r[0], -(float)r[1], -(float)r[2], (float)r[3])) : quaternion.identity);
|
||||
}
|
||||
|
||||
static float4x4[] ReadBindPoses(JObject json, JObject skin, byte[] bin, int count)
|
||||
|
|
@ -292,7 +307,8 @@ namespace Aetheria.FaunaDisplay
|
|||
{
|
||||
var f = new float[16];
|
||||
Buffer.BlockCopy(bin, at + i * 64, f, 0, 64);
|
||||
result[i] = new float4x4(f[0], f[4], f[8], f[12], f[1], f[5], f[9], f[13], f[2], f[6], f[10], f[14], f[3], f[7], f[11], f[15]); // glTF is column-major
|
||||
// glTF is column-major; S M S negates the x row and column but for m00.
|
||||
result[i] = new float4x4(f[0], -f[4], -f[8], -f[12], -f[1], f[5], f[9], f[13], -f[2], f[6], f[10], f[14], f[3], f[7], f[11], f[15]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ using Unity.Mathematics;
|
|||
namespace Aetheria.FaunaDisplay
|
||||
{
|
||||
// The three jobs one whale runs each frame, in order: PoseJob (every bone at its swimming-body pose, springs at rest),
|
||||
// SpringSolveJob (the VRM SpringBone step over chains, overwriting the spring bones; skipped when frozen) and SkinJob
|
||||
// SpringSolveJob (the VRM SpringBone step over chains, then the interpolated drawn pose, overwriting the spring bones; skipped when frozen) and SkinJob
|
||||
// (skin = rootInverse x boneWorld x bindPose for every bone). Bone worlds are position + rotation with a uniform scale;
|
||||
// the solver works in world space so a swimming root drags the tendrils.
|
||||
|
||||
|
|
@ -54,7 +54,7 @@ namespace Aetheria.FaunaDisplay
|
|||
[NativeDisableParallelForRestriction] public NativeArray<float3> bonePos, simPos, simPrev;
|
||||
[NativeDisableParallelForRestriction] public NativeArray<quaternion> boneRot;
|
||||
public int substeps;
|
||||
public float step, scale;
|
||||
public float step, scale, frac;
|
||||
public bool reset;
|
||||
|
||||
public void Execute(int c)
|
||||
|
|
@ -70,43 +70,67 @@ namespace Aetheria.FaunaDisplay
|
|||
float3 headPos0 = bonePos[root];
|
||||
quaternion headRest0 = boneRot[root];
|
||||
|
||||
// With no substeps due the loop still runs once without integrating, to publish the held state.
|
||||
int passes = math.max(substeps, 1);
|
||||
for (int pass = 0; pass < passes; pass++)
|
||||
// The fixed steps: the VRM SpringBone step per joint, head to tip, against the body as posed this frame.
|
||||
for (int s = 0; s < substeps; s++)
|
||||
{
|
||||
bool integrate = pass < substeps;
|
||||
float3 headPos = headPos0;
|
||||
quaternion headRest = headRest0;
|
||||
for (int k = 1; k < len; k++)
|
||||
{
|
||||
int bone = chainBone[start + k];
|
||||
var jp = param[start + k - 1];
|
||||
float3 p = simPos[bone];
|
||||
float seg = math.length(restPos[bone]) * scale;
|
||||
float3 axis = math.normalizesafe(restPos[bone], new float3(0, -1, 0));
|
||||
float3 np = p;
|
||||
if (integrate)
|
||||
{
|
||||
var jp = param[start + k - 1];
|
||||
float3 target = headPos + math.mul(headRest, restPos[bone] * scale);
|
||||
np = p + (p - simPrev[bone]) * (1f - jp.DragForce) // inertia
|
||||
+ (target - p) * math.saturate(jp.Stiffness * step) // pull to the rest direction from the parent's current rotation
|
||||
+ jp.GravityDir * (jp.GravityPower * step * scale); // gravity
|
||||
np = Reproject(headPos, np, seg, math.mul(headRest, axis));
|
||||
float hit = jp.HitRadius * scale;
|
||||
int cs = chainColStart[c], cn = chainColLen[c];
|
||||
for (int q = 0; q < cn; q++) np = PushOut(np, colliders[chainCol[cs + q]], hit);
|
||||
np = Reproject(headPos, np, seg, math.mul(headRest, axis));
|
||||
simPrev[bone] = p;
|
||||
simPos[bone] = np;
|
||||
}
|
||||
quaternion aimed = math.mul(FromTo(math.mul(headRest, axis), math.normalizesafe(np - headPos, math.mul(headRest, axis))), headRest);
|
||||
boneRot[chainBone[start + k - 1]] = aimed;
|
||||
bonePos[bone] = np;
|
||||
float3 target = headPos + math.mul(headRest, restPos[bone] * scale);
|
||||
float3 np = p + (p - simPrev[bone]) * (1f - jp.DragForce) // inertia
|
||||
+ (target - p) * math.saturate(jp.Stiffness * step) // pull to the rest direction from the parent's current rotation
|
||||
+ jp.GravityDir * (jp.GravityPower * step * scale); // gravity
|
||||
np = Reproject(headPos, np, seg, math.mul(headRest, axis));
|
||||
float hit = jp.HitRadius * scale;
|
||||
int cs = chainColStart[c], cn = chainColLen[c];
|
||||
for (int q = 0; q < cn; q++) np = PushOut(np, colliders[chainCol[cs + q]], hit);
|
||||
np = Reproject(headPos, np, seg, math.mul(headRest, axis));
|
||||
simPrev[bone] = p;
|
||||
simPos[bone] = np;
|
||||
headRest = math.mul(Aim(headRest, axis, np - headPos), restRot[bone]);
|
||||
headPos = np;
|
||||
headRest = math.mul(aimed, restRot[bone]);
|
||||
}
|
||||
boneRot[chainBone[start + len - 1]] = headRest;
|
||||
}
|
||||
|
||||
// The pose drawn: the last two fixed steps blended by the leftover fraction of a step (so a frame that owes no
|
||||
// step still moves), then pushed out of the colliders and the lengths held again from this frame's head. The state itself is not touched.
|
||||
float3 head = headPos0;
|
||||
quaternion rest = headRest0;
|
||||
for (int k = 1; k < len; k++)
|
||||
{
|
||||
int bone = chainBone[start + k];
|
||||
float seg = math.length(restPos[bone]) * scale;
|
||||
float3 axis = math.normalizesafe(restPos[bone], new float3(0, -1, 0));
|
||||
var jp = param[start + k - 1];
|
||||
float3 fall = math.mul(rest, axis);
|
||||
float3 p = Reproject(head, math.lerp(simPrev[bone], simPos[bone], frac), seg, fall);
|
||||
int cs = chainColStart[c], cn = chainColLen[c];
|
||||
// A blend of two pushed-out points can dip into a collider; two rounds of push-out and length settle it.
|
||||
for (int round = 0; round < 2 && cn > 0; round++)
|
||||
{
|
||||
for (int q = 0; q < cn; q++) p = PushOut(p, colliders[chainCol[cs + q]], jp.HitRadius * scale);
|
||||
p = Reproject(head, p, seg, fall);
|
||||
}
|
||||
quaternion aimed = Aim(rest, axis, p - head);
|
||||
boneRot[chainBone[start + k - 1]] = aimed;
|
||||
bonePos[bone] = p;
|
||||
head = p;
|
||||
rest = math.mul(aimed, restRot[bone]);
|
||||
}
|
||||
boneRot[chainBone[start + len - 1]] = rest;
|
||||
}
|
||||
|
||||
// The head's rotation once its bone axis (in its rest frame) points along dir.
|
||||
static quaternion Aim(quaternion headRest, float3 axis, float3 dir)
|
||||
{
|
||||
float3 from = math.mul(headRest, axis);
|
||||
return math.mul(FromTo(from, math.normalizesafe(dir, from)), headRest);
|
||||
}
|
||||
|
||||
static float3 Reproject(float3 head, float3 p, float length, float3 fallback) => head + math.normalizesafe(p - head, fallback) * length;
|
||||
|
|
|
|||
Loading…
Reference in a new issue