ModelAnimation.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*global define*/
  2. define([
  3. '../Core/defaultValue',
  4. '../Core/defineProperties',
  5. '../Core/Event',
  6. '../Core/JulianDate',
  7. './ModelAnimationLoop',
  8. './ModelAnimationState'
  9. ], function(
  10. defaultValue,
  11. defineProperties,
  12. Event,
  13. JulianDate,
  14. ModelAnimationLoop,
  15. ModelAnimationState) {
  16. "use strict";
  17. /**
  18. * An active glTF animation. A glTF asset can contain animations. An active animation
  19. * is an animation that is currently playing or scheduled to be played because it was
  20. * added to a model's {@link ModelAnimationCollection}. An active animation is an
  21. * instance of an animation; for example, there can be multiple active animations
  22. * for the same glTF animation, each with a different start time.
  23. * <p>
  24. * Create this by calling {@link ModelAnimationCollection#add}.
  25. * </p>
  26. *
  27. * @alias ModelAnimation
  28. * @internalConstructor
  29. *
  30. * @see ModelAnimationCollection#add
  31. */
  32. var ModelAnimation = function(options, model, runtimeAnimation) {
  33. this._name = options.name;
  34. this._startTime = JulianDate.clone(options.startTime);
  35. this._delay = defaultValue(options.delay, 0.0); // in seconds
  36. this._stopTime = options.stopTime;
  37. /**
  38. * When <code>true</code>, the animation is removed after it stops playing.
  39. * This is slightly more efficient that not removing it, but if, for example,
  40. * time is reversed, the animation is not played again.
  41. *
  42. * @type {Boolean}
  43. * @default false
  44. */
  45. this.removeOnStop = defaultValue(options.removeOnStop, false);
  46. this._speedup = defaultValue(options.speedup, 1.0);
  47. this._reverse = defaultValue(options.reverse, false);
  48. this._loop = defaultValue(options.loop, ModelAnimationLoop.NONE);
  49. /**
  50. * The event fired when this animation is started. This can be used, for
  51. * example, to play a sound or start a particle system, when the animation starts.
  52. * <p>
  53. * This event is fired at the end of the frame after the scene is rendered.
  54. * </p>
  55. *
  56. * @type {Event}
  57. * @default new Event()
  58. *
  59. * @example
  60. * animation.start.addEventListener(function(model, animation) {
  61. * console.log('Animation started: ' + animation.name);
  62. * });
  63. */
  64. this.start = new Event();
  65. /**
  66. * The event fired when on each frame when this animation is updated. The
  67. * current time of the animation, relative to the glTF animation time span, is
  68. * passed to the event, which allows, for example, starting new animations at a
  69. * specific time relative to a playing animation.
  70. * <p>
  71. * This event is fired at the end of the frame after the scene is rendered.
  72. * </p>
  73. *
  74. * @type {Event}
  75. * @default new Event()
  76. *
  77. * @example
  78. * animation.update.addEventListener(function(model, animation, time) {
  79. * console.log('Animation updated: ' + animation.name + '. glTF animation time: ' + time);
  80. * });
  81. */
  82. this.update = new Event();
  83. /**
  84. * The event fired when this animation is stopped. This can be used, for
  85. * example, to play a sound or start a particle system, when the animation stops.
  86. * <p>
  87. * This event is fired at the end of the frame after the scene is rendered.
  88. * </p>
  89. *
  90. * @type {Event}
  91. * @default new Event()
  92. *
  93. * @example
  94. * animation.stop.addEventListener(function(model, animation) {
  95. * console.log('Animation stopped: ' + animation.name);
  96. * });
  97. */
  98. this.stop = new Event();
  99. this._state = ModelAnimationState.STOPPED;
  100. this._runtimeAnimation = runtimeAnimation;
  101. // Set during animation update
  102. this._computedStartTime = undefined;
  103. this._duration = undefined;
  104. // To avoid allocations in ModelAnimationCollection.update
  105. var that = this;
  106. this._raiseStartEvent = function() {
  107. that.start.raiseEvent(model, that);
  108. };
  109. this._updateEventTime = 0.0;
  110. this._raiseUpdateEvent = function() {
  111. that.update.raiseEvent(model, that, that._updateEventTime);
  112. };
  113. this._raiseStopEvent = function() {
  114. that.stop.raiseEvent(model, that);
  115. };
  116. };
  117. defineProperties(ModelAnimation.prototype, {
  118. /**
  119. * The glTF animation name that identifies this animation.
  120. *
  121. * @memberof ModelAnimation.prototype
  122. *
  123. * @type {String}
  124. * @readonly
  125. */
  126. name : {
  127. get : function() {
  128. return this._name;
  129. }
  130. },
  131. /**
  132. * The scene time to start playing this animation. When this is <code>undefined</code>,
  133. * the animation starts at the next frame.
  134. *
  135. * @memberof ModelAnimation.prototype
  136. *
  137. * @type {JulianDate}
  138. * @readonly
  139. *
  140. * @default undefined
  141. */
  142. startTime : {
  143. get : function() {
  144. return this._startTime;
  145. }
  146. },
  147. /**
  148. * The delay, in seconds, from {@link ModelAnimation#startTime} to start playing.
  149. *
  150. * @memberof ModelAnimation.prototype
  151. *
  152. * @type {Number}
  153. * @readonly
  154. *
  155. * @default undefined
  156. */
  157. delay : {
  158. get : function() {
  159. return this._delay;
  160. }
  161. },
  162. /**
  163. * The scene time to stop playing this animation. When this is <code>undefined</code>,
  164. * the animation is played for its full duration and perhaps repeated depending on
  165. * {@link ModelAnimation#loop}.
  166. *
  167. * @memberof ModelAnimation.prototype
  168. *
  169. * @type {JulianDate}
  170. * @readonly
  171. *
  172. * @default undefined
  173. */
  174. stopTime : {
  175. get : function() {
  176. return this._stopTime;
  177. }
  178. },
  179. /**
  180. * Values greater than <code>1.0</code> increase the speed that the animation is played relative
  181. * to the scene clock speed; values less than <code>1.0</code> decrease the speed. A value of
  182. * <code>1.0</code> plays the animation at the speed in the glTF animation mapped to the scene
  183. * clock speed. For example, if the scene is played at 2x real-time, a two-second glTF animation
  184. * will play in one second even if <code>speedup</code> is <code>1.0</code>.
  185. *
  186. * @memberof ModelAnimation.prototype
  187. *
  188. * @type {Number}
  189. * @readonly
  190. *
  191. * @default 1.0
  192. */
  193. speedup : {
  194. get : function() {
  195. return this._speedup;
  196. }
  197. },
  198. /**
  199. * When <code>true</code>, the animation is played in reverse.
  200. *
  201. * @memberof ModelAnimation.prototype
  202. *
  203. * @type {Boolean}
  204. * @readonly
  205. *
  206. * @default false
  207. */
  208. reverse : {
  209. get : function() {
  210. return this._reverse;
  211. }
  212. },
  213. /**
  214. * Determines if and how the animation is looped.
  215. *
  216. * @memberof ModelAnimation.prototype
  217. *
  218. * @type {ModelAnimationLoop}
  219. * @readonly
  220. *
  221. * @default {@link ModelAnimationLoop.NONE}
  222. */
  223. loop : {
  224. get : function() {
  225. return this._loop;
  226. }
  227. }
  228. });
  229. return ModelAnimation;
  230. });