Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GeometricTransform (fix #56) #58

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions src/fbx/Fbx2Raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
hu-xd marked this conversation as resolved.
Show resolved Hide resolved
const FbxMatrix transform = dummyTransform;
const FbxMatrix normalTransform = dummyTransform;
const FbxMatrix inverseTransposeTransform = normalTransform.Inverse().Transpose();

raw.AddVertexAttribute(RAW_VERTEX_ATTRIBUTE_POSITION);
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 15 additions & 3 deletions src/gltf/Raw2Gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
hu-xd marked this conversation as resolved.
Show resolved Hide resolved
auto nodeData = nodesById[node.id];

//
// Assign mesh to node
Expand All @@ -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)
hu-xd marked this conversation as resolved.
Show resolved Hide resolved
);
meshNodeData->SetMesh(meshData.ix);
nodeData->AddChildNode(meshNodeIx);
} else {
nodeData->SetMesh(meshData.ix);
}


//
// surface skin
Expand Down Expand Up @@ -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];;
fire marked this conversation as resolved.
Show resolved Hide resolved

if (node.lightIx >= 0) {
// we lean on the fact that in this simple case, raw and gltf indexing are aligned
Expand Down
7 changes: 7 additions & 0 deletions src/raw/RawModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,16 @@ struct RawNode {
std::string name;
long parentId;
std::vector<long> childIds;

Vec3f translation;
Quatf rotation;
Vec3f scale;

bool hasGeometricTransform;
Vec3f geometricTranslation;
Quatf geometricRotation;
Vec3f geometricScaling;

long surfaceId;
long lightIx;
std::vector<std::string> userProperties;
Expand Down