加载 .obj 后如何保留 Three.js 对象的引用?
官方文档 有一个很好的示例,说明如何添加 .obj
文件到场景。
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 的第一个无名函数。但是,保留对该对象的引用的“最佳”方法是什么?
The official docs has a pretty good example of how to add a .obj
file to a scene.
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' );
}
);
However, I am not sure how I would now go about manipulating the loaded object. The URL is used to load the object and then the loaded object is passed as the argument to the on load function, i.e., the first nameless function passed to loader.load
. However, what would be the 'best' way to retain a reference to that object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这相当简单。只需在调用加载函数之前创建一个引用即可。
This is fairly simple. Just create a reference before you call the loading function.
我可以管理的一种方法看起来不太好,但是很有效。
我可以简单地将它绑定到当前窗口或文档:
但这对我来说似乎有点奇怪,因为这会将它绑定到全局空间。例如,如果我想将范围限制得更小怎么办?
The one way I can manage does not seem great, but it works.
I could simply bind it to the current window or document:
But this seems a little odd to me as this would bind it to global space. What if I wanted to keep the scope more contained, for example?