@@ -22,6 +22,7 @@ export class AnimationHelper {
2222 const animations = json . animate ! ;
2323 const kf = json . keyframes ! ;
2424 const kfl = kf . length ;
25+ const animationType = json . animateType ! ;
2526
2627 // this supports animations based on the position
2728 // pseudo code:
@@ -42,7 +43,27 @@ export class AnimationHelper {
4243 // this.pushAnimations('Action', kfl, [positionKF], three);
4344 // ```
4445 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 ) ;
4667 } else if ( json . type === JSON3DObject . CYLINDERS ) {
4768 animations . forEach ( ( animation , aIdx ) => {
4869 // create cylinders from u to v
@@ -55,18 +76,30 @@ export class AnimationHelper {
5576 let valuess : any [ ] = [ ] ;
5677
5778 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+ }
70103
71104 const {
72105 position : positionEnd ,
@@ -168,18 +201,26 @@ export class AnimationHelper {
168201 }
169202 }
170203
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 ) ;
173206 const positionKF = new THREE . VectorKeyframeTrack ( '.position' , [ ...kf ] , values ) ;
174207 this . pushAnimations ( 'Action' , kfl , [ positionKF ] , three ) ;
175208 }
176209
177- private calculateTargetPosition ( { position } : THREE . Object3D , animation , kfl ) {
210+ private calculateTargetPosition ( { position } : THREE . Object3D , animation , kfl , animationType ) {
178211 // Iterate through all keyframes and construct a flattened array of their corresponding positions.
179212 const p = [ position . x , position . y , position . z ] ;
180213 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 } ` ) ;
183224 }
184225 return result ;
185226 }
0 commit comments