Skip to content

Commit

Permalink
Support Skeleton attached to Mesh.
Browse files Browse the repository at this point in the history
  • Loading branch information
syoyo committed Apr 20, 2024
1 parent 616302d commit 30481ec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
26 changes: 23 additions & 3 deletions src/tydra/render-data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3562,6 +3562,7 @@ bool RenderSceneConverter::ConvertMesh(
dst.joint_and_weights.elementSize = int(jointIndicesElementSize);

if (mesh.skeleton.has_value()) {
DCOUT("Convert Skeleton");
Path skelPath;

if (mesh.skeleton.value().is_path()) {
Expand All @@ -3578,8 +3579,25 @@ bool RenderSceneConverter::ConvertMesh(
}

if (skelPath.is_valid()) {
// TODO
// GetSkeletonImpl
SkelHierarchy skel;
if (!ConvertSkeletonImpl(env, abs_path.full_path_name(), mesh, &skel)) {
return false;
}
DCOUT("Converted skeleton attached to : " << abs_path);

auto it = std::find_if(skeletons.begin(), skeletons.end(), [&abs_path](const SkelHierarchy &sk) {
return sk.abs_path == abs_path.full_path_name();
});

int skel_id{0};
if (it != skeletons.end()) {
skel_id = int(std::distance(skeletons.begin(), it));
} else {
skel_id = int(skeletons.size());
skeletons.emplace_back(std::move(skel));
}

dst.skel_id = skel_id;
}
}

Expand Down Expand Up @@ -5527,7 +5545,7 @@ bool RenderSceneConverter::ConvertToRenderScene(
}

bool RenderSceneConverter::ConvertSkeletonImpl(const RenderSceneConverterEnv &env, const std::string &abs_path, const tinyusdz::GeomMesh &mesh,
SkelHierarchy *&out_skel) {
SkelHierarchy *out_skel) {

if (!out_skel) {
return false;
Expand Down Expand Up @@ -6278,6 +6296,8 @@ std::string DumpMesh(const RenderMesh &mesh, uint32_t indent) {
ss << pprint::Indent(indent + 1) << "}\n";
}

ss << pprint::Indent(indent + 1) << "skek_id " << mesh.skel_id << "\n";

if (mesh.joint_and_weights.jointIndices.size()) {
ss << pprint::Indent(indent + 1) << "skin {\n";
ss << pprint::Indent(indent + 2) << "geomBindTransform "
Expand Down
3 changes: 2 additions & 1 deletion src/tydra/render-data.hh
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ struct RenderMesh {

// For vertex skinning
JointAndWeight joint_and_weights;
int skel_id{-1}; // index to RenderScene::skeletons

// BlendShapes
// key = USD BlendShape prim name.
Expand Down Expand Up @@ -1777,7 +1778,7 @@ class RenderSceneConverter {
// Get Skeleton assigned to the GeomMesh Prim and convert it to SkelHierarchy.
//
bool ConvertSkeletonImpl(const RenderSceneConverterEnv &env, const std::string &mesh_abs_path, const tinyusdz::GeomMesh &mesh,
SkelHierarchy *&out_skel);
SkelHierarchy *out_skel);

bool BuildNodeHierarchyImpl(
const RenderSceneConverterEnv &env,
Expand Down
33 changes: 19 additions & 14 deletions src/tydra/scene-access.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2788,11 +2788,6 @@ bool BuildSkelHierarchy(const Skeleton &skel, SkelNode &dst, std::string *err) {
"Skeleton.joints attrbitue is not authored: {}", skel.name));
}

if (!skel.jointNames.authored()) {
PUSH_ERROR_AND_RETURN(fmt::format(
"Skeleton.jointNames attrbitue is not authored: {}", skel.name));
}

std::vector<value::token> joints;
if (!skel.joints.get_value(&joints)) {
PUSH_ERROR_AND_RETURN(
Expand All @@ -2805,18 +2800,28 @@ bool BuildSkelHierarchy(const Skeleton &skel, SkelNode &dst, std::string *err) {
}

std::vector<value::token> jointNames;
if (!skel.jointNames.get_value(&jointNames)) {
PUSH_ERROR_AND_RETURN(fmt::format(
"Failed to get Skeleton.jointNames attrbitue: {}", skel.name));
}

if (joints.size() != jointNames.size()) {
PUSH_ERROR_AND_RETURN(
fmt::format("Skeleton.joints.size {} must be equal to "
"Skeleton.jointNames.size {}: {}",
joints.size(), jointNames.size(), skel.name));
if (skel.jointNames.authored()) {
if (!skel.jointNames.get_value(&jointNames)) {
PUSH_ERROR_AND_RETURN(fmt::format(
"Failed to get Skeleton.jointNames attrbitue: {}", skel.name));
}

if (joints.size() != jointNames.size()) {
PUSH_ERROR_AND_RETURN(
fmt::format("Skeleton.joints.size {} must be equal to "
"Skeleton.jointNames.size {}: {}",
joints.size(), jointNames.size(), skel.name));
}
} else {
// Use joints
jointNames.resize(joints.size());
for (size_t i = 0; i < joints.size(); i++) {
jointNames[i] = joints[i];
}
}


std::vector<value::matrix4d> restTransforms;
if (skel.restTransforms.authored()) {
if (!skel.restTransforms.get_value(&restTransforms)) {
Expand Down

0 comments on commit 30481ec

Please sign in to comment.