USD Skinned Mesh (#5812)

USD: Skinned Mesh support
This commit is contained in:
Gene Walters
2024-10-31 21:48:28 +01:00
committed by GitHub
parent ec563823b6
commit 7634cf84ca
7 changed files with 267 additions and 69 deletions
+130 -21
View File
@@ -212,15 +212,14 @@ void USDImporterImplTinyusdz::InternReadFile(
return;
}
// sanityCheckNodesRecursive(pScene->mRootNode);
// sanityCheckNodesRecursive(pScene->mRootNode);
meshes(render_scene, pScene, nameWExt);
materials(render_scene, pScene, nameWExt);
textures(render_scene, pScene, nameWExt);
textureImages(render_scene, pScene, nameWExt);
buffers(render_scene, pScene, nameWExt);
std::map<size_t, tinyusdz::tydra::Node> meshNodes;
setupNodes(render_scene, pScene, meshNodes, nameWExt);
setupNodes(render_scene, pScene, nameWExt);
setupBlendShapes(render_scene, pScene, nameWExt);
}
@@ -269,8 +268,68 @@ void USDImporterImplTinyusdz::verticesForMesh(
size_t meshIdx,
const std::string &nameWExt) {
UNUSED(nameWExt);
pScene->mMeshes[meshIdx]->mNumVertices = static_cast<unsigned int>(render_scene.meshes[meshIdx].points.size());
const auto numVertices = static_cast<unsigned int>(render_scene.meshes[meshIdx].points.size());
pScene->mMeshes[meshIdx]->mNumVertices = numVertices;
pScene->mMeshes[meshIdx]->mVertices = new aiVector3D[pScene->mMeshes[meshIdx]->mNumVertices];
// Check if this is a skinned mesh
if (int skeleton_id = render_scene.meshes[meshIdx].skel_id; skeleton_id > -1) {
// Recursively iterate to collect all the joints in the hierarchy into a flattened array
std::vector<const tinyusdz::tydra::SkelNode *> skeletonNodes;
skeletonNodes.push_back(&render_scene.skeletons[skeleton_id].root_node);
for (int i = 0; i < skeletonNodes.size(); ++i) {
const tinyusdz::tydra::SkelNode *skeletonNode = skeletonNodes[i];
for (const auto &child : skeletonNodes[i]->children) {
skeletonNodes.push_back(&child);
}
}
// Convert USD skeleton joints to Assimp bones
const unsigned int numBones = skeletonNodes.size();
pScene->mMeshes[meshIdx]->mNumBones = numBones;
pScene->mMeshes[meshIdx]->mBones = new aiBone *[numBones];
for (unsigned int i = 0; i < numBones; ++i) {
const tinyusdz::tydra::SkelNode *skeletonNode = skeletonNodes[i];
const int boneIndex = skeletonNode->joint_id;
// Sorted so that Assimp bone ids align with USD joint id
auto outputBone = new aiBone();
outputBone->mName = aiString(skeletonNode->joint_name);
outputBone->mOffsetMatrix = tinyUsdzMat4ToAiMat4(skeletonNode->bind_transform.m).Inverse();
pScene->mMeshes[meshIdx]->mBones[boneIndex] = outputBone;
}
// Vertex weights
std::vector<std::vector<aiVertexWeight>> aiBonesVertexWeights;
aiBonesVertexWeights.resize(numBones);
const std::vector<int> &jointIndices = render_scene.meshes[meshIdx].joint_and_weights.jointIndices;
const std::vector<float> &jointWeightIndices = render_scene.meshes[meshIdx].joint_and_weights.jointWeights;
const int numWeightsPerVertex = render_scene.meshes[meshIdx].joint_and_weights.elementSize;
for (unsigned int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex) {
for (int weightIndex = 0; weightIndex < numWeightsPerVertex; ++weightIndex) {
const unsigned int index = vertexIndex * numWeightsPerVertex + weightIndex;
const float jointWeight = jointWeightIndices[index];
if (jointWeight > 0) {
const int jointIndex = jointIndices[index];
aiBonesVertexWeights[jointIndex].emplace_back(vertexIndex, jointWeight);
}
}
}
for (int boneIndex = 0; boneIndex < numBones; ++boneIndex) {
const unsigned int numWeightsForBone = aiBonesVertexWeights[boneIndex].size();
pScene->mMeshes[meshIdx]->mBones[boneIndex]->mWeights = new aiVertexWeight[numWeightsForBone];
pScene->mMeshes[meshIdx]->mBones[boneIndex]->mNumWeights = numWeightsForBone;
std::swap_ranges(aiBonesVertexWeights[boneIndex].begin(), aiBonesVertexWeights[boneIndex].end(), pScene->mMeshes[meshIdx]->mBones[boneIndex]->mWeights);
}
} // Skinned mesh end
for (size_t j = 0; j < pScene->mMeshes[meshIdx]->mNumVertices; ++j) {
pScene->mMeshes[meshIdx]->mVertices[j].x = render_scene.meshes[meshIdx].points[j][0];
pScene->mMeshes[meshIdx]->mVertices[j].y = render_scene.meshes[meshIdx].points[j][1];
@@ -620,29 +679,29 @@ void USDImporterImplTinyusdz::buffers(
void USDImporterImplTinyusdz::setupNodes(
const tinyusdz::tydra::RenderScene &render_scene,
aiScene *pScene,
std::map<size_t, tinyusdz::tydra::Node> &meshNodes,
const std::string &nameWExt) {
stringstream ss;
pScene->mRootNode = nodes(render_scene, meshNodes, nameWExt);
pScene->mRootNode = nodes(render_scene, nameWExt);
if (pScene->mRootNode == nullptr) {
return;
}
pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
pScene->mRootNode->mMeshes = new unsigned int[pScene->mRootNode->mNumMeshes];
ss.str("");
ss << "setupNodes(): pScene->mNumMeshes: " << pScene->mNumMeshes;
if (pScene->mRootNode != nullptr) {
ss << ", mRootNode->mNumMeshes: " << pScene->mRootNode->mNumMeshes;
}
ss << ", mRootNode->mNumMeshes: " << pScene->mRootNode->mNumMeshes;
TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
for (unsigned int meshIdx = 0; meshIdx < pScene->mNumMeshes; meshIdx++) {
pScene->mRootNode->mMeshes[meshIdx] = meshIdx;
}
}
aiNode *USDImporterImplTinyusdz::nodes(
const tinyusdz::tydra::RenderScene &render_scene,
std::map<size_t, tinyusdz::tydra::Node> &meshNodes,
const std::string &nameWExt) {
const size_t numNodes{render_scene.nodes.size()};
(void) numNodes; // Ignore unused variable when -Werror enabled
@@ -650,7 +709,9 @@ aiNode *USDImporterImplTinyusdz::nodes(
ss.str("");
ss << "nodes(): model" << nameWExt << ", numNodes: " << numNodes;
TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
return nodesRecursive(nullptr, render_scene.nodes[0], meshNodes);
aiNode *rootNode = nodesRecursive(nullptr, render_scene.nodes[0], render_scene.skeletons);
return rootNode;
}
using Assimp::tinyusdzNodeTypeFor;
@@ -659,7 +720,7 @@ using tinyusdz::tydra::NodeType;
aiNode *USDImporterImplTinyusdz::nodesRecursive(
aiNode *pNodeParent,
const tinyusdz::tydra::Node &node,
std::map<size_t, tinyusdz::tydra::Node> &meshNodes) {
const std::vector<tinyusdz::tydra::SkelHierarchy> &skeletons) {
stringstream ss;
aiNode *cNode = new aiNode();
cNode->mParent = pNodeParent;
@@ -673,21 +734,69 @@ aiNode *USDImporterImplTinyusdz::nodesRecursive(
ss << " (parent " << cNode->mParent->mName.C_Str() << ")";
}
ss << " has " << node.children.size() << " children";
if (node.id > -1) {
if (node.id != -1) {
ss << "\n node mesh id: " << node.id << " (node type: " << tinyusdzNodeTypeFor(node.nodeType) << ")";
meshNodes[node.id] = node;
}
TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
if (!node.children.empty()) {
cNode->mNumChildren = static_cast<unsigned int>(node.children.size());
cNode->mChildren = new aiNode *[cNode->mNumChildren];
unsigned int numChildren = node.children.size();
// Find any tinyusdz skeletons which might begin at this node
// Add the skeleton bones as child nodes
const tinyusdz::tydra::SkelNode *skelNode = nullptr;
for (const auto &skeleton : skeletons) {
if (skeleton.abs_path == node.abs_path) {
// Add this skeleton's bones as child nodes
++numChildren;
skelNode = &skeleton.root_node;
break;
}
}
size_t i{0};
for (const auto &childNode: node.children) {
cNode->mChildren[i] = nodesRecursive(cNode, childNode, meshNodes);
cNode->mNumChildren = numChildren;
// Done. No more children.
if (numChildren == 0) {
return cNode;
}
cNode->mChildren = new aiNode *[cNode->mNumChildren];
size_t i{ 0 };
for (const auto &childNode : node.children) {
cNode->mChildren[i] = nodesRecursive(cNode, childNode, skeletons);
++i;
}
if (skelNode != nullptr) {
// Convert USD skeleton into an Assimp node and make it the last child
cNode->mChildren[cNode->mNumChildren-1] = skeletonNodesRecursive(cNode, *skelNode);
}
return cNode;
}
aiNode *USDImporterImplTinyusdz::skeletonNodesRecursive(
aiNode* pNodeParent,
const tinyusdz::tydra::SkelNode& joint) {
auto *cNode = new aiNode(joint.joint_path);
cNode->mParent = pNodeParent;
cNode->mNumMeshes = 0; // not a mesh node
cNode->mTransformation = tinyUsdzMat4ToAiMat4(joint.rest_transform.m);
// Done. No more children.
if (joint.children.empty()) {
return cNode;
}
cNode->mNumChildren = static_cast<unsigned int>(joint.children.size());
cNode->mChildren = new aiNode *[cNode->mNumChildren];
for (int i = 0; i < cNode->mNumChildren; ++i) {
const tinyusdz::tydra::SkelNode &childJoint = joint.children[i];
cNode->mChildren[i] = skeletonNodesRecursive(cNode, childJoint);
}
return cNode;
}
+6 -5
View File
@@ -123,19 +123,20 @@ public:
void setupNodes(
const tinyusdz::tydra::RenderScene &render_scene,
aiScene *pScene,
std::map<size_t, tinyusdz::tydra::Node> &meshNodes,
const std::string &nameWExt
);
const std::string &nameWExt);
aiNode *nodes(
const tinyusdz::tydra::RenderScene &render_scene,
std::map<size_t, tinyusdz::tydra::Node> &meshNodes,
const std::string &nameWExt);
aiNode *nodesRecursive(
aiNode *pNodeParent,
const tinyusdz::tydra::Node &node,
std::map<size_t, tinyusdz::tydra::Node> &meshNodes);
const std::vector<tinyusdz::tydra::SkelHierarchy> &skeletons);
aiNode *skeletonNodesRecursive(
aiNode *pNodeParent,
const tinyusdz::tydra::SkelNode &joint);
void sanityCheckNodesRecursive(
aiNode *pNode);
@@ -103,37 +103,21 @@ std::string Assimp::tinyusdzNodeTypeFor(NodeType type) {
aiMatrix4x4 Assimp::tinyUsdzMat4ToAiMat4(const double matIn[4][4]) {
aiMatrix4x4 matOut;
matOut.a1 = matIn[0][0];
matOut.a2 = matIn[0][1];
matOut.a3 = matIn[0][2];
matOut.a4 = matIn[0][3];
matOut.b1 = matIn[1][0];
matOut.a2 = matIn[1][0];
matOut.a3 = matIn[2][0];
matOut.a4 = matIn[3][0];
matOut.b1 = matIn[0][1];
matOut.b2 = matIn[1][1];
matOut.b3 = matIn[1][2];
matOut.b4 = matIn[1][3];
matOut.c1 = matIn[2][0];
matOut.c2 = matIn[2][1];
matOut.b3 = matIn[2][1];
matOut.b4 = matIn[3][1];
matOut.c1 = matIn[0][2];
matOut.c2 = matIn[1][2];
matOut.c3 = matIn[2][2];
matOut.c4 = matIn[2][3];
matOut.d1 = matIn[3][0];
matOut.d2 = matIn[3][1];
matOut.d3 = matIn[3][2];
matOut.c4 = matIn[3][2];
matOut.d1 = matIn[0][3];
matOut.d2 = matIn[1][3];
matOut.d3 = matIn[2][3];
matOut.d4 = matIn[3][3];
// matOut.a1 = matIn[0][0];
// matOut.a2 = matIn[1][0];
// matOut.a3 = matIn[2][0];
// matOut.a4 = matIn[3][0];
// matOut.b1 = matIn[0][1];
// matOut.b2 = matIn[1][1];
// matOut.b3 = matIn[2][1];
// matOut.b4 = matIn[3][1];
// matOut.c1 = matIn[0][2];
// matOut.c2 = matIn[1][2];
// matOut.c3 = matIn[2][2];
// matOut.c4 = matIn[3][2];
// matOut.d1 = matIn[0][3];
// matOut.d2 = matIn[1][3];
// matOut.d3 = matIn[2][3];
// matOut.d4 = matIn[3][3];
return matOut;
}