#
luxiaotao1123
2024-05-15 b8bac27bcc895b540bae4c130a3c59d289736081
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { useState, useMemo, useRef, useEffect } from 'react';
import { useFrame } from '@react-three/fiber';
import { useFBX, useAnimations } from '@react-three/drei';
import * as THREE from 'three';
import {
    MODEL_BOX_SCALE,
    SHELF_BAY_GROUP_COUNT,
    SHELF_BAY_UNIT_SEPARTE_DISTANCE,
    SHELF_LEV_UNIT_SEPARTE_DISTANCE,
    SHELF_HEIGHT_FROM_GROUND
} from '@/config/setting'
import * as Common from '../utils/common';
import { useStore } from '@/store';
 
const getShelfRow = (row) => {
    return row;
}
 
const getShelfBay = (bay) => {
    return bay - (bay - 1) % SHELF_BAY_GROUP_COUNT;
}
 
const getShelfLev = (lev) => {
    return 1;
}
 
const getShelfNo = (row, bay, lev) => {
    return Common.generateLocNo(getShelfRow(row), getShelfBay(bay), getShelfLev(lev));
}
 
const Box = (props) => {
    const { row, bay, lev, position = [0, 300, 0], rotationY = 0 } = props;
    const state = useStore();
 
    const [pos, setPos] = useState([]);
    const [rotaY, setRotaY] = useState(0);
 
    const boxModel = useMemo(() => {
        const fbx = useFBX('/models/box/box.fbx');
        if (!fbx.castShadow) {
            Common.setShadow(fbx);
        }
        fbx.scale.set(MODEL_BOX_SCALE, MODEL_BOX_SCALE, MODEL_BOX_SCALE);
        return fbx.clone();
    }, [])
 
    useEffect(() => {
        if (row && bay && lev) {
            // from shelf
            const shelfNo = getShelfNo(row, bay, lev);
            const shelfPos = state.shelfList[shelfNo];
            if (!shelfPos) { return }
            const { row: shelfRow, bay: shelfBay, lev: shelfLev } = Common.parseLocNo(shelfNo);
 
            // cal bay position
            let posZByLev = shelfPos[2];
            if (bay === shelfBay) {
                posZByLev = shelfPos[2] + SHELF_BAY_UNIT_SEPARTE_DISTANCE;
            }
            if (bay === shelfBay + 1) {
                posZByLev = shelfPos[2];
            }
            if (bay === shelfBay + 2) {
                posZByLev = shelfPos[2] - SHELF_BAY_UNIT_SEPARTE_DISTANCE;
            }
 
            setPos([
                shelfPos[0],
                shelfPos[1] + SHELF_HEIGHT_FROM_GROUND + (lev - shelfLev) * SHELF_LEV_UNIT_SEPARTE_DISTANCE,
                posZByLev
            ]);
            setRotaY(Math.PI / 2);
        } else {
            // from agv
            setPos(position);
            setRotaY(rotationY);
        }
    }, [props]);
 
    return (
        <>
            <group rotation-y={rotaY} position={pos}>
                <primitive object={boxModel} castShadow />
            </group>
        </>
    )
}
 
export default Box;