2

Here is my code to load a .fbx object, which loads an object as BufferGeometry by default:

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);
  });
}

I wanted to use var geometry = new THREE.Geometry().fromBufferGeometry( bufferGeometry ); to fix this, but I don't seem to build a mesh in my loader function so I don't see how I could implement this code.

I want to access the vertices of the object in a readable way, which is why I don't want it to load as BufferGeometry.

qbuffer
  • 383
  • 4
  • 14

1 Answers1

3

The loader returns an object that will contain meshes that have geometry on them. You'll want to traverse the object and its children and convert the BufferGeometry as you come across it. Here's an idea of how to achieve that:

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...

});

Hope that helps!

Garrett Johnson
  • 2,413
  • 9
  • 17
  • This seems to work, however after looking at the updated vertices it still seems to display that my object has 1848 vertices, when it should have only 310. Does this method also update the vertices array? Thanks. – qbuffer Jan 07 '19 at 02:04
  • 1
    It's hard to say without seeing the geometry you're working with. How are you counting the vertices? The docs on `fromBufferGeometry` mention that you may wind up with duplicate vertices and can call `mergeVertices` to remove them: https://threejs.org/docs/#api/en/core/Geometry.fromBufferGeometry – Garrett Johnson Jan 07 '19 at 07:25