Skip to content

Commit

Permalink
Fix bone rotations converting to FLVER
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadowth117 committed Jan 7, 2023
1 parent cba41a0 commit dd19c83
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 322 deletions.
1 change: 0 additions & 1 deletion AquaModelLibrary.Native/FbxExporterCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ namespace AquaModelLibrary::Objects::Processing::Fbx
Quaternion rotation;

Matrix4x4::Decompose(localTransformation, scale, rotation, translation);

FbxVector4 eulerAngles;
eulerAngles.SetXYZ(FbxQuaternion(rotation.X, rotation.Y, rotation.Z, rotation.W));

Expand Down
13 changes: 12 additions & 1 deletion AquaModelLibrary/AquaMethods/AquaGeneralMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,18 @@ public static string ReadCStringPSZ(BufferedStreamReader streamReader)

public static string ReadCString(BufferedStreamReader streamReader, int blockSize = 0x100)
{
string str = Encoding.ASCII.GetString(streamReader.ReadBytes(streamReader.Position(), blockSize)); //Shouldn't ever be more than 0x60... in theory
var pos = streamReader.Position();
var strLen = streamReader.BaseStream().Length;
if (strLen <= pos + blockSize)
{
blockSize = (int)(strLen - pos);
}
//Past end of file
if(blockSize <= 0)
{
return null;
}
string str = Encoding.ASCII.GetString(streamReader.ReadBytes(pos, blockSize)); //Shouldn't ever be more than 0x60... in theory
return str.Remove(str.IndexOf(char.MinValue));
}

