import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
const canvas = document.querySelector('#frontPageCanvas');
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth
window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({canvas, antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor( 0xffffff, 0);
function loadFiles(capsulesFiles, scene) {
const capsuleloader = new GLTFLoader();
capsulesFiles.forEach((capsulesFile) => {
capsuleloader.load(capsulesFile, (gltf) => {
scene.add(gltf.scene);
});
});
};
let capsulesFiles = ['../three.js/models/capsule_1.glb',
'../three.js/models/capsule_1.glb'];
loadFiles(capsulesFiles, scene);
//const geometry = new THREE.BoxGeometry(2, 2, 2);
//const material = new THREE.MeshBasicMaterial({color: 0x0000ff});
//const cube = new THREE.Mesh(geometry, material);
//scene.add(cube);
camera.position.z = 5;
console.log(scene);
function animate() {
requestAnimationFrame(animate);
// cube.rotation.x += 0.05
// cube.rotation.y += 0.05
scene.children[0].rotation.y += 0.05;
scene.children[1].rotation.x -= 0.01;
scene.children[1].rotation.y -= 0.01;
renderer.render(scene, camera);
}
animate();
Hi, I am new to Three.js and JavaScript! I was trying to see if I could insert my glb models via array (to save code time instead of having unique functions for each one). I figured I could use the scene object to do this, but when I run this code I get this error for the rotation code which says Uncaught TypeError: Cannot read properties of undefined (reading 'rotation') at animate (threejsanimations.js:49:23) at threejsanimations.js:56:1
However, after a little delay, one of my models does rotate (makes no sense since all rotation code shouldn't work). Not sure how to fix this. Also, if any additional help as to why my models have no texture/material would be cool (I used Maya to make them with aiStandardSurface of Glass and Plastic for the materials and the Verge3D plugin to export them as .glb files).