Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Conceptos básicos de Three.js – No se puede encontrar la variable – Animación de GLTFLoader

Tengo conocimientos muy básicos de JS. Siguiendo los documentos de three.js Cargando modelos 3D, he renderizado con éxito el objeto 3D y lo he centrado:

 const loader = new GLTFLoader(); loader.load( 'Duck.gltf', function ( duck ) { const model = duck.scene const box = new THREE.Box3().setFromObject( model ); const center = new THREE.Vector3(); box.getCenter( center ); model.position.sub( center ); // center the model scene.add( model ); }, undefined, function ( error ) { console.error( error ); } );

Me gustaría animarlo ahora, comenzando con una rotación simple:

 /** * Animate */ const clock = new THREE.Clock() const tick = () => { const elapsedTime = clock.getElapsedTime() // Update objects model.rotation.y = .5 * elapsedTime // Update Orbital Controls // controls.update() // Render renderer.render(scene, camera) // Call tick again on the next frame window.requestAnimationFrame(tick) } tick()

Pero el problema es que la consola devuelve:

 ReferenceError: Can't find variable: model
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ha declarado la variable del modelo dentro del alcance funcional, intente declararla fuera

 const loader = new GLTFLoader(); let model, box, center; loader.load( 'Duck.gltf', function ( duck ) { model = duck.scene box = new THREE.Box3().setFromObject( model ); center = new THREE.Vector3(); box.getCenter( center ); model.position.sub( center ); // center the model scene.add( model ); }, undefined, function ( error ) { console.error( error ); } );

¡Con suerte, esto funcionará!

editado

como sugirió @prisoner849 en los comentarios

 const clock = new THREE.Clock() const tick = () => { const elapsedTime = clock.getElapsedTime() // Update objects if (model) { model.rotation.y = .5 * elapsedTime } // Update Orbital Controls // controls.update() // Render renderer.render(scene, camera) // Call tick again on the next frame window.requestAnimationFrame(tick) } tick()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!