mirror of
https://git.triforceproject.com/triforce/source
synced 2026-12-01 05:18:50 -04:00
Triforce v0.9.0
Character tab, faster renderer defaults, HD rumble shipped, and the 11-30 bug pass: ending softlock below 60 fps, adult first-night lock, Intel Arc robust buffers, adult map, randomizer load order.
This commit is contained in:
107
launcher/Core/GameFiles/Fska.cs
Normal file
107
launcher/Core/GameFiles/Fska.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
// (Triforce, 11-29) FSKA, a skeletal animation inside a BFRES v10 animation file (romfs/Model/*_Animation.anim.bfres.zs),
|
||||
// read far enough to pose a model at one frame: each bone track's base values (frame 0) and its curves sampled at the frame.
|
||||
// Layout as the lab's offline renders read it (export.py anim_base, 11-17), little-endian:
|
||||
// FSKA 0x04 flags (0x1000: rotations are Euler XYZ), 0x08 name, 0x28 bone tracks (0x38 each), 0x4c track count
|
||||
// track 0x00 bone name, 0x08 curves (0x30 each), 0x10 base data, 0x28 flags (0x08 scale, 0x10 rotation, 0x20 translation
|
||||
// in the base data), 0x2e curve count
|
||||
// base scale (3 f32), rotation (4 f32: Euler XYZ and a pad, or a quaternion), translation (3 f32), each when flagged
|
||||
// curve 0x00 frames, 0x08 keys, 0x10 flags (bits 0-1 frame type: 0 f32, 1 s16/32, 2 u8; bits 2-3 key type: 0 f32,
|
||||
// 1 u16, 2 u8), 0x12 key count, 0x14 target offset (0x04.. scale, 0x10.. translation, 0x20.. rotation),
|
||||
// 0x18 start, 0x1c end, 0x24 scale, 0x28 offset (a key is offset + value * scale); keys are sampled with a
|
||||
// Catmull-Rom spline through them
|
||||
using System.Buffers.Binary;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
|
||||
namespace Triforce.Core.GameFiles;
|
||||
|
||||
public sealed record FskaPose(Vector3? Scale, Vector4? Rotation, Vector3? Translation, bool Euler);
|
||||
|
||||
public static class Fska
|
||||
{
|
||||
/// <summary>The bone poses of animation <paramref name="name"/> in this file at <paramref name="frame"/> (null when the file
|
||||
/// has no such animation).</summary>
|
||||
public static Dictionary<string, FskaPose>? Pose(byte[] a, string name, float frame)
|
||||
{
|
||||
ushort U16(long o) => o >= 0 && o + 2 <= a.Length ? BinaryPrimitives.ReadUInt16LittleEndian(a.AsSpan((int)o)) : (ushort)0;
|
||||
uint U32(long o) => o >= 0 && o + 4 <= a.Length ? BinaryPrimitives.ReadUInt32LittleEndian(a.AsSpan((int)o)) : 0;
|
||||
long U64(long o) => o >= 0 && o + 8 <= a.Length ? (long)BinaryPrimitives.ReadUInt64LittleEndian(a.AsSpan((int)o)) : -1;
|
||||
float F(long o) => o >= 0 && o + 4 <= a.Length ? BinaryPrimitives.ReadSingleLittleEndian(a.AsSpan((int)o)) : 0;
|
||||
string S(long o)
|
||||
{
|
||||
if (o <= 0 || o + 2 > a.Length) return "";
|
||||
var n = U16(o);
|
||||
return o + 2 + n <= a.Length ? Encoding.UTF8.GetString(a, (int)o + 2, n) : "";
|
||||
}
|
||||
var magic = "FSKA"u8;
|
||||
for (var f = a.AsSpan().IndexOf(magic); f >= 0;)
|
||||
{
|
||||
if (S(U64(f + 8)) == name)
|
||||
{
|
||||
var euler = (U32(f + 4) & 0x1000) != 0;
|
||||
long tracks = U64(f + 0x28);
|
||||
int n = U16(f + 0x4c);
|
||||
var poses = new Dictionary<string, FskaPose>(StringComparer.Ordinal);
|
||||
for (var i = 0; i < n && i < 4096; i++)
|
||||
{
|
||||
var o = tracks + 0x38L * i;
|
||||
var bone = S(U64(o));
|
||||
long k = U64(o + 0x10);
|
||||
var fl = U32(o + 0x28);
|
||||
float[]? sc = null, rot = null, tr = null;
|
||||
if ((fl & 0x08) != 0) { sc = new[] { F(k), F(k + 4), F(k + 8) }; k += 12; }
|
||||
if ((fl & 0x10) != 0) { rot = new[] { F(k), F(k + 4), F(k + 8), F(k + 12) }; k += 16; }
|
||||
// (a file may mix them: an Euler track keeps 1 in its fourth value, a quaternion track does not unless it is
|
||||
// the identity, which reads the same either way)
|
||||
var trackEuler = rot == null ? euler : euler || MathF.Abs(rot[3] - 1) < 1e-6f;
|
||||
if ((fl & 0x20) != 0) { tr = new[] { F(k), F(k + 4), F(k + 8) }; k += 12; }
|
||||
int nc = o + 0x2e < a.Length ? a[o + 0x2e] : 0;
|
||||
long cv = U64(o + 8);
|
||||
for (var c = 0; c < nc; c++)
|
||||
{
|
||||
var co = cv + 0x30L * c;
|
||||
long frames = U64(co), keys = U64(co + 8);
|
||||
int cfl = U16(co + 0x10), nk = U16(co + 0x12);
|
||||
var target = U32(co + 0x14);
|
||||
float scale = F(co + 0x24), offset = F(co + 0x28);
|
||||
if (nk <= 0 || nk > 100000) continue;
|
||||
int ft = cfl & 3, kt = (cfl >> 2) & 3;
|
||||
var fr = new float[nk];
|
||||
var val = new float[nk];
|
||||
for (var j = 0; j < nk; j++)
|
||||
{
|
||||
fr[j] = ft == 2 ? (frames + j < a.Length ? a[frames + j] : 0) : ft == 1 ? (short)U16(frames + 2 * j) / 32f : F(frames + 4 * j);
|
||||
var kp = keys + 8;
|
||||
val[j] = kt == 2 ? offset + (kp + 2 * j < a.Length ? a[kp + 2 * j] : 0) * scale
|
||||
: kt == 1 ? offset + U16(kp + 4 * j) * scale
|
||||
: F(kp + 8 * j);
|
||||
}
|
||||
var v = Sample(fr, val, frame);
|
||||
if (target >= 0x04 && target <= 0x0c && sc != null) sc[(target - 0x04) / 4] = v;
|
||||
else if (target >= 0x10 && target <= 0x18 && tr != null) tr[(target - 0x10) / 4] = v;
|
||||
else if (target >= 0x20 && target <= 0x2c && rot != null) rot[(target - 0x20) / 4] = v;
|
||||
}
|
||||
poses[bone] = new FskaPose(sc == null ? null : new Vector3(sc[0], sc[1], sc[2]),
|
||||
rot == null ? null : new Vector4(rot[0], rot[1], rot[2], rot[3]), tr == null ? null : new Vector3(tr[0], tr[1], tr[2]), trackEuler);
|
||||
}
|
||||
return poses;
|
||||
}
|
||||
var next = a.AsSpan(f + 4).IndexOf(magic);
|
||||
f = next < 0 ? -1 : f + 4 + next;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>A Catmull-Rom spline through the keys, at frame t (held at the ends).</summary>
|
||||
static float Sample(float[] fr, float[] v, float t)
|
||||
{
|
||||
var n = fr.Length;
|
||||
if (n == 1) return v[0];
|
||||
t = Math.Clamp(t, fr[0], fr[n - 1]);
|
||||
var i = 0;
|
||||
while (i < n - 2 && fr[i + 1] < t) i++;
|
||||
float f0 = fr[i], f1 = fr[i + 1], u = f1 == f0 ? 0 : (t - f0) / (f1 - f0);
|
||||
float p0 = i > 0 ? v[i - 1] : v[i], p1 = v[i], p2 = v[i + 1], p3 = i + 2 < n ? v[i + 2] : v[i + 1];
|
||||
return 0.5f * (2 * p1 + (-p0 + p2) * u + (2 * p0 - 5 * p1 + 4 * p2 - p3) * u * u + (-p0 + 3 * p1 - 3 * p2 + p3) * u * u * u);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user