Aetheria/tests/Aetheria.Shared.Tests/ShipFixture.cs
soul 28666e3550 A directional thruster's emitter is one-sided, and the catalog refuses one that fires along its push
The exhaust is born along each triangle's normal of the emitter mesh, and every Nakovalnya, Wren and starter emitter was
a thin closed box, so it fired both ways along the thruster axis. The generator now writes one side: hull_source keeps
of a hull source's emit mesh only the triangles facing its anchor's yaw (the exhaust), and the starter's thruster boxes
carry a single exhaust face. ShipModCatalog.Bind, which Compose and ship-authoring validate both run, reads the emitter
geometry from the GLB (world transform included) and refuses a directional thruster whose emitter has any face or vertex
normal with a non-negative dot with the push, naming the anchor. The rule is the directional class's alone; a ring
thruster would bring its own. The Nakovalnya and Wren ship.glb are regenerated through Build + Package and the starter;
ship.cc, anchors, node indices and every non-emitter node are unchanged. Blender smokes and the test GLBs now give
thrusters an exhaust-facing emitter and a matching Rotation.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-19 20:47:05 +01:00

99 lines
5.8 KiB
C#

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 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", "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
};
// One-sided quad in the z = 0 plane, wound counter-clockwise seen from +Z, so its normal is +Z (glTF's nose): the emitter
// of a thruster whose Rotation is None, which faces its exhaust ahead. Four vertices and six 16-bit indices follow the
// tetrahedron in the BIN chunk.
private static readonly float[] Quad = { 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0 };
private static readonly ushort[] QuadIndices = { 0, 1, 2, 0, 2, 3 };
public static byte[] MeshBytes() =>
Tetrahedron.SelectMany(BitConverter.GetBytes).Concat(Quad.SelectMany(BitConverter.GetBytes)).Concat(QuadIndices.SelectMany(BitConverter.GetBytes)).ToArray();
// The model's geometry as glTF JSON members (meshes, accessors, bufferViews, buffers) over MeshBytes: mesh 0 is the
// tetrahedron, mesh 1 the quad.
public static string Geometry =>
@"""meshes"":[{""primitives"":[{""attributes"":{""POSITION"":0}}]},{""primitives"":[{""attributes"":{""POSITION"":1},""indices"":2}]}]," +
@"""accessors"":[{""bufferView"":0,""componentType"":5126,""count"":12,""type"":""VEC3"",""min"":[0,0,0],""max"":[1,1,1]}," +
@"{""bufferView"":1,""componentType"":5126,""count"":4,""type"":""VEC3"",""min"":[0,0,0],""max"":[1,1,0]}," +
@"{""bufferView"":2,""componentType"":5123,""count"":6,""type"":""SCALAR""}]," +
$@"""bufferViews"":[{{""buffer"":0,""byteLength"":144}},{{""buffer"":0,""byteOffset"":144,""byteLength"":48}},{{""buffer"":0,""byteOffset"":192,""byteLength"":12}}],""buffers"":[{{""byteLength"":{MeshBytes().Length}}}]";
// The GLB carries a glTF scene naming every node: glTFast instantiates a scene, and a scene-less file has none.
// Nodes in meshNodes (default MeshNodes) get the shared tetrahedron mesh, whose vertices live in the BIN chunk (MeshBytes).
public static string GlbJson(string[] nodes, string[] meshNodes = null)
{
meshNodes ??= MeshNodes;
var mesh = nodes.Any(meshNodes.Contains) ? "," + Geometry : "";
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) ? node.StartsWith("thruster") ? @",""mesh"":1" : @",""mesh"":0" : "")},""extras"":{{""aetheria.id"":""{node}""}}}}")) + "]}";
}
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);
bin ??= Array.Empty<byte>();
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((uint)binPadded.Length).CopyTo(bytes, 20 + padded.Length);
BitConverter.GetBytes(0x004E4942u).CopyTo(bytes, 24 + padded.Length);
binPadded.CopyTo(bytes, 28 + padded.Length);
return bytes;
}
// <modsRoot>/<id>/ship.cc plus the GLB at the record's ModelAsset path.
public static string WritePackage(string modsRoot, string id, string hullName = "Skiff", string[] nodes = null,
string modelAsset = "skiff.glb", Action<ShipParts> tweak = null, string[] meshNodes = null)
{
var ship = ShipAuthoringTests.Fixture();
// A playable rig: the fixture thruster plus one energy weapon mount on the hull's other cell, with its muzzle.
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 = "weapon-mount", 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;
ship.Visual.ModelAsset = modelAsset;
var directory = Path.Combine(modsRoot, id);
Directory.CreateDirectory(directory);
using (var cache = ShipAuthoringStore.Open(Path.Combine(directory, "ship.cc"), writable: true))
{
ShipAuthoringStore.Write(cache, ship.Hull, ship.Visual);
cache.FlushAsync().Wait();
}
File.WriteAllBytes(Path.Combine(directory, modelAsset), Glb(GlbJson(nodes ?? Nodes, meshNodes), MeshBytes()));
return directory;
}
}