1 | // from http://www.ogre3d.org/wiki/index.php/AnimationBlender |
---|
2 | |
---|
3 | #include "AnimationBlender.h" |
---|
4 | #include "OgreAnimationState.h" |
---|
5 | |
---|
6 | |
---|
7 | using namespace Ogre; |
---|
8 | |
---|
9 | |
---|
10 | void AnimationBlender::init(const String &animation) |
---|
11 | { |
---|
12 | AnimationStateSet *set = mEntity->getAllAnimationStates(); |
---|
13 | AnimationStateIterator it = set->getAnimationStateIterator(); |
---|
14 | while(it.hasMoreElements()) |
---|
15 | { |
---|
16 | AnimationState *anim = it.getNext(); |
---|
17 | anim->setEnabled(false); |
---|
18 | anim->setWeight(0); |
---|
19 | anim->setTimePosition(0); |
---|
20 | } |
---|
21 | mSource = mEntity->getAnimationState( animation ); |
---|
22 | mSource->setEnabled(true); |
---|
23 | mSource->setWeight(1); |
---|
24 | mTimeleft = 0; |
---|
25 | mDuration = 1; |
---|
26 | mTarget = 0; |
---|
27 | complete=false; |
---|
28 | } |
---|
29 | void AnimationBlender::blend( const String &animation, BlendingTransition transition, Real duration, bool l ) |
---|
30 | { |
---|
31 | loop=l; |
---|
32 | if( transition == AnimationBlender::BlendSwitch ) |
---|
33 | { |
---|
34 | if( mSource != 0 ) |
---|
35 | mSource->setEnabled(false); |
---|
36 | mSource = mEntity->getAnimationState( animation ); |
---|
37 | mSource->setEnabled(true); |
---|
38 | mSource->setWeight(1); |
---|
39 | mSource->setTimePosition(0); |
---|
40 | mTimeleft = 0; |
---|
41 | } |
---|
42 | else |
---|
43 | { |
---|
44 | AnimationState *newTarget = mEntity->getAnimationState( animation ); |
---|
45 | if( mTimeleft > 0 ) |
---|
46 | { |
---|
47 | // oops, weren't finished yet |
---|
48 | if( newTarget == mTarget ) |
---|
49 | { |
---|
50 | // nothing to do! (ignoring duration here) |
---|
51 | } |
---|
52 | else if( newTarget == mSource ) |
---|
53 | { |
---|
54 | // going back to the source state, so let's switch |
---|
55 | mSource = mTarget; |
---|
56 | mTarget = newTarget; |
---|
57 | mTimeleft = mDuration - mTimeleft; // i'm ignoring the new duration here |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | // ok, newTarget is really new, so either we simply replace the target with this one, or |
---|
62 | // we make the target the new source |
---|
63 | if( mTimeleft < mDuration * 0.5 ) |
---|
64 | { |
---|
65 | // simply replace the target with this one |
---|
66 | mTarget->setEnabled(false); |
---|
67 | mTarget->setWeight(0); |
---|
68 | } |
---|
69 | else |
---|
70 | { |
---|
71 | // old target becomes new source |
---|
72 | mSource->setEnabled(false); |
---|
73 | mSource->setWeight(0); |
---|
74 | mSource = mTarget; |
---|
75 | } |
---|
76 | mTarget = newTarget; |
---|
77 | mTarget->setEnabled(true); |
---|
78 | mTarget->setWeight( 1.0 - mTimeleft / mDuration ); |
---|
79 | mTarget->setTimePosition(0); |
---|
80 | } |
---|
81 | } |
---|
82 | else |
---|
83 | { |
---|
84 | if (mSource == newTarget) |
---|
85 | { |
---|
86 | mTimeleft = 0; |
---|
87 | return; |
---|
88 | } |
---|
89 | // assert( target == 0, "target should be 0 when not blending" ) |
---|
90 | // mSource->setEnabled(true); |
---|
91 | // mSource->setWeight(1); |
---|
92 | mTransition = transition; |
---|
93 | mTimeleft = mDuration = duration; |
---|
94 | mTarget = newTarget; |
---|
95 | mTarget->setEnabled(true); |
---|
96 | mTarget->setWeight(0); |
---|
97 | mTarget->setTimePosition(0); |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | void AnimationBlender::addTime( Real time ) |
---|
102 | { |
---|
103 | if( mSource != 0 ) |
---|
104 | { |
---|
105 | if( mTimeleft > 0 ) |
---|
106 | { |
---|
107 | mTimeleft -= time; |
---|
108 | if( mTimeleft < 0 ) |
---|
109 | { |
---|
110 | // finish blending |
---|
111 | mSource->setEnabled(false); |
---|
112 | mSource->setWeight(0); |
---|
113 | mSource = mTarget; |
---|
114 | mSource->setEnabled(true); |
---|
115 | mSource->setWeight(1); |
---|
116 | mTarget = 0; |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | // still blending, advance weights |
---|
121 | mSource->setWeight(mTimeleft / mDuration); |
---|
122 | mTarget->setWeight(1.0 - mTimeleft / mDuration); |
---|
123 | if(mTransition == AnimationBlender::BlendWhileAnimating) |
---|
124 | mTarget->addTime(time); |
---|
125 | } |
---|
126 | } |
---|
127 | if (mSource->getTimePosition() >= mSource->getLength()) |
---|
128 | { |
---|
129 | complete=true; |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | complete=false; |
---|
134 | } |
---|
135 | mSource->addTime(time); |
---|
136 | mSource->setLoop(loop); |
---|
137 | } |
---|
138 | } |
---|
139 | AnimationBlender::AnimationBlender( Entity *entity ) : mEntity(entity) {} |
---|