bakedVertexAnimationManager.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { __decorate } from "../tslib.es6.js";
  2. import { serialize, expandToProperty, serializeAsTexture } from "../Misc/decorators.js";
  3. import { Vector4 } from "../Maths/math.vector.js";
  4. import { EngineStore } from "../Engines/engineStore.js";
  5. import { SerializationHelper } from "../Misc/decorators.serialization.js";
  6. /**
  7. * This class is used to animate meshes using a baked vertex animation texture
  8. * @see https://doc.babylonjs.com/features/featuresDeepDive/animation/baked_texture_animations
  9. * @since 5.0
  10. */
  11. export class BakedVertexAnimationManager {
  12. /**
  13. * Creates a new BakedVertexAnimationManager
  14. * @param scene defines the current scene
  15. */
  16. constructor(scene) {
  17. this._texture = null;
  18. this._isEnabled = true;
  19. /**
  20. * Enable or disable the vertex animation manager
  21. */
  22. this.isEnabled = true;
  23. /**
  24. * The time counter, to pick the correct animation frame.
  25. */
  26. this.time = 0;
  27. scene = scene || EngineStore.LastCreatedScene;
  28. if (!scene) {
  29. return;
  30. }
  31. this._scene = scene;
  32. this.animationParameters = new Vector4(0, 0, 0, 30);
  33. }
  34. /** @internal */
  35. _markSubMeshesAsAttributesDirty() {
  36. for (const mesh of this._scene.meshes) {
  37. if (mesh.bakedVertexAnimationManager === this) {
  38. mesh._markSubMeshesAsAttributesDirty();
  39. }
  40. }
  41. }
  42. /**
  43. * Binds to the effect.
  44. * @param effect The effect to bind to.
  45. * @param useInstances True when it's an instance.
  46. */
  47. bind(effect, useInstances = false) {
  48. if (!this._texture || !this._isEnabled) {
  49. return;
  50. }
  51. const size = this._texture.getSize();
  52. effect.setFloat2("bakedVertexAnimationTextureSizeInverted", 1.0 / size.width, 1.0 / size.height);
  53. effect.setFloat("bakedVertexAnimationTime", this.time);
  54. if (!useInstances) {
  55. effect.setVector4("bakedVertexAnimationSettings", this.animationParameters);
  56. }
  57. effect.setTexture("bakedVertexAnimationTexture", this._texture);
  58. }
  59. /**
  60. * Clone the current manager
  61. * @returns a new BakedVertexAnimationManager
  62. */
  63. clone() {
  64. const copy = new BakedVertexAnimationManager(this._scene);
  65. this.copyTo(copy);
  66. return copy;
  67. }
  68. /**
  69. * Sets animation parameters.
  70. * @param startFrame The first frame of the animation.
  71. * @param endFrame The last frame of the animation.
  72. * @param offset The offset when starting the animation.
  73. * @param speedFramesPerSecond The frame rate.
  74. */
  75. setAnimationParameters(startFrame, endFrame, offset = 0, speedFramesPerSecond = 30) {
  76. this.animationParameters = new Vector4(startFrame, endFrame, offset, speedFramesPerSecond);
  77. }
  78. /**
  79. * Disposes the resources of the manager.
  80. * @param forceDisposeTextures - Forces the disposal of all textures.
  81. */
  82. dispose(forceDisposeTextures) {
  83. if (forceDisposeTextures) {
  84. this._texture?.dispose();
  85. }
  86. }
  87. /**
  88. * Get the current class name useful for serialization or dynamic coding.
  89. * @returns "BakedVertexAnimationManager"
  90. */
  91. getClassName() {
  92. return "BakedVertexAnimationManager";
  93. }
  94. /**
  95. * Makes a duplicate of the current instance into another one.
  96. * @param vatMap define the instance where to copy the info
  97. */
  98. copyTo(vatMap) {
  99. SerializationHelper.Clone(() => vatMap, this);
  100. }
  101. /**
  102. * Serializes this vertex animation instance
  103. * @returns - An object with the serialized instance.
  104. */
  105. serialize() {
  106. return SerializationHelper.Serialize(this);
  107. }
  108. /**
  109. * Parses a vertex animation setting from a serialized object.
  110. * @param source - Serialized object.
  111. * @param scene Defines the scene we are parsing for
  112. * @param rootUrl Defines the rootUrl to load from
  113. */
  114. parse(source, scene, rootUrl) {
  115. SerializationHelper.Parse(() => this, source, scene, rootUrl);
  116. }
  117. }
  118. __decorate([
  119. serializeAsTexture(),
  120. expandToProperty("_markSubMeshesAsAttributesDirty")
  121. ], BakedVertexAnimationManager.prototype, "texture", void 0);
  122. __decorate([
  123. serialize(),
  124. expandToProperty("_markSubMeshesAsAttributesDirty")
  125. ], BakedVertexAnimationManager.prototype, "isEnabled", void 0);
  126. __decorate([
  127. serialize()
  128. ], BakedVertexAnimationManager.prototype, "animationParameters", void 0);
  129. __decorate([
  130. serialize()
  131. ], BakedVertexAnimationManager.prototype, "time", void 0);
  132. //# sourceMappingURL=bakedVertexAnimationManager.js.map