source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/src/objects/MorphAnimMesh.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: 4.0 KB
Line 
1/**
2 * @author alteredq / http://alteredqualia.com/
3 */
4
5THREE.MorphAnimMesh = function ( geometry, material ) {
6
7 THREE.Mesh.call( this, geometry, material );
8
9 // API
10
11 this.duration = 1000; // milliseconds
12 this.mirroredLoop = false;
13 this.time = 0;
14
15 // internals
16
17 this.lastKeyframe = 0;
18 this.currentKeyframe = 0;
19
20 this.direction = 1;
21 this.directionBackwards = false;
22
23 this.setFrameRange( 0, this.geometry.morphTargets.length - 1 );
24
25};
26
27THREE.MorphAnimMesh.prototype = Object.create( THREE.Mesh.prototype );
28
29THREE.MorphAnimMesh.prototype.setFrameRange = function ( start, end ) {
30
31 this.startKeyframe = start;
32 this.endKeyframe = end;
33
34 this.length = this.endKeyframe - this.startKeyframe + 1;
35
36};
37
38THREE.MorphAnimMesh.prototype.setDirectionForward = function () {
39
40 this.direction = 1;
41 this.directionBackwards = false;
42
43};
44
45THREE.MorphAnimMesh.prototype.setDirectionBackward = function () {
46
47 this.direction = -1;
48 this.directionBackwards = true;
49
50};
51
52THREE.MorphAnimMesh.prototype.parseAnimations = function () {
53
54 var geometry = this.geometry;
55
56 if ( ! geometry.animations ) geometry.animations = {};
57
58 var firstAnimation, animations = geometry.animations;
59
60 var pattern = /([a-z]+)(\d+)/;
61
62 for ( var i = 0, il = geometry.morphTargets.length; i < il; i ++ ) {
63
64 var morph = geometry.morphTargets[ i ];
65 var parts = morph.name.match( pattern );
66
67 if ( parts && parts.length > 1 ) {
68
69 var label = parts[ 1 ];
70 var num = parts[ 2 ];
71
72 if ( ! animations[ label ] ) animations[ label ] = { start: Infinity, end: -Infinity };
73
74 var animation = animations[ label ];
75
76 if ( i < animation.start ) animation.start = i;
77 if ( i > animation.end ) animation.end = i;
78
79 if ( ! firstAnimation ) firstAnimation = label;
80
81 }
82
83 }
84
85 geometry.firstAnimation = firstAnimation;
86
87};
88
89THREE.MorphAnimMesh.prototype.setAnimationLabel = function ( label, start, end ) {
90
91 if ( ! this.geometry.animations ) this.geometry.animations = {};
92
93 this.geometry.animations[ label ] = { start: start, end: end };
94
95};
96
97THREE.MorphAnimMesh.prototype.playAnimation = function ( label, fps ) {
98
99 var animation = this.geometry.animations[ label ];
100
101 if ( animation ) {
102
103 this.setFrameRange( animation.start, animation.end );
104 this.duration = 1000 * ( ( animation.end - animation.start ) / fps );
105 this.time = 0;
106
107 } else {
108
109 console.warn( "animation[" + label + "] undefined" );
110
111 }
112
113};
114
115THREE.MorphAnimMesh.prototype.updateAnimation = function ( delta ) {
116
117 var frameTime = this.duration / this.length;
118
119 this.time += this.direction * delta;
120
121 if ( this.mirroredLoop ) {
122
123 if ( this.time > this.duration || this.time < 0 ) {
124
125 this.direction *= -1;
126
127 if ( this.time > this.duration ) {
128
129 this.time = this.duration;
130 this.directionBackwards = true;
131
132 }
133
134 if ( this.time < 0 ) {
135
136 this.time = 0;
137 this.directionBackwards = false;
138
139 }
140
141 }
142
143 } else {
144
145 this.time = this.time % this.duration;
146
147 if ( this.time < 0 ) this.time += this.duration;
148
149 }
150
151 var keyframe = this.startKeyframe + THREE.Math.clamp( Math.floor( this.time / frameTime ), 0, this.length - 1 );
152
153 if ( keyframe !== this.currentKeyframe ) {
154
155 this.morphTargetInfluences[ this.lastKeyframe ] = 0;
156 this.morphTargetInfluences[ this.currentKeyframe ] = 1;
157
158 this.morphTargetInfluences[ keyframe ] = 0;
159
160 this.lastKeyframe = this.currentKeyframe;
161 this.currentKeyframe = keyframe;
162
163 }
164
165 var mix = ( this.time % frameTime ) / frameTime;
166
167 if ( this.directionBackwards ) {
168
169 mix = 1 - mix;
170
171 }
172
173 this.morphTargetInfluences[ this.currentKeyframe ] = mix;
174 this.morphTargetInfluences[ this.lastKeyframe ] = 1 - mix;
175
176};
177
178THREE.MorphAnimMesh.prototype.clone = function ( object ) {
179
180 if ( object === undefined ) object = new THREE.MorphAnimMesh( this.geometry, this.material );
181
182 object.duration = this.duration;
183 object.mirroredLoop = this.mirroredLoop;
184 object.time = this.time;
185
186 object.lastKeyframe = this.lastKeyframe;
187 object.currentKeyframe = this.currentKeyframe;
188
189 object.direction = this.direction;
190 object.directionBackwards = this.directionBackwards;
191
192 THREE.Mesh.prototype.clone.call( this, object );
193
194 return object;
195
196};
Note: See TracBrowser for help on using the repository browser.