[英]How to load animated 3D model on webpage using THREE.js
我想問一下如何使用THREE.js在網頁上加載動畫3D model? 目前,我已經設法在網頁上使用 THREE.js 加載我自己的 3D model (它名為“aaa.gltf”),並能夠使用自動旋轉並能夠單擊以使用 Z20F35E630DAF44DBFA4C3F68jsZ539 控制在軌道上移動。 但是,我的 model 是 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.
這是 html 文件:
<!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>
這是js文件:
//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);
這是 css 文件:
* {
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%;
}
我只在js文件的第46行將3D model的來源從'aaa.gltf'更改為'bbb.glb':
由此
loader.load("./house/aaa.gltf", function(gltf) {
進入這個
loader.load("./house/bbb.glb", function(gltf) {
這是'aaa.gltf'的附件: https://drive.google.com/file/d/1RCO8iSvYwTZdcTC55EVzJ0rBLl6aZy9L/view?usp=sharing
這是“bbb.glb”的附件: https://drive.google.com/file/d/1fzk7AZl2VHwTvZm0Gl0m-oyaIZUmHyeB/view?usp=sharing
任何人都知道如何解決它? 提前致謝!
您的代碼非常不完整,無法為 3D model 設置動畫,基本上您只是加載它而不播放嵌入式 animation。 為此,您需要創建一個AnimationMixer
, select 要播放的剪輯動作(動畫),然后更新 animation。
除此之外,model 是用 DRACO 編碼的,所以我也添加了它。 更多更改,例如THREE.sRGBEncoding
以獲得 model 的真實 colors,並減少了照明,因為它太強烈了......
編輯:整頁代碼
<!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" />
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: sans-serif;
overflow: hidden;
}
.scene,
canvas {
position: absolute;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div class="scene"></div>
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/loaders/GLTFLoader.js"></script>
<script src="https://threejs.org/examples/js/loaders/DRACOLoader.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script>
//Variables for setup
let container;
let camera;
let renderer;
let scene;
let house;
let mixer;
const clock = new THREE.Clock();
function init() {
container = document.querySelector(".scene");
//Create scene
scene = new THREE.Scene();
const fov = 35;
const aspect = container.clientWidth / container.clientHeight;
const near = 1;
const far = 10000;
//Camera setup
camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 0, 1000);
const ambient = new THREE.AmbientLight(0x404040, 1);
scene.add(ambient);
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(50, 50, 100);
scene.add(light);
//Renderer
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.outputEncoding = THREE.sRGBEncoding;
container.appendChild(renderer.domElement);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0.5, 0);
controls.update();
controls.enablePan = false;
controls.enableDamping = true;
const dracoLoader = new THREE.DRACOLoader();
dracoLoader.setDecoderPath('https://threejs.org/examples/js/libs/draco/gltf/');
//Load Model
let loader = new THREE.GLTFLoader();
loader.setDRACOLoader(dracoLoader);
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);
</script>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.