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