// (Triforce, 11-29) BFRES v10: the game's model files (romfs/Model/*.bfres.zs), read far enough to draw a character in the // launcher's Character page: the first model's skeleton, its shapes' level-of-detail 0 vertices and triangles, and each // material's name and texture names. Offsets as the lab's offline renders read them (export.py, 11-17), little-endian: // // FRES 0x28 u64 the model array (FMDL, the first one is used), 0x170 u64 where the vertex and index data starts // FMDL 0x18 skeleton, 0x20 vertex buffers, 0x28 shapes (0x60 each), 0x38 materials (0xb0 each); 0x68/0x6a/0x6c counts // FSKL 0x10 bones (0x58 each), 0x18 matrix-to-bone list (u16), 0x20 inverse bind matrices (3x4 f32); // 0x38 bone count, 0x3a smooth matrices, 0x3c rigid matrices // bone 0x00 name, 0x22 parent (s16), 0x2c flags (0x1000 = the rotation is Euler XYZ, else a quaternion), // 0x30 scale (3 f32), 0x3c rotation (4 f32), 0x4c translation (3 f32) // FVTX 0x08 attributes (0x10 each: name, 0x08 format, 0x0c offset, 0x0e buffer), 0x30 buffer sizes, 0x38 strides // (0x10 each), 0x48 u32 the buffers' offset in the data, 0x4c attribute count, 0x4d buffer count, 0x50 vertex count, // 0x54 skin count (0: rigid on the shape's bone, 1: rigid on one matrix with bone-local positions, 2+: smooth) // FSHP 0x08 name, 0x10 vertex buffer, 0x18 meshes (the first is level of detail 0), 0x52 material, 0x58 bone // mesh 0x20 index offset, 0x24 primitive (3 = triangles), 0x28 index format (0 u8, 1 u16, 2 u32), 0x2c count, 0x30 first // FMAT 0x08 name, 0x20 texture names, 0xa3 texture count // Names are inline (u16 length, then the text). Nothing is written back: the Character page only reads. using System.Buffers.Binary; using System.Numerics; using System.Text; namespace Triforce.Core.GameFiles; public sealed record BfresBone(string Name, int Parent, Vector3 Scale, Vector4 Rotation, bool Euler, Vector3 Translation); public sealed record BfresMaterial(string Name, IReadOnlyList Textures); /// One shape at level of detail 0, in the model's bind pose: positions, normals, tangents (w = handedness), the /// first UV set, its bone influences (skeleton bone indices) and triangles. public sealed class BfresShape { public string Name = ""; public int Material; public int SkinCount; public Vector3[] Positions = Array.Empty(); public Vector3[] Normals = Array.Empty(); public Vector4[] Tangents = Array.Empty(); public Vector2[] Uvs = Array.Empty(); /// The second UV set (zero when the shape has none). public Vector2[] Uvs1 = Array.Empty(); /// The vertex colour (_c0), white when the shape has none. public Vector4[] Colors = Array.Empty(); /// Up to 8 influences a vertex: (bone, weight); a rigid vertex has one of weight 1. public (int Bone, float Weight)[][] Influences = Array.Empty<(int, float)[]>(); public int[] Indices = Array.Empty(); } public sealed class Bfres { readonly byte[] b; public IReadOnlyList Bones { get; } public IReadOnlyList Materials { get; } public IReadOnlyList Shapes { get; } /// Each bone's bind pose in model space (parents applied). public Matrix4x4[] BindWorld { get; } public Bfres(byte[] data) { b = data; if (b.Length < 0x180 || Encoding.ASCII.GetString(b, 0, 4) != "FRES") throw new InvalidDataException("not a BFRES file"); var dataStart = (long)U64(0x170); var fmdl = (long)U64(0x28); Need(fmdl, 0x70); if (Encoding.ASCII.GetString(b, (int)fmdl, 4) != "FMDL") throw new InvalidDataException("BFRES: no model"); long skl = (long)U64(fmdl + 0x18), shapesAt = (long)U64(fmdl + 0x28), matsAt = (long)U64(fmdl + 0x38); int nShapes = U16(fmdl + 0x6a), nMats = U16(fmdl + 0x6c); if (nShapes > 512 || nMats > 512) throw new InvalidDataException("BFRES: too many shapes or materials"); // skeleton Need(skl, 0x40); int nBones = U16(skl + 0x38), nSmooth = U16(skl + 0x3a), nRigid = U16(skl + 0x3c); if (nBones > 4096 || nSmooth + nRigid > 8192) throw new InvalidDataException("BFRES: too many bones"); long bonesAt = (long)U64(skl + 0x10), m2bAt = (long)U64(skl + 0x18); var bones = new List(); for (var i = 0; i < nBones; i++) { var o = bonesAt + 0x58 * i; Need(o, 0x58); var flags = U32(o + 0x2c); bones.Add(new BfresBone(Str((long)U64(o)), (short)U16(o + 0x22), V3(o + 0x30), new Vector4(F(o + 0x3c), F(o + 0x40), F(o + 0x44), F(o + 0x48)), (flags & 0x1000) != 0, V3(o + 0x4c))); } Bones = bones; BindWorld = new Matrix4x4[nBones]; for (var i = 0; i < nBones; i++) { var local = Local(bones[i]); var p = bones[i].Parent; BindWorld[i] = p >= 0 && p < i ? local * BindWorld[p] : local; } var m2b = new int[nSmooth + nRigid]; for (var i = 0; i < m2b.Length; i++) m2b[i] = Math.Min(U16(m2bAt + 2 * i), nBones - 1); // materials var mats = new List(); for (var i = 0; i < nMats; i++) { var f = matsAt + 0xb0 * i; Need(f, 0xb0); var tp = (long)U64(f + 0x20); var n = b[f + 0xa3]; var tex = new List(); for (var k = 0; k < n; k++) tex.Add(Str((long)U64(tp + 8 * k))); mats.Add(new BfresMaterial(Str((long)U64(f + 8)), tex)); } Materials = mats; // shapes var shapes = new List(); for (var i = 0; i < nShapes; i++) { var sh = shapesAt + 0x60 * i; Need(sh, 0x60); var vb = (long)U64(sh + 0x10); Need(vb, 0x58); int nAttr = b[vb + 0x4c], nBuf = b[vb + 0x4d], skin = b[vb + 0x54]; var vc = (int)U32(vb + 0x50); if (vc > 2_000_000 || nBuf > 16) throw new InvalidDataException("BFRES: a shape too large"); var bufStart = new long[nBuf]; var strides = new int[nBuf]; var at = dataStart + U32(vb + 0x48); for (var k = 0; k < nBuf; k++) { bufStart[k] = at; var size = U32((long)U64(vb + 0x30) + 0x10 * k); strides[k] = (int)U32((long)U64(vb + 0x38) + 0x10 * k); at += (size + 7) & ~7u; } var attrs = new Dictionary(); for (var k = 0; k < nAttr; k++) { var a = (long)U64(vb + 8) + 0x10 * k; var name = Str((long)U64(a)); var fmt = U32(a + 8); int off = U16(a + 0xc), bi = U16(a + 0xe); if (bi >= nBuf) continue; Need(bufStart[bi], (long)vc * strides[bi]); var vals = new Vector4[vc]; var ok = true; for (var v = 0; v < vc && ok; v++) ok = Attr(fmt, bufStart[bi] + (long)v * strides[bi] + off, out vals[v]); if (ok) attrs[name] = vals; } if (!attrs.TryGetValue("_p0", out var p0)) continue; attrs.TryGetValue("_n0", out var n0); attrs.TryGetValue("_t0", out var t0); attrs.TryGetValue("_u0", out var u0); attrs.TryGetValue("_u1", out var u1); attrs.TryGetValue("_c0", out var c0); attrs.TryGetValue("_i0", out var i0); attrs.TryGetValue("_i1", out var i1); attrs.TryGetValue("_w0", out var w0); attrs.TryGetValue("_w1", out var w1); int shapeBone = Math.Min(U16(sh + 0x58), nBones - 1); var s = new BfresShape { Name = Str((long)U64(sh + 8)), Material = U16(sh + 0x52), SkinCount = skin }; s.Positions = new Vector3[vc]; s.Normals = new Vector3[vc]; s.Tangents = new Vector4[vc]; s.Uvs = new Vector2[vc]; s.Influences = new (int, float)[vc][]; s.Uvs1 = new Vector2[vc]; s.Colors = new Vector4[vc]; for (var v = 0; v < vc; v++) { var p = new Vector3(p0[v].X, p0[v].Y, p0[v].Z); var n = n0 != null ? new Vector3(n0[v].X, n0[v].Y, n0[v].Z) : Vector3.UnitY; var t = t0 != null ? new Vector3(t0[v].X, t0[v].Y, t0[v].Z) : Vector3.UnitX; (int, float)[] inf; if (skin <= 1) { // (rigid: skin 0 on the shape's own bone, skin 1 on one matrix; both store bone-local positions) var bone = skin == 0 || i0 == null ? shapeBone : m2b[Math.Clamp((int)i0[v].X, 0, m2b.Length - 1)]; var w = BindWorld[bone]; p = Vector3.Transform(p, w); n = Vector3.TransformNormal(n, w); t = Vector3.TransformNormal(t, w); inf = new[] { (bone, 1f) }; } else { var l = new List<(int, float)>(8); float[] ia = i0 != null ? new[] { i0[v].X, i0[v].Y, i0[v].Z, i0[v].W } : new float[4]; float[] wa = w0 != null ? new[] { w0[v].X, w0[v].Y, w0[v].Z, w0[v].W } : new[] { 1f, 0, 0, 0 }; for (var k = 0; k < Math.Min(4, skin); k++) l.Add((m2b[Math.Clamp((int)ia[k], 0, m2b.Length - 1)], wa[k])); if (skin > 4 && i1 != null && w1 != null) { float[] ib = { i1[v].X, i1[v].Y, i1[v].Z, i1[v].W }, wb = { w1[v].X, w1[v].Y, w1[v].Z, w1[v].W }; for (var k = 0; k < Math.Min(4, skin - 4); k++) l.Add((m2b[Math.Clamp((int)ib[k], 0, m2b.Length - 1)], wb[k])); } inf = l.ToArray(); } s.Positions[v] = p; s.Normals[v] = n.LengthSquared() > 1e-12f ? Vector3.Normalize(n) : Vector3.UnitY; s.Tangents[v] = new Vector4(t.LengthSquared() > 1e-12f ? Vector3.Normalize(t) : Vector3.UnitX, t0 != null && t0[v].W < 0 ? -1 : 1); s.Uvs[v] = u0 != null ? new Vector2(u0[v].X, u0[v].Y) : Vector2.Zero; s.Uvs1[v] = u1 != null ? new Vector2(u1[v].X, u1[v].Y) : Vector2.Zero; s.Colors[v] = c0 != null ? c0[v] : Vector4.One; s.Influences[v] = inf; } // level of detail 0 var mesh = (long)U64(sh + 0x18); Need(mesh, 0x34); long faceOff = U32(mesh + 0x20); uint prim = U32(mesh + 0x24), ifmt = U32(mesh + 0x28); var count = (int)U32(mesh + 0x2c); var first = (int)U32(mesh + 0x30); if (prim != 3 || ifmt > 2 || count > 12_000_000) continue; var isz = ifmt == 0 ? 1 : ifmt == 1 ? 2 : 4; Need(dataStart + faceOff, (long)count * isz); var idx = new int[count]; for (var k = 0; k < count; k++) { var o = dataStart + faceOff + (long)k * isz; var x = (ifmt == 0 ? b[o] : ifmt == 1 ? U16(o) : (int)U32(o)) + first; idx[k] = x < vc ? x : 0; } s.Indices = idx; shapes.Add(s); } Shapes = shapes; } /// A bone's local transform (scale, then rotation, then translation; row vectors as System.Numerics has them). public static Matrix4x4 Local(BfresBone bone, Quaternion? rotation = null) { var q = rotation ?? Rotation(bone); return Matrix4x4.CreateScale(bone.Scale) * Matrix4x4.CreateFromQuaternion(q) * Matrix4x4.CreateTranslation(bone.Translation); } /// A bone's rotation as a quaternion (Euler XYZ: X first, then Y, then Z). public static Quaternion Rotation(BfresBone bone) { if (!bone.Euler) return Quaternion.Normalize(new Quaternion(bone.Rotation.X, bone.Rotation.Y, bone.Rotation.Z, bone.Rotation.W)); var r = bone.Rotation; return Quaternion.Normalize(Quaternion.CreateFromAxisAngle(Vector3.UnitZ, r.Z) * Quaternion.CreateFromAxisAngle(Vector3.UnitY, r.Y) * Quaternion.CreateFromAxisAngle(Vector3.UnitX, r.X)); } // ---------------- reading ---------------- void Need(long at, long n) { if (at < 0 || n < 0 || at + n > b.Length) throw new InvalidDataException("BFRES: a part lies outside the file"); } ushort U16(long at) { Need(at, 2); return BinaryPrimitives.ReadUInt16LittleEndian(b.AsSpan((int)at)); } uint U32(long at) { Need(at, 4); return BinaryPrimitives.ReadUInt32LittleEndian(b.AsSpan((int)at)); } ulong U64(long at) { Need(at, 8); return BinaryPrimitives.ReadUInt64LittleEndian(b.AsSpan((int)at)); } float F(long at) { Need(at, 4); return BinaryPrimitives.ReadSingleLittleEndian(b.AsSpan((int)at)); } Vector3 V3(long at) => new(F(at), F(at + 4), F(at + 8)); string Str(long at) { if (at <= 0 || at + 2 > b.Length) return ""; var n = U16(at); if (at + 2 + n > b.Length) return ""; return Encoding.UTF8.GetString(b, (int)at + 2, n); } /// One vertex attribute decoded to floats (unused components 0). False for a format this reader does not know. bool Attr(uint fmt, long o, out Vector4 v) { v = default; switch (fmt) { case 0x1805: v = new Vector4(F(o), F(o + 4), F(o + 8), 0); return true; case 0x1905: v = new Vector4(F(o), F(o + 4), F(o + 8), F(o + 12)); return true; case 0x1705: v = new Vector4(F(o), F(o + 4), 0, 0); return true; case 0x1205: Need(o, 4); v = new Vector4((float)BinaryPrimitives.ReadHalfLittleEndian(b.AsSpan((int)o)), (float)BinaryPrimitives.ReadHalfLittleEndian(b.AsSpan((int)o + 2)), 0, 0); return true; case 0x1505: Need(o, 8); v = new Vector4((float)BinaryPrimitives.ReadHalfLittleEndian(b.AsSpan((int)o)), (float)BinaryPrimitives.ReadHalfLittleEndian(b.AsSpan((int)o + 2)), (float)BinaryPrimitives.ReadHalfLittleEndian(b.AsSpan((int)o + 4)), (float)BinaryPrimitives.ReadHalfLittleEndian(b.AsSpan((int)o + 6))); return true; case 0x1202: v = new Vector4(Math.Max((short)U16(o) / 32767f, -1), Math.Max((short)U16(o + 2) / 32767f, -1), 0, 0); return true; case 0x1201: v = new Vector4(U16(o) / 65535f, U16(o + 2) / 65535f, 0, 0); return true; case 0x0e02: { var x = U32(o); float C(int c) { var q = (int)((x >> (10 * c)) & 0x3ff); if (q >= 512) q -= 1024; return Math.Max(q / 511f, -1); } var w = (int)((x >> 30) & 3); if (w >= 2) w -= 4; v = new Vector4(C(0), C(1), C(2), w); return true; } case 0x0201: Need(o, 1); v = new Vector4(b[o] / 255f, 0, 0, 0); return true; case 0x0203: Need(o, 1); v = new Vector4(b[o], 0, 0, 0); return true; case 0x0901: Need(o, 2); v = new Vector4(b[o] / 255f, b[o + 1] / 255f, 0, 0); return true; case 0x0903: Need(o, 2); v = new Vector4(b[o], b[o + 1], 0, 0); return true; case 0x0b01: Need(o, 4); v = new Vector4(b[o] / 255f, b[o + 1] / 255f, b[o + 2] / 255f, b[o + 3] / 255f); return true; case 0x0b03: Need(o, 4); v = new Vector4(b[o], b[o + 1], b[o + 2], b[o + 3]); return true; case 0x0a01: v = new Vector4(U16(o) / 65535f, 0, 0, 0); return true; case 0x0a03: v = new Vector4(U16(o), 0, 0, 0); return true; case 0x1001: v = new Vector4(U16(o) / 65535f, U16(o + 2) / 65535f, 0, 0); return true; case 0x1003: v = new Vector4(U16(o), U16(o + 2), 0, 0); return true; case 0x1501 or 0x1503: { var k = fmt == 0x1501 ? 1 / 65535f : 1f; v = new Vector4(U16(o) * k, U16(o + 2) * k, U16(o + 4) * k, U16(o + 6) * k); return true; } default: return false; } } /// The attribute formats a file uses that this reader skips (for diagnostics). public static string FormatName(uint fmt) => "0x" + fmt.ToString("x4"); }