@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,269 +1,335 @@
1
+ // Native Draco attribute names to GLTF attribute names.
1
2
  const GLTF_TO_DRACO_ATTRIBUTE_NAME_MAP = {
2
- POSITION: 'POSITION',
3
- NORMAL: 'NORMAL',
4
- COLOR_0: 'COLOR',
5
- TEXCOORD_0: 'TEX_COORD'
3
+ POSITION: 'POSITION',
4
+ NORMAL: 'NORMAL',
5
+ COLOR_0: 'COLOR',
6
+ TEXCOORD_0: 'TEX_COORD'
6
7
  };
7
- const noop = () => {};
8
+ const noop = () => { };
8
9
  export default class DracoBuilder {
9
- constructor(draco) {
10
- this.draco = void 0;
11
- this.dracoEncoder = void 0;
12
- this.dracoMeshBuilder = void 0;
13
- this.dracoMetadataBuilder = void 0;
14
- this.log = void 0;
15
- this.draco = draco;
16
- this.dracoEncoder = new this.draco.Encoder();
17
- this.dracoMeshBuilder = new this.draco.MeshBuilder();
18
- this.dracoMetadataBuilder = new this.draco.MetadataBuilder();
19
- }
20
- destroy() {
21
- this.destroyEncodedObject(this.dracoMeshBuilder);
22
- this.destroyEncodedObject(this.dracoEncoder);
23
- this.destroyEncodedObject(this.dracoMetadataBuilder);
24
- this.dracoMeshBuilder = null;
25
- this.dracoEncoder = null;
26
- this.draco = null;
27
- }
28
- destroyEncodedObject(object) {
29
- if (object) {
30
- this.draco.destroy(object);
10
+ // draco - the draco decoder, either import `draco3d` or load dynamically
11
+ constructor(draco) {
12
+ this.draco = draco;
13
+ this.dracoEncoder = new this.draco.Encoder();
14
+ this.dracoMeshBuilder = new this.draco.MeshBuilder();
15
+ this.dracoMetadataBuilder = new this.draco.MetadataBuilder();
31
16
  }
32
- }
33
- encodeSync(mesh) {
34
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
35
- this.log = noop;
36
- this._setOptions(options);
37
- return options.pointcloud ? this._encodePointCloud(mesh, options) : this._encodeMesh(mesh, options);
38
- }
39
- _getAttributesFromMesh(mesh) {
40
- const attributes = {
41
- ...mesh,
42
- ...mesh.attributes
43
- };
44
- if (mesh.indices) {
45
- attributes.indices = mesh.indices;
17
+ destroy() {
18
+ this.destroyEncodedObject(this.dracoMeshBuilder);
19
+ this.destroyEncodedObject(this.dracoEncoder);
20
+ this.destroyEncodedObject(this.dracoMetadataBuilder);
21
+ // @ts-ignore
22
+ this.dracoMeshBuilder = null;
23
+ // @ts-ignore
24
+ this.dracoEncoder = null;
25
+ // @ts-ignore
26
+ this.draco = null;
46
27
  }
47
- return attributes;
48
- }
49
- _encodePointCloud(pointcloud, options) {
50
- const dracoPointCloud = new this.draco.PointCloud();
51
- if (options.metadata) {
52
- this._addGeometryMetadata(dracoPointCloud, options.metadata);
53
- }
54
- const attributes = this._getAttributesFromMesh(pointcloud);
55
- this._createDracoPointCloud(dracoPointCloud, attributes, options);
56
- const dracoData = new this.draco.DracoInt8Array();
57
- try {
58
- const encodedLen = this.dracoEncoder.EncodePointCloudToDracoBuffer(dracoPointCloud, false, dracoData);
59
- if (!(encodedLen > 0)) {
60
- throw new Error('Draco encoding failed.');
61
- }
62
- this.log(`DRACO encoded ${dracoPointCloud.num_points()} points
63
- with ${dracoPointCloud.num_attributes()} attributes into ${encodedLen} bytes`);
64
- return dracoInt8ArrayToArrayBuffer(dracoData);
65
- } finally {
66
- this.destroyEncodedObject(dracoData);
67
- this.destroyEncodedObject(dracoPointCloud);
28
+ // TBD - when does this need to be called?
29
+ destroyEncodedObject(object) {
30
+ if (object) {
31
+ this.draco.destroy(object);
32
+ }
68
33
  }
69
- }
70
- _encodeMesh(mesh, options) {
71
- const dracoMesh = new this.draco.Mesh();
72
- if (options.metadata) {
73
- this._addGeometryMetadata(dracoMesh, options.metadata);
34
+ /**
35
+ * Encode mesh or point cloud
36
+ * @param mesh =({})
37
+ * @param options
38
+ */
39
+ encodeSync(mesh, options = {}) {
40
+ this.log = noop; // TODO
41
+ this._setOptions(options);
42
+ return options.pointcloud
43
+ ? this._encodePointCloud(mesh, options)
44
+ : this._encodeMesh(mesh, options);
74
45
  }
75
- const attributes = this._getAttributesFromMesh(mesh);
76
- this._createDracoMesh(dracoMesh, attributes, options);
77
- const dracoData = new this.draco.DracoInt8Array();
78
- try {
79
- const encodedLen = this.dracoEncoder.EncodeMeshToDracoBuffer(dracoMesh, dracoData);
80
- if (encodedLen <= 0) {
81
- throw new Error('Draco encoding failed.');
82
- }
83
- this.log(`DRACO encoded ${dracoMesh.num_points()} points
84
- with ${dracoMesh.num_attributes()} attributes into ${encodedLen} bytes`);
85
- return dracoInt8ArrayToArrayBuffer(dracoData);
86
- } finally {
87
- this.destroyEncodedObject(dracoData);
88
- this.destroyEncodedObject(dracoMesh);
46
+ // PRIVATE
47
+ _getAttributesFromMesh(mesh) {
48
+ // TODO - Change the encodePointCloud interface instead?
49
+ const attributes = { ...mesh, ...mesh.attributes };
50
+ // Fold indices into the attributes
51
+ if (mesh.indices) {
52
+ attributes.indices = mesh.indices;
53
+ }
54
+ return attributes;
89
55
  }
90
- }
91
- _setOptions(options) {
92
- if ('speed' in options) {
93
- this.dracoEncoder.SetSpeedOptions(...options.speed);
56
+ _encodePointCloud(pointcloud, options) {
57
+ const dracoPointCloud = new this.draco.PointCloud();
58
+ if (options.metadata) {
59
+ this._addGeometryMetadata(dracoPointCloud, options.metadata);
60
+ }
61
+ const attributes = this._getAttributesFromMesh(pointcloud);
62
+ // Build a `DracoPointCloud` from the input data
63
+ this._createDracoPointCloud(dracoPointCloud, attributes, options);
64
+ const dracoData = new this.draco.DracoInt8Array();
65
+ try {
66
+ const encodedLen = this.dracoEncoder.EncodePointCloudToDracoBuffer(dracoPointCloud, false, dracoData);
67
+ if (!(encodedLen > 0)) {
68
+ throw new Error('Draco encoding failed.');
69
+ }
70
+ this.log(`DRACO encoded ${dracoPointCloud.num_points()} points
71
+ with ${dracoPointCloud.num_attributes()} attributes into ${encodedLen} bytes`);
72
+ return dracoInt8ArrayToArrayBuffer(dracoData);
73
+ }
74
+ finally {
75
+ this.destroyEncodedObject(dracoData);
76
+ this.destroyEncodedObject(dracoPointCloud);
77
+ }
94
78
  }
95
- if ('method' in options) {
96
- const dracoMethod = this.draco[options.method || 'MESH_SEQUENTIAL_ENCODING'];
97
- this.dracoEncoder.SetEncodingMethod(dracoMethod);
79
+ _encodeMesh(mesh, options) {
80
+ const dracoMesh = new this.draco.Mesh();
81
+ if (options.metadata) {
82
+ this._addGeometryMetadata(dracoMesh, options.metadata);
83
+ }
84
+ const attributes = this._getAttributesFromMesh(mesh);
85
+ // Build a `DracoMesh` from the input data
86
+ this._createDracoMesh(dracoMesh, attributes, options);
87
+ const dracoData = new this.draco.DracoInt8Array();
88
+ try {
89
+ const encodedLen = this.dracoEncoder.EncodeMeshToDracoBuffer(dracoMesh, dracoData);
90
+ if (encodedLen <= 0) {
91
+ throw new Error('Draco encoding failed.');
92
+ }
93
+ this.log(`DRACO encoded ${dracoMesh.num_points()} points
94
+ with ${dracoMesh.num_attributes()} attributes into ${encodedLen} bytes`);
95
+ return dracoInt8ArrayToArrayBuffer(dracoData);
96
+ }
97
+ finally {
98
+ this.destroyEncodedObject(dracoData);
99
+ this.destroyEncodedObject(dracoMesh);
100
+ }
98
101
  }
99
- if ('quantization' in options) {
100
- for (const attribute in options.quantization) {
101
- const bits = options.quantization[attribute];
102
- const dracoPosition = this.draco[attribute];
103
- this.dracoEncoder.SetAttributeQuantization(dracoPosition, bits);
104
- }
102
+ /**
103
+ * Set encoding options.
104
+ * @param {{speed?: any; method?: any; quantization?: any;}} options
105
+ */
106
+ _setOptions(options) {
107
+ if ('speed' in options) {
108
+ // @ts-ignore
109
+ this.dracoEncoder.SetSpeedOptions(...options.speed);
110
+ }
111
+ if ('method' in options) {
112
+ const dracoMethod = this.draco[options.method || 'MESH_SEQUENTIAL_ENCODING'];
113
+ // assert(dracoMethod)
114
+ this.dracoEncoder.SetEncodingMethod(dracoMethod);
115
+ }
116
+ if ('quantization' in options) {
117
+ for (const attribute in options.quantization) {
118
+ const bits = options.quantization[attribute];
119
+ const dracoPosition = this.draco[attribute];
120
+ this.dracoEncoder.SetAttributeQuantization(dracoPosition, bits);
121
+ }
122
+ }
105
123
  }
106
- }
107
- _createDracoMesh(dracoMesh, attributes, options) {
108
- const optionalMetadata = options.attributesMetadata || {};
109
- try {
110
- const positions = this._getPositionAttribute(attributes);
111
- if (!positions) {
112
- throw new Error('positions');
113
- }
114
- const vertexCount = positions.length / 3;
115
- for (let attributeName in attributes) {
116
- const attribute = attributes[attributeName];
117
- attributeName = GLTF_TO_DRACO_ATTRIBUTE_NAME_MAP[attributeName] || attributeName;
118
- const uniqueId = this._addAttributeToMesh(dracoMesh, attributeName, attribute, vertexCount);
119
- if (uniqueId !== -1) {
120
- this._addAttributeMetadata(dracoMesh, uniqueId, {
121
- name: attributeName,
122
- ...(optionalMetadata[attributeName] || {})
123
- });
124
+ /**
125
+ * @param {Mesh} dracoMesh
126
+ * @param {object} attributes
127
+ * @returns {Mesh}
128
+ */
129
+ _createDracoMesh(dracoMesh, attributes, options) {
130
+ const optionalMetadata = options.attributesMetadata || {};
131
+ try {
132
+ const positions = this._getPositionAttribute(attributes);
133
+ if (!positions) {
134
+ throw new Error('positions');
135
+ }
136
+ const vertexCount = positions.length / 3;
137
+ for (let attributeName in attributes) {
138
+ const attribute = attributes[attributeName];
139
+ attributeName = GLTF_TO_DRACO_ATTRIBUTE_NAME_MAP[attributeName] || attributeName;
140
+ const uniqueId = this._addAttributeToMesh(dracoMesh, attributeName, attribute, vertexCount);
141
+ if (uniqueId !== -1) {
142
+ this._addAttributeMetadata(dracoMesh, uniqueId, {
143
+ name: attributeName,
144
+ ...(optionalMetadata[attributeName] || {})
145
+ });
146
+ }
147
+ }
124
148
  }
125
- }
126
- } catch (error) {
127
- this.destroyEncodedObject(dracoMesh);
128
- throw error;
149
+ catch (error) {
150
+ this.destroyEncodedObject(dracoMesh);
151
+ throw error;
152
+ }
153
+ return dracoMesh;
129
154
  }
130
- return dracoMesh;
131
- }
132
- _createDracoPointCloud(dracoPointCloud, attributes, options) {
133
- const optionalMetadata = options.attributesMetadata || {};
134
- try {
135
- const positions = this._getPositionAttribute(attributes);
136
- if (!positions) {
137
- throw new Error('positions');
138
- }
139
- const vertexCount = positions.length / 3;
140
- for (let attributeName in attributes) {
141
- const attribute = attributes[attributeName];
142
- attributeName = GLTF_TO_DRACO_ATTRIBUTE_NAME_MAP[attributeName] || attributeName;
143
- const uniqueId = this._addAttributeToMesh(dracoPointCloud, attributeName, attribute, vertexCount);
144
- if (uniqueId !== -1) {
145
- this._addAttributeMetadata(dracoPointCloud, uniqueId, {
146
- name: attributeName,
147
- ...(optionalMetadata[attributeName] || {})
148
- });
155
+ /**
156
+ * @param {} dracoPointCloud
157
+ * @param {object} attributes
158
+ */
159
+ _createDracoPointCloud(dracoPointCloud, attributes, options) {
160
+ const optionalMetadata = options.attributesMetadata || {};
161
+ try {
162
+ const positions = this._getPositionAttribute(attributes);
163
+ if (!positions) {
164
+ throw new Error('positions');
165
+ }
166
+ const vertexCount = positions.length / 3;
167
+ for (let attributeName in attributes) {
168
+ const attribute = attributes[attributeName];
169
+ attributeName = GLTF_TO_DRACO_ATTRIBUTE_NAME_MAP[attributeName] || attributeName;
170
+ const uniqueId = this._addAttributeToMesh(dracoPointCloud, attributeName, attribute, vertexCount);
171
+ if (uniqueId !== -1) {
172
+ this._addAttributeMetadata(dracoPointCloud, uniqueId, {
173
+ name: attributeName,
174
+ ...(optionalMetadata[attributeName] || {})
175
+ });
176
+ }
177
+ }
178
+ }
179
+ catch (error) {
180
+ this.destroyEncodedObject(dracoPointCloud);
181
+ throw error;
149
182
  }
150
- }
151
- } catch (error) {
152
- this.destroyEncodedObject(dracoPointCloud);
153
- throw error;
183
+ return dracoPointCloud;
154
184
  }
155
- return dracoPointCloud;
156
- }
157
- _addAttributeToMesh(mesh, attributeName, attribute, vertexCount) {
158
- if (!ArrayBuffer.isView(attribute)) {
159
- return -1;
185
+ /**
186
+ * @param mesh
187
+ * @param attributeName
188
+ * @param attribute
189
+ * @param vertexCount
190
+ */
191
+ _addAttributeToMesh(mesh, attributeName, attribute, vertexCount) {
192
+ if (!ArrayBuffer.isView(attribute)) {
193
+ return -1;
194
+ }
195
+ const type = this._getDracoAttributeType(attributeName);
196
+ // @ts-ignore TODO/fix types
197
+ const size = attribute.length / vertexCount;
198
+ if (type === 'indices') {
199
+ // @ts-ignore TODO/fix types
200
+ const numFaces = attribute.length / 3;
201
+ this.log(`Adding attribute ${attributeName}, size ${numFaces}`);
202
+ // @ts-ignore assumes mesh is a Mesh, not a point cloud
203
+ this.dracoMeshBuilder.AddFacesToMesh(mesh, numFaces, attribute);
204
+ return -1;
205
+ }
206
+ this.log(`Adding attribute ${attributeName}, size ${size}`);
207
+ const builder = this.dracoMeshBuilder;
208
+ const { buffer } = attribute;
209
+ switch (attribute.constructor) {
210
+ case Int8Array:
211
+ return builder.AddInt8Attribute(mesh, type, vertexCount, size, new Int8Array(buffer));
212
+ case Int16Array:
213
+ return builder.AddInt16Attribute(mesh, type, vertexCount, size, new Int16Array(buffer));
214
+ case Int32Array:
215
+ return builder.AddInt32Attribute(mesh, type, vertexCount, size, new Int32Array(buffer));
216
+ case Uint8Array:
217
+ case Uint8ClampedArray:
218
+ return builder.AddUInt8Attribute(mesh, type, vertexCount, size, new Uint8Array(buffer));
219
+ case Uint16Array:
220
+ return builder.AddUInt16Attribute(mesh, type, vertexCount, size, new Uint16Array(buffer));
221
+ case Uint32Array:
222
+ return builder.AddUInt32Attribute(mesh, type, vertexCount, size, new Uint32Array(buffer));
223
+ case Float32Array:
224
+ default:
225
+ return builder.AddFloatAttribute(mesh, type, vertexCount, size, new Float32Array(buffer));
226
+ }
160
227
  }
161
- const type = this._getDracoAttributeType(attributeName);
162
- const size = attribute.length / vertexCount;
163
- if (type === 'indices') {
164
- const numFaces = attribute.length / 3;
165
- this.log(`Adding attribute ${attributeName}, size ${numFaces}`);
166
- this.dracoMeshBuilder.AddFacesToMesh(mesh, numFaces, attribute);
167
- return -1;
228
+ /**
229
+ * DRACO can compress attributes of know type better
230
+ * TODO - expose an attribute type map?
231
+ * @param attributeName
232
+ */
233
+ _getDracoAttributeType(attributeName) {
234
+ switch (attributeName.toLowerCase()) {
235
+ case 'indices':
236
+ return 'indices';
237
+ case 'position':
238
+ case 'positions':
239
+ case 'vertices':
240
+ return this.draco.POSITION;
241
+ case 'normal':
242
+ case 'normals':
243
+ return this.draco.NORMAL;
244
+ case 'color':
245
+ case 'colors':
246
+ return this.draco.COLOR;
247
+ case 'texcoord':
248
+ case 'texcoords':
249
+ return this.draco.TEX_COORD;
250
+ default:
251
+ return this.draco.GENERIC;
252
+ }
168
253
  }
169
- this.log(`Adding attribute ${attributeName}, size ${size}`);
170
- const builder = this.dracoMeshBuilder;
171
- const {
172
- buffer
173
- } = attribute;
174
- switch (attribute.constructor) {
175
- case Int8Array:
176
- return builder.AddInt8Attribute(mesh, type, vertexCount, size, new Int8Array(buffer));
177
- case Int16Array:
178
- return builder.AddInt16Attribute(mesh, type, vertexCount, size, new Int16Array(buffer));
179
- case Int32Array:
180
- return builder.AddInt32Attribute(mesh, type, vertexCount, size, new Int32Array(buffer));
181
- case Uint8Array:
182
- case Uint8ClampedArray:
183
- return builder.AddUInt8Attribute(mesh, type, vertexCount, size, new Uint8Array(buffer));
184
- case Uint16Array:
185
- return builder.AddUInt16Attribute(mesh, type, vertexCount, size, new Uint16Array(buffer));
186
- case Uint32Array:
187
- return builder.AddUInt32Attribute(mesh, type, vertexCount, size, new Uint32Array(buffer));
188
- case Float32Array:
189
- default:
190
- return builder.AddFloatAttribute(mesh, type, vertexCount, size, new Float32Array(buffer));
254
+ _getPositionAttribute(attributes) {
255
+ for (const attributeName in attributes) {
256
+ const attribute = attributes[attributeName];
257
+ const dracoType = this._getDracoAttributeType(attributeName);
258
+ if (dracoType === this.draco.POSITION) {
259
+ return attribute;
260
+ }
261
+ }
262
+ return null;
191
263
  }
192
- }
193
- _getDracoAttributeType(attributeName) {
194
- switch (attributeName.toLowerCase()) {
195
- case 'indices':
196
- return 'indices';
197
- case 'position':
198
- case 'positions':
199
- case 'vertices':
200
- return this.draco.POSITION;
201
- case 'normal':
202
- case 'normals':
203
- return this.draco.NORMAL;
204
- case 'color':
205
- case 'colors':
206
- return this.draco.COLOR;
207
- case 'texcoord':
208
- case 'texcoords':
209
- return this.draco.TEX_COORD;
210
- default:
211
- return this.draco.GENERIC;
264
+ /**
265
+ * Add metadata for the geometry.
266
+ * @param dracoGeometry - WASM Draco Object
267
+ * @param metadata
268
+ */
269
+ _addGeometryMetadata(dracoGeometry, metadata) {
270
+ const dracoMetadata = new this.draco.Metadata();
271
+ this._populateDracoMetadata(dracoMetadata, metadata);
272
+ this.dracoMeshBuilder.AddMetadata(dracoGeometry, dracoMetadata);
212
273
  }
213
- }
214
- _getPositionAttribute(attributes) {
215
- for (const attributeName in attributes) {
216
- const attribute = attributes[attributeName];
217
- const dracoType = this._getDracoAttributeType(attributeName);
218
- if (dracoType === this.draco.POSITION) {
219
- return attribute;
220
- }
274
+ /**
275
+ * Add metadata for an attribute to geometry.
276
+ * @param dracoGeometry - WASM Draco Object
277
+ * @param uniqueAttributeId
278
+ * @param metadata
279
+ */
280
+ _addAttributeMetadata(dracoGeometry, uniqueAttributeId, metadata) {
281
+ // Note: Draco JS IDL doesn't seem to expose draco.AttributeMetadata, however it seems to
282
+ // create such objects automatically from draco.Metadata object.
283
+ const dracoAttributeMetadata = new this.draco.Metadata();
284
+ this._populateDracoMetadata(dracoAttributeMetadata, metadata);
285
+ // Draco3d doc note: Directly add attribute metadata to geometry.
286
+ // You can do this without explicitly adding |GeometryMetadata| to mesh.
287
+ this.dracoMeshBuilder.SetMetadataForAttribute(dracoGeometry, uniqueAttributeId, dracoAttributeMetadata);
221
288
  }
222
- return null;
223
- }
224
- _addGeometryMetadata(dracoGeometry, metadata) {
225
- const dracoMetadata = new this.draco.Metadata();
226
- this._populateDracoMetadata(dracoMetadata, metadata);
227
- this.dracoMeshBuilder.AddMetadata(dracoGeometry, dracoMetadata);
228
- }
229
- _addAttributeMetadata(dracoGeometry, uniqueAttributeId, metadata) {
230
- const dracoAttributeMetadata = new this.draco.Metadata();
231
- this._populateDracoMetadata(dracoAttributeMetadata, metadata);
232
- this.dracoMeshBuilder.SetMetadataForAttribute(dracoGeometry, uniqueAttributeId, dracoAttributeMetadata);
233
- }
234
- _populateDracoMetadata(dracoMetadata, metadata) {
235
- for (const [key, value] of getEntries(metadata)) {
236
- switch (typeof value) {
237
- case 'number':
238
- if (Math.trunc(value) === value) {
239
- this.dracoMetadataBuilder.AddIntEntry(dracoMetadata, key, value);
240
- } else {
241
- this.dracoMetadataBuilder.AddDoubleEntry(dracoMetadata, key, value);
242
- }
243
- break;
244
- case 'object':
245
- if (value instanceof Int32Array) {
246
- this.dracoMetadataBuilder.AddIntEntryArray(dracoMetadata, key, value, value.length);
247
- }
248
- break;
249
- case 'string':
250
- default:
251
- this.dracoMetadataBuilder.AddStringEntry(dracoMetadata, key, value);
252
- }
289
+ /**
290
+ * Add contents of object or map to a WASM Draco Metadata Object
291
+ * @param dracoMetadata - WASM Draco Object
292
+ * @param metadata
293
+ */
294
+ _populateDracoMetadata(dracoMetadata, metadata) {
295
+ for (const [key, value] of getEntries(metadata)) {
296
+ switch (typeof value) {
297
+ case 'number':
298
+ if (Math.trunc(value) === value) {
299
+ this.dracoMetadataBuilder.AddIntEntry(dracoMetadata, key, value);
300
+ }
301
+ else {
302
+ this.dracoMetadataBuilder.AddDoubleEntry(dracoMetadata, key, value);
303
+ }
304
+ break;
305
+ case 'object':
306
+ if (value instanceof Int32Array) {
307
+ this.dracoMetadataBuilder.AddIntEntryArray(dracoMetadata, key, value, value.length);
308
+ }
309
+ break;
310
+ case 'string':
311
+ default:
312
+ this.dracoMetadataBuilder.AddStringEntry(dracoMetadata, key, value);
313
+ }
314
+ }
253
315
  }
254
- }
255
316
  }
317
+ // HELPER FUNCTIONS
318
+ /**
319
+ * Copy encoded data to buffer
320
+ * @param dracoData
321
+ */
256
322
  function dracoInt8ArrayToArrayBuffer(dracoData) {
257
- const byteLength = dracoData.size();
258
- const outputBuffer = new ArrayBuffer(byteLength);
259
- const outputData = new Int8Array(outputBuffer);
260
- for (let i = 0; i < byteLength; ++i) {
261
- outputData[i] = dracoData.GetValue(i);
262
- }
263
- return outputBuffer;
323
+ const byteLength = dracoData.size();
324
+ const outputBuffer = new ArrayBuffer(byteLength);
325
+ const outputData = new Int8Array(outputBuffer);
326
+ for (let i = 0; i < byteLength; ++i) {
327
+ outputData[i] = dracoData.GetValue(i);
328
+ }
329
+ return outputBuffer;
264
330
  }
331
+ /** Enable iteration over either an object or a map */
265
332
  function getEntries(container) {
266
- const hasEntriesFunc = container.entries && !container.hasOwnProperty('entries');
267
- return hasEntriesFunc ? container.entries() : Object.entries(container);
333
+ const hasEntriesFunc = container.entries && !container.hasOwnProperty('entries');
334
+ return hasEntriesFunc ? container.entries() : Object.entries(container);
268
335
  }
269
- //# sourceMappingURL=draco-builder.js.map