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