好吧,我有 .gltf 动画模型。我成功加载了模型,但无法播放嵌入的动画。我想知道是否有人可以解决它。顺便说一句,我正在对此做出react。先感谢您。
在这里你可以找到模型 https://drive.google.com/file/d/1ZVyklaQuqKbSliu33hFxdyNpg4EQtcBH/view?usp=sharing
这是我尝试过的代码。
import * as THREE from 'three'
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"
import React, { Suspense,useRef, useEffect, useState } from 'react'
import { Canvas ,extend, useThree, useFrame} from 'react-three-fiber'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import './style.module.css'
extend({ OrbitControls })
function Hardware() {
const [scene, set] = useState()
useEffect(() => {
new GLTFLoader().load("tern_construct_animation.gltf", gltf => {
set(gltf.scene)
const mixer = new THREE.AnimationMixer(gltf.scene)
gltf.animations.forEach(clip => mixer.clipAction(clip).play())
})
}, [])
return scene ? <primitive object={scene} /> : null
}
const Controls = () => {
const orbitRef = useRef()
const { camera, gl } = useThree()
useFrame(() => {
orbitRef.current.update()
})
return (
<orbitControls
autoRotate
maxPolarAngle={Math.PI / 3}
minPolarAngle={Math.PI / 3}
args={[camera, gl.domElement]}
ref={orbitRef}
/>
)
}
const animated_element = () => {
return (
<Canvas camera={{ position: [0, 0, 5] }}>
<ambientLight intensity={2} />
<pointLight position={[40, 40, 40]} />
<Controls/>
<Suspense fallback={null}>
<Hardware/>
</Suspense>
</Canvas>
)
}
export default animated_element;