加载 .obj 后如何保留 Three.js 对象的引用?

发布于 2025-01-09 14:04:46 字数 764 浏览 0 评论 0原文

官方文档 有一个很好的示例,说明如何添加 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不甘平庸 2025-01-16 14:04:46

这相当简单。只需在调用加载函数之前创建一个引用即可。

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);

This is fairly simple. Just create a reference before you call the loading function.

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);
难以启齿的温柔 2025-01-16 14:04:46

我可以管理的一种方法看起来不太好,但是很有效。

我可以简单地将它绑定到当前窗口或文档:

function ( object ) {
    scene.add( object ); 
    window.obj = object; // binding it to the window
}

但这对我来说似乎有点奇怪,因为这会将它绑定到全局空间。例如,如果我想将范围限制得更小怎么办?

The one way I can manage does not seem great, but it works.

I could simply bind it to the current window or document:

function ( object ) {
    scene.add( object ); 
    window.obj = object; // binding it to the window
}

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文