-
Notifications
You must be signed in to change notification settings - Fork 46
/
cubemap-loader.ts
56 lines (50 loc) · 1.89 KB
/
cubemap-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
import { Loader, LoaderResource } from "@pixi/loaders"
import { Texture } from "@pixi/core"
import { Cubemap } from "../cubemap/cubemap"
import { CubemapFaces } from "../cubemap/cubemap-faces"
import { Compatibility } from "../compatibility/compatibility"
export const CubemapLoader = {
use: function (resource: any, next: () => void) {
if (resource.extension !== "cubemap") {
return next()
}
let loader = <Loader><unknown>this
const mipmaps = (<string[]>resource.data).map(mipmap => {
return Cubemap.faces.map(face => {
return resource.url.substring(0, resource.url.lastIndexOf("/") + 1) + mipmap.replace("{{face}}", face)
})
})
// The list of urls (faces and mipmaps) which needs to be loaded before the
// cubemap should be created.
let urls = mipmaps.reduce((acc, val) => acc.concat(val), [])
loader.add(urls.filter(url => !loader.resources[url]).map((url) => {
return { parentResource: resource, url: url }
}))
let completed = 0
// Listen for resources being loaded.
let binding = loader.onLoad.add((loader: any, res: any) => {
if (urls.includes(res.url)) {
if (++completed === urls.length) {
// All resources used by cubemap has been loaded.
const textures = mipmaps.map(face => {
return <CubemapFaces>{
posx: Texture.from(face[0]),
negx: Texture.from(face[1]),
posy: Texture.from(face[2]),
negy: Texture.from(face[3]),
posz: Texture.from(face[4]),
negz: Texture.from(face[5]),
}
})
resource.cubemap = Cubemap.fromFaces(textures)
binding.detach(); next()
}
}
})
},
add: function () {
LoaderResource.setExtensionXhrType(
"cubemap", LoaderResource.XHR_RESPONSE_TYPE.JSON)
}
}
Compatibility.installLoaderPlugin("cubemap", CubemapLoader)