Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
How to keep the reference from Three.js object after loading in an .obj?

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?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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);
about 4 years ago · Juan Pablo Isaza Relatório

0

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?

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda