首页 > 解决方案 > 在 Three.js 中使用 FBXLoader 将 BufferGeometry 转换为 Geometry

问题描述

这是我加载 .fbx 对象的代码,它默认加载一个对象BufferGeometry

var loader = new THREE.FBXLoader();

async function loadFiles(scene,props) {

  const {files, path, childName, fn} = props;

  if (index > files.length - 1) return;
  loader.load(path+files[index], object => {
    // apply functions / transformations to obj
    let sceneObj = fn.call(null,object,props);
    if (sceneObj) { scene.add(sceneObj) }

    // add obj to scene and push to array
    scene.add(object);
    objects.push(object);

    // if there is another object to load, load it
    index++;
    loadFiles(scene,props);
  });
}

我想用它var geometry = new THREE.Geometry().fromBufferGeometry( bufferGeometry );来解决这个问题,但我似乎没有mesh在我的加载器函数中构建一个,所以我看不到如何实现这段代码。

我想以一种可读的方式访问对象的顶点,这就是为什么我不希望它加载为BufferGeometry.

标签: javascriptthree.jsgeometrybuffer-geometry

解决方案


加载器返回一个对象,该对象将包含具有几何图形的网格。您需要遍历对象及其子对象并在遇到它时转换 BufferGeometry。以下是如何实现这一目标的想法:

loader.load(path+files[index], object => {

    // iterate over all objects and children
    object.traverse(child => {

        // convert the buffer geometry
        if (child.isMesh && child.geometry.isBufferGeometry) {

            const bg = child.geometry;
            child.geometry = (new THREE.Geometry()).fromBufferGeometry(bg);

        }

    });

    // ... do something with the loaded model...

});

希望有帮助!


推荐阅读