FBX Export: reconstruct full skeleton for any FBX deformers.

pull/1812/head
Tommy 2018-02-22 11:06:29 +01:00
parent 9287adb735
commit 249f1844ae
2 changed files with 126 additions and 80 deletions

View File

@ -1463,21 +1463,28 @@ void FBXExporter::WriteObjects ()
// one sticky point is that the number of vertices may not match,
// because assimp splits vertices by normal, uv, etc.
// first we should mark all the skeleton nodes,
// so that they can be treated as LimbNode in stead of Mesh or Null.
// at the same time we can build up a map of bone nodes.
// first we should mark the skeleton for each mesh.
// the skeleton must include not only the aiBones,
// but also all their parent nodes.
// anything that affects the position of any bone node must be included.
std::vector<std::set<const aiNode*>> skeleton_by_mesh(mScene->mNumMeshes);
// at the same time we can build a list of all the skeleton nodes,
// which will be used later to mark them as type "limbNode".
std::unordered_set<const aiNode*> limbnodes;
// and a map of nodes by bone name, as finding them is annoying.
std::map<std::string,aiNode*> node_by_bone;
for (size_t mi = 0; mi < mScene->mNumMeshes; ++mi) {
const aiMesh* m = mScene->mMeshes[mi];
std::set<const aiNode*> skeleton;
for (size_t bi =0; bi < m->mNumBones; ++bi) {
const aiBone* b = m->mBones[bi];
const std::string name(b->mName.C_Str());
if (node_by_bone.count(name) > 0) {
// already processed, skip
continue;
}
aiNode* n = mScene->mRootNode->FindNode(b->mName);
auto elem = node_by_bone.find(name);
aiNode* n;
if (elem != node_by_bone.end()) {
n = elem->second;
} else {
n = mScene->mRootNode->FindNode(b->mName);
if (!n) {
// this should never happen
std::stringstream err;
@ -1486,23 +1493,37 @@ void FBXExporter::WriteObjects ()
}
node_by_bone[name] = n;
limbnodes.insert(n);
if (n == mScene->mRootNode) { continue; }
}
skeleton.insert(n);
// mark all parent nodes as skeleton as well,
// up until we find the root node,
// or else the node containing the mesh,
// or else the parent of a node containig the mesh.
for (
const aiNode* parent = n->mParent;
parent != mScene->mRootNode;
parent && parent != mScene->mRootNode;
parent = parent->mParent
) {
// if we've already done this node we can skip it all
if (skeleton.count(parent)) {
break;
}
// ignore fbx transform nodes as these will be collapsed later
// TODO: cache this by aiNode*
const std::string node_name(parent->mName.C_Str());
if (node_name.find(MAGIC_NODE_TAG) != std::string::npos) {
continue;
}
// otherwise check if this is the root of the skeleton
bool end = false;
// is the mesh part of this node?
for (size_t i = 0; i < parent->mNumMeshes; ++i) {
if (parent->mMeshes[i] == mi) {
end = true;
break;
}
}
// is the mesh in one of the children of this node?
for (size_t j = 0; j < parent->mNumChildren; ++j) {
aiNode* child = parent->mChildren[j];
for (size_t i = 0; i < child->mNumMeshes; ++i) {
@ -1513,27 +1534,23 @@ void FBXExporter::WriteObjects ()
}
if (end) { break; }
}
if (end) { break; }
limbnodes.insert(parent);
skeleton.insert(parent);
// if it was the skeleton root we can finish here
if (end) { break; }
}
}
skeleton_by_mesh[mi] = skeleton;
}
// we'll need the uids for the bone nodes, so generate them now
std::map<std::string,int64_t> bone_uids;
for (auto &bone : limbnodes) {
std::string bone_name(bone->mName.C_Str());
aiNode* bone_node = mScene->mRootNode->FindNode(bone->mName);
if (!bone_node) {
throw DeadlyExportError("Couldn't find node for bone" + bone_name);
}
auto elem = node_uids.find(bone_node);
for (size_t i = 0; i < mScene->mNumMeshes; ++i) {
auto &s = skeleton_by_mesh[i];
for (const aiNode* n : s) {
auto elem = node_uids.find(n);
if (elem == node_uids.end()) {
int64_t uid = generate_uid();
node_uids[bone_node] = uid;
bone_uids[bone_name] = uid;
} else {
bone_uids[bone_name] = elem->second;
node_uids[n] = generate_uid();
}
}
}
@ -1585,6 +1602,9 @@ void FBXExporter::WriteObjects ()
}
}
// TODO, FIXME: this won't work if anything is not in the bind pose.
// for now if such a situation is detected, we throw an exception.
// first get this mesh's position in world space,
// as we'll need it for each subdeformer.
//
@ -1597,12 +1617,23 @@ void FBXExporter::WriteObjects ()
// but there's no guarantee that the bone is in the bindpose,
// so this would be even less reliable.
aiNode* mesh_node = get_node_for_mesh(mi, mScene->mRootNode);
aiMatrix4x4 mesh_node_xform = get_world_transform(mesh_node, mScene);
aiMatrix4x4 mesh_xform = get_world_transform(mesh_node, mScene);
// now make a subdeformer for each bone
// now make a subdeformer for each bone in the skeleton
const std::set<const aiNode*> &skeleton = skeleton_by_mesh[mi];
for (const aiNode* bone_node : skeleton) {
// if there's a bone for this node, find it
const aiBone* b = nullptr;
for (size_t bi = 0; bi < m->mNumBones; ++bi) {
const aiBone* b = m->mBones[bi];
const std::string name(b->mName.C_Str());
// TODO: this probably should index by something else
const std::string name(m->mBones[bi]->mName.C_Str());
if (node_by_bone[name] == bone_node) {
b = m->mBones[bi];
break;
}
}
// start the subdeformer node
const int64_t subdeformer_uid = generate_uid();
FBX::Node sdnode("Deformer");
sdnode.AddProperties(
@ -1611,7 +1642,8 @@ void FBXExporter::WriteObjects ()
sdnode.AddChild("Version", int32_t(100));
sdnode.AddChild("UserData", "", "");
// get indices and weights
// add indices and weights, if any
if (b) {
std::vector<int32_t> subdef_indices;
std::vector<double> subdef_weights;
int32_t last_index = -1;
@ -1634,20 +1666,33 @@ void FBXExporter::WriteObjects ()
// yes, "indexes"
sdnode.AddChild("Indexes", subdef_indices);
sdnode.AddChild("Weights", subdef_weights);
// transform is the transform of the mesh, but in bone space...
// which is exactly what assimp's mOffsetMatrix is,
// no matter what the assimp docs may say.
aiMatrix4x4 tr = b->mOffsetMatrix;
}
// transform is the transform of the mesh, but in bone space.
// To get it we take the inverse of the world-space bone transform,
// and multiply by the world-space transform of the mesh.
aiMatrix4x4 bone_xform = get_world_transform(bone_node, mScene);
aiMatrix4x4 inverse_bone_xform = bone_xform;
inverse_bone_xform.Inverse();
aiMatrix4x4 tr = inverse_bone_xform * mesh_xform;
sdnode.AddChild("Transform", tr);
// this should match assimp's mOffsetMatrix.
// if it doesn't then we have a problem.
// as assimp doesn't store a mOffsetMatrix for bones with 0 weight
// we have no way of reconstructing that information.
const float epsilon = 1e-5; // some error is to be expected
if (b && ! tr.Equal(b->mOffsetMatrix, epsilon)) {
std::stringstream err;
err << "transform matrix for bone \"" << b->mName.C_Str();
err << "\" does not match mOffsetMatrix!";
err << " Bones *must* be in the bind pose to export.";
throw DeadlyExportError(err.str());
}
// transformlink should be the position of the bone in world space,
// in the bind pose.
// For now let's use the inverse of mOffsetMatrix,
// and the (assumedly static) mesh position in world space.
// TODO: find a better way of doing this? there aren't many options
tr = b->mOffsetMatrix;
tr.Inverse();
tr *= mesh_node_xform;
sdnode.AddChild("TransformLink", tr);
// which we just calculated.
sdnode.AddChild("TransformLink", bone_xform);
// done
sdnode.Dump(outstream);
@ -1659,7 +1704,7 @@ void FBXExporter::WriteObjects ()
// we also need to connect the limb node to the subdeformer.
c = FBX::Node("C");
c.AddProperties("OO", bone_uids[name], subdeformer_uid);
c.AddProperties("OO", node_uids[bone_node], subdeformer_uid);
connections.push_back(c); // TODO: emplace_back
}
@ -1753,7 +1798,7 @@ void FBXExporter::WriteObjects ()
// write nodes (i.e. model heirarchy)
// start at root node
WriteModelNodes(
outstream, mScene->mRootNode, 0, bone_uids
outstream, mScene->mRootNode, 0, limbnodes
);
object_node.End(outstream, true);
@ -1864,17 +1909,17 @@ void FBXExporter::WriteModelNodes(
StreamWriterLE& s,
const aiNode* node,
int64_t parent_uid,
const std::map<std::string,int64_t>& bone_uids
const std::unordered_set<const aiNode*>& limbnodes
) {
std::vector<std::pair<std::string,aiVector3D>> chain;
WriteModelNodes(s, node, parent_uid, bone_uids, chain);
WriteModelNodes(s, node, parent_uid, limbnodes, chain);
}
void FBXExporter::WriteModelNodes(
StreamWriterLE& outstream,
const aiNode* node,
int64_t parent_uid,
const std::map<std::string,int64_t>& bone_uids,
const std::unordered_set<const aiNode*>& limbnodes,
std::vector<std::pair<std::string,aiVector3D>>& transform_chain
) {
// first collapse any expanded transformation chains created by FBX import.
@ -1924,7 +1969,7 @@ void FBXExporter::WriteModelNodes(
}
// now just continue to the next node
WriteModelNodes(
outstream, next_node, parent_uid, bone_uids, transform_chain
outstream, next_node, parent_uid, limbnodes, transform_chain
);
return;
}
@ -1962,7 +2007,7 @@ void FBXExporter::WriteModelNodes(
connections.push_back(c);
// write model node
WriteModelNode(outstream, node, node_uid, "Mesh", transform_chain);
} else if (bone_uids.count(node_name)) {
} else if (limbnodes.count(node)) {
WriteModelNode(outstream, node, node_uid, "LimbNode", transform_chain);
// we also need to write a nodeattribute to mark it as a skeleton
int64_t node_attribute_uid = generate_uid();
@ -2021,7 +2066,7 @@ void FBXExporter::WriteModelNodes(
// now recurse into children
for (size_t i = 0; i < node->mNumChildren; ++i) {
WriteModelNodes(
outstream, node->mChildren[i], node_uid, bone_uids
outstream, node->mChildren[i], node_uid, limbnodes
);
}
}

View File

@ -56,6 +56,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <vector>
#include <map>
#include <unordered_set>
#include <memory> // shared_ptr
#include <sstream> // stringstream
@ -129,13 +130,13 @@ namespace Assimp
Assimp::StreamWriterLE& s,
const aiNode* node,
int64_t parent_uid,
const std::map<std::string,int64_t>& bone_uids
const std::unordered_set<const aiNode*>& limbnodes
);
void WriteModelNodes( // usually don't call this directly
StreamWriterLE& s,
const aiNode* node,
int64_t parent_uid,
const std::map<std::string,int64_t>& bone_uids,
const std::unordered_set<const aiNode*>& limbnodes,
std::vector<std::pair<std::string,aiVector3D>>& transform_chain
);
};