Verify inventory_grid and panel_chrome shaders, hit-testing, and GPU output in Smoke

This commit is contained in:
Meta Vens 2026-12-04 17:16:39 +01:00
commit 05fcfecf91

View file

@ -199,6 +199,232 @@ public partial class Smoke : Node
}
_main.SetPhysicsProcess(true);
await VerifyHudAndShadersAsync(isHeadless);
await MaybeCapturePreviewAsync(isHeadless);
}
private async Task VerifyHudAndShadersAsync(bool isHeadless)
{
var invShader = _main.Hud.Schematic.ShaderMaterial.Shader;
Check(invShader is not null && !string.IsNullOrWhiteSpace(invShader.Code),
"inventory_grid.gdshader failed to load");
int chromeCount = 0;
foreach (var mat in _main.Hud.GetChromeMaterials())
{
chromeCount++;
Check(mat.Shader is not null && !string.IsNullOrWhiteSpace(mat.Shader.Code),
$"panel_chrome.gdshader failed to load on HUD panel #{chromeCount}");
}
Check(chromeCount == 5, $"Expected 5 ChromePanel materials on HUD, got {chromeCount}");
// Pack a real Tank chassis with mounted modules, thermal gradient, stripped cell, and active hit-pulse
var testGrid = new ChassisGrid(Catalog.Tank);
Check(testGrid.TryMount(Catalog.Recoilless, "wpn_turret"), "Expected Recoilless to mount on wpn_turret");
Check(testGrid.TryMount(Catalog.Mg762, "wpn_coax"), "Expected Mg762 to mount on wpn_coax");
Check(testGrid.TryMount(Catalog.TurboCore, "eng_rear"), "Expected TurboCore to mount on eng_rear");
Check(testGrid.TryMount(Catalog.CopperRad, "rad_deck"), "Expected CopperRad to mount on rad_deck");
Check(testGrid.TryMount(Catalog.WeldedPlate, "arm_glacis"), "Expected WeldedPlate to mount on arm_glacis");
testGrid.InjectHardpointHeat("eng_rear", 1800f);
testGrid.InjectHardpointHeat("wpn_turret", 900f);
testGrid.StepHeat(0.25f);
testGrid.Armor[0, 0] = 0f; // stripped armor cell
var schematic = _main.Hud.Schematic;
schematic.SetGrid(testGrid);
schematic.TriggerHitPulse(1, 1, 1f);
// Hit-testing verification: center of cell (1, 1) must map to (1, 1) and hardpoint "wpn_turret"
schematic.ComputeCellLayout(out float cellSize, out Vector2 origin);
Check(cellSize > 10f, $"Expected InventoryGridControl cellSize > 10px, got {cellSize:F2}");
Vector2 probePos = origin + new Vector2(1.5f * cellSize, 1.5f * cellSize);
bool hitCell = schematic.TryGetCellAt(probePos, out var mappedCell);
Check(hitCell && mappedCell == new Vector2I(1, 1),
$"Expected TryGetCellAt to map center of (1,1) to (1,1), got hit={hitCell} cell={mappedCell}");
var mappedHp = schematic.GetHardpointAt(1, 1);
Check(mappedHp is { Id: "wpn_turret" },
$"Expected GetHardpointAt(1,1) to return wpn_turret, got {mappedHp?.Id ?? "null"}");
// Toggle thermal mode off -> on -> off
_main.Hud.SetThermalMode(true, instant: true);
Check(_main.Hud.ThermalMode && MathF.Abs(schematic.ThermalBlend - 1f) < 0.01f,
"Expected ThermalMode to activate immediately");
_main.Hud.SetThermalMode(false, instant: true);
Check(!_main.Hud.ThermalMode && MathF.Abs(schematic.ThermalBlend) < 0.01f,
"Expected ThermalMode to deactivate immediately");
// Restore live player grid to HUD and verify layout has no overlaps or clipping
_main.Hud.Sync(_main.Session);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
Vector2 vpSize = GetViewport().GetVisibleRect().Size;
Check(!_main.Hud.HasPanelOverlapOrClip(vpSize), $"Compact HUD panels overlapped or clipped at {vpSize}");
_main.Hud.ToggleGarage();
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
Check(!_main.Hud.HasPanelOverlapOrClip(vpSize), $"Garage HUD panels overlapped or clipped at {vpSize}");
_main.Hud.ToggleGarage();
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
if (isHeadless)
{
return;
}
// GPU verification on OpenGL 3.3: render isolated InventoryGridControl (tactical + thermal) & panel_chrome.gdshader
var gridVp = new SubViewport
{
Size = new Vector2I(240, 240),
TransparentBg = true,
RenderTargetUpdateMode = SubViewport.UpdateMode.Always
};
var vpGrid = new InventoryGridControl
{
Size = new Vector2(240f, 240f),
CustomMinimumSize = new Vector2(240f, 240f)
};
gridVp.AddChild(vpGrid);
AddChild(gridVp);
vpGrid.SetGrid(testGrid);
vpGrid.TriggerHitPulse(1, 1, 1f);
vpGrid.SetThermalInstant(false);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
var tacticalImg = gridVp.GetTexture().GetImage();
int tacticalOpaque = CountNonZeroAlphaPixels(tacticalImg, 0.08f);
GD.Print($"SMOKE INFO: InventoryGrid GPU tactical opaque pixels={tacticalOpaque}");
Check(tacticalOpaque > 4000, $"Expected > 4000 rendered pixels from InventoryGrid tactical shader, got {tacticalOpaque}");
vpGrid.SetThermalInstant(true);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
var thermalImg = gridVp.GetTexture().GetImage();
int thermalOpaque = CountNonZeroAlphaPixels(thermalImg, 0.08f);
int modeDiff = CountDifferentPixels(tacticalImg, thermalImg, 0.05f);
GD.Print($"SMOKE INFO: InventoryGrid GPU thermal opaque={thermalOpaque}, diff vs tactical={modeDiff}");
Check(thermalOpaque > 4000, $"Expected > 4000 rendered pixels from InventoryGrid thermal shader, got {thermalOpaque}");
Check(modeDiff > 500, $"Expected thermal conduit toggle to visibly change > 500 pixels, got {modeDiff}");
gridVp.QueueFree();
// GPU verification for panel_chrome.gdshader
var chromeVp = new SubViewport
{
Size = new Vector2I(240, 120),
TransparentBg = true,
RenderTargetUpdateMode = SubViewport.UpdateMode.Always
};
var chromeShader = GD.Load<Shader>("res://panel_chrome.gdshader");
var chromeMat = new ShaderMaterial { Shader = chromeShader };
chromeMat.SetShaderParameter("rect_size", new Vector2(240f, 120f));
chromeMat.SetShaderParameter("accent_color", Color.FromHtml("#e08a4a"));
var chromeImgTex = Image.CreateEmpty(1, 1, false, Image.Format.Rgba8);
chromeImgTex.SetPixel(0, 0, Colors.White);
var chromeRect = new TextureRect
{
Texture = ImageTexture.CreateFromImage(chromeImgTex),
Size = new Vector2(240f, 120f),
ExpandMode = TextureRect.ExpandModeEnum.IgnoreSize,
StretchMode = TextureRect.StretchModeEnum.Scale,
Material = chromeMat
};
chromeVp.AddChild(chromeRect);
AddChild(chromeVp);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
var chromeImg = chromeVp.GetTexture().GetImage();
int chromeOpaque = CountNonZeroAlphaPixels(chromeImg, 0.08f);
Color cornerPx = chromeImg.GetPixel(2, 2);
Color centerPx = chromeImg.GetPixel(120, 60);
GD.Print($"SMOKE INFO: panel_chrome GPU opaque={chromeOpaque}, corner={cornerPx}, center={centerPx}");
Check(chromeOpaque > 10000, $"Expected > 10000 rendered pixels from panel_chrome.gdshader, got {chromeOpaque}");
Check(cornerPx.R > centerPx.R + 0.25f,
$"Expected L-bracket corner pixel ({cornerPx}) to be accented brighter than panel center ({centerPx})");
chromeVp.QueueFree();
}
private async Task MaybeCapturePreviewAsync(bool isHeadless)
{
if (isHeadless) return;
string? previewPath = null;
foreach (string arg in OS.GetCmdlineUserArgs())
{
const string prefix = "--capture-preview=";
if (arg.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
{
previewPath = arg[prefix.Length..];
break;
}
}
if (string.IsNullOrWhiteSpace(previewPath))
{
return;
}
if (!_main.Hud.GarageExpanded)
{
_main.Hud.ToggleGarage();
}
// Seed realistic combat telemetry so the preview clearly showcases armor stripping, hit-flash, cargo rack, and thermal heat
var grid = _main.Session.State.Player.Grid;
grid.TryAddCargo(Catalog.TouringPlasma);
grid.TryAddCargo(Catalog.GoldRadiator);
grid.InjectHardpointHeat("eng_mid", 1100f);
grid.InjectHardpointHeat("wpn_roof", 750f);
grid.StepHeat(0.15f);
grid.Armor[0, 0] = 0f;
_main.Hud.Sync(_main.Session);
_main.Hud.Schematic.TriggerHitPulse(1, 0, 0.9f);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
var frameImg = GetViewport().GetTexture().GetImage();
var err = frameImg.SavePng(previewPath);
GD.Print($"SMOKE INFO: Saved preview screenshot ({frameImg.GetWidth()}x{frameImg.GetHeight()}) to {previewPath} (err={err})");
Check(err == Error.Ok, $"Failed to save preview screenshot to {previewPath}: {err}");
}
private static int CountNonZeroAlphaPixels(Image img, float minAlpha)
{
int count = 0;
int w = img.GetWidth();
int h = img.GetHeight();
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
if (img.GetPixel(x, y).A > minAlpha) count++;
}
}
return count;
}
private static int CountDifferentPixels(Image a, Image b, float threshold)
{
int count = 0;
int w = Math.Min(a.GetWidth(), b.GetWidth());
int h = Math.Min(a.GetHeight(), b.GetHeight());
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
Color ca = a.GetPixel(x, y);
Color cb = b.GetPixel(x, y);
float diff = MathF.Abs(ca.R - cb.R) + MathF.Abs(ca.G - cb.G) + MathF.Abs(ca.B - cb.B) + MathF.Abs(ca.A - cb.A);
if (diff > threshold) count++;
}
}
return count;
}
private void Check(bool condition, string message)