WeightSpline.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 WeightSpline
  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. * var times = [ 0.0, 1.5, 3.0, 4.5, 6.0 ];
  26. * var weights = [0.0, 1.0, 0.25, 0.75, 0.5, 0.5, 0.75, 0.25, 1.0, 0.0]; //Two targets
  27. * var spline = new Cesium.WeightSpline({
  28. * times : times,
  29. * weights : weights
  30. * });
  31. *
  32. * var p0 = spline.evaluate(times[0]);
  33. *
  34. * @see LinearSpline
  35. * @see HermiteSpline
  36. * @see CatmullRomSpline
  37. * @see QuaternionSpline
  38. */
  39. function WeightSpline(options) {
  40. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  41. var weights = options.weights;
  42. var times = options.times;
  43. //>>includeStart('debug', pragmas.debug);
  44. Check.defined("weights", weights);
  45. Check.defined("times", times);
  46. Check.typeOf.number.greaterThanOrEquals("weights.length", weights.length, 3);
  47. if (weights.length % times.length !== 0) {
  48. throw new DeveloperError(
  49. "times.length must be a factor of weights.length."
  50. );
  51. }
  52. //>>includeEnd('debug');
  53. this._times = times;
  54. this._weights = weights;
  55. this._count = weights.length / times.length;
  56. this._lastTimeIndex = 0;
  57. }
  58. Object.defineProperties(WeightSpline.prototype, {
  59. /**
  60. * An array of times for the control weights.
  61. *
  62. * @memberof WeightSpline.prototype
  63. *
  64. * @type {Number[]}
  65. * @readonly
  66. */
  67. times: {
  68. get: function () {
  69. return this._times;
  70. },
  71. },
  72. /**
  73. * An array of floating-point array control weights.
  74. *
  75. * @memberof WeightSpline.prototype
  76. *
  77. * @type {Number[]}
  78. * @readonly
  79. */
  80. weights: {
  81. get: function () {
  82. return this._weights;
  83. },
  84. },
  85. });
  86. /**
  87. * Finds an index <code>i</code> in <code>times</code> such that the parameter
  88. * <code>time</code> is in the interval <code>[times[i], times[i + 1]]</code>.
  89. * @function
  90. *
  91. * @param {Number} time The time.
  92. * @returns {Number} The index for the element at the start of the interval.
  93. *
  94. * @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>
  95. * is the first element in the array <code>times</code> and <code>t<sub>n</sub></code> is the last element
  96. * in the array <code>times</code>.
  97. */
  98. WeightSpline.prototype.findTimeInterval = Spline.prototype.findTimeInterval;
  99. /**
  100. * Wraps the given time to the period covered by the spline.
  101. * @function
  102. *
  103. * @param {Number} time The time.
  104. * @return {Number} The time, wrapped around to the updated animation.
  105. */
  106. WeightSpline.prototype.wrapTime = Spline.prototype.wrapTime;
  107. /**
  108. * Clamps the given time to the period covered by the spline.
  109. * @function
  110. *
  111. * @param {Number} time The time.
  112. * @return {Number} The time, clamped to the animation period.
  113. */
  114. WeightSpline.prototype.clampTime = Spline.prototype.clampTime;
  115. /**
  116. * Evaluates the curve at a given time.
  117. *
  118. * @param {Number} time The time at which to evaluate the curve.
  119. * @param {Number[]} [result] The object onto which to store the result.
  120. * @returns {Number[]} The modified result parameter or a new instance of the point on the curve at the given time.
  121. *
  122. * @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>
  123. * is the first element in the array <code>times</code> and <code>t<sub>n</sub></code> is the last element
  124. * in the array <code>times</code>.
  125. */
  126. WeightSpline.prototype.evaluate = function (time, result) {
  127. var weights = this.weights;
  128. var times = this.times;
  129. var i = (this._lastTimeIndex = this.findTimeInterval(
  130. time,
  131. this._lastTimeIndex
  132. ));
  133. var u = (time - times[i]) / (times[i + 1] - times[i]);
  134. if (!defined(result)) {
  135. result = new Array(this._count);
  136. }
  137. for (var j = 0; j < this._count; j++) {
  138. var index = i * this._count + j;
  139. result[j] = weights[index] * (1.0 - u) + weights[index + this._count] * u;
  140. }
  141. return result;
  142. };
  143. export default WeightSpline;