From 79154b9a4a4858e8be16db5580432f7df4825a00 Mon Sep 17 00:00:00 2001 From: "K. S. Ernest (iFire) Lee" Date: Sat, 12 Nov 2022 10:06:41 -0800 Subject: [PATCH 1/2] Revert to fix animation issues. Change version to 0.13.0. --- src/FBX2glTF.h | 2 +- src/fbx/Fbx2Raw.cpp | 32 ++++++-------------------------- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/src/FBX2glTF.h b/src/FBX2glTF.h index 3d42cb6..4128c2d 100644 --- a/src/FBX2glTF.h +++ b/src/FBX2glTF.h @@ -17,7 +17,7 @@ #include #endif -#define FBX2GLTF_VERSION std::string("0.12.9") +#define FBX2GLTF_VERSION std::string("0.13.0") #include diff --git a/src/fbx/Fbx2Raw.cpp b/src/fbx/Fbx2Raw.cpp index d66bcd9..39557a3 100644 --- a/src/fbx/Fbx2Raw.cpp +++ b/src/fbx/Fbx2Raw.cpp @@ -182,10 +182,7 @@ static void ReadMesh( const FbxVector4 meshTranslation = pNode->GetGeometricTranslation(FbxNode::eSourcePivot); const FbxVector4 meshRotation = pNode->GetGeometricRotation(FbxNode::eSourcePivot); const FbxVector4 meshScaling = pNode->GetGeometricScaling(FbxNode::eSourcePivot); - FbxAMatrix meshTransform(meshTranslation, meshRotation, meshScaling); - const FbxVector4 meshRotationPivot = pNode->GetRotationPivot(FbxNode::eSourcePivot); - const FbxAMatrix meshPivotTransform(-meshRotationPivot, FbxVector4(0, 0, 0, 0), FbxVector4(1, 1, 1, 1)); - meshTransform *= meshPivotTransform; + const FbxAMatrix meshTransform(meshTranslation, meshRotation, meshScaling); const FbxMatrix transform = meshTransform; // Remove translation & scaling from transforms that will bi applied to normals, tangents & @@ -686,23 +683,6 @@ static FbxVector4 computeLocalScale(FbxNode* pNode, FbxTime pTime = FBXSDK_TIME_ return FbxVector4(1, 1, 1, 1); } -/** - * Compute the local position incorporating the rotation pivot offset, and subtracting out the pivot - * of the parent node. - */ -static FbxVector4 computeLocalTranslation(FbxNode* pNode, FbxTime pTime = FBXSDK_TIME_INFINITE) { - const FbxVector4 meshRotationPivot = pNode->GetRotationPivot(FbxNode::eSourcePivot); - const FbxAMatrix meshPivotTransform(meshRotationPivot, FbxVector4(0, 0, 0, 0), FbxVector4(1, 1, 1, 1)); - FbxAMatrix localTransform = pNode->EvaluateLocalTransform(pTime); - localTransform *= meshPivotTransform; - FbxVector4 lTranslation = localTransform.GetT(); - FbxNode* parent = pNode->GetParent(); - if (pNode->GetParent() != nullptr) { - lTranslation -= parent->GetRotationPivot(FbxNode::eSourcePivot); - } - return lTranslation; -} - static void ReadNodeHierarchy( RawModel& raw, FbxScene* pScene, @@ -745,9 +725,9 @@ static void ReadNodeHierarchy( // Set the initial node transform. const FbxAMatrix localTransform = pNode->EvaluateLocalTransform(); - FbxVector4 localTranslation = computeLocalTranslation(pNode); - FbxQuaternion localRotation = localTransform.GetQ(); - FbxVector4 localScaling = computeLocalScale(pNode); + const FbxVector4 localTranslation = localTransform.GetT(); + const FbxQuaternion localRotation = localTransform.GetQ(); + const FbxVector4 localScaling = computeLocalScale(pNode); node.translation = toVec3f(localTranslation) * scaleFactor; node.rotation = toQuatf(localRotation); @@ -855,7 +835,7 @@ static void ReadAnimations(RawModel& raw, FbxScene* pScene, const GltfOptions& o for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++) { FbxNode* pNode = pScene->GetNode(nodeIndex); const FbxAMatrix baseTransform = pNode->EvaluateLocalTransform(); - const FbxVector4 baseTranslation = computeLocalTranslation(pNode); + const FbxVector4 baseTranslation = baseTransform.GetT(); const FbxQuaternion baseRotation = baseTransform.GetQ(); const FbxVector4 baseScaling = computeLocalScale(pNode); @@ -867,7 +847,7 @@ static void ReadAnimations(RawModel& raw, FbxScene* pScene, const GltfOptions& o pTime.SetFrame(frameIndex, eMode); const FbxAMatrix localTransform = pNode->EvaluateLocalTransform(pTime); - const FbxVector4 localTranslation = computeLocalTranslation(pNode, pTime); + const FbxVector4 localTranslation = localTransform.GetT(); const FbxQuaternion localRotation = localTransform.GetQ(); const FbxVector4 localScale = computeLocalScale(pNode, pTime); From 69126183b6fa71915e0cb4f369e8fe4716f785eb Mon Sep 17 00:00:00 2001 From: "K. S. Ernest (iFire) Lee" Date: Sat, 12 Nov 2022 16:35:31 -0800 Subject: [PATCH 2/2] Remove very small values. --- src/fbx/Fbx2Raw.cpp | 46 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/src/fbx/Fbx2Raw.cpp b/src/fbx/Fbx2Raw.cpp index 39557a3..5db2faa 100644 --- a/src/fbx/Fbx2Raw.cpp +++ b/src/fbx/Fbx2Raw.cpp @@ -762,6 +762,7 @@ static void ReadAnimations(RawModel& raw, FbxScene* pScene, const GltfOptions& o eMode = FbxTime::eFrames60; break; } + const double epsilon = 1e-5f; const int animationCount = pScene->GetSrcObjectCount(); for (size_t animIx = 0; animIx < animationCount; animIx++) { @@ -838,6 +839,10 @@ static void ReadAnimations(RawModel& raw, FbxScene* pScene, const GltfOptions& o const FbxVector4 baseTranslation = baseTransform.GetT(); const FbxQuaternion baseRotation = baseTransform.GetQ(); const FbxVector4 baseScaling = computeLocalScale(pNode); + bool hasTranslation = false; + bool hasRotation = false; + bool hasScale = false; + bool hasMorphs = false; RawChannel channel; channel.nodeIndex = raw.GetNodeById(pNode->GetUniqueID()); @@ -851,6 +856,20 @@ static void ReadAnimations(RawModel& raw, FbxScene* pScene, const GltfOptions& o const FbxQuaternion localRotation = localTransform.GetQ(); const FbxVector4 localScale = computeLocalScale(pNode, pTime); + hasTranslation |= + (fabs(localTranslation[0] - baseTranslation[0]) > epsilon || + fabs(localTranslation[1] - baseTranslation[1]) > epsilon || + fabs(localTranslation[2] - baseTranslation[2]) > epsilon); + hasRotation |= + (fabs(localRotation[0] - baseRotation[0]) > epsilon || + fabs(localRotation[1] - baseRotation[1]) > epsilon || + fabs(localRotation[2] - baseRotation[2]) > epsilon || + fabs(localRotation[3] - baseRotation[3]) > epsilon); + hasScale |= + (fabs(localScale[0] - baseScaling[0]) > epsilon || + fabs(localScale[1] - baseScaling[1]) > epsilon || + fabs(localScale[2] - baseScaling[2]) > epsilon); + channel.translations.push_back(toVec3f(localTranslation) * scaleFactor); channel.rotations.push_back(toQuatf(localRotation)); channel.scales.push_back(toVec3f(localScale)); @@ -905,6 +924,7 @@ static void ReadAnimations(RawModel& raw, FbxScene* pScene, const GltfOptions& o if (!std::isnan(result)) { // we're transitioning into targetIx channel.weights.push_back(result); + hasMorphs = true; continue; } if (targetIx != targetCount - 1) { @@ -912,6 +932,7 @@ static void ReadAnimations(RawModel& raw, FbxScene* pScene, const GltfOptions& o if (!std::isnan(result)) { // we're transitioning AWAY from targetIx channel.weights.push_back(1.0f - result); + hasMorphs = true; continue; } } @@ -925,12 +946,27 @@ static void ReadAnimations(RawModel& raw, FbxScene* pScene, const GltfOptions& o } } - animation.channels.emplace_back(channel); + if (hasTranslation || hasRotation || hasScale || hasMorphs) { + if (!hasTranslation) { + channel.translations.clear(); + } + if (!hasRotation) { + channel.rotations.clear(); + } + if (!hasScale) { + channel.scales.clear(); + } + if (!hasMorphs) { + channel.weights.clear(); + } + + animation.channels.emplace_back(channel); - totalSizeInBytes += channel.translations.size() * sizeof(channel.translations[0]) + - channel.rotations.size() * sizeof(channel.rotations[0]) + - channel.scales.size() * sizeof(channel.scales[0]) + - channel.weights.size() * sizeof(channel.weights[0]); + totalSizeInBytes += channel.translations.size() * sizeof(channel.translations[0]) + + channel.rotations.size() * sizeof(channel.rotations[0]) + + channel.scales.size() * sizeof(channel.scales[0]) + + channel.weights.size() * sizeof(channel.weights[0]); + } if (verboseOutput) { fmt::printf(