Skip to content

Commit

Permalink
Merge pull request #247 from Laupetin/fix/gltf-model-import-1
Browse files Browse the repository at this point in the history
feat: support gltf bone rotation as matrix notation
  • Loading branch information
Laupetin authored Sep 6, 2024
2 parents 149a24b + 7eb1714 commit f4aed31
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/ObjCommon/XModel/Gltf/JsonGltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ namespace gltf
std::optional<std::array<float, 3>> translation;
std::optional<std::array<float, 4>> rotation;
std::optional<std::array<float, 3>> scale;
std::optional<std::array<float, 16>> matrix;
std::optional<std::vector<unsigned>> children;
std::optional<unsigned> skin;
std::optional<unsigned> mesh;
};

NLOHMANN_DEFINE_TYPE_EXTENSION(JsonNode, name, translation, rotation, scale, children, skin, mesh);
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonNode, name, translation, rotation, scale, matrix, children, skin, mesh);

class JsonBuffer
{
Expand Down
115 changes: 77 additions & 38 deletions src/ObjLoading/XModel/Gltf/GltfLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,77 @@ namespace
return std::nullopt;
}

static void ApplyNodeMatrixTRS(XModelBone& bone, const JsonNode& node)
{
const auto matrix = Eigen::Matrix4f({
{(*node.matrix)[0], (*node.matrix)[4], (*node.matrix)[8], (*node.matrix)[12]},
{(*node.matrix)[1], (*node.matrix)[5], (*node.matrix)[9], (*node.matrix)[13]},
{(*node.matrix)[2], (*node.matrix)[6], (*node.matrix)[10], (*node.matrix)[14]},
{(*node.matrix)[3], (*node.matrix)[7], (*node.matrix)[11], (*node.matrix)[15]}
});
Eigen::Affine3f transform(matrix);

const auto translation = transform.translation();
bone.localOffset[0] = translation.x();
bone.localOffset[1] = -translation.z();
bone.localOffset[2] = translation.y();

const auto rotation = transform.rotation();
const auto rotationQuat = Eigen::Quaternionf(rotation);
bone.localRotation.x = rotationQuat.x();
bone.localRotation.y = -rotationQuat.z();
bone.localRotation.z = rotationQuat.y();
bone.localRotation.w = rotationQuat.w();

bone.scale[0] = matrix.block<3, 1>(0, 0).norm();
bone.scale[1] = matrix.block<3, 1>(0, 1).norm();
bone.scale[2] = matrix.block<3, 1>(0, 2).norm();
}

static void ApplyNodeSeparateTRS(XModelBone& bone, const JsonNode& node)
{
if (node.translation)
{
bone.localOffset[0] = (*node.translation)[0];
bone.localOffset[1] = -(*node.translation)[2];
bone.localOffset[2] = (*node.translation)[1];
}
else
{
bone.localOffset[0] = 0.0f;
bone.localOffset[1] = 0.0f;
bone.localOffset[2] = 0.0f;
}

if (node.rotation)
{
bone.localRotation.x = (*node.rotation)[0];
bone.localRotation.y = -(*node.rotation)[2];
bone.localRotation.z = (*node.rotation)[1];
bone.localRotation.w = (*node.rotation)[3];
}
else
{
bone.localRotation.x = 0.0f;
bone.localRotation.y = 0.0f;
bone.localRotation.z = 0.0f;
bone.localRotation.w = 1.0f;
}

if (node.scale)
{
bone.scale[0] = (*node.scale)[0];
bone.scale[1] = (*node.scale)[1];
bone.scale[2] = (*node.scale)[2];
}
else
{
bone.scale[0] = 1.0f;
bone.scale[1] = 1.0f;
bone.scale[2] = 1.0f;
}
}

static bool ConvertJoint(const JsonRoot& jRoot,
const JsonSkin& skin,
XModelCommon& common,
Expand All @@ -435,51 +506,19 @@ namespace
bone.name = node.name.value_or(std::string());
bone.parentIndex = parentIndex;

if (node.scale)
{
bone.scale[0] = parentScale[0] * (*node.scale)[0];
bone.scale[1] = parentScale[1] * (*node.scale)[1];
bone.scale[2] = parentScale[2] * (*node.scale)[2];
}
if (node.matrix)
ApplyNodeMatrixTRS(bone, node);
else
{
bone.scale[0] = parentScale[0];
bone.scale[1] = parentScale[1];
bone.scale[2] = parentScale[2];
}
ApplyNodeSeparateTRS(bone, node);

if (node.translation)
{
bone.localOffset[0] = (*node.translation)[0];
bone.localOffset[1] = -(*node.translation)[2];
bone.localOffset[2] = (*node.translation)[1];
}
else
{
bone.localOffset[0] = 0.0f;
bone.localOffset[1] = 0.0f;
bone.localOffset[2] = 0.0f;
}
bone.scale[0] *= parentScale[0];
bone.scale[1] *= parentScale[1];
bone.scale[2] *= parentScale[2];

bone.globalOffset[0] = bone.localOffset[0] + parentOffset[0];
bone.globalOffset[1] = bone.localOffset[1] + parentOffset[1];
bone.globalOffset[2] = bone.localOffset[2] + parentOffset[2];

if (node.rotation)
{
bone.localRotation.x = (*node.rotation)[0];
bone.localRotation.y = -(*node.rotation)[2];
bone.localRotation.z = (*node.rotation)[1];
bone.localRotation.w = (*node.rotation)[3];
}
else
{
bone.localRotation.x = 0.0f;
bone.localRotation.y = 0.0f;
bone.localRotation.z = 0.0f;
bone.localRotation.w = 1.0f;
}

const auto localRotationEigen = Eigen::Quaternionf(bone.localRotation.w, bone.localRotation.x, bone.localRotation.y, bone.localRotation.z);
const auto parentRotationEigen = Eigen::Quaternionf(parentRotation.w, parentRotation.x, parentRotation.y, parentRotation.z);
const auto globalRotationEigen = localRotationEigen * parentRotationEigen;
Expand Down

0 comments on commit f4aed31

Please sign in to comment.