Hey everyone, Im still new to threejs and have been having some trouble loading in a model I built from blender through the gltf loader. I’ve tried loading models I’ve found on the web and they work perfect. I’ve also tried loading my exported gltf file into https://gltf-viewer.donmccurdy.com/ and I can see if fine too so it must be something in my code. I just cant figure out what.
Here is my code: Thanks in advance
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<!-- <div class="scene"></div> -->
<script src="libraries/three.min.js"></script>
<script src="libraries/PointerLockControls.js"></script>
<script src="libraries/OrbitControls.js"></script>
<script src="libraries/GLTFLoader.js"></script>
<script>
let scene,camera,renderer, controls, model;
function init(){
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xFFFFFF );
//Camera Things
camera = new THREE.PerspectiveCamera( 75, window.innerWidth/ window.innerHeight, 0.1, 5000 );
camera.position.set(0,25,50);
// Lighting Things
const ambient = new THREE.AmbientLight(0xFFFFFF,4);
scene.add(ambient);
//
// const light = new THREE.DirectionalLight(0xFFFFFF,2);
// light.position.set(0,50,100);
// scene.add(light);
// Renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth,window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
//Injects the canvas into HTML Page
document.body.appendChild( renderer.domElement );
//--------------- Above is always the same with setting a scene up in three
// controls
controls = new THREE.PointerLockControls(camera, renderer.domElement);
// controls = new THREE.OrbitControls(camera, renderer.domElement);
document.body.addEventListener( 'click', function () {
controls.lock();
}, false );
scene.add( new THREE.AxesHelper(500));
// controls.update();
camera.position.y = 1;
camera.position.z = 2;
const onKeyDown = function (event) {
switch (event.keyCode) {
case 87: // w
controls.moveForward(.25);
break;
case 65: // a
controls.moveRight(-.25);
break;
case 83: // s
controls.moveForward(-.25);
break;
case 68: // d
controls.moveRight(.25);
break;
}
};
document.addEventListener('keydown', onKeyDown, false);
}
// basic Animation
function animate() {
// controls.update();
requestAnimationFrame( animate );
renderer.render( scene, camera);
};
init();
animate();
// Load Model
new THREE.GLTFLoader().load('3d/trial1/file3.gltf', result => {
model = result.scene.children[0];
model.position.set(0,-5,0);
scene.add(model);
animate();
})
// function onWindowResize(){
// camera.aspect = window.innerWidth / window.innerHeight;
// camera.updateProjectionMatrix( );
// renderer.setSize(window.innerWidth , window.innerHeight);
// };
//
// window.addEventListener('resize',onWindowResize,false);
</script>
</body>
</html>
file3.bin (1.8 MB) file3.gltf (868.2 KB)