Dev
Dev

Reputation: 501

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!

Upvotes: 5

Views: 5023

Answers (2)

Gero3
Gero3

Reputation: 2887

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.

Upvotes: 7

WestLangley
WestLangley

Reputation: 104783

How about something like this?

loader.load( "mesh1.js", function( geometry ) { createScene( geometry, 1 ) } ); 
loader.load( "mesh2.js", function( geometry ) { createScene( geometry, 2 ) } ); 

Then,

function createScene( geometry, id ) {
 ...
}

The id can be the mesh name if you prefer that.

Upvotes: 0

Related Questions