diff --git a/tests/Aetheria.Shared.Tests/ShipFixture.cs b/tests/Aetheria.Shared.Tests/ShipFixture.cs index eb8945780..6f32b6389 100644 --- a/tests/Aetheria.Shared.Tests/ShipFixture.cs +++ b/tests/Aetheria.Shared.Tests/ShipFixture.cs @@ -2,33 +2,62 @@ using System; using System.IO; using System.Linq; using System.Text; +using CultMath; -// Writes the mod package the tests, the Python round-trip check and the Unity smoke (ShipModPreview.Smoke) share. It is -// generated from ShipAuthoringTests.Fixture() so no opaque bytes are committed. +// Writes the mod package the tests, the Python round-trip check and the Unity smokes (ShipModPreview.Smoke and +// ShipModPlaySmoke) share. It is generated from ShipAuthoringTests.Fixture() so no opaque bytes are committed. internal static class ShipFixture { - public static readonly string[] Nodes = { "map", "collider", "shield", "tractor", "thruster-port" }; + public static readonly string[] Nodes = { "map", "collider", "shield", "tractor", "thruster-port", "gun", "gun-muzzle" }; + + // The nodes that carry the fixture mesh: the anchors the assembler reads a renderer or a collider mesh from. + public static readonly string[] MeshNodes = { "map", "collider", "thruster-port" }; + + // One tetrahedron, non-indexed (12 vertices), so a convex MeshCollider can cook it. Written by the generator. + private static readonly float[] Tetrahedron = + { + 0, 0, 0, 0, 1, 0, 1, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 1, 0, 1, 0, + 1, 0, 0, 0, 1, 0, 0, 0, 1 + }; + + public static byte[] MeshBytes() => Tetrahedron.SelectMany(BitConverter.GetBytes).ToArray(); // The GLB carries a glTF scene naming every node: glTFast instantiates a scene, and a scene-less file has none. - public static string GlbJson(string[] nodes) => - @"{""asset"":{""version"":""2.0""},""scene"":0,""scenes"":[{""nodes"":[" + string.Join(",", nodes.Select((_, index) => index)) + @"]}],""nodes"":[" + - string.Join(",", nodes.Select(node => $@"{{""name"":""{node}"",""extras"":{{""aetheria.id"":""{node}""}}}}")) + "]}"; + // Nodes in MeshNodes get the shared tetrahedron mesh, whose vertices live in the BIN chunk (MeshBytes). + public static string GlbJson(string[] nodes) + { + var length = MeshBytes().Length; + var mesh = nodes.Any(MeshNodes.Contains) + ? @",""meshes"":[{""primitives"":[{""attributes"":{""POSITION"":0}}]}]," + + @"""accessors"":[{""bufferView"":0,""componentType"":5126,""count"":12,""type"":""VEC3"",""min"":[0,0,0],""max"":[1,1,1]}]," + + $@"""bufferViews"":[{{""buffer"":0,""byteLength"":{length}}}],""buffers"":[{{""byteLength"":{length}}}]" + : ""; + return @"{""asset"":{""version"":""2.0""},""scene"":0,""scenes"":[{""nodes"":[" + string.Join(",", nodes.Select((_, index) => index)) + @"]}]" + mesh + @",""nodes"":[" + + string.Join(",", nodes.Select(node => + $@"{{""name"":""{node}""{(MeshNodes.Contains(node) ? @",""mesh"":0" : "")},""extras"":{{""aetheria.id"":""{node}""}}}}")) + "]}"; + } - public static byte[] Glb(string json) + public static byte[] Glb(string json, byte[] bin = null) { var body = Encoding.UTF8.GetBytes(json); var padded = new byte[(body.Length + 3) / 4 * 4]; Array.Fill(padded, (byte)' '); body.CopyTo(padded, 0); - var bytes = new byte[20 + padded.Length + 8]; + bin ??= Array.Empty(); + var binPadded = new byte[(bin.Length + 3) / 4 * 4]; + bin.CopyTo(binPadded, 0); + var bytes = new byte[28 + padded.Length + binPadded.Length]; BitConverter.GetBytes(0x46546C67u).CopyTo(bytes, 0); BitConverter.GetBytes(2u).CopyTo(bytes, 4); BitConverter.GetBytes((uint)bytes.Length).CopyTo(bytes, 8); BitConverter.GetBytes((uint)padded.Length).CopyTo(bytes, 12); BitConverter.GetBytes(0x4E4F534Au).CopyTo(bytes, 16); padded.CopyTo(bytes, 20); - BitConverter.GetBytes(0u).CopyTo(bytes, 20 + padded.Length); + BitConverter.GetBytes((uint)binPadded.Length).CopyTo(bytes, 20 + padded.Length); BitConverter.GetBytes(0x004E4942u).CopyTo(bytes, 24 + padded.Length); + binPadded.CopyTo(bytes, 28 + padded.Length); return bytes; } @@ -37,6 +66,11 @@ internal static class ShipFixture string modelAsset = "skiff.glb", Action tweak = null) { var ship = ShipAuthoringTests.Fixture(); + // A playable rig: the thruster outside the schematic, one energy weapon mount inside it, with its muzzle. + ship.Hull.Hardpoints[0].Position = new int2(2, 0); + ship.Hull.Hardpoints.Add(new HardpointData { Type = HardpointType.Energy, Position = new int2(0, 0), Shape = new Shape(), Transform = "gun" }); + ship.Visual.Anchors.Add(new ShipAnchor { Id = "gun", Role = "articulation", ModelNodeId = "gun" }); + ship.Visual.Anchors.Add(new ShipAnchor { Id = "gun.muzzle", Role = "weapon-muzzle", ModelNodeId = "gun-muzzle", ParentId = "gun" }); tweak?.Invoke(ship); ship.WithId(id); ship.Hull.Name = hullName; @@ -48,7 +82,7 @@ internal static class ShipFixture ShipAuthoringStore.Write(cache, ship.Hull, ship.Visual); cache.FlushAsync().Wait(); } - File.WriteAllBytes(Path.Combine(directory, modelAsset), Glb(GlbJson(nodes ?? Nodes))); + File.WriteAllBytes(Path.Combine(directory, modelAsset), Glb(GlbJson(nodes ?? Nodes), MeshBytes())); return directory; } } diff --git a/tests/Aetheria.Shared.Tests/ShipModCatalogTests.cs b/tests/Aetheria.Shared.Tests/ShipModCatalogTests.cs index 7db843d30..7598ba4cf 100644 --- a/tests/Aetheria.Shared.Tests/ShipModCatalogTests.cs +++ b/tests/Aetheria.Shared.Tests/ShipModCatalogTests.cs @@ -123,6 +123,33 @@ public sealed class ShipModCatalogTests : IDisposable foreach (var name in new[] { "ship.cc", "skiff.glb" }) File.Copy(Path.Combine(directory, name), Path.Combine(target, name), true); } + [Fact] + public void TheFixtureGlbCarriesARealMeshOnEveryAnchorTheAssemblerReadsOneFrom() + { + var package = ShipModCatalog.ReadPackage(Path.Combine(ShipFixture.WritePackage(Mods, "mod.skiff"), "ship.cc")); + var glb = File.ReadAllBytes(package.ModelPath); + var jsonLength = (int)BitConverter.ToUInt32(glb, 12); + var json = JObject.Parse(Encoding.UTF8.GetString(glb, 20, jsonLength)); + var binLength = (int)BitConverter.ToUInt32(glb, 20 + jsonLength); + Assert.Equal(0x004E4942u, BitConverter.ToUInt32(glb, 24 + jsonLength)); + Assert.Equal(glb.Length, 28 + jsonLength + binLength); + Assert.Equal((int)json["buffers"][0]["byteLength"], ShipFixture.MeshBytes().Length); + Assert.True(binLength >= ShipFixture.MeshBytes().Length); + + var roles = new[] { "map-icon", "hull-collider", "thruster-emitter", "radiator-mesh" }; + var needed = package.Visual.Anchors.Where(anchor => roles.Contains(anchor.Role)).ToArray(); + Assert.Contains(needed, anchor => anchor.Role == "thruster-emitter"); + foreach (var anchor in needed) + { + var node = json["nodes"][(int)package.NodeIndices[anchor.ModelNodeId]]; + var mesh = json["meshes"][(int)node["mesh"]]; + var accessor = json["accessors"][(int)mesh["primitives"][0]["attributes"]["POSITION"]]; + Assert.True((int)accessor["count"] >= 4, anchor.Id); + } + Assert.Contains(package.Visual.Anchors, anchor => anchor.Role == "weapon-muzzle" && anchor.ParentId == "gun"); + Assert.Contains(package.Hull.Hardpoints, hardpoint => hardpoint.Type == HardpointType.Energy && hardpoint.Transform == "gun"); + } + [Fact] public void ComposeCountsEveryPackageAndReplacesAnExistingDerivedCatalog() {