Skip to content

Commit

Permalink
Fixes for model importer
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadowth117 committed Dec 22, 2022
1 parent 1aef785 commit 7a68dd0
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 59 deletions.
18 changes: 18 additions & 0 deletions AquaModelLibrary/Extra/MathExtras.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ namespace AquaModelLibrary.Extra
{
public static class MathExtras
{
public static void MirrorX(this Quaternion quat)
{
quat.Y = -quat.Y;
quat.Z = -quat.Z;
}

public static void MirrorY(this Quaternion quat)
{
quat.X = -quat.X;
quat.Z = -quat.Z;
}
public static void MirrorZ(this Quaternion quat)
{
quat.X = -quat.X;
quat.Y = -quat.Y;
}


public static Quaternion ToQuat(this Vector4 vec4)
{
return new Quaternion(vec4.X, vec4.Y, vec4.Z, vec4.W);
Expand Down
16 changes: 11 additions & 5 deletions AquaModelLibrary/Extra/ModelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -780,11 +780,8 @@ public static AquaObject AssimpAquaConvertFull(string initialFilePath, float sca
//Default to this so ids can be assigned by order if needed
Dictionary<string, int> boneDict = new Dictionary<string, int>();
var aqnRoot = GetRootNode(aiScene.RootNode);
if (aqnRoot == null || preAssignNodeIds == true)
{
int nodeCounter = 0;
BuildAiNodeDictionary(aiScene.RootNode, ref nodeCounter, boneDict);
}
int nodeCounter = 0;
BuildAiNodeDictionary(aiScene.RootNode, ref nodeCounter, boneDict);
IterateAiNodesAQP(aqp, aqn, aiScene, aiScene.RootNode, Matrix4x4.Transpose(GetMat4FromAssimpMat4(aiScene.RootNode.Transform)), baseScale, boneDict);

//Generate bonepalette. No real reason not to just put in every bone at the moment.
Expand Down Expand Up @@ -855,6 +852,11 @@ private static void IterateAiNodesAQP(AquaObject aqp, AquaNode aqn, Assimp.Scene
//Decide if this is an effect node or not
string nodeName = aiNode.Name;
var nodeParent = aiNode.Parent;
if(nodeName.EndsWith("_end"))
{
//Blender, please
return;
}
if (ParseNodeId(nodeName, out string finalName, out int nodeId))
{
AquaNode.NODE node = new AquaNode.NODE();
Expand Down Expand Up @@ -1098,6 +1100,10 @@ public static void AddAiMeshToAQP(AquaObject aqp, Assimp.Mesh mesh, Matrix4x4 no
List<float> vertWeights = new List<float>();
foreach (var bone in mesh.Bones)
{
if(!boneDict.ContainsKey(bone.Name))
{
continue;
}
var boneId = boneDict[bone.Name];
foreach (var weight in bone.VertexWeights)
{
Expand Down
56 changes: 42 additions & 14 deletions AquaModelLibrary/Extra/SoulsConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using static AquaModelLibrary.AquaNode;
using NvTriStripDotNet;
using AquaModelLibrary.Extra.FromSoft;
using AquaModelLibrary.Native.Fbx;

namespace AquaModelLibrary.Extra
{
Expand Down Expand Up @@ -92,9 +93,20 @@ public static void DebugDumpToFile(FLVER0 flver, int id)
sb2.AppendLine(flver.Meshes[m].Vertices[faces[f]].Tangents[0].ToString());
}
}
StringBuilder sb3 = new StringBuilder();
for (int m = 0; m < flver.Meshes.Count; m++)
{
var faces = flver.Meshes[m].Triangulate(flver.Header.Version);
for (int f = 0; f < faces.Count; f++)
{
var indices = flver.Meshes[m].Vertices[faces[f]].BoneIndices;
sb3.AppendLine($"{indices[0]} {indices[1]} {indices[2]} {indices[3]}");
}
}

File.WriteAllText($"C:\\Normals_{id}", sb.ToString());
File.WriteAllText($"C:\\NormalsTan_{id}", sb2.ToString());
File.WriteAllText($"C:\\NormalsWeight_{id}", sb3.ToString());
#endif
}

Expand All @@ -112,7 +124,6 @@ public static AquaObject MDL4ToAqua(SoulsFormats.Other.MDL4 mdl4, out AquaNode a
var tfmMat = Matrix4x4.Identity;

Matrix4x4 mat = flverBone.ComputeLocalTransform();

mat *= tfmMat;

Matrix4x4.Decompose(mat, out var scale, out var quatRot, out var translation);
Expand Down Expand Up @@ -301,11 +312,8 @@ public static AquaObject FlverToAqua(IFlver flver, out AquaNode aqn, bool useMet
var tfmMat = Matrix4x4.Identity;

Matrix4x4 mat = flverBone.ComputeLocalTransform(order);

mat *= tfmMat;

Matrix4x4.Decompose(mat, out var scale, out var quatRot, out var translation);

//If there's a parent, multiply by it
if (parentId != -1)
{
Expand Down Expand Up @@ -542,7 +550,7 @@ public static IFlver ConvertModelToFlver(string initialFilePath, float scaleFact
{
var aqp = ModelImporter.AssimpAquaConvertFull(initialFilePath, scaleFactor, preAssignNodeIds, isNGS, out AquaNode aqn);

//Demon's Souls and Dark Souls models have a limit of 28 bones.
//Demon's Souls has a limit of 28 bones per mesh.
AquaObjectMethods.BatchSplitByBoneCount(aqp, 28, true);
return AquaToFlver(initialFilePath, aqp, aqn, game, referenceFlver);
}
Expand Down Expand Up @@ -721,13 +729,27 @@ public static IFlver AquaToFlver(string initialFilePath, AquaObject aqp, AquaNod
flvMesh.BoneIndices = new short[28];
for(int b = 0; b < 28; b++)
{
//Correct this for bone split meshes later
if (aqp.bonePalette.Count > b)
if(vtxl.bonePalette.Count > 0)
{
flvMesh.BoneIndices[b] = (short)aqp.bonePalette[b];

if (vtxl.bonePalette.Count > b)
{
flvMesh.BoneIndices[b] = (short)vtxl.bonePalette[b];
}
else
{
flvMesh.BoneIndices[b] = -1;
}
} else
{
flvMesh.BoneIndices[b] = -1;
if (aqp.bonePalette.Count > b)
{
flvMesh.BoneIndices[b] = (short)aqp.bonePalette[b];
}
else
{
flvMesh.BoneIndices[b] = -1;
}
}
}

Expand Down Expand Up @@ -906,11 +928,10 @@ public static IFlver AquaToFlver(string initialFilePath, AquaObject aqp, AquaNod
{
var aqBone = aqn.nodeList[i];
Matrix4x4.Invert(aqBone.GetInverseBindPoseMatrix(), out Matrix4x4 aqBoneWorldTfm);
aqBoneWorldTfm *= mirrorMat;

//Set the inverted transform so when we read it back we can use it for parent calls
Matrix4x4.Invert(aqBoneWorldTfm, out Matrix4x4 aqBoneInvWorldTfm);
aqn.nodeList[i].SetInverseBindPoseMatrix(aqBoneInvWorldTfm);
//Matrix4x4.Invert(aqBoneWorldTfm, out Matrix4x4 aqBoneInvWorldTfm);
//aqn.nodeList[i].SetInverseBindPoseMatrix(aqBoneInvWorldTfm);

FLVER.Bone bone = new FLVER.Bone();
bone.Name = aqBone.boneName.GetString();
Expand All @@ -927,7 +948,7 @@ public static IFlver AquaToFlver(string initialFilePath, AquaObject aqp, AquaNod
if (aqBone.parentId == -1)
{
rootSiblings.Add(i);
localTfm = aqBoneInvWorldTfm;
localTfm = aqBoneWorldTfm;
} else
{
//Calc local transforms
Expand All @@ -939,7 +960,14 @@ public static IFlver AquaToFlver(string initialFilePath, AquaObject aqp, AquaNod
Matrix4x4.Decompose(localTfm, out var scale, out var rotation, out var translation);

bone.Translation = translation;
bone.Rotation = MathExtras.QuaternionToEuler(rotation);

//Swap to XZY
var eulerAngles = MathExtras.QuaternionToEuler(rotation);
var temp = eulerAngles.Y;
eulerAngles.Y = eulerAngles.Z;
eulerAngles.Z = temp;

bone.Rotation = eulerAngles;
bone.Scale = new Vector3(1, 1, 1);

flver.Bones.Add(bone);
Expand Down
Loading

0 comments on commit 7a68dd0

Please sign in to comment.