TimelinerController.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * Controller class for the Timeliner GUI.
  3. *
  4. * Timeliner GUI library (required to use this class):
  5. *
  6. * ../libs/timeliner_gui.min.js
  7. *
  8. * Source code:
  9. *
  10. * https://github.com/tschw/timeliner_gui
  11. * https://github.com/zz85/timeliner (fork's origin)
  12. *
  13. * @author tschw
  14. *
  15. */
  16. THREE.TimelinerController = function TimelinerController( scene, trackInfo, onUpdate ) {
  17. this._scene = scene;
  18. this._trackInfo = trackInfo;
  19. this._onUpdate = onUpdate;
  20. this._mixer = new THREE.AnimationMixer( scene );
  21. this._clip = null;
  22. this._action = null;
  23. this._tracks = {};
  24. this._propRefs = {};
  25. this._channelNames = [];
  26. };
  27. THREE.TimelinerController.prototype = {
  28. constructor: THREE.TimelinerController,
  29. init: function () {
  30. var tracks = [],
  31. trackInfo = this._trackInfo;
  32. for ( var i = 0, n = trackInfo.length; i !== n; ++ i ) {
  33. var spec = trackInfo[ i ];
  34. tracks.push( this._addTrack( spec.type, spec.propertyPath, spec.initialValue, spec.interpolation ) );
  35. }
  36. this._clip = new THREE.AnimationClip( 'editclip', 0, tracks );
  37. this._action = this._mixer.clipAction( this._clip ).play();
  38. },
  39. setDisplayTime: function ( time ) {
  40. this._action.time = time;
  41. this._mixer.update( 0 );
  42. this._onUpdate();
  43. },
  44. setDuration: function ( duration ) {
  45. this._clip.duration = duration;
  46. },
  47. getChannelNames: function () {
  48. return this._channelNames;
  49. },
  50. getChannelKeyTimes: function ( channelName ) {
  51. return this._tracks[ channelName ].times;
  52. },
  53. setKeyframe: function ( channelName, time ) {
  54. var track = this._tracks[ channelName ],
  55. times = track.times,
  56. index = Timeliner.binarySearch( times, time ),
  57. values = track.values,
  58. stride = track.getValueSize(),
  59. offset = index * stride;
  60. if ( index < 0 ) {
  61. // insert new keyframe
  62. index = ~ index;
  63. offset = index * stride;
  64. var nTimes = times.length + 1,
  65. nValues = values.length + stride;
  66. for ( var i = nTimes - 1; i !== index; -- i ) {
  67. times[ i ] = times[ i - 1 ];
  68. }
  69. for ( var i = nValues - 1, e = offset + stride - 1; i !== e; -- i ) {
  70. values[ i ] = values[ i - stride ];
  71. }
  72. }
  73. times[ index ] = time;
  74. this._propRefs[ channelName ].getValue( values, offset );
  75. },
  76. delKeyframe: function ( channelName, time ) {
  77. var track = this._tracks[ channelName ],
  78. times = track.times,
  79. index = Timeliner.binarySearch( times, time );
  80. // we disallow to remove the keyframe when it is the last one we have,
  81. // since the animation system is designed to always produce a defined
  82. // state
  83. if ( times.length > 1 && index >= 0 ) {
  84. var nTimes = times.length - 1,
  85. values = track.values,
  86. stride = track.getValueSize(),
  87. nValues = values.length - stride;
  88. // note: no track.getValueSize when array sizes are out of sync
  89. for ( var i = index; i !== nTimes; ++ i ) {
  90. times[ i ] = times[ i + 1 ];
  91. }
  92. times.pop();
  93. for ( var offset = index * stride; offset !== nValues; ++ offset ) {
  94. values[ offset ] = values[ offset + stride ];
  95. }
  96. values.length = nValues;
  97. }
  98. },
  99. moveKeyframe: function ( channelName, time, delta, moveRemaining ) {
  100. var track = this._tracks[ channelName ],
  101. times = track.times,
  102. index = Timeliner.binarySearch( times, time );
  103. if ( index >= 0 ) {
  104. var endAt = moveRemaining ? times.length : index + 1,
  105. needsSort = times[ index - 1 ] <= time ||
  106. ! moveRemaining && time >= times[ index + 1 ];
  107. while ( index !== endAt ) times[ index ++ ] += delta;
  108. if ( needsSort ) this._sort( track );
  109. }
  110. },
  111. serialize: function () {
  112. var result = {
  113. duration: this._clip.duration,
  114. channels: {}
  115. },
  116. names = this._channelNames,
  117. tracks = this._tracks,
  118. channels = result.channels;
  119. for ( var i = 0, n = names.length; i !== n; ++ i ) {
  120. var name = names[ i ],
  121. track = tracks[ name ];
  122. channels[ name ] = {
  123. times: track.times,
  124. values: track.values
  125. };
  126. }
  127. return result;
  128. },
  129. deserialize: function ( structs ) {
  130. var names = this._channelNames,
  131. tracks = this._tracks,
  132. channels = structs.channels;
  133. this.setDuration( structs.duration );
  134. for ( var i = 0, n = names.length; i !== n; ++ i ) {
  135. var name = names[ i ],
  136. track = tracks[ name ],
  137. data = channels[ name ];
  138. this._setArray( track.times, data.times );
  139. this._setArray( track.values, data.values );
  140. }
  141. // update display
  142. this.setDisplayTime( this._mixer.time );
  143. },
  144. _sort: function ( track ) {
  145. var times = track.times, order = THREE.AnimationUtils.getKeyframeOrder( times );
  146. this._setArray( times, THREE.AnimationUtils.sortedArray( times, 1, order ) );
  147. var values = track.values,
  148. stride = track.getValueSize();
  149. this._setArray( values, THREE.AnimationUtils.sortedArray( values, stride, order ) );
  150. },
  151. _setArray: function ( dst, src ) {
  152. dst.length = 0;
  153. dst.push.apply( dst, src );
  154. },
  155. _addTrack: function ( type, prop, initialValue, interpolation ) {
  156. var track = new type( prop, [ 0 ], initialValue, interpolation );
  157. // data must be in JS arrays so it can be resized
  158. track.times = Array.prototype.slice.call( track.times );
  159. track.values = Array.prototype.slice.call( track.values );
  160. this._channelNames.push( prop );
  161. this._tracks[ prop ] = track;
  162. // for recording the state:
  163. this._propRefs[ prop ] =
  164. new THREE.PropertyBinding( this._scene, prop );
  165. return track;
  166. }
  167. };