# 资源

# 资源路径

通过AssetsPath的静态属性设置或获取资源路径。

🌏 rootPath 默认资源根路径

import { AssetsPath } from '@tx3d/core';

// 获取默认资源根路径
const rootPath = AssetsPath.rootPath;

// 设置默认资源根路径
AssetsPath.rootPath = '../assets/';
1
2
3
4
5
6
7

🌏 audiosPath 默认音频资源根路径(只读)

import { AssetsPath } from '@tx3d/core';

// 获取告警音频资源路径
const path = `${AssetsPath.audiosPath}alarm.wav`;

1
2
3
4
5

🌏 fontPaths 默认字体资源根路径(只读)

import { AssetsPath } from '@tx3d/core';

// 获取"DS-DIGIB"字体资源路径
const path = `${AssetsPath.fontPaths}DS-DIGIB.TTF`;
1
2
3
4

🌏 modelsPath 默认模型资源根路径(只读)

import { AssetsPath } from '@tx3d/core';

// 获取液压支架模型资源路径
const path = `${AssetsPath.modelsPath}液压支架/液压支架.gltf`;
1
2
3
4

🌏 texturesPath 默认纹理资源根路径(只读)

import { AssetsPath } from '@tx3d/core';

// 获取“Grid”纹理资源路径
const path = `${AssetsPath.texturesPath}Grid.png`;
1
2
3
4

提示

内置资源默认目录结构如下图所示:
avatar

# 模型资源

模型资源(ModelAsset)用于记录加载完成的模型数据。

# 模型资源属性

🌏 url 模型资源路径,只读。

🌏 type 模型资源类型(OBJ、FBX、GLTF),只读。

🌏 data 模型数据,只读。

🌏 refCount 模型资源引用计数,只读。

# 模型资源接口

🌏 reference 引用模型资源

// 引用模型资源
const asset = modelAsset.reference();
1
2

注意

调用“reference”接口会递增引用计数并复用当前模型资源。

🌏 dispose 释放模型资源

// 释放模型资源
modelAsset.dispose();
1
2

注意

调用“dispose”接口会递减引用计数,只有当引用计数等于0时才会释放模型数据。

# 纹理资源

纹理资源(TextureAsset)用于记录加载完成的纹理数据。

# 纹理资源属性

🌏 url 纹理资源路径,只读。

🌏 texture 纹理数据,只读。

🌏 refCount 纹理资源引用计数,只读。

# 纹理资源接口

🌏 reference 引用纹理资源

// 引用纹理资源
const asset = textureAsset.reference();
1
2

注意

调用“reference”接口会递增引用计数并复用当前纹理资源。

🌏 dispose 释放纹理资源

// 释放纹理资源
textureAsset.dispose();
1
2

注意

调用“dispose”接口会递减引用计数,只有当引用计数等于0时才会释放纹理数据。

# 加载资源

使用AssetsManager的静态接口加载文件、模型、纹理等资源。

# 加载文件资源

使用AssetsManager的loadFile接口加载文件资源。

import { AssetsPath, AssetsManager } from '@tx3d/core';

// 加载laneway.json资源
AssetsManager.loadFile( `${AssetsPath.rootPath}jsons/laneway.json` ).then( ( data ) => {

    // TODO:加载成功回调

}, ( error ) => {

    // TODO:加载失败回调

} );
1
2
3
4
5
6
7
8
9
10
11
12

# 加载模型资源

1️⃣ 使用AssetsManger的loadOBJModelAsset加载OBJ模型资源。

import { AssetsManager } from '@tx3d/core';

// 加载OBJ模型资源
AssetsManager.loadOBJModelAsset(

    'assets/models/LiCun/LiCun.obj',    // obj数据
    'assets/models/LiCun/LiCun.mtl',    // mtl数据

).then( ( modelAsset ) => {

    // TODO:加载成功回调

}, ( error ) => {

    // TODO:加载失败回调

} )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

2️⃣ 使用AssetsManger的loadGLTFModelAsset接口加载GLTF模型资源。

import { AssetsManager } from '@tx3d/core';

// 加载GLTF模型资源
AssetsManager.loadGLTFModelAsset( 'assets/models/工业广场/工业广场.gltf' ).then( ( modelAsset ) => {

    // TODO:加载成功回调

}, ( error ) => {

    // TODO:加载失败回调

} );
1
2
3
4
5
6
7
8
9
10
11
12

3️⃣ 使用AssetsManager的loadFBXModelAsset接口加载FBX模型资源。

import { AssetsManager } from '@tx3d/core';

// 加载FBX模型资源
AssetsManager.loadFBXModelAsset( 'assets/models/Samba Dancing/Samba Dancing.fbx' ).then( ( modelAsset ) => {

    // TODO:加载成功回调

}, ( error ) => {

    // TODO:加载失败回调

} );
1
2
3
4
5
6
7
8
9
10
11
12

注意

模型资源加载时不会自动递增资源引用计数,需要手动引用递增引用计数!!!

# 加载纹理资源

使用AssetsManager的loadTextureAsset接口加载纹理资源。

// 加载“floor.jpg”纹理资源
AssetsManager.loadTextureAsset( 'assets/textures/floor.jpg' ).then( ( textureAsset ) => {

    // TODO:加载成功回调

}, ( error ) => {

    // TODO:加载失败回调

} );
1
2
3
4
5
6
7
8
9
10

注意

纹理资源加载成功后会自动递增引用计数!!!

Last Updated: 10/17/2023, 10:19:05 AM