2019-10-26 17:47:48 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2021-02-28 11:17:54 +00:00
|
|
|
Copyright (c) 2006-2021, assimp team
|
2019-10-26 17:47:48 +00:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "ArmaturePopulate.h"
|
|
|
|
|
|
|
|
#include <assimp/BaseImporter.h>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
|
|
|
#include <assimp/postprocess.h>
|
|
|
|
#include <assimp/scene.h>
|
2019-10-27 12:27:54 +00:00
|
|
|
#include <iostream>
|
2019-10-26 17:47:48 +00:00
|
|
|
|
|
|
|
namespace Assimp {
|
|
|
|
|
2019-10-26 18:48:34 +00:00
|
|
|
/// The default class constructor.
|
2020-02-09 10:14:42 +00:00
|
|
|
ArmaturePopulate::ArmaturePopulate() :
|
|
|
|
BaseProcess() {
|
|
|
|
// do nothing
|
|
|
|
}
|
2019-10-26 18:48:34 +00:00
|
|
|
|
|
|
|
/// The class destructor.
|
2020-02-09 10:14:42 +00:00
|
|
|
ArmaturePopulate::~ArmaturePopulate() {
|
|
|
|
// do nothing
|
|
|
|
}
|
2019-10-26 18:48:34 +00:00
|
|
|
|
2019-10-26 17:47:48 +00:00
|
|
|
bool ArmaturePopulate::IsActive(unsigned int pFlags) const {
|
2020-02-09 10:14:42 +00:00
|
|
|
return (pFlags & aiProcess_PopulateArmatureData) != 0;
|
2019-10-26 17:47:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 14:37:20 +00:00
|
|
|
void ArmaturePopulate::SetupProperties(const Importer *) {
|
2020-02-09 10:14:42 +00:00
|
|
|
// do nothing
|
2019-10-26 17:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArmaturePopulate::Execute(aiScene *out) {
|
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
// Now convert all bone positions to the correct mOffsetMatrix
|
|
|
|
std::vector<aiBone *> bones;
|
|
|
|
std::vector<aiNode *> nodes;
|
|
|
|
std::map<aiBone *, aiNode *> bone_stack;
|
|
|
|
BuildBoneList(out->mRootNode, out->mRootNode, out, bones);
|
|
|
|
BuildNodeList(out->mRootNode, nodes);
|
2019-10-26 17:47:48 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
BuildBoneStack(out->mRootNode, out->mRootNode, out, bones, bone_stack, nodes);
|
2019-10-26 17:47:48 +00:00
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_DEBUG("Bone stack size: ", bone_stack.size());
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
for (std::pair<aiBone *, aiNode *> kvp : bone_stack) {
|
|
|
|
aiBone *bone = kvp.first;
|
|
|
|
aiNode *bone_node = kvp.second;
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("active node lookup: ", bone->mName.C_Str());
|
2020-02-09 10:14:42 +00:00
|
|
|
// lcl transform grab - done in generate_nodes :)
|
2019-10-26 17:47:48 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
// bone->mOffsetMatrix = bone_node->mTransformation;
|
|
|
|
aiNode *armature = GetArmatureRoot(bone_node, bones);
|
2019-10-26 17:47:48 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
ai_assert(armature);
|
2019-10-26 17:47:48 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
// set up bone armature id
|
|
|
|
bone->mArmature = armature;
|
2019-10-26 17:47:48 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
// set this bone node to be referenced properly
|
|
|
|
ai_assert(bone_node);
|
|
|
|
bone->mNode = bone_node;
|
|
|
|
}
|
2019-10-26 17:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
// Reprocess all nodes to calculate bone transforms properly based on the REAL
|
|
|
|
// mOffsetMatrix not the local.
|
|
|
|
// Before this would use mesh transforms which is wrong for bone transforms
|
|
|
|
// Before this would work for simple character skeletons but not complex meshes
|
|
|
|
// with multiple origins
|
|
|
|
// Source: sketch fab log cutter fbx
|
2019-10-26 17:47:48 +00:00
|
|
|
void ArmaturePopulate::BuildBoneList(aiNode *current_node,
|
|
|
|
const aiNode *root_node,
|
|
|
|
const aiScene *scene,
|
|
|
|
std::vector<aiBone *> &bones) {
|
2020-02-09 10:14:42 +00:00
|
|
|
ai_assert(scene);
|
|
|
|
for (unsigned int nodeId = 0; nodeId < current_node->mNumChildren; ++nodeId) {
|
|
|
|
aiNode *child = current_node->mChildren[nodeId];
|
|
|
|
ai_assert(child);
|
|
|
|
|
|
|
|
// check for bones
|
|
|
|
for (unsigned int meshId = 0; meshId < child->mNumMeshes; ++meshId) {
|
|
|
|
ai_assert(child->mMeshes);
|
|
|
|
unsigned int mesh_index = child->mMeshes[meshId];
|
|
|
|
aiMesh *mesh = scene->mMeshes[mesh_index];
|
|
|
|
ai_assert(mesh);
|
|
|
|
|
|
|
|
for (unsigned int boneId = 0; boneId < mesh->mNumBones; ++boneId) {
|
|
|
|
aiBone *bone = mesh->mBones[boneId];
|
2021-02-25 13:01:18 +00:00
|
|
|
ai_assert(nullptr != bone);
|
2020-02-09 10:14:42 +00:00
|
|
|
|
|
|
|
// duplicate mehes exist with the same bones sometimes :)
|
|
|
|
// so this must be detected
|
|
|
|
if (std::find(bones.begin(), bones.end(), bone) == bones.end()) {
|
|
|
|
// add the element once
|
2021-02-25 13:01:18 +00:00
|
|
|
bones.emplace_back(bone);
|
2020-02-09 10:14:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find mesh and get bones
|
|
|
|
// then do recursive lookup for bones in root node hierarchy
|
2019-10-26 17:47:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
BuildBoneList(child, root_node, scene, bones);
|
2019-10-26 17:47:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
// Prepare flat node list which can be used for non recursive lookups later
|
2019-10-27 13:53:11 +00:00
|
|
|
void ArmaturePopulate::BuildNodeList(const aiNode *current_node,
|
|
|
|
std::vector<aiNode *> &nodes) {
|
2021-02-25 13:01:18 +00:00
|
|
|
ai_assert(nullptr != current_node);
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
for (unsigned int nodeId = 0; nodeId < current_node->mNumChildren; ++nodeId) {
|
|
|
|
aiNode *child = current_node->mChildren[nodeId];
|
|
|
|
ai_assert(child);
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
if (child->mNumMeshes == 0) {
|
2021-02-25 13:01:18 +00:00
|
|
|
nodes.emplace_back(child);
|
2020-02-09 10:14:42 +00:00
|
|
|
}
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
BuildNodeList(child, nodes);
|
2019-10-27 13:53:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
// A bone stack allows us to have multiple armatures, with the same bone names
|
|
|
|
// A bone stack allows us also to retrieve bones true transform even with
|
|
|
|
// duplicate names :)
|
2020-02-16 14:37:20 +00:00
|
|
|
void ArmaturePopulate::BuildBoneStack(aiNode *,
|
2019-10-26 17:47:48 +00:00
|
|
|
const aiNode *root_node,
|
2020-03-15 10:47:05 +00:00
|
|
|
const aiScene*,
|
2019-10-26 17:47:48 +00:00
|
|
|
const std::vector<aiBone *> &bones,
|
|
|
|
std::map<aiBone *, aiNode *> &bone_stack,
|
2020-02-09 10:14:42 +00:00
|
|
|
std::vector<aiNode *> &node_stack) {
|
2021-02-25 13:01:18 +00:00
|
|
|
if (node_stack.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ai_assert(nullptr != root_node);
|
2019-10-26 17:47:48 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
for (aiBone *bone : bones) {
|
|
|
|
ai_assert(bone);
|
|
|
|
aiNode *node = GetNodeFromStack(bone->mName, node_stack);
|
|
|
|
if (node == nullptr) {
|
|
|
|
node_stack.clear();
|
|
|
|
BuildNodeList(root_node, node_stack);
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("Resetting bone stack: nullptr element ", bone->mName.C_Str());
|
2020-02-09 10:14:42 +00:00
|
|
|
|
|
|
|
node = GetNodeFromStack(bone->mName, node_stack);
|
|
|
|
|
2021-02-25 13:01:18 +00:00
|
|
|
if (nullptr == node) {
|
2020-02-09 10:14:42 +00:00
|
|
|
ASSIMP_LOG_ERROR("serious import issue node for bone was not detected");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 17:47:48 +00:00
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("Successfully added bone[", bone->mName.C_Str(), "] to stack and bone node is: ", node->mName.C_Str());
|
2019-10-26 17:47:48 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
bone_stack.insert(std::pair<aiBone *, aiNode *>(bone, node));
|
|
|
|
}
|
|
|
|
}
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
// Returns the armature root node
|
|
|
|
// This is required to be detected for a bone initially, it will recurse up
|
|
|
|
// until it cannot find another bone and return the node No known failure
|
|
|
|
// points. (yet)
|
2019-10-27 13:53:11 +00:00
|
|
|
aiNode *ArmaturePopulate::GetArmatureRoot(aiNode *bone_node,
|
|
|
|
std::vector<aiBone *> &bone_list) {
|
2021-02-25 13:01:18 +00:00
|
|
|
while (nullptr != bone_node) {
|
2020-02-09 10:14:42 +00:00
|
|
|
if (!IsBoneNode(bone_node->mName, bone_list)) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("GetArmatureRoot() Found valid armature: ", bone_node->mName.C_Str());
|
2020-02-09 10:14:42 +00:00
|
|
|
return bone_node;
|
|
|
|
}
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
bone_node = bone_node->mParent;
|
|
|
|
}
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
ASSIMP_LOG_ERROR("GetArmatureRoot() can't find armature!");
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
// Simple IsBoneNode check if this could be a bone
|
2019-10-27 13:53:11 +00:00
|
|
|
bool ArmaturePopulate::IsBoneNode(const aiString &bone_name,
|
|
|
|
std::vector<aiBone *> &bones) {
|
2020-02-09 10:14:42 +00:00
|
|
|
for (aiBone *bone : bones) {
|
|
|
|
if (bone->mName == bone_name) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-27 13:53:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
return false;
|
2019-10-27 13:53:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
// Pop this node by name from the stack if found
|
|
|
|
// Used in multiple armature situations with duplicate node / bone names
|
|
|
|
// Known flaw: cannot have nodes with bone names, will be fixed in later release
|
|
|
|
// (serious to be fixed) Known flaw: nodes which have more than one bone could
|
|
|
|
// be prematurely dropped from stack
|
2019-10-27 13:53:11 +00:00
|
|
|
aiNode *ArmaturePopulate::GetNodeFromStack(const aiString &node_name,
|
|
|
|
std::vector<aiNode *> &nodes) {
|
2020-02-09 10:14:42 +00:00
|
|
|
std::vector<aiNode *>::iterator iter;
|
|
|
|
aiNode *found = nullptr;
|
|
|
|
for (iter = nodes.begin(); iter < nodes.end(); ++iter) {
|
|
|
|
aiNode *element = *iter;
|
2021-02-25 13:01:18 +00:00
|
|
|
ai_assert(nullptr != element);
|
2020-02-09 10:14:42 +00:00
|
|
|
// node valid and node name matches
|
|
|
|
if (element->mName == node_name) {
|
|
|
|
found = element;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-27 13:53:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
if (found != nullptr) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_INFO("Removed node from stack: ", found->mName.C_Str());
|
2020-02-09 10:14:42 +00:00
|
|
|
// now pop the element from the node list
|
|
|
|
nodes.erase(iter);
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
return found;
|
|
|
|
}
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
// unique names can cause this problem
|
|
|
|
ASSIMP_LOG_ERROR("[Serious] GetNodeFromStack() can't find node from stack!");
|
2019-10-27 13:53:11 +00:00
|
|
|
|
2020-02-09 10:14:42 +00:00
|
|
|
return nullptr;
|
2019-10-27 13:53:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 17:47:48 +00:00
|
|
|
} // Namespace Assimp
|