I just started using three js and I was trying to switch to modules but the gltf loader doesn’t work. I even tried using examples I found on the main site but I can’t make them work. This is the code, very simple because I was trying to understand what didn’t work exactly, and it turns out it’s the loader section, everything else works if I comment it out
<html>
<head>
<title>Document</title>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<script type="module">
import * as THREE from './build/three.module.js';
import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
import { OrbitControls } from './jsm/controls/OrbitControls.js';
let camera,
scene,
renderer;
//RENDERER
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
//CAMERA
camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 10000;
camera.position.set(10,4,-5);
var controls = new OrbitControls(camera, renderer.domElement);
controls.update();
//SCENE
scene = new THREE.Scene();
//LIGHTS
var light = new THREE.AmbientLight(0xffffff, 1);
scene.add(light);
//MATERIAL
var material = new THREE.MeshBasicMaterial({color: 0x77cc77});
var geometry = new THREE.BoxGeometry();
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);
const loader = new GLTFLoader();
loader.load('./cube.glb', function ( gltf ) {
const mesh = scene.children[0];
scene.add(mesh);
} );
//RENDER LOOP
render();
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
}
</script>
</body>
</html>
The cube.glb object is literally the default Blender cube I exported after getting rid of the light and the camera. I also tried to take its geometry and paint it like the other cube, and also to change the background to white to make sure it wasn’t just blending in with the background, still nothing
I managed to load models, merge them etc with three.js, that’s why I feel like it’s due to modules, unless I’m missing something