最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Three.js Accessing an object after it was loaded with GLTF Loader - Stack Overflow

programmeradmin0浏览0评论

In three.js with GLTF loader, is there a way to access an object to perform transformations after it was loaded

Doing this doesn't seem to work

gltf.scene.position.set(10,10,10)

Code:

function loadObject(){
    var loader = new THREE.GLTFLoader();

    loader.load('test.gltf',
        function ( gltf ) {
            scene.add( gltf.scene );

        },
        function ( xhr ) {
            //console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
        },
        function ( error ) {
            //console.log( 'An error happened' );
        }
    );
}

loadObject()

// Translate
gltf.scene.position.set(10,10,10)

In three.js with GLTF loader, is there a way to access an object to perform transformations after it was loaded

Doing this doesn't seem to work

gltf.scene.position.set(10,10,10)

Code:

function loadObject(){
    var loader = new THREE.GLTFLoader();

    loader.load('test.gltf',
        function ( gltf ) {
            scene.add( gltf.scene );

        },
        function ( xhr ) {
            //console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
        },
        function ( error ) {
            //console.log( 'An error happened' );
        }
    );
}

loadObject()

// Translate
gltf.scene.position.set(10,10,10)
Share Improve this question asked Oct 24, 2018 at 14:17 MikeMike 251 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Yes, there is. It's all about scoping and having the variables available to you throughout your app.

Check the source of this example - https://threejs/examples/webgl_loader_gltf.html

See how the variables are declared and then used throughout the code (line 54,55).

var container, stats, controls;
var camera, scene, renderer, light;

You also need to remember that the gltf model data won't be available until it's loaded, so you'll need to integrate a way to handle that too. I expect that the gltf model hasn't loaded by the time you are trying to set the position of it.

The LoadingManager is a good way to manage this - https://threejs/docs/#api/en/loaders/managers/LoadingManager

You could execute an init() method once all of your assets have loaded, for example.

Example for your scenario:

var model;

function loadObject(){
    var loader = new THREE.GLTFLoader();

    loader.load('test.gltf',
        function ( gltf ) {

            model = gltf;
            scene.add( model );

            init();

        },
        function ( xhr ) {
            //console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
        },
        function ( error ) {
            //console.log( 'An error happened' );
        }
    );
}

loadObject()

function init() {
    // Translate
    model.scene.position.set(10,10,10);
}
发布评论

评论列表(0)

  1. 暂无评论