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);
      }
}

就愉快的转起来了

相关推荐
To_OC7 小时前
LC 131 分割回文串:刚学回溯时,我连怎么切字符串都想不明白
javascript·算法·leetcode
To_OC9 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
小林ixn9 小时前
从 ??= 到 onKeyDown:一个 React 组件的“自我修养”
前端·javascript·react.js
এ慕ོ冬℘゜9 小时前
前端实战:jQuery 多条件联合搜索(标题模糊查询 + 日历时间段筛选)
前端·javascript·jquery
随心点儿10 小时前
js postMessage 实现跨域发送消息-应用示例
javascript·ecmascript·postmessage
kyriewen12 小时前
我让AI改一个bug——它偷偷动了5个我没让它碰的地方
前端·javascript·ai编程
慧一居士13 小时前
Element Plus 按需引入的配置使用说明和完整示例
前端·vue.js
科技大视界15 小时前
非接触3D形貌测量:按样品类型选设备指南
3d
柒木森16 小时前
AAAI 2026 | Arbi-3DGSR:任意倍率 3D 高斯超分辨率
3d
gis开发之家16 小时前
《Vue3 从入门到大神35篇》Vue3 源码详解(五):effect 依赖收集原理——track 与 trigger 是如何工作的?
javascript·typescript·前端框架·vue3·vue3源码