#
luxiaotao1123
2024-04-23 9fce7ce7b76b859c5b70cc622d885cd9d6c1579f
src/components/agv.jsx
@@ -1,62 +1,100 @@
import { useRef, useEffect } from 'react';
import { useState, useMemo, useRef, useEffect } from 'react';
import { useFrame } from '@react-three/fiber';
import { useFBX, useAnimations } from '@react-three/drei';
import { AnimationMixer } from 'three';
import * as THREE from 'three';
import Box from './box';
import * as Common from '../utils/common';
import * as TWEEN from "@tweenjs/tween.js";
import { INTERVAL_TIME } from '@/config/setting'
const Agv = (props) => {
    const fbxRef = useRef();
    const mixerRef = useRef();
    const { position, theta, height, loaderTheta, forkLength, trayList, hasBox } = props;
    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);
    }
    const [groupPos, setGroupPos] = useState({ x: 0, y: 0, z: 0 });
    const [groupTheta, setGroupTheta] = useState(0);
    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();
        if (!props || Object.keys(props).length === 0) {
            return;
        }
        // group position
        const pos = {
            x: position?.[0],
            y: position?.[1],
            z: position?.[2],
        }
        if (!Common.deepEqual(groupPos, pos)) {
            new TWEEN.Tween(groupPos)
                .to({
                    x: position[0],
                    y: position[1],
                    z: position[2]
                }, INTERVAL_TIME)
                .easing(TWEEN.Easing.Linear.None)
                .onUpdate((e) => {
                    setGroupPos({ ...e });
                })
                .onComplete((e) => {
                    setGroupPos({ ...e });
                }).start();
        }
        return () => {
            mixerRef.current.stopAllAction();
        };
    }, []);
        // group theta
        if (groupTheta !== theta) {
            new TWEEN.Tween({ value: groupTheta })
                .to({ value: theta }, INTERVAL_TIME)
                .easing(TWEEN.Easing.Linear.None)
                .onUpdate((e) => {
                    setGroupTheta(e.value)
                })
                .onComplete((e) => {
                    setGroupTheta(e.value)
                }).start();
        }
    }, [props]);
    useFrame((state, delta) => {
        TWEEN.update();
    })
    const bodyModel = useMemo(() => {
        const fbx = useFBX('/models/agv/body.fbx');
        if (!fbx.castShadow) {
            Common.setShadow(fbx);
        }
        return fbx.clone();
    }, [])
    const loaderModel = useMemo(() => {
        const fbx = useFBX('/models/agv/loader.fbx');
        if (!fbx.castShadow) {
            Common.setShadow(fbx);
        }
        return fbx.clone();
    }, [])
    const forkModel = useMemo(() => {
        const fbx = useFBX('/models/agv/fork.fbx');
        if (!fbx.castShadow) {
            Common.setShadow(fbx);
        }
        return fbx.clone();
    }, [])
    return (
        <>
            <group rotation={[0, 0, 0]} scale={0.5} position={[0, 0, 0]}>
                <primitive object={fbx} castShadow />
            <group
                rotation-y={Common.rotationParseNum(groupTheta)}
                scale={0.5}
                position={[groupPos.x, groupPos.y, groupPos.z]}
            >
                <primitive object={bodyModel} castShadow />
                <primitive object={loaderModel} castShadow position={[0, 100, 0]} />
                <primitive object={forkModel} castShadow position={[0, 120, 0]} />
                <Box position={[100, 100, 300]} />
            </group>
        </>
    )