Merge branch 'main' of http://185.100.212.76:7776/Dwinzo-Beta/Dwinzo_dev
This commit is contained in:
@@ -1,157 +1,68 @@
|
||||
import PolygonGenerator from "./polygonGenerator";
|
||||
import { useThree } from "@react-three/fiber";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import * as THREE from "three";
|
||||
import * as Types from "../../../types/world/worldTypes";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Line } from "@react-three/drei";
|
||||
import { useNavMesh, useSimulationStates } from "../../../store/store";
|
||||
import PathNavigator from "./pathNavigator";
|
||||
import NavMeshDetails from "./navMeshDetails";
|
||||
import {
|
||||
useSelectedActionSphere,
|
||||
useSimulationPaths,
|
||||
} from "../../../store/store";
|
||||
import { usePlayButtonStore } from "../../../store/usePlayButtonStore";
|
||||
|
||||
const Agv = ({
|
||||
lines,
|
||||
plane,
|
||||
}: {
|
||||
lines: Types.RefLines;
|
||||
plane: Types.RefMesh;
|
||||
}) => {
|
||||
const [pathPoints, setPathPoints] = useState<
|
||||
{
|
||||
modelUuid: string;
|
||||
modelSpeed: number;
|
||||
bufferTime: number;
|
||||
points: { x: number; y: number; z: number }[];
|
||||
}[]
|
||||
>([]);
|
||||
const { simulationPaths } = useSimulationPaths();
|
||||
const { selectedActionSphere } = useSelectedActionSphere();
|
||||
useEffect(() => {
|
||||
if (!Array.isArray(simulationPaths)) {
|
||||
} else {
|
||||
let agvModels = simulationPaths.filter(
|
||||
(val: any) => val.modelName === "agv"
|
||||
);
|
||||
type PathPoints = {
|
||||
modelUuid: string;
|
||||
modelSpeed: number;
|
||||
bufferTime: number;
|
||||
points: { x: number; y: number; z: number }[];
|
||||
hitCount: number;
|
||||
};
|
||||
|
||||
console.log("agvModels: ", agvModels);
|
||||
let findMesh = agvModels.filter(
|
||||
(val: any) =>
|
||||
val.modeluuid === selectedActionSphere?.path?.modeluuid &&
|
||||
val.type === "Vehicle"
|
||||
);
|
||||
const Agv = () => {
|
||||
const [pathPoints, setPathPoints] = useState<PathPoints[]>([]);
|
||||
const { simulationStates } = useSimulationStates();
|
||||
const { navMesh } = useNavMesh();
|
||||
const { isPlaying } = usePlayButtonStore();
|
||||
|
||||
const result =
|
||||
findMesh.length > 0 &&
|
||||
findMesh[0].type === "Vehicle" &&
|
||||
typeof findMesh[0].point?.actions.start === "object" &&
|
||||
typeof findMesh[0].point?.actions.end === "object" &&
|
||||
"x" in findMesh[0].point.actions.start &&
|
||||
"y" in findMesh[0].point.actions.start &&
|
||||
"x" in findMesh[0].point.actions.end &&
|
||||
"y" in findMesh[0].point.actions.end
|
||||
? [
|
||||
{
|
||||
modelUuid: findMesh[0].modeluuid, // Ensure it's a number
|
||||
modelSpeed: findMesh[0].point.speed,
|
||||
bufferTime: findMesh[0].point.actions.buffer,
|
||||
points: [
|
||||
{
|
||||
x: findMesh[0].position[0],
|
||||
y: findMesh[0].position[1],
|
||||
z: findMesh[0].position[2],
|
||||
},
|
||||
{
|
||||
x: findMesh[0].point.actions.start.x,
|
||||
y: 0,
|
||||
z: findMesh[0].point.actions.start.y,
|
||||
},
|
||||
{
|
||||
x: findMesh[0].point.actions.end.x,
|
||||
y: 0,
|
||||
z: findMesh[0].point.actions.end.y,
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
: [];
|
||||
if (result.length > 0) {
|
||||
// setPathPoints((prev) => [...prev, ...result]);
|
||||
setPathPoints((prev) => {
|
||||
const existingIndex = prev.findIndex(
|
||||
(item) => item.modelUuid === result[0].modelUuid
|
||||
);
|
||||
useEffect(() => {
|
||||
if (simulationStates.length > 0) {
|
||||
|
||||
if (existingIndex !== -1) {
|
||||
const updatedPathPoints = [...prev];
|
||||
updatedPathPoints[existingIndex] = result[0];
|
||||
return updatedPathPoints;
|
||||
} else {
|
||||
return [...prev, ...result];
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
// setPathPoints((prev) => {
|
||||
// const existingUUIDs = new Set(prev.map((item) => item.uuid));
|
||||
// const newItems = result.filter(
|
||||
// (item) => !existingUUIDs.has(item.uuid)
|
||||
// );
|
||||
// return [...prev, ...newItems];
|
||||
// });
|
||||
}, [simulationPaths, selectedActionSphere]);
|
||||
const agvModels = simulationStates.filter((val) => val.modelName === "agv" && val.type === "Vehicle");
|
||||
|
||||
let groupRef = useRef() as Types.RefGroup;
|
||||
const [navMesh, setNavMesh] = useState();
|
||||
const newPathPoints = agvModels.filter((model: any) => model.points && model.points.actions && typeof model.points.actions.start === "object" && typeof model.points.actions.end === "object" && "x" in model.points.actions.start && "y" in model.points.actions.start && "x" in model.points.actions.end && "y" in model.points.actions.end)
|
||||
.map((model: any) => ({
|
||||
modelUuid: model.modeluuid,
|
||||
modelSpeed: model.points.speed,
|
||||
bufferTime: model.points.actions.buffer,
|
||||
hitCount: model.points.actions.hitCount,
|
||||
points: [
|
||||
{ x: model.position[0], y: model.position[1], z: model.position[2] },
|
||||
{ x: model.points.actions.start.x, y: 0, z: model.points.actions.start.y },
|
||||
{ x: model.points.actions.end.x, y: 0, z: model.points.actions.end.y },
|
||||
],
|
||||
}));
|
||||
|
||||
return (
|
||||
<>
|
||||
<PolygonGenerator groupRef={groupRef} lines={lines} plane={plane} />
|
||||
<NavMeshDetails
|
||||
lines={lines}
|
||||
setNavMesh={setNavMesh}
|
||||
groupRef={groupRef}
|
||||
plane={plane}
|
||||
/>
|
||||
{pathPoints.map((pair, i) => (
|
||||
<>
|
||||
<PathNavigator
|
||||
navMesh={navMesh}
|
||||
selectedPoints={pair.points}
|
||||
id={pair.modelUuid}
|
||||
key={i}
|
||||
speed={pair.modelSpeed}
|
||||
bufferTime={pair.bufferTime}
|
||||
/>
|
||||
{/* {pair.points.length > 2 && (
|
||||
<>
|
||||
<mesh
|
||||
position={[
|
||||
pair.points[1].x,
|
||||
pair.points[1].y,
|
||||
pair.points[1].z,
|
||||
]}
|
||||
>
|
||||
<sphereGeometry args={[0.3, 15, 15]} />
|
||||
<meshStandardMaterial color="red" />
|
||||
</mesh>
|
||||
<mesh
|
||||
position={[
|
||||
pair.points[2].x,
|
||||
pair.points[2].y,
|
||||
pair.points[2].z,
|
||||
]}
|
||||
>
|
||||
<sphereGeometry args={[0.3, 15, 15]} />
|
||||
<meshStandardMaterial color="red" />
|
||||
</mesh>
|
||||
</>
|
||||
)} */}
|
||||
</>
|
||||
))}
|
||||
<group ref={groupRef} visible={false} name="Meshes"></group>
|
||||
</>
|
||||
);
|
||||
setPathPoints(newPathPoints);
|
||||
}
|
||||
}, [simulationStates]);
|
||||
|
||||
return (
|
||||
<>
|
||||
{pathPoints.map((pair, i) => (
|
||||
<group key={i} visible={!isPlaying}>
|
||||
<PathNavigator
|
||||
navMesh={navMesh}
|
||||
pathPoints={pair.points}
|
||||
id={pair.modelUuid}
|
||||
speed={pair.modelSpeed}
|
||||
bufferTime={pair.bufferTime}
|
||||
hitCount={pair.hitCount}
|
||||
/>
|
||||
|
||||
{pair.points.slice(1).map((point, idx) => (
|
||||
<mesh position={[point.x, point.y, point.z]} key={idx}>
|
||||
<sphereGeometry args={[0.3, 15, 15]} />
|
||||
<meshStandardMaterial color="red" />
|
||||
</mesh>
|
||||
))}
|
||||
</group>
|
||||
))}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default Agv;
|
||||
|
||||
31
app/src/modules/builder/agv/navMeshCreator.tsx
Normal file
31
app/src/modules/builder/agv/navMeshCreator.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
import { useRef } from "react";
|
||||
import { useNavMesh } from "../../../store/store";
|
||||
import PolygonGenerator from "./polygonGenerator";
|
||||
import NavMeshDetails from "./navMeshDetails";
|
||||
import * as CONSTANTS from "../../../types/world/worldConstants";
|
||||
import * as Types from "../../../types/world/worldTypes";
|
||||
|
||||
type NavMeshCreatorProps = {
|
||||
lines: Types.RefLines
|
||||
};
|
||||
|
||||
function NavMeshCreator({ lines }: NavMeshCreatorProps) {
|
||||
let groupRef = useRef() as Types.RefGroup;
|
||||
const { setNavMesh } = useNavMesh();
|
||||
|
||||
return (
|
||||
<>
|
||||
<PolygonGenerator groupRef={groupRef} lines={lines} />
|
||||
<NavMeshDetails lines={lines} setNavMesh={setNavMesh} groupRef={groupRef} />
|
||||
|
||||
<group ref={groupRef} visible={false} name="Meshes">
|
||||
<mesh rotation-x={CONSTANTS.planeConfig.rotation} position={CONSTANTS.planeConfig.position3D} name="Plane" receiveShadow>
|
||||
<planeGeometry args={[300, 300]} />
|
||||
<meshBasicMaterial color={CONSTANTS.planeConfig.color} />
|
||||
</mesh>
|
||||
</group>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default NavMeshCreator
|
||||
@@ -10,14 +10,12 @@ interface NavMeshDetailsProps {
|
||||
setNavMesh: (navMesh: any) => void;
|
||||
groupRef: React.MutableRefObject<THREE.Group | null>;
|
||||
lines: Types.RefLines;
|
||||
plane: Types.RefMesh;
|
||||
}
|
||||
|
||||
export default function NavMeshDetails({
|
||||
lines,
|
||||
setNavMesh,
|
||||
groupRef,
|
||||
plane,
|
||||
}: NavMeshDetailsProps) {
|
||||
const { scene } = useThree();
|
||||
|
||||
@@ -34,14 +32,13 @@ export default function NavMeshDetails({
|
||||
|
||||
const [positions, indices] = getPositionsAndIndices(meshes);
|
||||
|
||||
const cs = 0.25;
|
||||
const ch = 0.69;
|
||||
const cellSize = 0.35;
|
||||
const cellHeight = 0.7;
|
||||
const walkableRadius = 0.5;
|
||||
|
||||
const { success, navMesh } = generateSoloNavMesh(positions, indices, {
|
||||
cs,
|
||||
ch,
|
||||
walkableRadius: Math.round(walkableRadius / ch),
|
||||
cs: cellSize,
|
||||
ch: cellHeight,
|
||||
walkableRadius: Math.round(walkableRadius / cellHeight),
|
||||
});
|
||||
|
||||
if (!success || !navMesh) {
|
||||
@@ -50,10 +47,14 @@ export default function NavMeshDetails({
|
||||
|
||||
setNavMesh(navMesh);
|
||||
|
||||
scene.children
|
||||
.filter((child) => child instanceof DebugDrawer)
|
||||
.forEach((child) => scene.remove(child));
|
||||
|
||||
const debugDrawer = new DebugDrawer();
|
||||
debugDrawer.drawNavMesh(navMesh);
|
||||
// scene.add(debugDrawer);
|
||||
} catch (error) {}
|
||||
} catch (error) { }
|
||||
};
|
||||
|
||||
initializeNavigation();
|
||||
|
||||
@@ -3,171 +3,207 @@ import * as THREE from "three";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import { NavMeshQuery } from "@recast-navigation/core";
|
||||
import { Line } from "@react-three/drei";
|
||||
import { useActiveTool } from "../../../store/store";
|
||||
import { usePlayButtonStore } from "../../../store/usePlayButtonStore";
|
||||
|
||||
interface PathNavigatorProps {
|
||||
navMesh: any;
|
||||
selectedPoints: any;
|
||||
id: string;
|
||||
speed: number;
|
||||
bufferTime: number;
|
||||
navMesh: any;
|
||||
pathPoints: any;
|
||||
id: string;
|
||||
speed: number;
|
||||
bufferTime: number;
|
||||
hitCount: number;
|
||||
}
|
||||
|
||||
export default function PathNavigator({
|
||||
navMesh,
|
||||
selectedPoints,
|
||||
id,
|
||||
speed,
|
||||
bufferTime,
|
||||
navMesh,
|
||||
pathPoints,
|
||||
id,
|
||||
speed,
|
||||
bufferTime,
|
||||
hitCount
|
||||
}: PathNavigatorProps) {
|
||||
const [path, setPath] = useState<[number, number, number][]>([]);
|
||||
const progressRef = useRef(0);
|
||||
const distancesRef = useRef<number[]>([]);
|
||||
const totalDistanceRef = useRef(0);
|
||||
const currentSegmentIndex = useRef(0);
|
||||
const [stop, setStop] = useState<boolean>(true);
|
||||
const { scene } = useThree();
|
||||
const { isPlaying, setIsPlaying } = usePlayButtonStore();
|
||||
const [startPoint, setStartPoint] = useState(new THREE.Vector3());
|
||||
const isWaiting = useRef<boolean>(false); // Flag to track waiting state
|
||||
const delayTime = bufferTime;
|
||||
|
||||
const movingForward = useRef<boolean>(true); // Tracks whether the object is moving forward
|
||||
// Compute distances and total distance when the path changes
|
||||
useEffect(() => {
|
||||
if (!scene || !id || path.length < 2) return;
|
||||
const [path, setPath] = useState<[number, number, number][]>([]);
|
||||
const [currentPhase, setCurrentPhase] = useState<'initial' | 'loop'>('initial');
|
||||
const [toPickupPath, setToPickupPath] = useState<[number, number, number][]>([]);
|
||||
const [pickupDropPath, setPickupDropPath] = useState<[number, number, number][]>([]);
|
||||
const [dropPickupPath, setDropPickupPath] = useState<[number, number, number][]>([]);
|
||||
const [initialPosition, setInitialPosition] = useState<THREE.Vector3 | null>(null);
|
||||
const [initialRotation, setInitialRotation] = useState<THREE.Euler | null>(null);
|
||||
|
||||
let totalDistance = 0;
|
||||
const distances: number[] = [];
|
||||
for (let i = 0; i < path.length - 1; i++) {
|
||||
const start = new THREE.Vector3(...path[i]);
|
||||
const end = new THREE.Vector3(...path[i + 1]);
|
||||
const segmentDistance = start.distanceTo(end);
|
||||
distances.push(segmentDistance);
|
||||
totalDistance += segmentDistance;
|
||||
}
|
||||
distancesRef.current = distances;
|
||||
totalDistanceRef.current = totalDistance;
|
||||
progressRef.current = 0;
|
||||
}, [path]);
|
||||
const distancesRef = useRef<number[]>([]);
|
||||
const totalDistanceRef = useRef(0);
|
||||
const progressRef = useRef(0);
|
||||
const isWaiting = useRef(false);
|
||||
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!navMesh || selectedPoints.length === 0) return;
|
||||
const { scene } = useThree();
|
||||
const { isPlaying } = usePlayButtonStore();
|
||||
|
||||
const allPoints = selectedPoints.flat();
|
||||
const computedPath: [number, number, number][] = [];
|
||||
useEffect(() => {
|
||||
const object = scene.getObjectByProperty("uuid", id);
|
||||
if (object) {
|
||||
setInitialPosition(object.position.clone());
|
||||
setInitialRotation(object.rotation.clone());
|
||||
}
|
||||
}, [scene, id]);
|
||||
|
||||
for (let i = 0; i < allPoints.length - 1; i++) {
|
||||
const start = allPoints[i];
|
||||
setStartPoint(
|
||||
new THREE.Vector3(allPoints[0].x, allPoints[0].y, allPoints[0].z)
|
||||
);
|
||||
const computePath = (start: any, end: any) => {
|
||||
try {
|
||||
const navMeshQuery = new NavMeshQuery(navMesh);
|
||||
const { path: segmentPath } = navMeshQuery.computePath(start, end);
|
||||
return segmentPath?.map(({ x, y, z }) => [x, y + 0.1, z] as [number, number, number]) || [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const end = allPoints[i + 1];
|
||||
const resetState = () => {
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
timeoutRef.current = null;
|
||||
}
|
||||
|
||||
try {
|
||||
const navMeshQuery = new NavMeshQuery(navMesh);
|
||||
const { path: segmentPath } = navMeshQuery.computePath(start, end);
|
||||
setPath([]);
|
||||
setCurrentPhase('initial');
|
||||
setPickupDropPath([]);
|
||||
setDropPickupPath([]);
|
||||
distancesRef.current = [];
|
||||
totalDistanceRef.current = 0;
|
||||
progressRef.current = 0;
|
||||
isWaiting.current = false;
|
||||
|
||||
if (!segmentPath || segmentPath.length === 0) {
|
||||
continue;
|
||||
}
|
||||
if (initialPosition && initialRotation) {
|
||||
const object = scene.getObjectByProperty("uuid", id);
|
||||
if (object) {
|
||||
object.position.copy(initialPosition);
|
||||
object.rotation.copy(initialRotation);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
computedPath.push(
|
||||
...segmentPath.map(({ x, y, z }): [number, number, number] => [
|
||||
x,
|
||||
y + 0.1,
|
||||
z,
|
||||
])
|
||||
);
|
||||
} catch (error) {}
|
||||
}
|
||||
useEffect(() => {
|
||||
if (!isPlaying) {
|
||||
resetState();
|
||||
}
|
||||
|
||||
if (computedPath.length > 0) {
|
||||
setPath(computedPath);
|
||||
currentSegmentIndex.current = 0;
|
||||
}
|
||||
}, [selectedPoints, navMesh]);
|
||||
useFrame((_, delta) => {
|
||||
if (!scene || !id || path.length < 2) return;
|
||||
if (!navMesh || pathPoints.length < 2) return;
|
||||
|
||||
// Find the object in the scene by its UUID
|
||||
const findObject = scene.getObjectByProperty("uuid", id);
|
||||
if (!findObject) return;
|
||||
const [pickup, drop] = pathPoints.slice(-2);
|
||||
const object = scene.getObjectByProperty("uuid", id);
|
||||
if (!object) return;
|
||||
|
||||
if (isPlaying) {
|
||||
const fast = speed;
|
||||
progressRef.current += delta * fast;
|
||||
const currentPosition = { x: object.position.x, y: object.position.y, z: object.position.z };
|
||||
|
||||
let coveredDistance = progressRef.current;
|
||||
let accumulatedDistance = 0;
|
||||
let index = 0;
|
||||
const toPickupPath = computePath(currentPosition, pickup);
|
||||
const pickupToDropPath = computePath(pickup, drop);
|
||||
const dropToPickupPath = computePath(drop, pickup);
|
||||
|
||||
// Determine the current segment of the path
|
||||
while (
|
||||
index < distancesRef.current.length &&
|
||||
coveredDistance > accumulatedDistance + distancesRef.current[index]
|
||||
) {
|
||||
accumulatedDistance += distancesRef.current[index];
|
||||
index++;
|
||||
}
|
||||
if (toPickupPath.length && pickupToDropPath.length && dropToPickupPath.length) {
|
||||
setPickupDropPath(pickupToDropPath);
|
||||
setDropPickupPath(dropToPickupPath);
|
||||
setToPickupPath(toPickupPath);
|
||||
setPath(toPickupPath);
|
||||
setCurrentPhase('initial');
|
||||
}
|
||||
}, [navMesh, pathPoints, hitCount, isPlaying]);
|
||||
|
||||
if (index >= distancesRef.current.length) {
|
||||
progressRef.current = totalDistanceRef.current;
|
||||
useEffect(() => {
|
||||
if (path.length < 2) return;
|
||||
|
||||
if (!isWaiting.current) {
|
||||
isWaiting.current = true; // Set waiting flag
|
||||
let total = 0;
|
||||
const segmentDistances = path.slice(0, -1).map((point, i) => {
|
||||
const dist = new THREE.Vector3(...point).distanceTo(new THREE.Vector3(...path[i + 1]));
|
||||
total += dist;
|
||||
return dist;
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
progressRef.current = 0; // Reset progress
|
||||
movingForward.current = !movingForward.current; // Toggle direction
|
||||
distancesRef.current = segmentDistances;
|
||||
totalDistanceRef.current = total;
|
||||
progressRef.current = 0;
|
||||
isWaiting.current = false;
|
||||
}, [path]);
|
||||
|
||||
// Reverse the path and distances arrays
|
||||
path.reverse();
|
||||
distancesRef.current.reverse();
|
||||
useFrame((_, delta) => {
|
||||
if (!isPlaying || path.length < 2 || !scene || !id) return;
|
||||
|
||||
// Reset the waiting flag
|
||||
isWaiting.current = false;
|
||||
}, delayTime * 1000); // Convert seconds to milliseconds
|
||||
}
|
||||
return;
|
||||
}
|
||||
const object = scene.getObjectByProperty("uuid", id);
|
||||
if (!object) return;
|
||||
|
||||
// Interpolate position within the current segment
|
||||
const start = new THREE.Vector3(...path[index]);
|
||||
const end = new THREE.Vector3(...path[index + 1]);
|
||||
const segmentDistance = distancesRef.current[index];
|
||||
const speedFactor = speed;
|
||||
progressRef.current += delta * speedFactor;
|
||||
|
||||
const t = Math.min(
|
||||
(coveredDistance - accumulatedDistance) / segmentDistance,
|
||||
1
|
||||
); // Clamp t to avoid overshooting
|
||||
const position = start.clone().lerp(end, t);
|
||||
findObject.position.copy(position);
|
||||
let covered = progressRef.current;
|
||||
let accumulated = 0;
|
||||
let index = 0;
|
||||
|
||||
// Rotate the object to face the direction of movement
|
||||
const direction = new THREE.Vector3().subVectors(end, start).normalize();
|
||||
const targetYRotation = Math.atan2(direction.x, direction.z);
|
||||
findObject.rotation.y += (targetYRotation - findObject.rotation.y) * 0.1;
|
||||
} else {
|
||||
findObject.position.copy(startPoint);
|
||||
}
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
{path.length > 0 && (
|
||||
<>
|
||||
<Line points={path} color="blue" lineWidth={3} />
|
||||
{selectedPoints.map((val: any, i: any) => (
|
||||
<mesh position={[val[0], val[1] + 1, val[2]]} key={i}>
|
||||
<sphereGeometry args={[50, 5, 5]} />
|
||||
<meshBasicMaterial color={"red"} />
|
||||
</mesh>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
while (
|
||||
index < distancesRef.current.length &&
|
||||
covered > accumulated + distancesRef.current[index]
|
||||
) {
|
||||
accumulated += distancesRef.current[index];
|
||||
index++;
|
||||
}
|
||||
|
||||
if (index >= distancesRef.current.length) {
|
||||
progressRef.current = totalDistanceRef.current;
|
||||
|
||||
if (!isWaiting.current) {
|
||||
isWaiting.current = true;
|
||||
|
||||
timeoutRef.current = setTimeout(() => {
|
||||
if (currentPhase === 'initial') {
|
||||
setPath(pickupDropPath);
|
||||
setCurrentPhase('loop');
|
||||
} else {
|
||||
setPath(prevPath =>
|
||||
prevPath === pickupDropPath ? dropPickupPath : pickupDropPath
|
||||
);
|
||||
}
|
||||
|
||||
progressRef.current = 0;
|
||||
isWaiting.current = false;
|
||||
}, bufferTime * 1000);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const start = new THREE.Vector3(...path[index]);
|
||||
const end = new THREE.Vector3(...path[index + 1]);
|
||||
const dist = distancesRef.current[index];
|
||||
const t = THREE.MathUtils.clamp((covered - accumulated) / dist, 0, 1);
|
||||
const position = start.clone().lerp(end, t);
|
||||
|
||||
object.position.copy(position);
|
||||
|
||||
const direction = new THREE.Vector3().subVectors(end, start).normalize();
|
||||
const targetRotationY = Math.atan2(direction.x, direction.z);
|
||||
|
||||
let angleDifference = targetRotationY - object.rotation.y;
|
||||
angleDifference = ((angleDifference + Math.PI) % (Math.PI * 2)) - Math.PI;
|
||||
object.rotation.y += angleDifference * 0.1;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<group name="path-navigator-lines" visible={!isPlaying} >
|
||||
{toPickupPath.length > 0 && (
|
||||
<Line points={toPickupPath} color="blue" lineWidth={3} dashed dashSize={0.75} dashScale={2} />
|
||||
)}
|
||||
|
||||
{pickupDropPath.length > 0 && (
|
||||
<Line points={pickupDropPath} color="red" lineWidth={3} />
|
||||
)}
|
||||
|
||||
{dropPickupPath.length > 0 && (
|
||||
<Line points={dropPickupPath} color="red" lineWidth={3} />
|
||||
)}
|
||||
</group>
|
||||
);
|
||||
}
|
||||
@@ -6,21 +6,12 @@ import arrayLinesToObject from "../geomentries/lines/lineConvertions/arrayLinesT
|
||||
interface PolygonGeneratorProps {
|
||||
groupRef: React.MutableRefObject<THREE.Group | null>;
|
||||
lines: Types.RefLines;
|
||||
plane: Types.RefMesh;
|
||||
}
|
||||
|
||||
export default function PolygonGenerator({
|
||||
groupRef,
|
||||
lines,
|
||||
plane,
|
||||
}: PolygonGeneratorProps) {
|
||||
// const [rooms, setRooms] = useState<THREE.Vector3[][]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (groupRef.current && plane.current) {
|
||||
groupRef.current.add(plane.current.clone());
|
||||
}
|
||||
}, [groupRef, plane]);
|
||||
|
||||
useEffect(() => {
|
||||
let allLines = arrayLinesToObject(lines.current);
|
||||
@@ -37,13 +28,14 @@ export default function PolygonGenerator({
|
||||
uuid: point.uuid,
|
||||
}))
|
||||
);
|
||||
|
||||
if (!result || result.some((line) => !line)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const lineFeatures = result?.map((line: any) =>
|
||||
turf.lineString(line.map((p: any) => p?.position))
|
||||
);
|
||||
);
|
||||
|
||||
const polygons = turf.polygonize(turf.featureCollection(lineFeatures));
|
||||
renderWallGeometry(wallPoints);
|
||||
@@ -79,8 +71,8 @@ export default function PolygonGenerator({
|
||||
groupRef.current?.add(mesh);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}, [lines.current]);
|
||||
|
||||
const renderWallGeometry = (walls: THREE.Vector3[][]) => {
|
||||
|
||||
@@ -24,7 +24,7 @@ async function addAssetModel(
|
||||
socket: Socket<any>,
|
||||
selectedItem: any,
|
||||
setSelectedItem: any,
|
||||
setSimulationPaths: any,
|
||||
setSimulationStates: any,
|
||||
plane: Types.RefMesh,
|
||||
): Promise<void> {
|
||||
|
||||
@@ -65,7 +65,7 @@ async function addAssetModel(
|
||||
const cachedModel = THREE.Cache.get(selectedItem.id);
|
||||
if (cachedModel) {
|
||||
// console.log(`[Cache] Fetching ${selectedItem.name}`);
|
||||
handleModelLoad(cachedModel, intersectPoint!, selectedItem, itemsGroup, tempLoader, isTempLoader, setFloorItems, setSimulationPaths, socket);
|
||||
handleModelLoad(cachedModel, intersectPoint!, selectedItem, itemsGroup, tempLoader, isTempLoader, setFloorItems, setSimulationStates, socket);
|
||||
return;
|
||||
} else {
|
||||
const cachedModelBlob = await retrieveGLTF(selectedItem.id);
|
||||
@@ -78,7 +78,7 @@ async function addAssetModel(
|
||||
URL.revokeObjectURL(blobUrl);
|
||||
THREE.Cache.remove(blobUrl);
|
||||
THREE.Cache.add(selectedItem.id, gltf);
|
||||
handleModelLoad(gltf, intersectPoint!, selectedItem, itemsGroup, tempLoader, isTempLoader, setFloorItems, setSimulationPaths, socket);
|
||||
handleModelLoad(gltf, intersectPoint!, selectedItem, itemsGroup, tempLoader, isTempLoader, setFloorItems, setSimulationStates, socket);
|
||||
},
|
||||
() => {
|
||||
TempLoader(intersectPoint!, isTempLoader, tempLoader, itemsGroup);
|
||||
@@ -90,7 +90,7 @@ async function addAssetModel(
|
||||
const modelBlob = await fetch(`${url_Backend_dwinzo}/api/v1/AssetFile/${selectedItem.id}`).then((res) => res.blob());
|
||||
await storeGLTF(selectedItem.id, modelBlob);
|
||||
THREE.Cache.add(selectedItem.id, gltf);
|
||||
await handleModelLoad(gltf, intersectPoint!, selectedItem, itemsGroup, tempLoader, isTempLoader, setFloorItems, setSimulationPaths, socket);
|
||||
await handleModelLoad(gltf, intersectPoint!, selectedItem, itemsGroup, tempLoader, isTempLoader, setFloorItems, setSimulationStates, socket);
|
||||
},
|
||||
() => {
|
||||
TempLoader(intersectPoint!, isTempLoader, tempLoader, itemsGroup);
|
||||
@@ -113,7 +113,7 @@ async function handleModelLoad(
|
||||
tempLoader: Types.RefMesh,
|
||||
isTempLoader: Types.RefBoolean,
|
||||
setFloorItems: Types.setFloorItemSetState,
|
||||
setSimulationPaths: any,
|
||||
setSimulationStates: any,
|
||||
socket: Socket<any>
|
||||
) {
|
||||
const model = gltf.scene.clone();
|
||||
@@ -136,7 +136,7 @@ async function handleModelLoad(
|
||||
tempLoader.current = undefined;
|
||||
}
|
||||
|
||||
const newFloorItem: Types.FloorItemType = {
|
||||
const newFloorItem: Types.EventData = {
|
||||
modeluuid: model.uuid,
|
||||
modelname: selectedItem.name,
|
||||
modelfileID: selectedItem.id,
|
||||
@@ -154,7 +154,7 @@ async function handleModelLoad(
|
||||
if (res.type === "Conveyor") {
|
||||
const pointUUIDs = res.points.map(() => THREE.MathUtils.generateUUID());
|
||||
|
||||
const backendEventData: Extract<Types.FloorItemType['eventData'], { type: 'Conveyor' }> = {
|
||||
const backendEventData: Extract<Types.EventData['eventData'], { type: 'Conveyor' }> = {
|
||||
type: 'Conveyor',
|
||||
points: res.points.map((point: any, index: number) => ({
|
||||
uuid: pointUUIDs[index],
|
||||
@@ -167,11 +167,11 @@ async function handleModelLoad(
|
||||
material: 'Inherit',
|
||||
delay: 'Inherit',
|
||||
spawnInterval: 'Inherit',
|
||||
isUsed: false
|
||||
isUsed: true
|
||||
}],
|
||||
triggers: [],
|
||||
connections: {
|
||||
source: { pathUUID: model.uuid, pointUUID: pointUUIDs[index] },
|
||||
source: { modelUUID: model.uuid, pointUUID: pointUUIDs[index] },
|
||||
targets: []
|
||||
}
|
||||
})),
|
||||
@@ -189,7 +189,7 @@ async function handleModelLoad(
|
||||
// { x: model.rotation.x, y: model.rotation.y, z: model.rotation.z },
|
||||
// false,
|
||||
// true,
|
||||
// newFloorItem.eventData
|
||||
// { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed }
|
||||
// );
|
||||
|
||||
// SOCKET
|
||||
@@ -206,8 +206,7 @@ async function handleModelLoad(
|
||||
eventData: backendEventData,
|
||||
socketId: socket.id
|
||||
};
|
||||
|
||||
console.log('data: ', data);
|
||||
|
||||
setFloorItems((prevItems) => {
|
||||
const updatedItems = [...(prevItems || []), newFloorItem];
|
||||
localStorage.setItem("FloorItems", JSON.stringify(updatedItems));
|
||||
@@ -220,9 +219,71 @@ async function handleModelLoad(
|
||||
eventData.position = newFloorItem.position;
|
||||
eventData.rotation = [model.rotation.x, model.rotation.y, model.rotation.z];
|
||||
|
||||
setSimulationPaths((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[]) => [
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => [
|
||||
...(prevEvents || []),
|
||||
eventData as Types.ConveyorEventsSchema | Types.VehicleEventsSchema
|
||||
eventData as Types.ConveyorEventsSchema
|
||||
]);
|
||||
|
||||
socket.emit("v2:model-asset:add", data);
|
||||
|
||||
} else if (res.type === "Vehicle") {
|
||||
|
||||
const pointUUID = THREE.MathUtils.generateUUID();
|
||||
|
||||
const backendEventData: Extract<Types.EventData['eventData'], { type: 'Vehicle' }> = {
|
||||
type: "Vehicle",
|
||||
points: {
|
||||
uuid: pointUUID,
|
||||
position: res.points.position as [number, number, number],
|
||||
actions: { uuid: THREE.MathUtils.generateUUID(), name: 'Action 1', type: '', start: {}, hitCount: 1, end: {}, buffer: 0 },
|
||||
connections: { source: { modelUUID: model.uuid, pointUUID: pointUUID }, targets: [] },
|
||||
speed: 2,
|
||||
}
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
// await setFloorItemApi(
|
||||
// organization,
|
||||
// newFloorItem.modeluuid,
|
||||
// newFloorItem.modelname,
|
||||
// newFloorItem.modelfileID,
|
||||
// newFloorItem.position,
|
||||
// { x: model.rotation.x, y: model.rotation.y, z: model.rotation.z },
|
||||
// false,
|
||||
// true,
|
||||
// { type: backendEventData.type, points: backendEventData.points }
|
||||
// );
|
||||
|
||||
// SOCKET
|
||||
|
||||
const data = {
|
||||
organization,
|
||||
modeluuid: newFloorItem.modeluuid,
|
||||
modelname: newFloorItem.modelname,
|
||||
modelfileID: newFloorItem.modelfileID,
|
||||
position: newFloorItem.position,
|
||||
rotation: { x: model.rotation.x, y: model.rotation.y, z: model.rotation.z },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
eventData: { type: backendEventData.type, points: backendEventData.points },
|
||||
socketId: socket.id
|
||||
};
|
||||
|
||||
const eventData: any = backendEventData;
|
||||
eventData.modeluuid = newFloorItem.modeluuid;
|
||||
eventData.modelName = newFloorItem.modelname;
|
||||
eventData.position = newFloorItem.position;
|
||||
|
||||
setFloorItems((prevItems) => {
|
||||
const updatedItems = [...(prevItems || []), newFloorItem];
|
||||
localStorage.setItem("FloorItems", JSON.stringify(updatedItems));
|
||||
return updatedItems;
|
||||
});
|
||||
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => [
|
||||
...(prevEvents || []),
|
||||
eventData as Types.VehicleEventsSchema
|
||||
]);
|
||||
|
||||
socket.emit("v2:model-asset:add", data);
|
||||
@@ -239,7 +300,7 @@ async function handleModelLoad(
|
||||
// newFloorItem.position,
|
||||
// { x: model.rotation.x, y: model.rotation.y, z: model.rotation.z },
|
||||
// false,
|
||||
// true
|
||||
// true,
|
||||
// );
|
||||
|
||||
// SOCKET
|
||||
@@ -255,7 +316,8 @@ async function handleModelLoad(
|
||||
isVisible: true,
|
||||
socketId: socket.id
|
||||
};
|
||||
|
||||
|
||||
|
||||
setFloorItems((prevItems) => {
|
||||
const updatedItems = [...(prevItems || []), newFloorItem];
|
||||
localStorage.setItem("FloorItems", JSON.stringify(updatedItems));
|
||||
@@ -263,7 +325,6 @@ async function handleModelLoad(
|
||||
});
|
||||
|
||||
socket.emit("v2:model-asset:add", data);
|
||||
|
||||
}
|
||||
|
||||
gsap.to(model.position, { y: newFloorItem.position[1], duration: 1.5, ease: "power2.out" });
|
||||
|
||||
@@ -10,7 +10,7 @@ async function DeleteFloorItems(
|
||||
itemsGroup: Types.RefGroup,
|
||||
hoveredDeletableFloorItem: Types.RefMesh,
|
||||
setFloorItems: Types.setFloorItemSetState,
|
||||
setSimulationPaths: any,
|
||||
setSimulationStates: any,
|
||||
socket: Socket<any>
|
||||
): Promise<void> {
|
||||
|
||||
@@ -76,7 +76,7 @@ async function DeleteFloorItems(
|
||||
}
|
||||
setFloorItems(updatedItems);
|
||||
|
||||
setSimulationPaths((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[]) => {
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => {
|
||||
const updatedEvents = (prevEvents || []).filter(event => event.modeluuid !== removedItem.modeluuid);
|
||||
return updatedEvents;
|
||||
});
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
useRenderDistance,
|
||||
useselectedFloorItem,
|
||||
useSelectedItem,
|
||||
useSimulationPaths,
|
||||
useSimulationStates,
|
||||
useSocketStore,
|
||||
useToggleView,
|
||||
useTransformMode,
|
||||
@@ -65,7 +65,7 @@ const FloorItemsGroup = ({
|
||||
const { setselectedFloorItem } = useselectedFloorItem();
|
||||
const { activeTool } = useActiveTool();
|
||||
const { selectedItem, setSelectedItem } = useSelectedItem();
|
||||
const { simulationPaths, setSimulationPaths } = useSimulationPaths();
|
||||
const { simulationStates, setSimulationStates } = useSimulationStates();
|
||||
const { setLoadingProgress } = useLoadingProgress();
|
||||
const { activeModule } = useModuleStore();
|
||||
const { socket } = useSocketStore();
|
||||
@@ -110,9 +110,8 @@ const FloorItemsGroup = ({
|
||||
}
|
||||
gltfLoaderWorker.postMessage({ floorItems: data });
|
||||
} else {
|
||||
console.log('data: ', data);
|
||||
gltfLoaderWorker.postMessage({ floorItems: [] });
|
||||
loadInitialFloorItems(itemsGroup, setFloorItems, setSimulationPaths);
|
||||
loadInitialFloorItems(itemsGroup, setFloorItems, setSimulationStates);
|
||||
updateLoadingProgress(100);
|
||||
}
|
||||
});
|
||||
@@ -131,7 +130,7 @@ const FloorItemsGroup = ({
|
||||
updateLoadingProgress(progress);
|
||||
|
||||
if (loadedAssets === totalAssets) {
|
||||
loadInitialFloorItems(itemsGroup, setFloorItems, setSimulationPaths);
|
||||
loadInitialFloorItems(itemsGroup, setFloorItems, setSimulationStates);
|
||||
updateLoadingProgress(100);
|
||||
}
|
||||
});
|
||||
@@ -249,7 +248,7 @@ const FloorItemsGroup = ({
|
||||
itemsGroup,
|
||||
hoveredDeletableFloorItem,
|
||||
setFloorItems,
|
||||
setSimulationPaths,
|
||||
setSimulationStates,
|
||||
socket
|
||||
);
|
||||
}
|
||||
@@ -375,7 +374,7 @@ const FloorItemsGroup = ({
|
||||
socket,
|
||||
selectedItem,
|
||||
setSelectedItem,
|
||||
setSimulationPaths,
|
||||
setSimulationStates,
|
||||
plane
|
||||
);
|
||||
}
|
||||
|
||||
@@ -12,203 +12,189 @@ import CollabUserIcon from "./collabUserIcon";
|
||||
import { getAvatarColor } from "./users/functions/getAvatarColor";
|
||||
|
||||
const CamModelsGroup = () => {
|
||||
let navigate = useNavigate();
|
||||
const groupRef = useRef<THREE.Group>(null);
|
||||
const email = localStorage.getItem("email");
|
||||
const { activeUsers, setActiveUsers } = useActiveUsers();
|
||||
const { socket } = useSocketStore();
|
||||
const loader = new GLTFLoader();
|
||||
const dracoLoader = new DRACOLoader();
|
||||
const [cams, setCams] = useState<any[]>([]);
|
||||
const [models, setModels] = useState<Record<string, { targetPosition: THREE.Vector3; targetRotation: THREE.Euler }>>({});
|
||||
const navigate = useNavigate();
|
||||
const groupRef = useRef<THREE.Group>(null);
|
||||
const email = localStorage.getItem("email");
|
||||
const { activeUsers, setActiveUsers } = useActiveUsers();
|
||||
const { socket } = useSocketStore();
|
||||
|
||||
dracoLoader.setDecoderPath("three/examples/jsm/libs/draco/gltf/");
|
||||
loader.setDRACOLoader(dracoLoader);
|
||||
const loader = new GLTFLoader();
|
||||
const dracoLoader = new DRACOLoader();
|
||||
dracoLoader.setDecoderPath("three/examples/jsm/libs/draco/gltf/");
|
||||
loader.setDRACOLoader(dracoLoader);
|
||||
|
||||
useEffect(() => {
|
||||
if (!email) {
|
||||
navigate("/");
|
||||
}
|
||||
if (!socket) return;
|
||||
const organization = email!.split("@")[1].split(".")[0];
|
||||
const [cams, setCams] = useState<any[]>([]);
|
||||
const [models, setModels] = useState<Record<string, { targetPosition: THREE.Vector3; targetRotation: THREE.Euler }>>({});
|
||||
|
||||
socket.on("userConnectResponse", (data: any) => {
|
||||
if (!groupRef.current) return;
|
||||
if (data.data.userData.email === email) return;
|
||||
if (socket.id === data.socketId || organization !== data.organization)
|
||||
return;
|
||||
const dedupeCams = (cams: any[]) => {
|
||||
const seen = new Set();
|
||||
return cams.filter((cam) => {
|
||||
if (seen.has(cam.uuid)) return false;
|
||||
seen.add(cam.uuid);
|
||||
return true;
|
||||
});
|
||||
};
|
||||
|
||||
const model = groupRef.current.getObjectByProperty(
|
||||
"uuid",
|
||||
data.data.userData._id
|
||||
);
|
||||
if (model) {
|
||||
groupRef.current.remove(model);
|
||||
}
|
||||
loader.load(camModel, (gltf) => {
|
||||
const newModel = gltf.scene.clone();
|
||||
newModel.uuid = data.data.userData._id;
|
||||
newModel.position.set(
|
||||
data.data.position.x,
|
||||
data.data.position.y,
|
||||
data.data.position.z
|
||||
);
|
||||
newModel.rotation.set(
|
||||
data.data.rotation.x,
|
||||
data.data.rotation.y,
|
||||
data.data.rotation.z
|
||||
);
|
||||
newModel.userData = data.data.userData;
|
||||
setCams((prev) => [...prev, newModel]);
|
||||
setActiveUsers([...activeUsers, data.data.userData]);
|
||||
});
|
||||
});
|
||||
const dedupeUsers = (users: any[]) => {
|
||||
const seen = new Set();
|
||||
return users.filter((user) => {
|
||||
if (seen.has(user._id)) return false;
|
||||
seen.add(user._id);
|
||||
return true;
|
||||
});
|
||||
};
|
||||
|
||||
// socket.on("users:online", (data: any) => {
|
||||
// console.log('users online: ', data);
|
||||
// })
|
||||
useEffect(() => {
|
||||
if (!email) navigate("/");
|
||||
|
||||
socket.on("userDisConnectResponse", (data: any) => {
|
||||
if (!groupRef.current) return;
|
||||
if (socket.id === data.socketId || organization !== data.organization)
|
||||
return;
|
||||
if (!socket) return;
|
||||
const organization = email!.split("@")[1].split(".")[0];
|
||||
|
||||
setCams((prev) =>
|
||||
prev.filter((cam) => cam.uuid !== data.data.userData._id)
|
||||
);
|
||||
setActiveUsers(
|
||||
activeUsers.filter((user: any) => user._id !== data.data.userData._id)
|
||||
);
|
||||
});
|
||||
socket.on("userConnectResponse", (data: any) => {
|
||||
if (!groupRef.current) return;
|
||||
if (data.data.userData.email === email) return;
|
||||
if (socket.id === data.socketId || organization !== data.organization) return;
|
||||
|
||||
socket.on("cameraUpdateResponse", (data: any) => {
|
||||
if (
|
||||
!groupRef.current ||
|
||||
socket.id === data.socketId ||
|
||||
organization !== data.organization
|
||||
)
|
||||
return;
|
||||
const model = groupRef.current.getObjectByProperty("uuid", data.data.userData._id);
|
||||
if (model) {
|
||||
groupRef.current.remove(model);
|
||||
}
|
||||
|
||||
setModels((prev) => ({
|
||||
...prev,
|
||||
[data.data.userId]: {
|
||||
targetPosition: new THREE.Vector3(
|
||||
data.data.position.x,
|
||||
data.data.position.y,
|
||||
data.data.position.z
|
||||
),
|
||||
targetRotation: new THREE.Euler(
|
||||
data.data.rotation.x,
|
||||
data.data.rotation.y,
|
||||
data.data.rotation.z
|
||||
),
|
||||
},
|
||||
}));
|
||||
});
|
||||
loader.load(camModel, (gltf) => {
|
||||
const newModel = gltf.scene.clone();
|
||||
newModel.uuid = data.data.userData._id;
|
||||
newModel.position.set(
|
||||
data.data.position.x,
|
||||
data.data.position.y,
|
||||
data.data.position.z
|
||||
);
|
||||
newModel.rotation.set(
|
||||
data.data.rotation.x,
|
||||
data.data.rotation.y,
|
||||
data.data.rotation.z
|
||||
);
|
||||
newModel.userData = data.data.userData;
|
||||
|
||||
return () => {
|
||||
socket.off("userConnectRespones");
|
||||
socket.off("userDisConnectRespones");
|
||||
socket.off("cameraUpdateResponse");
|
||||
};
|
||||
}, [socket, activeUsers]);
|
||||
setCams((prev) => dedupeCams([...prev, newModel]));
|
||||
setActiveUsers((prev: any) =>
|
||||
dedupeUsers([...prev, data.data.userData])
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on("userDisConnectResponse", (data: any) => {
|
||||
if (!groupRef.current) return;
|
||||
if (socket.id === data.socketId || organization !== data.organization) return;
|
||||
|
||||
// useEffect(() => {
|
||||
// console.log(activeUsers);
|
||||
// }, [activeUsers])
|
||||
setCams((prev) =>
|
||||
prev.filter((cam) => cam.uuid !== data.data.userData._id)
|
||||
);
|
||||
setActiveUsers((prev: any) =>
|
||||
prev.filter((user: any) => user._id !== data.data.userData._id)
|
||||
);
|
||||
});
|
||||
|
||||
// useEffect(() => {
|
||||
// console.log(models);
|
||||
// }, [models])
|
||||
socket.on("cameraUpdateResponse", (data: any) => {
|
||||
if (
|
||||
!groupRef.current ||
|
||||
socket.id === data.socketId ||
|
||||
organization !== data.organization
|
||||
)
|
||||
return;
|
||||
|
||||
useFrame(() => {
|
||||
if (!groupRef.current) return;
|
||||
Object.keys(models).forEach((uuid) => {
|
||||
const model = groupRef.current!.getObjectByProperty("uuid", uuid);
|
||||
if (!model) return;
|
||||
setModels((prev) => ({
|
||||
...prev,
|
||||
[data.data.userId]: {
|
||||
targetPosition: new THREE.Vector3(
|
||||
data.data.position.x,
|
||||
data.data.position.y,
|
||||
data.data.position.z
|
||||
),
|
||||
targetRotation: new THREE.Euler(
|
||||
data.data.rotation.x,
|
||||
data.data.rotation.y,
|
||||
data.data.rotation.z
|
||||
),
|
||||
},
|
||||
}));
|
||||
});
|
||||
|
||||
const { targetPosition, targetRotation } = models[uuid];
|
||||
model.position.lerp(targetPosition, 0.1);
|
||||
model.rotation.x = THREE.MathUtils.lerp(
|
||||
model.rotation.x,
|
||||
targetRotation.x,
|
||||
0.1
|
||||
);
|
||||
model.rotation.y = THREE.MathUtils.lerp(
|
||||
model.rotation.y,
|
||||
targetRotation.y,
|
||||
0.1
|
||||
);
|
||||
model.rotation.z = THREE.MathUtils.lerp(
|
||||
model.rotation.z,
|
||||
targetRotation.z,
|
||||
0.1
|
||||
);
|
||||
});
|
||||
});
|
||||
return () => {
|
||||
socket.off("userConnectResponse");
|
||||
socket.off("userDisConnectResponse");
|
||||
socket.off("cameraUpdateResponse");
|
||||
};
|
||||
}, [socket]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!groupRef.current) return;
|
||||
const organization = email!.split("@")[1].split(".")[0];
|
||||
getActiveUsersData(organization).then((data) => {
|
||||
const filteredData = data.cameraDatas.filter(
|
||||
(camera: any) => camera.userData.email !== email
|
||||
);
|
||||
let a: any = [];
|
||||
if (filteredData.length > 0) {
|
||||
loader.load(camModel, (gltf) => {
|
||||
const newCams = filteredData.map((cam: any) => {
|
||||
const newModel = gltf.scene.clone();
|
||||
newModel.uuid = cam.userData._id;
|
||||
newModel.position.set(
|
||||
cam.position.x,
|
||||
cam.position.y,
|
||||
cam.position.z
|
||||
);
|
||||
newModel.rotation.set(
|
||||
cam.rotation.x,
|
||||
cam.rotation.y,
|
||||
cam.rotation.z
|
||||
);
|
||||
newModel.userData = cam.userData;
|
||||
a.push(cam.userData);
|
||||
return newModel;
|
||||
});
|
||||
setActiveUsers(a);
|
||||
setCams((prev) => [...prev, ...newCams]);
|
||||
});
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
useFrame(() => {
|
||||
if (!groupRef.current) return;
|
||||
Object.keys(models).forEach((uuid) => {
|
||||
const model = groupRef.current!.getObjectByProperty("uuid", uuid);
|
||||
if (!model) return;
|
||||
|
||||
return (
|
||||
<group ref={groupRef} name="Cam-Model-Group">
|
||||
{cams.map((cam, index) => (
|
||||
<primitive key={index} object={cam}>
|
||||
<Html
|
||||
as="div"
|
||||
center
|
||||
zIndexRange={[1, 0]}
|
||||
sprite
|
||||
style={{
|
||||
color: "white",
|
||||
textAlign: "center",
|
||||
fontFamily: "Arial, sans-serif",
|
||||
}}
|
||||
position={[-0.015, 0, 0.7]}
|
||||
>
|
||||
<CollabUserIcon
|
||||
userImage={""}
|
||||
userName={cam.userData.userName}
|
||||
index={index}
|
||||
color={getAvatarColor(index)}
|
||||
/>
|
||||
</Html>
|
||||
</primitive>
|
||||
))}
|
||||
</group>
|
||||
);
|
||||
const { targetPosition, targetRotation } = models[uuid];
|
||||
model.position.lerp(targetPosition, 0.1);
|
||||
model.rotation.x = THREE.MathUtils.lerp(model.rotation.x, targetRotation.x, 0.1);
|
||||
model.rotation.y = THREE.MathUtils.lerp(model.rotation.y, targetRotation.y, 0.1);
|
||||
model.rotation.z = THREE.MathUtils.lerp(model.rotation.z, targetRotation.z, 0.1);
|
||||
});
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!groupRef.current) return;
|
||||
const organization = email!.split("@")[1].split(".")[0];
|
||||
|
||||
getActiveUsersData(organization).then((data) => {
|
||||
const filteredData = data.cameraDatas.filter(
|
||||
(camera: any) => camera.userData.email !== email
|
||||
);
|
||||
|
||||
if (filteredData.length > 0) {
|
||||
loader.load(camModel, (gltf) => {
|
||||
const newCams = filteredData.map((cam: any) => {
|
||||
const newModel = gltf.scene.clone();
|
||||
newModel.uuid = cam.userData._id;
|
||||
newModel.position.set(cam.position.x, cam.position.y, cam.position.z);
|
||||
newModel.rotation.set(cam.rotation.x, cam.rotation.y, cam.rotation.z);
|
||||
newModel.userData = cam.userData;
|
||||
return newModel;
|
||||
});
|
||||
|
||||
const users = filteredData.map((cam: any) => cam.userData);
|
||||
setActiveUsers((prev: any) => dedupeUsers([...prev, ...users]));
|
||||
setCams((prev) => dedupeCams([...prev, ...newCams]));
|
||||
});
|
||||
}
|
||||
});
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<group ref={groupRef} name="Cam-Model-Group">
|
||||
{cams.map((cam, index) => (
|
||||
<primitive key={cam.uuid} object={cam}>
|
||||
<Html
|
||||
as="div"
|
||||
center
|
||||
zIndexRange={[1, 0]}
|
||||
sprite
|
||||
style={{
|
||||
color: "white",
|
||||
textAlign: "center",
|
||||
fontFamily: "Arial, sans-serif",
|
||||
}}
|
||||
position={[-0.015, 0, 0.7]}
|
||||
>
|
||||
<CollabUserIcon
|
||||
userImage={""}
|
||||
userName={cam.userData.userName}
|
||||
index={index}
|
||||
color={getAvatarColor(index)}
|
||||
/>
|
||||
</Html>
|
||||
</primitive>
|
||||
))}
|
||||
</group>
|
||||
);
|
||||
};
|
||||
|
||||
export default CamModelsGroup;
|
||||
|
||||
@@ -20,7 +20,24 @@ const CollabUserIcon: React.FC<CollabUserIconProps> = ({
|
||||
{userImage ? (
|
||||
<img className="user-image" src={userImage} alt={userName} />
|
||||
) : (
|
||||
<CustomAvatar name={userName} index={index} />
|
||||
<CustomAvatar name={userName} index={index} color={color} />
|
||||
// <div
|
||||
// className="user-image"
|
||||
// style={{
|
||||
// lineHeight: "30px",
|
||||
// textTransform: "uppercase",
|
||||
// textAlign: "center",
|
||||
// fontSize: "16px",
|
||||
// borderRadius: "50%",
|
||||
// backgroundColor: color,
|
||||
// overflow: "hidden",
|
||||
// backgroundSize: "cover",
|
||||
// backgroundPosition: "center",
|
||||
// color: "white",
|
||||
// fontWeight: "bold",
|
||||
// }}>
|
||||
// {userName[0]}
|
||||
// </div>
|
||||
)}
|
||||
</div>
|
||||
<div className="user-name" style={{ backgroundColor: color }}>
|
||||
|
||||
@@ -143,10 +143,6 @@ export default function SocketResponses({
|
||||
isVisible: data.data.isVisible,
|
||||
};
|
||||
|
||||
if (data.data.eventData) {
|
||||
newFloorItem.eventData = data.data.eventData;
|
||||
}
|
||||
|
||||
setFloorItems((prevItems: any) => {
|
||||
const updatedItems = [...(prevItems || []), newFloorItem];
|
||||
localStorage.setItem("FloorItems", JSON.stringify(updatedItems));
|
||||
@@ -221,10 +217,6 @@ export default function SocketResponses({
|
||||
isVisible: data.data.isVisible,
|
||||
};
|
||||
|
||||
if (data.data.eventData) {
|
||||
newFloorItem.eventData = data.data.eventData;
|
||||
}
|
||||
|
||||
setFloorItems((prevItems: any) => {
|
||||
const updatedItems = [...(prevItems || []), newFloorItem];
|
||||
localStorage.setItem("FloorItems", JSON.stringify(updatedItems));
|
||||
@@ -241,7 +233,7 @@ export default function SocketResponses({
|
||||
}
|
||||
|
||||
} else if (data.message === "Model updated successfully") {
|
||||
itemsGroup.current.children.forEach((item: THREE.Group) => {
|
||||
itemsGroup.current?.children.forEach((item: THREE.Group) => {
|
||||
if (item.uuid === data.data.modeluuid) {
|
||||
item.position.set(...data.data.position as [number, number, number]);
|
||||
item.rotation.set(data.data.rotation.x, data.data.rotation.y, data.data.rotation.z);
|
||||
|
||||
@@ -7,6 +7,7 @@ interface AvatarProps {
|
||||
size?: number;
|
||||
index?: number;
|
||||
textColor?: string;
|
||||
color?: string; // Optional color prop for future use
|
||||
}
|
||||
|
||||
const CustomAvatar: React.FC<AvatarProps> = ({
|
||||
@@ -14,6 +15,7 @@ const CustomAvatar: React.FC<AvatarProps> = ({
|
||||
size = 100,
|
||||
index = 0,
|
||||
textColor = "#ffffff",
|
||||
color, // Optional color prop for future use
|
||||
}) => {
|
||||
const [imageSrc, setImageSrc] = useState<string | null>(null);
|
||||
|
||||
@@ -26,7 +28,7 @@ const CustomAvatar: React.FC<AvatarProps> = ({
|
||||
const initials = getInitials(name); // Convert name to initials if needed
|
||||
|
||||
// Draw background
|
||||
ctx.fillStyle = getAvatarColor(index);
|
||||
ctx.fillStyle = color || getAvatarColor(index); // Use color prop or generate color based on index
|
||||
ctx.fillRect(0, 0, size, size);
|
||||
|
||||
// Draw initials
|
||||
@@ -40,7 +42,7 @@ const CustomAvatar: React.FC<AvatarProps> = ({
|
||||
const dataURL = canvas.toDataURL("image/png");
|
||||
setImageSrc(dataURL);
|
||||
}
|
||||
}, [name, size, textColor]);
|
||||
}, [name, size, textColor, index]);
|
||||
|
||||
if (!imageSrc) {
|
||||
return null; // Return null while the image is being generated
|
||||
@@ -53,6 +55,18 @@ const CustomAvatar: React.FC<AvatarProps> = ({
|
||||
alt="User Avatar"
|
||||
style={{ width: "100%", height: "100%" }}
|
||||
/>
|
||||
// <div
|
||||
// className="user-image"
|
||||
// style={{
|
||||
// width: size,
|
||||
// height: size,
|
||||
// borderRadius: "50%",
|
||||
// overflow: "hidden",
|
||||
// backgroundSize: "cover",
|
||||
// backgroundPosition: "center",
|
||||
// }}>
|
||||
// {name[0]}
|
||||
// </div>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import { getFloorAssets } from '../../../services/factoryBuilder/assest/floorAss
|
||||
async function loadInitialFloorItems(
|
||||
itemsGroup: Types.RefGroup,
|
||||
setFloorItems: Types.setFloorItemSetState,
|
||||
setSimulationPaths: (paths: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[]) => void
|
||||
setSimulationStates: (paths: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => void
|
||||
): Promise<void> {
|
||||
if (!itemsGroup.current) return;
|
||||
let url_Backend_dwinzo = `http://${process.env.REACT_APP_SERVER_MARKETPLACE_URL}`;
|
||||
@@ -23,10 +23,10 @@ async function loadInitialFloorItems(
|
||||
localStorage.setItem("FloorItems", JSON.stringify(items));
|
||||
await initializeDB();
|
||||
|
||||
if (items.message === "floorItems not found") return;
|
||||
if (items.message === "floorItems not found") return;
|
||||
|
||||
if (items) {
|
||||
const storedFloorItems: Types.FloorItems = items;
|
||||
const storedFloorItems: Types.EventData[] = items;
|
||||
const loader = new GLTFLoader();
|
||||
const dracoLoader = new DRACOLoader();
|
||||
|
||||
@@ -53,7 +53,6 @@ async function loadInitialFloorItems(
|
||||
});
|
||||
|
||||
for (const item of storedFloorItems) {
|
||||
console.log('item: ', item);
|
||||
if (!item.modelfileID) return;
|
||||
const itemPosition = new THREE.Vector3(item.position[0], item.position[1], item.position[2]);
|
||||
let storedPosition;
|
||||
@@ -72,7 +71,7 @@ async function loadInitialFloorItems(
|
||||
const cachedModel = THREE.Cache.get(item.modelfileID!);
|
||||
if (cachedModel) {
|
||||
// console.log(`[Cache] Fetching ${item.modelname}`);
|
||||
processLoadedModel(cachedModel.scene.clone(), item, itemsGroup, setFloorItems, setSimulationPaths);
|
||||
processLoadedModel(cachedModel.scene.clone(), item, itemsGroup, setFloorItems, setSimulationStates);
|
||||
modelsLoaded++;
|
||||
checkLoadingCompletion(modelsLoaded, modelsToLoad, dracoLoader, resolve);
|
||||
return;
|
||||
@@ -89,7 +88,7 @@ async function loadInitialFloorItems(
|
||||
URL.revokeObjectURL(blobUrl);
|
||||
THREE.Cache.remove(blobUrl);
|
||||
THREE.Cache.add(item.modelfileID!, gltf);
|
||||
processLoadedModel(gltf.scene.clone(), item, itemsGroup, setFloorItems, setSimulationPaths);
|
||||
processLoadedModel(gltf.scene.clone(), item, itemsGroup, setFloorItems, setSimulationStates);
|
||||
modelsLoaded++;
|
||||
checkLoadingCompletion(modelsLoaded, modelsToLoad, dracoLoader, resolve);
|
||||
},
|
||||
@@ -112,7 +111,7 @@ async function loadInitialFloorItems(
|
||||
const modelBlob = await fetch(modelUrl).then((res) => res.blob());
|
||||
await storeGLTF(item.modelfileID!, modelBlob);
|
||||
THREE.Cache.add(item.modelfileID!, gltf);
|
||||
processLoadedModel(gltf.scene.clone(), item, itemsGroup, setFloorItems, setSimulationPaths);
|
||||
processLoadedModel(gltf.scene.clone(), item, itemsGroup, setFloorItems, setSimulationStates);
|
||||
modelsLoaded++;
|
||||
checkLoadingCompletion(modelsLoaded, modelsToLoad, dracoLoader, resolve);
|
||||
},
|
||||
@@ -138,8 +137,8 @@ async function loadInitialFloorItems(
|
||||
},
|
||||
]);
|
||||
|
||||
if (item.eventData) {
|
||||
processEventData(item, setSimulationPaths);
|
||||
if (item.eventData || item.modelfileID === '67e3db95c2e8f37134526fb2') {
|
||||
processEventData(item, setSimulationStates);
|
||||
}
|
||||
|
||||
modelsLoaded++;
|
||||
@@ -155,10 +154,10 @@ async function loadInitialFloorItems(
|
||||
|
||||
function processLoadedModel(
|
||||
gltf: any,
|
||||
item: Types.FloorItemType,
|
||||
item: Types.EventData,
|
||||
itemsGroup: Types.RefGroup,
|
||||
setFloorItems: Types.setFloorItemSetState,
|
||||
setSimulationPaths: (paths: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[]) => void
|
||||
setSimulationStates: (paths: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => void
|
||||
) {
|
||||
const model = gltf;
|
||||
model.uuid = item.modeluuid;
|
||||
@@ -193,15 +192,15 @@ function processLoadedModel(
|
||||
},
|
||||
]);
|
||||
|
||||
if (item.eventData || item.modelfileID === '67e3da19c2e8f37134526e6a') {
|
||||
processEventData(item, setSimulationPaths);
|
||||
if (item.eventData || item.modelfileID === '67e3db95c2e8f37134526fb2') {
|
||||
processEventData(item, setSimulationStates);
|
||||
}
|
||||
|
||||
gsap.to(model.position, { y: item.position[1], duration: 1.5, ease: 'power2.out' });
|
||||
gsap.to(model.scale, { x: 1, y: 1, z: 1, duration: 1.5, ease: 'power2.out' });
|
||||
}
|
||||
|
||||
function processEventData(item: Types.FloorItemType, setSimulationPaths: any) {
|
||||
function processEventData(item: Types.EventData, setSimulationStates: any) {
|
||||
|
||||
if (item.eventData?.type === 'Conveyor') {
|
||||
|
||||
@@ -211,32 +210,45 @@ function processEventData(item: Types.FloorItemType, setSimulationPaths: any) {
|
||||
data.position = item.position;
|
||||
data.rotation = [item.rotation.x, item.rotation.y, item.rotation.z];
|
||||
|
||||
setSimulationPaths((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[]) => [
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => [
|
||||
...(prevEvents || []),
|
||||
data as Types.ConveyorEventsSchema
|
||||
]);
|
||||
} else {
|
||||
|
||||
} else if (item.eventData?.type === 'Vehicle') {
|
||||
|
||||
const data: any = item.eventData;
|
||||
data.modeluuid = item.modeluuid;
|
||||
data.modelName = item.modelname;
|
||||
data.position = item.position;
|
||||
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => [
|
||||
...(prevEvents || []),
|
||||
data as Types.VehicleEventsSchema
|
||||
]);
|
||||
|
||||
} else if (item.modelfileID === '67e3db95c2e8f37134526fb2') {
|
||||
|
||||
const pointUUID = THREE.MathUtils.generateUUID();
|
||||
const pointPosition = new THREE.Vector3(0, 1.3, 0);
|
||||
const pointPosition = new THREE.Vector3(0, 1.75, 0);
|
||||
|
||||
const newVehiclePath: Types.VehicleEventsSchema = {
|
||||
const staticMachine: Types.StaticMachineEventsSchema = {
|
||||
modeluuid: item.modeluuid,
|
||||
modelName: item.modelname,
|
||||
type: 'Vehicle',
|
||||
point: {
|
||||
type: "StaticMachine",
|
||||
points: {
|
||||
uuid: pointUUID,
|
||||
position: [pointPosition.x, pointPosition.y, pointPosition.z],
|
||||
actions: { uuid: THREE.MathUtils.generateUUID(), name: 'Action 1', type: 'Start', start: {}, hitCount: 1, end: {}, buffer: 0 },
|
||||
connections: { source: { pathUUID: item.modeluuid, pointUUID: pointUUID }, targets: [] },
|
||||
speed: 2,
|
||||
actions: { uuid: THREE.MathUtils.generateUUID(), name: 'Action 1', buffer: 'Inherit', material: 'Inherit', isUsed: false },
|
||||
triggers: { uuid: THREE.MathUtils.generateUUID(), name: 'Trigger 1', type: 'OnComplete' },
|
||||
connections: { source: { modelUUID: item.modeluuid, pointUUID: pointUUID }, targets: [] },
|
||||
},
|
||||
position: [...item.position],
|
||||
position: item.position
|
||||
};
|
||||
|
||||
setSimulationPaths((prevEvents: (Types.VehicleEventsSchema)[]) => [
|
||||
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => [
|
||||
...(prevEvents || []),
|
||||
newVehiclePath as Types.VehicleEventsSchema
|
||||
staticMachine as Types.StaticMachineEventsSchema
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as THREE from "three";
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import { useFloorItems, useSelectedAssets, useSimulationPaths, useSocketStore, useToggleView } from "../../../../store/store";
|
||||
import { useFloorItems, useSelectedAssets, useSimulationStates, useSocketStore, useToggleView } from "../../../../store/store";
|
||||
import { toast } from "react-toastify";
|
||||
// import { setFloorItemApi } from '../../../../services/factoryBuilder/assest/floorAsset/setFloorItemApi';
|
||||
import * as Types from "../../../../types/world/worldTypes";
|
||||
@@ -10,7 +10,7 @@ const CopyPasteControls = ({ itemsGroupRef, copiedObjects, setCopiedObjects, pas
|
||||
const { camera, controls, gl, scene, pointer, raycaster } = useThree();
|
||||
const { toggleView } = useToggleView();
|
||||
const { selectedAssets, setSelectedAssets } = useSelectedAssets();
|
||||
const { simulationPaths, setSimulationPaths } = useSimulationPaths();
|
||||
const { simulationStates, setSimulationStates } = useSimulationStates();
|
||||
const plane = useMemo(() => new THREE.Plane(new THREE.Vector3(0, 1, 0), 0), []);
|
||||
const { floorItems, setFloorItems } = useFloorItems();
|
||||
const { socket } = useSocketStore()
|
||||
@@ -151,7 +151,7 @@ const CopyPasteControls = ({ itemsGroupRef, copiedObjects, setCopiedObjects, pas
|
||||
return updatedItems;
|
||||
});
|
||||
|
||||
let eventData: Types.ConveyorEventsSchema | Types.VehicleEventsSchema | undefined = simulationPaths.find((events) => events.modeluuid === obj.userData.modeluuid);
|
||||
let eventData: Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema | undefined = simulationStates.find((events) => events.modeluuid === obj.userData.modeluuid);
|
||||
|
||||
const email = localStorage.getItem("email");
|
||||
const organization = email ? email.split("@")[1].split(".")[0] : "default";
|
||||
@@ -183,7 +183,7 @@ const CopyPasteControls = ({ itemsGroupRef, copiedObjects, setCopiedObjects, pas
|
||||
: [defaultAction],
|
||||
triggers: (eventData as Types.ConveyorEventsSchema)?.points[index].triggers,
|
||||
connections: {
|
||||
source: { pathUUID: obj.uuid, pointUUID },
|
||||
source: { modelUUID: obj.uuid, pointUUID },
|
||||
targets: []
|
||||
}
|
||||
};
|
||||
@@ -224,22 +224,102 @@ const CopyPasteControls = ({ itemsGroupRef, copiedObjects, setCopiedObjects, pas
|
||||
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
eventData: backendEventData,
|
||||
eventData: { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed },
|
||||
socketId: socket.id,
|
||||
};
|
||||
|
||||
const newEventData: any = backendEventData;
|
||||
const newEventData: any = { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed };
|
||||
newEventData.modeluuid = newFloorItem.modeluuid;
|
||||
newEventData.modelName = newFloorItem.modelname;
|
||||
newEventData.position = newFloorItem.position;
|
||||
newEventData.rotation = [obj.rotation.x, obj.rotation.y, obj.rotation.z];
|
||||
|
||||
setSimulationPaths((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[]) => [
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => [
|
||||
...(prevEvents || []),
|
||||
newEventData as Types.ConveyorEventsSchema | Types.VehicleEventsSchema
|
||||
newEventData as Types.ConveyorEventsSchema
|
||||
]);
|
||||
|
||||
socket.emit("v2:model-asset:add", data);
|
||||
|
||||
} else if (eventData.type === 'Vehicle' && eventData) {
|
||||
const createVehiclePoint = () => {
|
||||
const pointUUID = THREE.MathUtils.generateUUID();
|
||||
const vehiclePoint = (eventData as Types.VehicleEventsSchema)?.points;
|
||||
const hasActions = vehiclePoint?.actions !== undefined;
|
||||
|
||||
const defaultAction = {
|
||||
uuid: THREE.MathUtils.generateUUID(),
|
||||
name: 'Action 1',
|
||||
type: 'Inherit',
|
||||
start: {},
|
||||
hitCount: 0,
|
||||
end: {},
|
||||
buffer: 0
|
||||
};
|
||||
|
||||
return {
|
||||
uuid: pointUUID,
|
||||
position: vehiclePoint?.position,
|
||||
actions: hasActions
|
||||
? {
|
||||
...vehiclePoint.actions,
|
||||
uuid: THREE.MathUtils.generateUUID()
|
||||
}
|
||||
: defaultAction,
|
||||
connections: {
|
||||
source: { modelUUID: obj.uuid, pointUUID },
|
||||
targets: []
|
||||
},
|
||||
speed: vehiclePoint?.speed || 1
|
||||
};
|
||||
};
|
||||
|
||||
const backendEventData = {
|
||||
type: 'Vehicle',
|
||||
points: createVehiclePoint(),
|
||||
};
|
||||
|
||||
// API
|
||||
|
||||
// setFloorItemApi(
|
||||
// organization,
|
||||
// obj.uuid,
|
||||
// obj.userData.name,
|
||||
// obj.userData.modelId,
|
||||
// [worldPosition.x, worldPosition.y, worldPosition.z],
|
||||
// { "x": obj.rotation.x, "y": obj.rotation.y, "z": obj.rotation.z },
|
||||
// false,
|
||||
// true,
|
||||
// { type: backendEventData.type, points: backendEventData.points }
|
||||
// );
|
||||
|
||||
// SOCKET
|
||||
|
||||
const data = {
|
||||
organization,
|
||||
modeluuid: newFloorItem.modeluuid,
|
||||
modelname: newFloorItem.modelname,
|
||||
modelfileID: newFloorItem.modelfileID,
|
||||
position: newFloorItem.position,
|
||||
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
eventData: { type: backendEventData.type, points: backendEventData.points },
|
||||
socketId: socket.id,
|
||||
};
|
||||
|
||||
const newEventData: any = { type: backendEventData.type, points: backendEventData.points };
|
||||
newEventData.modeluuid = newFloorItem.modeluuid;
|
||||
newEventData.modelName = newFloorItem.modelname;
|
||||
newEventData.position = newFloorItem.position;
|
||||
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => [
|
||||
...(prevEvents || []),
|
||||
newEventData as Types.VehicleEventsSchema
|
||||
]);
|
||||
|
||||
socket.emit("v2:model-asset:add", data);
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import * as THREE from "three";
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import { useFloorItems, useSelectedAssets, useSimulationPaths, useSocketStore, useToggleView } from "../../../../store/store";
|
||||
import { useFloorItems, useSelectedAssets, useSimulationStates, useSocketStore, useToggleView } from "../../../../store/store";
|
||||
import { toast } from "react-toastify";
|
||||
// import { setFloorItemApi } from '../../../../services/factoryBuilder/assest/floorAsset/setFloorItemApi';
|
||||
import * as Types from "../../../../types/world/worldTypes";
|
||||
import { setFloorItemApi } from "../../../../services/factoryBuilder/assest/floorAsset/setFloorItemApi";
|
||||
|
||||
const DuplicationControls = ({ itemsGroupRef, duplicatedObjects, setDuplicatedObjects, setpastedObjects, selectionGroup, movedObjects, setMovedObjects, rotatedObjects, setRotatedObjects, boundingBoxRef }: any) => {
|
||||
const { camera, controls, gl, scene, pointer, raycaster } = useThree();
|
||||
const { toggleView } = useToggleView();
|
||||
const { selectedAssets, setSelectedAssets } = useSelectedAssets();
|
||||
const { simulationPaths, setSimulationPaths } = useSimulationPaths();
|
||||
const { simulationStates, setSimulationStates } = useSimulationStates();
|
||||
const plane = useMemo(() => new THREE.Plane(new THREE.Vector3(0, 1, 0), 0), []);
|
||||
const { floorItems, setFloorItems } = useFloorItems();
|
||||
const { socket } = useSocketStore();
|
||||
@@ -131,7 +132,7 @@ const DuplicationControls = ({ itemsGroupRef, duplicatedObjects, setDuplicatedOb
|
||||
return updatedItems;
|
||||
});
|
||||
|
||||
let eventData: Types.ConveyorEventsSchema | Types.VehicleEventsSchema | undefined = simulationPaths.find((events) => events.modeluuid === obj.userData.modeluuid);
|
||||
let eventData: Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema | undefined = simulationStates.find((events) => events.modeluuid === obj.userData.modeluuid);
|
||||
|
||||
const email = localStorage.getItem("email");
|
||||
const organization = email ? email.split("@")[1].split(".")[0] : "default";
|
||||
@@ -164,7 +165,7 @@ const DuplicationControls = ({ itemsGroupRef, duplicatedObjects, setDuplicatedOb
|
||||
: [defaultAction],
|
||||
triggers: (eventData as Types.ConveyorEventsSchema)?.points[index].triggers,
|
||||
connections: {
|
||||
source: { pathUUID: obj.uuid, pointUUID },
|
||||
source: { modelUUID: newFloorItem.modeluuid, pointUUID },
|
||||
targets: []
|
||||
}
|
||||
};
|
||||
@@ -182,16 +183,16 @@ const DuplicationControls = ({ itemsGroupRef, duplicatedObjects, setDuplicatedOb
|
||||
|
||||
//REST
|
||||
|
||||
// await setFloorItemApi(
|
||||
// setFloorItemApi(
|
||||
// organization,
|
||||
// obj.uuid,
|
||||
// obj.userData.name,
|
||||
// obj.userData.modelId,
|
||||
// [worldPosition.x, worldPosition.y, worldPosition.z],
|
||||
// { "x": obj.rotation.x, "y": obj.rotation.y, "z": obj.rotation.z },
|
||||
// obj.userData.modelId,
|
||||
// false,
|
||||
// true,
|
||||
// backendEventData
|
||||
// { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed }
|
||||
// );
|
||||
|
||||
//SOCKET
|
||||
@@ -205,22 +206,102 @@ const DuplicationControls = ({ itemsGroupRef, duplicatedObjects, setDuplicatedOb
|
||||
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
eventData: backendEventData,
|
||||
eventData: { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed },
|
||||
socketId: socket.id,
|
||||
};
|
||||
|
||||
const newEventData: any = backendEventData;
|
||||
const newEventData: any = { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed };
|
||||
newEventData.modeluuid = newFloorItem.modeluuid;
|
||||
newEventData.modelName = newFloorItem.modelname;
|
||||
newEventData.position = newFloorItem.position;
|
||||
newEventData.rotation = [obj.rotation.x, obj.rotation.y, obj.rotation.z];
|
||||
|
||||
setSimulationPaths((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[]) => [
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => [
|
||||
...(prevEvents || []),
|
||||
newEventData as Types.ConveyorEventsSchema | Types.VehicleEventsSchema
|
||||
newEventData as Types.ConveyorEventsSchema
|
||||
]);
|
||||
|
||||
socket.emit("v2:model-asset:add", data);
|
||||
|
||||
} else if (eventData.type === 'Vehicle' && eventData) {
|
||||
const createVehiclePoint = () => {
|
||||
const pointUUID = THREE.MathUtils.generateUUID();
|
||||
const vehiclePoint = (eventData as Types.VehicleEventsSchema)?.points;
|
||||
const hasActions = vehiclePoint?.actions !== undefined;
|
||||
|
||||
const defaultAction = {
|
||||
uuid: THREE.MathUtils.generateUUID(),
|
||||
name: 'Action 1',
|
||||
type: 'Inherit',
|
||||
start: {},
|
||||
hitCount: 0,
|
||||
end: {},
|
||||
buffer: 0
|
||||
};
|
||||
|
||||
return {
|
||||
uuid: pointUUID,
|
||||
position: vehiclePoint?.position,
|
||||
actions: hasActions
|
||||
? {
|
||||
...vehiclePoint.actions,
|
||||
uuid: THREE.MathUtils.generateUUID()
|
||||
}
|
||||
: defaultAction,
|
||||
connections: {
|
||||
source: { modelUUID: obj.uuid, pointUUID },
|
||||
targets: []
|
||||
},
|
||||
speed: vehiclePoint?.speed || 2
|
||||
};
|
||||
};
|
||||
|
||||
const backendEventData = {
|
||||
type: 'Vehicle',
|
||||
points: createVehiclePoint()
|
||||
};
|
||||
|
||||
// API
|
||||
|
||||
// setFloorItemApi(
|
||||
// organization,
|
||||
// obj.uuid,
|
||||
// obj.userData.name,
|
||||
// obj.userData.modelId,
|
||||
// [worldPosition.x, worldPosition.y, worldPosition.z],
|
||||
// { "x": obj.rotation.x, "y": obj.rotation.y, "z": obj.rotation.z },
|
||||
// false,
|
||||
// true,
|
||||
// { type: backendEventData.type, points: backendEventData.points }
|
||||
// );
|
||||
|
||||
// SOCKET
|
||||
|
||||
const data = {
|
||||
organization,
|
||||
modeluuid: newFloorItem.modeluuid,
|
||||
modelname: newFloorItem.modelname,
|
||||
modelfileID: newFloorItem.modelfileID,
|
||||
position: newFloorItem.position,
|
||||
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
eventData: { type: backendEventData.type, points: backendEventData.points },
|
||||
socketId: socket.id,
|
||||
};
|
||||
|
||||
const newEventData: any = { type: backendEventData.type, points: backendEventData.points };
|
||||
newEventData.modeluuid = newFloorItem.modeluuid;
|
||||
newEventData.modelName = newFloorItem.modelname;
|
||||
newEventData.position = newFloorItem.position;
|
||||
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => [
|
||||
...(prevEvents || []),
|
||||
newEventData as Types.VehicleEventsSchema
|
||||
]);
|
||||
|
||||
socket.emit("v2:model-asset:add", data);
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as THREE from "three";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import { useFloorItems, useSelectedAssets, useSimulationPaths, useSocketStore, useToggleView } from "../../../../store/store";
|
||||
import { useFloorItems, useSelectedAssets, useSimulationStates, useSocketStore, useToggleView } from "../../../../store/store";
|
||||
// import { setFloorItemApi } from '../../../../services/factoryBuilder/assest/floorAsset/setFloorItemApi';
|
||||
import { toast } from "react-toastify";
|
||||
import * as Types from "../../../../types/world/worldTypes";
|
||||
@@ -12,7 +12,7 @@ function MoveControls({ movedObjects, setMovedObjects, itemsGroupRef, copiedObje
|
||||
|
||||
const { toggleView } = useToggleView();
|
||||
const { selectedAssets, setSelectedAssets } = useSelectedAssets();
|
||||
const { simulationPaths, setSimulationPaths } = useSimulationPaths();
|
||||
const { simulationStates, setSimulationStates } = useSimulationStates();
|
||||
const { floorItems, setFloorItems } = useFloorItems();
|
||||
const { socket } = useSocketStore();
|
||||
const itemsData = useRef<Types.FloorItems>([]);
|
||||
@@ -180,12 +180,12 @@ function MoveControls({ movedObjects, setMovedObjects, itemsGroupRef, copiedObje
|
||||
return updatedItems;
|
||||
});
|
||||
|
||||
let eventData: Types.ConveyorEventsSchema | Types.VehicleEventsSchema | undefined = simulationPaths.find((events) => events.modeluuid === obj.userData.modeluuid);
|
||||
let eventData: Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema | undefined = simulationStates.find((events) => events.modeluuid === obj.userData.modeluuid);
|
||||
|
||||
const email = localStorage.getItem("email");
|
||||
const organization = email ? email.split("@")[1].split(".")[0] : "default";
|
||||
|
||||
if (eventData) {
|
||||
if (eventData && eventData.type !== 'StaticMachine') {
|
||||
if (eventData.type === 'Conveyor' && eventData) {
|
||||
|
||||
const backendEventData = {
|
||||
@@ -205,7 +205,7 @@ function MoveControls({ movedObjects, setMovedObjects, itemsGroupRef, copiedObje
|
||||
// obj.userData.modelId,
|
||||
// false,
|
||||
// true,
|
||||
// backendEventData
|
||||
// { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed }
|
||||
// );
|
||||
|
||||
//SOCKET
|
||||
@@ -219,17 +219,17 @@ function MoveControls({ movedObjects, setMovedObjects, itemsGroupRef, copiedObje
|
||||
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
eventData: backendEventData,
|
||||
eventData: { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed },
|
||||
socketId: socket.id,
|
||||
};
|
||||
|
||||
const newEventData: any = backendEventData;
|
||||
const newEventData: any = { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed };
|
||||
newEventData.modeluuid = newFloorItem.modeluuid;
|
||||
newEventData.modelName = newFloorItem.modelname;
|
||||
newEventData.position = newFloorItem.position;
|
||||
newEventData.rotation = [obj.rotation.x, obj.rotation.y, obj.rotation.z];
|
||||
|
||||
setSimulationPaths((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[]) => {
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => {
|
||||
const updatedEvents = (prevEvents || []).map(event =>
|
||||
event.modeluuid === newFloorItem.modeluuid
|
||||
? { ...event, ...newEventData }
|
||||
@@ -239,6 +239,58 @@ function MoveControls({ movedObjects, setMovedObjects, itemsGroupRef, copiedObje
|
||||
});
|
||||
|
||||
socket.emit("v2:model-asset:add", data);
|
||||
} else if (eventData.type === 'Vehicle' && eventData) {
|
||||
|
||||
const backendEventData = {
|
||||
type: 'Vehicle',
|
||||
points: eventData.points
|
||||
};
|
||||
|
||||
// REST
|
||||
|
||||
// await setFloorItemApi(
|
||||
// organization,
|
||||
// obj.uuid,
|
||||
// obj.userData.name,
|
||||
// obj.userData.modelId,
|
||||
// [worldPosition.x, worldPosition.y, worldPosition.z],
|
||||
// { "x": obj.rotation.x, "y": obj.rotation.y, "z": obj.rotation.z },
|
||||
// false,
|
||||
// true,
|
||||
// { type: backendEventData.type, points: backendEventData.points }
|
||||
// );
|
||||
|
||||
//SOCKET
|
||||
|
||||
const data = {
|
||||
organization,
|
||||
modeluuid: newFloorItem.modeluuid,
|
||||
modelname: newFloorItem.modelname,
|
||||
modelfileID: newFloorItem.modelfileID,
|
||||
position: newFloorItem.position,
|
||||
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
eventData: { type: backendEventData.type, points: backendEventData.points },
|
||||
socketId: socket.id,
|
||||
};
|
||||
|
||||
const newEventData: any = { type: backendEventData.type, points: backendEventData.points };
|
||||
newEventData.modeluuid = newFloorItem.modeluuid;
|
||||
newEventData.modelName = newFloorItem.modelname;
|
||||
newEventData.position = newFloorItem.position;
|
||||
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => {
|
||||
const updatedEvents = (prevEvents || []).map(event =>
|
||||
event.modeluuid === newFloorItem.modeluuid
|
||||
? { ...event, ...newEventData }
|
||||
: event
|
||||
);
|
||||
return updatedEvents;
|
||||
});
|
||||
|
||||
socket.emit("v2:model-asset:add", data);
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import * as THREE from "three";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import { useFloorItems, useSelectedAssets, useSimulationPaths, useSocketStore, useToggleView } from "../../../../store/store";
|
||||
import { useFloorItems, useSelectedAssets, useSimulationStates, useSocketStore, useToggleView } from "../../../../store/store";
|
||||
// import { setFloorItemApi } from '../../../../services/factoryBuilder/assest/floorAsset/setFloorItemApi';
|
||||
import { toast } from "react-toastify";
|
||||
import * as Types from "../../../../types/world/worldTypes";
|
||||
import { setFloorItemApi } from "../../../../services/factoryBuilder/assest/floorAsset/setFloorItemApi";
|
||||
|
||||
function RotateControls({ rotatedObjects, setRotatedObjects, movedObjects, setMovedObjects, itemsGroupRef, copiedObjects, setCopiedObjects, pastedObjects, setpastedObjects, duplicatedObjects, setDuplicatedObjects, selectionGroup, boundingBoxRef }: any) {
|
||||
const { camera, controls, gl, scene, pointer, raycaster } = useThree();
|
||||
@@ -12,7 +13,7 @@ function RotateControls({ rotatedObjects, setRotatedObjects, movedObjects, setMo
|
||||
|
||||
const { toggleView } = useToggleView();
|
||||
const { selectedAssets, setSelectedAssets } = useSelectedAssets();
|
||||
const { simulationPaths, setSimulationPaths } = useSimulationPaths();
|
||||
const { simulationStates, setSimulationStates } = useSimulationStates();
|
||||
const { floorItems, setFloorItems } = useFloorItems();
|
||||
const { socket } = useSocketStore();
|
||||
const itemsData = useRef<Types.FloorItems>([]);
|
||||
@@ -183,12 +184,12 @@ function RotateControls({ rotatedObjects, setRotatedObjects, movedObjects, setMo
|
||||
return updatedItems;
|
||||
});
|
||||
|
||||
let eventData: Types.ConveyorEventsSchema | Types.VehicleEventsSchema | undefined = simulationPaths.find((events) => events.modeluuid === obj.userData.modeluuid);
|
||||
let eventData: Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema | undefined = simulationStates.find((events) => events.modeluuid === obj.userData.modeluuid);
|
||||
|
||||
const email = localStorage.getItem("email");
|
||||
const organization = email ? email.split("@")[1].split(".")[0] : "default";
|
||||
|
||||
if (eventData) {
|
||||
if (eventData && eventData.type !== 'StaticMachine') {
|
||||
if (eventData.type === 'Conveyor' && eventData) {
|
||||
|
||||
const backendEventData = {
|
||||
@@ -197,18 +198,18 @@ function RotateControls({ rotatedObjects, setRotatedObjects, movedObjects, setMo
|
||||
speed: (eventData as Types.ConveyorEventsSchema)?.speed
|
||||
};
|
||||
|
||||
//REST
|
||||
// REST
|
||||
|
||||
// await setFloorItemApi(
|
||||
// organization,
|
||||
// obj.uuid,
|
||||
// obj.userData.name,
|
||||
// obj.userData.modelId,
|
||||
// [worldPosition.x, worldPosition.y, worldPosition.z],
|
||||
// { "x": obj.rotation.x, "y": obj.rotation.y, "z": obj.rotation.z },
|
||||
// obj.userData.modelId,
|
||||
// false,
|
||||
// true,
|
||||
// backendEventData
|
||||
// { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed }
|
||||
// );
|
||||
|
||||
//SOCKET
|
||||
@@ -222,17 +223,17 @@ function RotateControls({ rotatedObjects, setRotatedObjects, movedObjects, setMo
|
||||
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
eventData: backendEventData,
|
||||
eventData: { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed },
|
||||
socketId: socket.id,
|
||||
};
|
||||
|
||||
const newEventData: any = backendEventData;
|
||||
const newEventData: any = { type: backendEventData.type, points: backendEventData.points, speed: backendEventData.speed };
|
||||
newEventData.modeluuid = newFloorItem.modeluuid;
|
||||
newEventData.modelName = newFloorItem.modelname;
|
||||
newEventData.position = newFloorItem.position;
|
||||
newEventData.rotation = [obj.rotation.x, obj.rotation.y, obj.rotation.z];
|
||||
|
||||
setSimulationPaths((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[]) => {
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => {
|
||||
const updatedEvents = (prevEvents || []).map(event =>
|
||||
event.modeluuid === newFloorItem.modeluuid
|
||||
? { ...event, ...newEventData }
|
||||
@@ -242,10 +243,62 @@ function RotateControls({ rotatedObjects, setRotatedObjects, movedObjects, setMo
|
||||
});
|
||||
|
||||
socket.emit("v2:model-asset:add", data);
|
||||
|
||||
} else if (eventData.type === 'Vehicle' && eventData) {
|
||||
|
||||
const backendEventData = {
|
||||
type: 'Vehicle',
|
||||
points: eventData.points
|
||||
};
|
||||
|
||||
// REST
|
||||
|
||||
// await setFloorItemApi(
|
||||
// organization,
|
||||
// obj.uuid,
|
||||
// obj.userData.name,
|
||||
// obj.userData.modelId,
|
||||
// [worldPosition.x, worldPosition.y, worldPosition.z],
|
||||
// { "x": obj.rotation.x, "y": obj.rotation.y, "z": obj.rotation.z },
|
||||
// false,
|
||||
// true,
|
||||
// { type: backendEventData.type, points: backendEventData.points }
|
||||
// );
|
||||
|
||||
//SOCKET
|
||||
|
||||
const data = {
|
||||
organization,
|
||||
modeluuid: newFloorItem.modeluuid,
|
||||
modelname: newFloorItem.modelname,
|
||||
modelfileID: newFloorItem.modelfileID,
|
||||
position: newFloorItem.position,
|
||||
rotation: { x: obj.rotation.x, y: obj.rotation.y, z: obj.rotation.z },
|
||||
isLocked: false,
|
||||
isVisible: true,
|
||||
eventData: { type: backendEventData.type, points: backendEventData.points },
|
||||
socketId: socket.id,
|
||||
};
|
||||
|
||||
const newEventData: any = { type: backendEventData.type, points: backendEventData.points };
|
||||
newEventData.modeluuid = newFloorItem.modeluuid;
|
||||
newEventData.modelName = newFloorItem.modelname;
|
||||
newEventData.position = newFloorItem.position;
|
||||
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => {
|
||||
const updatedEvents = (prevEvents || []).map(event =>
|
||||
event.modeluuid === newFloorItem.modeluuid
|
||||
? { ...event, ...newEventData }
|
||||
: event
|
||||
);
|
||||
return updatedEvents;
|
||||
});
|
||||
|
||||
socket.emit("v2:model-asset:add", data);
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
|
||||
//REST
|
||||
|
||||
// await setFloorItemApi(
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { SelectionBox } from "three/examples/jsm/interactive/SelectionBox";
|
||||
import { SelectionHelper } from "./selectionHelper";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import { useFloorItems, useSelectedAssets, useSimulationPaths, useSocketStore, useToggleView } from "../../../../store/store";
|
||||
import { useFloorItems, useSelectedAssets, useSimulationStates, useSocketStore, useToggleView } from "../../../../store/store";
|
||||
import BoundingBox from "./boundingBoxHelper";
|
||||
import { toast } from "react-toastify";
|
||||
// import { deleteFloorItem } from '../../../../services/factoryBuilder/assest/floorAsset/deleteFloorItemApi';
|
||||
@@ -20,7 +20,7 @@ const SelectionControls: React.FC = () => {
|
||||
const itemsGroupRef = useRef<THREE.Group | undefined>(undefined);
|
||||
const selectionGroup = useRef() as Types.RefGroup;
|
||||
const { toggleView } = useToggleView();
|
||||
const { setSimulationPaths } = useSimulationPaths();
|
||||
const { setSimulationStates } = useSimulationStates();
|
||||
const { selectedAssets, setSelectedAssets } = useSelectedAssets();
|
||||
const [movedObjects, setMovedObjects] = useState<THREE.Object3D[]>([]);
|
||||
const [rotatedObjects, setRotatedObjects] = useState<THREE.Object3D[]>([]);
|
||||
@@ -240,7 +240,7 @@ const SelectionControls: React.FC = () => {
|
||||
}
|
||||
});
|
||||
|
||||
setSimulationPaths((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[]) => {
|
||||
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => {
|
||||
const updatedEvents = (prevEvents || []).filter(event => event.modeluuid !== selectedMesh.uuid);
|
||||
return updatedEvents;
|
||||
});
|
||||
|
||||
@@ -91,7 +91,7 @@ export default function PostProcessing() {
|
||||
)}
|
||||
{selectedActionSphere && (
|
||||
<Outline
|
||||
selection={[selectedActionSphere.point]}
|
||||
selection={[selectedActionSphere.points]}
|
||||
selectionLayer={10}
|
||||
width={1000}
|
||||
blendFunction={BlendFunction.ALPHA}
|
||||
|
||||
@@ -53,8 +53,8 @@ import { findEnvironment } from "../../../services/factoryBuilder/environment/fi
|
||||
import Layer2DVisibility from "../../builder/geomentries/layers/layer2DVisibility";
|
||||
import DrieHtmlTemp from "../mqttTemp/drieHtmlTemp";
|
||||
import ZoneGroup from "../../builder/groups/zoneGroup";
|
||||
import Agv from "../../builder/agv/agv";
|
||||
import useModuleStore from "../../../store/useModuleStore";
|
||||
import NavMeshCreator from "../../builder/agv/navMeshCreator";
|
||||
|
||||
export default function World() {
|
||||
const state = useThree<Types.ThreeState>(); // Importing the state from the useThree hook, which contains the scene, camera, and other Three.js elements.
|
||||
@@ -367,7 +367,9 @@ export default function World() {
|
||||
/>
|
||||
|
||||
{/* <DrieHtmlTemp itemsGroup={itemsGroup} /> */}
|
||||
{activeModule === "simulation" && <Agv lines={lines} plane={plane} />}
|
||||
|
||||
<NavMeshCreator lines={lines} />
|
||||
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { useFloorItems, useSimulationPaths } from '../../../store/store';
|
||||
import { useFloorItems, useSimulationStates } from '../../../store/store';
|
||||
import * as THREE from 'three';
|
||||
import * as Types from '../../../types/world/worldTypes';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
function Behaviour() {
|
||||
const { setSimulationPaths } = useSimulationPaths();
|
||||
const { setSimulationStates } = useSimulationStates();
|
||||
const { floorItems } = useFloorItems();
|
||||
|
||||
useEffect(() => {
|
||||
const newPaths: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema)[] = [];
|
||||
const newPaths: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[] = [];
|
||||
|
||||
// floorItems.forEach((item: Types.FloorItemType) => {
|
||||
// if (item.modelfileID === "672a090f80d91ac979f4d0bd") {
|
||||
@@ -31,7 +31,7 @@ function Behaviour() {
|
||||
// rotation: [0, 0, 0],
|
||||
// actions: [{ uuid: THREE.MathUtils.generateUUID(), name: 'Action 1', type: 'Inherit', material: 'Inherit', delay: 'Inherit', spawnInterval: 'Inherit', isUsed: true }],
|
||||
// triggers: [],
|
||||
// connections: { source: { pathUUID: item.modeluuid, pointUUID: point1UUID }, targets: [] },
|
||||
// connections: { source: { modelUUID: item.modeluuid, pointUUID: point1UUID }, targets: [] },
|
||||
// },
|
||||
// {
|
||||
// uuid: middlePointUUID,
|
||||
@@ -39,7 +39,7 @@ function Behaviour() {
|
||||
// rotation: [0, 0, 0],
|
||||
// actions: [{ uuid: THREE.MathUtils.generateUUID(), name: 'Action 1', type: 'Inherit', material: 'Inherit', delay: 'Inherit', spawnInterval: 'Inherit', isUsed: true }],
|
||||
// triggers: [],
|
||||
// connections: { source: { pathUUID: item.modeluuid, pointUUID: middlePointUUID }, targets: [] },
|
||||
// connections: { source: { modelUUID: item.modeluuid, pointUUID: middlePointUUID }, targets: [] },
|
||||
// },
|
||||
// {
|
||||
// uuid: point2UUID,
|
||||
@@ -47,7 +47,7 @@ function Behaviour() {
|
||||
// rotation: [0, 0, 0],
|
||||
// actions: [{ uuid: THREE.MathUtils.generateUUID(), name: 'Action 1', type: 'Inherit', material: 'Inherit', delay: 'Inherit', spawnInterval: 'Inherit', isUsed: true }],
|
||||
// triggers: [],
|
||||
// connections: { source: { pathUUID: item.modeluuid, pointUUID: point2UUID }, targets: [] },
|
||||
// connections: { source: { modelUUID: item.modeluuid, pointUUID: point2UUID }, targets: [] },
|
||||
// },
|
||||
// ],
|
||||
// position: [...item.position],
|
||||
@@ -68,7 +68,7 @@ function Behaviour() {
|
||||
// uuid: pointUUID,
|
||||
// position: [pointPosition.x, pointPosition.y, pointPosition.z],
|
||||
// actions: { uuid: THREE.MathUtils.generateUUID(), name: 'Action 1', type: 'Start', start: {}, hitCount: 1, end: {}, buffer: 0 },
|
||||
// connections: { source: { pathUUID: item.modeluuid, pointUUID: pointUUID }, targets: [] },
|
||||
// connections: { source: { modelUUID: item.modeluuid, pointUUID: pointUUID }, targets: [] },
|
||||
// speed: 2,
|
||||
// },
|
||||
// position: [...item.position],
|
||||
@@ -78,7 +78,7 @@ function Behaviour() {
|
||||
// }
|
||||
// });
|
||||
|
||||
// setSimulationPaths(newPaths);
|
||||
// setSimulationStates(newPaths);
|
||||
// console.log('floorItems: ', floorItems);
|
||||
}, [floorItems]);
|
||||
|
||||
|
||||
@@ -3,17 +3,21 @@ import React, { useEffect, useState } from 'react';
|
||||
import * as THREE from 'three';
|
||||
import * as Types from '../../../types/world/worldTypes';
|
||||
import { QuadraticBezierLine } from '@react-three/drei';
|
||||
import { useIsConnecting, useSimulationPaths } from '../../../store/store';
|
||||
import { useIsConnecting, useSimulationStates, useSocketStore } from '../../../store/store';
|
||||
import useModuleStore from '../../../store/useModuleStore';
|
||||
import { usePlayButtonStore } from '../../../store/usePlayButtonStore';
|
||||
import { setEventApi } from '../../../services/factoryBuilder/assest/floorAsset/setEventsApt';
|
||||
|
||||
function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObject<THREE.Group> }) {
|
||||
const { activeModule } = useModuleStore();
|
||||
const { gl, raycaster, scene, pointer, camera } = useThree();
|
||||
const { setIsConnecting } = useIsConnecting();
|
||||
const { simulationPaths, setSimulationPaths } = useSimulationPaths();
|
||||
const { simulationStates, setSimulationStates } = useSimulationStates();
|
||||
const { isPlaying } = usePlayButtonStore();
|
||||
const { socket } = useSocketStore();
|
||||
|
||||
const [firstSelected, setFirstSelected] = useState<{
|
||||
pathUUID: string;
|
||||
modelUUID: string;
|
||||
sphereUUID: string;
|
||||
position: THREE.Vector3;
|
||||
isCorner: boolean;
|
||||
@@ -22,26 +26,26 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
const [helperlineColor, setHelperLineColor] = useState<string>('red');
|
||||
|
||||
const updatePathConnections = (
|
||||
fromPathUUID: string,
|
||||
fromModelUUID: string,
|
||||
fromPointUUID: string,
|
||||
toPathUUID: string,
|
||||
toModelUUID: string,
|
||||
toPointUUID: string
|
||||
) => {
|
||||
const updatedPaths = simulationPaths.map(path => {
|
||||
const updatedPaths = simulationStates.map(path => {
|
||||
if (path.type === 'Conveyor') {
|
||||
if (path.modeluuid === fromPathUUID) {
|
||||
if (path.modeluuid === fromModelUUID) {
|
||||
return {
|
||||
...path,
|
||||
points: path.points.map(point => {
|
||||
if (point.uuid === fromPointUUID) {
|
||||
const newTarget = {
|
||||
pathUUID: toPathUUID,
|
||||
modelUUID: toModelUUID,
|
||||
pointUUID: toPointUUID
|
||||
};
|
||||
const existingTargets = point.connections.targets || [];
|
||||
|
||||
if (!existingTargets.some(target =>
|
||||
target.pathUUID === newTarget.pathUUID &&
|
||||
target.modelUUID === newTarget.modelUUID &&
|
||||
target.pointUUID === newTarget.pointUUID
|
||||
)) {
|
||||
return {
|
||||
@@ -57,19 +61,19 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
})
|
||||
};
|
||||
}
|
||||
else if (path.modeluuid === toPathUUID) {
|
||||
else if (path.modeluuid === toModelUUID) {
|
||||
return {
|
||||
...path,
|
||||
points: path.points.map(point => {
|
||||
if (point.uuid === toPointUUID) {
|
||||
const reverseTarget = {
|
||||
pathUUID: fromPathUUID,
|
||||
modelUUID: fromModelUUID,
|
||||
pointUUID: fromPointUUID
|
||||
};
|
||||
const existingTargets = point.connections.targets || [];
|
||||
|
||||
if (!existingTargets.some(target =>
|
||||
target.pathUUID === reverseTarget.pathUUID &&
|
||||
target.modelUUID === reverseTarget.modelUUID &&
|
||||
target.pointUUID === reverseTarget.pointUUID
|
||||
)) {
|
||||
return {
|
||||
@@ -86,18 +90,17 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
};
|
||||
}
|
||||
}
|
||||
// In the updatePathConnections function, modify the Vehicle handling section:
|
||||
else if (path.type === 'Vehicle') {
|
||||
// Handle outgoing connections from Vehicle
|
||||
if (path.modeluuid === fromPathUUID && path.point.uuid === fromPointUUID) {
|
||||
if (path.modeluuid === fromModelUUID && path.points.uuid === fromPointUUID) {
|
||||
const newTarget = {
|
||||
pathUUID: toPathUUID,
|
||||
modelUUID: toModelUUID,
|
||||
pointUUID: toPointUUID
|
||||
};
|
||||
const existingTargets = path.point.connections.targets || [];
|
||||
const existingTargets = path.points.connections.targets || [];
|
||||
|
||||
// Check if target is a Conveyor
|
||||
const toPath = simulationPaths.find(p => p.modeluuid === toPathUUID);
|
||||
const toPath = simulationStates.find(p => p.modeluuid === toModelUUID);
|
||||
if (toPath?.type !== 'Conveyor') {
|
||||
console.log("Vehicle can only connect to Conveyors");
|
||||
return path;
|
||||
@@ -110,15 +113,15 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
}
|
||||
|
||||
if (!existingTargets.some(target =>
|
||||
target.pathUUID === newTarget.pathUUID &&
|
||||
target.modelUUID === newTarget.modelUUID &&
|
||||
target.pointUUID === newTarget.pointUUID
|
||||
)) {
|
||||
return {
|
||||
...path,
|
||||
point: {
|
||||
...path.point,
|
||||
points: {
|
||||
...path.points,
|
||||
connections: {
|
||||
...path.point.connections,
|
||||
...path.points.connections,
|
||||
targets: [...existingTargets, newTarget]
|
||||
}
|
||||
}
|
||||
@@ -126,15 +129,15 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
}
|
||||
}
|
||||
// Handle incoming connections to Vehicle
|
||||
else if (path.modeluuid === toPathUUID && path.point.uuid === toPointUUID) {
|
||||
else if (path.modeluuid === toModelUUID && path.points.uuid === toPointUUID) {
|
||||
const reverseTarget = {
|
||||
pathUUID: fromPathUUID,
|
||||
modelUUID: fromModelUUID,
|
||||
pointUUID: fromPointUUID
|
||||
};
|
||||
const existingTargets = path.point.connections.targets || [];
|
||||
const existingTargets = path.points.connections.targets || [];
|
||||
|
||||
// Check if source is a Conveyor
|
||||
const fromPath = simulationPaths.find(p => p.modeluuid === fromPathUUID);
|
||||
const fromPath = simulationStates.find(p => p.modeluuid === fromModelUUID);
|
||||
if (fromPath?.type !== 'Conveyor') {
|
||||
console.log("Vehicle can only connect to Conveyors");
|
||||
return path;
|
||||
@@ -147,15 +150,15 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
}
|
||||
|
||||
if (!existingTargets.some(target =>
|
||||
target.pathUUID === reverseTarget.pathUUID &&
|
||||
target.modelUUID === reverseTarget.modelUUID &&
|
||||
target.pointUUID === reverseTarget.pointUUID
|
||||
)) {
|
||||
return {
|
||||
...path,
|
||||
point: {
|
||||
...path.point,
|
||||
points: {
|
||||
...path.points,
|
||||
connections: {
|
||||
...path.point.connections,
|
||||
...path.points.connections,
|
||||
targets: [...existingTargets, reverseTarget]
|
||||
}
|
||||
}
|
||||
@@ -164,14 +167,139 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
}
|
||||
return path;
|
||||
}
|
||||
// else if (path.type === 'StaticMachine') {
|
||||
// if (path.modeluuid === fromModelUUID && path.points.uuid === fromPointUUID) {
|
||||
// const newTarget = {
|
||||
// modelUUID: toModelUUID,
|
||||
// pointUUID: toPointUUID
|
||||
// };
|
||||
// const existingTargets = path.points.connections.targets || [];
|
||||
|
||||
// // Check if target is an ArmBot
|
||||
// const toPath = simulationStates.find(p => p.modeluuid === toModelUUID);
|
||||
// if (toPath?.type !== 'ArmBot') {
|
||||
// console.log("StaticMachine can only connect to ArmBot");
|
||||
// return path;
|
||||
// }
|
||||
|
||||
// // Check if already has a connection
|
||||
// if (existingTargets.length >= 1) {
|
||||
// console.log("StaticMachine can have only one connection");
|
||||
// return path;
|
||||
// }
|
||||
|
||||
// if (!existingTargets.some(target =>
|
||||
// target.modelUUID === newTarget.modelUUID &&
|
||||
// target.pointUUID === newTarget.pointUUID
|
||||
// )) {
|
||||
// return {
|
||||
// ...path,
|
||||
// points: {
|
||||
// ...path.points,
|
||||
// connections: {
|
||||
// ...path.points.connections,
|
||||
// targets: [...existingTargets, newTarget]
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
// // Handle incoming connections to StaticMachine
|
||||
// else if (path.modeluuid === toModelUUID && path.points.uuid === toPointUUID) {
|
||||
// const reverseTarget = {
|
||||
// modelUUID: fromModelUUID,
|
||||
// pointUUID: fromPointUUID
|
||||
// };
|
||||
// const existingTargets = path.points.connections.targets || [];
|
||||
|
||||
// // Check if source is an ArmBot
|
||||
// const fromPath = simulationStates.find(p => p.modeluuid === fromModelUUID);
|
||||
// if (fromPath?.type !== 'ArmBot') {
|
||||
// console.log("StaticMachine can only connect to ArmBot");
|
||||
// return path;
|
||||
// }
|
||||
|
||||
// // Check if already has a connection
|
||||
// if (existingTargets.length >= 1) {
|
||||
// console.log("StaticMachine can have only one connection");
|
||||
// return path;
|
||||
// }
|
||||
|
||||
// if (!existingTargets.some(target =>
|
||||
// target.modelUUID === reverseTarget.modelUUID &&
|
||||
// target.pointUUID === reverseTarget.pointUUID
|
||||
// )) {
|
||||
// return {
|
||||
// ...path,
|
||||
// points: {
|
||||
// ...path.points,
|
||||
// connections: {
|
||||
// ...path.points.connections,
|
||||
// targets: [...existingTargets, reverseTarget]
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
// return path;
|
||||
// }
|
||||
return path;
|
||||
});
|
||||
|
||||
setSimulationPaths(updatedPaths);
|
||||
setSimulationStates(updatedPaths);
|
||||
|
||||
const updatedPathDetails = updatedPaths.filter(path =>
|
||||
path.modeluuid === fromModelUUID || path.modeluuid === toModelUUID
|
||||
);
|
||||
|
||||
updateBackend(updatedPathDetails);
|
||||
};
|
||||
|
||||
const handleAddConnection = (fromPathUUID: string, fromUUID: string, toPathUUID: string, toUUID: string) => {
|
||||
updatePathConnections(fromPathUUID, fromUUID, toPathUUID, toUUID);
|
||||
const updateBackend = async (updatedPaths: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => {
|
||||
if (updatedPaths.length === 0) return;
|
||||
const email = localStorage.getItem("email");
|
||||
const organization = email ? email.split("@")[1].split(".")[0] : "";
|
||||
|
||||
updatedPaths.forEach(async (updatedPath) => {
|
||||
if (updatedPath.type === 'Conveyor') {
|
||||
|
||||
// await setEventApi(
|
||||
// organization,
|
||||
// updatedPath.modeluuid,
|
||||
// { type: "Conveyor", points: updatedPath.points, speed: updatedPath.speed }
|
||||
// );
|
||||
|
||||
const data = {
|
||||
organization: organization,
|
||||
modeluuid: updatedPath.modeluuid,
|
||||
eventData: { type: "Conveyor", points: updatedPath.points, speed: updatedPath.speed }
|
||||
}
|
||||
|
||||
socket.emit('v2:model-asset:updateEventData', data);
|
||||
|
||||
} else if (updatedPath.type === 'Vehicle') {
|
||||
|
||||
// await setEventApi(
|
||||
// organization,
|
||||
// updatedPath.modeluuid,
|
||||
// { type: "Vehicle", points: updatedPath.points }
|
||||
// );
|
||||
|
||||
const data = {
|
||||
organization: organization,
|
||||
modeluuid: updatedPath.modeluuid,
|
||||
eventData: { type: "Vehicle", points: updatedPath.points }
|
||||
}
|
||||
|
||||
socket.emit('v2:model-asset:updateEventData', data);
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
const handleAddConnection = (fromModelUUID: string, fromUUID: string, toModelUUID: string, toUUID: string) => {
|
||||
updatePathConnections(fromModelUUID, fromUUID, toModelUUID, toUUID);
|
||||
setFirstSelected(null);
|
||||
setCurrentLine(null);
|
||||
setIsConnecting(false);
|
||||
@@ -208,25 +336,25 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
const intersected = intersects[0].object;
|
||||
|
||||
if (intersected.name.includes("events-sphere")) {
|
||||
const pathUUID = intersected.userData.path.modeluuid;
|
||||
const modelUUID = intersected.userData.path.modeluuid;
|
||||
const sphereUUID = intersected.uuid;
|
||||
const worldPosition = new THREE.Vector3();
|
||||
intersected.getWorldPosition(worldPosition);
|
||||
|
||||
let isStartOrEnd = false;
|
||||
|
||||
if (intersected.userData.path.points) {
|
||||
if (intersected.userData.path.points && intersected.userData.path.points.length > 1) {
|
||||
isStartOrEnd = intersected.userData.path.points.length > 0 && (
|
||||
sphereUUID === intersected.userData.path.points[0].uuid ||
|
||||
sphereUUID === intersected.userData.path.points[intersected.userData.path.points.length - 1].uuid
|
||||
);
|
||||
} else if (intersected.userData.path.point) {
|
||||
isStartOrEnd = sphereUUID === intersected.userData.path.point.uuid;
|
||||
} else if (intersected.userData.path.points) {
|
||||
isStartOrEnd = sphereUUID === intersected.userData.path.points.uuid;
|
||||
}
|
||||
|
||||
if (pathUUID) {
|
||||
const firstPath = simulationPaths.find(p => p.modeluuid === firstSelected?.pathUUID);
|
||||
const secondPath = simulationPaths.find(p => p.modeluuid === pathUUID);
|
||||
if (modelUUID) {
|
||||
const firstPath = simulationStates.find(p => p.modeluuid === firstSelected?.modelUUID);
|
||||
const secondPath = simulationStates.find(p => p.modeluuid === modelUUID);
|
||||
|
||||
// Prevent vehicle-to-vehicle connections
|
||||
if (firstPath && secondPath && firstPath.type === 'Vehicle' && secondPath.type === 'Vehicle') {
|
||||
@@ -238,23 +366,23 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
if (firstPath && secondPath &&
|
||||
firstPath.type === 'Conveyor' &&
|
||||
secondPath.type === 'Conveyor' &&
|
||||
!firstSelected?.isCorner) {
|
||||
console.log("Conveyor middle points can only connect to non-conveyor paths");
|
||||
(!firstSelected?.isCorner || !isStartOrEnd)) {
|
||||
console.log("Conveyor connections must be between start/end points");
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if this specific connection already exists
|
||||
const isDuplicateConnection = firstSelected
|
||||
? simulationPaths.some(path => {
|
||||
if (path.modeluuid === firstSelected.pathUUID) {
|
||||
? simulationStates.some(path => {
|
||||
if (path.modeluuid === firstSelected.modelUUID) {
|
||||
if (path.type === 'Conveyor') {
|
||||
const point = path.points.find(p => p.uuid === firstSelected.sphereUUID);
|
||||
return point?.connections.targets.some(t =>
|
||||
t.pathUUID === pathUUID && t.pointUUID === sphereUUID
|
||||
t.modelUUID === modelUUID && t.pointUUID === sphereUUID
|
||||
);
|
||||
} else if (path.type === 'Vehicle') {
|
||||
return path.point.connections.targets.some(t =>
|
||||
t.pathUUID === pathUUID && t.pointUUID === sphereUUID
|
||||
return path.points.connections.targets.some(t =>
|
||||
t.modelUUID === modelUUID && t.pointUUID === sphereUUID
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -269,7 +397,8 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
|
||||
// For Vehicles, check if they're already connected to anything
|
||||
if (intersected.userData.path.type === 'Vehicle') {
|
||||
const vehicleConnections = intersected.userData.path.point.connections.targets.length;
|
||||
console.log('intersected: ', intersected);
|
||||
const vehicleConnections = intersected.userData.path.points.connections.targets.length;
|
||||
if (vehicleConnections >= 1) {
|
||||
console.log("Vehicle can only have one connection");
|
||||
return;
|
||||
@@ -278,7 +407,7 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
|
||||
// For non-Vehicle paths, check if already connected
|
||||
if (intersected.userData.path.type !== 'Vehicle') {
|
||||
const isAlreadyConnected = simulationPaths.some(path => {
|
||||
const isAlreadyConnected = simulationStates.some(path => {
|
||||
if (path.type === 'Conveyor') {
|
||||
return path.points.some(point =>
|
||||
point.uuid === sphereUUID &&
|
||||
@@ -303,7 +432,7 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
}
|
||||
|
||||
// Prevent same-path connections
|
||||
if (firstSelected.pathUUID === pathUUID) {
|
||||
if (firstSelected.modelUUID === modelUUID) {
|
||||
console.log("Cannot connect spheres on the same path.");
|
||||
return;
|
||||
}
|
||||
@@ -316,15 +445,15 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
|
||||
// All checks passed - make the connection
|
||||
handleAddConnection(
|
||||
firstSelected.pathUUID,
|
||||
firstSelected.modelUUID,
|
||||
firstSelected.sphereUUID,
|
||||
pathUUID,
|
||||
modelUUID,
|
||||
sphereUUID
|
||||
);
|
||||
} else {
|
||||
// First selection - just store it
|
||||
setFirstSelected({
|
||||
pathUUID,
|
||||
modelUUID,
|
||||
sphereUUID,
|
||||
position: worldPosition,
|
||||
isCorner: isStartOrEnd
|
||||
@@ -358,7 +487,7 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
canvasElement.removeEventListener("mousemove", onMouseMove);
|
||||
canvasElement.removeEventListener("contextmenu", onContextMenu);
|
||||
};
|
||||
}, [camera, scene, raycaster, firstSelected, simulationPaths]);
|
||||
}, [camera, scene, raycaster, firstSelected, simulationStates]);
|
||||
|
||||
useFrame(() => {
|
||||
if (firstSelected) {
|
||||
@@ -372,7 +501,7 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
);
|
||||
|
||||
let point: THREE.Vector3 | null = null;
|
||||
let snappedSphere: { sphereUUID: string, position: THREE.Vector3, pathUUID: string, isCorner: boolean } | null = null;
|
||||
let snappedSphere: { sphereUUID: string, position: THREE.Vector3, modelUUID: string, isCorner: boolean } | null = null;
|
||||
let isInvalidConnection = false;
|
||||
|
||||
if (intersects.length > 0) {
|
||||
@@ -392,10 +521,10 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
const spherePosition = new THREE.Vector3();
|
||||
sphere.getWorldPosition(spherePosition);
|
||||
const pathData = sphere.userData.path;
|
||||
const pathUUID = pathData.modeluuid;
|
||||
const modelUUID = pathData.modeluuid;
|
||||
|
||||
const firstPath = simulationPaths.find(p => p.modeluuid === firstSelected.pathUUID);
|
||||
const secondPath = simulationPaths.find(p => p.modeluuid === pathUUID);
|
||||
const firstPath = simulationStates.find(p => p.modeluuid === firstSelected.modelUUID);
|
||||
const secondPath = simulationStates.find(p => p.modeluuid === modelUUID);
|
||||
const isVehicleToVehicle = firstPath?.type === 'Vehicle' && secondPath?.type === 'Vehicle';
|
||||
|
||||
// Inside the useFrame hook, where we check for snapped spheres:
|
||||
@@ -410,16 +539,16 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
!firstSelected.isCorner);
|
||||
|
||||
// Check for duplicate connection (regardless of path type)
|
||||
const isDuplicateConnection = simulationPaths.some(path => {
|
||||
if (path.modeluuid === firstSelected.pathUUID) {
|
||||
const isDuplicateConnection = simulationStates.some(path => {
|
||||
if (path.modeluuid === firstSelected.modelUUID) {
|
||||
if (path.type === 'Conveyor') {
|
||||
const point = path.points.find(p => p.uuid === firstSelected.sphereUUID);
|
||||
return point?.connections.targets.some(t =>
|
||||
t.pathUUID === pathUUID && t.pointUUID === sphereUUID
|
||||
t.modelUUID === modelUUID && t.pointUUID === sphereUUID
|
||||
);
|
||||
} else if (path.type === 'Vehicle') {
|
||||
return path.point.connections.targets.some(t =>
|
||||
t.pathUUID === pathUUID && t.pointUUID === sphereUUID
|
||||
return path.points.connections.targets.some(t =>
|
||||
t.modelUUID === modelUUID && t.pointUUID === sphereUUID
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -428,7 +557,7 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
|
||||
// For non-Vehicle paths, check if already connected
|
||||
const isNonVehicleAlreadyConnected = pathData.type !== 'Vehicle' &&
|
||||
simulationPaths.some(path => {
|
||||
simulationStates.some(path => {
|
||||
if (path.type === 'Conveyor') {
|
||||
return path.points.some(point =>
|
||||
point.uuid === sphereUUID &&
|
||||
@@ -440,7 +569,7 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
|
||||
// Check vehicle connection rules
|
||||
const isVehicleAtMaxConnections = pathData.type === 'Vehicle' &&
|
||||
pathData.point.connections.targets.length >= 1;
|
||||
pathData.points.connections.targets.length >= 1;
|
||||
const isVehicleConnectingToNonConveyor =
|
||||
(firstPath?.type === 'Vehicle' && secondPath?.type !== 'Conveyor') ||
|
||||
(secondPath?.type === 'Vehicle' && firstPath?.type !== 'Conveyor');
|
||||
@@ -452,13 +581,16 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
!isVehicleAtMaxConnections &&
|
||||
!isVehicleConnectingToNonConveyor &&
|
||||
firstSelected.sphereUUID !== sphereUUID &&
|
||||
firstSelected.pathUUID !== pathUUID &&
|
||||
(firstSelected.isCorner || isConnectable)
|
||||
firstSelected.modelUUID !== modelUUID &&
|
||||
(firstSelected.isCorner || isConnectable) &&
|
||||
!(firstPath?.type === 'Conveyor' &&
|
||||
pathData.type === 'Conveyor' &&
|
||||
!(firstSelected.isCorner && isConnectable))
|
||||
) {
|
||||
snappedSphere = {
|
||||
sphereUUID,
|
||||
position: spherePosition,
|
||||
pathUUID,
|
||||
modelUUID,
|
||||
isCorner: isConnectable
|
||||
};
|
||||
} else {
|
||||
@@ -501,12 +633,12 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
{simulationPaths.flatMap(path => {
|
||||
<group name='simulationConnectionGroup' visible={!isPlaying} >
|
||||
{simulationStates.flatMap(path => {
|
||||
if (path.type === 'Conveyor') {
|
||||
return path.points.flatMap(point =>
|
||||
point.connections.targets.map((target, index) => {
|
||||
const targetPath = simulationPaths.find(p => p.modeluuid === target.pathUUID);
|
||||
const targetPath = simulationStates.find(p => p.modeluuid === target.modelUUID);
|
||||
if (targetPath?.type === 'Vehicle') return null;
|
||||
|
||||
const fromSphere = pathsGroupRef.current?.getObjectByProperty('uuid', point.uuid);
|
||||
@@ -545,8 +677,8 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
})
|
||||
);
|
||||
} else if (path.type === 'Vehicle') {
|
||||
return path.point.connections.targets.map((target, index) => {
|
||||
const fromSphere = pathsGroupRef.current?.getObjectByProperty('uuid', path.point.uuid);
|
||||
return path.points.connections.targets.map((target, index) => {
|
||||
const fromSphere = pathsGroupRef.current?.getObjectByProperty('uuid', path.points.uuid);
|
||||
const toSphere = pathsGroupRef.current?.getObjectByProperty('uuid', target.pointUUID);
|
||||
|
||||
if (fromSphere && toSphere) {
|
||||
@@ -566,7 +698,7 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
|
||||
return (
|
||||
<QuadraticBezierLine
|
||||
key={`${path.point.uuid}-${target.pointUUID}-${index}`}
|
||||
key={`${path.points.uuid}-${target.pointUUID}-${index}`}
|
||||
start={fromWorldPosition.toArray()}
|
||||
end={toWorldPosition.toArray()}
|
||||
mid={midPoint.toArray()}
|
||||
@@ -596,7 +728,7 @@ function PathConnector({ pathsGroupRef }: { pathsGroupRef: React.MutableRefObjec
|
||||
dashScale={20}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
</group>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,362 +3,340 @@ import * as Types from "../../../types/world/worldTypes";
|
||||
import { useRef, useState, useEffect, useMemo } from "react";
|
||||
import { Sphere, TransformControls } from "@react-three/drei";
|
||||
import {
|
||||
useEditingPoint,
|
||||
useEyeDropMode,
|
||||
useIsConnecting,
|
||||
usePreviewPosition,
|
||||
useRenderDistance,
|
||||
useSelectedActionSphere,
|
||||
useSelectedPath,
|
||||
useSimulationPaths,
|
||||
useEditingPoint,
|
||||
useEyeDropMode,
|
||||
useIsConnecting,
|
||||
usePreviewPosition,
|
||||
useRenderDistance,
|
||||
useSelectedActionSphere,
|
||||
useSelectedPath,
|
||||
useSimulationStates,
|
||||
} from "../../../store/store";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import { useSubModuleStore } from "../../../store/useModuleStore";
|
||||
import { usePlayButtonStore } from "../../../store/usePlayButtonStore";
|
||||
import { setEventApi } from "../../../services/factoryBuilder/assest/floorAsset/setEventsApt";
|
||||
|
||||
function PathCreation({
|
||||
pathsGroupRef,
|
||||
}: {
|
||||
pathsGroupRef: React.MutableRefObject<THREE.Group>;
|
||||
}) {
|
||||
const { isPlaying } = usePlayButtonStore();
|
||||
const { renderDistance } = useRenderDistance();
|
||||
const { setSubModule } = useSubModuleStore();
|
||||
const { setSelectedActionSphere, selectedActionSphere } =
|
||||
useSelectedActionSphere();
|
||||
const { eyeDropMode, setEyeDropMode } = useEyeDropMode();
|
||||
const { editingPoint, setEditingPoint } = useEditingPoint();
|
||||
const { previewPosition, setPreviewPosition } = usePreviewPosition();
|
||||
const { raycaster, camera, pointer, gl } = useThree();
|
||||
const plane = useMemo(
|
||||
() => new THREE.Plane(new THREE.Vector3(0, 1, 0), 0),
|
||||
[]
|
||||
);
|
||||
const { setSelectedPath } = useSelectedPath();
|
||||
const { simulationPaths, setSimulationPaths } = useSimulationPaths();
|
||||
const { isConnecting } = useIsConnecting();
|
||||
function PathCreation({ pathsGroupRef, }: { pathsGroupRef: React.MutableRefObject<THREE.Group>; }) {
|
||||
const { isPlaying } = usePlayButtonStore();
|
||||
const { renderDistance } = useRenderDistance();
|
||||
const { setSubModule } = useSubModuleStore();
|
||||
const { setSelectedActionSphere, selectedActionSphere } = useSelectedActionSphere();
|
||||
const { eyeDropMode, setEyeDropMode } = useEyeDropMode();
|
||||
const { editingPoint, setEditingPoint } = useEditingPoint();
|
||||
const { previewPosition, setPreviewPosition } = usePreviewPosition();
|
||||
const { raycaster, camera, pointer, gl } = useThree();
|
||||
const { setSelectedPath } = useSelectedPath();
|
||||
const { simulationStates, setSimulationStates } = useSimulationStates();
|
||||
const { isConnecting } = useIsConnecting();
|
||||
const plane = useMemo(() => new THREE.Plane(new THREE.Vector3(0, 1, 0), 0), []);
|
||||
|
||||
const groupRefs = useRef<{ [key: string]: THREE.Group }>({});
|
||||
const sphereRefs = useRef<{ [key: string]: THREE.Mesh }>({});
|
||||
const isMovingRef = useRef(false);
|
||||
const transformRef = useRef<any>(null);
|
||||
const [transformMode, setTransformMode] = useState<
|
||||
"translate" | "rotate" | null
|
||||
>(null);
|
||||
const groupRefs = useRef<{ [key: string]: THREE.Group }>({});
|
||||
const sphereRefs = useRef<{ [key: string]: THREE.Mesh }>({});
|
||||
const isMovingRef = useRef(false);
|
||||
const transformRef = useRef<any>(null);
|
||||
const [transformMode, setTransformMode] = useState<"translate" | "rotate" | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
setTransformMode(null);
|
||||
const handleKeyDown = (e: KeyboardEvent) => {
|
||||
if (!selectedActionSphere) return;
|
||||
if (e.key === "g") {
|
||||
setTransformMode((prev) => (prev === "translate" ? null : "translate"));
|
||||
}
|
||||
if (e.key === "r") {
|
||||
setTransformMode((prev) => (prev === "rotate" ? null : "rotate"));
|
||||
}
|
||||
};
|
||||
useEffect(() => {
|
||||
setTransformMode(null);
|
||||
const handleKeyDown = (e: KeyboardEvent) => {
|
||||
if (!selectedActionSphere) return;
|
||||
if (e.key === "g") {
|
||||
setTransformMode((prev) => (prev === "translate" ? null : "translate"));
|
||||
}
|
||||
if (e.key === "r") {
|
||||
setTransformMode((prev) => (prev === "rotate" ? null : "rotate"));
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("keydown", handleKeyDown);
|
||||
return () => window.removeEventListener("keydown", handleKeyDown);
|
||||
}, [selectedActionSphere]);
|
||||
window.addEventListener("keydown", handleKeyDown);
|
||||
return () => window.removeEventListener("keydown", handleKeyDown);
|
||||
}, [selectedActionSphere]);
|
||||
|
||||
useFrame(() => {
|
||||
Object.values(groupRefs.current).forEach((group) => {
|
||||
if (group) {
|
||||
const distance = new THREE.Vector3(
|
||||
...group.position.toArray()
|
||||
).distanceTo(camera.position);
|
||||
group.visible = ((distance <= renderDistance) && !isPlaying);
|
||||
}
|
||||
});
|
||||
});
|
||||
useFrame(() => {
|
||||
Object.values(groupRefs.current).forEach((group) => {
|
||||
if (group) {
|
||||
const distance = new THREE.Vector3(
|
||||
...group.position.toArray()
|
||||
).distanceTo(camera.position);
|
||||
group.visible = ((distance <= renderDistance) && !isPlaying);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const updateSimulationPaths = () => {
|
||||
if (!selectedActionSphere) return;
|
||||
const updateSimulationPaths = () => {
|
||||
if (!selectedActionSphere) return;
|
||||
|
||||
const updatedPaths = simulationPaths.map((path) => {
|
||||
if (path.type === "Conveyor") {
|
||||
return {
|
||||
...path,
|
||||
points: path.points.map((point) =>
|
||||
point.uuid === selectedActionSphere.point.uuid
|
||||
? {
|
||||
...point,
|
||||
position: [
|
||||
selectedActionSphere.point.position.x,
|
||||
selectedActionSphere.point.position.y,
|
||||
selectedActionSphere.point.position.z,
|
||||
],
|
||||
rotation: [
|
||||
selectedActionSphere.point.rotation.x,
|
||||
selectedActionSphere.point.rotation.y,
|
||||
selectedActionSphere.point.rotation.z,
|
||||
],
|
||||
}
|
||||
: point
|
||||
),
|
||||
};
|
||||
}
|
||||
return path;
|
||||
}) as Types.ConveyorEventsSchema[];
|
||||
const updatedPaths = simulationStates.map((path) => {
|
||||
if (path.type === "Conveyor") {
|
||||
return {
|
||||
...path,
|
||||
points: path.points.map((point) =>
|
||||
point.uuid === selectedActionSphere.points.uuid
|
||||
? {
|
||||
...point,
|
||||
position: [
|
||||
selectedActionSphere.points.position.x,
|
||||
selectedActionSphere.points.position.y,
|
||||
selectedActionSphere.points.position.z,
|
||||
],
|
||||
rotation: [
|
||||
selectedActionSphere.points.rotation.x,
|
||||
selectedActionSphere.points.rotation.y,
|
||||
selectedActionSphere.points.rotation.z,
|
||||
],
|
||||
}
|
||||
: point
|
||||
),
|
||||
};
|
||||
} else {
|
||||
return path;
|
||||
}
|
||||
}) as Types.ConveyorEventsSchema[];
|
||||
|
||||
setSimulationPaths(updatedPaths);
|
||||
};
|
||||
const updatedPath = updatedPaths.find(
|
||||
(path) => path.type === "Conveyor" && path.points.some((point) => point.uuid === selectedActionSphere.points.uuid)
|
||||
);
|
||||
|
||||
useFrame(() => {
|
||||
if (eyeDropMode) {
|
||||
raycaster.setFromCamera(pointer, camera);
|
||||
const intersectionPoint = new THREE.Vector3();
|
||||
const point = raycaster.ray.intersectPlane(plane, intersectionPoint);
|
||||
// console.log("Updated Path:", updatedPath);
|
||||
|
||||
if (point) {
|
||||
setPreviewPosition({ x: point.x, y: point.z });
|
||||
}
|
||||
} else {
|
||||
setPreviewPosition(null);
|
||||
}
|
||||
});
|
||||
setSimulationStates(updatedPaths);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!camera) return;
|
||||
const canvasElement = gl.domElement;
|
||||
canvasElement.tabIndex = 0;
|
||||
useFrame(() => {
|
||||
if (eyeDropMode) {
|
||||
raycaster.setFromCamera(pointer, camera);
|
||||
const intersectionPoint = new THREE.Vector3();
|
||||
const point = raycaster.ray.intersectPlane(plane, intersectionPoint);
|
||||
|
||||
const onPointerDown = () => {
|
||||
isMovingRef.current = false;
|
||||
};
|
||||
if (point) {
|
||||
setPreviewPosition({ x: point.x, y: point.z });
|
||||
}
|
||||
} else {
|
||||
setPreviewPosition(null);
|
||||
}
|
||||
});
|
||||
|
||||
const onPointerMove = () => {
|
||||
isMovingRef.current = true;
|
||||
};
|
||||
useEffect(() => {
|
||||
if (!camera) return;
|
||||
const canvasElement = gl.domElement;
|
||||
canvasElement.tabIndex = 0;
|
||||
|
||||
const onPointerUp = (event: PointerEvent) => {
|
||||
if (
|
||||
!isMovingRef.current &&
|
||||
eyeDropMode &&
|
||||
event.button === 0 &&
|
||||
previewPosition
|
||||
) {
|
||||
event.preventDefault();
|
||||
if (editingPoint) {
|
||||
handlePointUpdate(editingPoint, previewPosition.x, previewPosition.y);
|
||||
setEditingPoint(null);
|
||||
setEyeDropMode(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
const onPointerDown = () => {
|
||||
isMovingRef.current = false;
|
||||
};
|
||||
|
||||
if (eyeDropMode) {
|
||||
canvasElement.addEventListener("pointerdown", onPointerDown);
|
||||
canvasElement.addEventListener("pointermove", onPointerMove);
|
||||
canvasElement.addEventListener("pointerup", onPointerUp);
|
||||
}
|
||||
const onPointerMove = () => {
|
||||
isMovingRef.current = true;
|
||||
};
|
||||
|
||||
return () => {
|
||||
canvasElement.removeEventListener("pointerdown", onPointerDown);
|
||||
canvasElement.removeEventListener("pointermove", onPointerMove);
|
||||
canvasElement.removeEventListener("pointerup", onPointerUp);
|
||||
};
|
||||
}, [eyeDropMode, editingPoint, previewPosition]);
|
||||
const onPointerUp = (event: PointerEvent) => {
|
||||
if (
|
||||
!isMovingRef.current &&
|
||||
eyeDropMode &&
|
||||
event.button === 0 &&
|
||||
previewPosition
|
||||
) {
|
||||
event.preventDefault();
|
||||
if (editingPoint) {
|
||||
handlePointUpdate(editingPoint, previewPosition.x, previewPosition.y);
|
||||
setEditingPoint(null);
|
||||
setEyeDropMode(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handlePointUpdate = (
|
||||
pointType: "start" | "end",
|
||||
x: number,
|
||||
z: number
|
||||
) => {
|
||||
if (!selectedActionSphere?.point?.uuid) return;
|
||||
if (eyeDropMode) {
|
||||
canvasElement.addEventListener("pointerdown", onPointerDown);
|
||||
canvasElement.addEventListener("pointermove", onPointerMove);
|
||||
canvasElement.addEventListener("pointerup", onPointerUp);
|
||||
}
|
||||
|
||||
const updatedPaths = simulationPaths.map((path) => {
|
||||
if (
|
||||
path.type === "Vehicle" &&
|
||||
path.point.uuid === selectedActionSphere.point.uuid
|
||||
) {
|
||||
return {
|
||||
...path,
|
||||
point: {
|
||||
...path.point,
|
||||
actions: {
|
||||
...path.point.actions,
|
||||
[pointType]: {
|
||||
...path.point.actions[pointType],
|
||||
x: x,
|
||||
y: z,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
return path;
|
||||
});
|
||||
return () => {
|
||||
canvasElement.removeEventListener("pointerdown", onPointerDown);
|
||||
canvasElement.removeEventListener("pointermove", onPointerMove);
|
||||
canvasElement.removeEventListener("pointerup", onPointerUp);
|
||||
};
|
||||
}, [eyeDropMode, editingPoint, previewPosition]);
|
||||
|
||||
setSimulationPaths(updatedPaths);
|
||||
};
|
||||
const updateBackend = async (updatedPath: Types.VehicleEventsSchema | undefined) => {
|
||||
if (!updatedPath) return;
|
||||
const email = localStorage.getItem("email");
|
||||
const organization = email ? email.split("@")[1].split(".")[0] : "";
|
||||
await setEventApi(
|
||||
organization,
|
||||
updatedPath.modeluuid,
|
||||
{ type: "Vehicle", points: updatedPath.points }
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<group visible={!isPlaying} name="simulation-simulationPaths-group" ref={pathsGroupRef}>
|
||||
{simulationPaths.map((path) => {
|
||||
if (path.type === "Conveyor") {
|
||||
const points = path.points.map(
|
||||
(point) => new THREE.Vector3(...point.position)
|
||||
);
|
||||
const handlePointUpdate = (pointType: "start" | "end", x: number, z: number) => {
|
||||
if (!selectedActionSphere?.points?.uuid) return;
|
||||
const updatedPaths = simulationStates.map((path) => {
|
||||
|
||||
return (
|
||||
<group
|
||||
name={`${path.modeluuid}-event-path`}
|
||||
key={path.modeluuid}
|
||||
ref={(el) => (groupRefs.current[path.modeluuid] = el!)}
|
||||
position={path.position}
|
||||
rotation={path.rotation}
|
||||
onClick={(e) => {
|
||||
if (isConnecting || eyeDropMode) return;
|
||||
e.stopPropagation();
|
||||
setSelectedPath({
|
||||
path,
|
||||
group: groupRefs.current[path.modeluuid],
|
||||
});
|
||||
setSelectedActionSphere(null);
|
||||
setTransformMode(null);
|
||||
setSubModule("mechanics");
|
||||
}}
|
||||
onPointerMissed={() => {
|
||||
if (eyeDropMode) return;
|
||||
setSelectedPath(null);
|
||||
setSubModule("properties");
|
||||
}}
|
||||
>
|
||||
{path.points.map((point, index) => (
|
||||
<Sphere
|
||||
key={point.uuid}
|
||||
uuid={point.uuid}
|
||||
position={point.position}
|
||||
args={[0.15, 32, 32]}
|
||||
name="events-sphere"
|
||||
ref={(el) => (sphereRefs.current[point.uuid] = el!)}
|
||||
onClick={(e) => {
|
||||
if (isConnecting || eyeDropMode) return;
|
||||
e.stopPropagation();
|
||||
setSelectedActionSphere({
|
||||
path,
|
||||
point: sphereRefs.current[point.uuid],
|
||||
});
|
||||
setSubModule("mechanics");
|
||||
setSelectedPath(null);
|
||||
}}
|
||||
userData={{ point, path }}
|
||||
onPointerMissed={() => {
|
||||
if (eyeDropMode) return;
|
||||
setSubModule("properties");
|
||||
setSelectedActionSphere(null);
|
||||
}}
|
||||
>
|
||||
<meshStandardMaterial
|
||||
color={
|
||||
index === 0
|
||||
? "orange"
|
||||
: index === path.points.length - 1
|
||||
? "blue"
|
||||
: "green"
|
||||
}
|
||||
/>
|
||||
</Sphere>
|
||||
))}
|
||||
if (path.type === "Vehicle" && path.points.uuid === selectedActionSphere.points.uuid) {
|
||||
return {
|
||||
...path,
|
||||
points: {
|
||||
...path.points,
|
||||
actions: {
|
||||
...path.points.actions,
|
||||
[pointType]: { ...path.points.actions[pointType], x: x, y: z, },
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
return path;
|
||||
});
|
||||
|
||||
{points.slice(0, -1).map((point, index) => {
|
||||
const nextPoint = points[index + 1];
|
||||
const segmentCurve = new THREE.CatmullRomCurve3([
|
||||
point,
|
||||
nextPoint,
|
||||
]);
|
||||
const tubeGeometry = new THREE.TubeGeometry(
|
||||
segmentCurve,
|
||||
20,
|
||||
0.1,
|
||||
16,
|
||||
false
|
||||
);
|
||||
const updatedPath = updatedPaths.find((path): path is Types.VehicleEventsSchema => path.type === "Vehicle" && path.points.uuid === selectedActionSphere.points.uuid);
|
||||
updateBackend(updatedPath);
|
||||
|
||||
return (
|
||||
<mesh
|
||||
name="event-connection-tube"
|
||||
key={`tube-${index}`}
|
||||
geometry={tubeGeometry}
|
||||
>
|
||||
<meshStandardMaterial
|
||||
transparent
|
||||
opacity={0.9}
|
||||
color="red"
|
||||
/>
|
||||
</mesh>
|
||||
);
|
||||
})}
|
||||
</group>
|
||||
);
|
||||
} else if (path.type === "Vehicle") {
|
||||
return (
|
||||
<group
|
||||
name={`${path.modeluuid}-vehicle-path`}
|
||||
key={path.modeluuid}
|
||||
ref={(el) => (groupRefs.current[path.modeluuid] = el!)}
|
||||
position={path.position}
|
||||
onClick={(e) => {
|
||||
if (isConnecting || eyeDropMode) return;
|
||||
e.stopPropagation();
|
||||
setSelectedPath({
|
||||
path,
|
||||
group: groupRefs.current[path.modeluuid],
|
||||
});
|
||||
setSelectedActionSphere(null);
|
||||
setTransformMode(null);
|
||||
setSubModule("mechanics");
|
||||
}}
|
||||
onPointerMissed={() => {
|
||||
if (eyeDropMode) return;
|
||||
setSelectedPath(null);
|
||||
setSubModule("properties");
|
||||
}}
|
||||
>
|
||||
<Sphere
|
||||
key={path.point.uuid}
|
||||
uuid={path.point.uuid}
|
||||
position={path.point.position}
|
||||
args={[0.15, 32, 32]}
|
||||
name="events-sphere"
|
||||
ref={(el) => (sphereRefs.current[path.point.uuid] = el!)}
|
||||
onClick={(e) => {
|
||||
if (isConnecting || eyeDropMode) return;
|
||||
e.stopPropagation();
|
||||
setSelectedActionSphere({
|
||||
path,
|
||||
point: sphereRefs.current[path.point.uuid],
|
||||
});
|
||||
setSubModule("mechanics");
|
||||
setSelectedPath(null);
|
||||
}}
|
||||
userData={{ point: path.point, path }}
|
||||
onPointerMissed={() => {
|
||||
if (eyeDropMode) return;
|
||||
setSubModule("properties");
|
||||
setSelectedActionSphere(null);
|
||||
}}
|
||||
>
|
||||
<meshStandardMaterial color="purple" />
|
||||
</Sphere>
|
||||
</group>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
})}
|
||||
setSimulationStates(updatedPaths);
|
||||
};
|
||||
|
||||
{selectedActionSphere && transformMode && (
|
||||
<TransformControls
|
||||
ref={transformRef}
|
||||
object={selectedActionSphere.point}
|
||||
mode={transformMode}
|
||||
onMouseUp={updateSimulationPaths}
|
||||
/>
|
||||
)}
|
||||
</group>
|
||||
);
|
||||
return (
|
||||
<group visible={!isPlaying} name="simulation-simulationStates-group" ref={pathsGroupRef}>
|
||||
{simulationStates.map((path) => {
|
||||
if (path.type === "Conveyor") {
|
||||
const points = path.points.map(
|
||||
(point) => new THREE.Vector3(...point.position)
|
||||
);
|
||||
|
||||
return (
|
||||
<group
|
||||
name={`${path.modeluuid}-${path.type}-path`}
|
||||
key={path.modeluuid}
|
||||
ref={(el) => (groupRefs.current[path.modeluuid] = el!)}
|
||||
position={path.position}
|
||||
rotation={path.rotation}
|
||||
onClick={(e) => {
|
||||
if (isConnecting || eyeDropMode) return;
|
||||
e.stopPropagation();
|
||||
setSelectedPath({
|
||||
path,
|
||||
group: groupRefs.current[path.modeluuid],
|
||||
});
|
||||
setSelectedActionSphere(null);
|
||||
setTransformMode(null);
|
||||
setSubModule("mechanics");
|
||||
}}
|
||||
onPointerMissed={() => {
|
||||
if (eyeDropMode) return;
|
||||
setSelectedPath(null);
|
||||
setSubModule("properties");
|
||||
}}
|
||||
>
|
||||
{path.points.map((point, index) => (
|
||||
<Sphere
|
||||
key={point.uuid}
|
||||
uuid={point.uuid}
|
||||
position={point.position}
|
||||
args={[0.15, 32, 32]}
|
||||
name="events-sphere"
|
||||
ref={(el) => (sphereRefs.current[point.uuid] = el!)}
|
||||
onClick={(e) => {
|
||||
if (isConnecting || eyeDropMode) return;
|
||||
e.stopPropagation();
|
||||
setSelectedActionSphere({
|
||||
path,
|
||||
points: sphereRefs.current[point.uuid],
|
||||
});
|
||||
setSubModule("mechanics");
|
||||
setSelectedPath(null);
|
||||
}}
|
||||
userData={{ points, path }}
|
||||
onPointerMissed={() => {
|
||||
if (eyeDropMode) return;
|
||||
setSubModule("properties");
|
||||
setSelectedActionSphere(null);
|
||||
}}
|
||||
>
|
||||
<meshStandardMaterial
|
||||
color={index === 0 ? "orange" : index === path.points.length - 1 ? "blue" : "green"}
|
||||
/>
|
||||
</Sphere>
|
||||
))}
|
||||
|
||||
{points.slice(0, -1).map((point, index) => {
|
||||
const nextPoint = points[index + 1];
|
||||
const segmentCurve = new THREE.CatmullRomCurve3([point, nextPoint,]);
|
||||
const tubeGeometry = new THREE.TubeGeometry(segmentCurve, 20, 0.1, 16, false);
|
||||
|
||||
return (
|
||||
<mesh name="event-connection-tube" key={`tube-${index}`} geometry={tubeGeometry}>
|
||||
<meshStandardMaterial transparent opacity={0.9} color="red" />
|
||||
</mesh>
|
||||
);
|
||||
})}
|
||||
</group>
|
||||
);
|
||||
} else if (path.type === "Vehicle" || path.type === "StaticMachine") {
|
||||
return (
|
||||
<group
|
||||
name={`${path.modeluuid}-${path.type}-path`}
|
||||
key={path.modeluuid}
|
||||
ref={(el) => (groupRefs.current[path.modeluuid] = el!)}
|
||||
position={path.position}
|
||||
onClick={(e) => {
|
||||
if (isConnecting || eyeDropMode) return;
|
||||
e.stopPropagation();
|
||||
setSelectedPath({
|
||||
path,
|
||||
group: groupRefs.current[path.modeluuid],
|
||||
});
|
||||
setSelectedActionSphere(null);
|
||||
setTransformMode(null);
|
||||
setSubModule("mechanics");
|
||||
}}
|
||||
onPointerMissed={() => {
|
||||
if (eyeDropMode) return;
|
||||
setSelectedPath(null);
|
||||
setSubModule("properties");
|
||||
}}
|
||||
>
|
||||
<Sphere
|
||||
key={path.points.uuid}
|
||||
uuid={path.points.uuid}
|
||||
position={path.points.position}
|
||||
args={[0.15, 32, 32]}
|
||||
name="events-sphere"
|
||||
ref={(el) => (sphereRefs.current[path.points.uuid] = el!)}
|
||||
onClick={(e) => {
|
||||
if (isConnecting || eyeDropMode) return;
|
||||
e.stopPropagation();
|
||||
setSelectedActionSphere({
|
||||
path,
|
||||
points: sphereRefs.current[path.points.uuid],
|
||||
});
|
||||
setSubModule("mechanics");
|
||||
setSelectedPath(null);
|
||||
}}
|
||||
userData={{ points: path.points, path }}
|
||||
onPointerMissed={() => {
|
||||
if (eyeDropMode) return;
|
||||
setSubModule("properties");
|
||||
setSelectedActionSphere(null);
|
||||
}}
|
||||
>
|
||||
<meshStandardMaterial color="purple" />
|
||||
</Sphere>
|
||||
</group>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
})}
|
||||
|
||||
{selectedActionSphere && transformMode && (
|
||||
<TransformControls
|
||||
ref={transformRef}
|
||||
object={selectedActionSphere.points}
|
||||
mode={transformMode}
|
||||
onMouseUp={updateSimulationPaths}
|
||||
/>
|
||||
)}
|
||||
</group>
|
||||
);
|
||||
}
|
||||
|
||||
export default PathCreation;
|
||||
|
||||
@@ -1,916 +0,0 @@
|
||||
// // animation-worker.js
|
||||
// // This web worker handles animation calculations off the main thread
|
||||
|
||||
// /* eslint-disable no-restricted-globals */
|
||||
// // The above disables the ESLint rule for this file since 'self' is valid in web workers
|
||||
|
||||
// // Store process data, animation states, and objects
|
||||
// let processes = [];
|
||||
// let animationStates = {};
|
||||
// let lastTimestamp = 0;
|
||||
|
||||
// // Message handler for communication with main thread
|
||||
// self.onmessage = function (event) {
|
||||
// const { type, data } = event.data;
|
||||
|
||||
// switch (type) {
|
||||
// case "initialize":
|
||||
// processes = data.processes;
|
||||
// initializeAnimationStates();
|
||||
// break;
|
||||
|
||||
// case "update":
|
||||
// const { timestamp, isPlaying } = data;
|
||||
// if (isPlaying) {
|
||||
// const delta = (timestamp - lastTimestamp) / 1000; // Convert to seconds
|
||||
// updateAnimations(delta, timestamp);
|
||||
// }
|
||||
// lastTimestamp = timestamp;
|
||||
// break;
|
||||
|
||||
// case "reset":
|
||||
// resetAnimations();
|
||||
// break;
|
||||
|
||||
// case "togglePlay":
|
||||
// // If resuming from pause, recalculate the time delta
|
||||
// lastTimestamp = data.timestamp;
|
||||
// break;
|
||||
// }
|
||||
// };
|
||||
|
||||
// // Initialize animation states for all processes
|
||||
// function initializeAnimationStates() {
|
||||
// animationStates = {};
|
||||
|
||||
// processes.forEach((process) => {
|
||||
// animationStates[process.id] = {
|
||||
// spawnedObjects: {},
|
||||
// nextSpawnTime: 0,
|
||||
// objectIdCounter: 0,
|
||||
// };
|
||||
// });
|
||||
|
||||
// // Send initial states back to main thread
|
||||
// self.postMessage({
|
||||
// type: "statesInitialized",
|
||||
// data: { animationStates },
|
||||
// });
|
||||
// }
|
||||
|
||||
// // Reset all animations
|
||||
// function resetAnimations() {
|
||||
// initializeAnimationStates();
|
||||
// }
|
||||
|
||||
// // Find spawn point in a process
|
||||
// function findSpawnPoint(process) {
|
||||
// for (const path of process.paths || []) {
|
||||
// for (const point of path.points || []) {
|
||||
// const spawnAction = point.actions?.find(
|
||||
// (a) => a.isUsed && a.type === "Spawn"
|
||||
// );
|
||||
// if (spawnAction) {
|
||||
// return { point, path };
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
// // Create a new spawned object with proper initial position
|
||||
// function createSpawnedObject(process, spawnPoint, currentTime, materialType) {
|
||||
// // Extract spawn position from the actual spawn point
|
||||
// const position = spawnPoint.point.position
|
||||
// ? [...spawnPoint.point.position]
|
||||
// : [0, 0, 0];
|
||||
|
||||
// // Get the path position and add it to the spawn point position
|
||||
// const pathPosition = spawnPoint.path.pathPosition || [0, 0, 0];
|
||||
// const absolutePosition = [
|
||||
// position[0] + pathPosition[0],
|
||||
// position[1] + pathPosition[1],
|
||||
// position[2] + pathPosition[2],
|
||||
// ];
|
||||
|
||||
// return {
|
||||
// id: `obj-${process.id}-${animationStates[process.id].objectIdCounter}`,
|
||||
// position: absolutePosition,
|
||||
// state: {
|
||||
// currentIndex: 0,
|
||||
// progress: 0,
|
||||
// isAnimating: true,
|
||||
// speed: process.speed || 1,
|
||||
// isDelaying: false,
|
||||
// delayStartTime: 0,
|
||||
// currentDelayDuration: 0,
|
||||
// delayComplete: false,
|
||||
// currentPathIndex: 0,
|
||||
// // Store the spawn point index to start animation from correct path point
|
||||
// spawnPointIndex: getPointIndexInProcess(process, spawnPoint.point),
|
||||
// },
|
||||
// visible: true,
|
||||
// materialType: materialType || "Default",
|
||||
// spawnTime: currentTime,
|
||||
// };
|
||||
// }
|
||||
|
||||
// // Get the index of a point within the process animation path
|
||||
// function getPointIndexInProcess(process, point) {
|
||||
// if (!process.paths) return 0;
|
||||
|
||||
// let cumulativePoints = 0;
|
||||
// for (const path of process.paths) {
|
||||
// for (let i = 0; i < (path.points?.length || 0); i++) {
|
||||
// if (path.points[i].uuid === point.uuid) {
|
||||
// return cumulativePoints + i;
|
||||
// }
|
||||
// }
|
||||
// cumulativePoints += path.points?.length || 0;
|
||||
// }
|
||||
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// // Get point data for current animation index
|
||||
// function getPointDataForAnimationIndex(process, index) {
|
||||
// if (!process.paths) return null;
|
||||
|
||||
// let cumulativePoints = 0;
|
||||
// for (const path of process.paths) {
|
||||
// const pointCount = path.points?.length || 0;
|
||||
|
||||
// if (index < cumulativePoints + pointCount) {
|
||||
// const pointIndex = index - cumulativePoints;
|
||||
// return path.points?.[pointIndex] || null;
|
||||
// }
|
||||
|
||||
// cumulativePoints += pointCount;
|
||||
// }
|
||||
|
||||
// return null;
|
||||
// }
|
||||
|
||||
// // Convert process paths to Vector3 format
|
||||
// function getProcessPath(process) {
|
||||
// return process.animationPath?.map((p) => ({ x: p.x, y: p.y, z: p.z })) || [];
|
||||
// }
|
||||
|
||||
// // Handle material swap for an object
|
||||
// function handleMaterialSwap(processId, objectId, materialType) {
|
||||
// const processState = animationStates[processId];
|
||||
// if (!processState || !processState.spawnedObjects[objectId]) return;
|
||||
|
||||
// processState.spawnedObjects[objectId].materialType = materialType;
|
||||
|
||||
// // Notify main thread about material change
|
||||
// self.postMessage({
|
||||
// type: "materialChanged",
|
||||
// data: {
|
||||
// processId,
|
||||
// objectId,
|
||||
// materialType,
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
|
||||
// // Handle point actions for an object
|
||||
// function handlePointActions(processId, objectId, actions = [], currentTime) {
|
||||
// let shouldStopAnimation = false;
|
||||
// const processState = animationStates[processId];
|
||||
|
||||
// if (!processState || !processState.spawnedObjects[objectId]) return false;
|
||||
|
||||
// const objectState = processState.spawnedObjects[objectId];
|
||||
|
||||
// actions.forEach((action) => {
|
||||
// if (!action.isUsed) return;
|
||||
|
||||
// switch (action.type) {
|
||||
// case "Delay":
|
||||
// if (objectState.state.isDelaying) return;
|
||||
|
||||
// const delayDuration =
|
||||
// typeof action.delay === "number"
|
||||
// ? action.delay
|
||||
// : parseFloat(action.delay || "0");
|
||||
|
||||
// if (delayDuration > 0) {
|
||||
// objectState.state.isDelaying = true;
|
||||
// objectState.state.delayStartTime = currentTime;
|
||||
// objectState.state.currentDelayDuration = delayDuration;
|
||||
// objectState.state.delayComplete = false;
|
||||
// shouldStopAnimation = true;
|
||||
// }
|
||||
// break;
|
||||
|
||||
// case "Despawn":
|
||||
// delete processState.spawnedObjects[objectId];
|
||||
// shouldStopAnimation = true;
|
||||
|
||||
// // Notify main thread about despawn
|
||||
// self.postMessage({
|
||||
// type: "objectDespawned",
|
||||
// data: {
|
||||
// processId,
|
||||
// objectId,
|
||||
// },
|
||||
// });
|
||||
// break;
|
||||
|
||||
// case "Swap":
|
||||
// if (action.material) {
|
||||
// handleMaterialSwap(processId, objectId, action.material);
|
||||
// }
|
||||
// break;
|
||||
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// });
|
||||
|
||||
// return shouldStopAnimation;
|
||||
// }
|
||||
|
||||
// // Check if point has non-inherit actions
|
||||
// function hasNonInheritActions(actions = []) {
|
||||
// return actions.some((action) => action.isUsed && action.type !== "Inherit");
|
||||
// }
|
||||
|
||||
// // Calculate vector lerp (linear interpolation)
|
||||
// function lerpVectors(v1, v2, alpha) {
|
||||
// return {
|
||||
// x: v1.x + (v2.x - v1.x) * alpha,
|
||||
// y: v1.y + (v2.y - v1.y) * alpha,
|
||||
// z: v1.z + (v2.z - v1.z) * alpha,
|
||||
// };
|
||||
// }
|
||||
|
||||
// // Calculate vector distance
|
||||
// function distanceBetweenVectors(v1, v2) {
|
||||
// const dx = v2.x - v1.x;
|
||||
// const dy = v2.y - v1.y;
|
||||
// const dz = v2.z - v1.z;
|
||||
// return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
||||
// }
|
||||
|
||||
// // Process spawn logic
|
||||
// function processSpawns(currentTime) {
|
||||
// processes.forEach((process) => {
|
||||
// const processState = animationStates[process.id];
|
||||
// if (!processState) return;
|
||||
|
||||
// const spawnPointData = findSpawnPoint(process);
|
||||
// if (!spawnPointData || !spawnPointData.point.actions) return;
|
||||
|
||||
// const spawnAction = spawnPointData.point.actions.find(
|
||||
// (a) => a.isUsed && a.type === "Spawn"
|
||||
// );
|
||||
// if (!spawnAction) return;
|
||||
|
||||
// const spawnInterval =
|
||||
// typeof spawnAction.spawnInterval === "number"
|
||||
// ? spawnAction.spawnInterval
|
||||
// : parseFloat(spawnAction.spawnInterval || "0");
|
||||
|
||||
// if (currentTime >= processState.nextSpawnTime) {
|
||||
// const newObject = createSpawnedObject(
|
||||
// process,
|
||||
// spawnPointData,
|
||||
// currentTime,
|
||||
// spawnAction.material || "Default"
|
||||
// );
|
||||
|
||||
// processState.spawnedObjects[newObject.id] = newObject;
|
||||
// processState.objectIdCounter++;
|
||||
// processState.nextSpawnTime = currentTime + spawnInterval;
|
||||
|
||||
// // Notify main thread about new object
|
||||
// self.postMessage({
|
||||
// type: "objectSpawned",
|
||||
// data: {
|
||||
// processId: process.id,
|
||||
// object: newObject,
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
// // Update all animations
|
||||
// function updateAnimations(delta, currentTime) {
|
||||
// // First handle spawning of new objects
|
||||
// processSpawns(currentTime);
|
||||
|
||||
// // Then animate existing objects
|
||||
// processes.forEach((process) => {
|
||||
// const processState = animationStates[process.id];
|
||||
// if (!processState) return;
|
||||
|
||||
// const path = getProcessPath(process);
|
||||
// if (path.length < 2) return;
|
||||
|
||||
// const updatedObjects = {};
|
||||
// let hasChanges = false;
|
||||
|
||||
// Object.entries(processState.spawnedObjects).forEach(([objectId, obj]) => {
|
||||
// if (!obj.visible || !obj.state.isAnimating) return;
|
||||
|
||||
// const stateRef = obj.state;
|
||||
|
||||
// // Use the spawnPointIndex as starting point if it's the initial movement
|
||||
// if (stateRef.currentIndex === 0 && stateRef.progress === 0) {
|
||||
// stateRef.currentIndex = stateRef.spawnPointIndex || 0;
|
||||
// }
|
||||
|
||||
// // Get current point data
|
||||
// const currentPointData = getPointDataForAnimationIndex(
|
||||
// process,
|
||||
// stateRef.currentIndex
|
||||
// );
|
||||
|
||||
// // Execute actions when arriving at a new point
|
||||
// if (stateRef.progress === 0 && currentPointData?.actions) {
|
||||
// const shouldStop = handlePointActions(
|
||||
// process.id,
|
||||
// objectId,
|
||||
// currentPointData.actions,
|
||||
// currentTime
|
||||
// );
|
||||
// if (shouldStop) return;
|
||||
// }
|
||||
|
||||
// // Handle delays
|
||||
// if (stateRef.isDelaying) {
|
||||
// if (
|
||||
// currentTime - stateRef.delayStartTime >=
|
||||
// stateRef.currentDelayDuration
|
||||
// ) {
|
||||
// stateRef.isDelaying = false;
|
||||
// stateRef.delayComplete = true;
|
||||
// } else {
|
||||
// updatedObjects[objectId] = { ...obj, state: { ...stateRef } };
|
||||
// return; // Keep waiting
|
||||
// }
|
||||
// }
|
||||
|
||||
// const nextPointIdx = stateRef.currentIndex + 1;
|
||||
// const isLastPoint = nextPointIdx >= path.length;
|
||||
|
||||
// if (isLastPoint) {
|
||||
// if (currentPointData?.actions) {
|
||||
// const shouldStop = !hasNonInheritActions(currentPointData.actions);
|
||||
// if (shouldStop) {
|
||||
// // Reached the end of path with no more actions
|
||||
// delete processState.spawnedObjects[objectId];
|
||||
|
||||
// // Notify main thread to remove the object
|
||||
// self.postMessage({
|
||||
// type: "objectCompleted",
|
||||
// data: {
|
||||
// processId: process.id,
|
||||
// objectId,
|
||||
// },
|
||||
// });
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (!isLastPoint) {
|
||||
// const currentPos = path[stateRef.currentIndex];
|
||||
// const nextPos = path[nextPointIdx];
|
||||
// const distance = distanceBetweenVectors(currentPos, nextPos);
|
||||
// const movement = stateRef.speed * delta;
|
||||
|
||||
// // Update progress based on distance and speed
|
||||
// const oldProgress = stateRef.progress;
|
||||
// stateRef.progress += movement / distance;
|
||||
|
||||
// if (stateRef.progress >= 1) {
|
||||
// // Reached next point
|
||||
// stateRef.currentIndex = nextPointIdx;
|
||||
// stateRef.progress = 0;
|
||||
// stateRef.delayComplete = false;
|
||||
// obj.position = [nextPos.x, nextPos.y, nextPos.z];
|
||||
// } else {
|
||||
// // Interpolate position
|
||||
// const lerpedPos = lerpVectors(currentPos, nextPos, stateRef.progress);
|
||||
// obj.position = [lerpedPos.x, lerpedPos.y, lerpedPos.z];
|
||||
// }
|
||||
|
||||
// // Only send updates when there's meaningful movement
|
||||
// if (Math.abs(oldProgress - stateRef.progress) > 0.01) {
|
||||
// hasChanges = true;
|
||||
// }
|
||||
// }
|
||||
|
||||
// updatedObjects[objectId] = { ...obj, state: { ...stateRef } };
|
||||
// });
|
||||
|
||||
// // Update animation state with modified objects
|
||||
// if (Object.keys(updatedObjects).length > 0) {
|
||||
// processState.spawnedObjects = {
|
||||
// ...processState.spawnedObjects,
|
||||
// ...updatedObjects,
|
||||
// };
|
||||
|
||||
// // Only send position updates when there are meaningful changes
|
||||
// if (hasChanges) {
|
||||
// self.postMessage({
|
||||
// type: "positionsUpdated",
|
||||
// data: {
|
||||
// processId: process.id,
|
||||
// objects: updatedObjects,
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
// animation-worker.js
|
||||
// This web worker handles animation calculations off the main thread
|
||||
|
||||
/* eslint-disable no-restricted-globals */
|
||||
// The above disables the ESLint rule for this file since 'self' is valid in web workers
|
||||
|
||||
// Store process data, animation states, and objects
|
||||
let processes = [];
|
||||
let animationStates = {};
|
||||
let lastTimestamp = 0;
|
||||
let debugMode = true;
|
||||
|
||||
// Logger function for debugging
|
||||
function log(...args) {
|
||||
if (debugMode) {
|
||||
self.postMessage({
|
||||
type: "debug",
|
||||
data: { message: args.join(' ') }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Message handler for communication with main thread
|
||||
self.onmessage = function (event) {
|
||||
const { type, data } = event.data;
|
||||
log(`Worker received message: ${type}`);
|
||||
|
||||
switch (type) {
|
||||
case "initialize":
|
||||
processes = data.processes;
|
||||
log(`Initialized with ${processes.length} processes`);
|
||||
initializeAnimationStates();
|
||||
break;
|
||||
|
||||
case "update":
|
||||
const { timestamp, isPlaying } = data;
|
||||
if (isPlaying) {
|
||||
const delta = lastTimestamp === 0 ? 0.016 : (timestamp - lastTimestamp) / 1000; // Convert to seconds
|
||||
updateAnimations(delta, timestamp);
|
||||
}
|
||||
lastTimestamp = timestamp;
|
||||
break;
|
||||
|
||||
case "reset":
|
||||
log("Resetting animations");
|
||||
resetAnimations();
|
||||
break;
|
||||
|
||||
case "togglePlay":
|
||||
// If resuming from pause, recalculate the time delta
|
||||
log(`Toggle play: ${data.isPlaying}`);
|
||||
lastTimestamp = data.timestamp;
|
||||
break;
|
||||
|
||||
case "setDebug":
|
||||
debugMode = data.enabled;
|
||||
log(`Debug mode: ${debugMode}`);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
// Initialize animation states for all processes
|
||||
function initializeAnimationStates() {
|
||||
animationStates = {};
|
||||
|
||||
processes.forEach((process) => {
|
||||
if (!process || !process.id) {
|
||||
log("Invalid process found:", process);
|
||||
return;
|
||||
}
|
||||
|
||||
animationStates[process.id] = {
|
||||
spawnedObjects: {},
|
||||
nextSpawnTime: 0,
|
||||
objectIdCounter: 0,
|
||||
};
|
||||
});
|
||||
|
||||
// Send initial states back to main thread
|
||||
self.postMessage({
|
||||
type: "statesInitialized",
|
||||
data: { animationStates },
|
||||
});
|
||||
}
|
||||
|
||||
// Reset all animations
|
||||
function resetAnimations() {
|
||||
initializeAnimationStates();
|
||||
}
|
||||
|
||||
// Find spawn point in a process
|
||||
function findSpawnPoint(process) {
|
||||
if (!process || !process.paths) {
|
||||
log(`No paths found for process ${process?.id}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
for (const path of process.paths) {
|
||||
if (!path || !path.points) continue;
|
||||
|
||||
for (const point of path.points) {
|
||||
if (!point || !point.actions) continue;
|
||||
|
||||
const spawnAction = point.actions.find(
|
||||
(a) => a && a.isUsed && a.type === "Spawn"
|
||||
);
|
||||
if (spawnAction) {
|
||||
return { point, path };
|
||||
}
|
||||
}
|
||||
}
|
||||
log(`No spawn points found for process ${process.id}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Create a new spawned object with proper initial position
|
||||
function createSpawnedObject(process, spawnPoint, currentTime, materialType) {
|
||||
// Extract spawn position from the actual spawn point
|
||||
const position = spawnPoint.point.position
|
||||
? [...spawnPoint.point.position]
|
||||
: [0, 0, 0];
|
||||
|
||||
// Get the path position and add it to the spawn point position
|
||||
const pathPosition = spawnPoint.path.pathPosition || [0, 0, 0];
|
||||
const absolutePosition = [
|
||||
position[0] + pathPosition[0],
|
||||
position[1] + pathPosition[1],
|
||||
position[2] + pathPosition[2],
|
||||
];
|
||||
|
||||
return {
|
||||
id: `obj-${process.id}-${animationStates[process.id].objectIdCounter}`,
|
||||
position: absolutePosition,
|
||||
state: {
|
||||
currentIndex: 0,
|
||||
progress: 0,
|
||||
isAnimating: true,
|
||||
speed: process.speed || 1,
|
||||
isDelaying: false,
|
||||
delayStartTime: 0,
|
||||
currentDelayDuration: 0,
|
||||
delayComplete: false,
|
||||
currentPathIndex: 0,
|
||||
// Store the spawn point index to start animation from correct path point
|
||||
spawnPointIndex: getPointIndexInProcess(process, spawnPoint.point),
|
||||
},
|
||||
visible: true,
|
||||
materialType: materialType || "Default",
|
||||
spawnTime: currentTime,
|
||||
};
|
||||
}
|
||||
|
||||
// Get the index of a point within the process animation path
|
||||
function getPointIndexInProcess(process, point) {
|
||||
if (!process.paths) return 0;
|
||||
|
||||
let cumulativePoints = 0;
|
||||
for (const path of process.paths) {
|
||||
for (let i = 0; i < (path.points?.length || 0); i++) {
|
||||
if (path.points[i].uuid === point.uuid) {
|
||||
return cumulativePoints + i;
|
||||
}
|
||||
}
|
||||
cumulativePoints += path.points?.length || 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get point data for current animation index
|
||||
function getPointDataForAnimationIndex(process, index) {
|
||||
if (!process.paths) return null;
|
||||
|
||||
let cumulativePoints = 0;
|
||||
for (const path of process.paths) {
|
||||
const pointCount = path.points?.length || 0;
|
||||
|
||||
if (index < cumulativePoints + pointCount) {
|
||||
const pointIndex = index - cumulativePoints;
|
||||
return path.points?.[pointIndex] || null;
|
||||
}
|
||||
|
||||
cumulativePoints += pointCount;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Convert process paths to Vector3 format
|
||||
function getProcessPath(process) {
|
||||
if (!process.animationPath) {
|
||||
log(`No animation path for process ${process.id}`);
|
||||
return [];
|
||||
}
|
||||
return process.animationPath.map((p) => ({ x: p.x, y: p.y, z: p.z })) || [];
|
||||
}
|
||||
|
||||
// Handle material swap for an object
|
||||
function handleMaterialSwap(processId, objectId, materialType) {
|
||||
const processState = animationStates[processId];
|
||||
if (!processState || !processState.spawnedObjects[objectId]) return;
|
||||
|
||||
processState.spawnedObjects[objectId].materialType = materialType;
|
||||
|
||||
// Notify main thread about material change
|
||||
self.postMessage({
|
||||
type: "materialChanged",
|
||||
data: {
|
||||
processId,
|
||||
objectId,
|
||||
materialType,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Handle point actions for an object
|
||||
function handlePointActions(processId, objectId, actions = [], currentTime) {
|
||||
let shouldStopAnimation = false;
|
||||
const processState = animationStates[processId];
|
||||
|
||||
if (!processState || !processState.spawnedObjects[objectId]) return false;
|
||||
|
||||
const objectState = processState.spawnedObjects[objectId];
|
||||
|
||||
actions.forEach((action) => {
|
||||
if (!action || !action.isUsed) return;
|
||||
|
||||
switch (action.type) {
|
||||
case "Delay":
|
||||
if (objectState.state.isDelaying) return;
|
||||
|
||||
const delayDuration =
|
||||
typeof action.delay === "number"
|
||||
? action.delay
|
||||
: parseFloat(action.delay || "0");
|
||||
|
||||
if (delayDuration > 0) {
|
||||
objectState.state.isDelaying = true;
|
||||
objectState.state.delayStartTime = currentTime;
|
||||
objectState.state.currentDelayDuration = delayDuration;
|
||||
objectState.state.delayComplete = false;
|
||||
shouldStopAnimation = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case "Despawn":
|
||||
delete processState.spawnedObjects[objectId];
|
||||
shouldStopAnimation = true;
|
||||
|
||||
// Notify main thread about despawn
|
||||
self.postMessage({
|
||||
type: "objectDespawned",
|
||||
data: {
|
||||
processId,
|
||||
objectId,
|
||||
},
|
||||
});
|
||||
break;
|
||||
|
||||
case "Swap":
|
||||
if (action.material) {
|
||||
handleMaterialSwap(processId, objectId, action.material);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
return shouldStopAnimation;
|
||||
}
|
||||
|
||||
// Check if point has non-inherit actions
|
||||
function hasNonInheritActions(actions = []) {
|
||||
return actions.some((action) => action && action.isUsed && action.type !== "Inherit");
|
||||
}
|
||||
|
||||
// Calculate vector lerp (linear interpolation)
|
||||
function lerpVectors(v1, v2, alpha) {
|
||||
return {
|
||||
x: v1.x + (v2.x - v1.x) * alpha,
|
||||
y: v1.y + (v2.y - v1.y) * alpha,
|
||||
z: v1.z + (v2.z - v1.z) * alpha,
|
||||
};
|
||||
}
|
||||
|
||||
// Calculate vector distance
|
||||
function distanceBetweenVectors(v1, v2) {
|
||||
const dx = v2.x - v1.x;
|
||||
const dy = v2.y - v1.y;
|
||||
const dz = v2.z - v1.z;
|
||||
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
||||
}
|
||||
|
||||
// Process spawn logic
|
||||
function processSpawns(currentTime) {
|
||||
processes.forEach((process) => {
|
||||
const processState = animationStates[process.id];
|
||||
if (!processState) return;
|
||||
|
||||
const spawnPointData = findSpawnPoint(process);
|
||||
if (!spawnPointData || !spawnPointData.point.actions) return;
|
||||
|
||||
const spawnAction = spawnPointData.point.actions.find(
|
||||
(a) => a.isUsed && a.type === "Spawn"
|
||||
);
|
||||
if (!spawnAction) return;
|
||||
|
||||
const spawnInterval =
|
||||
typeof spawnAction.spawnInterval === "number"
|
||||
? spawnAction.spawnInterval
|
||||
: parseFloat(spawnAction.spawnInterval || "2"); // Default to 2 seconds if not specified
|
||||
|
||||
if (currentTime >= processState.nextSpawnTime) {
|
||||
const newObject = createSpawnedObject(
|
||||
process,
|
||||
spawnPointData,
|
||||
currentTime,
|
||||
spawnAction.material || "Default"
|
||||
);
|
||||
|
||||
processState.spawnedObjects[newObject.id] = newObject;
|
||||
processState.objectIdCounter++;
|
||||
processState.nextSpawnTime = currentTime + spawnInterval;
|
||||
|
||||
log(`Spawned object ${newObject.id} for process ${process.id}`);
|
||||
|
||||
// Notify main thread about new object
|
||||
self.postMessage({
|
||||
type: "objectSpawned",
|
||||
data: {
|
||||
processId: process.id,
|
||||
object: newObject,
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Update all animations
|
||||
function updateAnimations(delta, currentTime) {
|
||||
// First handle spawning of new objects
|
||||
processSpawns(currentTime);
|
||||
|
||||
// Then animate existing objects
|
||||
processes.forEach((process) => {
|
||||
const processState = animationStates[process.id];
|
||||
if (!processState) return;
|
||||
|
||||
const path = getProcessPath(process);
|
||||
if (path.length < 2) {
|
||||
log(`Path too short for process ${process.id}, length: ${path.length}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const updatedObjects = {};
|
||||
let hasChanges = false;
|
||||
|
||||
Object.entries(processState.spawnedObjects).forEach(([objectId, obj]) => {
|
||||
if (!obj.visible || !obj.state.isAnimating) return;
|
||||
|
||||
const stateRef = obj.state;
|
||||
|
||||
// Use the spawnPointIndex as starting point if it's the initial movement
|
||||
if (stateRef.currentIndex === 0 && stateRef.progress === 0) {
|
||||
stateRef.currentIndex = stateRef.spawnPointIndex || 0;
|
||||
}
|
||||
|
||||
// Get current point data
|
||||
const currentPointData = getPointDataForAnimationIndex(
|
||||
process,
|
||||
stateRef.currentIndex
|
||||
);
|
||||
|
||||
// Execute actions when arriving at a new point
|
||||
if (stateRef.progress === 0 && currentPointData?.actions) {
|
||||
const shouldStop = handlePointActions(
|
||||
process.id,
|
||||
objectId,
|
||||
currentPointData.actions,
|
||||
currentTime
|
||||
);
|
||||
if (shouldStop) return;
|
||||
}
|
||||
|
||||
// Handle delays
|
||||
if (stateRef.isDelaying) {
|
||||
if (
|
||||
currentTime - stateRef.delayStartTime >=
|
||||
stateRef.currentDelayDuration
|
||||
) {
|
||||
stateRef.isDelaying = false;
|
||||
stateRef.delayComplete = true;
|
||||
} else {
|
||||
updatedObjects[objectId] = { ...obj, state: { ...stateRef } };
|
||||
return; // Keep waiting
|
||||
}
|
||||
}
|
||||
|
||||
const nextPointIdx = stateRef.currentIndex + 1;
|
||||
const isLastPoint = nextPointIdx >= path.length;
|
||||
|
||||
if (isLastPoint) {
|
||||
if (currentPointData?.actions) {
|
||||
const shouldStop = !hasNonInheritActions(currentPointData.actions);
|
||||
if (shouldStop) {
|
||||
// Reached the end of path with no more actions
|
||||
delete processState.spawnedObjects[objectId];
|
||||
log(`Object ${objectId} completed path`);
|
||||
|
||||
// Notify main thread to remove the object
|
||||
self.postMessage({
|
||||
type: "objectCompleted",
|
||||
data: {
|
||||
processId: process.id,
|
||||
objectId,
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isLastPoint) {
|
||||
const currentPos = path[stateRef.currentIndex];
|
||||
const nextPos = path[nextPointIdx];
|
||||
const distance = distanceBetweenVectors(currentPos, nextPos);
|
||||
|
||||
// Ensure we don't divide by zero
|
||||
if (distance > 0) {
|
||||
const movement = stateRef.speed * delta;
|
||||
|
||||
// Update progress based on distance and speed
|
||||
const oldProgress = stateRef.progress;
|
||||
stateRef.progress += movement / distance;
|
||||
|
||||
if (stateRef.progress >= 1) {
|
||||
// Reached next point
|
||||
stateRef.currentIndex = nextPointIdx;
|
||||
stateRef.progress = 0;
|
||||
stateRef.delayComplete = false;
|
||||
obj.position = [nextPos.x, nextPos.y, nextPos.z];
|
||||
} else {
|
||||
// Interpolate position
|
||||
const lerpedPos = lerpVectors(currentPos, nextPos, stateRef.progress);
|
||||
obj.position = [lerpedPos.x, lerpedPos.y, lerpedPos.z];
|
||||
}
|
||||
|
||||
// Only send updates when there's meaningful movement
|
||||
if (Math.abs(oldProgress - stateRef.progress) > 0.01) {
|
||||
hasChanges = true;
|
||||
}
|
||||
} else {
|
||||
// Skip to next point if distance is zero
|
||||
stateRef.currentIndex = nextPointIdx;
|
||||
stateRef.progress = 0;
|
||||
obj.position = [nextPos.x, nextPos.y, nextPos.z];
|
||||
hasChanges = true;
|
||||
}
|
||||
}
|
||||
|
||||
updatedObjects[objectId] = { ...obj, state: { ...stateRef } };
|
||||
});
|
||||
|
||||
// Update animation state with modified objects
|
||||
if (Object.keys(updatedObjects).length > 0) {
|
||||
processState.spawnedObjects = {
|
||||
...processState.spawnedObjects,
|
||||
...updatedObjects,
|
||||
};
|
||||
|
||||
// Only send position updates when there are meaningful changes
|
||||
if (hasChanges) {
|
||||
self.postMessage({
|
||||
type: "positionsUpdated",
|
||||
data: {
|
||||
processId: process.id,
|
||||
objects: updatedObjects,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -5,7 +5,7 @@
|
||||
// useCallback,
|
||||
// useRef,
|
||||
// } from "react";
|
||||
// import { useSimulationPaths } from "../../../store/store";
|
||||
// import { useSimulationStates } from "../../../store/store";
|
||||
// import * as THREE from "three";
|
||||
// import { useThree } from "@react-three/fiber";
|
||||
// import {
|
||||
@@ -29,7 +29,7 @@
|
||||
// position: [number, number, number];
|
||||
// actions: PointAction[];
|
||||
// connections: {
|
||||
// targets: Array<{ pathUUID: string }>;
|
||||
// targets: Array<{ modelUUID: string }>;
|
||||
// };
|
||||
// }
|
||||
|
||||
@@ -72,7 +72,7 @@
|
||||
// actions: point.actions.map(normalizeAction), // Preserve exact actions
|
||||
// connections: {
|
||||
// targets: point.connections.targets.map((target) => ({
|
||||
// pathUUID: target.pathUUID,
|
||||
// modelUUID: target.modelUUID,
|
||||
// })),
|
||||
// },
|
||||
// })),
|
||||
@@ -94,7 +94,7 @@
|
||||
// : [normalizeAction(path.point.actions)],
|
||||
// connections: {
|
||||
// targets: path.point.connections.targets.map((target) => ({
|
||||
// pathUUID: target.pathUUID,
|
||||
// modelUUID: target.modelUUID,
|
||||
// })),
|
||||
// },
|
||||
// },
|
||||
@@ -137,18 +137,18 @@
|
||||
// // Check if current last connects to next last (requires reversal)
|
||||
// const connectsToLast = currentLastPoint.connections.targets.some(
|
||||
// (target) =>
|
||||
// target.pathUUID === nextPath.modeluuid &&
|
||||
// target.modelUUID === nextPath.modeluuid &&
|
||||
// nextLastPoint.connections.targets.some(
|
||||
// (t) => t.pathUUID === currentPath.modeluuid
|
||||
// (t) => t.modelUUID === currentPath.modeluuid
|
||||
// )
|
||||
// );
|
||||
|
||||
// // Check if current last connects to next first (no reversal needed)
|
||||
// const connectsToFirst = currentLastPoint.connections.targets.some(
|
||||
// (target) =>
|
||||
// target.pathUUID === nextPath.modeluuid &&
|
||||
// target.modelUUID === nextPath.modeluuid &&
|
||||
// nextFirstPoint.connections.targets.some(
|
||||
// (t) => t.pathUUID === currentPath.modeluuid
|
||||
// (t) => t.modelUUID === currentPath.modeluuid
|
||||
// )
|
||||
// );
|
||||
|
||||
@@ -249,10 +249,10 @@
|
||||
// // Process outgoing connections
|
||||
// for (const point of currentPath.points) {
|
||||
// for (const target of point.connections.targets) {
|
||||
// if (!visited.has(target.pathUUID)) {
|
||||
// const targetPath = pathMap.get(target.pathUUID);
|
||||
// if (!visited.has(target.modelUUID)) {
|
||||
// const targetPath = pathMap.get(target.modelUUID);
|
||||
// if (targetPath) {
|
||||
// visited.add(target.pathUUID);
|
||||
// visited.add(target.modelUUID);
|
||||
// queue.push(targetPath);
|
||||
// }
|
||||
// }
|
||||
@@ -264,7 +264,7 @@
|
||||
// if (!visited.has(uuid)) {
|
||||
// const hasConnectionToCurrent = path.points.some((point) =>
|
||||
// point.connections.targets.some(
|
||||
// (t) => t.pathUUID === currentPath.modeluuid
|
||||
// (t) => t.modelUUID === currentPath.modeluuid
|
||||
// )
|
||||
// );
|
||||
// if (hasConnectionToCurrent) {
|
||||
@@ -312,19 +312,19 @@
|
||||
|
||||
// const ProcessCreator: React.FC<ProcessCreatorProps> = React.memo(
|
||||
// ({ onProcessesCreated }) => {
|
||||
// const { simulationPaths } = useSimulationPaths();
|
||||
// const { simulationStates } = useSimulationStates();
|
||||
// const { createProcessesFromPaths } = useProcessCreation();
|
||||
// const prevPathsRef = useRef<SimulationPath[]>([]);
|
||||
// const prevProcessesRef = useRef<Process[]>([]);
|
||||
|
||||
// const convertedPaths = useMemo((): SimulationPath[] => {
|
||||
// if (!simulationPaths) return [];
|
||||
// return simulationPaths.map((path) =>
|
||||
// if (!simulationStates) return [];
|
||||
// return simulationStates.map((path) =>
|
||||
// convertToSimulationPath(
|
||||
// path as ConveyorEventsSchema | VehicleEventsSchema
|
||||
// )
|
||||
// );
|
||||
// }, [simulationPaths]);
|
||||
// }, [simulationStates]);
|
||||
|
||||
// const pathsDependency = useMemo(() => {
|
||||
// if (!convertedPaths) return null;
|
||||
@@ -335,7 +335,7 @@
|
||||
// ),
|
||||
// connections: path.points
|
||||
// .flatMap((p: PathPoint) =>
|
||||
// p.connections.targets.map((t: { pathUUID: string }) => t.pathUUID)
|
||||
// p.connections.targets.map((t: { modelUUID: string }) => t.modelUUID)
|
||||
// )
|
||||
// .join(","),
|
||||
// }));
|
||||
@@ -404,7 +404,7 @@ import React, {
|
||||
useCallback,
|
||||
useRef,
|
||||
} from "react";
|
||||
import { useSimulationPaths } from "../../../store/store";
|
||||
import { useSimulationStates } from "../../../store/store";
|
||||
import * as THREE from "three";
|
||||
import { useThree } from "@react-three/fiber";
|
||||
import {
|
||||
@@ -428,11 +428,12 @@ export interface PathPoint {
|
||||
position: [number, number, number];
|
||||
actions: PointAction[];
|
||||
connections: {
|
||||
targets: Array<{ pathUUID: string }>;
|
||||
targets: Array<{ modelUUID: string }>;
|
||||
};
|
||||
}
|
||||
|
||||
export interface SimulationPath {
|
||||
type: string;
|
||||
modeluuid: string;
|
||||
points: PathPoint[];
|
||||
pathPosition: [number, number, number];
|
||||
@@ -464,6 +465,7 @@ function convertToSimulationPath(
|
||||
|
||||
if (path.type === "Conveyor") {
|
||||
return {
|
||||
type: path.type,
|
||||
modeluuid,
|
||||
points: path.points.map((point) => ({
|
||||
uuid: point.uuid,
|
||||
@@ -471,7 +473,7 @@ function convertToSimulationPath(
|
||||
actions: point.actions.map(normalizeAction), // Preserve exact actions
|
||||
connections: {
|
||||
targets: point.connections.targets.map((target) => ({
|
||||
pathUUID: target.pathUUID,
|
||||
modelUUID: target.modelUUID,
|
||||
})),
|
||||
},
|
||||
})),
|
||||
@@ -483,23 +485,24 @@ function convertToSimulationPath(
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
type: path.type,
|
||||
modeluuid,
|
||||
points: [
|
||||
{
|
||||
uuid: path.point.uuid,
|
||||
position: path.point.position,
|
||||
actions: Array.isArray(path.point.actions)
|
||||
? path.point.actions.map(normalizeAction)
|
||||
: [normalizeAction(path.point.actions)],
|
||||
uuid: path.points.uuid,
|
||||
position: path.points.position,
|
||||
actions: Array.isArray(path.points.actions)
|
||||
? path.points.actions.map(normalizeAction)
|
||||
: [normalizeAction(path.points.actions)],
|
||||
connections: {
|
||||
targets: path.point.connections.targets.map((target) => ({
|
||||
pathUUID: target.pathUUID,
|
||||
targets: path.points.connections.targets.map((target) => ({
|
||||
modelUUID: target.modelUUID,
|
||||
})),
|
||||
},
|
||||
},
|
||||
],
|
||||
pathPosition: path.position,
|
||||
speed: path.point.speed || 1,
|
||||
speed: path.points.speed || 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -536,18 +539,18 @@ function shouldReverseNextPath(
|
||||
// Check if current last connects to next last (requires reversal)
|
||||
const connectsToLast = currentLastPoint.connections.targets.some(
|
||||
(target) =>
|
||||
target.pathUUID === nextPath.modeluuid &&
|
||||
target.modelUUID === nextPath.modeluuid &&
|
||||
nextLastPoint.connections.targets.some(
|
||||
(t) => t.pathUUID === currentPath.modeluuid
|
||||
(t) => t.modelUUID === currentPath.modeluuid
|
||||
)
|
||||
);
|
||||
|
||||
// Check if current last connects to next first (no reversal needed)
|
||||
const connectsToFirst = currentLastPoint.connections.targets.some(
|
||||
(target) =>
|
||||
target.pathUUID === nextPath.modeluuid &&
|
||||
target.modelUUID === nextPath.modeluuid &&
|
||||
nextFirstPoint.connections.targets.some(
|
||||
(t) => t.pathUUID === currentPath.modeluuid
|
||||
(t) => t.modelUUID === currentPath.modeluuid
|
||||
)
|
||||
);
|
||||
|
||||
@@ -614,6 +617,7 @@ export function useProcessCreation() {
|
||||
const [processes, setProcesses] = useState<Process[]>([]);
|
||||
|
||||
const hasSpawnAction = useCallback((path: SimulationPath): boolean => {
|
||||
if (path.type !== "Conveyor") return false;
|
||||
return path.points.some((point) =>
|
||||
point.actions.some((action) => action.type.toLowerCase() === "spawn")
|
||||
);
|
||||
@@ -674,10 +678,10 @@ export function useProcessCreation() {
|
||||
// Process outgoing connections
|
||||
for (const point of currentPath.points) {
|
||||
for (const target of point.connections.targets) {
|
||||
if (!visited.has(target.pathUUID)) {
|
||||
const targetPath = pathMap.get(target.pathUUID);
|
||||
if (!visited.has(target.modelUUID)) {
|
||||
const targetPath = pathMap.get(target.modelUUID);
|
||||
if (targetPath) {
|
||||
visited.add(target.pathUUID);
|
||||
visited.add(target.modelUUID);
|
||||
queue.push(targetPath);
|
||||
}
|
||||
}
|
||||
@@ -689,7 +693,7 @@ export function useProcessCreation() {
|
||||
if (!visited.has(uuid)) {
|
||||
const hasConnectionToCurrent = path.points.some((point) =>
|
||||
point.connections.targets.some(
|
||||
(t) => t.pathUUID === currentPath.modeluuid
|
||||
(t) => t.modelUUID === currentPath.modeluuid
|
||||
)
|
||||
);
|
||||
if (hasConnectionToCurrent) {
|
||||
@@ -737,19 +741,19 @@ export function useProcessCreation() {
|
||||
|
||||
const ProcessCreator: React.FC<ProcessCreatorProps> = React.memo(
|
||||
({ onProcessesCreated }) => {
|
||||
const { simulationPaths } = useSimulationPaths();
|
||||
const { simulationStates } = useSimulationStates();
|
||||
const { createProcessesFromPaths } = useProcessCreation();
|
||||
const prevPathsRef = useRef<SimulationPath[]>([]);
|
||||
const prevProcessesRef = useRef<Process[]>([]);
|
||||
|
||||
const convertedPaths = useMemo((): SimulationPath[] => {
|
||||
if (!simulationPaths) return [];
|
||||
return simulationPaths.map((path) =>
|
||||
if (!simulationStates) return [];
|
||||
return simulationStates.map((path) =>
|
||||
convertToSimulationPath(
|
||||
path as ConveyorEventsSchema | VehicleEventsSchema
|
||||
)
|
||||
);
|
||||
}, [simulationPaths]);
|
||||
}, [simulationStates]);
|
||||
|
||||
// Enhanced dependency tracking that includes action types
|
||||
const pathsDependency = useMemo(() => {
|
||||
@@ -764,7 +768,7 @@ const ProcessCreator: React.FC<ProcessCreatorProps> = React.memo(
|
||||
.join(","),
|
||||
connections: path.points
|
||||
.flatMap((p: PathPoint) =>
|
||||
p.connections.targets.map((t: { pathUUID: string }) => t.pathUUID)
|
||||
p.connections.targets.map((t: { modelUUID: string }) => t.modelUUID)
|
||||
)
|
||||
.join(","),
|
||||
}));
|
||||
|
||||
114
app/src/modules/simulation/process/processObjectRender.tsx
Normal file
114
app/src/modules/simulation/process/processObjectRender.tsx
Normal file
@@ -0,0 +1,114 @@
|
||||
import React, { useRef, useEffect } from "react";
|
||||
import { useFrame } from "@react-three/fiber";
|
||||
import * as THREE from "three";
|
||||
import { GLTF } from "three-stdlib";
|
||||
import { Box3Helper } from "three";
|
||||
import { SpawnedObject, ProcessData } from "./types";
|
||||
|
||||
interface ProcessObjectRendererProps {
|
||||
objectId: string;
|
||||
object: SpawnedObject;
|
||||
process: ProcessData;
|
||||
gltf: GLTF;
|
||||
showBoundingBox?: boolean;
|
||||
}
|
||||
|
||||
export const ProcessObjectRenderer: React.FC<ProcessObjectRendererProps> = ({
|
||||
objectId,
|
||||
object,
|
||||
process,
|
||||
gltf,
|
||||
showBoundingBox = false,
|
||||
}) => {
|
||||
const meshRef = useRef<THREE.Mesh>(null);
|
||||
const boxHelperRef = useRef<THREE.Box3Helper | null>(null);
|
||||
const boundingBoxRef = useRef<THREE.Box3>(new THREE.Box3());
|
||||
|
||||
// Issue 1: Can't assign to ref.current as it's read-only
|
||||
useEffect(() => {
|
||||
if (object.ref && meshRef.current) {
|
||||
// Instead of direct assignment, we need to store the mesh reference another way
|
||||
// Option 1: If you can modify the SpawnedObject interface, add a setMesh method
|
||||
if (typeof (object as any).setMesh === 'function') {
|
||||
(object as any).setMesh(meshRef.current);
|
||||
}
|
||||
|
||||
// Option 2: Store the mesh in a property that isn't ref.current
|
||||
// This requires modifying your SpawnedObject interface to include this property
|
||||
(object as any).meshInstance = meshRef.current;
|
||||
|
||||
// Option 3: If you need to maintain compatibility, you could use Object.defineProperty
|
||||
// But this is a hack and not recommended
|
||||
// Object.defineProperty(object.ref, 'current', { value: meshRef.current, writable: true });
|
||||
}
|
||||
}, [object.ref]);
|
||||
|
||||
// Create a bounding box helper for visualization
|
||||
useFrame(() => {
|
||||
if (meshRef.current && showBoundingBox) {
|
||||
// Update the bounding box to match the mesh position
|
||||
if (!boxHelperRef.current) {
|
||||
// Get the size of the mesh
|
||||
const size = new THREE.Vector3(1, 1, 1);
|
||||
|
||||
// If the mesh has geometry, use its dimensions
|
||||
if (meshRef.current.geometry) {
|
||||
const box = new THREE.Box3().setFromObject(meshRef.current);
|
||||
box.getSize(size);
|
||||
}
|
||||
|
||||
// Create a new bounding box centered on the mesh
|
||||
boundingBoxRef.current = new THREE.Box3().setFromCenterAndSize(
|
||||
meshRef.current.position,
|
||||
size
|
||||
);
|
||||
|
||||
// Create a helper to visualize the box
|
||||
boxHelperRef.current = new Box3Helper(
|
||||
boundingBoxRef.current,
|
||||
new THREE.Color(0xff0000)
|
||||
);
|
||||
|
||||
// Add the helper to the scene
|
||||
meshRef.current.parent?.add(boxHelperRef.current);
|
||||
} else {
|
||||
// Update the box position to match the mesh
|
||||
boundingBoxRef.current.setFromCenterAndSize(
|
||||
meshRef.current.position,
|
||||
boundingBoxRef.current.getSize(new THREE.Vector3())
|
||||
);
|
||||
|
||||
// Force the helper to update
|
||||
boxHelperRef.current.updateMatrixWorld(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (gltf?.scene) {
|
||||
return (
|
||||
<primitive
|
||||
ref={meshRef}
|
||||
object={gltf.scene.clone()}
|
||||
position={[0, 0, 0]}
|
||||
material={object.material}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// Issue 2: Material color type problem
|
||||
return (
|
||||
<mesh ref={meshRef}>
|
||||
<boxGeometry args={[1, 1, 1]} />
|
||||
<meshStandardMaterial
|
||||
// Fix the color property access
|
||||
color={
|
||||
object.material && 'color' in object.material
|
||||
? (object.material as THREE.MeshStandardMaterial).color
|
||||
: "#00ff00"
|
||||
}
|
||||
metalness={0.5}
|
||||
roughness={0.3}
|
||||
/>
|
||||
</mesh>
|
||||
);
|
||||
};
|
||||
79
app/src/modules/simulation/process/types.ts
Normal file
79
app/src/modules/simulation/process/types.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import * as THREE from "three";
|
||||
import React from "react";
|
||||
|
||||
export interface ProcessPoint {
|
||||
uuid: string;
|
||||
position: number[];
|
||||
actions?: PointAction[];
|
||||
}
|
||||
|
||||
export interface PointAction {
|
||||
type: string;
|
||||
isUsed: boolean;
|
||||
spawnInterval?: number | string;
|
||||
material?: string;
|
||||
delay?: number | string;
|
||||
}
|
||||
|
||||
export interface ProcessData {
|
||||
id: string;
|
||||
name: string;
|
||||
paths?: {
|
||||
points?: ProcessPoint[];
|
||||
}[];
|
||||
animationPath?: { x: number; y: number; z: number }[];
|
||||
speed?: number;
|
||||
customMaterials?: Record<string, THREE.Material>;
|
||||
}
|
||||
|
||||
export interface ProcessAnimationState {
|
||||
spawnedObjects: Record<string, SpawnedObject | SpawnedObjectWithCollision>;
|
||||
nextSpawnTime: number;
|
||||
objectIdCounter: number;
|
||||
isProcessDelaying: boolean;
|
||||
processDelayStartTime: number;
|
||||
processDelayDuration: number;
|
||||
isCollisionPaused?: boolean;
|
||||
}
|
||||
|
||||
export interface SpawnedObject {
|
||||
ref: React.RefObject<THREE.Object3D>;
|
||||
state: {
|
||||
currentIndex: number;
|
||||
progress: number;
|
||||
isAnimating: boolean;
|
||||
speed: number;
|
||||
isDelaying: boolean;
|
||||
delayStartTime: number;
|
||||
currentDelayDuration: number;
|
||||
delayComplete: boolean;
|
||||
currentPathIndex: number;
|
||||
};
|
||||
visible: boolean;
|
||||
material: THREE.Material;
|
||||
currentMaterialType: string;
|
||||
spawnTime: number;
|
||||
position: THREE.Vector3;
|
||||
collision?: {
|
||||
boundingBox: THREE.Box3;
|
||||
isColliding: boolean;
|
||||
colliding: boolean; // Added this property
|
||||
};
|
||||
}
|
||||
|
||||
// For use in your processAnimator.tsx
|
||||
// Update the CollisionState interface to include all required properties
|
||||
interface CollisionState {
|
||||
boundingBox: THREE.Box3;
|
||||
isColliding: boolean;
|
||||
colliding: boolean; // This was missing
|
||||
collidingWith: string[];
|
||||
}
|
||||
export interface SpawnedObjectWithCollision extends SpawnedObject {
|
||||
collision: {
|
||||
boundingBox: THREE.Box3;
|
||||
isColliding: boolean;
|
||||
colliding: boolean;
|
||||
collidingWith: string[];
|
||||
};
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { useState, useEffect, useRef, useMemo } from "react";
|
||||
import {
|
||||
useSelectedActionSphere,
|
||||
useSelectedPath,
|
||||
useSimulationPaths,
|
||||
useSimulationStates,
|
||||
} from "../../store/store";
|
||||
import * as THREE from "three";
|
||||
import Behaviour from "./behaviour/behaviour";
|
||||
@@ -15,12 +15,12 @@ import Agv from "../builder/agv/agv";
|
||||
function Simulation() {
|
||||
const { activeModule } = useModuleStore();
|
||||
const pathsGroupRef = useRef() as React.MutableRefObject<THREE.Group>;
|
||||
const { simulationPaths, setSimulationPaths } = useSimulationPaths();
|
||||
const { simulationStates, setSimulationStates } = useSimulationStates();
|
||||
const [processes, setProcesses] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
console.log('simulationPaths: ', simulationPaths);
|
||||
}, [simulationPaths]);
|
||||
// console.log('simulationStates: ', simulationStates);
|
||||
}, [simulationStates]);
|
||||
|
||||
// useEffect(() => {
|
||||
// if (selectedActionSphere) {
|
||||
@@ -42,7 +42,7 @@ function Simulation() {
|
||||
<PathCreation pathsGroupRef={pathsGroupRef} />
|
||||
<PathConnector pathsGroupRef={pathsGroupRef} />
|
||||
<ProcessContainer />
|
||||
{/* <Agv /> */}
|
||||
<Agv />
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// import { useMemo, useState } from 'react';
|
||||
// import { useSelectedActionSphere, useToggleView, useSimulationPaths, useSelectedPath, useStartSimulation, useDrawMaterialPath } from '../../store/store';
|
||||
// import { useSelectedActionSphere, useToggleView, useSimulationStates, useSelectedPath, useStartSimulation, useDrawMaterialPath } from '../../store/store';
|
||||
// import * as THREE from 'three';
|
||||
// import useModuleStore from '../../store/useModuleStore';
|
||||
|
||||
@@ -9,17 +9,17 @@
|
||||
// const { startSimulation, setStartSimulation } = useStartSimulation();
|
||||
// const { selectedActionSphere } = useSelectedActionSphere();
|
||||
// const { selectedPath, setSelectedPath } = useSelectedPath();
|
||||
// const { simulationPaths, setSimulationPaths } = useSimulationPaths();
|
||||
// const { simulationStates, setSimulationStates } = useSimulationStates();
|
||||
// const { drawMaterialPath, setDrawMaterialPath } = useDrawMaterialPath();
|
||||
// const [activeButton, setActiveButton] = useState<string | null>(null);
|
||||
|
||||
// const handleAddAction = () => {
|
||||
// if (!selectedActionSphere) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) => ({
|
||||
// const updatedPaths = simulationStates.map((path) => ({
|
||||
// ...path,
|
||||
// points: path.points.map((point) => {
|
||||
// if (point.uuid === selectedActionSphere.point.uuid) {
|
||||
// if (point.uuid === selectedActionSphere.points.uuid) {
|
||||
// const actionIndex = point.actions.length;
|
||||
// const newAction = {
|
||||
// uuid: THREE.MathUtils.generateUUID(),
|
||||
@@ -37,31 +37,31 @@
|
||||
// }),
|
||||
// }));
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// };
|
||||
|
||||
// const handleDeleteAction = (uuid: string) => {
|
||||
// if (!selectedActionSphere) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) => ({
|
||||
// const updatedPaths = simulationStates.map((path) => ({
|
||||
// ...path,
|
||||
// points: path.points.map((point) =>
|
||||
// point.uuid === selectedActionSphere.point.uuid
|
||||
// point.uuid === selectedActionSphere.points.uuid
|
||||
// ? { ...point, actions: point.actions.filter(action => action.uuid !== uuid) }
|
||||
// : point
|
||||
// ),
|
||||
// }));
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// };
|
||||
|
||||
// const handleActionSelect = (uuid: string, actionType: string) => {
|
||||
// if (!selectedActionSphere) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) => ({
|
||||
// const updatedPaths = simulationStates.map((path) => ({
|
||||
// ...path,
|
||||
// points: path.points.map((point) =>
|
||||
// point.uuid === selectedActionSphere.point.uuid
|
||||
// point.uuid === selectedActionSphere.points.uuid
|
||||
// ? {
|
||||
// ...point,
|
||||
// actions: point.actions.map((action) =>
|
||||
@@ -72,16 +72,16 @@
|
||||
// ),
|
||||
// }));
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// };
|
||||
|
||||
// const handleMaterialSelect = (uuid: string, material: string) => {
|
||||
// if (!selectedActionSphere) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) => ({
|
||||
// const updatedPaths = simulationStates.map((path) => ({
|
||||
// ...path,
|
||||
// points: path.points.map((point) =>
|
||||
// point.uuid === selectedActionSphere.point.uuid
|
||||
// point.uuid === selectedActionSphere.points.uuid
|
||||
// ? {
|
||||
// ...point,
|
||||
// actions: point.actions.map((action) =>
|
||||
@@ -92,16 +92,16 @@
|
||||
// ),
|
||||
// }));
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// };
|
||||
|
||||
// const handleDelayChange = (uuid: string, delay: number | string) => {
|
||||
// if (!selectedActionSphere) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) => ({
|
||||
// const updatedPaths = simulationStates.map((path) => ({
|
||||
// ...path,
|
||||
// points: path.points.map((point) =>
|
||||
// point.uuid === selectedActionSphere.point.uuid
|
||||
// point.uuid === selectedActionSphere.points.uuid
|
||||
// ? {
|
||||
// ...point,
|
||||
// actions: point.actions.map((action) =>
|
||||
@@ -112,16 +112,16 @@
|
||||
// ),
|
||||
// }));
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// };
|
||||
|
||||
// const handleSpawnIntervalChange = (uuid: string, spawnInterval: number | string) => {
|
||||
// if (!selectedActionSphere) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) => ({
|
||||
// const updatedPaths = simulationStates.map((path) => ({
|
||||
// ...path,
|
||||
// points: path.points.map((point) =>
|
||||
// point.uuid === selectedActionSphere.point.uuid
|
||||
// point.uuid === selectedActionSphere.points.uuid
|
||||
// ? {
|
||||
// ...point,
|
||||
// actions: point.actions.map((action) =>
|
||||
@@ -132,27 +132,27 @@
|
||||
// ),
|
||||
// }));
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// };
|
||||
|
||||
// const handleSpeedChange = (speed: number) => {
|
||||
// if (!selectedPath) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) =>
|
||||
// const updatedPaths = simulationStates.map((path) =>
|
||||
// path.modeluuid === selectedPath.path.modeluuid ? { ...path, speed } : path
|
||||
// );
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// setSelectedPath({ ...selectedPath, path: { ...selectedPath.path, speed } });
|
||||
// };
|
||||
|
||||
// const handleAddTrigger = () => {
|
||||
// if (!selectedActionSphere) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) => ({
|
||||
// const updatedPaths = simulationStates.map((path) => ({
|
||||
// ...path,
|
||||
// points: path.points.map((point) => {
|
||||
// if (point.uuid === selectedActionSphere.point.uuid) {
|
||||
// if (point.uuid === selectedActionSphere.points.uuid) {
|
||||
// const triggerIndex = point.triggers.length;
|
||||
// const newTrigger = {
|
||||
// uuid: THREE.MathUtils.generateUUID(),
|
||||
@@ -167,31 +167,31 @@
|
||||
// }),
|
||||
// }));
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// };
|
||||
|
||||
// const handleDeleteTrigger = (uuid: string) => {
|
||||
// if (!selectedActionSphere) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) => ({
|
||||
// const updatedPaths = simulationStates.map((path) => ({
|
||||
// ...path,
|
||||
// points: path.points.map((point) =>
|
||||
// point.uuid === selectedActionSphere.point.uuid
|
||||
// point.uuid === selectedActionSphere.points.uuid
|
||||
// ? { ...point, triggers: point.triggers.filter(trigger => trigger.uuid !== uuid) }
|
||||
// : point
|
||||
// ),
|
||||
// }));
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// };
|
||||
|
||||
// const handleTriggerSelect = (uuid: string, triggerType: string) => {
|
||||
// if (!selectedActionSphere) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) => ({
|
||||
// const updatedPaths = simulationStates.map((path) => ({
|
||||
// ...path,
|
||||
// points: path.points.map((point) =>
|
||||
// point.uuid === selectedActionSphere.point.uuid
|
||||
// point.uuid === selectedActionSphere.points.uuid
|
||||
// ? {
|
||||
// ...point,
|
||||
// triggers: point.triggers.map((trigger) =>
|
||||
@@ -202,7 +202,7 @@
|
||||
// ),
|
||||
// }));
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// };
|
||||
|
||||
// const handleResetPath = () => {
|
||||
@@ -214,10 +214,10 @@
|
||||
// const handleActionToggle = (uuid: string) => {
|
||||
// if (!selectedActionSphere) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) => ({
|
||||
// const updatedPaths = simulationStates.map((path) => ({
|
||||
// ...path,
|
||||
// points: path.points.map((point) =>
|
||||
// point.uuid === selectedActionSphere.point.uuid
|
||||
// point.uuid === selectedActionSphere.points.uuid
|
||||
// ? {
|
||||
// ...point,
|
||||
// actions: point.actions.map((action) => ({
|
||||
@@ -229,16 +229,16 @@
|
||||
// ),
|
||||
// }));
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// };
|
||||
|
||||
// const handleTriggerToggle = (uuid: string) => {
|
||||
// if (!selectedActionSphere) return;
|
||||
|
||||
// const updatedPaths = simulationPaths.map((path) => ({
|
||||
// const updatedPaths = simulationStates.map((path) => ({
|
||||
// ...path,
|
||||
// points: path.points.map((point) =>
|
||||
// point.uuid === selectedActionSphere.point.uuid
|
||||
// point.uuid === selectedActionSphere.points.uuid
|
||||
// ? {
|
||||
// ...point,
|
||||
// triggers: point.triggers.map((trigger) => ({
|
||||
@@ -250,13 +250,13 @@
|
||||
// ),
|
||||
// }));
|
||||
|
||||
// setSimulationPaths(updatedPaths);
|
||||
// setSimulationStates(updatedPaths);
|
||||
// };
|
||||
|
||||
// const selectedPoint = useMemo(() => {
|
||||
// if (!selectedActionSphere) return null;
|
||||
// return simulationPaths.flatMap((path) => path.points).find((point) => point.uuid === selectedActionSphere.point.uuid);
|
||||
// }, [selectedActionSphere, simulationPaths]);
|
||||
// return simulationStates.flatMap((path) => path.points).find((point) => point.uuid === selectedActionSphere.points.uuid);
|
||||
// }, [selectedActionSphere, simulationStates]);
|
||||
|
||||
// const createPath = () => {
|
||||
// setActiveButton(activeButton !== 'addMaterialPath' ? 'addMaterialPath' : null);
|
||||
|
||||
@@ -11,13 +11,13 @@ type PathPoint = {
|
||||
};
|
||||
|
||||
type PathCreatorProps = {
|
||||
simulationPaths: PathPoint[][];
|
||||
setSimulationPaths: React.Dispatch<React.SetStateAction<PathPoint[][]>>;
|
||||
simulationStates: PathPoint[][];
|
||||
setSimulationStates: React.Dispatch<React.SetStateAction<PathPoint[][]>>;
|
||||
connections: { start: PathPoint; end: PathPoint }[];
|
||||
setConnections: React.Dispatch<React.SetStateAction<{ start: PathPoint; end: PathPoint }[]>>
|
||||
};
|
||||
|
||||
const PathCreator = ({ simulationPaths, setSimulationPaths, connections, setConnections }: PathCreatorProps) => {
|
||||
const PathCreator = ({ simulationStates, setSimulationStates, connections, setConnections }: PathCreatorProps) => {
|
||||
const { camera, scene, raycaster, pointer, gl } = useThree();
|
||||
const { drawMaterialPath } = useDrawMaterialPath();
|
||||
|
||||
@@ -71,7 +71,7 @@ const PathCreator = ({ simulationPaths, setSimulationPaths, connections, setConn
|
||||
e.preventDefault();
|
||||
if (drag || e.button === 0) return;
|
||||
if (currentPath.length > 1) {
|
||||
setSimulationPaths((prevPaths) => [...prevPaths, currentPath]);
|
||||
setSimulationStates((prevPaths) => [...prevPaths, currentPath]);
|
||||
}
|
||||
setCurrentPath([]);
|
||||
setTemporaryPoint(null);
|
||||
@@ -125,7 +125,7 @@ const PathCreator = ({ simulationPaths, setSimulationPaths, connections, setConn
|
||||
canvasElement.addEventListener("contextmenu", onContextMenu);
|
||||
} else {
|
||||
if (currentPath.length > 1) {
|
||||
setSimulationPaths((prevPaths) => [...prevPaths, currentPath]);
|
||||
setSimulationStates((prevPaths) => [...prevPaths, currentPath]);
|
||||
}
|
||||
setCurrentPath([]);
|
||||
setTemporaryPoint(null);
|
||||
@@ -179,25 +179,25 @@ const PathCreator = ({ simulationPaths, setSimulationPaths, connections, setConn
|
||||
if (selectedPoint) {
|
||||
const updatedPosition = e.target.object.position.clone();
|
||||
const updatedRotation = e.target.object.quaternion.clone();
|
||||
const updatedPaths = simulationPaths.map((path) =>
|
||||
const updatedPaths = simulationStates.map((path) =>
|
||||
path.map((p) =>
|
||||
p.uuid === selectedPoint.uuid ? { ...p, position: updatedPosition, rotation: updatedRotation } : p
|
||||
)
|
||||
);
|
||||
setSimulationPaths(updatedPaths);
|
||||
setSimulationStates(updatedPaths);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const meshContext = (uuid: string) => {
|
||||
const pathIndex = simulationPaths.findIndex(path => path.some(point => point.uuid === uuid));
|
||||
const pathIndex = simulationStates.findIndex(path => path.some(point => point.uuid === uuid));
|
||||
if (pathIndex === -1) return;
|
||||
|
||||
const clickedPoint = simulationPaths[pathIndex].find(point => point.uuid === uuid);
|
||||
const clickedPoint = simulationStates[pathIndex].find(point => point.uuid === uuid);
|
||||
if (!clickedPoint) return;
|
||||
|
||||
const isStart = simulationPaths[pathIndex][0].uuid === uuid;
|
||||
const isEnd = simulationPaths[pathIndex][simulationPaths[pathIndex].length - 1].uuid === uuid;
|
||||
const isStart = simulationStates[pathIndex][0].uuid === uuid;
|
||||
const isEnd = simulationStates[pathIndex][simulationStates[pathIndex].length - 1].uuid === uuid;
|
||||
|
||||
if (pathIndex === 0 && isStart) {
|
||||
console.log("The first-ever point is not connectable.");
|
||||
@@ -285,8 +285,8 @@ const PathCreator = ({ simulationPaths, setSimulationPaths, connections, setConn
|
||||
return (
|
||||
<>
|
||||
<group name='pathObjects'>
|
||||
{/* Render finalized simulationPaths */}
|
||||
{simulationPaths.map((path, pathIndex) => (
|
||||
{/* Render finalized simulationStates */}
|
||||
{simulationStates.map((path, pathIndex) => (
|
||||
<group key={`path-line-${pathIndex}`}>
|
||||
<Line
|
||||
name={`path-line-${pathIndex}`}
|
||||
@@ -299,7 +299,7 @@ const PathCreator = ({ simulationPaths, setSimulationPaths, connections, setConn
|
||||
))}
|
||||
|
||||
{/* Render finalized points */}
|
||||
{simulationPaths.map((path) =>
|
||||
{simulationStates.map((path) =>
|
||||
path.map((point) => (
|
||||
<mesh
|
||||
key={`path-point-${point.uuid}`}
|
||||
|
||||
@@ -10,13 +10,13 @@ type PathPoint = {
|
||||
};
|
||||
|
||||
function Simulation() {
|
||||
const [simulationPaths, setSimulationPaths] = useState<{ position: THREE.Vector3; rotation: THREE.Quaternion; uuid: string }[][]>([]);
|
||||
const [simulationStates, setSimulationStates] = useState<{ position: THREE.Vector3; rotation: THREE.Quaternion; uuid: string }[][]>([]);
|
||||
const [connections, setConnections] = useState<{ start: PathPoint; end: PathPoint }[]>([]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<PathCreator simulationPaths={simulationPaths} setSimulationPaths={setSimulationPaths} connections={connections} setConnections={setConnections} />
|
||||
{simulationPaths.map((path, index) => (
|
||||
<PathCreator simulationStates={simulationStates} setSimulationStates={setSimulationStates} connections={connections} setConnections={setConnections} />
|
||||
{simulationStates.map((path, index) => (
|
||||
<PathFlow key={index} path={path} connections={connections} />
|
||||
))}
|
||||
</>
|
||||
|
||||
@@ -5,6 +5,14 @@ import { useDroppedObjectsStore } from "../../store/useDroppedObjectsStore";
|
||||
import { useZoneWidgetStore } from "../../store/useZone3DWidgetStore";
|
||||
import useTemplateStore from "../../store/useTemplateStore";
|
||||
|
||||
type WidgetData = {
|
||||
id: string;
|
||||
type: string;
|
||||
position: [number, number, number];
|
||||
rotation?: [number, number, number];
|
||||
tempPosition?: [number, number, number];
|
||||
};
|
||||
|
||||
export default function SocketRealTimeViz() {
|
||||
const { visualizationSocket } = useSocketStore();
|
||||
const { selectedZone, setSelectedZone } = useSelectedZoneStore();
|
||||
@@ -14,6 +22,7 @@ export default function SocketRealTimeViz() {
|
||||
const { addWidget } = useZoneWidgetStore()
|
||||
const { templates, removeTemplate } = useTemplateStore();
|
||||
const { setTemplates } = useTemplateStore();
|
||||
const { zoneWidgetData, setZoneWidgetData, updateWidgetPosition, updateWidgetRotation } = useZoneWidgetStore();
|
||||
|
||||
useEffect(() => {
|
||||
const email = localStorage.getItem("email") || "";
|
||||
@@ -132,14 +141,36 @@ export default function SocketRealTimeViz() {
|
||||
});
|
||||
//add 3D Widget response
|
||||
visualizationSocket.on("viz-widget3D:response:updates", (add3DWidget: any) => {
|
||||
console.log('add3DWidget: ', add3DWidget);
|
||||
|
||||
console.log('add3DWidget: ', add3DWidget);
|
||||
if (add3DWidget.success) {
|
||||
console.log('add3DWidget: ', add3DWidget);
|
||||
if (add3DWidget.message === "Widget created successfully") {
|
||||
addWidget(add3DWidget.data.zoneId, add3DWidget.data.widget);
|
||||
}
|
||||
}
|
||||
});
|
||||
//delete 3D Widget response
|
||||
visualizationSocket.on("viz-widget3D:response:delete", (delete3DWidget: any) => {
|
||||
console.log('delete3DWidget: ', delete3DWidget);
|
||||
// "3DWidget delete unsuccessfull"
|
||||
if (delete3DWidget.success && delete3DWidget.message === "3DWidget delete successfull") {
|
||||
const activeZoneWidgets = zoneWidgetData[delete3DWidget.data.zoneId] || [];
|
||||
setZoneWidgetData(
|
||||
delete3DWidget.data.zoneId,
|
||||
activeZoneWidgets.filter((w: WidgetData) => w.id !== delete3DWidget.data.id)
|
||||
);
|
||||
}
|
||||
});
|
||||
//update3D widget response
|
||||
visualizationSocket.on("viz-widget3D:response:modifyPositionRotation", (update3DWidget: any) => {
|
||||
console.log('update3DWidget: ', update3DWidget);
|
||||
|
||||
if (update3DWidget.success && update3DWidget.message==="widget update successfully") {
|
||||
updateWidgetPosition(update3DWidget.data.zoneId, update3DWidget.data.widget.id, update3DWidget.data.widget.position);
|
||||
updateWidgetRotation(update3DWidget.data.zoneId, update3DWidget.data.widget.id, update3DWidget.data.widget.rotation);
|
||||
}
|
||||
});
|
||||
// add Template response
|
||||
visualizationSocket.on("viz-template:response:add", (addingTemplate: any) => {
|
||||
console.log('addingTemplate: ', addingTemplate);
|
||||
|
||||
Reference in New Issue
Block a user