TimelinerController.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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( timeliner ) {
  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(
  35. spec.type, spec.propertyPath,
  36. spec.initialValue, spec.interpolation ) );
  37. }
  38. this._clip = new THREE.AnimationClip( 'editclip', 0, tracks );
  39. this._action = this._mixer.clipAction( this._clip ).play();
  40. },
  41. setDisplayTime: function( time ) {
  42. this._action.time = time;
  43. this._mixer.update( 0 );
  44. this._onUpdate();
  45. },
  46. setDuration: function( duration ) {
  47. this._clip.duration = duration;
  48. },
  49. getChannelNames: function() {
  50. return this._channelNames;
  51. },
  52. getChannelKeyTimes: function( channelName ) {
  53. return this._tracks[ channelName ].times;
  54. },
  55. setKeyframe: function( channelName, time ) {
  56. var track = this._tracks[ channelName ],
  57. times = track.times,
  58. index = times.indexOf( time );
  59. if ( index === -1 ) index = times.length;
  60. var values = track.values,
  61. offset = index * track.getValueSize();
  62. // note: not calling track.getValueSize with
  63. // inconsistent array sizes
  64. times[ index ] = time;
  65. this._propRefs[ channelName ].getValue( values, offset );
  66. this._sort( track );
  67. },
  68. delKeyframe: function( channelName, time ) {
  69. var track = this._tracks[ channelName ],
  70. times = track.times,
  71. index = times.indexOf( time );
  72. if ( index === 0 ) {
  73. // we disallow to remove the first keyframe
  74. // since the animation system is designed to
  75. // always produce a defined state - we allow
  76. // the initial state to be changed however
  77. this.setKeyframe( channelName, time );
  78. } else if ( index !== -1 ) {
  79. var values = track.values,
  80. stride = track.getValueSize(),
  81. offset = index * stride,
  82. nValues = values.length;
  83. // note: not calling track.getValueSize with
  84. // inconsistent array sizes
  85. times[ index ] = times[ times.length - 1 ];
  86. times.pop();
  87. for ( var i = nValues - stride; i !== nValues; ++ i ) {
  88. values[ offset ++ ] = values[ i ];
  89. }
  90. values.length = nValues - stride;
  91. this._sort( track );
  92. }
  93. },
  94. hasKeyframe: function( channelName, time ) {
  95. var track = this._tracks[ channelName ],
  96. times = track.times,
  97. index = times.indexOf( time );
  98. return index !== -1;
  99. },
  100. serialize: function() {
  101. var result = {
  102. duration: this._clip.duration,
  103. channels: {}
  104. },
  105. names = this._channelNames,
  106. tracks = this._tracks,
  107. channels = result.channels;
  108. for ( var i = 0, n = names.length; i !== n; ++ i ) {
  109. var name = names[ i ],
  110. track = tracks[ name ];
  111. channels[ name ] = {
  112. times: track.times,
  113. values: track.values
  114. };
  115. }
  116. return result;
  117. },
  118. deserialize: function( structs ) {
  119. var names = this._channelNames,
  120. tracks = this._tracks,
  121. channels = structs.channels;
  122. this.setDuration( structs.duration );
  123. for ( var i = 0, n = names.length; i !== n; ++ i ) {
  124. var name = names[ i ],
  125. track = tracks[ name ];
  126. data = channels[ name ];
  127. this._setArray( track.times, data.times );
  128. this._setArray( track.values, data.values );
  129. }
  130. // update display
  131. this.setDisplayTime( this._mixer.time );
  132. },
  133. _sort: function( track ) {
  134. var times = track.times,
  135. order = THREE.AnimationUtils.getKeyframeOrder( times );
  136. this._setArray( times,
  137. THREE.AnimationUtils.sortedArray( times, 1, order ) );
  138. var values = track.values,
  139. stride = track.getValueSize();
  140. this._setArray( values,
  141. THREE.AnimationUtils.sortedArray( values, stride, order ) );
  142. },
  143. _setArray: function( dst, src ) {
  144. dst.length = 0;
  145. dst.push.apply( dst, src );
  146. },
  147. _addTrack: function( type, prop, initialValue, interpolation ) {
  148. var track = new type(
  149. prop, [ 0 ], initialValue, interpolation );
  150. // data must be in JS arrays so it can be resized
  151. track.times = Array.prototype.slice.call( track.times );
  152. track.values = Array.prototype.slice.call( track.values );
  153. this._channelNames.push( prop );
  154. this._tracks[ prop ] = track;
  155. // for recording the state:
  156. this._propRefs[ prop ] =
  157. new THREE.PropertyBinding( this._scene, prop );
  158. return track;
  159. }
  160. };