modelAnimation.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import { AnimationGroup, Animatable, Skeleton, Vector3 } from "babylonjs";
  2. /**
  3. * Animation play mode enum - is the animation looping or playing once
  4. */
  5. export const enum AnimationPlayMode {
  6. ONCE,
  7. LOOP
  8. }
  9. /**
  10. * An enum representing the current state of an animation object
  11. */
  12. export const enum AnimationState {
  13. INIT,
  14. PLAYING,
  15. PAUSED,
  16. STOPPED,
  17. ENDED
  18. }
  19. /**
  20. * The different type of easing functions available
  21. */
  22. export const enum EasingFunction {
  23. Linear = 0,
  24. CircleEase = 1,
  25. BackEase = 2,
  26. BounceEase = 3,
  27. CubicEase = 4,
  28. ElasticEase = 5,
  29. ExponentialEase = 6,
  30. PowerEase = 7,
  31. QuadraticEase = 8,
  32. QuarticEase = 9,
  33. QuinticEase = 10,
  34. SineEase = 11
  35. }
  36. /**
  37. * Defines a simple animation to be applied to a model (scale).
  38. */
  39. export interface ModelAnimationConfiguration {
  40. /**
  41. * Time of animation, in seconds
  42. */
  43. time: number;
  44. /**
  45. * Scale to apply
  46. */
  47. scaling?: Vector3;
  48. /**
  49. * Easing function to apply
  50. * See SPECTRE.EasingFunction
  51. */
  52. easingFunction?: number;
  53. /**
  54. * An Easing mode to apply to the easing function
  55. * See BABYLON.EasingFunction
  56. */
  57. easingMode?: number;
  58. }
  59. /**
  60. * This interface can be implemented to define new types of ModelAnimation objects.
  61. */
  62. export interface IModelAnimation {
  63. /**
  64. * Current animation state (playing, stopped etc')
  65. */
  66. readonly state: AnimationState;
  67. /**
  68. * the name of the animation
  69. */
  70. readonly name: string;
  71. /**
  72. * Get the max numbers of frame available in the animation group
  73. *
  74. * In correlation to an arry, this would be ".length"
  75. */
  76. readonly frames: number;
  77. /**
  78. * Get the current frame playing right now.
  79. * This can be used to poll the frame currently playing (and, for exmaple, display a progress bar with the data)
  80. *
  81. * In correlation to an array, this would be the current index
  82. */
  83. readonly currentFrame: number;
  84. /**
  85. * Animation's FPS value
  86. */
  87. readonly fps: number;
  88. /**
  89. * Get or set the animation's speed ration (Frame-to-fps)
  90. */
  91. speedRatio: number;
  92. /**
  93. * Gets or sets the aimation's play mode.
  94. */
  95. playMode: AnimationPlayMode;
  96. /**
  97. * Start the animation
  98. */
  99. start();
  100. /**
  101. * Stop the animation.
  102. * This will fail silently if the animation group is already stopped.
  103. */
  104. stop();
  105. /**
  106. * Pause the animation
  107. * This will fail silently if the animation is not currently playing
  108. */
  109. pause();
  110. /**
  111. * Reset this animation
  112. */
  113. reset();
  114. /**
  115. * Restart the animation
  116. */
  117. restart();
  118. /**
  119. * Go to a specific
  120. * @param frameNumber the frame number to go to
  121. */
  122. goToFrame(frameNumber: number);
  123. /**
  124. * Dispose this animation
  125. */
  126. dispose();
  127. }
  128. /**
  129. * The GroupModelAnimation is an implementation of the IModelAnimation interface using BABYLON's
  130. * native GroupAnimation class.
  131. */
  132. export class GroupModelAnimation implements IModelAnimation {
  133. private _playMode: AnimationPlayMode;
  134. private _state: AnimationState;
  135. /**
  136. * Create a new GroupModelAnimation object using an AnimationGroup object
  137. * @param _animationGroup The aniamtion group to base the class on
  138. */
  139. constructor(private _animationGroup: AnimationGroup) {
  140. this._state = AnimationState.INIT;
  141. this._playMode = AnimationPlayMode.LOOP;
  142. this._animationGroup.onAnimationEndObservable.add(() => {
  143. this.stop();
  144. this._state = AnimationState.ENDED;
  145. });
  146. }
  147. /**
  148. * Get the animation's name
  149. */
  150. public get name() {
  151. return this._animationGroup.name;
  152. }
  153. /**
  154. * Get the current animation's state
  155. */
  156. public get state() {
  157. return this._state;
  158. }
  159. /**
  160. * Gets the speed ratio to use for all animations
  161. */
  162. public get speedRatio(): number {
  163. return this._animationGroup.speedRatio;
  164. }
  165. /**
  166. * Sets the speed ratio to use for all animations
  167. */
  168. public set speedRatio(value: number) {
  169. this._animationGroup.speedRatio = value;
  170. }
  171. /**
  172. * Get the max numbers of frame available in the animation group
  173. *
  174. * In correlation to an arry, this would be ".length"
  175. */
  176. public get frames(): number {
  177. /*let animationFrames = this._animationGroup.targetedAnimations.map(ta => {
  178. let keys = ta.animation.getKeys();
  179. return keys[keys.length - 1].frame;
  180. });
  181. return Math.max.apply(null, animationFrames);*/
  182. return this._animationGroup.to - this._animationGroup.from;
  183. }
  184. /**
  185. * Get the current frame playing right now.
  186. * This can be used to poll the frame currently playing (and, for exmaple, display a progress bar with the data)
  187. *
  188. * In correlation to an array, this would be the current index
  189. */
  190. public get currentFrame(): number {
  191. // get the first currentFrame found
  192. /*for (let i = 0; i < this._animationGroup.animatables.length; ++i) {
  193. let animatable: Animatable = this._animationGroup.animatables[i];
  194. let animations = animatable.getAnimations();
  195. if (!animations || !animations.length) {
  196. continue;
  197. }
  198. for (let idx = 0; idx < animations.length; ++idx) {
  199. if (animations[idx].currentFrame) {
  200. return animations[idx].currentFrame;
  201. }
  202. }
  203. }*/
  204. if (this._animationGroup.targetedAnimations[0] && this._animationGroup.targetedAnimations[0].animation.runtimeAnimations[0]) {
  205. return this._animationGroup.targetedAnimations[0].animation.runtimeAnimations[0].currentFrame - this._animationGroup.from;
  206. } else {
  207. return 0;
  208. }
  209. }
  210. /**
  211. * Get the FPS value of this animation
  212. */
  213. public get fps(): number {
  214. // get the first currentFrame found
  215. for (let i = 0; i < this._animationGroup.animatables.length; ++i) {
  216. let animatable: Animatable = this._animationGroup.animatables[i];
  217. let animations = animatable.getAnimations();
  218. if (!animations || !animations.length) {
  219. continue;
  220. }
  221. for (let idx = 0; idx < animations.length; ++idx) {
  222. if (animations[idx].animation && animations[idx].animation.framePerSecond) {
  223. return animations[idx].animation.framePerSecond;
  224. }
  225. }
  226. }
  227. return 0;
  228. }
  229. /**
  230. * What is the animation'S play mode (looping or played once)
  231. */
  232. public get playMode(): AnimationPlayMode {
  233. return this._playMode;
  234. }
  235. /**
  236. * Set the play mode.
  237. * If the animation is played, it will continue playing at least once more, depending on the new play mode set.
  238. * If the animation is not set, the will be initialized and will wait for the user to start playing it.
  239. */
  240. public set playMode(value: AnimationPlayMode) {
  241. if (value === this._playMode) {
  242. return;
  243. }
  244. this._playMode = value;
  245. if (this.state === AnimationState.PLAYING) {
  246. this._animationGroup.play(this._playMode === AnimationPlayMode.LOOP);
  247. } else {
  248. this._animationGroup.reset();
  249. this._state = AnimationState.INIT;
  250. }
  251. }
  252. /**
  253. * Reset the animation group
  254. */
  255. reset() {
  256. this._animationGroup.reset();
  257. }
  258. /**
  259. * Restart the animation group
  260. */
  261. restart() {
  262. if (this.state === AnimationState.PAUSED)
  263. this._animationGroup.restart();
  264. else
  265. this.start();
  266. }
  267. /**
  268. *
  269. * @param frameNumber Go to a specific frame in the animation
  270. */
  271. goToFrame(frameNumber: number) {
  272. this._animationGroup.goToFrame(frameNumber + this._animationGroup.from);
  273. }
  274. /**
  275. * Start playing the animation.
  276. */
  277. public start() {
  278. this._animationGroup.start(this.playMode === AnimationPlayMode.LOOP, this.speedRatio);
  279. if (this._animationGroup.isStarted) {
  280. this._state = AnimationState.PLAYING;
  281. }
  282. }
  283. /**
  284. * Pause the animation
  285. */
  286. pause() {
  287. this._animationGroup.pause();
  288. this._state = AnimationState.PAUSED;
  289. }
  290. /**
  291. * Stop the animation.
  292. * This will fail silently if the animation group is already stopped.
  293. */
  294. public stop() {
  295. this._animationGroup.stop();
  296. if (!this._animationGroup.isStarted) {
  297. this._state = AnimationState.STOPPED;
  298. }
  299. }
  300. /**
  301. * Dispose this animation object.
  302. */
  303. public dispose() {
  304. this._animationGroup.dispose();
  305. }
  306. }