-
Notifications
You must be signed in to change notification settings - Fork 28
/
USDZLoadPlugin.ts
38 lines (33 loc) · 1.51 KB
/
USDZLoadPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {Importer} from '../../assetmanager'
import {USDZLoader} from 'three/examples/jsm/loaders/USDZLoader.js'
import {Group, Mesh} from 'three'
import {Zippable, zipSync} from 'three/examples/jsm/libs/fflate.module.js'
import {BaseImporterPlugin} from '../base/BaseImporterPlugin'
/**
* Adds support for loading `.usdz`, `model/vnd.usd+zip` and `.usda`, `model/vnd.usda` files and data uris
* @category Plugins
*/
export class USDZLoadPlugin extends BaseImporterPlugin {
public static readonly PluginType = 'USDZLoadPlugin'
protected _importer = new Importer(class extends USDZLoader {
currentUrl = ''
async loadAsync(url: string, onProgress?: (event: ProgressEvent) => void): Promise<Mesh> {
this.currentUrl = url
const res = await super.loadAsync(url, onProgress)
this.currentUrl = ''
return res
}
parse(buffer: ArrayBuffer|string): Group {
// todo make changes in three.js to allow passing unzipped buffer directly for usda
if (this.currentUrl.endsWith('.usda') && typeof buffer !== 'string') {
const filename = this.currentUrl.split('/').pop()
if (filename) {
const zip: Zippable = {}
zip[filename] = new Uint8Array(buffer)
buffer = zipSync(zip).buffer
}
}
return super.parse(buffer)
}
}, ['usdz', 'usda'], ['model/vnd.usd+zip', 'model/vnd.usdz+zip', 'model/vnd.usda'], false)
}