animatable.d.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import { Animation } from "./animation";
  2. import { RuntimeAnimation } from "./runtimeAnimation";
  3. import type { Nullable } from "../types";
  4. import { Observable } from "../Misc/observable";
  5. import { Scene } from "../scene";
  6. import { Matrix, Quaternion, Vector3 } from "../Maths/math.vector";
  7. import type { Node } from "../node";
  8. /**
  9. * Class used to store an actual running animation
  10. */
  11. export declare class Animatable {
  12. /** defines the target object */
  13. target: any;
  14. /** defines the starting frame number (default is 0) */
  15. fromFrame: number;
  16. /** defines the ending frame number (default is 100) */
  17. toFrame: number;
  18. /** defines if the animation must loop (default is false) */
  19. loopAnimation: boolean;
  20. /** defines a callback to call when animation ends if it is not looping */
  21. onAnimationEnd?: Nullable<() => void> | undefined;
  22. /** defines a callback to call when animation loops */
  23. onAnimationLoop?: Nullable<() => void> | undefined;
  24. /** defines whether the animation should be evaluated additively */
  25. isAdditive: boolean;
  26. /** defines the order in which this animatable should be processed in the list of active animatables (default: 0) */
  27. playOrder: number;
  28. private _localDelayOffset;
  29. private _pausedDelay;
  30. private _manualJumpDelay;
  31. /** @hidden */
  32. _runtimeAnimations: RuntimeAnimation[];
  33. private _paused;
  34. private _scene;
  35. private _speedRatio;
  36. private _weight;
  37. private _syncRoot;
  38. private _frameToSyncFromJump;
  39. private _goToFrame;
  40. /**
  41. * Gets or sets a boolean indicating if the animatable must be disposed and removed at the end of the animation.
  42. * This will only apply for non looping animation (default is true)
  43. */
  44. disposeOnEnd: boolean;
  45. /**
  46. * Gets a boolean indicating if the animation has started
  47. */
  48. animationStarted: boolean;
  49. /**
  50. * Observer raised when the animation ends
  51. */
  52. onAnimationEndObservable: Observable<Animatable>;
  53. /**
  54. * Observer raised when the animation loops
  55. */
  56. onAnimationLoopObservable: Observable<Animatable>;
  57. /**
  58. * Gets the root Animatable used to synchronize and normalize animations
  59. */
  60. get syncRoot(): Nullable<Animatable>;
  61. /**
  62. * Gets the current frame of the first RuntimeAnimation
  63. * Used to synchronize Animatables
  64. */
  65. get masterFrame(): number;
  66. /**
  67. * Gets or sets the animatable weight (-1.0 by default meaning not weighted)
  68. */
  69. get weight(): number;
  70. set weight(value: number);
  71. /**
  72. * Gets or sets the speed ratio to apply to the animatable (1.0 by default)
  73. */
  74. get speedRatio(): number;
  75. set speedRatio(value: number);
  76. /**
  77. * Gets the elapsed time since the animatable started in milliseconds
  78. */
  79. get elapsedTime(): number;
  80. /**
  81. * Creates a new Animatable
  82. * @param scene defines the hosting scene
  83. * @param target defines the target object
  84. * @param fromFrame defines the starting frame number (default is 0)
  85. * @param toFrame defines the ending frame number (default is 100)
  86. * @param loopAnimation defines if the animation must loop (default is false)
  87. * @param speedRatio defines the factor to apply to animation speed (default is 1)
  88. * @param onAnimationEnd defines a callback to call when animation ends if it is not looping
  89. * @param animations defines a group of animation to add to the new Animatable
  90. * @param onAnimationLoop defines a callback to call when animation loops
  91. * @param isAdditive defines whether the animation should be evaluated additively
  92. * @param playOrder defines the order in which this animatable should be processed in the list of active animatables (default: 0)
  93. */
  94. constructor(scene: Scene,
  95. /** defines the target object */
  96. target: any,
  97. /** defines the starting frame number (default is 0) */
  98. fromFrame?: number,
  99. /** defines the ending frame number (default is 100) */
  100. toFrame?: number,
  101. /** defines if the animation must loop (default is false) */
  102. loopAnimation?: boolean, speedRatio?: number,
  103. /** defines a callback to call when animation ends if it is not looping */
  104. onAnimationEnd?: Nullable<() => void> | undefined, animations?: Animation[],
  105. /** defines a callback to call when animation loops */
  106. onAnimationLoop?: Nullable<() => void> | undefined,
  107. /** defines whether the animation should be evaluated additively */
  108. isAdditive?: boolean,
  109. /** defines the order in which this animatable should be processed in the list of active animatables (default: 0) */
  110. playOrder?: number);
  111. /**
  112. * Synchronize and normalize current Animatable with a source Animatable
  113. * This is useful when using animation weights and when animations are not of the same length
  114. * @param root defines the root Animatable to synchronize with (null to stop synchronizing)
  115. * @returns the current Animatable
  116. */
  117. syncWith(root: Nullable<Animatable>): Animatable;
  118. /**
  119. * Gets the list of runtime animations
  120. * @returns an array of RuntimeAnimation
  121. */
  122. getAnimations(): RuntimeAnimation[];
  123. /**
  124. * Adds more animations to the current animatable
  125. * @param target defines the target of the animations
  126. * @param animations defines the new animations to add
  127. */
  128. appendAnimations(target: any, animations: Animation[]): void;
  129. /**
  130. * Gets the source animation for a specific property
  131. * @param property defines the property to look for
  132. * @returns null or the source animation for the given property
  133. */
  134. getAnimationByTargetProperty(property: string): Nullable<Animation>;
  135. /**
  136. * Gets the runtime animation for a specific property
  137. * @param property defines the property to look for
  138. * @returns null or the runtime animation for the given property
  139. */
  140. getRuntimeAnimationByTargetProperty(property: string): Nullable<RuntimeAnimation>;
  141. /**
  142. * Resets the animatable to its original state
  143. */
  144. reset(): void;
  145. /**
  146. * Allows the animatable to blend with current running animations
  147. * @see https://doc.babylonjs.com/features/featuresDeepDive/animation/advanced_animations#animation-blending
  148. * @param blendingSpeed defines the blending speed to use
  149. */
  150. enableBlending(blendingSpeed: number): void;
  151. /**
  152. * Disable animation blending
  153. * @see https://doc.babylonjs.com/features/featuresDeepDive/animation/advanced_animations#animation-blending
  154. */
  155. disableBlending(): void;
  156. /**
  157. * Jump directly to a given frame
  158. * @param frame defines the frame to jump to
  159. */
  160. goToFrame(frame: number): void;
  161. /**
  162. * Returns true if the animations for this animatable are paused
  163. */
  164. get paused(): boolean;
  165. /**
  166. * Pause the animation
  167. */
  168. pause(): void;
  169. /**
  170. * Restart the animation
  171. */
  172. restart(): void;
  173. private _raiseOnAnimationEnd;
  174. /**
  175. * Stop and delete the current animation
  176. * @param animationName defines a string used to only stop some of the runtime animations instead of all
  177. * @param targetMask a function that determines if the animation should be stopped based on its target (all animations will be stopped if both this and animationName are empty)
  178. * @param useGlobalSplice if true, the animatables will be removed by the caller of this function (false by default)
  179. */
  180. stop(animationName?: string, targetMask?: (target: any) => boolean, useGlobalSplice?: boolean): void;
  181. /**
  182. * Wait asynchronously for the animation to end
  183. * @returns a promise which will be fulfilled when the animation ends
  184. */
  185. waitAsync(): Promise<Animatable>;
  186. /**
  187. * @internal
  188. */
  189. _animate(delay: number): boolean;
  190. }
  191. declare module "../scene" {
  192. interface Scene {
  193. /** @internal */
  194. _registerTargetForLateAnimationBinding(runtimeAnimation: RuntimeAnimation, originalValue: any): void;
  195. /** @internal */
  196. _processLateAnimationBindingsForMatrices(holder: {
  197. totalWeight: number;
  198. totalAdditiveWeight: number;
  199. animations: RuntimeAnimation[];
  200. additiveAnimations: RuntimeAnimation[];
  201. originalValue: Matrix;
  202. }): any;
  203. /** @internal */
  204. _processLateAnimationBindingsForQuaternions(holder: {
  205. totalWeight: number;
  206. totalAdditiveWeight: number;
  207. animations: RuntimeAnimation[];
  208. additiveAnimations: RuntimeAnimation[];
  209. originalValue: Quaternion;
  210. }, refQuaternion: Quaternion): Quaternion;
  211. /** @internal */
  212. _processLateAnimationBindings(): void;
  213. /**
  214. * Sort active animatables based on their playOrder property
  215. */
  216. sortActiveAnimatables(): void;
  217. /**
  218. * Will start the animation sequence of a given target
  219. * @param target defines the target
  220. * @param from defines from which frame should animation start
  221. * @param to defines until which frame should animation run.
  222. * @param weight defines the weight to apply to the animation (1.0 by default)
  223. * @param loop defines if the animation loops
  224. * @param speedRatio defines the speed in which to run the animation (1.0 by default)
  225. * @param onAnimationEnd defines the function to be executed when the animation ends
  226. * @param animatable defines an animatable object. If not provided a new one will be created from the given params
  227. * @param targetMask defines if the target should be animated if animations are present (this is called recursively on descendant animatables regardless of return value)
  228. * @param onAnimationLoop defines the callback to call when an animation loops
  229. * @param isAdditive defines whether the animation should be evaluated additively (false by default)
  230. * @returns the animatable object created for this animation
  231. */
  232. beginWeightedAnimation(target: any, from: number, to: number, weight: number, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void, animatable?: Animatable, targetMask?: (target: any) => boolean, onAnimationLoop?: () => void, isAdditive?: boolean): Animatable;
  233. /**
  234. * Will start the animation sequence of a given target
  235. * @param target defines the target
  236. * @param from defines from which frame should animation start
  237. * @param to defines until which frame should animation run.
  238. * @param loop defines if the animation loops
  239. * @param speedRatio defines the speed in which to run the animation (1.0 by default)
  240. * @param onAnimationEnd defines the function to be executed when the animation ends
  241. * @param animatable defines an animatable object. If not provided a new one will be created from the given params
  242. * @param stopCurrent defines if the current animations must be stopped first (true by default)
  243. * @param targetMask defines if the target should be animate if animations are present (this is called recursively on descendant animatables regardless of return value)
  244. * @param onAnimationLoop defines the callback to call when an animation loops
  245. * @param isAdditive defines whether the animation should be evaluated additively (false by default)
  246. * @returns the animatable object created for this animation
  247. */
  248. beginAnimation(target: any, from: number, to: number, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void, animatable?: Animatable, stopCurrent?: boolean, targetMask?: (target: any) => boolean, onAnimationLoop?: () => void, isAdditive?: boolean): Animatable;
  249. /**
  250. * Will start the animation sequence of a given target and its hierarchy
  251. * @param target defines the target
  252. * @param directDescendantsOnly if true only direct descendants will be used, if false direct and also indirect (children of children, an so on in a recursive manner) descendants will be used.
  253. * @param from defines from which frame should animation start
  254. * @param to defines until which frame should animation run.
  255. * @param loop defines if the animation loops
  256. * @param speedRatio defines the speed in which to run the animation (1.0 by default)
  257. * @param onAnimationEnd defines the function to be executed when the animation ends
  258. * @param animatable defines an animatable object. If not provided a new one will be created from the given params
  259. * @param stopCurrent defines if the current animations must be stopped first (true by default)
  260. * @param targetMask defines if the target should be animated if animations are present (this is called recursively on descendant animatables regardless of return value)
  261. * @param onAnimationLoop defines the callback to call when an animation loops
  262. * @param isAdditive defines whether the animation should be evaluated additively (false by default)
  263. * @returns the list of created animatables
  264. */
  265. beginHierarchyAnimation(target: any, directDescendantsOnly: boolean, from: number, to: number, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void, animatable?: Animatable, stopCurrent?: boolean, targetMask?: (target: any) => boolean, onAnimationLoop?: () => void, isAdditive?: boolean): Animatable[];
  266. /**
  267. * Begin a new animation on a given node
  268. * @param target defines the target where the animation will take place
  269. * @param animations defines the list of animations to start
  270. * @param from defines the initial value
  271. * @param to defines the final value
  272. * @param loop defines if you want animation to loop (off by default)
  273. * @param speedRatio defines the speed ratio to apply to all animations
  274. * @param onAnimationEnd defines the callback to call when an animation ends (will be called once per node)
  275. * @param onAnimationLoop defines the callback to call when an animation loops
  276. * @param isAdditive defines whether the animation should be evaluated additively (false by default)
  277. * @returns the list of created animatables
  278. */
  279. beginDirectAnimation(target: any, animations: Animation[], from: number, to: number, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void, onAnimationLoop?: () => void, isAdditive?: boolean): Animatable;
  280. /**
  281. * Begin a new animation on a given node and its hierarchy
  282. * @param target defines the root node where the animation will take place
  283. * @param directDescendantsOnly if true only direct descendants will be used, if false direct and also indirect (children of children, an so on in a recursive manner) descendants will be used.
  284. * @param animations defines the list of animations to start
  285. * @param from defines the initial value
  286. * @param to defines the final value
  287. * @param loop defines if you want animation to loop (off by default)
  288. * @param speedRatio defines the speed ratio to apply to all animations
  289. * @param onAnimationEnd defines the callback to call when an animation ends (will be called once per node)
  290. * @param onAnimationLoop defines the callback to call when an animation loops
  291. * @param isAdditive defines whether the animation should be evaluated additively (false by default)
  292. * @returns the list of animatables created for all nodes
  293. */
  294. beginDirectHierarchyAnimation(target: Node, directDescendantsOnly: boolean, animations: Animation[], from: number, to: number, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void, onAnimationLoop?: () => void, isAdditive?: boolean): Animatable[];
  295. /**
  296. * Gets the animatable associated with a specific target
  297. * @param target defines the target of the animatable
  298. * @returns the required animatable if found
  299. */
  300. getAnimatableByTarget(target: any): Nullable<Animatable>;
  301. /**
  302. * Gets all animatables associated with a given target
  303. * @param target defines the target to look animatables for
  304. * @returns an array of Animatables
  305. */
  306. getAllAnimatablesByTarget(target: any): Array<Animatable>;
  307. /**
  308. * Stops and removes all animations that have been applied to the scene
  309. */
  310. stopAllAnimations(): void;
  311. /**
  312. * Gets the current delta time used by animation engine
  313. */
  314. deltaTime: number;
  315. }
  316. }
  317. declare module "../Bones/bone" {
  318. interface Bone {
  319. /**
  320. * Copy an animation range from another bone
  321. * @param source defines the source bone
  322. * @param rangeName defines the range name to copy
  323. * @param frameOffset defines the frame offset
  324. * @param rescaleAsRequired defines if rescaling must be applied if required
  325. * @param skelDimensionsRatio defines the scaling ratio
  326. * @returns true if operation was successful
  327. */
  328. copyAnimationRange(source: Bone, rangeName: string, frameOffset: number, rescaleAsRequired: boolean, skelDimensionsRatio: Nullable<Vector3>): boolean;
  329. }
  330. }