TimelinerController.js 5.6 KB

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