Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

THREE.js JSONLoader callback

In THREE.js, if I have multiple calls to the JSONLoader to load multiple objects like this (simplified example):

function init() {    
  var loader = new THREE.JSONLoader();    
  loader.load("mesh1.js", createScene);    
  loader.load("mesh2.js", createScene);
}    

function createScene( geometry ) {    
  if (geometry.filename == "mesh1.js") {    
    mesh1 = new THREE.Mesh( geometry, material );
    scene.add( mesh1 );    
  } else if (geometry.filename == "mesh2.js") {    
    mesh2 = new THREE.Mesh( geometry, material );
    scene.add( mesh2 );
  }
}

How can I determine which mesh has been returned on callback, especially when they frequently arrive out of order?

I'm trying to handle multiple returned meshes with a single generic callback function. Is there some property in the returned geometry that indicates the original filename that I can test against?

Or perhaps there's a more elegant way? Perhaps creating a new THREE.JSONLoader object for each call would help the callback function determine which mesh has arrived?

I appreciate any help/ideas! Thanks!

like image 611
Dev Avatar asked Aug 17 '12 00:08

Dev


1 Answers

well there is a more generic way then what WestLangley proposes.

loader.load( "mesh1.js", meshloader("mesh1.js")); 
loader.load( "mesh2.js", meshloader("mesh2.js"));

then

function meshloader(fileName){
    return function(geometry){
        ...
    }
}

This way, you can add a identifier on each loaded file.

like image 142
Gero3 Avatar answered Nov 09 '22 16:11

Gero3