osgAnimation.Animation是OpenSceneGraph中用于控制动画的类。
osgAnimation.Animation是osgAnimation动画的根节点,包含其他控制动画的节点,如动画的开始和结束。此外,它还包含动画的帧率、长度、循环。
osgAnimation.Animation包含了两种主要类型的动画: 关键帧动画和骨骼动画。 它们具有以下不同:
osgAnimation.Animation也允许用户创建自己的动画类型并在场景中进行控制。
osg::ref_ptr<osgAnimation::Animation> animation = new osgAnimation::Animation;
osg::ref_ptr<osgAnimation::Channel> channel = new osgAnimation::Vec3LinearChannel;
channel->setName("position"); // 设置通道名称
animation->addChannel(channel); // 将通道添加至动画中
osg::ref_ptr<osgAnimation::Vec3LinearChannel> channel = static_cast<osgAnimation::Vec3LinearChannel*>(animation->getChannel("position").get());
channel->getKeyframes()->push_back(osgAnimation::Vec3LinearChannel::Keyframe(0.0, osg::Vec3(0.0f, 0.0f, 0.0f)));
channel->getKeyframes()->push_back(osgAnimation::Vec3LinearChannel::Keyframe(1.0, osg::Vec3(10.0f, 0.0f, 0.0f)));
osgAnimation::AnimationManagerBase* animManager = osgAnimation::AnimationManagerBase::getManager();
if (animManager)
{
animManager->registerAnimation(animation.get());
}
animation->update(time); // 传递动画时间
animation->apply(sg); // 将动画应用到场景图SG中
以下是骨骼动画的示例代码:
// 创建节点矩阵
osg::ref_ptr<osg::MatrixTransform> transform = new osg::MatrixTransform;
transform->setDataVariance(osg::Object::DYNAMIC);
...
// 将骨架添加到动画组
osg::ref_ptr<osgAnimation::Bone> bone = new osgAnimation::Bone;
bone->setName("rootBone");
bone->setMatrix(osg::Matrix::translate(osg::Vec3(0.0f, -20.0f, 0.0f)));
...
osg::ref_ptr<osgAnimation::Skeleton> skeleton = new osgAnimation::Skeleton;
skeleton->addBone(bone);
...
osg::ref_ptr<osgAnimation::StackedTransform> st = new osgAnimation::StackedTransform;
st->setName("root");
st->setMatrix(skeleton->getBone(0)->getMatrixInSkeletonSpace()); // 设置根骨骼
...
osg::ref_ptr<osgAnimation::Animation> animation = new osgAnimation::Animation;
animation->addChannel(new osgAnimation::Vec3LinearChannel("root", osgAnimation::Animation::RELATIVE_TO_PARENTS)); // 添加动画通道
for (float t = 0.0f; t <= 1.0f; t += 0.1)
{
st->setMatrix(osg::Matrix::translate(osg::Vec3(0.0f, sin(2.0*osg::PI*t)*10.0f, cos(2.0*osg::PI*t)*10.0f)));
animation->getChannel("root")->getOrCreateSampler()->addTime((double)t, st->getMatrix());
}
...
osgAnimation::AnimationManagerBase* animManager = osgAnimation::AnimationManagerBase::getManager();
if (animManager) {
animManager->registerAnimation(animation.get());
}
osgAnimation.Animation还提供了对动画的管理方法。 可以使用AnimationManager进行全局动画管理。
osg::ref_ptr<osgAnimation::AnimationManagerBase> animManager = osgAnimation::AnimationManagerBase::createAnimationManagerBase();
// 停止所有动画
animManager->stopall();
// 删除所有动画
animManager->clear();
// 禁用/启用动画更新
animManager->setUpdateTraversalMask(animManager->getUpdateTraversalMask() & ~osg::NodeVisitor::PROCESS_ANIMATION);
// 设置全局动画播放速度
animManager->setGlobalAnimationTimeScale(double timeScale);
// 获取全局动画播放速度
double timeScale = animManager->getGlobalAnimationTimeScale();
// 获取全局动画管理器实例
osg::ref_ptr<osgAnimation::AnimationManagerBase> animManager = osgAnimation::AnimationManagerBase::getManager();
源代码可以在以下网址中找到:https://github.com/openscenegraph/OpenSceneGraph/blob/master/include/osgAnimation/Animation。