Using Three.js : THREE.VTKLoader() to assign an ID for an event

I have been attempting to load multiple .vtk files using a for loop in the following manner:

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, once I obtain the geometry, there is no unique file ID associated with the mesh. Is there a method to pass the file ID along with the geometry data or attach an ID to the events being dispatched?

Answer №1

If you want to pass the ID along, one approach is to use closures to encapsulate the variable i within its own function. For more details on closures, check out this informative 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));
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can the vacant space on an HTML page be determined through calculation?

Could someone please help me figure out the amount of empty space on a html page? https://i.sstatic.net/VXzbz.png Is it possible to determine the area highlighted in red using javascript? Apologies if this question seems trivial. ...

The iPhone webview keyboard causes the layout to be pushed upward and remain elevated

While editing text fields, the native keyboard pops up and stays visible in the webview. It's almost like leaving the toilet seat up! Is there a way to return to the original scroll position after the native keyboard disappears? Perhaps an event that ...

Extract data from a JSON-encoded array using JavaScript

I sent a JSON encoded array to JavaScript. Now I want to access that array to retrieve the different elements. When I print it out using console.log(), I see this array: array(1) { [16]=> array(2) { [3488]=> array(1) { [0]=> ...

Transmitting unique characters, such as a caron symbol, via xmlhttp.responseText and encoding them with json_encode

I am struggling to retrieve data from a database that contains a special character (caron) and then pass it through xmlhttp.responseText using json_encode to fill textboxes. However, the textbox linked to the data with the special character (caron) is not ...

Maximizing the efficiency of threejs through combining and selecting items

In my exploration of three.js optimization, I discovered that reducing the number of draw calls is crucial for improving performance. One way to achieve this is by consolidating geometries through the use of GeometryUtils.merge. Although this optimization ...

Iterate through each element in all arrays, but limit the loop to only iterate through the elements in the first array up to its

Within my collection of arrays, each containing elements, the structure is as follows: var result = [ // starting with the outer array [ // initial internal array {name: 'A', value: 111}, {name: 'B', value: 222} ...

JavaScript function following AJAX request

I'm attempting to invoke a JavaScript function that is returned in an Ajax call What is the proper way to run a JavaScript function received from an Ajax call? Consider this script before the Ajax call <script> function start(){ console.l ...

Executing a function without a callback within an Async library

I am facing an issue with this particular example mentioned in the Async documentation: async.map(['file1','file2','file3'], fs.stat, function(err, results) { // results is now an array of stats for each file }); In this ...

Formatting numbers in Angular 2 to include a space every three zeros in a money amount

Let's say I have the number 30000 and I want to format it as 30 000. What method should I use to achieve this? Here are more examples: 300000 -> 300 000, 3000000 -> 3 000 000. Just to clarify, this is not about using dots or commas, but rathe ...

Using React and Material UI to create a table filtering system with checkboxes

I'm currently developing a filtering feature using checkboxes for a data display in a project utilizing React and Material-UI. Is there a custom solution available within Material-UI? If not, what steps should I take to achieve this? As I am rel ...

Tips on implementing the onChange event in a custom text input for React Native applications

Recently, I started exploring React Native and attempted to create a custom text input with suggestions following a tutorial. However, I encountered an issue where I couldn't use onChange on my custom text input. I attempted to set up a state in App.j ...

What is the process for loading this image?

While browsing through this stunning website, I came across a feature that caught my eye. Can someone please explain how the designer displayed the "The magic is loading" effect: The image vanishes once the site has finished loading completely. ...

Create a path on the Google Map that follows the designated route

I am looking for a solution similar to one found here: Sample However, I have been unable to find a suitable solution anywhere. The main issue is being able to follow the route in order to draw a line between two points. Can anyone provide guidance on ho ...

Is there a way to use SCTP with Socket.io and Node.js?

I have a new project in the works, creating a web application that will utilize web sockets to provide real-time updates for users. The plan is to seamlessly transmit changes from the back-end engine. My challenge lies in Node.js not supporting SCTP sock ...

Modifying class selectors does not have any effect on newly added items

Imagine you have a ragged array structured as shown below: var myarray = [ [ ['k1', [] ], ['k2', ['l1', 'l2'] ], ['k3', [] ], ], [ ['k1', [] ], ['k2', ['l1', 'l2&ap ...

Using Regular Expressions in Express routing

Does anyone have experience serving a file with a dynamic hash in its name on a specific route? The file is named like workbox-someHash.js and the hash changes every time the app is deployed. I attempted to serve it using the following code snippets: &qu ...

How can I change a PHP for loop to Javascript?

Can the following code be converted to JavaScript? for (let lift of liftDetails) { document.write('<option>'+ lift["LiftMakes"] +'</option>'); } ...

Rearrange an irregular matrix using JavaScript

I have a matrix that can vary in width depending on the results received from the server. Here is an example: [undefined, undefined] [1, 2, 3, 4] [1, 2] [undefined, undefined, undefined] [1, 2, 3, 4, 5, 6] My goal is to transform it into this format: [u ...

Troublesome GSP: JavaScript not functioning properly in Gr

I am experiencing an issue with the JavaScript code I have. Here's my code: <script type="text/javascript"><!-- ​$(function () { $('#add').click(function() { $(this).before($('select:eq(0)').clone()); ...

Checkbox in Angular FormGroup not triggering touched state

There seems to be an issue with the Angular form when checking if the form is touched, especially in relation to a checkbox element. Despite the value of the checkbox changing on click, I am seeing !newDeviceGroup.touched = true. I'm not quite sure wh ...