| New file |
| | |
| | | import { useRef, useEffect } from 'react'; |
| | | import { useFrame } from '@react-three/fiber'; |
| | | import { useFBX, useAnimations } from '@react-three/drei'; |
| | | import { AnimationMixer } from 'three'; |
| | | |
| | | const Agv = (props) => { |
| | | const fbxRef = useRef(); |
| | | const mixerRef = useRef(); |
| | | |
| | | const fbx = useFBX('/models/agv/body.fbx'); |
| | | |
| | | const setShadow = (obj) => { |
| | | obj.castShadow = true; |
| | | obj.receiveShadow = true; |
| | | |
| | | if (obj.children) { |
| | | obj.children.forEach((child) => { |
| | | setShadow(child); |
| | | }); |
| | | } |
| | | }; |
| | | |
| | | const setColor = (obj) => { |
| | | if (obj.material) { |
| | | obj.material.color.set(0x4680BF); |
| | | } |
| | | if (obj.children) { |
| | | obj.children.forEach((child) => { |
| | | setColor(child); |
| | | }); |
| | | } |
| | | }; |
| | | |
| | | // setColor(fbx) |
| | | if (!fbx.castShadow) { |
| | | setShadow(fbx); |
| | | } |
| | | |
| | | useEffect(() => { |
| | | mixerRef.current = new AnimationMixer(fbx); |
| | | |
| | | if (fbx.animations.length > 0) { |
| | | useFrame((state, delta) => { |
| | | mixerRef?.current?.update(delta); |
| | | }); |
| | | |
| | | const action = mixerRef.current.clipAction(fbx.animations[0]); // 假设只有一个动画,可以根据实际情况修改 |
| | | action.play(); |
| | | } |
| | | |
| | | return () => { |
| | | mixerRef.current.stopAllAction(); |
| | | }; |
| | | }, []); |
| | | |
| | | return ( |
| | | <> |
| | | <group rotation={[0, 0, 0]} scale={0.5} position={[0, 0, 0]}> |
| | | <primitive object={fbx} castShadow /> |
| | | </group> |
| | | </> |
| | | ) |
| | | } |
| | | |
| | | export default Agv; |