TimelinerController.js 5.6 KB

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