import React, { useRef, useState } from 'react';
|
import { useFrame, useThree } from '@react-three/fiber';
|
import * as THREE from 'three';
|
import Text from './text';
|
|
const Y = 1;
|
const Area = (props) => {
|
const { x, y, width, height, textContent, textHeight, strokeColor } = props;
|
const [hovered, setHover] = useState(false);
|
const meshRef = useRef(null);
|
|
const position = new THREE.Vector3(x + width / 2, Y, y + height / 2);
|
const size = [width, 1, height];
|
textHeight ||= Y + 50;
|
|
useFrame(() => {
|
const box = meshRef.current;
|
if (box) {
|
const color = !hovered ? strokeColor : 'white';
|
const material = box.material;
|
material.color.set(color);
|
material.linewidth = 0.2;
|
}
|
});
|
|
return (
|
<group
|
onPointerOver={() => setHover(true)}
|
onPointerOut={() => setHover(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>
|
|
{textContent && (
|
<Text
|
position={new THREE.Vector3(position.x, textHeight, position.z)}
|
text={textContent}
|
/>
|
)}
|
</group>
|
);
|
};
|
|
export default Area;
|