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 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#include <components/sceneutil/osgacontroller.hpp>
#include <osg/MatrixTransform>
#include <osg/Node>
#include <osg/NodeVisitor>
#include <osg/ref_ptr>
#include <osgAnimation/Animation>
#include <osgAnimation/Channel>
#include <osgAnimation/Sampler>
#include <osgAnimation/UpdateMatrixTransform>
#include <components/misc/strings/algorithm.hpp>
#include <components/misc/strings/lower.hpp>
#include <components/resource/animation.hpp>
#include <components/sceneutil/controller.hpp>
#include <components/sceneutil/keyframe.hpp>
namespace SceneUtil
{
LinkVisitor::LinkVisitor()
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
{
mAnimation = nullptr;
}
void LinkVisitor::link(osgAnimation::UpdateMatrixTransform* umt)
{
const osgAnimation::ChannelList& channels = mAnimation->getChannels();
for (const auto& channel : channels)
{
const std::string& channelName = channel->getName();
const std::string& channelTargetName = channel->getTargetName();
if (channelTargetName != umt->getName())
continue;
// check if we can link a StackedTransformElement to the current Channel
for (const auto& stackedTransform : umt->getStackedTransforms())
{
osgAnimation::StackedTransformElement* element = stackedTransform.get();
if (element && !element->getName().empty() && channelName == element->getName())
{
osgAnimation::Target* target = element->getOrCreateTarget();
if (target)
{
channel->setTarget(target);
}
}
}
}
}
void LinkVisitor::setAnimation(Resource::Animation* animation)
{
mAnimation = animation;
}
void LinkVisitor::apply(osg::Node& node)
{
osg::Callback* cb = node.getUpdateCallback();
while (cb)
{
osgAnimation::UpdateMatrixTransform* umt = dynamic_cast<osgAnimation::UpdateMatrixTransform*>(cb);
if (umt)
if (Misc::StringUtils::lowerCase(node.getName()) != "bip01")
link(umt);
cb = cb->getNestedCallback();
}
if (node.getNumChildrenRequiringUpdateTraversal())
traverse(node);
}
OsgAnimationController::OsgAnimationController(const OsgAnimationController& copy, const osg::CopyOp& copyop)
: osg::Object(copy, copyop)
, SceneUtil::KeyframeController(copy)
, SceneUtil::NodeCallback<OsgAnimationController>(copy, copyop)
, mEmulatedAnimations(copy.mEmulatedAnimations)
{
mLinker = nullptr;
for (const auto& mergedAnimationTrack : copy.mMergedAnimationTracks)
{
Resource::Animation* copiedAnimationTrack
= static_cast<Resource::Animation*>(mergedAnimationTrack.get()->clone(copyop));
mMergedAnimationTracks.emplace_back(copiedAnimationTrack);
}
}
osg::Matrixf OsgAnimationController::getTransformForNode(float time, const std::string_view name) const
{
std::string animationName;
float newTime = time;
// Find the correct animation based on time
for (const EmulatedAnimation& emulatedAnimation : mEmulatedAnimations)
{
if (time >= emulatedAnimation.mStartTime && time <= emulatedAnimation.mStopTime)
{
newTime = time - emulatedAnimation.mStartTime;
animationName = emulatedAnimation.mName;
break;
}
}
// Find the bone's transform track in animation
for (const auto& mergedAnimationTrack : mMergedAnimationTracks)
{
if (mergedAnimationTrack->getName() != animationName)
continue;
const osgAnimation::ChannelList& channels = mergedAnimationTrack->getChannels();
for (const auto& channel : channels)
{
if (!Misc::StringUtils::ciEqual(name, channel->getTargetName()) || channel->getName() != "transform")
continue;
if (osgAnimation::MatrixLinearSampler* templateSampler
= dynamic_cast<osgAnimation::MatrixLinearSampler*>(channel->getSampler()))
{
osg::Matrixf matrix;
templateSampler->getValueAt(newTime, matrix);
return matrix;
}
}
}
return osg::Matrixf::identity();
}
osg::Vec3f OsgAnimationController::getTranslation(float time) const
{
return getTransformForNode(time, "bip01").getTrans();
}
void OsgAnimationController::update(float time, const std::string& animationName)
{
for (const auto& mergedAnimationTrack : mMergedAnimationTracks)
{
if (mergedAnimationTrack->getName() == animationName)
mergedAnimationTrack->update(time);
}
}
void OsgAnimationController::operator()(osg::Node* node, osg::NodeVisitor* nv)
{
if (hasInput())
{
if (mNeedToLink)
{
for (const auto& mergedAnimationTrack : mMergedAnimationTracks)
{
if (!mLinker.valid())
mLinker = new LinkVisitor();
mLinker->setAnimation(mergedAnimationTrack);
node->accept(*mLinker);
}
mNeedToLink = false;
}
float time = getInputValue(nv);
for (const EmulatedAnimation& emulatedAnimation : mEmulatedAnimations)
{
if (time > emulatedAnimation.mStartTime && time < emulatedAnimation.mStopTime)
{
update(time - emulatedAnimation.mStartTime, emulatedAnimation.mName);
}
}
// Reset the transform of this node to whats in the animation
// we force this here because downstream some code relies on the bone having a non-modified transform
// as this is how the NIF controller behaves. RotationController is a good example of this.
// Without this here, it causes osgAnimation skeletons to spin wildly
static_cast<osg::MatrixTransform*>(node)->setMatrix(getTransformForNode(time, node->getName()));
}
traverse(node, nv);
}
void OsgAnimationController::setEmulatedAnimations(const std::vector<EmulatedAnimation>& emulatedAnimations)
{
mEmulatedAnimations = emulatedAnimations;
}
void OsgAnimationController::addMergedAnimationTrack(osg::ref_ptr<Resource::Animation> animationTrack)
{
mMergedAnimationTracks.emplace_back(animationTrack);
}
}
|