@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/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.2",
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
  },
@@ -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
- if (!uncompressed) throw new Error('GLTFDracoExporter: gltf is null')
89
+ try {
90
+ if (!uncompressed) throw new Error('GLTFDracoExporter: gltf is null')
90
91
 
91
- let gltf = uncompressed
92
+ let gltf = uncompressed
92
93
 
93
- const bytes = (gltf as ArrayBuffer).byteLength || Infinity
94
+ const bytes = (gltf as ArrayBuffer).byteLength || Infinity
94
95
 
95
- const iDocument = await (typeof gltf === 'object' && !(gltf as any).byteLength ? this._io.readJSON({
96
- json: gltf as GLTF.IGLTF,
97
- resources: {},
98
- }) : this._io.readBinary(new Uint8Array(gltf as ArrayBuffer)))
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
- // iDocument.createExtension(GLTFViewerConfigExtensionGP)
101
- iDocument.createExtension(KHRDracoMeshCompression)
102
- .setRequired(true)
103
- .setEncoderOptions({...this._encoderOptions, ...dracoOptions ?? {}})
101
+ // iDocument.createExtension(GLTFViewerConfigExtensionGP)
102
+ iDocument.createExtension(KHRDracoMeshCompression)
103
+ .setRequired(true)
104
+ .setEncoderOptions({...this._encoderOptions, ...dracoOptions ?? {}})
104
105
 
105
- if (ops.exportExt === 'glb') {
106
- gltf = await this._io.writeBinary(iDocument)
107
- if (isFinite(bytes)) {
108
- console.log('DRACO Compression ratio: ' + ((gltf as ArrayBuffer).byteLength / bytes).toFixed(5))
109
- }
110
- } else {
111
- const jDoc = await this._io.writeJSON(iDocument)
112
- gltf = jDoc.json
113
- if (Object.values(jDoc.resources).filter(v => v).length > 0) {
114
- console.warn('DRACOExporter: extra resources in resources not supported properly')
115
- ;(gltf as any).resources = jDoc.resources
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
- gltf.__isGLTFOutput = true
120
- const blob = await super.parseAsync(gltf, ops) as any // this will just convert it to blob because __isGLTFOutput is set (checked in GLTFExporter2)
121
- if (!blob) throw new Error('GLTFDracoExporter: blob is null')
122
- blob.ext = 'glb'
123
- ;(blob as any).__uncompressed = uncompressedBlob
124
- return blob
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 {