@loaders.gl/draco 4.0.2 → 4.0.4
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 +1 -1
- package/dist/draco-worker-node.js +65897 -94
- package/dist/draco-worker-node.js.map +4 -4
- package/dist/draco-worker.js +1043 -1
- package/dist/draco-worker.js.map +4 -4
- package/dist/draco-writer-worker-node.js +65562 -96
- package/dist/draco-writer-worker-node.js.map +4 -4
- package/dist/draco-writer-worker.js +710 -3
- package/dist/draco-writer-worker.js.map +4 -4
- package/dist/draco-writer.d.ts +2 -2
- package/dist/draco-writer.d.ts.map +1 -1
- package/dist/draco-writer.js.map +1 -1
- package/package.json +10 -10
- package/src/draco-writer.ts +2 -2
|
@@ -1,4 +1,711 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
(() => {
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
10
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
|
|
29
|
+
// (disabled):../worker-utils/src/lib/node/require-utils.node
|
|
30
|
+
var require_require_utils = __commonJS({
|
|
31
|
+
"(disabled):../worker-utils/src/lib/node/require-utils.node"() {
|
|
32
|
+
"use strict";
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// ../worker-utils/src/lib/env-utils/version.ts
|
|
37
|
+
function getVersion() {
|
|
38
|
+
if (!globalThis._loadersgl_?.version) {
|
|
39
|
+
globalThis._loadersgl_ = globalThis._loadersgl_ || {};
|
|
40
|
+
if (false) {
|
|
41
|
+
console.warn(
|
|
42
|
+
"loaders.gl: The __VERSION__ variable is not injected using babel plugin. Latest unstable workers would be fetched from the CDN."
|
|
43
|
+
);
|
|
44
|
+
globalThis._loadersgl_.version = NPM_TAG;
|
|
45
|
+
} else {
|
|
46
|
+
globalThis._loadersgl_.version = "4.0.4";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return globalThis._loadersgl_.version;
|
|
50
|
+
}
|
|
51
|
+
var VERSION = getVersion();
|
|
52
|
+
|
|
53
|
+
// ../worker-utils/src/lib/env-utils/assert.ts
|
|
54
|
+
function assert(condition, message) {
|
|
55
|
+
if (!condition) {
|
|
56
|
+
throw new Error(message || "loaders.gl assertion failed.");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ../worker-utils/src/lib/env-utils/globals.ts
|
|
61
|
+
var globals = {
|
|
62
|
+
self: typeof self !== "undefined" && self,
|
|
63
|
+
window: typeof window !== "undefined" && window,
|
|
64
|
+
global: typeof global !== "undefined" && global,
|
|
65
|
+
document: typeof document !== "undefined" && document
|
|
66
|
+
};
|
|
67
|
+
var self_ = globals.self || globals.window || globals.global || {};
|
|
68
|
+
var window_ = globals.window || globals.self || globals.global || {};
|
|
69
|
+
var global_ = globals.global || globals.self || globals.window || {};
|
|
70
|
+
var document_ = globals.document || {};
|
|
71
|
+
var isBrowser = (
|
|
72
|
+
// @ts-ignore process.browser
|
|
73
|
+
typeof process !== "object" || String(process) !== "[object process]" || process.browser
|
|
74
|
+
);
|
|
75
|
+
var isWorker = typeof importScripts === "function";
|
|
76
|
+
var isMobile = typeof window !== "undefined" && typeof window.orientation !== "undefined";
|
|
77
|
+
var matches = typeof process !== "undefined" && process.version && /v([0-9]*)/.exec(process.version);
|
|
78
|
+
var nodeVersion = matches && parseFloat(matches[1]) || 0;
|
|
79
|
+
|
|
80
|
+
// ../worker-utils/src/lib/node/worker_threads-browser.ts
|
|
81
|
+
var parentPort = null;
|
|
82
|
+
|
|
83
|
+
// ../worker-utils/src/lib/worker-utils/get-transfer-list.ts
|
|
84
|
+
function getTransferList(object, recursive = true, transfers) {
|
|
85
|
+
const transfersSet = transfers || /* @__PURE__ */ new Set();
|
|
86
|
+
if (!object) {
|
|
87
|
+
} else if (isTransferable(object)) {
|
|
88
|
+
transfersSet.add(object);
|
|
89
|
+
} else if (isTransferable(object.buffer)) {
|
|
90
|
+
transfersSet.add(object.buffer);
|
|
91
|
+
} else if (ArrayBuffer.isView(object)) {
|
|
92
|
+
} else if (recursive && typeof object === "object") {
|
|
93
|
+
for (const key in object) {
|
|
94
|
+
getTransferList(object[key], recursive, transfersSet);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return transfers === void 0 ? Array.from(transfersSet) : [];
|
|
98
|
+
}
|
|
99
|
+
function isTransferable(object) {
|
|
100
|
+
if (!object) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
if (object instanceof ArrayBuffer) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
if (typeof MessagePort !== "undefined" && object instanceof MessagePort) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
if (typeof ImageBitmap !== "undefined" && object instanceof ImageBitmap) {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
if (typeof OffscreenCanvas !== "undefined" && object instanceof OffscreenCanvas) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ../worker-utils/src/lib/worker-farm/worker-body.ts
|
|
119
|
+
async function getParentPort() {
|
|
120
|
+
return parentPort;
|
|
121
|
+
}
|
|
122
|
+
var onMessageWrapperMap = /* @__PURE__ */ new Map();
|
|
123
|
+
var WorkerBody = class {
|
|
124
|
+
/** Check that we are actually in a worker thread */
|
|
125
|
+
static async inWorkerThread() {
|
|
126
|
+
return typeof self !== "undefined" || Boolean(await getParentPort());
|
|
127
|
+
}
|
|
128
|
+
/*
|
|
129
|
+
* (type: WorkerMessageType, payload: WorkerMessagePayload) => any
|
|
130
|
+
*/
|
|
131
|
+
static set onmessage(onMessage) {
|
|
132
|
+
async function handleMessage(message) {
|
|
133
|
+
const parentPort2 = await getParentPort();
|
|
134
|
+
const { type, payload } = parentPort2 ? message : message.data;
|
|
135
|
+
onMessage(type, payload);
|
|
136
|
+
}
|
|
137
|
+
getParentPort().then((parentPort2) => {
|
|
138
|
+
if (parentPort2) {
|
|
139
|
+
parentPort2.on("message", handleMessage);
|
|
140
|
+
parentPort2.on("exit", () => console.debug("Node worker closing"));
|
|
141
|
+
} else {
|
|
142
|
+
globalThis.onmessage = handleMessage;
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
static async addEventListener(onMessage) {
|
|
147
|
+
let onMessageWrapper = onMessageWrapperMap.get(onMessage);
|
|
148
|
+
if (!onMessageWrapper) {
|
|
149
|
+
onMessageWrapper = async (message) => {
|
|
150
|
+
if (!isKnownMessage(message)) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const parentPort3 = await getParentPort();
|
|
154
|
+
const { type, payload } = parentPort3 ? message : message.data;
|
|
155
|
+
onMessage(type, payload);
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
const parentPort2 = await getParentPort();
|
|
159
|
+
if (parentPort2) {
|
|
160
|
+
console.error("not implemented");
|
|
161
|
+
} else {
|
|
162
|
+
globalThis.addEventListener("message", onMessageWrapper);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
static async removeEventListener(onMessage) {
|
|
166
|
+
const onMessageWrapper = onMessageWrapperMap.get(onMessage);
|
|
167
|
+
onMessageWrapperMap.delete(onMessage);
|
|
168
|
+
const parentPort2 = await getParentPort();
|
|
169
|
+
if (parentPort2) {
|
|
170
|
+
console.error("not implemented");
|
|
171
|
+
} else {
|
|
172
|
+
globalThis.removeEventListener("message", onMessageWrapper);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Send a message from a worker to creating thread (main thread)
|
|
177
|
+
* @param type
|
|
178
|
+
* @param payload
|
|
179
|
+
*/
|
|
180
|
+
static async postMessage(type, payload) {
|
|
181
|
+
const data = { source: "loaders.gl", type, payload };
|
|
182
|
+
const transferList = getTransferList(payload);
|
|
183
|
+
const parentPort2 = await getParentPort();
|
|
184
|
+
if (parentPort2) {
|
|
185
|
+
parentPort2.postMessage(data, transferList);
|
|
186
|
+
} else {
|
|
187
|
+
globalThis.postMessage(data, transferList);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
function isKnownMessage(message) {
|
|
192
|
+
const { type, data } = message;
|
|
193
|
+
return type === "message" && data && typeof data.source === "string" && data.source.startsWith("loaders.gl");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ../worker-utils/src/lib/library-utils/library-utils.ts
|
|
197
|
+
var node = __toESM(require_require_utils(), 1);
|
|
198
|
+
var loadLibraryPromises = {};
|
|
199
|
+
async function loadLibrary(libraryUrl, moduleName = null, options = {}, libraryName = null) {
|
|
200
|
+
if (moduleName) {
|
|
201
|
+
libraryUrl = getLibraryUrl(libraryUrl, moduleName, options, libraryName);
|
|
202
|
+
}
|
|
203
|
+
loadLibraryPromises[libraryUrl] = // eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
204
|
+
loadLibraryPromises[libraryUrl] || loadLibraryFromFile(libraryUrl);
|
|
205
|
+
return await loadLibraryPromises[libraryUrl];
|
|
206
|
+
}
|
|
207
|
+
function getLibraryUrl(library, moduleName, options = {}, libraryName = null) {
|
|
208
|
+
if (!options.useLocalLibraries && library.startsWith("http")) {
|
|
209
|
+
return library;
|
|
210
|
+
}
|
|
211
|
+
libraryName = libraryName || library;
|
|
212
|
+
const modules = options.modules || {};
|
|
213
|
+
if (modules[libraryName]) {
|
|
214
|
+
return modules[libraryName];
|
|
215
|
+
}
|
|
216
|
+
if (!isBrowser) {
|
|
217
|
+
return `modules/${moduleName}/dist/libs/${libraryName}`;
|
|
218
|
+
}
|
|
219
|
+
if (options.CDN) {
|
|
220
|
+
assert(options.CDN.startsWith("http"));
|
|
221
|
+
return `${options.CDN}/${moduleName}@${VERSION}/dist/libs/${libraryName}`;
|
|
222
|
+
}
|
|
223
|
+
if (isWorker) {
|
|
224
|
+
return `../src/libs/${libraryName}`;
|
|
225
|
+
}
|
|
226
|
+
return `modules/${moduleName}/src/libs/${libraryName}`;
|
|
227
|
+
}
|
|
228
|
+
async function loadLibraryFromFile(libraryUrl) {
|
|
229
|
+
if (libraryUrl.endsWith("wasm")) {
|
|
230
|
+
return await loadAsArrayBuffer(libraryUrl);
|
|
231
|
+
}
|
|
232
|
+
if (!isBrowser) {
|
|
233
|
+
try {
|
|
234
|
+
return node && void 0 && await (void 0)(libraryUrl);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
console.error(error);
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (isWorker) {
|
|
241
|
+
return importScripts(libraryUrl);
|
|
242
|
+
}
|
|
243
|
+
const scriptSource = await loadAsText(libraryUrl);
|
|
244
|
+
return loadLibraryFromString(scriptSource, libraryUrl);
|
|
245
|
+
}
|
|
246
|
+
function loadLibraryFromString(scriptSource, id) {
|
|
247
|
+
if (!isBrowser) {
|
|
248
|
+
return void 0 && (void 0)(scriptSource, id);
|
|
249
|
+
}
|
|
250
|
+
if (isWorker) {
|
|
251
|
+
eval.call(globalThis, scriptSource);
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
const script = document.createElement("script");
|
|
255
|
+
script.id = id;
|
|
256
|
+
try {
|
|
257
|
+
script.appendChild(document.createTextNode(scriptSource));
|
|
258
|
+
} catch (e) {
|
|
259
|
+
script.text = scriptSource;
|
|
260
|
+
}
|
|
261
|
+
document.body.appendChild(script);
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
async function loadAsArrayBuffer(url) {
|
|
265
|
+
if (isBrowser || !void 0 || url.startsWith("http")) {
|
|
266
|
+
const response = await fetch(url);
|
|
267
|
+
return await response.arrayBuffer();
|
|
268
|
+
}
|
|
269
|
+
return await (void 0)(url);
|
|
270
|
+
}
|
|
271
|
+
async function loadAsText(url) {
|
|
272
|
+
if (isBrowser || !void 0 || url.startsWith("http")) {
|
|
273
|
+
const response = await fetch(url);
|
|
274
|
+
return await response.text();
|
|
275
|
+
}
|
|
276
|
+
return await (void 0)(url);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// src/lib/draco-builder.ts
|
|
280
|
+
var GLTF_TO_DRACO_ATTRIBUTE_NAME_MAP = {
|
|
281
|
+
POSITION: "POSITION",
|
|
282
|
+
NORMAL: "NORMAL",
|
|
283
|
+
COLOR_0: "COLOR",
|
|
284
|
+
TEXCOORD_0: "TEX_COORD"
|
|
285
|
+
};
|
|
286
|
+
var noop = () => {
|
|
287
|
+
};
|
|
288
|
+
var DracoBuilder = class {
|
|
289
|
+
draco;
|
|
290
|
+
dracoEncoder;
|
|
291
|
+
dracoMeshBuilder;
|
|
292
|
+
dracoMetadataBuilder;
|
|
293
|
+
log;
|
|
294
|
+
// draco - the draco decoder, either import `draco3d` or load dynamically
|
|
295
|
+
constructor(draco) {
|
|
296
|
+
this.draco = draco;
|
|
297
|
+
this.dracoEncoder = new this.draco.Encoder();
|
|
298
|
+
this.dracoMeshBuilder = new this.draco.MeshBuilder();
|
|
299
|
+
this.dracoMetadataBuilder = new this.draco.MetadataBuilder();
|
|
300
|
+
}
|
|
301
|
+
destroy() {
|
|
302
|
+
this.destroyEncodedObject(this.dracoMeshBuilder);
|
|
303
|
+
this.destroyEncodedObject(this.dracoEncoder);
|
|
304
|
+
this.destroyEncodedObject(this.dracoMetadataBuilder);
|
|
305
|
+
this.dracoMeshBuilder = null;
|
|
306
|
+
this.dracoEncoder = null;
|
|
307
|
+
this.draco = null;
|
|
308
|
+
}
|
|
309
|
+
// TBD - when does this need to be called?
|
|
310
|
+
destroyEncodedObject(object) {
|
|
311
|
+
if (object) {
|
|
312
|
+
this.draco.destroy(object);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Encode mesh or point cloud
|
|
317
|
+
* @param mesh =({})
|
|
318
|
+
* @param options
|
|
319
|
+
*/
|
|
320
|
+
encodeSync(mesh, options = {}) {
|
|
321
|
+
this.log = noop;
|
|
322
|
+
this._setOptions(options);
|
|
323
|
+
return options.pointcloud ? this._encodePointCloud(mesh, options) : this._encodeMesh(mesh, options);
|
|
324
|
+
}
|
|
325
|
+
// PRIVATE
|
|
326
|
+
_getAttributesFromMesh(mesh) {
|
|
327
|
+
const attributes = { ...mesh, ...mesh.attributes };
|
|
328
|
+
if (mesh.indices) {
|
|
329
|
+
attributes.indices = mesh.indices;
|
|
330
|
+
}
|
|
331
|
+
return attributes;
|
|
332
|
+
}
|
|
333
|
+
_encodePointCloud(pointcloud, options) {
|
|
334
|
+
const dracoPointCloud = new this.draco.PointCloud();
|
|
335
|
+
if (options.metadata) {
|
|
336
|
+
this._addGeometryMetadata(dracoPointCloud, options.metadata);
|
|
337
|
+
}
|
|
338
|
+
const attributes = this._getAttributesFromMesh(pointcloud);
|
|
339
|
+
this._createDracoPointCloud(dracoPointCloud, attributes, options);
|
|
340
|
+
const dracoData = new this.draco.DracoInt8Array();
|
|
341
|
+
try {
|
|
342
|
+
const encodedLen = this.dracoEncoder.EncodePointCloudToDracoBuffer(
|
|
343
|
+
dracoPointCloud,
|
|
344
|
+
false,
|
|
345
|
+
dracoData
|
|
346
|
+
);
|
|
347
|
+
if (!(encodedLen > 0)) {
|
|
348
|
+
throw new Error("Draco encoding failed.");
|
|
349
|
+
}
|
|
350
|
+
this.log(`DRACO encoded ${dracoPointCloud.num_points()} points
|
|
351
|
+
with ${dracoPointCloud.num_attributes()} attributes into ${encodedLen} bytes`);
|
|
352
|
+
return dracoInt8ArrayToArrayBuffer(dracoData);
|
|
353
|
+
} finally {
|
|
354
|
+
this.destroyEncodedObject(dracoData);
|
|
355
|
+
this.destroyEncodedObject(dracoPointCloud);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
_encodeMesh(mesh, options) {
|
|
359
|
+
const dracoMesh = new this.draco.Mesh();
|
|
360
|
+
if (options.metadata) {
|
|
361
|
+
this._addGeometryMetadata(dracoMesh, options.metadata);
|
|
362
|
+
}
|
|
363
|
+
const attributes = this._getAttributesFromMesh(mesh);
|
|
364
|
+
this._createDracoMesh(dracoMesh, attributes, options);
|
|
365
|
+
const dracoData = new this.draco.DracoInt8Array();
|
|
366
|
+
try {
|
|
367
|
+
const encodedLen = this.dracoEncoder.EncodeMeshToDracoBuffer(dracoMesh, dracoData);
|
|
368
|
+
if (encodedLen <= 0) {
|
|
369
|
+
throw new Error("Draco encoding failed.");
|
|
370
|
+
}
|
|
371
|
+
this.log(`DRACO encoded ${dracoMesh.num_points()} points
|
|
372
|
+
with ${dracoMesh.num_attributes()} attributes into ${encodedLen} bytes`);
|
|
373
|
+
return dracoInt8ArrayToArrayBuffer(dracoData);
|
|
374
|
+
} finally {
|
|
375
|
+
this.destroyEncodedObject(dracoData);
|
|
376
|
+
this.destroyEncodedObject(dracoMesh);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Set encoding options.
|
|
381
|
+
* @param {{speed?: any; method?: any; quantization?: any;}} options
|
|
382
|
+
*/
|
|
383
|
+
_setOptions(options) {
|
|
384
|
+
if ("speed" in options) {
|
|
385
|
+
this.dracoEncoder.SetSpeedOptions(...options.speed);
|
|
386
|
+
}
|
|
387
|
+
if ("method" in options) {
|
|
388
|
+
const dracoMethod = this.draco[options.method || "MESH_SEQUENTIAL_ENCODING"];
|
|
389
|
+
this.dracoEncoder.SetEncodingMethod(dracoMethod);
|
|
390
|
+
}
|
|
391
|
+
if ("quantization" in options) {
|
|
392
|
+
for (const attribute in options.quantization) {
|
|
393
|
+
const bits = options.quantization[attribute];
|
|
394
|
+
const dracoPosition = this.draco[attribute];
|
|
395
|
+
this.dracoEncoder.SetAttributeQuantization(dracoPosition, bits);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* @param {Mesh} dracoMesh
|
|
401
|
+
* @param {object} attributes
|
|
402
|
+
* @returns {Mesh}
|
|
403
|
+
*/
|
|
404
|
+
_createDracoMesh(dracoMesh, attributes, options) {
|
|
405
|
+
const optionalMetadata = options.attributesMetadata || {};
|
|
406
|
+
try {
|
|
407
|
+
const positions = this._getPositionAttribute(attributes);
|
|
408
|
+
if (!positions) {
|
|
409
|
+
throw new Error("positions");
|
|
410
|
+
}
|
|
411
|
+
const vertexCount = positions.length / 3;
|
|
412
|
+
for (let attributeName in attributes) {
|
|
413
|
+
const attribute = attributes[attributeName];
|
|
414
|
+
attributeName = GLTF_TO_DRACO_ATTRIBUTE_NAME_MAP[attributeName] || attributeName;
|
|
415
|
+
const uniqueId = this._addAttributeToMesh(dracoMesh, attributeName, attribute, vertexCount);
|
|
416
|
+
if (uniqueId !== -1) {
|
|
417
|
+
this._addAttributeMetadata(dracoMesh, uniqueId, {
|
|
418
|
+
name: attributeName,
|
|
419
|
+
...optionalMetadata[attributeName] || {}
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
} catch (error) {
|
|
424
|
+
this.destroyEncodedObject(dracoMesh);
|
|
425
|
+
throw error;
|
|
426
|
+
}
|
|
427
|
+
return dracoMesh;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* @param {} dracoPointCloud
|
|
431
|
+
* @param {object} attributes
|
|
432
|
+
*/
|
|
433
|
+
_createDracoPointCloud(dracoPointCloud, attributes, options) {
|
|
434
|
+
const optionalMetadata = options.attributesMetadata || {};
|
|
435
|
+
try {
|
|
436
|
+
const positions = this._getPositionAttribute(attributes);
|
|
437
|
+
if (!positions) {
|
|
438
|
+
throw new Error("positions");
|
|
439
|
+
}
|
|
440
|
+
const vertexCount = positions.length / 3;
|
|
441
|
+
for (let attributeName in attributes) {
|
|
442
|
+
const attribute = attributes[attributeName];
|
|
443
|
+
attributeName = GLTF_TO_DRACO_ATTRIBUTE_NAME_MAP[attributeName] || attributeName;
|
|
444
|
+
const uniqueId = this._addAttributeToMesh(
|
|
445
|
+
dracoPointCloud,
|
|
446
|
+
attributeName,
|
|
447
|
+
attribute,
|
|
448
|
+
vertexCount
|
|
449
|
+
);
|
|
450
|
+
if (uniqueId !== -1) {
|
|
451
|
+
this._addAttributeMetadata(dracoPointCloud, uniqueId, {
|
|
452
|
+
name: attributeName,
|
|
453
|
+
...optionalMetadata[attributeName] || {}
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
} catch (error) {
|
|
458
|
+
this.destroyEncodedObject(dracoPointCloud);
|
|
459
|
+
throw error;
|
|
460
|
+
}
|
|
461
|
+
return dracoPointCloud;
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* @param mesh
|
|
465
|
+
* @param attributeName
|
|
466
|
+
* @param attribute
|
|
467
|
+
* @param vertexCount
|
|
468
|
+
*/
|
|
469
|
+
_addAttributeToMesh(mesh, attributeName, attribute, vertexCount) {
|
|
470
|
+
if (!ArrayBuffer.isView(attribute)) {
|
|
471
|
+
return -1;
|
|
472
|
+
}
|
|
473
|
+
const type = this._getDracoAttributeType(attributeName);
|
|
474
|
+
const size = attribute.length / vertexCount;
|
|
475
|
+
if (type === "indices") {
|
|
476
|
+
const numFaces = attribute.length / 3;
|
|
477
|
+
this.log(`Adding attribute ${attributeName}, size ${numFaces}`);
|
|
478
|
+
this.dracoMeshBuilder.AddFacesToMesh(mesh, numFaces, attribute);
|
|
479
|
+
return -1;
|
|
480
|
+
}
|
|
481
|
+
this.log(`Adding attribute ${attributeName}, size ${size}`);
|
|
482
|
+
const builder = this.dracoMeshBuilder;
|
|
483
|
+
const { buffer } = attribute;
|
|
484
|
+
switch (attribute.constructor) {
|
|
485
|
+
case Int8Array:
|
|
486
|
+
return builder.AddInt8Attribute(mesh, type, vertexCount, size, new Int8Array(buffer));
|
|
487
|
+
case Int16Array:
|
|
488
|
+
return builder.AddInt16Attribute(mesh, type, vertexCount, size, new Int16Array(buffer));
|
|
489
|
+
case Int32Array:
|
|
490
|
+
return builder.AddInt32Attribute(mesh, type, vertexCount, size, new Int32Array(buffer));
|
|
491
|
+
case Uint8Array:
|
|
492
|
+
case Uint8ClampedArray:
|
|
493
|
+
return builder.AddUInt8Attribute(mesh, type, vertexCount, size, new Uint8Array(buffer));
|
|
494
|
+
case Uint16Array:
|
|
495
|
+
return builder.AddUInt16Attribute(mesh, type, vertexCount, size, new Uint16Array(buffer));
|
|
496
|
+
case Uint32Array:
|
|
497
|
+
return builder.AddUInt32Attribute(mesh, type, vertexCount, size, new Uint32Array(buffer));
|
|
498
|
+
case Float32Array:
|
|
499
|
+
default:
|
|
500
|
+
return builder.AddFloatAttribute(mesh, type, vertexCount, size, new Float32Array(buffer));
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* DRACO can compress attributes of know type better
|
|
505
|
+
* TODO - expose an attribute type map?
|
|
506
|
+
* @param attributeName
|
|
507
|
+
*/
|
|
508
|
+
_getDracoAttributeType(attributeName) {
|
|
509
|
+
switch (attributeName.toLowerCase()) {
|
|
510
|
+
case "indices":
|
|
511
|
+
return "indices";
|
|
512
|
+
case "position":
|
|
513
|
+
case "positions":
|
|
514
|
+
case "vertices":
|
|
515
|
+
return this.draco.POSITION;
|
|
516
|
+
case "normal":
|
|
517
|
+
case "normals":
|
|
518
|
+
return this.draco.NORMAL;
|
|
519
|
+
case "color":
|
|
520
|
+
case "colors":
|
|
521
|
+
return this.draco.COLOR;
|
|
522
|
+
case "texcoord":
|
|
523
|
+
case "texcoords":
|
|
524
|
+
return this.draco.TEX_COORD;
|
|
525
|
+
default:
|
|
526
|
+
return this.draco.GENERIC;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
_getPositionAttribute(attributes) {
|
|
530
|
+
for (const attributeName in attributes) {
|
|
531
|
+
const attribute = attributes[attributeName];
|
|
532
|
+
const dracoType = this._getDracoAttributeType(attributeName);
|
|
533
|
+
if (dracoType === this.draco.POSITION) {
|
|
534
|
+
return attribute;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return null;
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Add metadata for the geometry.
|
|
541
|
+
* @param dracoGeometry - WASM Draco Object
|
|
542
|
+
* @param metadata
|
|
543
|
+
*/
|
|
544
|
+
_addGeometryMetadata(dracoGeometry, metadata) {
|
|
545
|
+
const dracoMetadata = new this.draco.Metadata();
|
|
546
|
+
this._populateDracoMetadata(dracoMetadata, metadata);
|
|
547
|
+
this.dracoMeshBuilder.AddMetadata(dracoGeometry, dracoMetadata);
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Add metadata for an attribute to geometry.
|
|
551
|
+
* @param dracoGeometry - WASM Draco Object
|
|
552
|
+
* @param uniqueAttributeId
|
|
553
|
+
* @param metadata
|
|
554
|
+
*/
|
|
555
|
+
_addAttributeMetadata(dracoGeometry, uniqueAttributeId, metadata) {
|
|
556
|
+
const dracoAttributeMetadata = new this.draco.Metadata();
|
|
557
|
+
this._populateDracoMetadata(dracoAttributeMetadata, metadata);
|
|
558
|
+
this.dracoMeshBuilder.SetMetadataForAttribute(
|
|
559
|
+
dracoGeometry,
|
|
560
|
+
uniqueAttributeId,
|
|
561
|
+
dracoAttributeMetadata
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Add contents of object or map to a WASM Draco Metadata Object
|
|
566
|
+
* @param dracoMetadata - WASM Draco Object
|
|
567
|
+
* @param metadata
|
|
568
|
+
*/
|
|
569
|
+
_populateDracoMetadata(dracoMetadata, metadata) {
|
|
570
|
+
for (const [key, value] of getEntries(metadata)) {
|
|
571
|
+
switch (typeof value) {
|
|
572
|
+
case "number":
|
|
573
|
+
if (Math.trunc(value) === value) {
|
|
574
|
+
this.dracoMetadataBuilder.AddIntEntry(dracoMetadata, key, value);
|
|
575
|
+
} else {
|
|
576
|
+
this.dracoMetadataBuilder.AddDoubleEntry(dracoMetadata, key, value);
|
|
577
|
+
}
|
|
578
|
+
break;
|
|
579
|
+
case "object":
|
|
580
|
+
if (value instanceof Int32Array) {
|
|
581
|
+
this.dracoMetadataBuilder.AddIntEntryArray(dracoMetadata, key, value, value.length);
|
|
582
|
+
}
|
|
583
|
+
break;
|
|
584
|
+
case "string":
|
|
585
|
+
default:
|
|
586
|
+
this.dracoMetadataBuilder.AddStringEntry(dracoMetadata, key, value);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
function dracoInt8ArrayToArrayBuffer(dracoData) {
|
|
592
|
+
const byteLength = dracoData.size();
|
|
593
|
+
const outputBuffer = new ArrayBuffer(byteLength);
|
|
594
|
+
const outputData = new Int8Array(outputBuffer);
|
|
595
|
+
for (let i = 0; i < byteLength; ++i) {
|
|
596
|
+
outputData[i] = dracoData.GetValue(i);
|
|
597
|
+
}
|
|
598
|
+
return outputBuffer;
|
|
599
|
+
}
|
|
600
|
+
function getEntries(container) {
|
|
601
|
+
const hasEntriesFunc = container.entries && !container.hasOwnProperty("entries");
|
|
602
|
+
return hasEntriesFunc ? container.entries() : Object.entries(container);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// src/lib/draco-module-loader.ts
|
|
606
|
+
var DRACO_DECODER_VERSION = "1.5.6";
|
|
607
|
+
var DRACO_ENCODER_VERSION = "1.4.1";
|
|
608
|
+
var STATIC_DECODER_URL = `https://www.gstatic.com/draco/versioned/decoders/${DRACO_DECODER_VERSION}`;
|
|
609
|
+
var DRACO_EXTERNAL_LIBRARIES = {
|
|
610
|
+
/** The primary Draco3D encoder, javascript wrapper part */
|
|
611
|
+
DECODER: "draco_wasm_wrapper.js",
|
|
612
|
+
/** The primary draco decoder, compiled web assembly part */
|
|
613
|
+
DECODER_WASM: "draco_decoder.wasm",
|
|
614
|
+
/** Fallback decoder for non-webassebly environments. Very big bundle, lower performance */
|
|
615
|
+
FALLBACK_DECODER: "draco_decoder.js",
|
|
616
|
+
/** Draco encoder */
|
|
617
|
+
ENCODER: "draco_encoder.js"
|
|
618
|
+
};
|
|
619
|
+
var DRACO_EXTERNAL_LIBRARY_URLS = {
|
|
620
|
+
[DRACO_EXTERNAL_LIBRARIES.DECODER]: `${STATIC_DECODER_URL}/${DRACO_EXTERNAL_LIBRARIES.DECODER}`,
|
|
621
|
+
[DRACO_EXTERNAL_LIBRARIES.DECODER_WASM]: `${STATIC_DECODER_URL}/${DRACO_EXTERNAL_LIBRARIES.DECODER_WASM}`,
|
|
622
|
+
[DRACO_EXTERNAL_LIBRARIES.FALLBACK_DECODER]: `${STATIC_DECODER_URL}/${DRACO_EXTERNAL_LIBRARIES.FALLBACK_DECODER}`,
|
|
623
|
+
[DRACO_EXTERNAL_LIBRARIES.ENCODER]: `https://raw.githubusercontent.com/google/draco/${DRACO_ENCODER_VERSION}/javascript/${DRACO_EXTERNAL_LIBRARIES.ENCODER}`
|
|
624
|
+
};
|
|
625
|
+
var loadEncoderPromise;
|
|
626
|
+
async function loadDracoEncoderModule(options) {
|
|
627
|
+
const modules = options.modules || {};
|
|
628
|
+
if (modules.draco3d) {
|
|
629
|
+
loadEncoderPromise = loadEncoderPromise || modules.draco3d.createEncoderModule({}).then((draco) => {
|
|
630
|
+
return { draco };
|
|
631
|
+
});
|
|
632
|
+
} else {
|
|
633
|
+
loadEncoderPromise = loadEncoderPromise || loadDracoEncoder(options);
|
|
634
|
+
}
|
|
635
|
+
return await loadEncoderPromise;
|
|
636
|
+
}
|
|
637
|
+
async function loadDracoEncoder(options) {
|
|
638
|
+
let DracoEncoderModule = await loadLibrary(
|
|
639
|
+
DRACO_EXTERNAL_LIBRARY_URLS[DRACO_EXTERNAL_LIBRARIES.ENCODER],
|
|
640
|
+
"draco",
|
|
641
|
+
options,
|
|
642
|
+
DRACO_EXTERNAL_LIBRARIES.ENCODER
|
|
643
|
+
);
|
|
644
|
+
DracoEncoderModule = DracoEncoderModule || globalThis.DracoEncoderModule;
|
|
645
|
+
return new Promise((resolve) => {
|
|
646
|
+
DracoEncoderModule({
|
|
647
|
+
onModuleLoaded: (draco) => resolve({ draco })
|
|
648
|
+
// Module is Promise-like. Wrap in object to avoid loop.
|
|
649
|
+
});
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// src/lib/utils/version.ts
|
|
654
|
+
var VERSION2 = true ? "4.0.4" : "latest";
|
|
655
|
+
|
|
656
|
+
// src/draco-writer.ts
|
|
657
|
+
var DEFAULT_DRACO_WRITER_OPTIONS = {
|
|
658
|
+
pointcloud: false,
|
|
659
|
+
// Set to true if pointcloud (mode: 0, no indices)
|
|
660
|
+
attributeNameEntry: "name"
|
|
661
|
+
// Draco Compression Parameters
|
|
662
|
+
// method: 'MESH_EDGEBREAKER_ENCODING', // Use draco defaults
|
|
663
|
+
// speed: [5, 5], // Use draco defaults
|
|
664
|
+
// quantization: { // Use draco defaults
|
|
665
|
+
// POSITION: 10
|
|
666
|
+
// }
|
|
667
|
+
};
|
|
668
|
+
var DracoWriter = {
|
|
669
|
+
name: "DRACO",
|
|
670
|
+
id: "draco",
|
|
671
|
+
module: "draco",
|
|
672
|
+
version: VERSION2,
|
|
673
|
+
extensions: ["drc"],
|
|
674
|
+
options: {
|
|
675
|
+
draco: DEFAULT_DRACO_WRITER_OPTIONS
|
|
676
|
+
},
|
|
677
|
+
encode
|
|
678
|
+
};
|
|
679
|
+
async function encode(data, options = {}) {
|
|
680
|
+
const { draco } = await loadDracoEncoderModule(options);
|
|
681
|
+
const dracoBuilder = new DracoBuilder(draco);
|
|
682
|
+
try {
|
|
683
|
+
return dracoBuilder.encodeSync(data, options.draco);
|
|
684
|
+
} finally {
|
|
685
|
+
dracoBuilder.destroy();
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// src/workers/draco-writer-worker.ts
|
|
690
|
+
(async () => {
|
|
691
|
+
if (!await WorkerBody.inWorkerThread()) {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
WorkerBody.onmessage = async (type, payload) => {
|
|
695
|
+
switch (type) {
|
|
696
|
+
case "process":
|
|
697
|
+
try {
|
|
698
|
+
const { input, options } = payload;
|
|
699
|
+
const result = await DracoWriter.encode(input, options);
|
|
700
|
+
WorkerBody.postMessage("done", { result });
|
|
701
|
+
} catch (error) {
|
|
702
|
+
const message = error instanceof Error ? error.message : "";
|
|
703
|
+
WorkerBody.postMessage("error", { error: message });
|
|
704
|
+
}
|
|
705
|
+
break;
|
|
706
|
+
default:
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
})();
|
|
710
|
+
})();
|
|
4
711
|
//# sourceMappingURL=draco-writer-worker.js.map
|