source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/src/extras/animation/AnimationMorphTarget.js@ 28897

Last change on this file since 28897 was 28897, checked in by davidb, 10 years ago

GUI front-end to server base plus web page content

File size: 3.8 KB
Line 
1/**
2 * @author mikael emtinger / http://gomo.se/
3 */
4
5THREE.AnimationMorphTarget = function( root, data ) {
6
7 this.root = root;
8 this.data = THREE.AnimationHandler.get( data );
9 this.hierarchy = THREE.AnimationHandler.parse( root );
10 this.currentTime = 0;
11 this.timeScale = 1;
12 this.isPlaying = false;
13 this.isPaused = true;
14 this.loop = true;
15 this.influence = 1;
16}
17
18/*
19 * Play
20 */
21
22THREE.AnimationMorphTarget.prototype.play = function( loop, startTimeMS ) {
23
24 if( !this.isPlaying ) {
25
26 this.isPlaying = true;
27 this.loop = loop !== undefined ? loop : true;
28 this.currentTime = startTimeMS !== undefined ? startTimeMS : 0;
29
30
31 // reset key cache
32
33 for ( var h = 0; h < this.hierarchy.length; h++ ) {
34
35 if ( this.hierarchy[ h ].animationCache === undefined ) {
36
37 this.hierarchy[ h ].animationCache = {};
38 this.hierarchy[ h ].animationCache.prevKey = 0;
39 this.hierarchy[ h ].animationCache.nextKey = 0;
40 }
41
42 this.hierarchy[ h ].animationCache.prevKey = this.data.hierarchy[ h ].keys[ 0 ];
43 this.hierarchy[ h ].animationCache.nextKey = this.data.hierarchy[ h ].keys[ 1 ];
44 }
45
46 this.update( 0 );
47 }
48
49 this.isPaused = false;
50 THREE.AnimationHandler.addToUpdate( this );
51}
52
53
54/*
55 * Pause
56 */
57
58THREE.AnimationMorphTarget.prototype.pause = function() {
59
60 if( this.isPaused ) {
61
62 THREE.AnimationHandler.addToUpdate( this );
63
64 } else {
65
66 THREE.AnimationHandler.removeFromUpdate( this );
67
68 }
69
70 this.isPaused = !this.isPaused;
71}
72
73
74/*
75 * Stop
76 */
77
78THREE.AnimationMorphTarget.prototype.stop = function() {
79
80 this.isPlaying = false;
81 this.isPaused = false;
82
83 THREE.AnimationHandler.removeFromUpdate( this );
84
85
86 // reset JIT matrix and remove cache
87
88 for ( var h = 0; h < this.hierarchy.length; h++ ) {
89
90 if ( this.hierarchy[ h ].animationCache !== undefined ) {
91
92 delete this.hierarchy[ h ].animationCache;
93 }
94
95 }
96
97}
98
99
100/*
101 * Update
102 */
103
104THREE.AnimationMorphTarget.prototype.update = function( deltaTimeMS ) {
105
106 // early out
107
108 if( !this.isPlaying ) return;
109
110
111 // vars
112
113 var scale;
114 var vector;
115 var prevXYZ, nextXYZ;
116 var prevKey, nextKey;
117 var object;
118 var animationCache;
119 var currentTime, unloopedCurrentTime;
120
121
122 // update time
123
124 this.currentTime += deltaTimeMS * this.timeScale;
125
126 unloopedCurrentTime = this.currentTime;
127 currentTime = this.currentTime = this.currentTime % this.data.length;
128
129
130 // update
131
132 for ( var h = 0, hl = this.hierarchy.length; h < hl; h++ ) {
133
134 object = this.hierarchy[ h ];
135 animationCache = object.animationCache;
136
137
138 // get keys
139
140 prevKey = animationCache.prevKey;
141 nextKey = animationCache.nextKey;
142
143
144 // switch keys?
145
146 if ( nextKey.time <= unloopedCurrentTime ) {
147
148 // did we loop?
149
150 if ( currentTime < unloopedCurrentTime ) {
151
152 if ( this.loop ) {
153
154 prevKey = this.data.hierarchy[ h ].keys[ 0 ];
155 nextKey = this.data.hierarchy[ h ].keys[ 1 ];
156
157 while( nextKey.time < currentTime ) {
158
159 prevKey = nextKey;
160 nextKey = this.data.hierarchy[ h ].keys[ nextKey.index + 1 ];
161
162 }
163
164 } else {
165
166 this.stop();
167 return;
168
169 }
170
171 } else {
172
173 do {
174
175 prevKey = nextKey;
176 nextKey = this.data.hierarchy[ h ].keys[ nextKey.index + 1 ];
177
178 } while( nextKey.time < currentTime )
179
180 }
181
182 animationCache.prevKey = prevKey;
183 animationCache.nextKey = nextKey;
184
185 }
186
187
188 // calc scale and check for error
189
190 scale = ( currentTime - prevKey.time ) / ( nextKey.time - prevKey.time );
191
192 if ( scale < 0 || scale > 1 ) {
193
194 console.log( "THREE.AnimationMorphTarget.update: Warning! Scale out of bounds:" + scale );
195 scale = scale < 0 ? 0 : 1;
196
197 }
198
199
200 // interpolate
201
202 var pi, pmti = prevKey.morphTargetsInfluences;
203 var ni, nmti = nextKey.morphTargetsInfluences;
204 var mt, i;
205
206 for( mt in pmti ) {
207
208 pi = pmti[ mt ];
209 ni = nmti[ mt ];
210 i = this.root.getMorphTargetIndexByName( mt );
211
212 this.root.morphTargetInfluences[ i ] = ( pi + ( ni - pi ) * scale ) * this.influence;
213 }
214
215 }
216
217};
218
Note: See TracBrowser for help on using the repository browser.