15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* This file is automatically rebuilt by the Cesium build process. */
define(['./ComponentDatatype-9ed50558', './when-8166c7dd', './IndexDatatype-797210ca', './RuntimeError-4fdc4459', './createTaskProcessorWorker', './WebGLConstants-0664004c'], (function (ComponentDatatype, when, IndexDatatype, RuntimeError, createTaskProcessorWorker, WebGLConstants) { 'use strict';
 
  /* global require */
 
  var draco;
 
  function decodeIndexArray(dracoGeometry, dracoDecoder) {
    var numPoints = dracoGeometry.num_points();
    var numFaces = dracoGeometry.num_faces();
    var faceIndices = new draco.DracoInt32Array();
    var numIndices = numFaces * 3;
    var indexArray = IndexDatatype.IndexDatatype.createTypedArray(numPoints, numIndices);
 
    var offset = 0;
    for (var i = 0; i < numFaces; ++i) {
      dracoDecoder.GetFaceFromMesh(dracoGeometry, i, faceIndices);
 
      indexArray[offset + 0] = faceIndices.GetValue(0);
      indexArray[offset + 1] = faceIndices.GetValue(1);
      indexArray[offset + 2] = faceIndices.GetValue(2);
      offset += 3;
    }
 
    draco.destroy(faceIndices);
 
    return {
      typedArray: indexArray,
      numberOfIndices: numIndices,
    };
  }
 
  function decodeQuantizedDracoTypedArray(
    dracoGeometry,
    dracoDecoder,
    dracoAttribute,
    quantization,
    vertexArrayLength
  ) {
    var vertexArray;
    var attributeData;
    if (quantization.quantizationBits <= 8) {
      attributeData = new draco.DracoUInt8Array();
      vertexArray = new Uint8Array(vertexArrayLength);
      dracoDecoder.GetAttributeUInt8ForAllPoints(
        dracoGeometry,
        dracoAttribute,
        attributeData
      );
    } else {
      attributeData = new draco.DracoUInt16Array();
      vertexArray = new Uint16Array(vertexArrayLength);
      dracoDecoder.GetAttributeUInt16ForAllPoints(
        dracoGeometry,
        dracoAttribute,
        attributeData
      );
    }
 
    for (var i = 0; i < vertexArrayLength; ++i) {
      vertexArray[i] = attributeData.GetValue(i);
    }
 
    draco.destroy(attributeData);
    return vertexArray;
  }
 
  function decodeDracoTypedArray(
    dracoGeometry,
    dracoDecoder,
    dracoAttribute,
    vertexArrayLength
  ) {
    var vertexArray;
    var attributeData;
 
    // Some attribute types are casted down to 32 bit since Draco only returns 32 bit values
    switch (dracoAttribute.data_type()) {
      case 1:
      case 11: // DT_INT8 or DT_BOOL
        attributeData = new draco.DracoInt8Array();
        vertexArray = new Int8Array(vertexArrayLength);
        dracoDecoder.GetAttributeInt8ForAllPoints(
          dracoGeometry,
          dracoAttribute,
          attributeData
        );
        break;
      case 2: // DT_UINT8
        attributeData = new draco.DracoUInt8Array();
        vertexArray = new Uint8Array(vertexArrayLength);
        dracoDecoder.GetAttributeUInt8ForAllPoints(
          dracoGeometry,
          dracoAttribute,
          attributeData
        );
        break;
      case 3: // DT_INT16
        attributeData = new draco.DracoInt16Array();
        vertexArray = new Int16Array(vertexArrayLength);
        dracoDecoder.GetAttributeInt16ForAllPoints(
          dracoGeometry,
          dracoAttribute,
          attributeData
        );
        break;
      case 4: // DT_UINT16
        attributeData = new draco.DracoUInt16Array();
        vertexArray = new Uint16Array(vertexArrayLength);
        dracoDecoder.GetAttributeUInt16ForAllPoints(
          dracoGeometry,
          dracoAttribute,
          attributeData
        );
        break;
      case 5:
      case 7: // DT_INT32 or DT_INT64
        attributeData = new draco.DracoInt32Array();
        vertexArray = new Int32Array(vertexArrayLength);
        dracoDecoder.GetAttributeInt32ForAllPoints(
          dracoGeometry,
          dracoAttribute,
          attributeData
        );
        break;
      case 6:
      case 8: // DT_UINT32 or DT_UINT64
        attributeData = new draco.DracoUInt32Array();
        vertexArray = new Uint32Array(vertexArrayLength);
        dracoDecoder.GetAttributeUInt32ForAllPoints(
          dracoGeometry,
          dracoAttribute,
          attributeData
        );
        break;
      case 9:
      case 10: // DT_FLOAT32 or DT_FLOAT64
        attributeData = new draco.DracoFloat32Array();
        vertexArray = new Float32Array(vertexArrayLength);
        dracoDecoder.GetAttributeFloatForAllPoints(
          dracoGeometry,
          dracoAttribute,
          attributeData
        );
        break;
    }
 
    for (var i = 0; i < vertexArrayLength; ++i) {
      vertexArray[i] = attributeData.GetValue(i);
    }
 
    draco.destroy(attributeData);
    return vertexArray;
  }
 
  function decodeAttribute(dracoGeometry, dracoDecoder, dracoAttribute) {
    var numPoints = dracoGeometry.num_points();
    var numComponents = dracoAttribute.num_components();
 
    var quantization;
    var transform = new draco.AttributeQuantizationTransform();
    if (transform.InitFromAttribute(dracoAttribute)) {
      var minValues = new Array(numComponents);
      for (var i = 0; i < numComponents; ++i) {
        minValues[i] = transform.min_value(i);
      }
      quantization = {
        quantizationBits: transform.quantization_bits(),
        minValues: minValues,
        range: transform.range(),
        octEncoded: false,
      };
    }
    draco.destroy(transform);
 
    transform = new draco.AttributeOctahedronTransform();
    if (transform.InitFromAttribute(dracoAttribute)) {
      quantization = {
        quantizationBits: transform.quantization_bits(),
        octEncoded: true,
      };
    }
    draco.destroy(transform);
 
    var vertexArrayLength = numPoints * numComponents;
    var vertexArray;
    if (when.defined(quantization)) {
      vertexArray = decodeQuantizedDracoTypedArray(
        dracoGeometry,
        dracoDecoder,
        dracoAttribute,
        quantization,
        vertexArrayLength
      );
    } else {
      vertexArray = decodeDracoTypedArray(
        dracoGeometry,
        dracoDecoder,
        dracoAttribute,
        vertexArrayLength
      );
    }
 
    var componentDatatype = ComponentDatatype.ComponentDatatype.fromTypedArray(vertexArray);
 
    return {
      array: vertexArray,
      data: {
        componentsPerAttribute: numComponents,
        componentDatatype: componentDatatype,
        byteOffset: dracoAttribute.byte_offset(),
        byteStride:
          ComponentDatatype.ComponentDatatype.getSizeInBytes(componentDatatype) * numComponents,
        normalized: dracoAttribute.normalized(),
        quantization: quantization,
      },
    };
  }
 
  function decodePointCloud(parameters) {
    var dracoDecoder = new draco.Decoder();
 
    if (parameters.dequantizeInShader) {
      dracoDecoder.SkipAttributeTransform(draco.POSITION);
      dracoDecoder.SkipAttributeTransform(draco.NORMAL);
    }
 
    var buffer = new draco.DecoderBuffer();
    buffer.Init(parameters.buffer, parameters.buffer.length);
 
    var geometryType = dracoDecoder.GetEncodedGeometryType(buffer);
    if (geometryType !== draco.POINT_CLOUD) {
      throw new RuntimeError.RuntimeError("Draco geometry type must be POINT_CLOUD.");
    }
 
    var dracoPointCloud = new draco.PointCloud();
    var decodingStatus = dracoDecoder.DecodeBufferToPointCloud(
      buffer,
      dracoPointCloud
    );
    if (!decodingStatus.ok() || dracoPointCloud.ptr === 0) {
      throw new RuntimeError.RuntimeError(
        "Error decoding draco point cloud: " + decodingStatus.error_msg()
      );
    }
 
    draco.destroy(buffer);
 
    var result = {};
 
    var properties = parameters.properties;
    for (var propertyName in properties) {
      if (properties.hasOwnProperty(propertyName)) {
        var dracoAttribute;
        if (propertyName === "POSITION" || propertyName === "NORMAL") {
          var dracoAttributeId = dracoDecoder.GetAttributeId(
            dracoPointCloud,
            draco[propertyName]
          );
          dracoAttribute = dracoDecoder.GetAttribute(
            dracoPointCloud,
            dracoAttributeId
          );
        } else {
          var attributeId = properties[propertyName];
          dracoAttribute = dracoDecoder.GetAttributeByUniqueId(
            dracoPointCloud,
            attributeId
          );
        }
        result[propertyName] = decodeAttribute(
          dracoPointCloud,
          dracoDecoder,
          dracoAttribute
        );
      }
    }
 
    draco.destroy(dracoPointCloud);
    draco.destroy(dracoDecoder);
 
    return result;
  }
 
  function decodePrimitive(parameters) {
    var dracoDecoder = new draco.Decoder();
 
    // Skip all parameter types except generic
    // Note: As a temporary work-around until GetAttributeByUniqueId() works after
    // calling SkipAttributeTransform(), we will not skip attributes with multiple
    // sets of data in the glTF.
    var attributesToSkip = ["POSITION", "NORMAL"];
    var compressedAttributes = parameters.compressedAttributes;
    if (!when.defined(compressedAttributes["COLOR_1"])) {
      attributesToSkip.push("COLOR");
    }
    if (!when.defined(compressedAttributes["TEXCOORD_1"])) {
      attributesToSkip.push("TEX_COORD");
    }
    if (parameters.dequantizeInShader) {
      for (var i = 0; i < attributesToSkip.length; ++i) {
        dracoDecoder.SkipAttributeTransform(draco[attributesToSkip[i]]);
      }
    }
 
    var bufferView = parameters.bufferView;
    var buffer = new draco.DecoderBuffer();
    buffer.Init(parameters.array, bufferView.byteLength);
 
    var geometryType = dracoDecoder.GetEncodedGeometryType(buffer);
    if (geometryType !== draco.TRIANGULAR_MESH) {
      throw new RuntimeError.RuntimeError("Unsupported draco mesh geometry type.");
    }
 
    var dracoGeometry = new draco.Mesh();
    var decodingStatus = dracoDecoder.DecodeBufferToMesh(buffer, dracoGeometry);
    if (!decodingStatus.ok() || dracoGeometry.ptr === 0) {
      throw new RuntimeError.RuntimeError(
        "Error decoding draco mesh geometry: " + decodingStatus.error_msg()
      );
    }
 
    draco.destroy(buffer);
 
    var attributeData = {};
    for (var attributeName in compressedAttributes) {
      if (compressedAttributes.hasOwnProperty(attributeName)) {
        // Since GetAttributeByUniqueId() only works on attributes that we have not called
        // SkipAttributeTransform() on, we must first store a `dracoAttributeName` in case
        // we call GetAttributeId() instead.
        var dracoAttributeName = attributeName;
        if (attributeName === "TEXCOORD_0") {
          dracoAttributeName = "TEX_COORD";
        }
        if (attributeName === "COLOR_0") {
          dracoAttributeName = "COLOR";
        }
 
        var dracoAttribute;
        if (attributesToSkip.includes(dracoAttributeName)) {
          var dracoAttributeId = dracoDecoder.GetAttributeId(
            dracoGeometry,
            draco[dracoAttributeName]
          );
          dracoAttribute = dracoDecoder.GetAttribute(
            dracoGeometry,
            dracoAttributeId
          );
        } else {
          var compressedAttribute = compressedAttributes[attributeName];
          dracoAttribute = dracoDecoder.GetAttributeByUniqueId(
            dracoGeometry,
            compressedAttribute
          );
        }
 
        attributeData[attributeName] = decodeAttribute(
          dracoGeometry,
          dracoDecoder,
          dracoAttribute
        );
      }
    }
 
    var result = {
      indexArray: decodeIndexArray(dracoGeometry, dracoDecoder),
      attributeData: attributeData,
    };
 
    draco.destroy(dracoGeometry);
    draco.destroy(dracoDecoder);
 
    return result;
  }
 
  function decode(parameters) {
    if (when.defined(parameters.bufferView)) {
      return decodePrimitive(parameters);
    }
    return decodePointCloud(parameters);
  }
 
  function initWorker(dracoModule) {
    draco = dracoModule;
    self.onmessage = createTaskProcessorWorker(decode);
    self.postMessage(true);
  }
 
  function decodeDraco(event) {
    var data = event.data;
 
    // Expect the first message to be to load a web assembly module
    var wasmConfig = data.webAssemblyConfig;
    if (when.defined(wasmConfig)) {
      // Require and compile WebAssembly module, or use fallback if not supported
      return require([wasmConfig.modulePath], function (dracoModule) {
        if (when.defined(wasmConfig.wasmBinaryFile)) {
          if (!when.defined(dracoModule)) {
            dracoModule = self.DracoDecoderModule;
          }
 
          dracoModule(wasmConfig).then(function (compiledModule) {
            initWorker(compiledModule);
          });
        } else {
          initWorker(dracoModule());
        }
      });
    }
  }
 
  return decodeDraco;
 
}));