@loaders.gl/draco 4.2.0-alpha.3 → 4.2.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/dist.dev.js +260 -79
  2. package/dist/dist.min.js +11 -0
  3. package/dist/draco-loader.d.ts +2 -2
  4. package/dist/draco-loader.d.ts.map +1 -1
  5. package/dist/draco-loader.js +23 -17
  6. package/dist/draco-worker-node.js +2 -2
  7. package/dist/draco-worker.js +2 -2
  8. package/dist/draco-writer-worker-node.js +2 -2
  9. package/dist/draco-writer-worker.js +2 -2
  10. package/dist/draco-writer.d.ts +2 -2
  11. package/dist/draco-writer.d.ts.map +1 -1
  12. package/dist/draco-writer.js +30 -23
  13. package/dist/draco3d/draco3d-types.js +48 -41
  14. package/dist/index.cjs +26 -71
  15. package/dist/index.cjs.map +7 -0
  16. package/dist/index.d.ts +6 -6
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +27 -21
  19. package/dist/lib/draco-builder.d.ts +2 -2
  20. package/dist/lib/draco-builder.d.ts.map +1 -1
  21. package/dist/lib/draco-builder.js +309 -243
  22. package/dist/lib/draco-module-loader.js +78 -62
  23. package/dist/lib/draco-parser.d.ts +2 -2
  24. package/dist/lib/draco-parser.d.ts.map +1 -1
  25. package/dist/lib/draco-parser.js +434 -323
  26. package/dist/lib/draco-types.js +1 -1
  27. package/dist/lib/utils/get-draco-schema.d.ts +1 -1
  28. package/dist/lib/utils/get-draco-schema.d.ts.map +1 -1
  29. package/dist/lib/utils/get-draco-schema.js +29 -32
  30. package/dist/lib/utils/version.js +4 -2
  31. package/dist/workers/draco-worker-node.js +1 -1
  32. package/dist/workers/draco-worker.js +0 -1
  33. package/dist/workers/draco-writer-worker-node.js +20 -25
  34. package/dist/workers/draco-writer-worker.js +19 -25
  35. package/package.json +12 -9
  36. package/dist/draco-loader.js.map +0 -1
  37. package/dist/draco-writer.js.map +0 -1
  38. package/dist/draco3d/draco3d-types.js.map +0 -1
  39. package/dist/index.js.map +0 -1
  40. package/dist/lib/draco-builder.js.map +0 -1
  41. package/dist/lib/draco-module-loader.js.map +0 -1
  42. package/dist/lib/draco-parser.js.map +0 -1
  43. package/dist/lib/draco-types.js.map +0 -1
  44. package/dist/lib/utils/get-draco-schema.js.map +0 -1
  45. package/dist/lib/utils/version.js.map +0 -1
  46. package/dist/libs/libs/draco_decoder.wasm +0 -0
  47. package/dist/libs/libs/draco_encoder.js +0 -52
  48. package/dist/libs/libs/draco_wasm_wrapper.js +0 -117
  49. package/dist/workers/draco-worker-node.js.map +0 -1
  50. package/dist/workers/draco-worker.js.map +0 -1
  51. package/dist/workers/draco-writer-worker-node.js.map +0 -1
  52. package/dist/workers/draco-writer-worker.js.map +0 -1
@@ -1,362 +1,473 @@
1
+ /* eslint-disable camelcase */
1
2
  import { getMeshBoundingBox } from '@loaders.gl/schema';
2
3
  import { getDracoSchema } from "./utils/get-draco-schema.js";
4
+ // @ts-ignore
5
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
3
6
  const GEOMETRY_TYPE = {
4
- TRIANGULAR_MESH: 0,
5
- POINT_CLOUD: 1
7
+ TRIANGULAR_MESH: 0,
8
+ POINT_CLOUD: 1
6
9
  };
10
+ // Native Draco attribute names to GLTF attribute names.
7
11
  const DRACO_TO_GLTF_ATTRIBUTE_NAME_MAP = {
8
- POSITION: 'POSITION',
9
- NORMAL: 'NORMAL',
10
- COLOR: 'COLOR_0',
11
- TEX_COORD: 'TEXCOORD_0'
12
+ POSITION: 'POSITION',
13
+ NORMAL: 'NORMAL',
14
+ COLOR: 'COLOR_0',
15
+ TEX_COORD: 'TEXCOORD_0'
12
16
  };
13
17
  const DRACO_DATA_TYPE_TO_TYPED_ARRAY_MAP = {
14
- 1: Int8Array,
15
- 2: Uint8Array,
16
- 3: Int16Array,
17
- 4: Uint16Array,
18
- 5: Int32Array,
19
- 6: Uint32Array,
20
- 9: Float32Array
18
+ 1: Int8Array,
19
+ 2: Uint8Array,
20
+ 3: Int16Array,
21
+ 4: Uint16Array,
22
+ 5: Int32Array,
23
+ 6: Uint32Array,
24
+ 9: Float32Array
21
25
  };
