king2088 / vue-3d-loader

VueJS and threeJS 3d viewer plugin
https://king2088.github.io/vue-3d-loader-docs
MIT License
226 stars 38 forks source link

关于手动播放同一个glb模型上的不同动画不起效果 #129

Open GloriaCHL opened 1 week ago

GloriaCHL commented 1 week ago

你好。 我有个需求,在同一个glb模型中定义了不同的几个动画,在issues上找到个类似的需求:https://github.com/king2088/vue-3d-loader/issues/90 但是没成功,模型没有动起来。

以下是我的代码主要部分

<template>
  <div class="container">
    <vue3dLoader
      :filePath="files"
      ref="vue3dLoaderRef"
      @load="onLoad"
      :auto-play="false"
    />
  </div>
</template>

<script setup>
import { AnimationMixer } from "three";

const playAnimate = (model) => {
  if (model.animations && model.animations.length) {
    const mixer = new AnimationMixer(model);
    model.animations.forEach((clip, idx) => {
      if (clip) {
        // get animation
        const action = mixer.clipAction(clip);
        action.play();
      }
    });
  }
};

const onLoad = (models) => {
  console.log("~~@@", models);

  const model = models.children.find((i) => i.name === "Scene");
  playAnimate(model);
};

</script>

以下是 onLoad 事件的 models 输出截图

image 仔细观察了下模型的结构,我想逻辑应该没有错才对。


如果直接在组件中将 auto-play 设为 auto 的话,模型是可以动起来的。但是我想通过手动选择性触发某个动画,所以做了这个尝试,但没成功。希望大佬不吝赐教看看我是不是哪里的逻辑写错了,谢谢。

GloriaCHL commented 1 week ago

我手动改了组件代码,expose里面的动画组出去,然后从父组件中调用这个动画组来执行动画就可以成功播放。