@loaders.gl/draco 4.0.0-alpha.5 → 4.0.0-alpha.7

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 (127) hide show
  1. package/dist/bundle.js +2 -2
  2. package/dist/dist.min.js +3 -3
  3. package/dist/dist.min.js.map +3 -3
  4. package/dist/draco-loader.d.ts +4 -14
  5. package/dist/draco-loader.d.ts.map +1 -1
  6. package/dist/draco-loader.js +26 -21
  7. package/dist/draco-worker-node.js +190 -0
  8. package/dist/draco-worker-node.js.map +7 -0
  9. package/dist/draco-worker.js +1 -1
  10. package/dist/draco-worker.js.map +3 -3
  11. package/dist/draco-writer-worker-node.js +192 -0
  12. package/dist/draco-writer-worker-node.js.map +7 -0
  13. package/dist/draco-writer-worker.js +4 -0
  14. package/dist/draco-writer-worker.js.map +7 -0
  15. package/dist/draco-writer.d.ts +12 -3
  16. package/dist/draco-writer.d.ts.map +1 -1
  17. package/dist/draco-writer.js +40 -28
  18. package/dist/draco3d/draco3d-types.d.ts +1 -1
  19. package/dist/draco3d/draco3d-types.d.ts.map +1 -1
  20. package/dist/draco3d/draco3d-types.js +47 -44
  21. package/dist/es5/bundle.js +6 -0
  22. package/dist/es5/bundle.js.map +1 -0
  23. package/dist/es5/draco-loader.js +33 -0
  24. package/dist/es5/draco-loader.js.map +1 -0
  25. package/dist/es5/draco-writer.js +63 -0
  26. package/dist/es5/draco-writer.js.map +1 -0
  27. package/dist/es5/draco3d/draco3d-types.js +51 -0
  28. package/dist/es5/draco3d/draco3d-types.js.map +1 -0
  29. package/dist/es5/index.js +78 -0
  30. package/dist/es5/index.js.map +1 -0
  31. package/dist/es5/lib/draco-builder.js +324 -0
  32. package/dist/es5/lib/draco-builder.js.map +1 -0
  33. package/dist/es5/lib/draco-module-loader.js +183 -0
  34. package/dist/es5/lib/draco-module-loader.js.map +1 -0
  35. package/dist/es5/lib/draco-parser.js +435 -0
  36. package/dist/es5/lib/draco-parser.js.map +1 -0
  37. package/dist/es5/lib/draco-types.js +2 -0
  38. package/dist/es5/lib/draco-types.js.map +1 -0
  39. package/dist/es5/lib/utils/get-draco-schema.js +47 -0
  40. package/dist/es5/lib/utils/get-draco-schema.js.map +1 -0
  41. package/dist/es5/lib/utils/version.js +9 -0
  42. package/dist/es5/lib/utils/version.js.map +1 -0
  43. package/dist/es5/workers/draco-worker-node.js +7 -0
  44. package/dist/es5/workers/draco-worker-node.js.map +1 -0
  45. package/dist/es5/workers/draco-worker.js +6 -0
  46. package/dist/es5/workers/draco-worker.js.map +1 -0
  47. package/dist/es5/workers/draco-writer-worker-node.js +54 -0
  48. package/dist/es5/workers/draco-writer-worker-node.js.map +1 -0
  49. package/dist/es5/workers/draco-writer-worker.js +53 -0
  50. package/dist/es5/workers/draco-writer-worker.js.map +1 -0
  51. package/dist/esm/bundle.js +4 -0
  52. package/dist/esm/bundle.js.map +1 -0
  53. package/dist/esm/draco-loader.js +23 -0
  54. package/dist/esm/draco-loader.js.map +1 -0
  55. package/dist/esm/draco-writer.js +31 -0
  56. package/dist/esm/draco-writer.js.map +1 -0
  57. package/dist/esm/draco3d/draco3d-types.js +41 -0
  58. package/dist/esm/draco3d/draco3d-types.js.map +1 -0
  59. package/dist/esm/index.js +34 -0
  60. package/dist/esm/index.js.map +1 -0
  61. package/dist/esm/lib/draco-builder.js +268 -0
  62. package/dist/esm/lib/draco-builder.js.map +1 -0
  63. package/dist/esm/lib/draco-module-loader.js +76 -0
  64. package/dist/esm/lib/draco-module-loader.js.map +1 -0
  65. package/dist/esm/lib/draco-parser.js +363 -0
  66. package/dist/esm/lib/draco-parser.js.map +1 -0
  67. package/dist/esm/lib/draco-types.js +2 -0
  68. package/dist/esm/lib/draco-types.js.map +1 -0
  69. package/dist/esm/lib/utils/get-draco-schema.js +41 -0
  70. package/dist/esm/lib/utils/get-draco-schema.js.map +1 -0
  71. package/dist/esm/lib/utils/version.js +2 -0
  72. package/dist/esm/lib/utils/version.js.map +1 -0
  73. package/dist/esm/workers/draco-worker-node.js +5 -0
  74. package/dist/esm/workers/draco-worker-node.js.map +1 -0
  75. package/dist/esm/workers/draco-worker.js +4 -0
  76. package/dist/esm/workers/draco-worker.js.map +1 -0
  77. package/dist/esm/workers/draco-writer-worker-node.js +31 -0
  78. package/dist/esm/workers/draco-writer-worker-node.js.map +1 -0
  79. package/dist/esm/workers/draco-writer-worker.js +30 -0
  80. package/dist/esm/workers/draco-writer-worker.js.map +1 -0
  81. package/dist/index.d.ts +14 -13
  82. package/dist/index.d.ts.map +1 -1
  83. package/dist/index.js +44 -21
  84. package/dist/lib/draco-builder.d.ts +1 -1
  85. package/dist/lib/draco-builder.d.ts.map +1 -1
  86. package/dist/lib/draco-builder.js +313 -329
  87. package/dist/lib/draco-module-loader.d.ts.map +1 -1
  88. package/dist/lib/draco-module-loader.js +81 -73
  89. package/dist/lib/draco-parser.d.ts +1 -1
  90. package/dist/lib/draco-parser.d.ts.map +1 -1
  91. package/dist/lib/draco-parser.js +440 -416
  92. package/dist/lib/draco-types.d.ts +6 -6
  93. package/dist/lib/draco-types.d.ts.map +1 -1
  94. package/dist/lib/draco-types.js +3 -2
  95. package/dist/lib/utils/get-draco-schema.js +35 -41
  96. package/dist/lib/utils/version.js +7 -2
  97. package/dist/workers/draco-worker-node.d.ts +2 -0
  98. package/dist/workers/draco-worker-node.d.ts.map +1 -0
  99. package/dist/workers/draco-worker-node.js +7 -0
  100. package/dist/workers/draco-worker.js +5 -4
  101. package/dist/workers/draco-writer-worker-node.d.ts +2 -0
  102. package/dist/workers/draco-writer-worker-node.d.ts.map +1 -0
  103. package/dist/workers/draco-writer-worker-node.js +28 -0
  104. package/dist/workers/draco-writer-worker.d.ts +2 -0
  105. package/dist/workers/draco-writer-worker.d.ts.map +1 -0
  106. package/dist/workers/draco-writer-worker.js +26 -0
  107. package/package.json +17 -10
  108. package/src/draco-loader.ts +5 -3
  109. package/src/draco-writer.ts +17 -12
  110. package/src/index.ts +19 -1
  111. package/src/lib/draco-module-loader.ts +9 -5
  112. package/src/lib/utils/get-draco-schema.ts +7 -6
  113. package/src/workers/draco-worker-node.ts +6 -0
  114. package/src/workers/draco-writer-worker-node.ts +27 -0
  115. package/src/workers/draco-writer-worker.ts +25 -0
  116. package/dist/bundle.js.map +0 -1
  117. package/dist/draco-loader.js.map +0 -1
  118. package/dist/draco-writer.js.map +0 -1
  119. package/dist/draco3d/draco3d-types.js.map +0 -1
  120. package/dist/index.js.map +0 -1
  121. package/dist/lib/draco-builder.js.map +0 -1
  122. package/dist/lib/draco-module-loader.js.map +0 -1
  123. package/dist/lib/draco-parser.js.map +0 -1
  124. package/dist/lib/draco-types.js.map +0 -1
  125. package/dist/lib/utils/get-draco-schema.js.map +0 -1
  126. package/dist/lib/utils/version.js.map +0 -1
  127. package/dist/workers/draco-worker.js.map +0 -1
