Skip to content

Commit

Permalink
Finally got the melee meshes loading correctly!
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Oct 26, 2023
1 parent 36122f4 commit 3692dff
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
24 changes: 18 additions & 6 deletions FinModelUtility/Formats/Dat/src/api/DatModelImporter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Linq;
using System.Runtime.InteropServices.ComTypes;

using Assimp;
using Assimp.Unmanaged;

using dat.schema;
Expand All @@ -22,9 +23,11 @@ public IModel ImportModel(DatModelFileBundle modelFileBundle) {
modelFileBundle.PrimaryDatFile.ReadNew<Dat>(Endianness.BigEndian);

var finModel = new ModelImpl();
var finSkin = finModel.Skin;

// Adds skeleton
var finBoneByJObj = new Dictionary<JObj, IBone>();
var boneWeightsByJObj = new Dictionary<JObj, IBoneWeights>();
var boneQueue = new Queue<(IBone finParentBone, JObj datBone)>();
foreach (var datRootBone in dat.RootJObjs) {
boneQueue.Enqueue((finModel.Skeleton.Root, datRootBone));
Expand All @@ -50,6 +53,8 @@ public IModel ImportModel(DatModelFileBundle modelFileBundle) {
finBone.Name = jObj.Name;

finBoneByJObj[jObj] = finBone;
boneWeightsByJObj[jObj] =
finSkin.GetOrCreateBoneWeights(VertexSpace.BONE, finBone);

foreach (var datChildBone in jObj.Children) {
boneQueue.Enqueue((finBone, datChildBone));
Expand All @@ -61,9 +66,10 @@ public IModel ImportModel(DatModelFileBundle modelFileBundle) {
var finMaterialsByMObjOffset = new Dictionary<uint, IMaterial>();
var finTexturesByTObjOffset = new Dictionary<uint, ITexture>();

var finSkin = finModel.Skin;
var finMesh = finSkin.AddMesh();
foreach (var jObj in dat.JObjs) {
var defaultBoneWeights = boneWeightsByJObj[jObj];

foreach (var dObj in jObj.DObjs) {
// Gets material
IMaterial? finMaterial = null;
Expand Down Expand Up @@ -92,7 +98,8 @@ public IModel ImportModel(DatModelFileBundle modelFileBundle) {
}
}

finMaterial = finMaterialManager.AddTextureMaterial(firstTexture!);
finMaterial =
finMaterialManager.AddTextureMaterial(firstTexture!);
}

finMaterial.Name = mObj.Name ?? mObjOffset.ToHex();
Expand All @@ -111,7 +118,7 @@ public IModel ImportModel(DatModelFileBundle modelFileBundle) {
.Select(
pObjWeight => new BoneWeight(
finBoneByJObj[pObjWeight.JObj],
null,
pObjWeight.JObj.InverseBindMatrix,
pObjWeight.Weight
))
.ToArray()))
Expand All @@ -137,9 +144,14 @@ public IModel ImportModel(DatModelFileBundle modelFileBundle) {
finVertex.SetUv(1, uv1.X, uv1.Y);
}

if (datVertex.WeightId != null && finWeights != null) {
finVertex.SetBoneWeights(
finWeights[datVertex.WeightId.Value]);
// TODO: Is this right???
if (datVertex.WeightId != null) {
if (finWeights != null) {
finVertex.SetBoneWeights(
finWeights[datVertex.WeightId.Value]);
}
} else {
finVertex.SetBoneWeights(defaultBoneWeights);
}

return finVertex;
Expand Down
30 changes: 19 additions & 11 deletions FinModelUtility/Formats/Dat/src/schema/Dat.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using System.Numerics;

using CommunityToolkit.HighPerformance;

using fin.color;
using fin.data.queues;
using fin.math.matrix.four;
using fin.schema.matrix;
using fin.model.impl;
using fin.util.asserts;
using fin.util.enumerables;
using fin.util.hex;

using gx;

using schema.binary;
using schema.binary.attributes;

Expand Down Expand Up @@ -157,10 +151,24 @@ uint jObjDataOffset
br.Position = jObjDataOffset;
jObjData.Read(br);

br.Position = jObjData.InverseBindMatrixOffset;
var inverseBindMatrixValues = new float[4 * 4];
br.ReadSingles(inverseBindMatrixValues.AsSpan(0, 4 * 3));
jObj.InverseBindMatrix = new FinMatrix4x4(inverseBindMatrixValues).TransposeInPlace();
if (jObjData.InverseBindMatrixOffset != 0) {
br.Position = jObjData.InverseBindMatrixOffset;

var inverseBindMatrixValues = new float[4 * 4];
br.ReadSingles(inverseBindMatrixValues.AsSpan(0, 4 * 3));
inverseBindMatrixValues[15] = 1;
jObj.InverseBindMatrix =
new FinMatrix4x4(inverseBindMatrixValues).TransposeInPlace();

var parentInverseBindMatrix =
parentJObj?.InverseBindMatrix ?? FinMatrix4x4.IDENTITY;
var thisInverseBindMatrix =
parentInverseBindMatrix.CloneAndInvert()
.MultiplyInPlace(jObj.InverseBindMatrix!);

var thisMatrix = thisInverseBindMatrix.CloneAndInvert();
;
}

var firstChildOffset = jObj.Data.FirstChildBoneOffset;
if (this.AssertNullOrValidPointer_(firstChildOffset)) {
Expand Down
2 changes: 1 addition & 1 deletion FinModelUtility/Formats/Dat/src/schema/JObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public IEnumerable<DObj> DObjs {

public List<JObj> Children { get; } = new();

public IFinMatrix4x4 InverseBindMatrix { get; set; }
public IFinMatrix4x4? InverseBindMatrix { get; set; }

public override string ToString() => this.Name;

Check failure on line 93 in FinModelUtility/Formats/Dat/src/schema/JObj.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check failure on line 93 in FinModelUtility/Formats/Dat/src/schema/JObj.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}
Expand Down
3 changes: 1 addition & 2 deletions FinModelUtility/Formats/Dat/src/schema/PObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace dat.schema {
[Flags]
public enum PObjFlags : ushort {
OBJTYPE_SKIN = 0 << 12,
USE_BONES_DIRECTLY = 1 << 2,
OBJTYPE_SHAPEANIM = 1 << 12,
OBJTYPE_ENVELOPE = 1 << 13,
OBJTYPE_MASK = 0x3000,
Expand Down Expand Up @@ -114,7 +113,7 @@ private void ReadDisplayList_(IBinaryReader br) {
var pObjWeights = this.Weights = new List<IList<PObjWeight>>();

// Weight list is children of a given bone
if (this.Header.Flags.CheckFlag(PObjFlags.USE_BONES_DIRECTLY)) {
if (!this.Header.Flags.CheckFlag(PObjFlags.OBJTYPE_ENVELOPE)) {
var currentJObjOffset = weightListOffset;
while (currentJObjOffset != 0) {
var jObj = this.dat_.JObjByOffset[currentJObjOffset];
Expand Down

0 comments on commit 3692dff

Please sign in to comment.