Search code examples
javascriptarraysjsonthree.jsraytracing

How can I load an array of json objects in three.js?


Ok, so the asynchronous behavior of the loader is what is throwing me off. Here's what I have:

for( i = 0; i < 37; i++ ){
    partLoader.load( "parts/" + filename[i], function ( geometry ) {
        var partMesh = new THREE.Mesh( geometry, basicMat );
        partMesh.position.y = yPos;
        partMesh.position.x = xPos;
        bodyPart.push(partMesh);
        rayCastScene.add( partMesh );
    });
}

Originally I was setting bodyPart[i] = partMesh but by the time the callback ran i = 36 and all of them just overwrote each other in the last element. Pushing them at least gets them all in the array, but I have no idea which index goes with which element anymore.

I'm using this with raytracing to identify a particular object.if(SELECTED == bodyPart[i]) ...

Similar problem here, but no solution: THREE.JS loading an array of STL Meshes


Solution

  • Actualy this is not about Three.js, problem in logic, simple example:

        (function request(){
    
            $.ajax({
                url: "your path",
                data: "your data",
                timeout: "max respones time from the server in milliseconds"
            })
            .done(function(response){
    
                  //do something with response
                  request(); //call request function again
             });
    
        })();
    

    you can create simple counter to control how much times this function will runs