Merge eureka/aetheria-release-studio-underlay-fits-hull (head 1030e719)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-18 01:25:58 +02:00
commit f30f15cd41
4 changed files with 218 additions and 30 deletions

View file

@ -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<CultStoredDocument> records)
{
if (item == null || Event.current.type != EventType.Repaint) return;
var texture = string.IsNullOrEmpty(item.Schematic) ? null : AssetDatabase.LoadAssetAtPath<Texture2D>(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;
}
}

View file

@ -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<ShipPolyline> 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;
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 77b6dfe49272456a9f976d2578732d8a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,99 @@
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);
// the far extremes sit in the second line.
private static List<ShipPolyline> Lines() => new List<ShipPolyline>
{
Line(-3, -10, 0, 1, -4, 0),
Line(5, 2, 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 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]
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<ShipPolyline> { 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<ShipPolyline> { 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<ShipPolyline> { 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<ShipPolyline>(), new Shape(6, 4)));
Assert.Empty(SchematicUnderlay.Fit(Lines(), null));
}
}