Hi there, I’m new to Three.js and recently tried following a tutorial on importing a custom 3D model from Blender. Unfortunately, I encountered an issue where my model wouldn’t appear in the scene. I first checked if the GLB file was corrupted, but it seemed fine. I also attempted to rescale the model, but it still didn’t show up. According to the tutorial, a copy of the model should have been placed in the ‘dist’ folder, but I couldn’t find it there. Any suggestions on how to troubleshoot this further would be appreciated!
Here is the code:
import * as THREE from 'three';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js';
import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js';
const renderer = new THREE.WebGLRenderer();
renderer.shadowMap.enabled = true;
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
const orbit = new OrbitControls(camera, renderer.domElement);
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
camera.position.set(0,2,5);
orbit.update();
// const boxGeometry = new THREE.BoxGeometry();
// const boxMaterial = new THREE.MeshBasicMaterial({color: 0x00FF00});
// const box = new THREE.Mesh(boxGeometry, boxMaterial);
// scene.add(box);
// Add ambient light to the scene
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); // Color, Intensity
scene.add(ambientLight);
// Add directional light to the scene
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5); // Color, Intensity
directionalLight.position.set(5, 10, 5); // Set light position
directionalLight.castShadow = true; // Enable shadow casting
scene.add(directionalLight);
// Optional: Add helper to visualize the directional light
const directionalLightHelper = new THREE.DirectionalLightHelper(directionalLight);
scene.add(directionalLightHelper);
const loader = new GLTFLoader();
loader.load( '../assets/monkey.glb', function ( gltf ) {
const model = gltf.scene;
scene.add( model );
}, undefined, function ( error ) {
console.error( error );
} );
function animate(){
renderer.render(scene, camera);//allows for orbit control to be shown
}
renderer.setAnimationLoop(animate);
Here is the screen shot of the folder I was talking about: