0

I am trying to load multiple .vtk files with a for loop like such:

                for (var i = 0; i < vtkFilesArray.length; i++) {
                 var loader = new THREE.VTKLoader();
                 loader.load( i+".vtk", function ( geometry ) {

                    console.log(geometry);

                    geometry.center();
                    geometry.computeVertexNormals();

                    var mesh = new THREE.Mesh( geometry, material );
                    mesh.position.set( - 0.075, 0.005, 0 );
                    mesh.scale.multiplyScalar( 0.2 );
                    scene.add( mesh );

                } );
            }

However, after I get the geometry, there is no file ID that can identify back to the mesh. Is there a way to pass along the file ID to the geometry data or attach ID to the dispatched events?

Zaba
  • 27
  • 6

1 Answers1

0

You can pass along the ID by encapsulating the variable i in its own function by using closures. More information about closures can be found in this post -> How do JavaScript closures work?

for (var i = 0; i < vtkFilesArray.length; i++) {
     (function(fileName){
        var loader = new THREE.VTKLoader();
        loader.load( fileName +".vtk", function ( geometry ) {

            console.log(geometry);

            geometry.center();
            geometry.computeVertexNormals();

            var mesh = new THREE.Mesh( geometry, material );
            mesh.position.set( - 0.075, 0.005, 0 );
            mesh.scale.multiplyScalar( 0.2 );
            mesh.fileID = fileName;
            scene.add( mesh );

        } );
    }(i));
}
Community
  • 1
  • 1
Gero3
  • 2,817
  • 1
  • 22
  • 21