deep-engine-demo/packages/docs/api/manager/ResourceManager.md
2026-04-19 18:46:28 +08:00

207 lines
5.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
outline: [2, 3]
---
# ResourceManager
- 源文件:`packages/sdk/src/manager/ResourceManager.ts`
- 文档位置:`packages/docs/api/manager/ResourceManager.md`
## 枚举:`ResourceType`
- 作用:资源类型
| 枚举项 | 值 | 说明 |
| --- | --- | --- |
| `TEXTURE` | `"texture"` | 纹理 |
| `MATERIAL` | `"material"` | 材质 |
| `GEOMETRY` | `"geometry"` | 几何体 |
| `MODEL` | `"model"` | 模型 |
| `RAW_DATA` | `"raw_data"` | 原始数据 |
## 接口:`ILoadProgress`
- 作用:资源加载进度信息
### 字段
| 字段名 | 类型 | 必填 | 作用 |
| --- | --- | --- | --- |
| `loaded` | `number` | 是 | 已加载的字节数 |
| `total` | `number` | 是 | 总字节数 |
| `progress` | `number` | 是 | 加载进度百分比 (0-100) |
## 类:`ResourceManager`
- 作用:资源管理器 负责加载、缓存和管理三维资源
### 构造函数
#### `new ResourceManager()`
- 作用:创建 ResourceManager 实例。
- 入参:无
- 出参:`ResourceManager`
- 返回说明:返回当前类实例。
### 属性
| 属性名 | 可见性 | 类型 | 作用 |
| --- | --- | --- | --- |
| `cache` | `public` | `Map<string, unknown>` | 用于保存 `cache` 的映射关系。 |
| `textureLoader` | `public` | `THREE.TextureLoader` | 用于保存 `textureLoader` 对应的数据。 |
| `fbxLoader` | `public` | `FBXLoader` | 用于保存 `fbxLoader` 对应的数据。 |
| `gltfLoader` | `public` | `GLTFLoader` | 用于保存 `gltfLoader` 对应的数据。 |
| `fileLoader` | `public` | `THREE.FileLoader` | 用于保存 `fileLoader` 对应的数据。 |
| `loadingManager` | `public` | `THREE.LoadingManager` | 用于统一管理 `loadingManager` 相关能力。 |
### 方法
#### `createGLTFLoader(manager?: THREE.LoadingManager): GLTFLoader`
- 作用:创建 GLTF 加载器
- 入参:
| 参数名 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `manager` | `THREE.LoadingManager` | 否 | - 加载管理器 |
- 出参:`GLTFLoader`
- 返回说明GLTF 加载器实例
#### `loadTexture(url: string, onProgress?: (progress: ILoadProgress) => void): Promise<THREE.Texture<unknown, THREE.TextureEventMap>>`
- 作用:加载纹理
- 入参:
| 参数名 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `url` | `string` | 是 | - 纹理文件路径 |
| `onProgress` | `(progress: ILoadProgress) => void` | 否 | - 加载进度回调 |
- 出参:`Promise<THREE.Texture<unknown, THREE.TextureEventMap>>`
- 返回说明:纹理对象
#### `loadFBX(url: string, onProgress?: (progress: ILoadProgress) => void): Promise<THREE.Group<THREE.Object3DEventMap>>`
- 作用:加载 FBX 模型
- 入参:
| 参数名 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `url` | `string` | 是 | - FBX 文件路径 |
| `onProgress` | `(progress: ILoadProgress) => void` | 否 | - 加载进度回调 |
- 出参:`Promise<THREE.Group<THREE.Object3DEventMap>>`
- 返回说明FBX 模型对象 (Group)
#### `loadGLTF(url: string, onProgress?: (progress: ILoadProgress) => void): Promise<THREE.Object3D<THREE.Object3DEventMap>>`
- 作用:加载 GLTF 模型
- 入参:
| 参数名 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `url` | `string` | 是 | - GLTF 文件路径 |
| `onProgress` | `(progress: ILoadProgress) => void` | 否 | - 加载进度回调 |
- 出参:`Promise<THREE.Object3D<THREE.Object3DEventMap>>`
- 返回说明GLTF 模型对象
#### `loadRawData(url: string, onProgress?: (progress: ILoadProgress) => void): Promise<ArrayBuffer>`
- 作用:加载原始二进制数据
- 入参:
| 参数名 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `url` | `string` | 是 | - 原始数据文件路径 |
| `onProgress` | `(progress: ILoadProgress) => void` | 否 | - 加载进度回调 |
- 出参:`Promise<ArrayBuffer>`
- 返回说明ArrayBuffer 数据
#### `get<T = unknown>(type: ResourceType, key: string): T | undefined`
- 作用:从缓存中获取资源
- 入参:
| 参数名 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `type` | `ResourceType` | 是 | - 资源类型 |
| `key` | `string` | 是 | - 资源键名 |
- 出参:`T \| undefined`
- 返回说明:资源对象,如果不存在则返回 undefined
#### `has(type: ResourceType, key: string): boolean`
- 作用:检查资源是否存在于缓存中
- 入参:
| 参数名 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `type` | `ResourceType` | 是 | - 资源类型 |
| `key` | `string` | 是 | - 资源键名 |
- 出参:`boolean`
- 返回说明:是否存在
#### `remove(type: ResourceType, key: string): boolean`
- 作用:从缓存中移除资源
- 入参:
| 参数名 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `type` | `ResourceType` | 是 | - 资源类型 |
| `key` | `string` | 是 | - 资源键名 |
- 出参:`boolean`
- 返回说明:是否成功移除
#### `clear(): void`
- 作用:清空所有缓存
- 入参:无
- 出参:`void`
- 返回说明:无返回值。
#### `getCacheSize(): number`
- 作用:获取缓存大小
- 入参:无
- 出参:`number`
- 返回说明:缓存中的资源数量
#### `dispose(): void`
- 作用:销毁资源管理器
- 入参:无
- 出参:`void`
- 返回说明:无返回值。
## 构造示例
- 当前 Demo 中没有直接展示 `ResourceManager` 的构造调用。
## 函数示例
- 当前 Demo 中没有直接展示 `ResourceManager` 的公开方法调用。