WeightSpline.js 5.8 KB

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