Skip to content

Commit

Permalink
Remove cs dependency from Meddle Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
PassiveModding committed Nov 18, 2024
1 parent fca4b05 commit e6a4463
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 41 deletions.
40 changes: 14 additions & 26 deletions Meddle/Meddle.Utils/Files/PbdFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Numerics;
using System.Runtime.InteropServices;
using FFXIVClientStructs.Havok.Common.Base.Math.QsTransform;

namespace Meddle.Utils.Files;

Expand Down Expand Up @@ -57,7 +56,7 @@ public struct Link
public ushort HeaderIdx;
}

/*public readonly struct DeformMatrix4x4
public readonly struct DeformMatrix4x4
{
private readonly float[] matrix;

Expand All @@ -81,26 +80,20 @@ public DeformMatrix4x4(float[] matrix)
this.matrix = matrix;
}

public static DeformMatrix4x4 Identity => new([
0, 0, 0, 0, // Translation (vec3 + unused)
0, 0, 0, 1, // Rotation (vec4)
1, 1, 1, 0, // Scale (vec3 + unused)
]);
// https://github.com/TexTools/xivModdingFramework/blob/459b863fdacf291ee0817feef379a18275f33010/xivModdingFramework/Models/Helpers/ModelModifiers.cs#L1202
public Vector3 TransformCoordinate(Vector3 vector) =>
new(
(vector.X * matrix[0]) + (vector.Y * matrix[1]) + (vector.Z * matrix[2]) + (1.0f * matrix[3]),
(vector.X * matrix[4]) + (vector.Y * matrix[5]) + (vector.Z * matrix[6]) + (1.0f * matrix[7]),
(vector.X * matrix[8]) + (vector.Y * matrix[9]) + (vector.Z * matrix[10]) + (1.0f * matrix[11])
);
}*/
}

public struct Deformer
{
public int BoneCount;
public string[] BoneNames;
public hkQsTransformf?[] DeformMatrices;
public DeformMatrix4x4?[] DeformMatrices;

public static Deformer Read(SpanBinaryReader reader)
{
Expand All @@ -120,11 +113,14 @@ public static Deformer Read(SpanBinaryReader reader)
var padding = boneCount * 2 % 4;
reader.Read<byte>(padding);

var matrixArray = new hkQsTransformf?[boneCount];
var matrixArray = new DeformMatrix4x4?[boneCount];
for (var i = 0; i < boneCount; i++)
{
var matrix = reader.Read<hkQsTransformf>();
matrixArray[i] = matrix;
var matrix = reader.Read<float>(12);
var buf = new float[16];
matrix.CopyTo(buf);
buf[15] = 1.0f;
matrixArray[i] = new DeformMatrix4x4(buf);
}

return new Deformer
Expand All @@ -135,7 +131,7 @@ public static Deformer Read(SpanBinaryReader reader)
};
}

public static unsafe hkQsTransformf Identity()
public static DeformMatrix4x4 Identity()
{
var buf = new float[]
{
Expand All @@ -144,20 +140,12 @@ public static unsafe hkQsTransformf Identity()
1, 1, 1, 0, // Scale (vec3 + unused)
0, 0, 0, 1, // Affine
};

// marshal the buffer to a struct
fixed (float* ptr = buf)
{
return *(hkQsTransformf*)ptr;
}

return new DeformMatrix4x4(buf);
}

public static Vector3 TransformCoordinate(Vector3 vector, hkQsTransformf matrix) =>
new(
(vector.X * matrix.Translation.X) + (vector.Y * matrix.Translation.Y) + (vector.Z * matrix.Translation.Z) + (1.0f * matrix.Translation.W),
(vector.X * matrix.Rotation.X) + (vector.Y * matrix.Rotation.Y) + (vector.Z * matrix.Rotation.Z) + (1.0f * matrix.Rotation.W),
(vector.X * matrix.Scale.X) + (vector.Y * matrix.Scale.Y) + (vector.Z * matrix.Scale.Z) + (1.0f * matrix.Scale.W)
);
public static Vector3 TransformCoordinate(Vector3 vector, DeformMatrix4x4 matrix) =>
matrix.TransformCoordinate(vector);
}

public IEnumerable<Deformer> GetDeformers(ushort from, ushort to)
Expand Down
12 changes: 0 additions & 12 deletions Meddle/Meddle.Utils/Meddle.Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,12 @@
<HintPath>Lib\OtterTex.dll</HintPath>
</Reference>
</ItemGroup>

<PropertyGroup>
<DalamudLibPath>$(appdata)\XIVLauncher\addon\Hooks\dev\</DalamudLibPath>
</PropertyGroup>

<PropertyGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))'">
<DalamudLibPath>$(DALAMUD_HOME)/</DalamudLibPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="SharpGLTF.Core" Version="1.0.2" />
<PackageReference Include="SharpGLTF.Toolkit" Version="1.0.2" />
<PackageReference Include="SkiaSharp" Version="2.88.8"/>
<PackageReference Include="System.IO.Hashing" Version="8.0.0"/>
<Reference Include="FFXIVClientStructs">
<HintPath>$(DalamudLibPath)FFXIVClientStructs.dll</HintPath>
<Private>false</Private>
</Reference>
</ItemGroup>
</Project>
4 changes: 1 addition & 3 deletions Meddle/Meddle.Utils/RaceDeformer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Numerics;
using System.Text.RegularExpressions;
using FFXIVClientStructs.Havok.Common.Base.Math.QsTransform;
using Meddle.Utils.Constants;
using Meddle.Utils.Export;
using Meddle.Utils.Files;

namespace Meddle.Utils;
Expand All @@ -13,7 +11,7 @@ public partial class RaceDeformer(PbdFile pbd, IReadOnlyList<BoneNodeBuilder> bo
public PbdFile PbdFile { get; } = pbd;
private IReadOnlyList<BoneNodeBuilder> BoneMap { get; } = boneMap;

private hkQsTransformf? ResolveDeformation(PbdFile.Deformer deformer, string name)
private PbdFile.DeformMatrix4x4? ResolveDeformation(PbdFile.Deformer deformer, string name)
{
// Try and fetch it from the PBD
var boneNames = deformer.BoneNames;
Expand Down

0 comments on commit e6a4463

Please sign in to comment.