yzt
2023-05-05 4c558c77a6a9d23f057f094c4dc3e315eabef497
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import Cartesian3 from "../Core/Cartesian3.js";
import ComponentDatatype from "../Core/ComponentDatatype.js";
import defaultValue from "../Core/defaultValue.js";
import defined from "../Core/defined.js";
import LinearSpline from "../Core/LinearSpline.js";
import Matrix4 from "../Core/Matrix4.js";
import Quaternion from "../Core/Quaternion.js";
import QuaternionSpline from "../Core/QuaternionSpline.js";
import Spline from "../Core/Spline.js";
import WebGLConstants from "../Core/WebGLConstants.js";
import WeightSpline from "../Core/WeightSpline.js";
import getAccessorByteStride from "./GltfPipeline/getAccessorByteStride.js";
import numberOfComponentsForType from "./GltfPipeline/numberOfComponentsForType.js";
import AttributeType from "./AttributeType.js";
 
/**
 * @private
 */
function ModelAnimationCache() {}
 
var dataUriRegex = /^data\:/i;
 
function getAccessorKey(model, accessor) {
  var gltf = model.gltf;
  var buffers = gltf.buffers;
  var bufferViews = gltf.bufferViews;
 
  var bufferView = bufferViews[accessor.bufferView];
  var buffer = buffers[bufferView.buffer];
 
  var byteOffset = bufferView.byteOffset + accessor.byteOffset;
  var byteLength = accessor.count * numberOfComponentsForType(accessor.type);
 
  var uriKey = dataUriRegex.test(buffer.uri) ? "" : buffer.uri;
  return model.cacheKey + "//" + uriKey + "/" + byteOffset + "/" + byteLength;
}
 
var cachedAnimationParameters = {};
 
ModelAnimationCache.getAnimationParameterValues = function (model, accessor) {
  var key = getAccessorKey(model, accessor);
  var values = cachedAnimationParameters[key];
 
  if (!defined(values)) {
    // Cache miss
    var gltf = model.gltf;
 
    var buffers = gltf.buffers;
    var bufferViews = gltf.bufferViews;
 
    var bufferView = bufferViews[accessor.bufferView];
    var bufferId = bufferView.buffer;
    var buffer = buffers[bufferId];
    var source = buffer.extras._pipeline.source;
 
    var componentType = accessor.componentType;
    var type = accessor.type;
    var numberOfComponents = numberOfComponentsForType(type);
    var count = accessor.count;
    var byteStride = getAccessorByteStride(gltf, accessor);
 
    values = new Array(count);
    var accessorByteOffset = defaultValue(accessor.byteOffset, 0);
    var byteOffset = bufferView.byteOffset + accessorByteOffset;
    for (var i = 0; i < count; i++) {
      var typedArrayView = ComponentDatatype.createArrayBufferView(
        componentType,
        source.buffer,
        source.byteOffset + byteOffset,
        numberOfComponents
      );
      if (type === "SCALAR") {
        values[i] = typedArrayView[0];
      } else if (type === "VEC3") {
        values[i] = Cartesian3.fromArray(typedArrayView);
      } else if (type === "VEC4") {
        values[i] = Quaternion.unpack(typedArrayView);
      }
      byteOffset += byteStride;
    }
    // GLTF_SPEC: Support more parameter types when glTF supports targeting materials. https://github.com/KhronosGroup/glTF/issues/142
 
    if (defined(model.cacheKey)) {
      // Only cache when we can create a unique id
      cachedAnimationParameters[key] = values;
    }
  }
 
  return values;
};
 
var cachedAnimationSplines = {};
 
function getAnimationSplineKey(model, animationName, samplerName) {
  return model.cacheKey + "//" + animationName + "/" + samplerName;
}
 
function ConstantSpline(value) {
  this._value = value;
}
ConstantSpline.prototype.evaluate = function (time, result) {
  return this._value;
};
ConstantSpline.prototype.wrapTime = function (time) {
  return 0.0;
};
ConstantSpline.prototype.clampTime = function (time) {
  return 0.0;
};
 
