osgAnimation.TimelineAnimationManager是OpenSceneGraph(OSG)库中的一个动画管理器类,用于管理基于时间轴的动画。
osgAnimation.TimelineAnimationManager主要用于以下功能:
namespace osgAnimation {
class OSGANIMATION_EXPORT TimelineAnimationManager : public osg::NodeCallback {
public:
TimelineAnimationManager();
TimelineAnimationManager(const TimelineAnimationManager& tm, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
META_Node(osgAnimation, TimelineAnimationManager);
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
void playAnimation(unsigned int i);
void setAnimationOn(unsigned int i);
void setAnimationOff(unsigned int i);
void stopAnimation(unsigned int i);
void resetAnimation(unsigned int i);
void pauseAnimation(unsigned int i);
void resumeAnimation(unsigned int i);
void setNumChannels(unsigned int numChannels);
unsigned int getNumChannels() const;
bool getAnimationOn(unsigned int i) const;
double getTime(unsigned int i) const;
void setTime(unsigned int i, double t);
void setStartTime(unsigned int i, double t);
double getStartTime(unsigned int i) const;
void setEndTime(unsigned int i, double t);
double getEndTime(unsigned int i) const;
void setRepeat(unsigned int i, bool repeat);
bool getRepeat(unsigned int i) const;
void setSpeed(unsigned int i, double speed);
double getSpeed(unsigned int i) const;
void setPlayMode(unsigned int i, PlayMode playMode);
PlayMode getPlayMode(unsigned int i) const;
void setWeight(unsigned int i, double weight);
double getWeight(unsigned int i) const;
bool getPiBlend(unsigned int i) const;
void setPiBlend(unsigned int i, bool blend);
double getPiMerge(unsigned int i) const;
void setPiMerge(unsigned int i, double merge);
int getSchedulerInstanceID() const;
void setSchedulerInstanceID(int id);
void updateChild( osgAnimation::Animation * anim );
Animation* getAnimation(unsigned int i) const;
void setAnimation(unsigned int i, Animation* anim);
unsigned int addAnimation(Animation* anim);
void removeAnimation(Animation* anim);
void removeAnimation(unsigned int i);
void removeAllAnimations();
void setNode(osg::Node& node);
osg::Node* getNode() const;
virtual void getUsage(osg::ApplicationUsage& au) const;
protected:
virtual ~TimelineAnimationManager();
osg::ref_ptr<osgAnimation::Timeline> _timeline;
osg::ref_ptr<osg::Group> _group;
typedef std::vector<osgAnimation::TimeControlEventHandlerRefPtr> TimeControlList;
TimeControlList _timeControlList;
typedef std::map<osg::ref_ptr<osgAnimation::Animation>, unsigned int, osgAnimation::PointerLess<osgAnimation::Animation> > AnimationMap;
AnimationMap _animationMap;
class AnimationData;
typedef std::vector<osg::ref_ptr<AnimationData> > AnimationDataList;
AnimationDataList _animationDataList;
int _schedulerInstanceID;
void init();
void setAnimationState(unsigned int i, osgAnimation::TimeControlEventHandler::AnimationState state);
void setupEventHandlers();
bool event( osgAnimation::TimeEvent& event, void* ptr );
};
}
playAnimation(unsigned int i)
: 播放序号为i的动画。setAnimationOn(unsigned int i)
: 将序号为i的动画设置为开启状态。setAnimationOff(unsigned int i)
: 将序号为i的动画设置为关闭状态。stopAnimation(unsigned int i)
: 停止播放序号为i的动画。resetAnimation(unsigned int i)
: 将序号为i的动画重置至初始状态。pauseAnimation(unsigned int i)
: 暂停播放序号为i的动画。resumeAnimation(unsigned int i)
: 恢复播放序号为i的动画。unsigned int addAnimation(Animation* anim)
: 添加一个新动画,返回其序号。void removeAnimation(Animation* anim)
: 移除一个动画。void removeAllAnimations()
: 移除所有动画。setNode(osg::Node& node)
: 将动画管理器应用于指定节点。void operator()(osg::Node* node, osg::NodeVisitor* nv)
: 重载的()"运算符",用于每一次更新场景时调用。setTime(unsigned int i, double t)
: 将序号为i的动画时间轴移动至t秒。setStartTime(unsigned int i, double t)
: 设置序号为i的动画起始时间为t秒。setEndTime(unsigned int i, double t)
: 设置序号为i的动画结束时间为t秒。setRepeat(unsigned int i, bool repeat)
: 设置序号为i的动画是否循环播放。setSpeed(unsigned int i, double speed)
: 设置序号为i的动画播放速度。setPlayMode(unsigned int i, PlayMode playMode)
: 设置序号为i的动画的播放模式。setWeight(unsigned int i, double weight)
: 设置序号为i的动画的播放权重。void setPiBlend(unsigned int i, bool blend)
: 设置序号为i的动画混合状态。void setPiMerge(unsigned int i, double merge)
: 设置序号为i的动画混合值。Animation* getAnimation(unsigned int i) const
: 获得序号为i的动画对象。unsigned int getNumChannels() const
: 获得动画通道数。void setAnimation(unsigned int i, Animation* anim)
: 替换序号为i的动画。osg::Node* getNode() const
: 获得被应用动画的节点对象。#include <osg/AnimationPath>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgAnimation/TimelineAnimationManager>
int main()
{
osg::ref_ptr<osg::MatrixTransform> root = new osg::MatrixTransform; // 创建根节点
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("cow.osgt"); // 加载模型文件
root->addChild(model.get()); // 添加模型到根节点
osg::ref_ptr<osgAnimation::TimelineAnimationManager> timelineManager = new osgAnimation::TimelineAnimationManager(); // 创建动画管理器
root->setUpdateCallback(timelineManager.get()); // 应用动画管理器到根节点
osg::ref_ptr<osgAnimation::AnimationPath> animationPath1 = new osgAnimation::AnimationPath(); // 创建第一个动画路径
animationPath1->insert(0.0, osg::AnimationPath::ControlPoint(osg::Vec3(0.0,0.0,0.0))); // 路径的起点
animationPath1->insert(1.0, osg::AnimationPath::ControlPoint(osg::Vec3(0.0,0.0,10.0))); // 终点
osg::ref_ptr<osgAnimation::Animation> animation1 = new osgAnimation::AnimationPathCallback(animationPath1.get()); // 创建第一个动画
osg::ref_ptr<osgAnimation::AnimationPath> animationPath2 = new osgAnimation::AnimationPath(); // 创建第二个动画路径
animationPath2->insert(0.0, osg::AnimationPath::ControlPoint(osg::Vec3(0.0,0.0,0.0))); // 路径的起点
animationPath2->insert(1.0, osg::AnimationPath::ControlPoint(osg::Vec3(0.0,0.0,-10.0))); // 终点
osg::ref_ptr<osgAnimation::Animation> animation2 = new osgAnimation::AnimationPathCallback(animationPath2.get()); // 创建第二个动画
timelineManager->addAnimation(animation1.get()); // 将第一个动画添加到动画管理器
timelineManager->addAnimation(animation2.get()); // 将第二个动画添加到动画管理器
timelineManager->setAnimationOn(0); // 打开第一个动画
timelineManager->setAnimationOn(1); // 打开第二个动画
osgViewer::Viewer viewer; // 创建查看器
viewer.setSceneData(root.get()); // 设置场景树根节点
return viewer.run(); // 运行查看器
}