Resources.ts
1.33 KB
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
/**
* 资源管理和加载
*/
import { LoadingManager, Texture, TextureLoader } from 'three'
import { loadingStart, loadingFinish, loadingError } from '@/utils'
import { resources } from './Assets'
export class Resources {
private manager!: LoadingManager
private callback: () => void
private textureLoader!: InstanceType<typeof TextureLoader>
public textures: Record<string, Texture>
constructor(callback: () => void) {
this.callback = callback // 资源加载完成的回调
this.textures = {} // 贴图对象
this.setLoadingManager()
this.loadResources()
}
/**
* 管理加载状态
*/
private setLoadingManager() {
this.manager = new LoadingManager()
// 开始加载
this.manager.onStart = () => {
loadingStart()
}
// 加载完成
this.manager.onLoad = () => {
this.callback()
}
// 正在进行中
this.manager.onProgress = url => {
loadingFinish()
}
this.manager.onError = url => {
loadingError()
window['$message'].error('数据加载失败,请刷新重试!')
}
}
/**
* 加载资源
*/
private loadResources(): void {
this.textureLoader = new TextureLoader(this.manager)
resources.textures?.forEach(item => {
this.textureLoader.load(item.url, t => {
this.textures[item.name] = t
})
})
}
}