@threepipe/plugin-gltf-transform 0.1.2 → 0.1.3
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/GLTFDracoExporter.d.ts +1 -1
- package/dist/GLTFDracoExporter.d.ts.map +1 -1
- package/dist/index.js +34 -27
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +34 -27
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
- package/src/GLTFDracoExporter.ts +37 -30
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@threepipe/plugin-gltf-transform",
|
|
3
3
|
"description": "Utility plugins for threepipe using gltf-transform to optimize/compress glTF files.",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.3",
|
|
5
5
|
"dependencies": {},
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
8
9
|
"import": "./dist/index.mjs",
|
|
9
10
|
"require": "./dist/index.js"
|
|
10
11
|
},
|
package/src/GLTFDracoExporter.ts
CHANGED
|
@@ -67,7 +67,7 @@ export class GLTFDracoExporter extends GLTFExporter2 implements IExportParser {
|
|
|
67
67
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
async parseAsync(obj: any, {compress = false, dracoOptions, ...options}: {compress: boolean, dracoOptions?: EncoderOptions} & GLTFExporter2Options): Promise<Blob> {
|
|
70
|
+
async parseAsync(obj: any, {compress = false, dracoOptions, ...options}: {compress: boolean, dracoOptions?: EncoderOptions} & GLTFExporter2Options, throwOnError = false): Promise<Blob> {
|
|
71
71
|
if (!this.loader) {
|
|
72
72
|
console.error('GLTFDracoExporter: No DRACOLoader2 instance provided')
|
|
73
73
|
return super.parseAsync(obj, options)
|
|
@@ -86,42 +86,49 @@ export class GLTFDracoExporter extends GLTFExporter2 implements IExportParser {
|
|
|
86
86
|
const uncompressedBlob = await super.parseAsync(uncompressed, ops)
|
|
87
87
|
if (!compress) return uncompressedBlob
|
|
88
88
|
|
|
89
|
-
|
|
89
|
+
try {
|
|
90
|
+
if (!uncompressed) throw new Error('GLTFDracoExporter: gltf is null')
|
|
90
91
|
|
|
91
|
-
|
|
92
|
+
let gltf = uncompressed
|
|
92
93
|
|
|
93
|
-
|
|
94
|
+
const bytes = (gltf as ArrayBuffer).byteLength || Infinity
|
|
94
95
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
96
|
+
const iDocument = await (typeof gltf === 'object' && !(gltf as any).byteLength ? this._io.readJSON({
|
|
97
|
+
json: gltf as GLTF.IGLTF,
|
|
98
|
+
resources: {},
|
|
99
|
+
}) : this._io.readBinary(new Uint8Array(gltf as ArrayBuffer)))
|
|
99
100
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
// iDocument.createExtension(GLTFViewerConfigExtensionGP)
|
|
102
|
+
iDocument.createExtension(KHRDracoMeshCompression)
|
|
103
|
+
.setRequired(true)
|
|
104
|
+
.setEncoderOptions({...this._encoderOptions, ...dracoOptions ?? {}})
|
|
104
105
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
106
|
+
if (ops.exportExt === 'glb') {
|
|
107
|
+
gltf = await this._io.writeBinary(iDocument)
|
|
108
|
+
if (isFinite(bytes)) {
|
|
109
|
+
console.log('DRACO Compression ratio: ' + ((gltf as ArrayBuffer).byteLength / bytes).toFixed(5))
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
const jDoc = await this._io.writeJSON(iDocument)
|
|
113
|
+
gltf = jDoc.json
|
|
114
|
+
if (Object.values(jDoc.resources).filter(v => v).length > 0) {
|
|
115
|
+
console.warn('DRACOExporter: extra resources in resources not supported properly')
|
|
116
|
+
;(gltf as any).resources = jDoc.resources
|
|
117
|
+
}
|
|
116
118
|
}
|
|
117
|
-
}
|
|
118
119
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
120
|
+
gltf.__isGLTFOutput = true
|
|
121
|
+
const blob = await super.parseAsync(gltf, ops) as any // this will just convert it to blob because __isGLTFOutput is set (checked in GLTFExporter2)
|
|
122
|
+
if (!blob) throw new Error('GLTFDracoExporter: blob is null')
|
|
123
|
+
blob.ext = 'glb'
|
|
124
|
+
;(blob as any).__uncompressed = uncompressedBlob
|
|
125
|
+
return blob
|
|
126
|
+
} catch (e) {
|
|
127
|
+
if (throwOnError) throw e
|
|
128
|
+
console.error('Unable to compress glb with DRACO extension, fallback to uncompressed')
|
|
129
|
+
console.error(e)
|
|
130
|
+
return uncompressedBlob
|
|
131
|
+
}
|
|
125
132
|
}
|
|
126
133
|
|
|
127
134
|
addExtension(extension: typeof Extension): this {
|