I’m trying to use a mesh I created in blender in threejs. The event log shows the file is being loaded but I cannot see it. Instead there is just a black screen. How do I go about this to have the mesh actually show up?
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
const scene = new THREE.Scene();
const loader = new GLTFLoader();
loader.load(
'scene.glb',
function ( gltf ) {
scene.add( gltf.scene );
},
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
function ( error ) {
console.log( 'An error happened' );
}
);
const light = new THREE.PointLight(0xffffff, 9, 100, 1.7);
light.position.set(1.53, 2.627, 1.217)
scene.add(light)
const camera = new THREE.PerspectiveCamera(45, 800 / 600)
camera.position.z = 24.327
camera.position.x = -4.288
camera.position.y = 24.327
scene.add(camera)
const canvas = document.querySelector(".webgl")
const renderer = new THREE.WebGL1Renderer({canvas})
renderer.setSize(800, 600)
renderer.render(scene, camera)
I’ve gone to three.js editor to view my model and once I change my light’s intensity I can see my mesh but when I try to do in my javascript file it does not change. I’ve tried readjusting the camera position but I still cannot see the mesh.
2
Answers
Probably this is a combination of your model and the lighting. In general a single point light is not very effective as lighting – an environment map or THREE.RoomEnvironment is generally a safer place to start.
Also try setting a background color for the scene…
… so that even if the model is not well-lit, you’ll still see the black outline of the model against a non-black background.
Try adding an animation loop, put
camera.lookAt(scene.position);
inside. I don’t know if it will be clearly visible in the photo, but theglb
(blue) is the there, trust me 😉 Probably you still need to tinker with the camera settings or/and glb (scale
for example), because mesh is extremely small.