TimelinerController.js 5.4 KB

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