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

211
Visualizações
Unable to render GLTF object with three.js

I am trying to render a GLTF object provided by the three.js guide (https://github.com/mrdoob/three.js/blob/master/examples/models/gltf/DamagedHelmet)

To do so, I tried with the following loader provided in their guide (https://threejs.org/docs/#examples/en/loaders/GLTFLoader)

Following the threejs guide, I wrote this code that should load and render a GLTF file:

"use strict";
const loader = new THREE.GLTFLoader();
const scene = new THREE.Scene();
const ratio = window.innerWidth / window.innerHeight
const camera = new THREE.PerspectiveCamera( 75, ratio, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();


const path = 'https://raw.githubusercontent.com/mrdoob/three.js/master/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf'

camera.position.set(0,0,0)
camera.lookAt(10,0,0)
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

renderer.render( scene, camera );


loader.load(path, function ( gltf ) {
  gltf.scene.position.x=10
  scene.add( gltf.scene );
}, function ( xhr ) {
  console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
 
}, function ( error ) {
  console.log( 'An error happened' , path);
});

Unfortunatley the scene stays black even if the browser correctly loads the resource files from the internet and there are no errors in the console.

So, my question is: how can fix my codepen so it shows a GLTF object in a browser using three.js?

EDIT: thanks to the best answer, here a codepen that works right: https://codepen.io/spocchio/pen/vYJpaLg

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

0

I noticed 2 issues:

  1. Your scene needs lights. When meshes get added to the scene they're not illuminated so they stay black.
  2. You're only calling renderer.render() once, and that happens before you load your GLTF. So when the helmet is finally loaded no rendering takes place.

I made these 2 updates in your Codepen, and the helmet showed up.

renderer.render( scene, camera );

// Add ambient light
scene.add(new THREE.AmbientLight());

loader.load(path, function ( gltf ) {
  gltf.scene.position.x=10
  scene.add( gltf.scene );

  // Render scene after assets have been added
  renderer.render( scene, camera );
}, function ( xhr ) {
  console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
 
}, function ( error ) {
  console.log( 'An error happened' , path);
});
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