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: