interpolateValueAction.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Action } from "./action.js";
  2. import { Logger } from "../Misc/logger.js";
  3. import { Observable } from "../Misc/observable.js";
  4. import { Color3 } from "../Maths/math.color.js";
  5. import { Vector3, Matrix, Quaternion } from "../Maths/math.vector.js";
  6. import { Animation } from "../Animations/animation.js";
  7. import { RegisterClass } from "../Misc/typeStore.js";
  8. /**
  9. * This defines an action responsible to change the value of a property
  10. * by interpolating between its current value and the newly set one once triggered.
  11. * @see https://doc.babylonjs.com/features/featuresDeepDive/events/actions
  12. */
  13. export class InterpolateValueAction extends Action {
  14. /**
  15. * Instantiate the action
  16. * @param triggerOptions defines the trigger options
  17. * @param target defines the object containing the value to interpolate
  18. * @param propertyPath defines the path to the property in the target object
  19. * @param value defines the target value at the end of the interpolation
  20. * @param duration defines the time it will take for the property to interpolate to the value.
  21. * @param condition defines the trigger related conditions
  22. * @param stopOtherAnimations defines if the other scene animations should be stopped when the action has been triggered
  23. * @param onInterpolationDone defines a callback raised once the interpolation animation has been done
  24. */
  25. constructor(triggerOptions, target, propertyPath, value, duration = 1000, condition, stopOtherAnimations, onInterpolationDone) {
  26. super(triggerOptions, condition);
  27. /**
  28. * Defines the time it will take for the property to interpolate to the value.
  29. */
  30. this.duration = 1000;
  31. /**
  32. * Observable triggered once the interpolation animation has been done.
  33. */
  34. this.onInterpolationDoneObservable = new Observable();
  35. this.propertyPath = propertyPath;
  36. this.value = value;
  37. this.duration = duration;
  38. this.stopOtherAnimations = stopOtherAnimations;
  39. this.onInterpolationDone = onInterpolationDone;
  40. this._target = this._effectiveTarget = target;
  41. }
  42. /** @internal */
  43. _prepare() {
  44. this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
  45. this._property = this._getProperty(this.propertyPath);
  46. }
  47. /**
  48. * Execute the action starts the value interpolation.
  49. */
  50. execute() {
  51. const scene = this._actionManager.getScene();
  52. const keys = [
  53. {
  54. frame: 0,
  55. value: this._effectiveTarget[this._property],
  56. },
  57. {
  58. frame: 100,
  59. value: this.value,
  60. },
  61. ];
  62. let dataType;
  63. if (typeof this.value === "number") {
  64. dataType = Animation.ANIMATIONTYPE_FLOAT;
  65. }
  66. else if (this.value instanceof Color3) {
  67. dataType = Animation.ANIMATIONTYPE_COLOR3;
  68. }
  69. else if (this.value instanceof Vector3) {
  70. dataType = Animation.ANIMATIONTYPE_VECTOR3;
  71. }
  72. else if (this.value instanceof Matrix) {
  73. dataType = Animation.ANIMATIONTYPE_MATRIX;
  74. }
  75. else if (this.value instanceof Quaternion) {
  76. dataType = Animation.ANIMATIONTYPE_QUATERNION;
  77. }
  78. else {
  79. Logger.Warn("InterpolateValueAction: Unsupported type (" + typeof this.value + ")");
  80. return;
  81. }
  82. const animation = new Animation("InterpolateValueAction", this._property, 100 * (1000.0 / this.duration), dataType, Animation.ANIMATIONLOOPMODE_CONSTANT);
  83. animation.setKeys(keys);
  84. if (this.stopOtherAnimations) {
  85. scene.stopAnimation(this._effectiveTarget);
  86. }
  87. const wrapper = () => {
  88. this.onInterpolationDoneObservable.notifyObservers(this);
  89. if (this.onInterpolationDone) {
  90. this.onInterpolationDone();
  91. }
  92. };
  93. scene.beginDirectAnimation(this._effectiveTarget, [animation], 0, 100, false, 1, wrapper);
  94. }
  95. /**
  96. * Serializes the actions and its related information.
  97. * @param parent defines the object to serialize in
  98. * @returns the serialized object
  99. */
  100. serialize(parent) {
  101. return super._serialize({
  102. name: "InterpolateValueAction",
  103. properties: [
  104. Action._GetTargetProperty(this._target),
  105. { name: "propertyPath", value: this.propertyPath },
  106. { name: "value", value: Action._SerializeValueAsString(this.value) },
  107. { name: "duration", value: Action._SerializeValueAsString(this.duration) },
  108. { name: "stopOtherAnimations", value: Action._SerializeValueAsString(this.stopOtherAnimations) || false },
  109. ],
  110. }, parent);
  111. }
  112. }
  113. RegisterClass("BABYLON.InterpolateValueAction", InterpolateValueAction);
  114. //# sourceMappingURL=interpolateValueAction.js.map