@@ -1,452 +1,476 @@
1
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
- import { getMeshBoundingBox } from '@loaders.gl/schema';
3
- import { getDracoSchema } from './utils/get-draco-schema';
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
4
8
  const GEOMETRY_TYPE = {
5
- TRIANGULAR_MESH: 0,
6
- POINT_CLOUD: 1
9
+ TRIANGULAR_MESH: 0,
10
+ POINT_CLOUD: 1
7
11
  };
12
+ // Native Draco attribute names to GLTF attribute names.
8
13
  const DRACO_TO_GLTF_ATTRIBUTE_NAME_MAP = {
9
- POSITION: 'POSITION',
10
- NORMAL: 'NORMAL',
11
- COLOR: 'COLOR_0',
12
- TEX_COORD: 'TEXCOORD_0'
14
+ POSITION: 'POSITION',
15
+ NORMAL: 'NORMAL',
16
+ COLOR: 'COLOR_0',
17
+ TEX_COORD: 'TEXCOORD_0'
13
18
  };
14
19
  const DRACO_DATA_TYPE_TO_TYPED_ARRAY_MAP = {
15
- 1: Int8Array,
16
- 2: Uint8Array,
17
- 3: Int16Array,
18
- 4: Uint16Array,
19
- 5: Int32Array,
20
- 6: Uint32Array,
21
- 9: Float32Array
20
+ 1: Int8Array,
21
+ 2: Uint8Array,
22
+ 3: Int16Array,
23
+ 4: Uint16Array,
24
+ 5: Int32Array,
25
+ 6: Uint32Array,
26
+ 9: Float32Array
22
27
  };
