From 00d66ac5d2996a1c61cd14d4099fa9938727c604 Mon Sep 17 00:00:00 2001 From: hu-xd <74464946+hu-xd@users.noreply.github.com> Date: Mon, 29 Apr 2024 20:20:39 +0800 Subject: [PATCH 1/4] Fix GeometricTransform (#1) * add Mesh to a child node to hold GeometricTransform * Fix * Fix typo * Fix type * Fix Typo * Fix * Fix * Fix * Fix * Optimize * Cleanup --- src/fbx/Fbx2Raw.cpp | 45 ++++++++++++++++++++++++++++--------------- src/gltf/Raw2Gltf.cpp | 18 ++++++++++++++--- src/raw/RawModel.hpp | 7 +++++++ 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/src/fbx/Fbx2Raw.cpp b/src/fbx/Fbx2Raw.cpp index 63d7231..4e46fa4 100644 --- a/src/fbx/Fbx2Raw.cpp +++ b/src/fbx/Fbx2Raw.cpp @@ -173,21 +173,27 @@ static void ReadMesh( : "NO"); } - // The FbxNode geometric transformation describes how a FbxNodeAttribute is offset from - // the FbxNode's local frame of reference. These geometric transforms are applied to the - // FbxNodeAttribute after the FbxNode's local transforms are computed, and are not - // inherited across the node hierarchy. - // Apply the geometric transform to the mesh geometry (vertices, normal etc.) because - // glTF does not have an equivalent to the geometric transform. - const FbxVector4 meshTranslation = pNode->GetGeometricTranslation(FbxNode::eSourcePivot); - const FbxVector4 meshRotation = pNode->GetGeometricRotation(FbxNode::eSourcePivot); - const FbxVector4 meshScaling = pNode->GetGeometricScaling(FbxNode::eSourcePivot); - const FbxAMatrix meshTransform(meshTranslation, meshRotation, meshScaling); - const FbxMatrix transform = meshTransform; - - // Remove translation & scaling from transforms that will bi applied to normals, tangents & - // binormals - const FbxMatrix normalTransform(FbxVector4(), meshRotation, meshScaling); + // // The FbxNode geometric transformation describes how a FbxNodeAttribute is offset from + // // the FbxNode's local frame of reference. These geometric transforms are applied to the + // // FbxNodeAttribute after the FbxNode's local transforms are computed, and are not + // // inherited across the node hierarchy. + // // Apply the geometric transform to the mesh geometry (vertices, normal etc.) because + // // glTF does not have an equivalent to the geometric transform. + // const FbxVector4 meshTranslation = pNode->GetGeometricTranslation(FbxNode::eSourcePivot); + // const FbxVector4 meshRotation = pNode->GetGeometricRotation(FbxNode::eSourcePivot); + // const FbxVector4 meshScaling = pNode->GetGeometricScaling(FbxNode::eSourcePivot); + // const FbxAMatrix meshTransform(meshTranslation, meshRotation, meshScaling); + + // // Remove translation & scaling from transforms that will bi applied to normals, tangents & + // // binormals + //const FbxMatrix normalTransform(FbxVector4(), meshRotation, meshScaling); + //const FbxMatrix inverseTransposeTransform = normalTransform.Inverse().Transpose(); + + // TEMP HACK + FbxAMatrix dummyTransform; + dummyTransform.SetIdentity(); + const FbxMatrix transform = dummyTransform; + const FbxMatrix normalTransform = dummyTransform; const FbxMatrix inverseTransposeTransform = normalTransform.Inverse().Transpose(); raw.AddVertexAttribute(RAW_VERTEX_ATTRIBUTE_POSITION); @@ -742,6 +748,15 @@ static void ReadNodeHierarchy( node.rotation = toQuatf(localRotation); node.scale = toVec3f(localScaling); + const FbxVector4 geometricTranslation = pNode->GetGeometricTranslation(FbxNode::eSourcePivot); + const FbxVector4 geometricRotation = pNode->GetGeometricRotation(FbxNode::eSourcePivot); + const FbxVector4 geometricScaling = pNode->GetGeometricScaling(FbxNode::eSourcePivot); + FbxAMatrix geometricTransform(geometricTranslation, geometricRotation, geometricScaling); + node.hasGeometricTransform = !geometricTransform.IsIdentity(); + node.geometricTranslation = toVec3f(geometricTransform.GetT()) * scaleFactor; + node.geometricRotation = toQuatf(geometricTransform.GetQ()); + node.geometricScaling = toVec3f(geometricTransform.GetS()); + if (parentId) { RawNode& parentNode = raw.GetNode(raw.GetNodeById(parentId)); // Add unique child name to the parent node. diff --git a/src/gltf/Raw2Gltf.cpp b/src/gltf/Raw2Gltf.cpp index 8aba780..9324a3e 100644 --- a/src/gltf/Raw2Gltf.cpp +++ b/src/gltf/Raw2Gltf.cpp @@ -822,7 +822,8 @@ ModelData* Raw2Gltf( for (int i = 0; i < raw.GetNodeCount(); i++) { const RawNode& node = raw.GetNode(i); - auto nodeData = gltf->nodes.ptrs[i]; + //auto nodeData = gltf->nodes.ptrs[i]; + auto nodeData = nodesById[node.id]; // // Assign mesh to node @@ -832,7 +833,18 @@ ModelData* Raw2Gltf( const RawSurface& rawSurface = raw.GetSurface(surfaceIndex); MeshData& meshData = require(meshBySurfaceId, rawSurface.id); - nodeData->SetMesh(meshData.ix); + + if (node.hasGeometricTransform) { + const auto meshNodeIx = gltf->nodes.ptrs.size(); + auto meshNodeData = gltf->nodes.hold( + new NodeData(node.name + "-[Mesh]", node.geometricTranslation, node.geometricRotation, node.geometricScaling, false) + ); + meshNodeData->SetMesh(meshData.ix); + nodeData->AddChildNode(meshNodeIx); + } else { + nodeData->SetMesh(meshData.ix); + } + // // surface skin @@ -937,7 +949,7 @@ ModelData* Raw2Gltf( } for (int i = 0; i < raw.GetNodeCount(); i++) { const RawNode& node = raw.GetNode(i); - const auto nodeData = gltf->nodes.ptrs[i]; + const auto nodeData = nodesById[node.id];; if (node.lightIx >= 0) { // we lean on the fact that in this simple case, raw and gltf indexing are aligned diff --git a/src/raw/RawModel.hpp b/src/raw/RawModel.hpp index 5f25231..1e66548 100644 --- a/src/raw/RawModel.hpp +++ b/src/raw/RawModel.hpp @@ -349,9 +349,16 @@ struct RawNode { std::string name; long parentId; std::vector childIds; + Vec3f translation; Quatf rotation; Vec3f scale; + + bool hasGeometricTransform; + Vec3f geometricTranslation; + Quatf geometricRotation; + Vec3f geometricScaling; + long surfaceId; long lightIx; std::vector userProperties; From 23be9316ded315ab21660d4508491ae0195b9498 Mon Sep 17 00:00:00 2001 From: hu-xd <74464946+hu-xd@users.noreply.github.com> Date: Tue, 30 Apr 2024 01:41:52 +0800 Subject: [PATCH 2/4] Update src/fbx/Fbx2Raw.cpp Co-authored-by: K. S. Ernest (iFire) Lee --- src/fbx/Fbx2Raw.cpp | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/fbx/Fbx2Raw.cpp b/src/fbx/Fbx2Raw.cpp index 4e46fa4..572b9a3 100644 --- a/src/fbx/Fbx2Raw.cpp +++ b/src/fbx/Fbx2Raw.cpp @@ -173,22 +173,6 @@ static void ReadMesh( : "NO"); } - // // The FbxNode geometric transformation describes how a FbxNodeAttribute is offset from - // // the FbxNode's local frame of reference. These geometric transforms are applied to the - // // FbxNodeAttribute after the FbxNode's local transforms are computed, and are not - // // inherited across the node hierarchy. - // // Apply the geometric transform to the mesh geometry (vertices, normal etc.) because - // // glTF does not have an equivalent to the geometric transform. - // const FbxVector4 meshTranslation = pNode->GetGeometricTranslation(FbxNode::eSourcePivot); - // const FbxVector4 meshRotation = pNode->GetGeometricRotation(FbxNode::eSourcePivot); - // const FbxVector4 meshScaling = pNode->GetGeometricScaling(FbxNode::eSourcePivot); - // const FbxAMatrix meshTransform(meshTranslation, meshRotation, meshScaling); - - // // Remove translation & scaling from transforms that will bi applied to normals, tangents & - // // binormals - //const FbxMatrix normalTransform(FbxVector4(), meshRotation, meshScaling); - //const FbxMatrix inverseTransposeTransform = normalTransform.Inverse().Transpose(); - // TEMP HACK FbxAMatrix dummyTransform; dummyTransform.SetIdentity(); From af6548f045e9a98d6a6c6d185ca92cbbd31bdec2 Mon Sep 17 00:00:00 2001 From: hu-xd <74464946+hu-xd@users.noreply.github.com> Date: Tue, 30 Apr 2024 01:42:04 +0800 Subject: [PATCH 3/4] Update src/gltf/Raw2Gltf.cpp Co-authored-by: K. S. Ernest (iFire) Lee --- src/gltf/Raw2Gltf.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/gltf/Raw2Gltf.cpp b/src/gltf/Raw2Gltf.cpp index 9324a3e..e955bba 100644 --- a/src/gltf/Raw2Gltf.cpp +++ b/src/gltf/Raw2Gltf.cpp @@ -822,7 +822,6 @@ ModelData* Raw2Gltf( for (int i = 0; i < raw.GetNodeCount(); i++) { const RawNode& node = raw.GetNode(i); - //auto nodeData = gltf->nodes.ptrs[i]; auto nodeData = nodesById[node.id]; // From b124cb6bc0a5bf292316dc64b47139dbbbbf3dd0 Mon Sep 17 00:00:00 2001 From: hu-xd <74464946+hu-xd@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:39:49 +0800 Subject: [PATCH 4/4] Update Raw2Gltf.cpp --- src/gltf/Raw2Gltf.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gltf/Raw2Gltf.cpp b/src/gltf/Raw2Gltf.cpp index e955bba..c35d837 100644 --- a/src/gltf/Raw2Gltf.cpp +++ b/src/gltf/Raw2Gltf.cpp @@ -836,7 +836,7 @@ ModelData* Raw2Gltf( if (node.hasGeometricTransform) { const auto meshNodeIx = gltf->nodes.ptrs.size(); auto meshNodeData = gltf->nodes.hold( - new NodeData(node.name + "-[Mesh]", node.geometricTranslation, node.geometricRotation, node.geometricScaling, false) + new NodeData(node.name + "GeometricHelper", node.geometricTranslation, node.geometricRotation, node.geometricScaling, false) ); meshNodeData->SetMesh(meshData.ix); nodeData->AddChildNode(meshNodeIx);