-
Notifications
You must be signed in to change notification settings - Fork 46
/
gltf-loader.ts
57 lines (52 loc) · 2.02 KB
/
gltf-loader.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { ILoaderResource, Loader } from "@pixi/loaders"
import { Compatibility } from "../compatibility/compatibility"
import { LoaderResourceResponseType } from "../compatibility/compatibility-version"
import { glTFAsset } from "../gltf/gltf-asset"
import { glTFResourceLoader } from "../gltf/gltf-resource-loader"
export const glTFLoader = {
use: function (resource: ILoaderResource, next: () => void) {
if (resource.extension !== "gltf") {
return next()
}
let loader = <Loader><unknown>this
glTFAsset.load(resource.data, new glTFExternalResourceLoader(loader, resource), gltf => {
Object.assign(resource, { gltf }); next()
})
},
add: function () {
Compatibility.setLoaderResourceExtensionType("bin",
LoaderResourceResponseType.buffer)
Compatibility.setLoaderResourceExtensionType("gltf",
LoaderResourceResponseType.json)
},
test(url: string): boolean {
return url.includes(".gltf") || url.includes(".glb")
},
async load(url: string): Promise<glTFAsset> {
return await glTFAsset.fromURL(url)
},
}
Compatibility.installLoaderPlugin("gltf", glTFLoader)
class glTFExternalResourceLoader implements glTFResourceLoader {
constructor(private _loader: Loader, private _resource: ILoaderResource) {
}
load(uri: string, onComplete: (resource: ILoaderResource) => void) {
const url = this._resource.url.substring(
0, this._resource.url.lastIndexOf("/") + 1) + uri
if (!this._loader.resources[url]) {
// The resource does not exists and needs to be loaded.
// @ts-ignore
this._loader.add({ parentResource: this._resource, url, onComplete })
} else if (this._loader.resources[url].data) {
// The resource already exists, just use that one.
onComplete(this._loader.resources[url])
} else {
// The resource is in queue to be loaded, wait for it.
let binding = this._loader.onProgress.add((_, resource) => {
if (resource.url === url) {
onComplete(resource); binding.detach()
}
})
}
}
}