23
28
  const INDEX_ITEM_SIZE = 4;
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
- }
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();
95
35
  }
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
- }
36
+ /**
37
+ * Destroy draco resources
38
+ */
39
+ destroy() {
40
+ this.draco.destroy(this.decoder);
41
+ this.draco.destroy(this.metadataQuerier);
42
+ }
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
+ }
144
95
  }
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.');
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
+ };
156
115
  }
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
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;
168
144
  }
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
145
+ const octahedron = this._getOctahedronTransform(dracoAttribute, options);
146
+ if (octahedron) {
147
+ dracoAttributes[dracoAttribute.unique_id()].octahedron_transform = octahedron;
180
148
  }
181
- };
182
- }
149
+ }
150
+ return dracoAttributes;
183
151
  }
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
- };
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
+ };
212
196
  }
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);
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;
229
212
  }
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);
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
+ }
240
231
  }
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);
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
+ }
260
245
  }
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
- }
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 };
275
270
  }
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];
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;
284
284
  }
285
+ return attributeNames;
285
286
  }
286
-
287
- const entryName = options.attributeNameEntry || 'name';
288
-
289
- if (attribute.metadata[entryName]) {
290
- return attribute.metadata[entryName].string;
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}`;
291
321
  }
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 {};
322
+ // METADATA EXTRACTION
323
+ /** Get top level metadata */
324
+ _getTopLevelMetadata(dracoGeometry) {
325
+ const dracoMetadata = this.decoder.GetMetadata(dracoGeometry);
326
+ return this._getDracoMetadata(dracoMetadata);
309
327
  }
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);
328
+ /** Get per attribute metadata */
329
+ _getAttributeMetadata(dracoGeometry, attributeId) {
330
+ const dracoMetadata = this.decoder.GetAttributeMetadata(dracoGeometry, attributeId);
331
+ return this._getDracoMetadata(dracoMetadata);
317
332
  }
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);
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;
336
350
  }
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]);
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
+ }
348
372
  }
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
- };
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]);
368
380
  }
369
- } finally {
370
- this.draco.destroy(transform);
371
- }
372
381
  }
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
- };
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
+ }
392
404
  }
393
- } finally {
394
- this.draco.destroy(transform);
395
- }
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
+ }
425
+ }
426
+ return null;
396
427
  }
397
-
398
- return null;
399
- }
400
-
401
428
  }
402
-
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
+ */
403
435
  function getDracoDataType(draco, attributeType) {
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
- }
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
+ }
429
454
  }
430
-
455
+ /**
456
+ * Copy a Draco int32 array into a JS typed array
457
+ */
431
458
  function getInt32Array(dracoArray) {
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;
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;
440
465
  }
441
-
466
+ /**
467
+ * Copy a Draco int32 array into a JS typed array
468
+ */
442
469
  function getUint32Array(dracoArray) {
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;
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;
451
476
  }
452
- //# sourceMappingURL=draco-parser.js.map