function SteppedSpline(backingSpline) {
  this._spline = backingSpline;
  this._lastTimeIndex = 0;
}
SteppedSpline.prototype.findTimeInterval = Spline.prototype.findTimeInterval;
SteppedSpline.prototype.evaluate = function (time, result) {
  var i = (this._lastTimeIndex = this.findTimeInterval(
    time,
    this._lastTimeIndex
  ));
  var times = this._spline.times;
  var steppedTime = time >= times[i + 1] ? times[i + 1] : times[i];
  return this._spline.evaluate(steppedTime, result);
};
Object.defineProperties(SteppedSpline.prototype, {
  times: {
    get: function () {
      return this._spline.times;
    },
  },
});
SteppedSpline.prototype.wrapTime = function (time) {
  return this._spline.wrapTime(time);
};
SteppedSpline.prototype.clampTime = function (time) {
  return this._spline.clampTime(time);
};
 
ModelAnimationCache.getAnimationSpline = function (
  model,
  animationName,
  animation,
  samplerName,
  sampler,
  input,
  path,
  output
) {
  var key = getAnimationSplineKey(model, animationName, samplerName);
  var spline = cachedAnimationSplines[key];
 
  if (!defined(spline)) {
    var times = input;
    var controlPoints = output;
 
    if (times.length === 1 && controlPoints.length === 1) {
      spline = new ConstantSpline(controlPoints[0]);
    } else if (
      sampler.interpolation === "LINEAR" ||
      sampler.interpolation === "STEP"
    ) {
      if (path === "translation" || path === "scale") {
        spline = new LinearSpline({
          times: times,
          points: controlPoints,
        });
      } else if (path === "rotation") {
        spline = new QuaternionSpline({
          times: times,
          points: controlPoints,
        });
      } else if (path === "weights") {
        spline = new WeightSpline({
          times: times,
          weights: controlPoints,
        });
      }
 
      if (defined(spline) && sampler.interpolation === "STEP") {
        spline = new SteppedSpline(spline);
      }
    }
 
    if (defined(model.cacheKey)) {
      // Only cache when we can create a unique id
      cachedAnimationSplines[key] = spline;
    }
  }
 
  return spline;
};
 
var cachedSkinInverseBindMatrices = {};
 
ModelAnimationCache.getSkinInverseBindMatrices = function (model, accessor) {
  var key = getAccessorKey(model, accessor);
  var matrices = cachedSkinInverseBindMatrices[key];
 
  if (!defined(matrices)) {
    // Cache miss
    var gltf = model.gltf;
    var buffers = gltf.buffers;
    var bufferViews = gltf.bufferViews;
 
    var bufferViewId = accessor.bufferView;
    var bufferView = bufferViews[bufferViewId];
    var bufferId = bufferView.buffer;
    var buffer = buffers[bufferId];
    var source = buffer.extras._pipeline.source;
 
    var componentType = accessor.componentType;
    var type = accessor.type;
    var count = accessor.count;
    var byteStride = getAccessorByteStride(gltf, accessor);
    var byteOffset = bufferView.byteOffset + accessor.byteOffset;
    var numberOfComponents = numberOfComponentsForType(type);
 
    matrices = new Array(count);
 
    if (componentType === WebGLConstants.FLOAT && type === AttributeType.MAT4) {
      for (var i = 0; i < count; ++i) {
        var typedArrayView = ComponentDatatype.createArrayBufferView(
          componentType,
          source.buffer,
          source.byteOffset + byteOffset,
          numberOfComponents
        );
        matrices[i] = Matrix4.fromArray(typedArrayView);
        byteOffset += byteStride;
      }
    }
 
    cachedSkinInverseBindMatrices[key] = matrices;
  }
 
  return matrices;
};
export default ModelAnimationCache;