Correct preview ground placement and expose original animation clips
This commit is contained in:
1 parent
670adb4795
commit
37fa18094f
22 files changed
+398
-33
No files matched your search
@@ -0,0 +1,45 @@
|
||||
using GamePort.Assets;
|
||||
|
||||
namespace GamePort.Application;
|
||||
|
||||
/// <summary>Explicit animation inspection; does not simulate player actions.</summary>
|
||||
public sealed class PreviewAnimationLibrary
|
||||
{
|
||||
private readonly Dictionary<uint, (string Name, MorphAnimation Animation)> choices = [];
|
||||
public MorphAnimation Current { get; private set; }
|
||||
public string Name { get; private set; }
|
||||
public string WindowTitle => $"Jimmy Neutron — scene preview | {Name}{(Current.HasVertexMotion ? "" : " (still pose)")} | 1 idle, 2 run, 3 jump, 4 scratch | C camera";
|
||||
|
||||
public PreviewAnimationLibrary(string manifest)
|
||||
{
|
||||
Current = new MorphAnimation(manifest);
|
||||
Name = Path.GetFileName(Path.GetDirectoryName(Path.GetFullPath(manifest)))!;
|
||||
var parent = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(manifest)!, ".."));
|
||||
foreach (var (scancode, folder, label) in new[]
|
||||
{
|
||||
(30u, "jimstop", "Idle"), (31u, "jimrun", "Run cycle"),
|
||||
(32u, "jimjump", "Jump cycle"), (33u, "jimscratch", "Scratch cycle")
|
||||
})
|
||||
{
|
||||
var path = Path.Combine(parent, folder, "animation.json");
|
||||
if (!File.Exists(path)) continue;
|
||||
var clip = Path.GetFullPath(path) == Path.GetFullPath(manifest) ? Current : new MorphAnimation(path);
|
||||
if (clip.Base.Vertices.Length != Current.Base.Vertices.Length
|
||||
|| !clip.Base.Indices.SequenceEqual(Current.Base.Indices)
|
||||
|| !clip.Base.Vertices.Select(v => v.Uv).SequenceEqual(Current.Base.Vertices.Select(v => v.Uv))
|
||||
|| clip.Texture.Width != Current.Texture.Width || clip.Texture.Height != Current.Texture.Height
|
||||
|| !clip.Texture.Rgba.SequenceEqual(Current.Texture.Rgba))
|
||||
throw new InvalidDataException($"Animation family mismatch: {path}");
|
||||
choices[scancode] = (label, clip);
|
||||
if (ReferenceEquals(clip, Current)) Name = label;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Select(uint scancode)
|
||||
{
|
||||
if (!choices.TryGetValue(scancode, out var clip)) return false;
|
||||
Current = clip.Animation;
|
||||
Name = clip.Name;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System.Numerics;
|
||||
using GamePort.Assets;
|
||||
|
||||
namespace GamePort.Application;
|
||||
|
||||
/// <summary>
|
||||
/// Prevents the inspection character penetrating visible floor geometry.
|
||||
/// This is preview placement, not the original game's unrecovered collision solver.
|
||||
/// A single correction from the standing bind pose preserves animation displacement.
|
||||
/// </summary>
|
||||
public sealed record PreviewCharacterPlacement(Vector3 SourcePosition, Vector3 Position,
|
||||
Matrix4x4 Model, float? FloorAtOrigin)
|
||||
{
|
||||
public float VerticalCorrection => Position.Y - SourcePosition.Y;
|
||||
|
||||
public static PreviewCharacterPlacement Create(Mesh scene, Mesh standingPose,
|
||||
Vector3 position, float yawDegrees)
|
||||
{
|
||||
var rotation = Matrix4x4.CreateRotationY(-yawDegrees * MathF.PI / 180);
|
||||
var surfaces = FloorSurfaces(scene, position.Y);
|
||||
var floor = HeightAt(surfaces, position.X, position.Z, position.Y);
|
||||
var correction = 0f;
|
||||
// Use the standing base, never a moving frame: grounding each frame would
|
||||
// cancel the original jump animation and introduce foot-height jitter.
|
||||
foreach (var local in standingPose.Vertices.Select(v => v.Position).Distinct())
|
||||
{
|
||||
var world = Vector3.Transform(local, rotation) + position;
|
||||
if (HeightAt(surfaces, world.X, world.Z, position.Y) is { } height)
|
||||
correction = MathF.Max(correction, height - world.Y);
|
||||
}
|
||||
var corrected = position + new Vector3(0, correction, 0);
|
||||
return new PreviewCharacterPlacement(position, corrected,
|
||||
rotation * Matrix4x4.CreateTranslation(corrected), floor);
|
||||
}
|
||||
|
||||
private readonly record struct Triangle(Vector3 A, Vector3 B, Vector3 C, float Denominator);
|
||||
|
||||
private static List<Triangle> FloorSurfaces(Mesh scene, float maximumHeight)
|
||||
{
|
||||
var surfaces = new List<Triangle>();
|
||||
var draws = scene.HasExplicitDraws ? scene.Draws
|
||||
: [new MeshDraw(0, (uint)scene.Indices.Length, -1)];
|
||||
foreach (var draw in draws)
|
||||
for (var i = draw.FirstIndex; i + 2 < draw.FirstIndex + draw.IndexCount; i += 3)
|
||||
{
|
||||
var a = scene.Vertices[scene.Indices[i]].Position;
|
||||
var b = scene.Vertices[scene.Indices[i + 1]].Position;
|
||||
var c = scene.Vertices[scene.Indices[i + 2]].Position;
|
||||
// Exclude ceiling geometry and steep/vertical surfaces. Retained
|
||||
// invisible proxy geometry is not treated as proven collision data.
|
||||
if (MathF.Min(a.Y, MathF.Min(b.Y, c.Y)) > maximumHeight) continue;
|
||||
var normal = Vector3.Cross(b - a, c - a);
|
||||
if (normal.LengthSquared() < 0.000001f || MathF.Abs(normal.Y) < normal.Length() * 0.5f) continue;
|
||||
var denominator = (b.Z - c.Z) * (a.X - c.X) + (c.X - b.X) * (a.Z - c.Z);
|
||||
if (MathF.Abs(denominator) > 0.000001f)
|
||||
surfaces.Add(new Triangle(a, b, c, denominator));
|
||||
}
|
||||
return surfaces;
|
||||
}
|
||||
|
||||
private static float? HeightAt(List<Triangle> surfaces, float x, float z, float maximumHeight)
|
||||
{
|
||||
float? highest = null;
|
||||
foreach (var t in surfaces)
|
||||
{
|
||||
var u = ((t.B.Z - t.C.Z) * (x - t.C.X) + (t.C.X - t.B.X) * (z - t.C.Z)) / t.Denominator;
|
||||
var v = ((t.C.Z - t.A.Z) * (x - t.C.X) + (t.A.X - t.C.X) * (z - t.C.Z)) / t.Denominator;
|
||||
if (u < -0.00001f || v < -0.00001f || u + v > 1.00001f) continue;
|
||||
var y = u * t.A.Y + v * t.B.Y + (1 - u - v) * t.C.Y;
|
||||
if (y > maximumHeight) continue;
|
||||
if (highest == null || y > highest.Value) highest = y;
|
||||
}
|
||||
return highest;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ public sealed class MorphAnimation
|
||||
public Mesh Base { get; }
|
||||
public PngImage Texture { get; }
|
||||
public double SampleInterval { get; }
|
||||
public bool HasVertexMotion { get; }
|
||||
private readonly Mesh[] frames;
|
||||
|
||||
public MorphAnimation(string manifestPath)
|
||||
@@ -31,6 +32,7 @@ public sealed class MorphAnimation
|
||||
for (var i = 0; i < Base.Vertices.Length; i++)
|
||||
if (frames.Any(f => f.Vertices[i].Uv != Base.Vertices[i].Uv))
|
||||
throw new InvalidDataException("Morph sample UV differs from base");
|
||||
HasVertexMotion = frames.Any(f => f.Vertices.Where((v, i) => v.Position != Base.Vertices[i].Position).Any());
|
||||
SampleInterval = frames.Length > 1
|
||||
? (samples[1].GetProperty("time").GetDouble() - samples[0].GetProperty("time").GetDouble()) / ticks / fps
|
||||
: 1 / fps;
|
||||
|
||||
@@ -36,6 +36,7 @@ internal static unsafe class Sdl
|
||||
public static string Error=>Marshal.PtrToStringUTF8(SDL_GetError())??"SDL error";
|
||||
[DllImport(Library,CallingConvention=CallingConvention.Cdecl)] public static extern nint SDL_CreateWindow([MarshalAs(UnmanagedType.LPUTF8Str)]string title,int w,int h,ulong flags);
|
||||
[DllImport(Library,CallingConvention=CallingConvention.Cdecl)]public static extern void SDL_DestroyWindow(nint window);
|
||||
[DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_SetWindowTitle(nint window,[MarshalAs(UnmanagedType.LPUTF8Str)]string title);
|
||||
[DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_PollEvent(out Event e);
|
||||
[DllImport(Library,CallingConvention=CallingConvention.Cdecl)]public static extern byte** SDL_Vulkan_GetInstanceExtensions(out uint count);
|
||||
[DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_Vulkan_CreateSurface(nint window,nint instance,void* allocator,out ulong surface);
|
||||
|
||||
+31
-10
@@ -1,6 +1,7 @@
|
||||
using System.Diagnostics;
|
||||
using System.Numerics;
|
||||
using GamePort.Assets;
|
||||
using GamePort.Application;
|
||||
using GamePort.Audio;
|
||||
using GamePort.Platform;
|
||||
using GamePort.Rendering.Vulkan;
|
||||
@@ -48,7 +49,7 @@ internal static class Program
|
||||
--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.
|
||||
F11 fullscreen, Escape quit. Animation preview: 1 idle, 2 run, 3 jump, 4 scratch.
|
||||
Gameplay, original animation speed/state and cutscenes remain under reconstruction.
|
||||
""");
|
||||
return 0;
|
||||
@@ -91,18 +92,20 @@ internal static class Program
|
||||
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;
|
||||
PreviewAnimationLibrary? animations = 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.");
|
||||
animations = new PreviewAnimationLibrary(animationPath);
|
||||
var standingPose = AseMesh.Load(AssetPath.Resolve(root, "ASE/jimstop.ase"));
|
||||
var placement = PreviewCharacterPlacement.Create(mesh, standingPose, originalStart.Position, originalStart.Rotation.Y);
|
||||
characterModel = placement.Model;
|
||||
mesh = animations.Current.AppendTo(mesh);
|
||||
Console.WriteLine($"Preview floor correction: +{placement.VerticalCorrection:F3} Y; original GAM position remains unchanged.");
|
||||
Console.WriteLine("Scene preview: 1 idle (still pose), 2 run, 3 jump, 4 scratch. Playback loop/speed are preview policies; gameplay is incomplete.");
|
||||
}
|
||||
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");
|
||||
@@ -116,7 +119,7 @@ internal static class Program
|
||||
if (!headless)
|
||||
{
|
||||
if (!Sdl.SDL_Init(Sdl.InitVideo)) throw new InvalidOperationException(Sdl.Error);
|
||||
window = Sdl.SDL_CreateWindow("Jimmy Neutron reconstruction — scene investigation", 960, 720,
|
||||
window = Sdl.SDL_CreateWindow(animations?.WindowTitle ?? "Jimmy Neutron — scene preview | C camera", 960, 720,
|
||||
Sdl.WindowVulkan | Sdl.WindowResizable | Sdl.WindowHighPixelDensity);
|
||||
if (window == 0) { Sdl.SDL_Quit(); throw new InvalidOperationException(Sdl.Error); }
|
||||
}
|
||||
@@ -131,6 +134,8 @@ internal static class Program
|
||||
var sawKey = false;
|
||||
var didResize = false;
|
||||
var clock = Stopwatch.StartNew();
|
||||
var animationStarted = 0.0;
|
||||
var animationSwitched = false;
|
||||
double last = clock.Elapsed.TotalSeconds;
|
||||
float angle = 0.6f, elevation = 0.32f;
|
||||
var count = 0;
|
||||
@@ -146,6 +151,12 @@ internal static class Program
|
||||
keys.Add(e.Scancode);
|
||||
if (smoke && e.Scancode == 79) sawKey = true;
|
||||
if (e.Scancode == 41) running = false;
|
||||
if (e.Repeat == 0 && animations?.Select(e.Scancode) == true)
|
||||
{
|
||||
animationStarted = clock.Elapsed.TotalSeconds;
|
||||
animationSwitched = true;
|
||||
if (!Sdl.SDL_SetWindowTitle(window, animations.WindowTitle)) throw new InvalidOperationException(Sdl.Error);
|
||||
}
|
||||
if (e.Scancode == 6 && e.Repeat == 0 && originalStart != null)
|
||||
originalCamera = !originalCamera;
|
||||
if (e.Scancode == 68 && e.Repeat == 0)
|
||||
@@ -176,9 +187,9 @@ internal static class Program
|
||||
? 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)
|
||||
if (animations != null)
|
||||
{
|
||||
animation.Sample(fixedTime ?? (headless ? count / 60.0 : now), mesh.Vertices.AsSpan(characterOffset), characterModel);
|
||||
animations.Current.Sample(fixedTime ?? (headless ? count / 60.0 : now - animationStarted), mesh.Vertices.AsSpan(characterOffset), characterModel);
|
||||
renderer.UpdateVertices(mesh.Vertices);
|
||||
}
|
||||
renderer.Render(view * projection);
|
||||
@@ -189,6 +200,11 @@ internal static class Program
|
||||
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 (animations != null)
|
||||
{
|
||||
var cycle = new Sdl.Event { Type = Sdl.KeyDown, Scancode = 31, Down = 1 };
|
||||
if (!Sdl.SDL_PushEvent(ref cycle)) throw new InvalidOperationException(Sdl.Error);
|
||||
}
|
||||
}
|
||||
if (smoke && renderer.Width == 800 && renderer.Height == 600) didResize = true;
|
||||
if (frames > 0 && count >= frames) break;
|
||||
@@ -207,6 +223,11 @@ internal static class Program
|
||||
{
|
||||
Console.WriteLine($"SDL3 injected keyboard event received: {sawKey}; swapchain resized to 800x600: {didResize}.");
|
||||
if (!sawKey || !didResize) return 2;
|
||||
if (animations != null)
|
||||
{
|
||||
Console.WriteLine($"Animation preview switched by keyboard to {animations.Name}: {animationSwitched}; moving source samples: {animations.Current.HasVertexMotion}.");
|
||||
if (!animationSwitched || !animations.Current.HasVertexMotion) return 2;
|
||||
}
|
||||
}
|
||||
return renderer.ValidationErrors > 0 || changed == 0 ? 2 : 0;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user