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