I would like to ask how to load animated 3D model on webpage using THREE.js? Currently I had managed to load my own 3D model (it named 'aaa.gltf') using THREE.js on webpage with auto rotate and able to click to move around the model using orbitcontrol.js. However, my model is static. Then, I tried to load my another 3D model which is animated 3D model, it named "bbb.glb", but it turns out couldn't be displayed at all.
Here is html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="./style.css" />
<title>Document</title>
</head>
<body>
<div class="scene"></div>
<script src="./three.min.js"></script>
<script src="./GLTFLoader.js"></script>
<script src="./OrbitControls.js"></script>
<script src="./app.js"></script>
</body>
</html>
Here is js file:
//Variables for setup
let container;
let camera;
let renderer;
let scene;
let house;
let mixer;
function init() {
container = document.querySelector(".scene");
//Create scene
scene = new THREE.Scene();
const fov = 35;
const aspect = container.clientWidth / container.clientHeight;
const near = 0.1;
const far = 1000;
//Camera setup
camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 0, 100);
const ambient = new THREE.AmbientLight(0x404040, 2);
scene.add(ambient);
const light = new THREE.DirectionalLight(0xffffff, 2);
light.position.set(50, 50, 100);
scene.add(light);
// controls.addEventListener('change', renderer);
//Renderer
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.setPixelRatio(window.devicePixelRatio);
controls = new THREE.OrbitControls(camera, renderer.domElement);
container.appendChild(renderer.domElement);
const clock = new THREE.Clock();
//Load Model
let loader = new THREE.GLTFLoader();
loader.load("./house/bbb.glb", function(gltf) {
scene.add(gltf.scene);
house = gltf.scene.children[0];
mixer = new THREE.AnimationMixer( house );
mixer.clipAction(gltf.animations[0]).play();
animate();
});
}
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
mixer.update( delta );
house.rotation.z += 0.005;
renderer.render(scene, camera);
}
init();
function onWindowResize() {
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
}
window.addEventListener("resize", onWindowResize);
Here is css file:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: sans-serif;
overflow: hidden;
background: url("https://wallpaperaccess.com/full/1155041.jpg");
}
.scene,
canvas {
position: absolute;
width: 100%;
height: 100%;
}
I only change the source of 3D model from 'aaa.gltf' into 'bbb.glb' at the line 46 of js file:
from this
loader.load("./house/aaa.gltf", function(gltf) {
into this
loader.load("./house/bbb.glb", function(gltf) {
Here is the attachments of 'aaa.gltf': https://drive.google.com/file/d/1RCO8iSvYwTZdcTC55EVzJ0rBLl6aZy9L/view?usp=sharing
Here is the attachments of 'bbb.glb': https://drive.google.com/file/d/1fzk7AZl2VHwTvZm0Gl0m-oyaIZUmHyeB/view?usp=sharing
Anyone knows how to solve it? Thanks in advance!