#
luxiaotao1123
2024-04-24 6546ed84364eacccf10351f0c671dffb545900a0
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
import React, { useRef, useState } from 'react';
import { useFrame, useThree } from '@react-three/fiber';
import * as THREE from 'three';
import { CameraControls } from '@react-three/drei';
import Text2 from './text';
// import Annotation, { IAnnotationDataItem, IAnnotationRef } from './annotation';
 
const Y = 1;
const Area = ({
  x,
  y,
  width,
  height,
  areaNumber,
  textHeight,
  strokeColor,
}) => {
  const [hovered, setHover] = useState(false);
  const [clicked, setClicked] = useState(false);
  const meshRef = useRef(null);
  const { controls } = useThree((state) => ({
    controls: state.controls,
  }));
 
  // 转换为x轴和z轴组成的坐标系中的位置
  const position = new THREE.Vector3(x + width / 2, Y, y + height / 2);
  const size = [width, 1, height];
  textHeight ||= Y + 50;
 
  // 相机移动到区域的位置
  const handleClick = () => {
    if (clicked) return;
    setClicked(true);
    // controls.fitToBox(meshRef.current!, true);
    const mesh = meshRef.current;
    const { x, y, z } = mesh.position;
 
    const box = new THREE.Box3().setFromCenterAndSize(
      new THREE.Vector3(x, (y + textHeight) / 2, z),
      new THREE.Vector3(width, textHeight, height)
    );
    controls.fitToBox(box, true);
 
    // annotationRef.current?.show();
  };
 
  // 相机移动回原来的位置
  const handleDoubleClick = () => {
    setClicked(false);
  };
 
  // 监听鼠标移入和移出事件,改变hover状态
  const handlePointerOver = () => setHover(true);
  const handlePointerOut = () => setHover(false);
 
  // 每帧更新边框的颜色和粗细
  useFrame(() => {
    const box = meshRef.current;
    if (box) {
      const color = hovered || clicked ? strokeColor : 'white';
      const thickness = clicked ? 0.5 : 0.2;
      const material = box.material;
      material.color.set(color);
      material.linewidth = thickness;
    }
  });
 
  // const annotationRef = useRef(null);
  const annotationData = [
    {
      label: '长',
      value: width + '米',
    },
    {
      label: '宽',
      value: height + '米',
    },
  ];
 
  return (
    <group
      onClick={handleClick}
      onPointerOver={handlePointerOver}
      onPointerOut={handlePointerOut}
      onDoubleClick={handleDoubleClick}
      onPointerMissed={() => setClicked(false)}
    >
      <mesh ref={meshRef} position={position}>
        <boxGeometry attach="geometry" args={size} />
        <lineSegments>
          <edgesGeometry attach="geometry" args={[new THREE.BoxGeometry(...size)]} />
          <lineBasicMaterial attach="material" color={strokeColor} linewidth={0.2} />
        </lineSegments>
        <meshBasicMaterial attach="material" color={strokeColor} transparent opacity={0.2} />
      </mesh>
 
      {areaNumber && (
        <Text2
          position={new THREE.Vector3(position.x, textHeight, position.z)}
          text={areaNumber}
          scale={new THREE.Vector3(100, 100, 100)}
          fontSize={100}
        />
      )}
      {/* <Annotation
        ref={annotationRef}
        title={areaNumber}
        position={position}
        data={annotationData}
      ></Annotation> */}
    </group>
  );
};
 
export default Area;