首页 > 解决方案 > 巴比伦 JS 3D 文件加载不起作用

问题描述

无论我尝试导入什么类型的文件,我都不会让它出现在我的画布中。

我试过的:

这是我的代码:

import { SceneLoader, Scene } from "babylonjs";
import "babylonjs-loaders";

const importFile = (
  path: string,
  fileName: string,
  ext: string,
  scene: Scene
) => {
  console.log(SceneLoader.IsPluginForExtensionAvailable(ext));
  SceneLoader.ImportMeshAsync("", path, fileName + ext, scene).then((meshes) =>

   // this actually does log some object with a bunch of fields, which dont seem to help me
 identify the problem. Maybe someone knows what kind of info that could give me.
    console.log(meshes)
 );
};


//specific Model being loaded
export const createChessBoard = (scene: Scene) => {
  importFile("/home/tom/Models/", "ChessBoard", ".obj", scene);
};

在这里它被称为

// the rest of the scene 
const scene = new Scene(engine);

  const camera = new ArcRotateCamera(
    "Camera",
    Math.PI / 2,
    Math.PI / 2,
    2,
    Vector3.Zero(),
    scene
  );
  if (canvas) {
    camera.attachControl(canvas, true);
  }

  const light1 = new HemisphericLight(
    "ambientlight",
    new Vector3(1, 1, 0),
    scene
  );
  const light2 = new PointLight("spotlight", new Vector3(0, 1, -1), scene);

  createChessBoard(scene);

使用 MeshBuilder 创建相机、场景和所有这些或形状可以完美地工作。我在任何地方都找不到任何人在使用加载器时遇到问题......通常他们被高度赞扬为如此轻松地工作,我希望有人可以帮助我解决这个问题,因为巴比伦看起来像一个很酷的图书馆

标签: babylonjs

解决方案


https://doc.babylonjs.com/api/classes/babylon.sceneloader#load

ImportMesh 方法(而不是 ImportMeshAsync)允许可选的回调,如 onError、onComplete 等。

这些将在出现问题或成功等时触发,以便您更好地调试问题

function onError(e){ console.log(e) // 希望 e 中有一些有用的信息(可能是 e.message 或 e.error)

}

SceneLoader.ImportMesh("mesh1","./yourMeshFolder", onError);


推荐阅读