import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const loader = new GLTFLoader();
loader.load('assets/untitled.glb', function (gltf) {
scene.add(gltf.scene);
// Only render once after the model is loaded
renderer.render(scene, camera);
}, undefined, function (error) {
console.error(error);
});
I’ve tried to set up a camera and use materials from public URL. still not displaying.
But i tried all of the threejs tutorials (creating a scene, drawing lines, and text) all of them worked and displayed. except when i try to load a glb model.
And I’ve tried to view the web using chrome, and still not displaying.
It’s really important to open the JS console and look at errors there. In the codepen you’ll see a 404 “Failed to load resource”, there is no main.js in a Codepen, the script is just loaded automatically.
The other issues going on here are:
The model is very small and the camera is distant. Try a camera position of <0, 0, 2.5>.
No lights in the scene. Try setting a background color or adding scene.add(new THREE.AmbientLight());.
render() is called only once, and not in the loader callback. You can either call render again after adding the model to the scene, or look at three.js examples for how to set up a render loop that renders repeatedly on every frame.