#
luxiaotao1123
2024-10-17 4f32ade76be015062d305c6b0eecf1b7fbedd331
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React, { useRef, useEffect, useState } from 'react';
import * as THREE from 'three';
import { Box, CircularProgress } from '@mui/material';
import AgvThree from './AgvThree';
import { getAgvInfo } from '../../http';
 
let three;
 
const startThree = (dom) => {
    three = new AgvThree(dom);
    three.startup();
}
 
const endThree = () => {
    if (three) {
        three.destroy();
        three = null;
    }
}
 
const renderThree = (info, curAgvNo) => {
    if (info && three) {
        three.generateMesh((loader, addObject) => {
            const loadModel = (path) => {
                return new Promise((resolve, reject) => {
                    loader.load(path, (mesh) => {
                        resolve(mesh);
                    }, undefined, (error) => {
                        reject(error);
                    });
                });
            };
 
            Promise.all([
                loadModel('model/agv/body.fbx'),
                loadModel('model/agv/loader.fbx'),
                loadModel('model/agv/fork.fbx'),
            ]).then(([bodyMesh, loaderMesh, forkMesh]) => {
                const scaleVal = 1;
                bodyMesh.scale.set(scaleVal, scaleVal, scaleVal);
                loaderMesh.scale.set(scaleVal, scaleVal, scaleVal);
                forkMesh.scale.set(scaleVal, scaleVal, scaleVal);
 
                loaderMesh.position.set(0, 150, 0);;
                forkMesh.position.set(0, 165, 0);;
 
                const agvGroup = new THREE.Group();
                agvGroup.add(bodyMesh);
                agvGroup.add(loaderMesh);
                agvGroup.add(forkMesh);
 
                agvGroup.name = curAgvNo;
 
                addObject(agvGroup);
 
                three.setNewSelectedMesh(curAgvNo);
                three.rePerspective(350, 550);
 
            }).catch((error) => {
                console.error(error);
            });
        });
    }
}
 
const AgvMain = (props) => {
    const { data, curAgvNo, setCurAgvNo } = props;
    const containerRef = useRef();
    const [loading, setLoading] = useState(true);
    const [info, setInfo] = useState(null);
 
    useEffect(() => {
        if (data) {
            getAgvInfo(data.no, (response) => {
                setInfo(response);
                setCurAgvNo(data.no);
            });
        }
    }, [data]);
 
    useEffect(() => {
        if (info) {
            endThree();
            setLoading(true);
            setTimeout(() => {
                startThree(containerRef.current);
                three.handleClick = (objName) => {
                    setCurAgvNo(objName);
                };
                renderThree(info, curAgvNo);
                setLoading(false);
            }, 300);
        }
        return endThree;
    }, [info]);
 
    return (
        <Box display="flex" height="100%">
            <Box
                position="relative"
                width="50%"
                height="100%"
                ref={containerRef}
                style={{ backgroundColor: '#7a7a7a' }}
            >
                {loading && (
                    <Box
                        position="absolute"
                        top="50%"
                        left="50%"
                        style={{ transform: 'translate(-50%, -50%)' }}
                    >
                        <CircularProgress />
                    </Box>
                )}
            </Box>
            <Box width="50%" height="100%" overflow="auto" pl={1}>
            </Box>
        </Box>
    );
}
 
export default AgvMain;