MorphWeightSpline.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import Check from "./Check.js";
  2. import defaultValue from "./defaultValue.js";
  3. import defined from "./defined.js";
  4. import DeveloperError from "./DeveloperError.js";
  5. import Spline from "./Spline.js";
  6. /**
  7. * A spline that linearly interpolates over an array of weight values used by morph targets.
  8. *
  9. * @alias MorphWeightSpline
  10. * @constructor
  11. *
  12. * @param {Object} options Object with the following properties:
  13. * @param {Number[]} options.times An array of strictly increasing, unit-less, floating-point times at each point.
  14. * The values are in no way connected to the clock time. They are the parameterization for the curve.
  15. * @param {Number[]} options.weights The array of floating-point control weights given. The weights are ordered such
  16. * that all weights for the targets are given in chronological order and order in which they appear in
  17. * the glTF from which the morph targets come. This means for 2 targets, weights = [w(0,0), w(0,1), w(1,0), w(1,1) ...]
  18. * where i and j in w(i,j) are the time indices and target indices, respectively.
  19. *
  20. * @exception {DeveloperError} weights.length must be greater than or equal to 2.
  21. * @exception {DeveloperError} times.length must be a factor of weights.length.
  22. *
  23. *
  24. * @example
  25. * const times = [ 0.0, 1.5, 3.0, 4.5, 6.0 ];
  26. * const weights = [0.0, 1.0, 0.25, 0.75, 0.5, 0.5, 0.75, 0.25, 1.0, 0.0]; //Two targets
  27. * const spline = new Cesium.WeightSpline({
  28. * times : times,
  29. * weights : weights
  30. * });
  31. *
  32. * const p0 = spline.evaluate(times[0]);
  33. *
  34. * @see ConstantSpline
  35. * @see SteppedSpline
  36. * @see LinearSpline
  37. * @see HermiteSpline
  38. * @see CatmullRomSpline
  39. * @see QuaternionSpline
  40. */
  41. function MorphWeightSpline(options) {
  42. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  43. const weights = options.weights;
  44. const times = options.times;
  45. //>>includeStart('debug', pragmas.debug);
  46. Check.defined("weights", weights);
  47. Check.defined("times", times);
  48. Check.typeOf.number.greaterThanOrEquals("weights.length", weights.length, 3);
  49. if (weights.length % times.length !== 0) {
  50. throw new DeveloperError(
  51. "times.length must be a factor of weights.length."
  52. );
  53. }
  54. //>>includeEnd('debug');
  55. this._times = times;
  56. this._weights = weights;
  57. this._count = weights.length / times.length;
  58. this._lastTimeIndex = 0;
  59. }
  60. Object.defineProperties(MorphWeightSpline.prototype, {
  61. /**
  62. * An array of times for the control weights.
  63. *
  64. * @memberof WeightSpline.prototype
  65. *
  66. * @type {Number[]}
  67. * @readonly
  68. */
  69. times: {
  70. get: function () {
  71. return this._times;
  72. },
  73. },
  74. /**
  75. * An array of floating-point array control weights.
  76. *
  77. * @memberof WeightSpline.prototype
  78. *
  79. * @type {Number[]}
  80. * @readonly
  81. */
  82. weights: {
  83. get: function () {
  84. return this._weights;
  85. },
  86. },
  87. });
  88. /**
  89. * Finds an index <code>i</code> in <code>times</code> such that the parameter
  90. * <code>time</code> is in the interval <code>[times[i], times[i + 1]]</code>.
  91. * @function
  92. *
  93. * @param {Number} time The time.
  94. * @returns {Number} The index for the element at the start of the interval.
  95. *
  96. * @exception {DeveloperError} time must be in the range <code>[t<sub>0</sub>, t<sub>n</sub>]</code>, where <code>t<sub>0</sub></code>
  97. * is the first element in the array <code>times</code> and <code>t<sub>n</sub></code> is the last element
  98. * in the array <code>times</code>.
  99. */
  100. MorphWeightSpline.prototype.findTimeInterval =
  101. Spline.prototype.findTimeInterval;
  102. /**
  103. * Wraps the given time to the period covered by the spline.
  104. * @function
  105. *
  106. * @param {Number} time The time.
  107. * @return {Number} The time, wrapped around to the updated animation.
  108. */
  109. MorphWeightSpline.prototype.wrapTime = Spline.prototype.wrapTime;
  110. /**
  111. * Clamps the given time to the period covered by the spline.
  112. * @function
  113. *
  114. * @param {Number} time The time.
  115. * @return {Number} The time, clamped to the animation period.
  116. */
  117. MorphWeightSpline.prototype.clampTime = Spline.prototype.clampTime;
  118. /**
  119. * Evaluates the curve at a given time.
  120. *
  121. * @param {Number} time The time at which to evaluate the curve.
  122. * @param {Number[]} [result] The object onto which to store the result.
  123. * @returns {Number[]} The modified result parameter or a new instance of the point on the curve at the given time.
  124. *
  125. * @exception {DeveloperError} time must be in the range <code>[t<sub>0</sub>, t<sub>n</sub>]</code>, where <code>t<sub>0</sub></code>
  126. * is the first element in the array <code>times</code> and <code>t<sub>n</sub></code> is the last element
  127. * in the array <code>times</code>.
  128. */
  129. MorphWeightSpline.prototype.evaluate = function (time, result) {
  130. const weights = this.weights;
  131. const times = this.times;
  132. const i = (this._lastTimeIndex = this.findTimeInterval(
  133. time,
  134. this._lastTimeIndex
  135. ));
  136. const u = (time - times[i]) / (times[i + 1] - times[i]);
  137. if (!defined(result)) {
  138. result = new Array(this._count);
  139. }
  140. for (let j = 0; j < this._count; j++) {
  141. const index = i * this._count + j;
  142. result[j] = weights[index] * (1.0 - u) + weights[index + this._count] * u;
  143. }
  144. return result;
  145. };
  146. export default MorphWeightSpline;