vue中将three.js导入的3D模型中原本带有的动画进行播放

1.首先是你要确保模型中是有动画的

2.在加载模型后获取动画混合器并对设置动画的物体进行播放

javascript 复制代码
 loadModel() {
      // 加载3D模型
      const loader = new GLTFLoader();
      loader.load('/models/donghua.glb', (gltf) => {
        this.model = gltf.scene
        this.scene.add(this.model);
        // 获取动画混合器
        const animations = gltf.animations;
        if (animations && animations.length) {
          this.mixer = new THREE.AnimationMixer(gltf.scene);
          for (let i = 0; i < animations.length; i++) {
            const action = this.mixer.clipAction(animations[i]);
            action.play();
          }
        }
      }, (xhr) => {
        console.log(xhr, 'xhr');
      }, (err) => {
        console.log(err, 'err');
      });
}

3.关键的一步在动画的函数中去实时更新模型动画update函数中的值越小转动越快

javascript 复制代码
 animate() {
      this.controls.update()
      this.renderer.render(this.scene, this.camera)
      requestAnimationFrame(this.animate)
       // 更新动画
       if (this.mixer) {
        this.mixer.update(0.05);
      }
}

就愉快的转起来了

相关推荐
Hilaku40 分钟前
删了一个没用的 console.log,生产环境直接崩了!
前端·javascript·程序员
真上帝的左手42 分钟前
2. 编程语言-JavaScript
开发语言·javascript
2601_949847751 小时前
Form-data 跨端三方库仓颉适配全流程
前端·javascript·flutter
zyseo81 小时前
谷歌SEO 内容更新焕新流程:让老页面重新起量
java·前端·javascript
Oiiouui1 小时前
关于 Logto 登录系统的体验以及建议
javascript·安全·react
richdata2 小时前
从采购承诺到库存周转:降低库存资金占用的六个计划杠杆
大数据·前端·javascript
志尊宝4 小时前
AI原生前端开发(031):AI辅助的性能分析与瓶颈定位
javascript·人工智能·ai-native
adinnet20264 小时前
持续智能问数:多轮对话状态管理、下钻钻取与经营驾驶舱的演进
javascript·数据库·人工智能
FYKJ_20104 小时前
springboot高考志愿填报推荐系统14332-计算机课程设计、毕业设计
javascript·spring boot·python·架构·django·课程设计·高考
全栈若城4 小时前
从源码拆解一个 AIUI 每日一词智能体:页面、离线索引与硬件交互
javascript