【问题标题】:Three.js doesn't play animation in ReactThree.js 在 React 中不播放动画
【发布时间】:2021-03-16 19:24:21
【问题描述】:

我在 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}
        />
  )
}

【问题讨论】:

    标签: three.js gltf react-three-fiber


    【解决方案1】:

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

        import { useFrame } from 'react-three-fiber'
        function Model({url}) {
        .
        . 
        .
            useFrame((scene, delta) => {
                mixer?.update(delta)
            })
        .
        . 
        .
    

    【讨论】:

      【解决方案2】:

      解决方案

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

      现在可以了

      【讨论】:

      猜你喜欢
      • 2018-04-06
      • 2016-03-01
      • 1970-01-01
      • 2018-08-06
      • 2017-07-12
      • 2018-08-26
      • 1970-01-01
      • 2019-12-13
      • 2013-02-11
      相关资源
      最近更新 更多