I’m a newbie to Three JS, and I’ve got the basic cube displaying on screen, but I’m really struggling to get my 3D model loading.
I’ve validated the GLB file I’m referencing using https://gltf-viewer.donmccurdy.com, and it loads fine there (less the textures), but when I try to load it on my page it refuses to show anything (if I change the background colour it changes, no errors in the developer tools of MS Edge).
If I’m not mistaken, all objects by default are loaded to location 0,0,0… but is there a way to ensure the camera is locked onto a loaded object (so I can tell if the object is loaded or not) and can you see anything wrong with my code below?
My code is;
import {
Camera,
Color,
Scene,
PerspectiveCamera,
WebGLRenderer,
AmbientLight,
} from "https://cdn.skypack.dev/three@0.132.2";
import { GLTFLoader } from 'https://cdn.skypack.dev/three@0.132.2/examples/jsm/loaders/GLTFLoader.js';
const container = document.querySelector('#scene-container');
const scene = new Scene();
const renderer = new WebGLRenderer();
const hlight = new AmbientLight (0x404040,100);
const fov = 80;
const aspect = container.clientWidth / container.clientHeight;
const near = 0.1;
const far = 100;
const camera = new PerspectiveCamera(fov, aspect, near, far);
const loader = new GLTFLoader();
scene.background = new Color('white');
camera.position.set(0, 50, 100);
scene.add(hlight);
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.setPixelRatio(window.devicePixelRatio);
container.append(renderer.domElement);
loader.load('tree.glb', function(gltf){
const car = gltf.scene.children[0];
car.scale.set(1.5,1.5,1.5);
scene.add(gltf.scene);
animate();
});
renderer.render(scene, camera);
function animate() {
renderer.render(scene,camera);
requestAnimationFrame(animate);
}