22
26
  const INDEX_ITEM_SIZE = 4;
23
27
  export default class DracoParser {
24
- constructor(draco) {
25
- this.draco = void 0;
26
- this.decoder = void 0;
27
- this.metadataQuerier = void 0;
28
- this.draco = draco;
29
- this.decoder = new this.draco.Decoder();
30
- this.metadataQuerier = new this.draco.MetadataQuerier();
31
- }
32
- destroy() {
33
- this.draco.destroy(this.decoder);
34
- this.draco.destroy(this.metadataQuerier);
35
- }
36
- parseSync(arrayBuffer) {
37
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
38
- const buffer = new this.draco.DecoderBuffer();
39
- buffer.Init(new Int8Array(arrayBuffer), arrayBuffer.byteLength);
40
- this._disableAttributeTransforms(options);
41
- const geometry_type = this.decoder.GetEncodedGeometryType(buffer);
42
- const dracoGeometry = geometry_type === this.draco.TRIANGULAR_MESH ? new this.draco.Mesh() : new this.draco.PointCloud();
43
- try {
44
- let dracoStatus;
45
- switch (geometry_type) {
46
- case this.draco.TRIANGULAR_MESH:
47
- dracoStatus = this.decoder.DecodeBufferToMesh(buffer, dracoGeometry);
48
- break;
49
- case this.draco.POINT_CLOUD:
50
- dracoStatus = this.decoder.DecodeBufferToPointCloud(buffer, dracoGeometry);
51
- break;
52
- default:
53
- throw new Error('DRACO: Unknown geometry type.');
54
- }
55
- if (!dracoStatus.ok() || !dracoGeometry.ptr) {
56
- const message = `DRACO decompression failed: ${dracoStatus.error_msg()}`;
57
- throw new Error(message);
58
- }
59
- const loaderData = this._getDracoLoaderData(dracoGeometry, geometry_type, options);
60
- const geometry = this._getMeshData(dracoGeometry, loaderData, options);
61
- const boundingBox = getMeshBoundingBox(geometry.attributes);
62
- const schema = getDracoSchema(geometry.attributes, loaderData, geometry.indices);
63
- const data = {
64
- loader: 'draco',
65
- loaderData,
66
- header: {
67
- vertexCount: dracoGeometry.num_points(),
68
- boundingBox
69
- },
70
- ...geometry,
71
- schema
72
- };
73
- return data;
74
- } finally {
75
- this.draco.destroy(buffer);
76
- if (dracoGeometry) {
77
- this.draco.destroy(dracoGeometry);
78
- }
28
+ // draco - the draco decoder, either import `draco3d` or load dynamically
29
+ constructor(draco) {
30
+ this.draco = draco;
31
+ this.decoder = new this.draco.Decoder();
32
+ this.metadataQuerier = new this.draco.MetadataQuerier();
79
33
  }
80
- }
81
- _getDracoLoaderData(dracoGeometry, geometry_type, options) {
82
- const metadata = this._getTopLevelMetadata(dracoGeometry);
83
- const attributes = this._getDracoAttributes(dracoGeometry, options);
84
- return {
85
- geometry_type,
86
- num_attributes: dracoGeometry.num_attributes(),
87
- num_points: dracoGeometry.num_points(),
88
- num_faces: dracoGeometry instanceof this.draco.Mesh ? dracoGeometry.num_faces() : 0,
89
- metadata,
90
- attributes
91
- };
92
- }
93
- _getDracoAttributes(dracoGeometry, options) {
94
- const dracoAttributes = {};
95
- for (let attributeId = 0; attributeId < dracoGeometry.num_attributes(); attributeId++) {
96
- const dracoAttribute = this.decoder.GetAttribute(dracoGeometry, attributeId);
97
- const metadata = this._getAttributeMetadata(dracoGeometry, attributeId);
98
- dracoAttributes[dracoAttribute.unique_id()] = {
99
- unique_id: dracoAttribute.unique_id(),
100
- attribute_type: dracoAttribute.attribute_type(),
101
- data_type: dracoAttribute.data_type(),
102
- num_components: dracoAttribute.num_components(),
103
- byte_offset: dracoAttribute.byte_offset(),
104
- byte_stride: dracoAttribute.byte_stride(),
105
- normalized: dracoAttribute.normalized(),
106
- attribute_index: attributeId,
107
- metadata
108
- };
109
- const quantization = this._getQuantizationTransform(dracoAttribute, options);
110
- if (quantization) {
111
- dracoAttributes[dracoAttribute.unique_id()].quantization_transform = quantization;
112
- }
113
- const octahedron = this._getOctahedronTransform(dracoAttribute, options);
114
- if (octahedron) {
115
- dracoAttributes[dracoAttribute.unique_id()].octahedron_transform = octahedron;
116
- }
34
+ /**
35
+ * Destroy draco resources
36
+ */
37
+ destroy() {
38
+ this.draco.destroy(this.decoder);
39
+ this.draco.destroy(this.metadataQuerier);
40
+ }
41
+ /**
42
+ * NOTE: caller must call `destroyGeometry` on the return value after using it
43
+ * @param arrayBuffer
44
+ * @param options
45
+ */
46
+ parseSync(arrayBuffer, options = {}) {
47
+ const buffer = new this.draco.DecoderBuffer();
48
+ buffer.Init(new Int8Array(arrayBuffer), arrayBuffer.byteLength);
49
+ this._disableAttributeTransforms(options);
50
+ const geometry_type = this.decoder.GetEncodedGeometryType(buffer);
51
+ const dracoGeometry = geometry_type === this.draco.TRIANGULAR_MESH
52
+ ? new this.draco.Mesh()
53
+ : new this.draco.PointCloud();
54
+ try {
55
+ let dracoStatus;
56
+ switch (geometry_type) {
57
+ case this.draco.TRIANGULAR_MESH:
58
+ dracoStatus = this.decoder.DecodeBufferToMesh(buffer, dracoGeometry);
59
+ break;
60
+ case this.draco.POINT_CLOUD:
61
+ dracoStatus = this.decoder.DecodeBufferToPointCloud(buffer, dracoGeometry);
62
+ break;
63
+ default:
64
+ throw new Error('DRACO: Unknown geometry type.');
65
+ }
66
+ if (!dracoStatus.ok() || !dracoGeometry.ptr) {
67
+ const message = `DRACO decompression failed: ${dracoStatus.error_msg()}`;
68
+ // console.error(message);
69
+ throw new Error(message);
70
+ }
71
+ const loaderData = this._getDracoLoaderData(dracoGeometry, geometry_type, options);
72
+ const geometry = this._getMeshData(dracoGeometry, loaderData, options);
73
+ const boundingBox = getMeshBoundingBox(geometry.attributes);
74
+ const schema = getDracoSchema(geometry.attributes, loaderData, geometry.indices);
75
+ const data = {
76
+ loader: 'draco',
77
+ loaderData,
78
+ header: {
79
+ vertexCount: dracoGeometry.num_points(),
80
+ boundingBox
81
+ },
82
+ ...geometry,
83
+ schema
84
+ };
85
+ return data;
86
+ }
87
+ finally {
88
+ this.draco.destroy(buffer);
89
+ if (dracoGeometry) {
90
+ this.draco.destroy(dracoGeometry);
91
+ }
92
+ }
117
93
  }
118
- return dracoAttributes;
119
- }
120
- _getMeshData(dracoGeometry, loaderData, options) {
121
- const attributes = this._getMeshAttributes(loaderData, dracoGeometry, options);
122
- const positionAttribute = attributes.POSITION;
123
- if (!positionAttribute) {
124
- throw new Error('DRACO: No position attribute found.');
94
+ // Draco specific "loader data"
95
+ /**
96
+ * Extract
97
+ * @param dracoGeometry
98
+ * @param geometry_type
99
+ * @param options
100
+ * @returns
101
+ */
102
+ _getDracoLoaderData(dracoGeometry, geometry_type, options) {
103
+ const metadata = this._getTopLevelMetadata(dracoGeometry);
104
+ const attributes = this._getDracoAttributes(dracoGeometry, options);
105
+ return {
106
+ geometry_type,
107
+ num_attributes: dracoGeometry.num_attributes(),
108
+ num_points: dracoGeometry.num_points(),
109
+ num_faces: dracoGeometry instanceof this.draco.Mesh ? dracoGeometry.num_faces() : 0,
110
+ metadata,
111
+ attributes
112
+ };
125
113
  }
126
- if (dracoGeometry instanceof this.draco.Mesh) {
127
- switch (options.topology) {
128
- case 'triangle-strip':
129
- return {
130
- topology: 'triangle-strip',
131
- mode: 4,
132
- attributes,
133
- indices: {
134
- value: this._getTriangleStripIndices(dracoGeometry),
135
- size: 1
114
+ /**
115
+ * Extract all draco provided information and metadata for each attribute
116
+ * @param dracoGeometry
117
+ * @param options
118
+ * @returns
119
+ */
120
+ _getDracoAttributes(dracoGeometry, options) {
121
+ const dracoAttributes = {};
122
+ for (let attributeId = 0; attributeId < dracoGeometry.num_attributes(); attributeId++) {
123
+ // Note: Draco docs do not seem clear on `GetAttribute` ids just being a zero-based index,
124
+ // but it does seems to work this way
125
+ const dracoAttribute = this.decoder.GetAttribute(dracoGeometry, attributeId);
126
+ const metadata = this._getAttributeMetadata(dracoGeometry, attributeId);
127
+ dracoAttributes[dracoAttribute.unique_id()] = {
128
+ unique_id: dracoAttribute.unique_id(),
129
+ attribute_type: dracoAttribute.attribute_type(),
130
+ data_type: dracoAttribute.data_type(),
131
+ num_components: dracoAttribute.num_components(),
132
+ byte_offset: dracoAttribute.byte_offset(),
133
+ byte_stride: dracoAttribute.byte_stride(),
134
+ normalized: dracoAttribute.normalized(),
135
+ attribute_index: attributeId,
136
+ metadata
137
+ };
138
+ // Add transformation parameters for any attributes app wants untransformed
139
+ const quantization = this._getQuantizationTransform(dracoAttribute, options);
140
+ if (quantization) {
141
+ dracoAttributes[dracoAttribute.unique_id()].quantization_transform = quantization;
136
142
  }
137
- };
138
- case 'triangle-list':
139
- default:
140
- return {
141
- topology: 'triangle-list',
142
- mode: 5,
143
- attributes,
144
- indices: {
145
- value: this._getTriangleListIndices(dracoGeometry),
146
- size: 1
143
+ const octahedron = this._getOctahedronTransform(dracoAttribute, options);
144
+ if (octahedron) {
145
+ dracoAttributes[dracoAttribute.unique_id()].octahedron_transform = octahedron;
147
146
  }
148
- };
149
- }
147
+ }
148
+ return dracoAttributes;
150
149
  }
151
- return {
152
- topology: 'point-list',
153
- mode: 0,
154
- attributes
155
- };
156
- }
157
- _getMeshAttributes(loaderData, dracoGeometry, options) {
158
- const attributes = {};
159
- for (const loaderAttribute of Object.values(loaderData.attributes)) {
160
- const attributeName = this._deduceAttributeName(loaderAttribute, options);
161
- loaderAttribute.name = attributeName;
162
- const {
163
- value,
164
- size
165
- } = this._getAttributeValues(dracoGeometry, loaderAttribute);
166
- attributes[attributeName] = {
167
- value,
168
- size,
169
- byteOffset: loaderAttribute.byte_offset,
170
- byteStride: loaderAttribute.byte_stride,
171
- normalized: loaderAttribute.normalized
172
- };
150
+ /**
151
+ * Get standard loaders.gl mesh category data
152
+ * Extracts the geometry from draco
153
+ * @param dracoGeometry
154
+ * @param options
155
+ */
156
+ _getMeshData(dracoGeometry, loaderData, options) {
157
+ const attributes = this._getMeshAttributes(loaderData, dracoGeometry, options);
158
+ const positionAttribute = attributes.POSITION;
159
+ if (!positionAttribute) {
160
+ throw new Error('DRACO: No position attribute found.');
161
+ }
162
+ // For meshes, we need indices to define the faces.
163
+ if (dracoGeometry instanceof this.draco.Mesh) {
164
+ switch (options.topology) {
165
+ case 'triangle-strip':
166
+ return {
167
+ topology: 'triangle-strip',
168
+ mode: 4, // GL.TRIANGLES
169
+ attributes,
170
+ indices: {
171
+ value: this._getTriangleStripIndices(dracoGeometry),
172
+ size: 1
173
+ }
174
+ };
175
+ case 'triangle-list':
176
+ default:
177
+ return {
178
+ topology: 'triangle-list',
179
+ mode: 5, // GL.TRIANGLE_STRIP
180
+ attributes,
181
+ indices: {
182
+ value: this._getTriangleListIndices(dracoGeometry),
183
+ size: 1
184
+ }
185
+ };
186
+ }
187
+ }
188
+ // PointCloud - must come last as Mesh inherits from PointCloud
189
+ return {
190
+ topology: 'point-list',
191
+ mode: 0, // GL.POINTS
192
+ attributes
193
+ };
173
194
  }
174
- return attributes;
175
- }
176
- _getTriangleListIndices(dracoGeometry) {
177
- const numFaces = dracoGeometry.num_faces();
178
- const numIndices = numFaces * 3;
179
- const byteLength = numIndices * INDEX_ITEM_SIZE;
180
- const ptr = this.draco._malloc(byteLength);
181
- try {
182
- this.decoder.GetTrianglesUInt32Array(dracoGeometry, byteLength, ptr);
183
- return new Uint32Array(this.draco.HEAPF32.buffer, ptr, numIndices).slice();
184
- } finally {
185
- this.draco._free(ptr);
195
+ _getMeshAttributes(loaderData, dracoGeometry, options) {
196
+ const attributes = {};
197
+ for (const loaderAttribute of Object.values(loaderData.attributes)) {
198
+ const attributeName = this._deduceAttributeName(loaderAttribute, options);
199
+ loaderAttribute.name = attributeName;
200
+ const { value, size } = this._getAttributeValues(dracoGeometry, loaderAttribute);
201
+ attributes[attributeName] = {
202
+ value,
203
+ size,
204
+ byteOffset: loaderAttribute.byte_offset,
205
+ byteStride: loaderAttribute.byte_stride,
206
+ normalized: loaderAttribute.normalized
207
+ };
208
+ }
209
+ return attributes;
186
210
  }
187
- }
188
- _getTriangleStripIndices(dracoGeometry) {
189
- const dracoArray = new this.draco.DracoInt32Array();
190
- try {
191
- this.decoder.GetTriangleStripsFromMesh(dracoGeometry, dracoArray);
192
- return getUint32Array(dracoArray);
193
- } finally {
194
- this.draco.destroy(dracoArray);
211
+ // MESH INDICES EXTRACTION
212
+ /**
213
+ * For meshes, we need indices to define the faces.
214
+ * @param dracoGeometry
215
+ */
216
+ _getTriangleListIndices(dracoGeometry) {
217
+ // Example on how to retrieve mesh and attributes.
218
+ const numFaces = dracoGeometry.num_faces();
219
+ const numIndices = numFaces * 3;
220
+ const byteLength = numIndices * INDEX_ITEM_SIZE;
221
+ const ptr = this.draco._malloc(byteLength);
222
+ try {
223
+ this.decoder.GetTrianglesUInt32Array(dracoGeometry, byteLength, ptr);
224
+ return new Uint32Array(this.draco.HEAPF32.buffer, ptr, numIndices).slice();
225
+ }
226
+ finally {
227
+ this.draco._free(ptr);
228
+ }
195
229
  }
196
- }
197
- _getAttributeValues(dracoGeometry, attribute) {
198
- const TypedArrayCtor = DRACO_DATA_TYPE_TO_TYPED_ARRAY_MAP[attribute.data_type];
199
- const numComponents = attribute.num_components;
200
- const numPoints = dracoGeometry.num_points();
201
- const numValues = numPoints * numComponents;
202
- const byteLength = numValues * TypedArrayCtor.BYTES_PER_ELEMENT;
203
- const dataType = getDracoDataType(this.draco, TypedArrayCtor);
204
- let value;
205
- const ptr = this.draco._malloc(byteLength);
206
- try {
207
- const dracoAttribute = this.decoder.GetAttribute(dracoGeometry, attribute.attribute_index);
208
- this.decoder.GetAttributeDataArrayForAllPoints(dracoGeometry, dracoAttribute, dataType, byteLength, ptr);
209
- value = new TypedArrayCtor(this.draco.HEAPF32.buffer, ptr, numValues).slice();
210
- } finally {
211
- this.draco._free(ptr);
230
+ /**
231
+ * For meshes, we need indices to define the faces.
232
+ * @param dracoGeometry
233
+ */
234
+ _getTriangleStripIndices(dracoGeometry) {
235
+ const dracoArray = new this.draco.DracoInt32Array();
236
+ try {
237
+ /* const numStrips = */ this.decoder.GetTriangleStripsFromMesh(dracoGeometry, dracoArray);
238
+ return getUint32Array(dracoArray);
239
+ }
240
+ finally {
241
+ this.draco.destroy(dracoArray);
242
+ }
212
243
  }
213
- return {
214
- value,
215
- size: numComponents
216
- };
217
- }
218
- _deduceAttributeName(attribute, options) {
219
- const uniqueId = attribute.unique_id;
220
- for (const [attributeName, attributeUniqueId] of Object.entries(options.extraAttributes || {})) {
221
- if (attributeUniqueId === uniqueId) {
222
- return attributeName;
223
- }
244
+ /**
245
+ *
246
+ * @param dracoGeometry
247
+ * @param dracoAttribute
248
+ * @param attributeName
249
+ */
250
+ _getAttributeValues(dracoGeometry, attribute) {
251
+ const TypedArrayCtor = DRACO_DATA_TYPE_TO_TYPED_ARRAY_MAP[attribute.data_type];
252
+ const numComponents = attribute.num_components;
253
+ const numPoints = dracoGeometry.num_points();
254
+ const numValues = numPoints * numComponents;
255
+ const byteLength = numValues * TypedArrayCtor.BYTES_PER_ELEMENT;
256
+ const dataType = getDracoDataType(this.draco, TypedArrayCtor);
257
+ let value;
258
+ const ptr = this.draco._malloc(byteLength);
259
+ try {
260
+ const dracoAttribute = this.decoder.GetAttribute(dracoGeometry, attribute.attribute_index);
261
+ this.decoder.GetAttributeDataArrayForAllPoints(dracoGeometry, dracoAttribute, dataType, byteLength, ptr);
262
+ value = new TypedArrayCtor(this.draco.HEAPF32.buffer, ptr, numValues).slice();
263
+ }
264
+ finally {
265
+ this.draco._free(ptr);
266
+ }
267
+ return { value, size: numComponents };
224
268
  }
225
- const thisAttributeType = attribute.attribute_type;
226
- for (const dracoAttributeConstant in DRACO_TO_GLTF_ATTRIBUTE_NAME_MAP) {
227
- const attributeType = this.draco[dracoAttributeConstant];
228
- if (attributeType === thisAttributeType) {
229
- return DRACO_TO_GLTF_ATTRIBUTE_NAME_MAP[dracoAttributeConstant];
269
+ // Attribute names
270
+ /**
271
+ * DRACO does not store attribute names - We need to deduce an attribute name
272
+ * for each attribute
273
+ _getAttributeNames(
274
+ dracoGeometry: Mesh | PointCloud,
275
+ options: DracoParseOptions
276
+ ): {[unique_id: number]: string} {
277
+ const attributeNames: {[unique_id: number]: string} = {};
278
+ for (let attributeId = 0; attributeId < dracoGeometry.num_attributes(); attributeId++) {
279
+ const dracoAttribute = this.decoder.GetAttribute(dracoGeometry, attributeId);
280
+ const attributeName = this._deduceAttributeName(dracoAttribute, options);
281
+ attributeNames[attributeName] = attributeName;
230
282
  }
283
+ return attributeNames;
231
284
  }
232
- const entryName = options.attributeNameEntry || 'name';
233
- if (attribute.metadata[entryName]) {
234
- return attribute.metadata[entryName].string;
285
+ */
286
+ /**
287
+ * Deduce an attribute name.
288
+ * @note DRACO does not save attribute names, just general type (POSITION, COLOR)
289
+ * to help optimize compression. We generate GLTF compatible names for the Draco-recognized
290
+ * types
291
+ * @param attributeData
292
+ */
293
+ _deduceAttributeName(attribute, options) {
294
+ // Deduce name based on application provided map
295
+ const uniqueId = attribute.unique_id;
296
+ for (const [attributeName, attributeUniqueId] of Object.entries(options.extraAttributes || {})) {
297
+ if (attributeUniqueId === uniqueId) {
298
+ return attributeName;
299
+ }
300
+ }
301
+ // Deduce name based on attribute type
302
+ const thisAttributeType = attribute.attribute_type;
303
+ for (const dracoAttributeConstant in DRACO_TO_GLTF_ATTRIBUTE_NAME_MAP) {
304
+ const attributeType = this.draco[dracoAttributeConstant];
305
+ if (attributeType === thisAttributeType) {
306
+ // TODO - Return unique names if there multiple attributes per type
307
+ // (e.g. multiple TEX_COORDS or COLORS)
308
+ return DRACO_TO_GLTF_ATTRIBUTE_NAME_MAP[dracoAttributeConstant];
309
+ }
310
+ }
311
+ // Look up in metadata
312
+ // TODO - shouldn't this have priority?
313
+ const entryName = options.attributeNameEntry || 'name';
314
+ if (attribute.metadata[entryName]) {
315
+ return attribute.metadata[entryName].string;
316
+ }
317
+ // Attribute of "GENERIC" type, we need to assign some name
318
+ return `CUSTOM_ATTRIBUTE_${uniqueId}`;
235
319
  }
236
- return `CUSTOM_ATTRIBUTE_${uniqueId}`;
237
- }
238
- _getTopLevelMetadata(dracoGeometry) {
239
- const dracoMetadata = this.decoder.GetMetadata(dracoGeometry);
240
- return this._getDracoMetadata(dracoMetadata);
241
- }
242
- _getAttributeMetadata(dracoGeometry, attributeId) {
243
- const dracoMetadata = this.decoder.GetAttributeMetadata(dracoGeometry, attributeId);
244
- return this._getDracoMetadata(dracoMetadata);
245
- }
246
- _getDracoMetadata(dracoMetadata) {
247
- if (!dracoMetadata || !dracoMetadata.ptr) {
248
- return {};
320
+ // METADATA EXTRACTION
321
+ /** Get top level metadata */
322
+ _getTopLevelMetadata(dracoGeometry) {
323
+ const dracoMetadata = this.decoder.GetMetadata(dracoGeometry);
324
+ return this._getDracoMetadata(dracoMetadata);
249
325
  }
250
- const result = {};
251
- const numEntries = this.metadataQuerier.NumEntries(dracoMetadata);
252
- for (let entryIndex = 0; entryIndex < numEntries; entryIndex++) {
253
- const entryName = this.metadataQuerier.GetEntryName(dracoMetadata, entryIndex);
254
- result[entryName] = this._getDracoMetadataField(dracoMetadata, entryName);
326
+ /** Get per attribute metadata */
327
+ _getAttributeMetadata(dracoGeometry, attributeId) {
328
+ const dracoMetadata = this.decoder.GetAttributeMetadata(dracoGeometry, attributeId);
329
+ return this._getDracoMetadata(dracoMetadata);
255
330
  }
256
- return result;
257
- }
258
- _getDracoMetadataField(dracoMetadata, entryName) {
259
- const dracoArray = new this.draco.DracoInt32Array();
260
- try {
261
- this.metadataQuerier.GetIntEntryArray(dracoMetadata, entryName, dracoArray);
262
- const intArray = getInt32Array(dracoArray);
263
- return {
264
- int: this.metadataQuerier.GetIntEntry(dracoMetadata, entryName),
265
- string: this.metadataQuerier.GetStringEntry(dracoMetadata, entryName),
266
- double: this.metadataQuerier.GetDoubleEntry(dracoMetadata, entryName),
267
- intArray
268
- };
269
- } finally {
270
- this.draco.destroy(dracoArray);
331
+ /**
332
+ * Extract metadata field values
333
+ * @param dracoMetadata
334
+ * @returns
335
+ */
336
+ _getDracoMetadata(dracoMetadata) {
337
+ // The not so wonderful world of undocumented Draco APIs :(
338
+ if (!dracoMetadata || !dracoMetadata.ptr) {
339
+ return {};
340
+ }
341
+ const result = {};
342
+ const numEntries = this.metadataQuerier.NumEntries(dracoMetadata);
343
+ for (let entryIndex = 0; entryIndex < numEntries; entryIndex++) {
344
+ const entryName = this.metadataQuerier.GetEntryName(dracoMetadata, entryIndex);
345
+ result[entryName] = this._getDracoMetadataField(dracoMetadata, entryName);
346
+ }
347
+ return result;
271
348
  }
272
- }
273
- _disableAttributeTransforms(options) {
274
- const {
275
- quantizedAttributes = [],
276
- octahedronAttributes = []
277
- } = options;
278
- const skipAttributes = [...quantizedAttributes, ...octahedronAttributes];
279
- for (const dracoAttributeName of skipAttributes) {
280
- this.decoder.SkipAttributeTransform(this.draco[dracoAttributeName]);
349
+ /**
350
+ * Extracts possible values for one metadata entry by name
351
+ * @param dracoMetadata
352
+ * @param entryName
353
+ */
354
+ _getDracoMetadataField(dracoMetadata, entryName) {
355
+ const dracoArray = new this.draco.DracoInt32Array();
356
+ try {
357
+ // Draco metadata fields can hold int32 arrays
358
+ this.metadataQuerier.GetIntEntryArray(dracoMetadata, entryName, dracoArray);
359
+ const intArray = getInt32Array(dracoArray);
360
+ return {
361
+ int: this.metadataQuerier.GetIntEntry(dracoMetadata, entryName),
362
+ string: this.metadataQuerier.GetStringEntry(dracoMetadata, entryName),
363
+ double: this.metadataQuerier.GetDoubleEntry(dracoMetadata, entryName),
364
+ intArray
365
+ };
366
+ }
367
+ finally {
368
+ this.draco.destroy(dracoArray);
369
+ }
281
370
  }
282
- }
283
- _getQuantizationTransform(dracoAttribute, options) {
284
- const {
285
- quantizedAttributes = []
286
- } = options;
287
- const attribute_type = dracoAttribute.attribute_type();
288
- const skip = quantizedAttributes.map(type => this.decoder[type]).includes(attribute_type);
289
- if (skip) {
290
- const transform = new this.draco.AttributeQuantizationTransform();
291
- try {
292
- if (transform.InitFromAttribute(dracoAttribute)) {
293
- return {
294
- quantization_bits: transform.quantization_bits(),
295
- range: transform.range(),
296
- min_values: new Float32Array([1, 2, 3]).map(i => transform.min_value(i))
297
- };
371
+ // QUANTIZED ATTRIBUTE SUPPORT (NO DECOMPRESSION)
372
+ /** Skip transforms for specific attribute types */
373
+ _disableAttributeTransforms(options) {
374
+ const { quantizedAttributes = [], octahedronAttributes = [] } = options;
375
+ const skipAttributes = [...quantizedAttributes, ...octahedronAttributes];
376
+ for (const dracoAttributeName of skipAttributes) {
377
+ this.decoder.SkipAttributeTransform(this.draco[dracoAttributeName]);
298
378
  }
299
- } finally {
300
- this.draco.destroy(transform);
301
- }
302
379
  }
303
- return null;
304
- }
305
- _getOctahedronTransform(dracoAttribute, options) {
306
- const {
307
- octahedronAttributes = []
308
- } = options;
309
- const attribute_type = dracoAttribute.attribute_type();
310
- const octahedron = octahedronAttributes.map(type => this.decoder[type]).includes(attribute_type);
311
- if (octahedron) {
312
- const transform = new this.draco.AttributeQuantizationTransform();
313
- try {
314
- if (transform.InitFromAttribute(dracoAttribute)) {
315
- return {
316
- quantization_bits: transform.quantization_bits()
317
- };
380
+ /**
381
+ * Extract (and apply?) Position Transform
382
+ * @todo not used
383
+ */
384
+ _getQuantizationTransform(dracoAttribute, options) {
385
+ const { quantizedAttributes = [] } = options;
386
+ const attribute_type = dracoAttribute.attribute_type();
387
+ const skip = quantizedAttributes.map((type) => this.decoder[type]).includes(attribute_type);
388
+ if (skip) {
389
+ const transform = new this.draco.AttributeQuantizationTransform();
390
+ try {
391
+ if (transform.InitFromAttribute(dracoAttribute)) {
392
+ return {
393
+ quantization_bits: transform.quantization_bits(),
394
+ range: transform.range(),
395
+ min_values: new Float32Array([1, 2, 3]).map((i) => transform.min_value(i))
396
+ };
397
+ }
398
+ }
399
+ finally {
400
+ this.draco.destroy(transform);
401
+ }
318
402
  }
319
- } finally {
320
- this.draco.destroy(transform);
321
- }
403
+ return null;
404
+ }
405
+ _getOctahedronTransform(dracoAttribute, options) {
406
+ const { octahedronAttributes = [] } = options;
407
+ const attribute_type = dracoAttribute.attribute_type();
408
+ const octahedron = octahedronAttributes
409
+ .map((type) => this.decoder[type])
410
+ .includes(attribute_type);
411
+ if (octahedron) {
412
+ const transform = new this.draco.AttributeQuantizationTransform();
413
+ try {
414
+ if (transform.InitFromAttribute(dracoAttribute)) {
415
+ return {
416
+ quantization_bits: transform.quantization_bits()
417
+ };
418
+ }
419
+ }
420
+ finally {
421
+ this.draco.destroy(transform);
422
+ }
423
+ }
424
+ return null;
322
425
  }
323
- return null;
324
- }
325
426
  }
427
+ /**
428
+ * Get draco specific data type by TypedArray constructor type
429
+ * @param attributeType
430
+ * @returns draco specific data type
431
+ */
326
432
  function getDracoDataType(draco, attributeType) {
327
- switch (attributeType) {
328
- case Float32Array:
329
- return draco.DT_FLOAT32;
330
- case Int8Array:
331
- return draco.DT_INT8;
332
- case Int16Array:
333
- return draco.DT_INT16;
334
- case Int32Array:
335
- return draco.DT_INT32;
336
- case Uint8Array:
337
- return draco.DT_UINT8;
338
- case Uint16Array:
339
- return draco.DT_UINT16;
340
- case Uint32Array:
341
- return draco.DT_UINT32;
342
- default:
343
- return draco.DT_INVALID;
344
- }
433
+ switch (attributeType) {
434
+ case Float32Array:
435
+ return draco.DT_FLOAT32;
436
+ case Int8Array:
437
+ return draco.DT_INT8;
438
+ case Int16Array:
439
+ return draco.DT_INT16;
440
+ case Int32Array:
441
+ return draco.DT_INT32;
442
+ case Uint8Array:
443
+ return draco.DT_UINT8;
444
+ case Uint16Array:
445
+ return draco.DT_UINT16;
446
+ case Uint32Array:
447
+ return draco.DT_UINT32;
448
+ default:
449
+ return draco.DT_INVALID;
450
+ }
345
451
  }
452
+ /**
453
+ * Copy a Draco int32 array into a JS typed array
454
+ */
346
455
  function getInt32Array(dracoArray) {
347
- const numValues = dracoArray.size();
348
- const intArray = new Int32Array(numValues);
349
- for (let i = 0; i < numValues; i++) {
350
- intArray[i] = dracoArray.GetValue(i);
351
- }
352
- return intArray;
456
+ const numValues = dracoArray.size();
457
+ const intArray = new Int32Array(numValues);
458
+ for (let i = 0; i < numValues; i++) {
459
+ intArray[i] = dracoArray.GetValue(i);
460
+ }
461
+ return intArray;
353
462
  }
463
+ /**
464
+ * Copy a Draco int32 array into a JS typed array
465
+ */
354
466
  function getUint32Array(dracoArray) {
355
- const numValues = dracoArray.size();
356
- const intArray = new Int32Array(numValues);
357
- for (let i = 0; i < numValues; i++) {
358
- intArray[i] = dracoArray.GetValue(i);
359
- }
360
- return intArray;
467
+ const numValues = dracoArray.size();
468
+ const intArray = new Int32Array(numValues);
469
+ for (let i = 0; i < numValues; i++) {
470
+ intArray[i] = dracoArray.GetValue(i);
471
+ }
472
+ return intArray;
361
473
  }
362
- //# sourceMappingURL=draco-parser.js.map