@loaders.gl/draco 4.0.0-alpha.9 → 4.0.0-beta.1

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