From 59cbdd1f3d825b6c73adb1a92b9b91936db74e25 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 17 Oct 2026 18:47:35 +0200 Subject: [PATCH 1/3] SchematicUnderlay.Fit: the line art stretched onto the hull Shape's Width x Height Engine-free rule in ServerShared with five behaviour tests. Builds (ServerShared and tests only; Editor wiring follows in the next commit). Co-Authored-By: Claude Opus 5.5 --- .../Scripts/ServerShared/SchematicUnderlay.cs | 36 +++++++ .../ServerShared/SchematicUnderlay.cs.meta | 11 +++ .../SchematicUnderlayTests.cs | 98 +++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 Assets/Scripts/ServerShared/SchematicUnderlay.cs create mode 100644 Assets/Scripts/ServerShared/SchematicUnderlay.cs.meta create mode 100644 tests/Aetheria.Shared.Tests/SchematicUnderlayTests.cs diff --git a/Assets/Scripts/ServerShared/SchematicUnderlay.cs b/Assets/Scripts/ServerShared/SchematicUnderlay.cs new file mode 100644 index 000000000..09f3d4118 --- /dev/null +++ b/Assets/Scripts/ServerShared/SchematicUnderlay.cs @@ -0,0 +1,36 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +using System.Collections.Generic; +using System.Linq; +using CultMath; + +// A mod hull's line-art schematic laid under its cell grids. The one rule for where the lines land: the line art's +// top-down bounding box is stretched, per axis, onto the hull Shape's cell rect [0, Width] x [0, Height] as the hull +// drawer sets it, never onto the occupied cells (underlay-fits-hull-bounds). Display only: nothing is derived from the +// model into the schematic or back (schematic-and-model-are-separate-spaces). +public static class SchematicUnderlay +{ + // Points are Blender Z-up XYZ in the Ship Root frame, nose toward -Y and starboard toward -X, so the schematic's + // x (starboard right) is -X and its y (forward up) is -Y. Output is in hull cell units, index-aligned with `lines`. + public static float2[][] Fit(IReadOnlyList lines, Shape hull) + { + if (lines == null || lines.Count == 0 || hull == null) return new float2[0][]; + var top = lines.Select(line => Enumerable.Range(0, (line?.Points?.Length ?? 0) / 3) + .Select(i => new float2(-line.Points[i * 3], -line.Points[i * 3 + 1])).ToArray()).ToArray(); + var all = top.SelectMany(line => line).ToArray(); + if (all.Length == 0) return top; + + var minX = all.Min(p => p.x); + var maxX = all.Max(p => p.x); + var minY = all.Min(p => p.y); + var maxY = all.Max(p => p.y); + return top.Select(line => line.Select(p => new float2( + Stretch(p.x, minX, maxX, hull.Width), Stretch(p.y, minY, maxY, hull.Height))).ToArray()).ToArray(); + } + + // min lands on 0 and max on `size`; an axis with no extent sits at the midpoint. + private static float Stretch(float value, float min, float max, int size) => + max > min ? (value - min) / (max - min) * size : size / 2f; +} diff --git a/Assets/Scripts/ServerShared/SchematicUnderlay.cs.meta b/Assets/Scripts/ServerShared/SchematicUnderlay.cs.meta new file mode 100644 index 000000000..d6120a641 --- /dev/null +++ b/Assets/Scripts/ServerShared/SchematicUnderlay.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 77b6dfe49272456a9f976d2578732d8a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/tests/Aetheria.Shared.Tests/SchematicUnderlayTests.cs b/tests/Aetheria.Shared.Tests/SchematicUnderlayTests.cs new file mode 100644 index 000000000..6001b6b51 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/SchematicUnderlayTests.cs @@ -0,0 +1,98 @@ +using System.Collections.Generic; +using System.Linq; +using CultMath; +using Xunit; + +// SchematicUnderlay: the one rule that registers a mod hull's line art onto the hull Shape's cell rect. +public sealed class SchematicUnderlayTests +{ + private static ShipPolyline Line(params float[] xyz) => new ShipPolyline { Points = xyz }; + + // Lines spanning Blender X [-3,5], Y [-10,2] (nose at Y -10, starboard at X -3). + private static List Lines() => new List + { + Line(-3, -10, 0, 5, 2, 0), + Line(1, -4, 1, 0, 0, 1), + }; + + private static Shape SixByFour() + { + var shape = new Shape(6, 4); + shape[new int2(1, 1)] = true; + shape[new int2(2, 1)] = true; + return shape; + } + + private static (float2 min, float2 max) Bounds(float2[][] fit) + { + var all = fit.SelectMany(l => l).ToArray(); + return (new float2(all.Min(p => p.x), all.Min(p => p.y)), new float2(all.Max(p => p.x), all.Max(p => p.y))); + } + + [Fact] + public void UnderlaySpansHullBoundsNotOccupiedCells() + { + var (min, max) = Bounds(SchematicUnderlay.Fit(Lines(), SixByFour())); + + Assert.Equal(0f, min.x, 4); + Assert.Equal(0f, min.y, 4); + Assert.Equal(6f, max.x, 4); + Assert.Equal(4f, max.y, 4); + } + + [Fact] + public void UnderlayNoseUpStarboardRight() + { + var fit = SchematicUnderlay.Fit(Lines(), SixByFour()); + + // Line 0 point 0 holds both the lowest Blender Y (nose) and the lowest Blender X (starboard). + Assert.Equal(4f, fit[0][0].y, 4); + Assert.Equal(6f, fit[0][0].x, 4); + // Line 0 point 1 holds the highest of both: aft and port. + Assert.Equal(0f, fit[0][1].y, 4); + Assert.Equal(0f, fit[0][1].x, 4); + } + + [Fact] + public void UnderlayFollowsTheHullDrawersSize() + { + var hull = SixByFour(); + hull.Resize(8, 5); + + var (min, max) = Bounds(SchematicUnderlay.Fit(Lines(), hull)); + + Assert.Equal(new float2(0, 0), new float2(min.x, min.y)); + Assert.Equal(8f, max.x, 4); + Assert.Equal(5f, max.y, 4); + } + + [Fact] + public void UnderlayStretchesEachAxis() + { + var lines = new List { Line(0, 0, 0, 4, 2, 0) }; + + var (min, max) = Bounds(SchematicUnderlay.Fit(lines, new Shape(3, 3))); + + Assert.Equal(0f, min.x, 4); + Assert.Equal(0f, min.y, 4); + Assert.Equal(3f, max.x, 4); + Assert.Equal(3f, max.y, 4); + } + + [Fact] + public void UnderlayDegenerateAndEmpty() + { + var flat = new List { Line(2, 0, 0, 2, 5, 0) }; + + var fit = SchematicUnderlay.Fit(flat, new Shape(6, 4)); + + Assert.All(fit[0], p => Assert.Equal(3f, p.x, 4)); + Assert.All(fit[0], p => Assert.True(float.IsFinite(p.y))); + var pointless = SchematicUnderlay.Fit(new List { Line() }, new Shape(6, 4)); + Assert.Single(pointless); + Assert.Empty(pointless[0]); + Assert.Empty(SchematicUnderlay.Fit(null, new Shape(6, 4))); + Assert.Empty(SchematicUnderlay.Fit(new List(), new Shape(6, 4))); + Assert.Empty(SchematicUnderlay.Fit(Lines(), null)); + } +} From f69c997a85d751cb59e6963bcc8951d9081d806c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 17 Oct 2026 18:47:52 +0200 Subject: [PATCH 2/3] Studio: HullUnderlay draws the texture or the hull's line art under both cell grids The hull Shape grid and the footprint painter draw the underlay through HullUnderlay.Draw; the inline texture draw moves into it. Editor assembly: Unity compile not yet run. Co-Authored-By: Claude Opus 5.5 --- Assets/Scripts/Editor/CultCacheDrawers.cs | 102 +++++++++++++++------- 1 file changed, 72 insertions(+), 30 deletions(-) diff --git a/Assets/Scripts/Editor/CultCacheDrawers.cs b/Assets/Scripts/Editor/CultCacheDrawers.cs index 818746e0f..27d21afec 100644 --- a/Assets/Scripts/Editor/CultCacheDrawers.cs +++ b/Assets/Scripts/Editor/CultCacheDrawers.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System; using System.Linq; using System.Reflection; @@ -87,14 +88,13 @@ public sealed class InspectableTemperatureDrawer : ICultInspectorDrawer } } -// A size and a toggle per cell. When the owning item has a schematic texture, the texture lies under the grid and the -// height follows its aspect; a hull's hardpoint cells are tinted. Both read the owning record through +// A size and a toggle per cell. The underlay (the item's schematic texture, or a hull's line art stretched to the Shape's +// Width x Height) is drawn by HullUnderlay; the height follows a texture's aspect. A hull's hardpoint cells are tinted. +// Both read the owning record through // inspector.Record, which is a read-only copy: nothing here writes to it. [CultInspectorDrawer(typeof(InspectableSchematicShapeAttribute))] public sealed class InspectableSchematicShapeDrawer : ICultInspectorDrawer { - private static Material _schematicMaterial; - public object Draw(CultInspector inspector, string label, Type type, object value, MemberInfo member) { if (type != typeof(Shape)) return inspector.DrawDefault(label, type, value, member); @@ -122,11 +122,7 @@ public sealed class InspectableSchematicShapeDrawer : ICultInspectorDrawer { using (var grid = new EditorGUILayout.VerticalScope()) { - if (schematic != null && Event.current.type == EventType.Repaint) - { - _schematicMaterial ??= new Material(Shader.Find("Legacy Shaders/Particles/Additive")); - EditorGUI.DrawPreviewTexture(grid.rect, schematic, _schematicMaterial); - } + HullUnderlay.Draw(grid.rect, item, inspector.Records); for (var y = shape.Height - 1; y >= 0; y--) using (new EditorGUILayout.HorizontalScope()) @@ -145,6 +141,44 @@ public sealed class InspectableSchematicShapeDrawer : ICultInspectorDrawer } } +// The underlay under a hull's cell grids, drawn on Repaint only. A shipped hull's schematic texture is stretched over +// the grid rect; a mod hull's line art (its ShipAuthoring, found in the open cache at hull.Visual) is placed by +// SchematicUnderlay.Fit in cell units and mapped to the rect here, the one site that maps cells to a grid's screen +// rect. The hull Shape grid and the footprint painter both draw through it. No lookup found: no line underlay. +public static class HullUnderlay +{ + private static Material _schematicMaterial; + + public static void Draw(Rect grid, EquippableItemData item, IReadOnlyList records) + { + if (item == null || Event.current.type != EventType.Repaint) return; + var texture = string.IsNullOrEmpty(item.Schematic) ? null : AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(item.Schematic)); + if (texture != null) + { + _schematicMaterial ??= new Material(Shader.Find("Legacy Shaders/Particles/Additive")); + EditorGUI.DrawPreviewTexture(grid, texture, _schematicMaterial); + } + + if (!(item is HullData hull) || hull.Shape == null || hull.Shape.Width == 0 || hull.Shape.Height == 0) return; + var key = CultInspectorModel.RecordKey(hull.Visual); + var authoring = records?.FirstOrDefault(record => record.Key.Value == key && record.Document is ShipAuthoring)?.Document as ShipAuthoring; + if (authoring == null) return; + + var lines = authoring.SchematicLines; + var fit = SchematicUnderlay.Fit(lines, hull.Shape); + for (var i = 0; i < fit.Length; i++) + { + var rgba = lines[i].Color; + var color = rgba == null ? Color.white : new Color(rgba[0], rgba[1], rgba[2], rgba[3]); + var points = fit[i].Select(p => new Vector3( + grid.x + p.x / hull.Shape.Width * grid.width, + grid.yMax - p.y / hull.Shape.Height * grid.height)).ToList(); + if (lines[i].Cyclic && points.Count > 2) points.Add(points[0]); + using (new Handles.DrawingScope(color)) Handles.DrawAAPolyLine(2f, points.ToArray()); + } + } +} + // A hardpoint's schematic footprint over its hull's cells: the hull's occupied cells shaded, this hardpoint's cells lit // in its tint, other hardpoints' cells tinted and disabled. A click edits Position and Shape together through // HullFootprint.Toggle, which owns the rule so a Thing host reads the same one. The footprint is authored for gameplay, @@ -164,27 +198,35 @@ public sealed class HardpointFootprintDrawer : ICultInspectorDrawer var others = hull.Hardpoints.Where(hp => hp != own).ToArray(); EditorGUILayout.LabelField("Footprint"); - for (var y = hull.Shape.Height - 1; y >= 0; y--) - using (new EditorGUILayout.HorizontalScope()) - for (var x = 0; x < hull.Shape.Width; x++) - { - var cell = int2(x, y); - var other = others.FirstOrDefault(hp => HullFootprint.Cells(hp).Contains(cell)); - var on = HullFootprint.Cells(drawn).Contains(cell); - var previous = GUI.backgroundColor; - GUI.backgroundColor = on ? drawn.TintColor.ToColor() : other != null ? other.TintColor.ToColor() - : hull.Shape[cell] ? Color.gray : previous; - EditorGUI.BeginChangeCheck(); - using (new EditorGUI.DisabledScope(other != null && !on)) - GUILayout.Toggle(on || other != null, GUIContent.none, GUILayout.Width(16)); - if (EditorGUI.EndChangeCheck() && HullFootprint.Toggle(drawn.Shape, drawn.Position, cell, out var shape, out var position)) - { - drawn.Shape = shape; - drawn.Position = position; - GUI.changed = true; - } - GUI.backgroundColor = previous; - } + using (new EditorGUILayout.HorizontalScope()) + { + using (var grid = new EditorGUILayout.VerticalScope()) + { + HullUnderlay.Draw(grid.rect, hull, inspector.Records); + for (var y = hull.Shape.Height - 1; y >= 0; y--) + using (new EditorGUILayout.HorizontalScope()) + for (var x = 0; x < hull.Shape.Width; x++) + { + var cell = int2(x, y); + var other = others.FirstOrDefault(hp => HullFootprint.Cells(hp).Contains(cell)); + var on = HullFootprint.Cells(drawn).Contains(cell); + var previous = GUI.backgroundColor; + GUI.backgroundColor = on ? drawn.TintColor.ToColor() : other != null ? other.TintColor.ToColor() + : hull.Shape[cell] ? Color.gray : previous; + EditorGUI.BeginChangeCheck(); + using (new EditorGUI.DisabledScope(other != null && !on)) + GUILayout.Toggle(on || other != null, GUIContent.none, GUILayout.Width(16)); + if (EditorGUI.EndChangeCheck() && HullFootprint.Toggle(drawn.Shape, drawn.Position, cell, out var shape, out var position)) + { + drawn.Shape = shape; + drawn.Position = position; + GUI.changed = true; + } + GUI.backgroundColor = previous; + } + } + GUILayout.FlexibleSpace(); + } return drawn; } } From 1030e7198a966900bd2241eeba55b51f6733e12c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 17 Oct 2026 19:04:44 +0200 Subject: [PATCH 3/3] SchematicUnderlay tests: the bounds extremes sit in different lines A mutant fitting to the first line only survived because every other point lay inside it. Builds. Co-Authored-By: Claude Opus 5.5 --- .../Aetheria.Shared.Tests/SchematicUnderlayTests.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/SchematicUnderlayTests.cs b/tests/Aetheria.Shared.Tests/SchematicUnderlayTests.cs index 6001b6b51..7617dc2ef 100644 --- a/tests/Aetheria.Shared.Tests/SchematicUnderlayTests.cs +++ b/tests/Aetheria.Shared.Tests/SchematicUnderlayTests.cs @@ -8,11 +8,12 @@ public sealed class SchematicUnderlayTests { private static ShipPolyline Line(params float[] xyz) => new ShipPolyline { Points = xyz }; - // Lines spanning Blender X [-3,5], Y [-10,2] (nose at Y -10, starboard at X -3). + // Lines spanning Blender X [-3,5], Y [-10,2] (nose at Y -10, starboard at X -3); + // the far extremes sit in the second line. private static List Lines() => new List { - Line(-3, -10, 0, 5, 2, 0), - Line(1, -4, 1, 0, 0, 1), + Line(-3, -10, 0, 1, -4, 0), + Line(5, 2, 1, 0, 0, 1), }; private static Shape SixByFour() @@ -48,9 +49,9 @@ public sealed class SchematicUnderlayTests // Line 0 point 0 holds both the lowest Blender Y (nose) and the lowest Blender X (starboard). Assert.Equal(4f, fit[0][0].y, 4); Assert.Equal(6f, fit[0][0].x, 4); - // Line 0 point 1 holds the highest of both: aft and port. - Assert.Equal(0f, fit[0][1].y, 4); - Assert.Equal(0f, fit[0][1].x, 4); + // Line 1 point 0 holds the highest of both, in another line: aft and port. + Assert.Equal(0f, fit[1][0].y, 4); + Assert.Equal(0f, fit[1][0].x, 4); } [Fact]