0

我在 React 应用程序中加载从 Sketchfab 下载的 GLTF 模型。模型加载得很好,但动画根本不播放。我尝试了不同的方法。有任何想法吗?

function Model({ url }) {

  const model = useRef()
  const { scene, animations } = useLoader(GLTFLoader, url)
  const [mixer] = useState(() => new THREE.AnimationMixer())
  useEffect(() => void mixer.clipAction(animations[0], group.current).play(), [])


  return(
        <primitive
          ref={model}
          object={scene}
        />
  )
}
4

2 回答 2

0

您需要在组件内的 useFrame 中调用 mixer.update(delta) :

    import { useFrame } from 'react-three-fiber'
    function Model({url}) {
    .
    . 
    .
        useFrame((scene, delta) => {
            mixer?.update(delta)
        })
    .
    . 
    .
于 2021-01-03T05:58:50.600 回答
0

解决方案

  1. 我不得不使用节点而不是场景并选择“RootNode”(console.log 节点并选择我认为的主节点,模型包含十几个节点)
  2. 使用useFrame按帧更新混音器
  3. 将动画应用于模型(有点更新)

工作代码:


function Model({ url }) {

   const group = useRef()
  const { nodes, scene, materials, animations } = useLoader(GLTFLoader, url)
 const actions = useRef()
  const [mixer] = useState(() => new THREE.AnimationMixer())
  useFrame((state, delta) => mixer.update(delta))
  useEffect(() => {
    actions.current = { idle: mixer.clipAction(animations[0], group.current) }
    actions.current.idle.play()
    return () => animations.forEach((clip) => mixer.uncacheClip(clip))
  }, [])


  return(
     <group ref={group} dispose={null}>
        <primitive
          ref={group}
          name="Object_0"
          object={nodes["RootNode"]}
        />
        </group>
  )
}

现在它可以工作了

于 2020-12-04T18:30:46.567 回答