Skip to content

Commit

Permalink
fix nans in cloth bones
Browse files Browse the repository at this point in the history
  • Loading branch information
ZingBallyhoo committed Apr 23, 2024
1 parent b5fff1c commit d583739
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 257 deletions.
4 changes: 2 additions & 2 deletions TankLib/Chunks/teModelChunk_Hardpoint.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using TankLib.Math;

namespace TankLib.Chunks {
/// <inheritdoc />
Expand Down Expand Up @@ -30,7 +30,7 @@ public struct HardpointHeader {
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Hardpoint {
/// <summary>4x4 matrix</summary>
public teMtx44 Matrix;
public Matrix4x4 Matrix;

/// <summary>03C Hardpoint GUID</summary>
public teResourceGUID GUID;
Expand Down
46 changes: 35 additions & 11 deletions TankLib/Chunks/teModelChunk_Skeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ public struct SkeletonHeader {
// ... idk. something added tho
}

public struct BoneTransform {
public teQuat Orientation;
public teVec3 Scale;
public float Pad;
public teVec3 Translation;
public float Pad2;
}

/// <summary>Header data</summary>
public SkeletonHeader Header;

public Matrix4x4[] Matrices;
public teMtx44[] MatricesInverted;
public teMtx43[] Matrices34;
public teMtx43[] Matrices34Inverted;
public Matrix4x4[] MatricesInverted;
public BoneTransform[] BindPose;
public BoneTransform[] BindPoseDeltas;
public short[] Hierarchy;
public ushort[] Lookup;
// ReSharper disable once InconsistentNaming
Expand All @@ -65,9 +73,9 @@ public void Parse(Stream input) {
}

Matrices = new Matrix4x4[Header.BonesAbs];
MatricesInverted = new teMtx44[Header.BonesAbs];
Matrices34 = new teMtx43[Header.BonesAbs];
Matrices34Inverted = new teMtx43[Header.BonesAbs];
MatricesInverted = new Matrix4x4[Header.BonesAbs];
BindPose = new BoneTransform[Header.BonesAbs];
BindPoseDeltas = new BoneTransform[Header.BonesAbs];

if (Header.Matrix44Offset > 0) {
input.Position = Header.Matrix44Offset;
Expand All @@ -76,17 +84,17 @@ public void Parse(Stream input) {

if (Header.Matrix44iOffset > 0) {
input.Position = Header.Matrix44iOffset;
MatricesInverted = reader.ReadArray<teMtx44>(Header.BonesAbs);
MatricesInverted = reader.ReadArray<Matrix4x4>(Header.BonesAbs);
}

if (Header.Matrix43Offset > 0) {
input.Position = Header.Matrix43Offset;
Matrices34 = reader.ReadArray<teMtx43>(Header.BonesAbs);
BindPose = reader.ReadArray<BoneTransform>(Header.BonesAbs);
}

if (Header.Matrix43iOffset > 0) {
input.Position = Header.Matrix43iOffset;
Matrices34Inverted = reader.ReadArray<teMtx43>(Header.BonesAbs);
BindPoseDeltas = reader.ReadArray<BoneTransform>(Header.BonesAbs);
}

Lookup = new ushort[Header.RemapCount];
Expand All @@ -104,9 +112,25 @@ public void Parse(Stream input) {
}
}

public void GetWorldSpace(int idx, out teVec3 scale, out teQuat rotation, out teVec3 translation) {
if (idx == -1) {
translation = default;
rotation = teQuat.Identity;
scale = new teVec3(1, 1, 1);
return;
}
var bone = BindPose[idx];
scale = bone.Scale;
rotation = bone.Orientation;
translation = bone.Translation;
}

public Matrix4x4 GetWorldSpace(int idx) {
if (idx == -1) return Matrix4x4.Identity;
return Matrices[idx];
// note: you shouldn't use matrices from disk.. they have det of 0..
GetWorldSpace(idx, out teVec3 scale, out teQuat rotation, out teVec3 translation);
return Matrix4x4.CreateScale(scale) *
Matrix4x4.CreateFromQuaternion(rotation) *
Matrix4x4.CreateTranslation(translation);
}
}
}
10 changes: 6 additions & 4 deletions TankLib/ExportFormats/OverwatchModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ public void Write(Stream stream) {
public static void GetRefPoseTransform(int i, short[] hierarchy, teModelChunk_Skeleton skeleton, Dictionary<int, teModelChunk_Cloth.ClothNode> clothNodeMap, out teVec3 scale, out teQuat quat,
out teVec3 translation) {
if (clothNodeMap != null && clothNodeMap.ContainsKey(i)) {
// re-parent to the new parent the cloth system has given us

Matrix4x4 thisMat = skeleton.GetWorldSpace(i);
Matrix4x4 parentMat = skeleton.GetWorldSpace(hierarchy[i]);

Expand All @@ -275,11 +277,11 @@ public static void GetRefPoseTransform(int i, short[] hierarchy, teModelChunk_Sk
scale = new teVec3(scl2.X, scl2.Y, scl2.Z);
translation = new teVec3(pos2.X, pos2.Y, pos2.Z);
} else {
teMtx43 bone = skeleton.Matrices34Inverted[i];
var bone = skeleton.BindPoseDeltas[i];

scale = new teVec3(bone[1, 0], bone[1, 1], bone[1, 2]);
quat = new teQuat(bone[0, 0], bone[0, 1], bone[0, 2], bone[0, 3]);
translation = new teVec3(bone[2, 0], bone[2, 1], bone[2, 2]);
scale = bone.Scale;
quat = bone.Orientation;
translation = bone.Translation;
}
}

Expand Down
144 changes: 0 additions & 144 deletions TankLib/Math/teMtx43.cs

This file was deleted.

63 changes: 0 additions & 63 deletions TankLib/Math/teMtx44.cs

This file was deleted.

33 changes: 0 additions & 33 deletions TankLib/teMap.cs

This file was deleted.

0 comments on commit d583739

Please sign in to comment.