【发布时间】:2021-07-22 12:02:33
【问题描述】:
我想问一下如何使用 THREE.js 在网页上加载动画 3D 模型?目前,我已经设法在网页上使用 THREE.js 加载了我自己的 3D 模型(它名为“aaa.gltf”),具有自动旋转功能,并且能够单击以使用 orbitcontrol.js 在模型中移动。但是,我的模型是静态的。然后,我尝试加载我的另一个 3D 模型,它是动画 3D 模型,它命名为“bbb.glb”,但结果根本无法显示。
这里是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模型的来源从'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
有人知道怎么解决吗?提前致谢!
【问题讨论】:
标签: javascript html css three.js 3d