#
luxiaotao1123
2024-04-19 292cb4401ad94f02459eb802699ba83b75bfa112
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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;