1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include "skeleton.hpp"
#include <osg/MatrixTransform>
#include <components/debug/debuglog.hpp>
#include <components/misc/strings/lower.hpp>
#include <algorithm>
namespace SceneUtil
{
class InitBoneCacheVisitor : public osg::NodeVisitor
{
public:
typedef std::vector<osg::MatrixTransform*> TransformPath;
InitBoneCacheVisitor(std::unordered_map<std::string, TransformPath>& cache)
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
, mCache(cache)
{
}
void apply(osg::MatrixTransform& node) override
{
mPath.push_back(&node);
mCache.emplace(Misc::StringUtils::lowerCase(node.getName()), mPath);
traverse(node);
mPath.pop_back();
}
private:
TransformPath mPath;
std::unordered_map<std::string, TransformPath>& mCache;
};
Skeleton::Skeleton()
: mBoneCacheInit(false)
, mNeedToUpdateBoneMatrices(true)
, mActive(Active)
, mLastFrameNumber(0)
, mLastCullFrameNumber(0)
{
}
Skeleton::Skeleton(const Skeleton& copy, const osg::CopyOp& copyop)
: osg::Group(copy, copyop)
, mBoneCacheInit(false)
, mNeedToUpdateBoneMatrices(true)
, mActive(copy.mActive)
, mLastFrameNumber(0)
, mLastCullFrameNumber(0)
{
}
Bone* Skeleton::getBone(const std::string& name)
{
if (!mBoneCacheInit)
{
InitBoneCacheVisitor visitor(mBoneCache);
accept(visitor);
mBoneCacheInit = true;
}
BoneCache::iterator found = mBoneCache.find(Misc::StringUtils::lowerCase(name));
if (found == mBoneCache.end())
return nullptr;
// find or insert in the bone hierarchy
if (!mRootBone.get())
{
mRootBone = std::make_unique<Bone>();
}
Bone* bone = mRootBone.get();
for (osg::MatrixTransform* matrixTransform : found->second)
{
const auto it = std::find_if(bone->mChildren.begin(), bone->mChildren.end(),
[&](const auto& v) { return v->mNode == matrixTransform; });
if (it == bone->mChildren.end())
{
bone = bone->mChildren.emplace_back(std::make_unique<Bone>()).get();
mNeedToUpdateBoneMatrices = true;
}
else
bone = it->get();
bone->mNode = matrixTransform;
}
return bone;
}
void Skeleton::updateBoneMatrices(unsigned int traversalNumber)
{
if (traversalNumber != mLastFrameNumber)
mNeedToUpdateBoneMatrices = true;
mLastFrameNumber = traversalNumber;
if (mNeedToUpdateBoneMatrices)
{
if (mRootBone.get())
{
for (const auto& child : mRootBone->mChildren)
child->update(nullptr);
}
mNeedToUpdateBoneMatrices = false;
}
}
void Skeleton::setActive(ActiveType active)
{
mActive = active;
}
bool Skeleton::getActive() const
{
return mActive != Inactive;
}
void Skeleton::markDirty()
{
mLastFrameNumber = 0;
mBoneCache.clear();
mBoneCacheInit = false;
}
void Skeleton::traverse(osg::NodeVisitor& nv)
{
if (nv.getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
{
if (mActive == Inactive && mLastFrameNumber != 0)
return;
if (mActive == SemiActive && mLastFrameNumber != 0 && mLastCullFrameNumber + 3 <= nv.getTraversalNumber())
return;
}
else if (nv.getVisitorType() == osg::NodeVisitor::CULL_VISITOR)
mLastCullFrameNumber = nv.getTraversalNumber();
osg::Group::traverse(nv);
}
void Skeleton::childInserted(unsigned int)
{
markDirty();
}
void Skeleton::childRemoved(unsigned int, unsigned int)
{
markDirty();
}
Bone::Bone()
: mNode(nullptr)
{
}
void Bone::update(const osg::Matrixf* parentMatrixInSkeletonSpace)
{
if (!mNode)
{
Log(Debug::Error) << "Error: Bone without node";
return;
}
if (parentMatrixInSkeletonSpace)
mMatrixInSkeletonSpace = mNode->getMatrix() * (*parentMatrixInSkeletonSpace);
else
mMatrixInSkeletonSpace = mNode->getMatrix();
for (const auto& child : mChildren)
child->update(&mMatrixInSkeletonSpace);
}
}
|