HarmonyOS鸿蒙Next中ArkGraphics3D如何从沙盒目录读取.glb或者.gltf模型

HarmonyOS鸿蒙Next中ArkGraphics3D如何从沙盒目录读取.glb或者.gltf模型

import { Scene, Camera, Node, Container, SceneResourceFactory, EnvironmentBackgroundType } from '@kit.ArkGraphics3D';
const gltfUri = fileuri.getUriFromPath('/data/storage/el2/base/haps/entry/files/aaa.glb');
// console.info(this.TAG, gltfUri);
// Scene.load($rawfile('gltf/DamagedHelmet/glTF/DamagedHelmet.gltf'))
// Scene.load('file:///data/storage/el2/base/haps/entry/files/aaa.gltf')
Scene.load(gltfUri) // 试过很多办法,都读取失败

源代码是这个 https://gitee.com/harmonyos_samples/Graphics3D

ArkGraphics3D如何从沙盒目录读取.glb或者.gltf模型,源代码只说明$rawfile的情况,并没有举例从本地目录读取


更多关于HarmonyOS鸿蒙Next中ArkGraphics3D如何从沙盒目录读取.glb或者.gltf模型的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS鸿蒙Next中,使用ArkGraphics3D从沙盒目录读取.glb或.gltf模型的步骤如下:

  1. 获取沙盒目录路径:
let context = getContext(this) as common.UIAbilityContext;
let path = context.filesDir;
  1. 使用ArkGraphics3D加载模型:
import { GLTFModel } from '@ohos/arkgraphics3d';

let model = new GLTFModel();
model.loadFromFile(path + '/model.glb'); // 或.gltf文件
  1. 确保文件已正确放置在沙盒目录下,可通过文件管理API实现。

更多关于HarmonyOS鸿蒙Next中ArkGraphics3D如何从沙盒目录读取.glb或者.gltf模型的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,从沙盒目录读取.glb或.gltf模型需要注意以下几点:

  1. 确保文件权限正确,使用正确的沙盒路径格式。沙盒路径应该是类似internal://app/开头的URI。

  2. 推荐使用以下方式加载沙盒中的模型文件:

import { Scene } from '@kit.ArkGraphics3D';
import fileIO from '@ohos.fileio';

// 获取沙盒路径
const sandboxPath = 'internal://app/aaa.glb'; 

// 转换为可用的URI
const gltfUri = fileIO.getUriFromPath(sandboxPath);

// 加载场景
Scene.load(gltfUri).then(scene => {
  // 加载成功处理
}).catch(err => {
  console.error('加载失败:', err);
});
  1. 常见问题排查:
  • 确认文件确实存在于指定路径
  • 检查文件权限是否正确
  • 确保文件格式是有效的.glb或.gltf格式
  • 可以使用fileIO API先验证文件是否存在
  1. 与$rawfile不同,沙盒文件需要确保应用有正确的文件访问权限。

如果仍然失败,建议检查控制台输出的具体错误信息,这通常会指明问题所在。

回到顶部