THREE.js JSONLoader callback

时光总嘲笑我的痴心妄想 提交于 2019-12-07 05:19:09

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/11997234/three-js-jsonloader-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!