TimelinerController.js 5.4 KB

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