Correct preview ground placement and expose original animation clips

This commit is contained in:
UnixNight committed 2026-10-23 18:12:54 -04:00
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;
}
}