Expand Down
20 changes: 16 additions & 4 deletions AquaModelLibrary/AquaMethods/AquaGeneralParsingMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,10 @@ public static LandPieceSettings ReadNIFLLPS(BufferedStreamReader streamReader, i
var bookmark = streamReader.Position();
streamReader.Seek(strPtr + offset, SeekOrigin.Begin);
var str = ReadCString(streamReader);

if(str == null)
{
str = $"value_{i}";
}
lps.fVarDict.Add(str, flt);

streamReader.Seek(bookmark, SeekOrigin.Begin);
Expand All @@ -1264,7 +1267,10 @@ public static LandPieceSettings ReadNIFLLPS(BufferedStreamReader streamReader, i
var str = ReadCString(streamReader);
streamReader.Seek(strPtr2 + offset, SeekOrigin.Begin);
var str2 = ReadCString(streamReader);

if (str == null)
{
str = $"value_{i}";
}
lps.stringVarDict.Add(str, str2);

streamReader.Seek(bookmark, SeekOrigin.Begin);
Expand All @@ -1279,7 +1285,10 @@ public static LandPieceSettings ReadNIFLLPS(BufferedStreamReader streamReader, i
var str = ReadCString(streamReader);
streamReader.Seek(strPtr2 + offset, SeekOrigin.Begin);
var str2 = ReadCString(streamReader);

if (str == null)
{
str = $"value_{i}";
}
lps.areaEntryExitDefaults.Add(str, str2);

streamReader.Seek(bookmark, SeekOrigin.Begin);
Expand All @@ -1294,7 +1303,10 @@ public static LandPieceSettings ReadNIFLLPS(BufferedStreamReader streamReader, i
var str = ReadCString(streamReader);
streamReader.Seek(strPtr2 + offset, SeekOrigin.Begin);
var str2 = ReadCString(streamReader);

if (str == null)
{
str = $"value_{i}";
}
lps.areaEntryExitDefaults2.Add(str, str2);

streamReader.Seek(bookmark, SeekOrigin.Begin);
Expand Down
59 changes: 18 additions & 41 deletions AquaModelLibrary/Extra/MathExtras.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public enum RotationOrder
YXZ,
YZX,
ZXY,
ZYX
ZYX,
}

public static class MathExtras
Expand Down Expand Up @@ -266,13 +266,14 @@ public static Vector3 QuaternionToEulerRadiansByOrder(Quaternion quat, RotationO
return angles;
}

public static Vector3 QuaternionToEulerOld(Quaternion quat, RotationOrder order = RotationOrder.XYZ)
public static Vector3 QuaternionToEulerOld(Quaternion quat)
{
return QuaternionToEulerRadiansOld(quat, order) * (float)(180 / Math.PI);
return QuaternionToEulerRadiansOld(quat) * (float)(180 / Math.PI);
}

//Based on C++ code at https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
public static Vector3 QuaternionToEulerRadiansOld(Quaternion quat, RotationOrder order = RotationOrder.XYZ)
//Handles Gimbal Lock on Y axis
public static Vector3 QuaternionToEulerRadiansOld(Quaternion quat)
{
Vector3 angles;

Expand All @@ -296,41 +297,30 @@ public static Vector3 QuaternionToEulerRadiansOld(Quaternion quat, RotationOrder
return angles;
}

public static RotationOrder DetermineRotationOrder(Quaternion quat)
public static RotationOrder GetRotationOrder(Quaternion quat)
{
var rotMat = Matrix4x4.CreateFromQuaternion(quat);
if (Math.Abs(rotMat.M13) >= 0.9999999)
{
return RotationOrder.XYZ;
}
else if (Math.Abs(rotMat.M23) >= 0.9999999)
{
return RotationOrder.YXZ;
} else
{
//
}
double sinr_cosp = 2 * (quat.W * quat.X + quat.Y * quat.Z);
double sinp = 2 * (quat.W * quat.Y - quat.Z * quat.X);
double siny_cosp = 2 * (quat.W * quat.Z + quat.X * quat.Y);

return RotationOrder.XYZ;
}
bool xHigh = Math.Abs(sinr_cosp) >= 1;
bool yHigh = Math.Abs(sinp) >= 1;
bool zHigh = Math.Abs(siny_cosp) >= 1;

public static RotationOrder DetermineRotationOrderCombinationSouls(Quaternion quat)
{
var rotMat = Matrix4x4.CreateFromQuaternion(quat);
if ((Math.Abs(rotMat.M12) >= 0.9999999) || (Math.Abs(rotMat.M21) >= 0.9999999))
if (!yHigh)
{
return RotationOrder.XZY;
return RotationOrder.XYZ;
}
else if ((Math.Abs(rotMat.M13) >= 0.9999999) || (Math.Abs(rotMat.M31) >= 0.9999999))
else if (!zHigh)
{
return RotationOrder.XYZ;
return RotationOrder.XZY;
}
else if ((Math.Abs(rotMat.M23) >= 0.9999999) || (Math.Abs(rotMat.M32) < 0.9999999))
else if (!xHigh)
{
return RotationOrder.YXZ;
}

return RotationOrder.XZY;
return RotationOrder.XYZ;
}

//Adapted from https://github.com/mrdoob/three.js/blob/4d6d52aca6fd714fbf0aedb16ce0b8da5701e681/src/math/Euler.js#L105
Expand Down Expand Up @@ -426,19 +416,6 @@ public static Vector3 QuaternionToEulerRadians(Quaternion quat, RotationOrder or
}
break;
}

if(float.IsNaN(angles.X))
{
var a = 0;
}
if (float.IsNaN(angles.Y))
{
var b = 0;
}
if (float.IsNaN(angles.Z))
{
var c = 0;
}

return angles;
}
Expand Down
3 changes: 1 addition & 2 deletions AquaModelLibrary/Extra/ModelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -916,10 +916,9 @@ private static void IterateAiNodesAQP(AquaObject aqp, AquaNode aqn, Assimp.Scene
Matrix4x4 worldMat;
var localMat = SwapRow4Column4Mat4(GetMat4FromAssimpMat4(aiNode.Transform));
worldMat = localMat;

if (node.parentId != -1)
{
//Matrix4x4.Invert(aqn.nodeList[node.parentId].GetInverseBindPoseMatrix(), out var parMatrix);
//worldMat *= parMatrix;
worldMat = GetWorldTransform(aiNode);
}
worldMat = SetMatrixScale(worldMat);
Expand Down
2 changes: 1 addition & 1 deletion AquaModelLibrary/Extra/ReferenceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ public unsafe static void OutputFileLists(string pso2_binDir, string outputDirec
List<Dictionary<string, string>> strNameDicts;
List<string> genAnimList, genAnimListNGS;

GenerateCharacterPartLists(pso2_binDir, playerDirOut, playerClassicDirOut, playerRebootDirOut, aquaCMX, faceIds, textByCat, out masterIdList, out nameDicts, out masterNameList, out strNameDicts);
GenerateUILists(pso2_binDir, outputDirectory);
DumpPaletteData(outputDirectory, aquaCMX);
GenerateMusicData(pso2_binDir, musicDirOut);
GenerateCasinoData(pso2_binDir, outputDirectory);
GenerateAreaData(pso2_binDir, stageDirOut);
GenerateRoomLists(pso2_binDir, outputDirectory);
GenerateUnitLists(pso2_binDir, outputDirectory);
GenerateCharacterPartLists(pso2_binDir, playerDirOut, playerClassicDirOut, playerRebootDirOut, aquaCMX, faceIds, textByCat, out masterIdList, out nameDicts, out masterNameList, out strNameDicts);
GenerateVoiceLists(pso2_binDir, playerDirOut, npcDirOut, textByCat, masterIdList, nameDicts, masterNameList, strNameDicts, actorNameByCat, actorNameRebootByCat, actorNameRebootNPCByCat);
GenerateWeaponLists(pso2_binDir, outputDirectory);
GenerateLobbyActionLists(pso2_binDir, playerCAnimDirOut, playerRAnimDirOut, lac, rebootLac, lacTruePath, lacTruePathReboot, commByCat, commRebootByCat, masterNameList, strNameDicts);
Expand Down
Loading

0 comments on commit dd19c83

Please sign in to comment.