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