Render original lab and Jimmy with native Vulkan and SDL3 on .NET 10
This commit is contained in:
1 parent
3df20ac19a
commit
008c0e16da
41 files changed
+2125
-68
No files matched your search
@@ -0,0 +1,230 @@
|
||||
using System.Diagnostics;
|
||||
using System.Numerics;
|
||||
using GamePort.Assets;
|
||||
using GamePort.Audio;
|
||||
using GamePort.Platform;
|
||||
using GamePort.Rendering.Vulkan;
|
||||
|
||||
namespace GamePort;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
try { return Run(args); }
|
||||
catch (Exception error) { Console.Error.WriteLine(error); return 1; }
|
||||
}
|
||||
|
||||
private static string? Option(string[] args, string name)
|
||||
{
|
||||
var i = Array.IndexOf(args, name);
|
||||
if (i < 0) return null;
|
||||
if (i + 1 >= args.Length || args[i + 1].StartsWith("--", StringComparison.Ordinal))
|
||||
throw new ArgumentException($"{name} requires a value.");
|
||||
return args[i + 1];
|
||||
}
|
||||
|
||||
private static int Run(string[] args)
|
||||
{
|
||||
if (args.Contains("--help"))
|
||||
{
|
||||
Console.WriteLine("""
|
||||
Neutron reconstruction investigation runtime
|
||||
--assets PATH Original asset directory
|
||||
--scene PATH Extracted OMT scene JSON (default: prepared LV1B)
|
||||
--model RELPATH Original ASE model (default without scene: ASE/jimstop.ase)
|
||||
--texture RELPATH PNG override (default: png/jimycarl.png)
|
||||
--level RELPATH Original GAM (default with prepared LV1B: gam/level1b.gam)
|
||||
--original-camera Stored GAM start camera and recovered projection
|
||||
--animation PATH Prepared Jimmy animation.json; place at GAM startpoint
|
||||
--with-player Use prepared jimstop animation in scene
|
||||
--time SECONDS Fixed animation time for comparisons
|
||||
--headless Render offscreen through Vulkan
|
||||
--screenshot PATH Save actual GPU readback as PPM
|
||||
--frames N Exit after N frames (headless default: 1)
|
||||
--audio PATH Play recovered WAV; --loop-audio repeats it
|
||||
--audio-smoke PATH Verify audio-stream consumption
|
||||
--verify-assets Decode all original PNG/ASE files
|
||||
--smoke-window Test keyboard event/resize (requires --frames 6 or more)
|
||||
--no-validation Disable development validation
|
||||
Viewer: C toggles stored/orbit camera, arrows/WASD orbit, +/- zoom,
|
||||
F11 fullscreen, Escape quit.
|
||||
Gameplay, original animation speed/state and cutscenes remain under reconstruction.
|
||||
""");
|
||||
return 0;
|
||||
}
|
||||
var root = AssetPath.FindOriginal(Option(args, "--assets"));
|
||||
if (args.Contains("--verify-assets")) return VerifyAssets(root);
|
||||
if (Option(args, "--audio-smoke") is { } audioSmoke)
|
||||
{
|
||||
using var sound = new SdlAudio(audioSmoke);
|
||||
var before = sound.QueuedBytes;
|
||||
Thread.Sleep(500);
|
||||
var after = sound.QueuedBytes;
|
||||
Console.WriteLine($"SDL3 audio stream consumed {before - after} source bytes in 500 ms; queued {after}.");
|
||||
return after < before ? 0 : 2;
|
||||
}
|
||||
|
||||
var scene = Option(args, "--scene");
|
||||
var defaultScene = false;
|
||||
if (scene == null && Option(args, "--model") == null)
|
||||
{
|
||||
scene = new[] { "artifacts/extracted/level1b/scene.json",
|
||||
Path.Combine(AppContext.BaseDirectory, "scenes/level1b/scene.json") }.FirstOrDefault(File.Exists);
|
||||
defaultScene = scene != null;
|
||||
}
|
||||
var levelPath = Option(args, "--level") ?? (defaultScene ? "gam/level1b.gam" : null);
|
||||
GamLevel? level = levelPath == null ? null : GamLevel.Load(AssetPath.Resolve(root, levelPath));
|
||||
var originalStart = level?.FindStart();
|
||||
if (level != null) Console.WriteLine($"Decoded GAM {level.Code}: {level.Entities.Count} entities.");
|
||||
var originalCamera = args.Contains("--original-camera") || defaultScene;
|
||||
if (originalCamera && originalStart == null)
|
||||
throw new ArgumentException("Original camera requires a GAM with a matching startpoint.");
|
||||
|
||||
var mesh = scene != null ? Mesh.LoadSceneJson(scene)
|
||||
: AseMesh.Load(AssetPath.Resolve(root, Option(args, "--model") ?? "ASE/jimstop.ase"));
|
||||
var texture = scene != null ? PngImage.White
|
||||
: PngImage.Load(AssetPath.Resolve(root, Option(args, "--texture") ?? "png/jimycarl.png"));
|
||||
var bounds = mesh.Bounds();
|
||||
var target = (bounds.Min + bounds.Max) / 2;
|
||||
var radius = Math.Max(Vector3.Distance(bounds.Min, bounds.Max) * 1.15f, 1);
|
||||
var animationPath = Option(args, "--animation");
|
||||
if (args.Contains("--with-player") && scene != null)
|
||||
animationPath ??= Path.Combine(Path.GetDirectoryName(scene)!, "../animations/jimstop/animation.json");
|
||||
MorphAnimation? animation = null;
|
||||
var characterOffset = mesh.Vertices.Length;
|
||||
var characterModel = Matrix4x4.Identity;
|
||||
if (animationPath != null)
|
||||
{
|
||||
if (scene == null || originalStart == null)
|
||||
throw new ArgumentException("A scene and matching GAM are required to place an animated player.");
|
||||
animation = new MorphAnimation(animationPath);
|
||||
mesh = animation.AppendTo(mesh);
|
||||
characterModel = Matrix4x4.CreateRotationY(-originalStart.Rotation.Y * MathF.PI / 180)
|
||||
* Matrix4x4.CreateTranslation(originalStart.Position);
|
||||
Console.WriteLine("Original morph data loaded; playback looping/speed are preview policies.");
|
||||
}
|
||||
Console.WriteLine($"Loaded original geometry: {mesh.Vertices.Length} vertices, {mesh.Indices.Length / 3} triangles, {mesh.Textures.Count + (scene == null ? 1 : 0)} textures.");
|
||||
var fixedTimeText = Option(args, "--time");
|
||||
double? fixedTime = fixedTimeText == null ? null : double.Parse(fixedTimeText, System.Globalization.CultureInfo.InvariantCulture);
|
||||
var headless = args.Contains("--headless");
|
||||
var frames = int.Parse(Option(args, "--frames") ?? (headless ? "1" : "0"));
|
||||
var smoke = args.Contains("--smoke-window");
|
||||
if (frames < 0 || (smoke && (headless || frames < 6)))
|
||||
throw new ArgumentException("Invalid frame count; window smoke requires at least six frames.");
|
||||
nint window = 0;
|
||||
if (!headless)
|
||||
{
|
||||
if (!Sdl.SDL_Init(Sdl.InitVideo)) throw new InvalidOperationException(Sdl.Error);
|
||||
window = Sdl.SDL_CreateWindow("Jimmy Neutron reconstruction — scene investigation", 960, 720,
|
||||
Sdl.WindowVulkan | Sdl.WindowResizable | Sdl.WindowHighPixelDensity);
|
||||
if (window == 0) { Sdl.SDL_Quit(); throw new InvalidOperationException(Sdl.Error); }
|
||||
}
|
||||
try
|
||||
{
|
||||
using var renderer = new VulkanRenderer(mesh, texture, 960, 720, window, !args.Contains("--no-validation"));
|
||||
using var audio = Option(args, "--audio") is { } audioFile
|
||||
? new SdlAudio(audioFile, args.Contains("--loop-audio")) : null;
|
||||
var keys = new HashSet<uint>();
|
||||
var running = true;
|
||||
var fullscreen = false;
|
||||
var sawKey = false;
|
||||
var didResize = false;
|
||||
var clock = Stopwatch.StartNew();
|
||||
double last = clock.Elapsed.TotalSeconds;
|
||||
float angle = 0.6f, elevation = 0.32f;
|
||||
var count = 0;
|
||||
while (running)
|
||||
{
|
||||
while (!headless && Sdl.SDL_PollEvent(out var e))
|
||||
{
|
||||
if (e.Type is Sdl.Quit or 0x210) running = false;
|
||||
if (e.Type is Sdl.WindowResized or Sdl.WindowPixelSizeChanged) renderer.Resize();
|
||||
if (e.Type == Sdl.WindowFocusLost) keys.Clear();
|
||||
if (e.Type == Sdl.KeyDown)
|
||||
{
|
||||
keys.Add(e.Scancode);
|
||||
if (smoke && e.Scancode == 79) sawKey = true;
|
||||
if (e.Scancode == 41) running = false;
|
||||
if (e.Scancode == 6 && e.Repeat == 0 && originalStart != null)
|
||||
originalCamera = !originalCamera;
|
||||
if (e.Scancode == 68 && e.Repeat == 0)
|
||||
{
|
||||
fullscreen = !fullscreen;
|
||||
if (!Sdl.SDL_SetWindowFullscreen(window, fullscreen)) throw new InvalidOperationException(Sdl.Error);
|
||||
}
|
||||
}
|
||||
if (e.Type == Sdl.KeyUp) keys.Remove(e.Scancode);
|
||||
}
|
||||
var now = clock.Elapsed.TotalSeconds;
|
||||
var dt = (float)Math.Min(now - last, 0.1);
|
||||
last = now;
|
||||
if (keys.Contains(80) || keys.Contains(4)) angle -= dt;
|
||||
if (keys.Contains(79) || keys.Contains(7)) angle += dt;
|
||||
if (keys.Contains(82) || keys.Contains(26)) elevation = Math.Min(elevation + dt, 1.4f);
|
||||
if (keys.Contains(81) || keys.Contains(22)) elevation = Math.Max(elevation - dt, -1.4f);
|
||||
if (keys.Contains(46) || keys.Contains(87)) radius *= MathF.Exp(-dt);
|
||||
if (keys.Contains(45) || keys.Contains(86)) radius *= MathF.Exp(dt);
|
||||
radius = Math.Max(radius, 0.01f);
|
||||
var eye = target + radius * new Vector3(MathF.Cos(elevation) * MathF.Sin(angle),
|
||||
MathF.Sin(elevation), MathF.Cos(elevation) * MathF.Cos(angle));
|
||||
var view = originalCamera
|
||||
? Matrix4x4.CreateLookAtLeftHanded(originalStart!.CameraPosition, originalStart.Position + new Vector3(0, 80, 0), Vector3.UnitY)
|
||||
: Matrix4x4.CreateLookAt(eye, target, Vector3.UnitY);
|
||||
var far = level?.Code == "LV1B" ? 10000 : 3200;
|
||||
var projection = originalCamera
|
||||
? Matrix4x4.CreatePerspectiveFieldOfViewLeftHanded(0xAAA / 16384f * MathF.Tau, (float)renderer.Width / renderer.Height, 20, far)
|
||||
: Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 3, (float)renderer.Width / renderer.Height, Math.Max(radius / 10000, 0.01f), radius * 20);
|
||||
projection.M22 *= -1;
|
||||
if (animation != null)
|
||||
{
|
||||
animation.Sample(fixedTime ?? (headless ? count / 60.0 : now), mesh.Vertices.AsSpan(characterOffset), characterModel);
|
||||
renderer.UpdateVertices(mesh.Vertices);
|
||||
}
|
||||
renderer.Render(view * projection);
|
||||
count++;
|
||||
audio?.Pump();
|
||||
if (smoke && count == 1)
|
||||
{
|
||||
var key = new Sdl.Event { Type = Sdl.KeyDown, Scancode = 79, Down = 1 };
|
||||
if (!Sdl.SDL_PushEvent(ref key) || !Sdl.SDL_SetWindowSize(window, 800, 600))
|
||||
throw new InvalidOperationException(Sdl.Error);
|
||||
}
|
||||
if (smoke && renderer.Width == 800 && renderer.Height == 600) didResize = true;
|
||||
if (frames > 0 && count >= frames) break;
|
||||
}
|
||||
if (Option(args, "--screenshot") is { } screenshot)
|
||||
{
|
||||
renderer.SavePpm(screenshot);
|
||||
Console.WriteLine($"Saved Vulkan GPU readback: {screenshot}");
|
||||
}
|
||||
var pixels = renderer.ReadPixels();
|
||||
var changed = 0;
|
||||
for (var i = 0; i < pixels.Length; i += 4)
|
||||
if (Math.Abs(pixels[i] - 31) + Math.Abs(pixels[i + 1] - 51) + Math.Abs(pixels[i + 2] - 76) > 6) changed++;
|
||||
Console.WriteLine($"Rendered {count} frame(s), {changed} non-background pixels, {renderer.ValidationErrors} Vulkan validation error(s).");
|
||||
if (smoke)
|
||||
{
|
||||
Console.WriteLine($"SDL3 injected keyboard event received: {sawKey}; swapchain resized to 800x600: {didResize}.");
|
||||
if (!sawKey || !didResize) return 2;
|
||||
}
|
||||
return renderer.ValidationErrors > 0 || changed == 0 ? 2 : 0;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (window != 0) { Sdl.SDL_DestroyWindow(window); Sdl.SDL_Quit(); }
|
||||
}
|
||||
}
|
||||
|
||||
private static int VerifyAssets(string root)
|
||||
{
|
||||
var pngs = Directory.GetFiles(root, "*", SearchOption.AllDirectories)
|
||||
.Where(p => p.EndsWith(".png", StringComparison.OrdinalIgnoreCase)).ToArray();
|
||||
foreach (var path in pngs) PngImage.Load(path);
|
||||
var models = Directory.GetFiles(AssetPath.Resolve(root, "ASE"), "*", SearchOption.AllDirectories)
|
||||
.Where(p => p.EndsWith(".ase", StringComparison.OrdinalIgnoreCase)).ToArray();
|
||||
foreach (var path in models) AseMesh.Load(path);
|
||||
Console.WriteLine($"Verified runtime decoders: {pngs.Length} PNG textures and {models.Length} ASE base meshes.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user