Hi, I’m a beginner and having issues with loading a GLB model (the screen just appears black). Does anyone have any pointers for how to do this? Appreciate your help
main.js:
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
var renderer = new THREE.WebGLRenderer()
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
camera.position.set(20,0,20);
const loader = new THREE.GLTFLoader();
loader.load('turbo.glb', function (gltf) {
scene.add(gltf.scene);
}, undefined, function (error) {
console.error(error);
});
var light = new THREE.AmbientLight(0xffffff);
scene.add(light);
It’s unclear from your example if you have a rendering loop running or not.
In case you don’t:
Since loading takes time, you need to wait till the model is loaded, then add it to the scene and render:
loader.load('turbo.glb', function (gltf) {
scene.add(gltf.scene);
renderer.render(scene, camera); // <- add this line
}, undefined, function (error) {
console.error(error);
});
This link gives us the viewer website, but not your model, you would need to upload that model somewhere. In any case –- do you see the model correctly in the viewer? Or is it also a black screen there?
Note that metallic materials do not respond to ambient light. You need at least a point light or two, preferably an environment map. If the model appears fine in viewers but appears dark or not at all in your application, that could be the cause. Always many possible causes for a black screen in graphics, though, so any errors you see in the JS Console are important to share, and sharing all code necessary to see the issue, and a demo when possible.
The problem is the black background that you have, and the model is also black.
based on your code I set the background color to grey and the model appeared
scene.background = new THREE.Color( 0x999999 );
camera.position.set(5,0,10); //also try setting cam pos to this
Thank you so much all. I can now see the model against a grey background. Would the next step be to add orbit controls so I can move the model around, and how can I zoom in + get the lighting to work? Appreciate your help in advance