1 | /**
|
---|
2 | * @author alteredq / http://alteredqualia.com/
|
---|
3 | */
|
---|
4 |
|
---|
5 | THREE.MorphBlendMesh = function( geometry, material ) {
|
---|
6 |
|
---|
7 | THREE.Mesh.call( this, geometry, material );
|
---|
8 |
|
---|
9 | this.animationsMap = {};
|
---|
10 | this.animationsList = [];
|
---|
11 |
|
---|
12 | // prepare default animation
|
---|
13 | // (all frames played together in 1 second)
|
---|
14 |
|
---|
15 | var numFrames = this.geometry.morphTargets.length;
|
---|
16 |
|
---|
17 | var name = "__default";
|
---|
18 |
|
---|
19 | var startFrame = 0;
|
---|
20 | var endFrame = numFrames - 1;
|
---|
21 |
|
---|
22 | var fps = numFrames / 1;
|
---|
23 |
|
---|
24 | this.createAnimation( name, startFrame, endFrame, fps );
|
---|
25 | this.setAnimationWeight( name, 1 );
|
---|
26 |
|
---|
27 | };
|
---|
28 |
|
---|
29 | THREE.MorphBlendMesh.prototype = Object.create( THREE.Mesh.prototype );
|
---|
30 |
|
---|
31 | THREE.MorphBlendMesh.prototype.createAnimation = function ( name, start, end, fps ) {
|
---|
32 |
|
---|
33 | var animation = {
|
---|
34 |
|
---|
35 | startFrame: start,
|
---|
36 | endFrame: end,
|
---|
37 |
|
---|
38 | length: end - start + 1,
|
---|
39 |
|
---|
40 | fps: fps,
|
---|
41 | duration: ( end - start ) / fps,
|
---|
42 |
|
---|
43 | lastFrame: 0,
|
---|
44 | currentFrame: 0,
|
---|
45 |
|
---|
46 | active: false,
|
---|
47 |
|
---|
48 | time: 0,
|
---|
49 | direction: 1,
|
---|
50 | weight: 1,
|
---|
51 |
|
---|
52 | directionBackwards: false,
|
---|
53 | mirroredLoop: false
|
---|
54 |
|
---|
55 | };
|
---|
56 |
|
---|
57 | this.animationsMap[ name ] = animation;
|
---|
58 | this.animationsList.push( animation );
|
---|
59 |
|
---|
60 | };
|
---|
61 |
|
---|
62 | THREE.MorphBlendMesh.prototype.autoCreateAnimations = function ( fps ) {
|
---|
63 |
|
---|
64 | var pattern = /([a-z]+)(\d+)/;
|
---|
65 |
|
---|
66 | var firstAnimation, frameRanges = {};
|
---|
67 |
|
---|
68 | var geometry = this.geometry;
|
---|
69 |
|
---|
70 | for ( var i = 0, il = geometry.morphTargets.length; i < il; i ++ ) {
|
---|
71 |
|
---|
72 | var morph = geometry.morphTargets[ i ];
|
---|
73 | var chunks = morph.name.match( pattern );
|
---|
74 |
|
---|
75 | if ( chunks && chunks.length > 1 ) {
|
---|
76 |
|
---|
77 | var name = chunks[ 1 ];
|
---|
78 | var num = chunks[ 2 ];
|
---|
79 |
|
---|
80 | if ( ! frameRanges[ name ] ) frameRanges[ name ] = { start: Infinity, end: -Infinity };
|
---|
81 |
|
---|
82 | var range = frameRanges[ name ];
|
---|
83 |
|
---|
84 | if ( i < range.start ) range.start = i;
|
---|
85 | if ( i > range.end ) range.end = i;
|
---|
86 |
|
---|
87 | if ( ! firstAnimation ) firstAnimation = name;
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | for ( var name in frameRanges ) {
|
---|
94 |
|
---|
95 | var range = frameRanges[ name ];
|
---|
96 | this.createAnimation( name, range.start, range.end, fps );
|
---|
97 |
|
---|
98 | }
|
---|
99 |
|
---|
100 | this.firstAnimation = firstAnimation;
|
---|
101 |
|
---|
102 | };
|
---|
103 |
|
---|
104 | THREE.MorphBlendMesh.prototype.setAnimationDirectionForward = function ( name ) {
|
---|
105 |
|
---|
106 | var animation = this.animationsMap[ name ];
|
---|
107 |
|
---|
108 | if ( animation ) {
|
---|
109 |
|
---|
110 | animation.direction = 1;
|
---|
111 | animation.directionBackwards = false;
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | };
|
---|
116 |
|
---|
117 | THREE.MorphBlendMesh.prototype.setAnimationDirectionBackward = function ( name ) {
|
---|
118 |
|
---|
119 | var animation = this.animationsMap[ name ];
|
---|
120 |
|
---|
121 | if ( animation ) {
|
---|
122 |
|
---|
123 | animation.direction = -1;
|
---|
124 | animation.directionBackwards = true;
|
---|
125 |
|
---|
126 | }
|
---|
127 |
|
---|
128 | };
|
---|
129 |
|
---|
130 | THREE.MorphBlendMesh.prototype.setAnimationFPS = function ( name, fps ) {
|
---|
131 |
|
---|
132 | var animation = this.animationsMap[ name ];
|
---|
133 |
|
---|
134 | if ( animation ) {
|
---|
135 |
|
---|
136 | animation.fps = fps;
|
---|
137 | animation.duration = ( animation.end - animation.start ) / animation.fps;
|
---|
138 |
|
---|
139 | }
|
---|
140 |
|
---|
141 | };
|
---|
142 |
|
---|
143 | THREE.MorphBlendMesh.prototype.setAnimationDuration = function ( name, duration ) {
|
---|
144 |
|
---|
145 | var animation = this.animationsMap[ name ];
|
---|
146 |
|
---|
147 | if ( animation ) {
|
---|
148 |
|
---|
149 | animation.duration = duration;
|
---|
150 | animation.fps = ( animation.end - animation.start ) / animation.duration;
|
---|
151 |
|
---|
152 | }
|
---|
153 |
|
---|
154 | };
|
---|
155 |
|
---|
156 | THREE.MorphBlendMesh.prototype.setAnimationWeight = function ( name, weight ) {
|
---|
157 |
|
---|
158 | var animation = this.animationsMap[ name ];
|
---|
159 |
|
---|
160 | if ( animation ) {
|
---|
161 |
|
---|
162 | animation.weight = weight;
|
---|
163 |
|
---|
164 | }
|
---|
165 |
|
---|
166 | };
|
---|
167 |
|
---|
168 | THREE.MorphBlendMesh.prototype.setAnimationTime = function ( name, time ) {
|
---|
169 |
|
---|
170 | var animation = this.animationsMap[ name ];
|
---|
171 |
|
---|
172 | if ( animation ) {
|
---|
173 |
|
---|
174 | animation.time = time;
|
---|
175 |
|
---|
176 | }
|
---|
177 |
|
---|
178 | };
|
---|
179 |
|
---|
180 | THREE.MorphBlendMesh.prototype.getAnimationTime = function ( name ) {
|
---|
181 |
|
---|
182 | var time = 0;
|
---|
183 |
|
---|
184 | var animation = this.animationsMap[ name ];
|
---|
185 |
|
---|
186 | if ( animation ) {
|
---|
187 |
|
---|
188 | time = animation.time;
|
---|
189 |
|
---|
190 | }
|
---|
191 |
|
---|
192 | return time;
|
---|
193 |
|
---|
194 | };
|
---|
195 |
|
---|
196 | THREE.MorphBlendMesh.prototype.getAnimationDuration = function ( name ) {
|
---|
197 |
|
---|
198 | var duration = -1;
|
---|
199 |
|
---|
200 | var animation = this.animationsMap[ name ];
|
---|
201 |
|
---|
202 | if ( animation ) {
|
---|
203 |
|
---|
204 | duration = animation.duration;
|
---|
205 |
|
---|
206 | }
|
---|
207 |
|
---|
208 | return duration;
|
---|
209 |
|
---|
210 | };
|
---|
211 |
|
---|
212 | THREE.MorphBlendMesh.prototype.playAnimation = function ( name ) {
|
---|
213 |
|
---|
214 | var animation = this.animationsMap[ name ];
|
---|
215 |
|
---|
216 | if ( animation ) {
|
---|
217 |
|
---|
218 | animation.time = 0;
|
---|
219 | animation.active = true;
|
---|
220 |
|
---|
221 | } else {
|
---|
222 |
|
---|
223 | console.warn( "animation[" + name + "] undefined" );
|
---|
224 |
|
---|
225 | }
|
---|
226 |
|
---|
227 | };
|
---|
228 |
|
---|
229 | THREE.MorphBlendMesh.prototype.stopAnimation = function ( name ) {
|
---|
230 |
|
---|
231 | var animation = this.animationsMap[ name ];
|
---|
232 |
|
---|
233 | if ( animation ) {
|
---|
234 |
|
---|
235 | animation.active = false;
|
---|
236 |
|
---|
237 | }
|
---|
238 |
|
---|
239 | };
|
---|
240 |
|
---|
241 | THREE.MorphBlendMesh.prototype.update = function ( delta ) {
|
---|
242 |
|
---|
243 | for ( var i = 0, il = this.animationsList.length; i < il; i ++ ) {
|
---|
244 |
|
---|
245 | var animation = this.animationsList[ i ];
|
---|
246 |
|
---|
247 | if ( ! animation.active ) continue;
|
---|
248 |
|
---|
249 | var frameTime = animation.duration / animation.length;
|
---|
250 |
|
---|
251 | animation.time += animation.direction * delta;
|
---|
252 |
|
---|
253 | if ( animation.mirroredLoop ) {
|
---|
254 |
|
---|
255 | if ( animation.time > animation.duration || animation.time < 0 ) {
|
---|
256 |
|
---|
257 | animation.direction *= -1;
|
---|
258 |
|
---|
259 | if ( animation.time > animation.duration ) {
|
---|
260 |
|
---|
261 | animation.time = animation.duration;
|
---|
262 | animation.directionBackwards = true;
|
---|
263 |
|
---|
264 | }
|
---|
265 |
|
---|
266 | if ( animation.time < 0 ) {
|
---|
267 |
|
---|
268 | animation.time = 0;
|
---|
269 | animation.directionBackwards = false;
|
---|
270 |
|
---|
271 | }
|
---|
272 |
|
---|
273 | }
|
---|
274 |
|
---|
275 | } else {
|
---|
276 |
|
---|
277 | animation.time = animation.time % animation.duration;
|
---|
278 |
|
---|
279 | if ( animation.time < 0 ) animation.time += animation.duration;
|
---|
280 |
|
---|
281 | }
|
---|
282 |
|
---|
283 | var keyframe = animation.startFrame + THREE.Math.clamp( Math.floor( animation.time / frameTime ), 0, animation.length - 1 );
|
---|
284 | var weight = animation.weight;
|
---|
285 |
|
---|
286 | if ( keyframe !== animation.currentFrame ) {
|
---|
287 |
|
---|
288 | this.morphTargetInfluences[ animation.lastFrame ] = 0;
|
---|
289 | this.morphTargetInfluences[ animation.currentFrame ] = 1 * weight;
|
---|
290 |
|
---|
291 | this.morphTargetInfluences[ keyframe ] = 0;
|
---|
292 |
|
---|
293 | animation.lastFrame = animation.currentFrame;
|
---|
294 | animation.currentFrame = keyframe;
|
---|
295 |
|
---|
296 | }
|
---|
297 |
|
---|
298 | var mix = ( animation.time % frameTime ) / frameTime;
|
---|
299 |
|
---|
300 | if ( animation.directionBackwards ) mix = 1 - mix;
|
---|
301 |
|
---|
302 | this.morphTargetInfluences[ animation.currentFrame ] = mix * weight;
|
---|
303 | this.morphTargetInfluences[ animation.lastFrame ] = ( 1 - mix ) * weight;
|
---|
304 |
|
---|
305 | }
|
---|
306 |
|
---|
307 | };
|
---|