如何在加载 .obj 后保留来自 Three.js 对象的引用?
How to keep the reference from Three.js object after loading in an .obj?
关于如何将 .obj
文件添加到场景的官方文档 has a pretty good example。
const loader = new OBJLoader();
// load a resource
loader.load(
// resource URL
'models/monster.obj',
// called when resource is loaded
function ( object ) {
scene.add( object );
},
// called when loading is in progresses
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function ( error ) {
console.log( 'An error happened' );
}
);
但是,我不确定我现在将如何处理加载的对象。 URL 用于加载对象,然后将加载的对象作为参数传递给 on load 函数,即传递给 loader.load
的第一个无名函数。但是,'best' 保留对该对象的引用的方法是什么?
我可以管理的一种方法似乎不太好,但它有效。
我可以简单地将它绑定到当前 window 或文档:
function ( object ) {
scene.add( object );
window.obj = object; // binding it to the window
}
但这对我来说似乎有点奇怪,因为这会将它绑定到全局 space。例如,如果我想让范围更小怎么办?
这很简单。在调用加载函数之前创建一个引用。
const loader = new OBJLoader();
//Add a reference before loading model
var object;
//This is the same code, just without spaces/comments
loader.load('models/monster.obj', function ( object ) {
scene.add( object );
},
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
function ( error ) {
console.log( 'An error happened' );
});
//After loading, you can now manipulate the model
object.rotation.set(x, y, z);
object.position.set(x, y, z);
关于如何将 .obj
文件添加到场景的官方文档 has a pretty good example。
const loader = new OBJLoader();
// load a resource
loader.load(
// resource URL
'models/monster.obj',
// called when resource is loaded
function ( object ) {
scene.add( object );
},
// called when loading is in progresses
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function ( error ) {
console.log( 'An error happened' );
}
);
但是,我不确定我现在将如何处理加载的对象。 URL 用于加载对象,然后将加载的对象作为参数传递给 on load 函数,即传递给 loader.load
的第一个无名函数。但是,'best' 保留对该对象的引用的方法是什么?
我可以管理的一种方法似乎不太好,但它有效。
我可以简单地将它绑定到当前 window 或文档:
function ( object ) {
scene.add( object );
window.obj = object; // binding it to the window
}
但这对我来说似乎有点奇怪,因为这会将它绑定到全局 space。例如,如果我想让范围更小怎么办?
这很简单。在调用加载函数之前创建一个引用。
const loader = new OBJLoader();
//Add a reference before loading model
var object;
//This is the same code, just without spaces/comments
loader.load('models/monster.obj', function ( object ) {
scene.add( object );
},
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
function ( error ) {
console.log( 'An error happened' );
});
//After loading, you can now manipulate the model
object.rotation.set(x, y, z);
object.position.set(x, y, z);