2025-05-29 12:47:24 +00:00
|
|
|
import * as THREE from 'three';
|
|
|
|
import * as Constants from '../../../types/world/worldConstants';
|
|
|
|
import { useRef, useState, useEffect, useMemo } from 'react';
|
2025-05-30 06:04:57 +00:00
|
|
|
import { useDeletePointOrLine, useToolMode } from '../../../store/builder/store';
|
2025-05-30 04:51:45 +00:00
|
|
|
import { DragControls } from '@react-three/drei';
|
|
|
|
import { useAisleStore } from '../../../store/builder/useAisleStore';
|
|
|
|
import { useThree } from '@react-three/fiber';
|
|
|
|
import { useBuilderStore } from '../../../store/builder/useBuilderStore';
|
2025-05-30 10:57:28 +00:00
|
|
|
import { usePointSnapping } from './helpers/usePointSnapping';
|
2025-05-30 11:45:04 +00:00
|
|
|
import { useAislePointSnapping } from './helpers/useAisleDragSnap';
|
2025-06-04 09:49:37 +00:00
|
|
|
import { useWallStore } from '../../../store/builder/useWallStore';
|
2025-05-29 12:47:24 +00:00
|
|
|
|
2025-05-30 09:03:55 +00:00
|
|
|
function Point({ point }: { readonly point: Point }) {
|
2025-05-29 12:47:24 +00:00
|
|
|
const materialRef = useRef<THREE.ShaderMaterial>(null);
|
2025-05-30 06:04:57 +00:00
|
|
|
const { raycaster, camera, pointer } = useThree();
|
2025-05-30 04:51:45 +00:00
|
|
|
const plane = useMemo(() => new THREE.Plane(new THREE.Vector3(0, 1, 0), 0), []);
|
2025-05-29 12:47:24 +00:00
|
|
|
const [isHovered, setIsHovered] = useState(false);
|
|
|
|
const { toolMode } = useToolMode();
|
2025-06-04 09:49:37 +00:00
|
|
|
const { setPosition: setAislePosition, removePoint: removeAislePoint } = useAisleStore();
|
|
|
|
const { setPosition: setWallPosition, removePoint: removeWallPoint } = useWallStore();
|
2025-05-30 11:45:04 +00:00
|
|
|
const { snapPosition } = useAislePointSnapping(point);
|
2025-06-03 07:21:04 +00:00
|
|
|
const { checkSnapForAisle } = usePointSnapping({ uuid: point.pointUuid, pointType: point.pointType, position: point.position });
|
2025-05-30 04:51:45 +00:00
|
|
|
const { hoveredPoint, setHoveredPoint } = useBuilderStore();
|
2025-05-30 06:04:57 +00:00
|
|
|
const { deletePointOrLine } = useDeletePointOrLine();
|
2025-05-29 12:47:24 +00:00
|
|
|
|
|
|
|
const boxScale: [number, number, number] = Constants.pointConfig.boxScale;
|
2025-06-04 04:53:22 +00:00
|
|
|
const colors = getColor(point);
|
|
|
|
|
|
|
|
function getColor(point: Point) {
|
|
|
|
if (point.pointType === 'Aisle') {
|
|
|
|
return {
|
|
|
|
defaultInnerColor: Constants.pointConfig.defaultInnerColor,
|
|
|
|
defaultOuterColor: Constants.pointConfig.aisleOuterColor,
|
|
|
|
defaultDeleteColor: Constants.pointConfig.deleteColor,
|
|
|
|
}
|
|
|
|
} else if (point.pointType === 'Floor') {
|
|
|
|
return {
|
|
|
|
defaultInnerColor: Constants.pointConfig.defaultInnerColor,
|
|
|
|
defaultOuterColor: Constants.pointConfig.floorOuterColor,
|
|
|
|
defaultDeleteColor: Constants.pointConfig.deleteColor,
|
|
|
|
}
|
|
|
|
} else if (point.pointType === 'Wall') {
|
|
|
|
return {
|
|
|
|
defaultInnerColor: Constants.pointConfig.defaultInnerColor,
|
|
|
|
defaultOuterColor: Constants.pointConfig.wallOuterColor,
|
|
|
|
defaultDeleteColor: Constants.pointConfig.deleteColor,
|
|
|
|
}
|
|
|
|
} else if (point.pointType === 'Zone') {
|
|
|
|
return {
|
|
|
|
defaultInnerColor: Constants.pointConfig.defaultInnerColor,
|
|
|
|
defaultOuterColor: Constants.pointConfig.zoneOuterColor,
|
|
|
|
defaultDeleteColor: Constants.pointConfig.deleteColor,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
defaultInnerColor: Constants.pointConfig.defaultInnerColor,
|
|
|
|
defaultOuterColor: Constants.pointConfig.defaultOuterColor,
|
|
|
|
defaultDeleteColor: Constants.pointConfig.deleteColor,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-05-29 12:47:24 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2025-05-30 06:04:57 +00:00
|
|
|
if (materialRef.current && (toolMode === 'move' || deletePointOrLine)) {
|
|
|
|
let innerColor;
|
|
|
|
let outerColor;
|
|
|
|
if (isHovered) {
|
2025-06-04 04:53:22 +00:00
|
|
|
innerColor = deletePointOrLine ? colors.defaultDeleteColor : colors.defaultOuterColor;
|
|
|
|
outerColor = deletePointOrLine ? colors.defaultDeleteColor : colors.defaultOuterColor;
|
2025-05-30 06:04:57 +00:00
|
|
|
} else {
|
2025-06-04 04:53:22 +00:00
|
|
|
innerColor = colors.defaultInnerColor;
|
|
|
|
outerColor = colors.defaultOuterColor;
|
2025-05-30 06:04:57 +00:00
|
|
|
}
|
|
|
|
materialRef.current.uniforms.uInnerColor.value.set(innerColor);
|
|
|
|
materialRef.current.uniforms.uOuterColor.value.set(outerColor);
|
2025-05-29 12:47:24 +00:00
|
|
|
materialRef.current.uniformsNeedUpdate = true;
|
|
|
|
} else if (materialRef.current && toolMode !== 'move') {
|
2025-06-04 04:53:22 +00:00
|
|
|
materialRef.current.uniforms.uInnerColor.value.set(colors.defaultInnerColor);
|
|
|
|
materialRef.current.uniforms.uOuterColor.value.set(colors.defaultOuterColor);
|
2025-05-29 12:47:24 +00:00
|
|
|
materialRef.current.uniformsNeedUpdate = true;
|
|
|
|
}
|
2025-06-04 04:53:22 +00:00
|
|
|
}, [isHovered, colors.defaultInnerColor, colors.defaultOuterColor, colors.defaultDeleteColor, toolMode, deletePointOrLine]);
|
2025-05-29 12:47:24 +00:00
|
|
|
|
|
|
|
const uniforms = useMemo(() => ({
|
2025-06-04 04:53:22 +00:00
|
|
|
uOuterColor: { value: new THREE.Color(colors.defaultOuterColor) },
|
|
|
|
uInnerColor: { value: new THREE.Color(colors.defaultInnerColor) },
|
|
|
|
}), [colors.defaultInnerColor, colors.defaultOuterColor]);
|
2025-05-29 12:47:24 +00:00
|
|
|
|
2025-05-30 04:51:45 +00:00
|
|
|
const handleDrag = (point: Point) => {
|
|
|
|
if (toolMode === 'move' && isHovered) {
|
|
|
|
raycaster.setFromCamera(pointer, camera);
|
|
|
|
const intersectionPoint = new THREE.Vector3();
|
|
|
|
const position = raycaster.ray.intersectPlane(plane, intersectionPoint);
|
2025-05-30 09:03:55 +00:00
|
|
|
if (point.pointType === 'Aisle') {
|
2025-05-30 04:51:45 +00:00
|
|
|
if (position) {
|
2025-05-30 09:03:55 +00:00
|
|
|
const newPosition: [number, number, number] = [position.x, position.y, position.z];
|
2025-05-30 11:45:04 +00:00
|
|
|
const aisleSnappedPosition = snapPosition(newPosition);
|
|
|
|
const finalSnappedPosition = checkSnapForAisle(aisleSnappedPosition.position);
|
2025-05-30 09:03:55 +00:00
|
|
|
|
2025-06-04 09:49:37 +00:00
|
|
|
setAislePosition(point.pointUuid, finalSnappedPosition.position);
|
|
|
|
}
|
|
|
|
} else if (point.pointType === 'Wall') {
|
|
|
|
if (position) {
|
|
|
|
const newPosition: [number, number, number] = [position.x, position.y, position.z];
|
|
|
|
|
|
|
|
setWallPosition(point.pointUuid, newPosition);
|
2025-05-30 04:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleDragEnd = (point: Point) => {
|
2025-05-30 06:04:57 +00:00
|
|
|
if (deletePointOrLine) return;
|
2025-05-30 04:51:45 +00:00
|
|
|
console.log('point: ', point);
|
|
|
|
}
|
|
|
|
|
2025-05-30 06:04:57 +00:00
|
|
|
const handlePointClick = (point: Point) => {
|
|
|
|
if (deletePointOrLine) {
|
2025-06-04 04:53:22 +00:00
|
|
|
if (point.pointType === 'Aisle') {
|
2025-06-04 09:49:37 +00:00
|
|
|
const removedAisles = removeAislePoint(point.pointUuid);
|
2025-06-04 04:53:22 +00:00
|
|
|
if (removedAisles.length > 0) {
|
|
|
|
setHoveredPoint(null);
|
|
|
|
}
|
2025-05-30 06:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-30 04:51:45 +00:00
|
|
|
useEffect(() => {
|
2025-06-03 07:21:04 +00:00
|
|
|
if (hoveredPoint && hoveredPoint.pointUuid !== point.pointUuid) {
|
2025-05-30 04:51:45 +00:00
|
|
|
setIsHovered(false);
|
|
|
|
}
|
|
|
|
}, [hoveredPoint])
|
|
|
|
|
2025-05-29 12:47:24 +00:00
|
|
|
if (!point) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2025-05-30 04:51:45 +00:00
|
|
|
<DragControls
|
|
|
|
axisLock='y'
|
|
|
|
autoTransform={false}
|
|
|
|
onDrag={() => { handleDrag(point) }}
|
|
|
|
onDragEnd={() => { handleDragEnd(point) }}
|
2025-05-29 12:47:24 +00:00
|
|
|
>
|
2025-05-30 04:51:45 +00:00
|
|
|
<mesh
|
2025-06-03 07:21:04 +00:00
|
|
|
key={point.pointUuid}
|
|
|
|
uuid={point.pointUuid}
|
2025-05-30 06:04:57 +00:00
|
|
|
name='Aisle-Point'
|
2025-05-30 04:51:45 +00:00
|
|
|
position={new THREE.Vector3(...point.position)}
|
2025-05-30 06:04:57 +00:00
|
|
|
onClick={() => {
|
|
|
|
handlePointClick(point);
|
|
|
|
}}
|
2025-05-30 04:51:45 +00:00
|
|
|
onPointerOver={() => {
|
|
|
|
if (!hoveredPoint) {
|
|
|
|
setHoveredPoint(point);
|
|
|
|
setIsHovered(true)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onPointerOut={() => {
|
2025-06-03 07:21:04 +00:00
|
|
|
if (hoveredPoint && hoveredPoint.pointUuid === point.pointUuid) {
|
2025-05-30 04:51:45 +00:00
|
|
|
setHoveredPoint(null);
|
|
|
|
}
|
|
|
|
setIsHovered(false)
|
|
|
|
}}
|
2025-05-30 09:03:55 +00:00
|
|
|
userData={point}
|
2025-05-30 04:51:45 +00:00
|
|
|
>
|
|
|
|
<boxGeometry args={boxScale} />
|
|
|
|
<shaderMaterial
|
|
|
|
ref={materialRef}
|
|
|
|
uniforms={uniforms}
|
|
|
|
vertexShader={
|
|
|
|
`
|
|
|
|
varying vec2 vUv;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
vUv = uv;
|
|
|
|
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
|
|
}
|
|
|
|
`
|
2025-05-29 12:47:24 +00:00
|
|
|
}
|
2025-05-30 04:51:45 +00:00
|
|
|
fragmentShader={
|
|
|
|
`
|
|
|
|
varying vec2 vUv;
|
2025-05-30 06:04:57 +00:00
|
|
|
uniform vec3 uOuterColor;
|
2025-05-30 04:51:45 +00:00
|
|
|
uniform vec3 uInnerColor;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
// Define the size of the white square as a proportion of the face
|
|
|
|
float borderThickness = 0.2; // Adjust this value for border thickness
|
|
|
|
if (vUv.x > borderThickness && vUv.x < 1.0 - borderThickness && vUv.y > borderThickness && vUv.y < 1.0 - borderThickness) {
|
|
|
|
gl_FragColor = vec4(uInnerColor, 1.0); // Inner square
|
|
|
|
} else {
|
2025-05-30 06:04:57 +00:00
|
|
|
gl_FragColor = vec4(uOuterColor, 1.0); // Border
|
2025-05-30 04:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2025-05-29 12:47:24 +00:00
|
|
|
}
|
2025-05-30 04:51:45 +00:00
|
|
|
/>
|
|
|
|
</mesh>
|
|
|
|
</DragControls>
|
2025-05-29 12:47:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Point;
|