I’m new to threeJS, and I’m trying to import a GLTF file using the boilerplate from the three.js website. The console says GLTFLoader: 37.4208984375ms, but I’m not seeing anything. Code below. What’s going wrong here?
var camera, scene, renderer;
var geometry, material, mesh;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 1;
scene = new THREE.Scene();
// Instantiate a loader
var loader = new THREE.GLTFLoader();
// Load a glTF resource
loader.load( 'Box.gltf', function ( gltf ) {
scene.add( gltf.scene );
gltf.animations; // Array<THREE.AnimationClip>
gltf.scene; // THREE.Scene
gltf.scenes; // Array<THREE.Scene>
gltf.cameras; // Array<THREE.Camera>
} );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
}
function animate() {
renderer.render( scene, camera );
}
I wish there was a simple example of a GLFT import somewhere. Does anyone know of one?
I downloaded it and tested it in that viewer, and it looks fine there. Do I maybe need to move the camera position? Is there a conflict between the camera position and the model position?
Is there a conflict between the camera position and the model position?
Well ‘conflict’ is not the right word, but yes, the most likely scenario here, if you are not getting any console messages, is that the camera is just not pointing at the object, or the camera.far is too close.
Thanks Tom! The model shows up great. Now how can I run its animation? I tried gltf.animations[0].play() but I got any error message saying that there is no such play method on that object.
i’m pretty sure it’s not related to the positioning issue, my far is 10000, my minPolarAngle is 0 and my maxPolarAngle is Math.PI so I’m able to look all around the scene.
Of-course the link isn’t minified and you have a console.log for: start loading Gltf . .
and Gltf loaded successfully . . .
Can anyone help me understand what’s causing this issue?
Of-course the link isn’t minified and you have a console.log …
@ranbuch your code is obfuscated by Webpack, can you post it somewhere we can read it? Easier for others to help you debug that way. If you’re just trying to load a model and rotate it with the mouse, see the example here.
I am having this problem and I have tried almost all the suggestions given. My code works for only one model which shows the model in my output. For all the other models it doesn’t show the model in the output. Looks a bit wiered but I will appreciate any help. Tried many values for the camera position.
<script src="js/three.js"></script>
<script type="module">
import { GLTFLoader } from 'https://threejs.org/examples/jsm/loaders/GLTFLoader.js';
import {OrbitControls} from 'https://threejs.org/examples/jsm/controls/OrbitControls.js';
const scene = new THREE.Scene();
{
const color = 0xFFFFFF;
const intensity = 1;
const light = new THREE.DirectionalLight(color, intensity);
light.position.set(-2, 2, 5);
scene.add(light);
}
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor(0x00ffff);
document.body.appendChild( renderer.domElement );
let mixer;
const loader = new GLTFLoader();
loader.load( 'scene.gltf', ( gltf ) => {
const model = gltf.scene;
mixer = new THREE.AnimationMixer( model );
gltf.animations.forEach(( clip ) => {
mixer.clipAction(clip).play();
});
});
camera.position.set( 0, 0,100 );
function animate() {
requestAnimationFrame( animate );
//cube.rotation.y += 0.02;
//controls.update();
renderer.render( scene, camera );
}
animate();`Preformatted text`