animations and QOL

This commit is contained in:
bird_egop
2026-06-12 02:06:32 +03:00
parent 4d0d1aca12
commit 1cb3940cce
14 changed files with 444 additions and 32 deletions
+2
View File
@@ -105,6 +105,8 @@ public static class Msh0x01
ushort FallbackKey0x08,
ushort[] Msh02SlotIndicesByStateAndLOD)
{
public ushort DefaultKeyframeIndex0x08 => FallbackKey0x08;
public ushort ResolveSlotIndex(int state, int lod = 0)
{
// State и LOD выбираются для конкретного узла/piece, не для всего файла сразу.
+9 -9
View File
@@ -5,7 +5,7 @@ namespace MshLib;
public static class Msh0x08
{
public static List<AnimationDescriptor> ReadComponent(FileStream mshFs, NResArchive archive)
public static List<MshTransformKeyframe> ReadComponent(FileStream mshFs, NResArchive archive)
{
var entry = archive.Files.FirstOrDefault(x => x.FileType == "08 00 00 00");
@@ -16,11 +16,11 @@ public static class Msh0x08
mshFs.Seek(entry.OffsetInFile, SeekOrigin.Begin);
var descriptors = new List<AnimationDescriptor>();
var descriptors = new List<MshTransformKeyframe>();
for (var i = 0; i < entry.ElementCount; i++)
{
descriptors.Add(new AnimationDescriptor(
descriptors.Add(new MshTransformKeyframe(
new Vector3(mshFs.ReadFloatLittleEndian(),
mshFs.ReadFloatLittleEndian(),
mshFs.ReadFloatLittleEndian()),
@@ -36,10 +36,10 @@ public static class Msh0x08
return descriptors;
}
public record AnimationDescriptor(
Vector3 Position,
float Time,
UShortQuaternion Rotation
);
}
public record MshTransformKeyframe(
Vector3 Position,
float Time,
UShortQuaternion Rotation
);
+53
View File
@@ -0,0 +1,53 @@
using System.Buffers.Binary;
using NResLib;
namespace MshLib;
public static class Msh0x13
{
public static Msh0x13Component ReadComponent(FileStream mshFs, NResArchive archive)
{
var entry = archive.Files.FirstOrDefault(x => x.FileType == "13 00 00 00");
if (entry is null)
{
throw new Exception("Archive doesn't contain animation map component (0x13)");
}
if (entry.ElementSize < 2)
{
throw new Exception("Animation map component (0x13) element size is too small");
}
var data = new byte[entry.FileLength];
mshFs.Seek(entry.OffsetInFile, SeekOrigin.Begin);
mshFs.ReadExactly(data, 0, data.Length);
var elementCount = checked((int)entry.ElementCount);
var entries = new List<MshAnimationMapEntry>(elementCount);
var span = data.AsSpan();
for (var i = 0; i < elementCount; i++)
{
var elementOffset = i * entry.ElementSize;
entries.Add(new MshAnimationMapEntry(
BinaryPrimitives.ReadUInt16LittleEndian(span.Slice(elementOffset, 2))));
}
return new Msh0x13Component(entry.ElementSize, entries)
{
MaxAnimationTime = entry.Magic1
};
}
}
public readonly record struct MshAnimationMapEntry(
ushort KeyframeIndex
);
public sealed record Msh0x13Component(
int ElementSize,
List<MshAnimationMapEntry> Entries
)
{
public int MaxAnimationTime { get; init; }
}
+92
View File
@@ -0,0 +1,92 @@
using System.Numerics;
namespace MshLib;
public static class MshAnimationSampler
{
public static MshSampledPieceTransform SamplePieceAnimationAtTime(
Msh0x01.Node node,
IReadOnlyList<MshTransformKeyframe> keyframes,
IReadOnlyList<MshAnimationMapEntry> animationMap,
float sampleTime)
{
var defaultKeyframeIndex = node.DefaultKeyframeIndex0x08;
if (defaultKeyframeIndex == ushort.MaxValue || defaultKeyframeIndex >= keyframes.Count)
return MshSampledPieceTransform.Identity;
var frameIndex = (int)MathF.Round(sampleTime - 0.5f, MidpointRounding.AwayFromZero);
if (frameIndex < 0 ||
node.AnimMapStart0x13 == ushort.MaxValue ||
node.AnimMapStart0x13 + frameIndex >= animationMap.Count)
{
return FromKeyframe(keyframes[defaultKeyframeIndex]);
}
var keyframeIndex = animationMap[node.AnimMapStart0x13 + frameIndex].KeyframeIndex;
if (keyframeIndex >= defaultKeyframeIndex ||
keyframeIndex >= keyframes.Count ||
keyframeIndex + 1 >= keyframes.Count)
{
return FromKeyframe(keyframes[defaultKeyframeIndex]);
}
var key0 = keyframes[keyframeIndex];
var key1 = keyframes[keyframeIndex + 1];
if (sampleTime == key0.Time)
return FromKeyframe(key0);
if (sampleTime == key1.Time)
return FromKeyframe(key1);
var duration = key1.Time - key0.Time;
if (MathF.Abs(duration) < 1e-6f)
return FromKeyframe(key0);
var t = (sampleTime - key0.Time) / duration;
return new MshSampledPieceTransform(
Vector3.Lerp(ToNumericsVector3(key0.Position), ToNumericsVector3(key1.Position), t),
Quaternion.Slerp(ToNumericsQuaternion(key0.Rotation), ToNumericsQuaternion(key1.Rotation), t));
}
public static Matrix4x4 ToMatrix(MshSampledPieceTransform transform)
{
var matrix = Matrix4x4.CreateFromQuaternion(transform.Rotation);
matrix.Translation = transform.Position;
return matrix;
}
public static MshSampledPieceTransform FromKeyframe(MshTransformKeyframe keyframe)
{
return new MshSampledPieceTransform(
ToNumericsVector3(keyframe.Position),
ToNumericsQuaternion(keyframe.Rotation));
}
private static Vector3 ToNumericsVector3(Common.Vector3 vector)
{
return new Vector3(vector.X, vector.Y, vector.Z);
}
private static Quaternion ToNumericsQuaternion(Common.UShortQuaternion packedQuaternion)
{
var q = new Quaternion(
packedQuaternion.X / 32767f,
packedQuaternion.Y / 32767f,
packedQuaternion.Z / 32767f,
packedQuaternion.W / 32767f);
if (q.LengthSquared() < 1e-8f)
return Quaternion.Identity;
// MSH stores mesh-to-parent rotation; viewport composition uses the inverse.
return Quaternion.Conjugate(Quaternion.Normalize(q));
}
}
public readonly record struct MshSampledPieceTransform(
Vector3 Position,
Quaternion Rotation)
{
public static MshSampledPieceTransform Identity { get; } = new(Vector3.Zero, Quaternion.Identity);
}