vue3通过three.js加载模型并播放动画
获取模型地址,加载模型动画只播放一次,添加缩放、拉拽控件
<template>
<div>
<div ref="container"></div>
</div>
</template>
<script setup>
import { OrbitControls } from '../../../../node_modules/three/examples/jsm/controls/OrbitControls.js'
import * as THREE from "../../../../node_modules/three/build/three.module.js"
import { FBXLoader } from "../../../../node_modules/three/examples/jsm/loaders/FBXLoader.js"
const init = (e) => {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(55, 600 / 400, 30, 1000);
camera.position.z = 355;
camera.position.x = 0;
camera.position.y = -350;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(600, 400);
container.value.appendChild(renderer.domElement);
scene.background = new THREE.Color(0x333333);
let controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true
controls.enableZoom = true
controls.minDistance = 200
controls.maxDistance = 500
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
scene.add(directionalLight);
const loader = new FBXLoader();
loader.load(e, (fbx) => {
scene.add(fbx);
frame.value = fbx.animations[0].tracks[0].times.length
console.log(fbx.animations[0].tracks[0].times.length, '总帧数')
animationMixer.value = new THREE.AnimationMixer(fbx);
animationAction = animationMixer.value.clipAction(fbx.animations[0]);
animationAction.play();
})
const animate = () => {
requestAnimationFrame(animate);
controls.update()
renderer.render(scene, camera);
if (animationMixer.value) {
animationMixer.value.update(0.01);
}
};
animate();
}
var filePath = ""
function showInfo(row) {
filePath = "api/" + row.src
nextTick(() => {
init(filePath)
})
}
</script>
<style lang="scss" >
</style>
![在这里插入图片描述](https://img-blog.csdnimg.cn/6e49296d6dfd4cb18df0fa7331f7e190.jpeg#pic_center)