Reputation: 31
Im fairly new at three.js and am trying to load a model into my canvas. as soon as I import the GLTFLoader, I get the above reading in the console. What is going on? The syntax and relative paths looks fine. I dont really understand whats going on. I added another folder to put the master three.js folder into but it is not working.
import * as THREE from '../master/three.js-master/build/three.module.js'
import {GLTFLoader} from '../master/three.js-master/examples/jsm/loaders/GLTFLoader.js'
const canvas = document.querySelector('.webgl')
const scene = new THREE.Scene()
const loader = new GLTFLoader()
loader.load('../assets/scene.gltf', function(gltf){
console.log(gltf);
const root = gltf.scene;
scene.add(root);
}, function(xhr){
console.log((xhr.loaded/xhr.total * 100) + "% loaded")
}, function(error){
console.log('error');
})
// const geometry = new THREE.BoxGeometry(1,1,1)
// const material = new THREE.MeshBasicMaterial({
// color: 'green'
// })
const boxMesh = new THREE.Mesh(geometry,material)
scene.add(boxMesh)
//Boiler Plate
const sizes = {
width: window.innerWidth,
height: window.innerHeight,
}
const camera = new THREE.PerspectiveCamera(75, sizes.width/sizes.height, 0.1, 100)
camera.position.set(0,1,2)
scene.add(camera)
const renderer = new THREE.WebGLRenderer({
canvas: canvas
})
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
renderer.shadowMap.enabled = true
// renderer.outputEncoding = true
renderer.render(scene, camera)
console.log('working')
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Landing Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas class="webgl"></canvas>
<script type = "module" src="scripts.js"></script>
<script>
</script>
</body>
</html>
Upvotes: 3
Views: 777
Reputation: 31086
Since r137
using ES6 modules like GLTFLoader
in the browser requires an import map. Add the following section right below you canvas
.
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../master/three.js-master/build/three.module.js"
}
}
</script>
The three.js
import statement in your scripts.js
looks like so then:
import * as THREE from 'three'
Upvotes: 2