@@ -22,6 +22,7 @@ export class AnimationHelper {
22
22
const animations = json . animate ! ;
23
23
const kf = json . keyframes ! ;
24
24
const kfl = kf . length ;
25
+ const animationType = json . animateType ! ;
25
26
26
27
// this supports animations based on the position
27
28
// pseudo code:
@@ -42,7 +43,27 @@ export class AnimationHelper {
42
43
// this.pushAnimations('Action', kfl, [positionKF], three);
43
44
// ```
44
45
if ( json . type === JSON3DObject . SPHERES || json . type === JSON3DObject . CUBES ) {
45
- this . addAnimationForPosition ( animations , three , kf , kfl ) ;
46
+ const animation = json . animate ! ;
47
+ const p = json . positions ! [ 0 ] ;
48
+
49
+ const values : number [ ] = [ ] ;
50
+ if ( animationType == 'displacement' ) {
51
+ for ( let i = 0 ; i < kfl ; i ++ ) {
52
+ // VectorKeyframeTrack requires absolute positions relative to the current position
53
+ // i.e. displacemnt itself
54
+ values . push ( animation [ i ] [ 0 ] , animation [ i ] [ 1 ] , animation [ i ] [ 2 ] ) ;
55
+ }
56
+ } else if ( animationType == 'position' ) {
57
+ for ( let i = 0 ; i < kfl ; i ++ ) {
58
+ // Given an absolute position, we subtract the animation values to compute relative displacement
59
+ values . push ( animation [ i ] [ 0 ] - p [ 0 ] , animation [ i ] [ 1 ] - p [ 1 ] , animation [ i ] [ 2 ] - p [ 2 ] ) ;
60
+ }
61
+ } else {
62
+ console . warn ( `Unknown animationType: ${ animationType } ` ) ;
63
+ }
64
+
65
+ const positionKF = new THREE . VectorKeyframeTrack ( '.position' , [ ...kf ] , values ) ;
66
+ this . pushAnimations ( 'Action' , kfl , [ positionKF ] , three ) ;
46
67
} else if ( json . type === JSON3DObject . CYLINDERS ) {
47
68
animations . forEach ( ( animation , aIdx ) => {
48
69
// create cylinders from u to v
@@ -55,18 +76,30 @@ export class AnimationHelper {
55
76
let valuess : any [ ] = [ ] ;
56
77
57
78
for ( let i = 0 ; i < kfl ; i ++ ) {
58
- let target = [
59
- [
60
- u_position [ 0 ] + animation [ i ] [ 0 ] [ 0 ] ,
61
- u_position [ 1 ] + animation [ i ] [ 0 ] [ 1 ] ,
62
- u_position [ 2 ] + animation [ i ] [ 0 ] [ 2 ]
63
- ] ,
64
- [
65
- v_position [ 0 ] + animation [ i ] [ 1 ] [ 0 ] ,
66
- v_position [ 1 ] + animation [ i ] [ 1 ] [ 1 ] ,
67
- v_position [ 2 ] + animation [ i ] [ 1 ] [ 2 ]
68
- ]
69
- ] ;
79
+ let target ;
80
+ // The function `this.objectBuilder.getCylinderInfo` requires the actual positions
81
+ // of the atoms involved in the bond.
82
+ if ( animationType == 'displacement' ) {
83
+ target = [
84
+ [
85
+ u_position [ 0 ] + animation [ i ] [ 0 ] [ 0 ] ,
86
+ u_position [ 1 ] + animation [ i ] [ 0 ] [ 1 ] ,
87
+ u_position [ 2 ] + animation [ i ] [ 0 ] [ 2 ]
88
+ ] ,
89
+ [
90
+ v_position [ 0 ] + animation [ i ] [ 1 ] [ 0 ] ,
91
+ v_position [ 1 ] + animation [ i ] [ 1 ] [ 1 ] ,
92
+ v_position [ 2 ] + animation [ i ] [ 1 ] [ 2 ]
93
+ ]
94
+ ] ;
95
+ } else if ( animationType == 'position' ) {
96
+ target = [
97
+ [ animation [ i ] [ 0 ] [ 0 ] , animation [ i ] [ 0 ] [ 1 ] , animation [ i ] [ 0 ] [ 2 ] ] ,
98
+ [ animation [ i ] [ 1 ] [ 0 ] , animation [ i ] [ 1 ] [ 1 ] , animation [ i ] [ 1 ] [ 2 ] ]
99
+ ] ;
100
+ } else {
101
+ console . warn ( `Unknown animationType: ${ animationType } ` ) ;
102
+ }
70
103
71
104
const {
72
105
position : positionEnd ,
@@ -168,18 +201,26 @@ export class AnimationHelper {
168
201
}
169
202
}
170
203
171
- private addAnimationForPosition ( animation , three , kf : number [ ] , kfl : number ) {
172
- const values = this . calculateTargetPosition ( three , animation , kfl ) ;
204
+ private addAnimationForPosition ( animation , three , kf : number [ ] , kfl : number , animationType ) {
205
+ const values = this . calculateTargetPosition ( three , animation , kfl , animationType ) ;
173
206
const positionKF = new THREE . VectorKeyframeTrack ( '.position' , [ ...kf ] , values ) ;
174
207
this . pushAnimations ( 'Action' , kfl , [ positionKF ] , three ) ;
175
208
}
176
209
177
- private calculateTargetPosition ( { position } : THREE . Object3D , animation , kfl ) {
210
+ private calculateTargetPosition ( { position } : THREE . Object3D , animation , kfl , animationType ) {
178
211
// Iterate through all keyframes and construct a flattened array of their corresponding positions.
179
212
const p = [ position . x , position . y , position . z ] ;
180
213
const result : number [ ] = [ ] ;
181
- for ( let i = 0 ; i < kfl ; i ++ ) {
182
- result . push ( p [ 0 ] + animation [ i ] [ 0 ] , p [ 1 ] + animation [ i ] [ 1 ] , p [ 2 ] + animation [ i ] [ 2 ] ) ;
214
+ if ( animationType == 'displacement' ) {
215
+ for ( let i = 0 ; i < kfl ; i ++ ) {
216
+ result . push ( p [ 0 ] + animation [ i ] [ 0 ] , p [ 1 ] + animation [ i ] [ 1 ] , p [ 2 ] + animation [ i ] [ 2 ] ) ;
217
+ }
218
+ } else if ( animationType == 'position' ) {
219
+ for ( let i = 0 ; i < kfl ; i ++ ) {
220
+ result . push ( animation [ i ] [ 0 ] , animation [ i ] [ 1 ] , animation [ i ] [ 2 ] ) ;
221
+ }
222
+ } else {
223
+ console . warn ( `Unknown animationType: ${ animationType } ` ) ;
183
224
}
184
225
return result ;
185
226
}
0 commit comments