From 169e0980240340fca23fc9df8dcaeb4ab53ac516 Mon Sep 17 00:00:00 2001 From: Jerald-Golden-B Date: Tue, 5 Aug 2025 13:10:36 +0530 Subject: [PATCH] bug fix human --- .../actionHandler/useRetrieveHandler.ts | 29 ++++- .../triggerConnections/triggerConnector.tsx | 24 ++-- .../human/instances/instance/humanUi.tsx | 36 +++--- .../instances/animator/roboticArmAnimator.tsx | 2 +- .../spatialUI/vehicle/vehicleUI.tsx | 83 +++++++------- .../instances/animator/MaterialAnimator.tsx | 9 +- .../triggerHandler/useTriggerHandler.ts | 108 +++++++++++------- app/src/store/simulation/useMaterialStore.ts | 26 ++--- 8 files changed, 183 insertions(+), 134 deletions(-) diff --git a/app/src/modules/simulation/actions/storageUnit/actionHandler/useRetrieveHandler.ts b/app/src/modules/simulation/actions/storageUnit/actionHandler/useRetrieveHandler.ts index a92ad09..ebe01ff 100644 --- a/app/src/modules/simulation/actions/storageUnit/actionHandler/useRetrieveHandler.ts +++ b/app/src/modules/simulation/actions/storageUnit/actionHandler/useRetrieveHandler.ts @@ -6,12 +6,13 @@ import { useProductContext } from "../../../products/productContext"; import { useHumanEventManager } from "../../../human/eventManager/useHumanEventManager"; export function useRetrieveHandler() { - const { materialStore, armBotStore, machineStore, vehicleStore, storageUnitStore, productStore, humanStore, assetStore } = useSceneContext(); + const { materialStore, armBotStore, machineStore, vehicleStore, storageUnitStore, conveyorStore, productStore, humanStore, assetStore } = useSceneContext(); const { selectedProductStore } = useProductContext(); const { addMaterial } = materialStore(); const { getModelUuidByActionUuid, getPointUuidByActionUuid, getEventByModelUuid, getActionByUuid } = productStore(); const { getStorageUnitById, getLastMaterial, updateCurrentLoad, removeLastMaterial } = storageUnitStore(); const { getVehicleById, incrementVehicleLoad, addCurrentMaterial } = vehicleStore(); + const { getConveyorById } = conveyorStore(); const { getHumanById, incrementHumanLoad, addCurrentMaterial: addCurrentMaterialToHuman } = humanStore(); const { getAssetById, setCurrentAnimation } = assetStore(); const { selectedProduct } = selectedProductStore(); @@ -371,6 +372,32 @@ export function useRetrieveHandler() { } return; } + } else if (triggeredModel?.type === 'transfer') { + const model = getConveyorById(triggeredModel.modelUuid); + if (model && !model.isPaused) { + if (humanAsset?.animationState?.current === 'idle') { + setCurrentAnimation(human.modelUuid, 'pickup', true, false, false); + } else if (humanAsset?.animationState?.current === 'pickup' && humanAsset.animationState.isCompleted) { + const lastMaterial = getLastMaterial(storageUnit.modelUuid); + if (lastMaterial) { + const material = createNewMaterial( + lastMaterial.materialId, + lastMaterial.materialType, + storageUnit.point.action + ); + if (material) { + removeLastMaterial(storageUnit.modelUuid); + updateCurrentLoad(storageUnit.modelUuid, -1); + incrementHumanLoad(human.modelUuid, 1); + addCurrentMaterialToHuman(human.modelUuid, material.materialType, material.materialId); + retrieveLogStatus(material.materialName, `is picked by ${human.modelName}`); + + retrievalCountRef.current.set(actionUuid, currentCount + 1); + } + } + } + return; + } } else if (triggeredModel?.type === 'machine') { const machine = getMachineById(triggeredModel.modelUuid); if (machine && !machine.isActive && machine.state === 'idle' && !machine.currentAction) { diff --git a/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx b/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx index 236ab55..1469d7f 100644 --- a/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx +++ b/app/src/modules/simulation/events/triggerConnections/triggerConnector.tsx @@ -182,31 +182,23 @@ function TriggerConnector() { const canvasElement = gl.domElement; let drag = false; - let isRightMouseDown = false; + let isLeftMouseDown = false; const onMouseDown = (evt: MouseEvent) => { if (selectedAsset) { clearSelectedAsset(); } - if (evt.button === 2) { - isRightMouseDown = true; + if (evt.button === 0) { + isLeftMouseDown = true; drag = false; } }; const onMouseUp = (evt: MouseEvent) => { - if (evt.button === 2) { - isRightMouseDown = false; + if (evt.button === 0) { + isLeftMouseDown = false; } - } - const onMouseMove = () => { - if (isRightMouseDown) { - drag = true; - } - }; - - const handleRightClick = (evt: MouseEvent) => { if (drag) return; evt.preventDefault(); @@ -368,13 +360,16 @@ function TriggerConnector() { } else if (firstSelectedPoint) { setFirstSelectedPoint(null); } + } + + const onMouseMove = () => { + drag = true; }; if (subModule === 'mechanics' && toolMode === 'cursor' && selectedAction.actionId && selectedAction.actionName) { canvasElement.addEventListener("mousedown", onMouseDown); canvasElement.addEventListener("mouseup", onMouseUp); canvasElement.addEventListener("mousemove", onMouseMove); - canvasElement.addEventListener('contextmenu', handleRightClick); } else { setFirstSelectedPoint(null); } @@ -383,7 +378,6 @@ function TriggerConnector() { canvasElement.removeEventListener("mousedown", onMouseDown); canvasElement.removeEventListener("mouseup", onMouseUp); canvasElement.removeEventListener("mousemove", onMouseMove); - canvasElement.removeEventListener('contextmenu', handleRightClick); }; }, [gl, subModule, selectedProduct, firstSelectedPoint, toolMode, selectedAction]); diff --git a/app/src/modules/simulation/human/instances/instance/humanUi.tsx b/app/src/modules/simulation/human/instances/instance/humanUi.tsx index 1fcdfeb..043811a 100644 --- a/app/src/modules/simulation/human/instances/instance/humanUi.tsx +++ b/app/src/modules/simulation/human/instances/instance/humanUi.tsx @@ -1,6 +1,6 @@ -import { useEffect, useMemo, useRef, useState } from 'react' +import { useEffect, useRef, useState } from 'react' import * as THREE from 'three' -import { Line, useGLTF } from '@react-three/drei'; +import { Tube, useGLTF } from '@react-three/drei'; import { useFrame, useThree } from '@react-three/fiber'; import { useIsDragging, useIsRotating, useSelectedAction, useSelectedEventSphere } from '../../../../../store/simulation/useSimulationStore'; import { useProductContext } from '../../../products/productContext'; @@ -395,13 +395,12 @@ const MarkerPrimitive = ({ setIsRotating: (val: any) => void; handlePointerDown: any; }) => { - const { controls, scene } = useThree(); + const { controls, scene, camera } = useThree(); const [hitPoint, setHitPoint] = useState(null); - - const lineRef = useRef(null); + const [curve, setCurve] = useState(null); useFrame(() => { - if (!refProp.current || !outerGroupRef.current) return; + if (!refProp.current || !outerGroupRef.current || !scene) return; const worldPos = new THREE.Vector3(); refProp.current.getWorldPosition(worldPos); @@ -410,6 +409,7 @@ const MarkerPrimitive = ({ const rayOrigin = worldPos.clone(); const direction = new THREE.Vector3(0, -1, 0); const raycaster = new THREE.Raycaster(rayOrigin, direction, 0.1, 1000); + raycaster.camera = camera; const intersects = raycaster.intersectObjects(scene.children, true); const hit = intersects.find(i => i.object.name !== name); @@ -418,13 +418,14 @@ const MarkerPrimitive = ({ const localHit = outerGroupRef.current.worldToLocal(hit.point.clone()); setHitPoint(localHit); - if (lineRef.current) { - const positions = new Float32Array([localMarkerPos.x, localMarkerPos.y, localMarkerPos.z, localHit.x, localHit.y, localHit.z]); - lineRef.current.setAttribute('position', new THREE.BufferAttribute(positions, 3)); - lineRef.current.attributes.position.needsUpdate = true; - } + const newCurve = new THREE.CatmullRomCurve3([ + localMarkerPos.clone(), + localHit.clone(), + ]); + setCurve(newCurve); } else { setHitPoint(null); + setCurve(null); } }); @@ -450,15 +451,16 @@ const MarkerPrimitive = ({ <> - + - - - - + {curve && ( + + + + )} )} ); -}; +}; \ No newline at end of file diff --git a/app/src/modules/simulation/roboticArm/instances/animator/roboticArmAnimator.tsx b/app/src/modules/simulation/roboticArm/instances/animator/roboticArmAnimator.tsx index 755e3ba..be3a452 100644 --- a/app/src/modules/simulation/roboticArm/instances/animator/roboticArmAnimator.tsx +++ b/app/src/modules/simulation/roboticArm/instances/animator/roboticArmAnimator.tsx @@ -200,7 +200,7 @@ function RoboticArmAnimator({ HandleCallback, restPosition, ikSolver, targetBone } } } else if (prevModel && prevModel.type === 'storageUnit') { - const position = getMaterialPosition(currentMaterial); + const position = getMaterialPosition(prevModel.modelUuid, currentMaterial); const armbotModel = scene.getObjectByProperty("uuid", armBot.modelUuid); if (armbotModel) { diff --git a/app/src/modules/simulation/spatialUI/vehicle/vehicleUI.tsx b/app/src/modules/simulation/spatialUI/vehicle/vehicleUI.tsx index 7e59da1..e287b8e 100644 --- a/app/src/modules/simulation/spatialUI/vehicle/vehicleUI.tsx +++ b/app/src/modules/simulation/spatialUI/vehicle/vehicleUI.tsx @@ -1,6 +1,6 @@ import { useEffect, useRef, useState } from "react"; import * as Types from "../../../../types/world/worldTypes"; -import { useGLTF } from "@react-three/drei"; +import { Tube, useGLTF } from "@react-three/drei"; import * as THREE from "three"; import { useFrame, useThree } from "@react-three/fiber"; import { useSelectedEventSphere, useIsDragging, useIsRotating, } from "../../../../store/simulation/useSimulationStore"; @@ -17,8 +17,6 @@ import { useVersionContext } from "../../../builder/version/versionContext"; const VehicleUI = () => { const { scene: startScene } = useGLTF(startPoint) as any; const { scene: endScene } = useGLTF(startEnd) as any; - const startMarker = useRef(null); - const endMarker = useRef(null); const prevMousePos = useRef({ x: 0, y: 0 }); const { selectedEventSphere } = useSelectedEventSphere(); const { selectedProductStore } = useProductContext(); @@ -163,7 +161,7 @@ const VehicleUI = () => { let globalStartPosition = null; let globalEndPosition = null; - if (outerGroup.current && startMarker.current && endMarker.current) { + if (outerGroup.current) { const worldPosStart = new Vector3(...startPosition); globalStartPosition = outerGroup.current.localToWorld( worldPosStart.clone() @@ -278,24 +276,19 @@ const VehicleUI = () => { const currentPointerX = state.pointer.x; const deltaX = currentPointerX - prevMousePos.current.x; prevMousePos.current.x = currentPointerX; - const marker = - isRotating === "start" ? startMarker.current : endMarker.current; - if (marker) { - const rotationSpeed = 10; - marker.rotation.y += deltaX * rotationSpeed; - if (isRotating === "start") { - setStartRotation([ - marker.rotation.x, - marker.rotation.y, - marker.rotation.z, - ]); - } else { - setEndRotation([ - marker.rotation.x, - marker.rotation.y, - marker.rotation.z, - ]); - } + const rotationSpeed = 10; + if (isRotating === "start") { + setStartRotation([ + startRotation[0], + startRotation[1] + deltaX * rotationSpeed, + startRotation[2], + ]); + } else { + setEndRotation([ + endRotation[0], + endRotation[1] + deltaX * rotationSpeed, + endRotation[2], + ]); } }); @@ -362,7 +355,7 @@ const VehicleUI = () => { position={endPosition} rotation={endRotation} outerGroupRef={outerGroup} - color="red" + color="orange" handlePointerDown={handlePointerDown} setIsDragging={setIsDragging} setIsRotating={setIsRotating} @@ -394,10 +387,10 @@ export const VehicleMarkerPrimitive = ({ setIsDragging: (val: any) => void; setIsRotating: (val: any) => void; }) => { - const { scene } = useThree(); + const { scene, camera } = useThree(); const markerRef = useRef(null); - const lineRef = useRef(null); const [hitPoint, setHitPoint] = useState(null); + const [curve, setCurve] = useState(null); useFrame(() => { if (!markerRef.current || !outerGroupRef.current) return; @@ -409,6 +402,7 @@ export const VehicleMarkerPrimitive = ({ const rayOrigin = worldPos.clone(); const direction = new THREE.Vector3(0, -1, 0); const raycaster = new THREE.Raycaster(rayOrigin, direction, 0.1, 1000); + raycaster.camera = camera; const intersects = raycaster.intersectObjects(scene.children, true); const hit = intersects.find(i => i.object.name !== name); @@ -416,16 +410,14 @@ export const VehicleMarkerPrimitive = ({ const localHit = outerGroupRef.current.worldToLocal(hit.point.clone()); setHitPoint(localHit); - if (lineRef.current) { - const positions = new Float32Array([ - localMarkerPos.x, localMarkerPos.y, localMarkerPos.z, - localHit.x, localHit.y, localHit.z - ]); - lineRef.current.setAttribute('position', new THREE.BufferAttribute(positions, 3)); - lineRef.current.attributes.position.needsUpdate = true; - } + const newCurve = new THREE.CatmullRomCurve3([ + localMarkerPos.clone(), + localHit.clone(), + ]); + setCurve(newCurve); } else { setHitPoint(null); + setCurve(null); } }); @@ -437,7 +429,13 @@ export const VehicleMarkerPrimitive = ({ object={object} position={position} rotation={rotation} - onPointerDown={(e: any) => handlePointerDown(e, name === "startMarker" ? "start" : "end", name === "startMarker" ? "start" : "end")} + onPointerDown={(e: any) => + handlePointerDown( + e, + name === "startMarker" ? "start" : "end", + name === "startMarker" ? "start" : "end" + ) + } onPointerMissed={() => { setIsDragging(null); setIsRotating(null); @@ -446,15 +444,20 @@ export const VehicleMarkerPrimitive = ({ {hitPoint && ( <> - + - + - - - - + {curve && ( + + + + )} )} diff --git a/app/src/modules/simulation/storageUnit/instances/animator/MaterialAnimator.tsx b/app/src/modules/simulation/storageUnit/instances/animator/MaterialAnimator.tsx index 60d3733..87ffd32 100644 --- a/app/src/modules/simulation/storageUnit/instances/animator/MaterialAnimator.tsx +++ b/app/src/modules/simulation/storageUnit/instances/animator/MaterialAnimator.tsx @@ -20,7 +20,7 @@ const MaterialAnimator = ({ useEffect(() => { if (!storageModel || storage.currentMaterials.length === 0) { - setMaterialPositions([]); + setMaterialPositions(storage.modelUuid, []); return; } @@ -59,12 +59,12 @@ const MaterialAnimator = ({ }; }); - setMaterialPositions(newMaterials); - }, [storageModel, storage.currentMaterials, setMaterialPositions]); + setMaterialPositions(storage.modelUuid, newMaterials); + }, [storageModel, storage.currentMaterials]); return ( - {materialPositions.map(({ materialId, position }) => { + {(materialPositions[storage.modelUuid] || []).map(({ materialId, position }) => { const mat = storage.currentMaterials.find((m) => m.materialId === materialId); return ( ); + }; export default MaterialAnimator; diff --git a/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts b/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts index 04e7fa7..b98a527 100644 --- a/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts +++ b/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts @@ -370,40 +370,18 @@ export function useTriggerHandler() { } } } else { - if (human.isActive === false && human.state === 'idle') { - - // Handle current action from arm bot - setIsPaused(materialId, true); - handleAction(action, materialId); - - } else { - - // Handle current action using Event Manager - setHumanScheduled(human.modelUuid, true); - setIsPaused(materialId, true); - addHumanToMonitor(human.modelUuid, () => { - handleAction(action, materialId) - }, action.actionUuid); - - } - } - } else { - if (human.isActive === false && human.state === 'idle') { - - // Handle current action from arm bot - setIsPaused(materialId, true); setHumanScheduled(human.modelUuid, true); - handleAction(action, materialId); - - } else { - - // Handle current action using Event Manager setIsPaused(materialId, true); - setHumanScheduled(human.modelUuid, true); addHumanToMonitor(human.modelUuid, () => { handleAction(action, materialId) }, action.actionUuid); } + } else { + setHumanScheduled(human.modelUuid, true); + setIsPaused(materialId, true); + addHumanToMonitor(human.modelUuid, () => { + handleAction(action, materialId) + }, action.actionUuid); } } } @@ -1260,12 +1238,6 @@ export function useTriggerHandler() { actionUuid: material.current.actionUuid, }) - setCurrentLocation(material.materialId, { - modelUuid: material.current.modelUuid, - pointUuid: material.current.pointUuid, - actionUuid: material.current.actionUuid, - }); - setIsPaused(material.materialId, true); setIsVisible(material.materialId, true); @@ -1279,6 +1251,13 @@ export function useTriggerHandler() { if (model?.type === 'roboticArm') { addArmBotToMonitor(model.modelUuid, () => { + + setCurrentLocation(material.materialId, { + modelUuid: material.current.modelUuid, + pointUuid: material.current.pointUuid, + actionUuid: material.current.actionUuid, + }); + setNextLocation(material.materialId, { modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '', pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '', @@ -1288,6 +1267,13 @@ export function useTriggerHandler() { }) } else if (model?.type === 'vehicle') { addVehicleToMonitor(model.modelUuid, () => { + + setCurrentLocation(material.materialId, { + modelUuid: material.current.modelUuid, + pointUuid: material.current.pointUuid, + actionUuid: material.current.actionUuid, + }); + setNextLocation(material.materialId, { modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '', pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '', @@ -1296,6 +1282,13 @@ export function useTriggerHandler() { setIsPaused(material.materialId, false); }) } else if (model?.type === 'transfer') { + + setCurrentLocation(material.materialId, { + modelUuid: material.current.modelUuid, + pointUuid: material.current.pointUuid, + actionUuid: material.current.actionUuid, + }); + setNextLocation(material.materialId, { modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '', pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '', @@ -1303,15 +1296,48 @@ export function useTriggerHandler() { setIsPaused(material.materialId, false); } else if (model?.type === 'human') { - addHumanToMonitor(model.modelUuid, () => { - setNextLocation(material.materialId, { - modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '', - pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '', - }) - + if (fromEvent.modelUuid === model.modelUuid) { setIsPaused(material.materialId, false); - }, action.actionUuid) + + setCurrentLocation(material.materialId, { + modelUuid: material.current.modelUuid, + pointUuid: material.current.pointUuid, + actionUuid: material.current.actionUuid, + }); + + setTimeout(() => { + setIsPaused(material.materialId, false); + setNextLocation(material.materialId, { + modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '', + pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '', + }) + }, 0) + + } else { + addHumanToMonitor(model.modelUuid, () => { + + setCurrentLocation(material.materialId, { + modelUuid: material.current.modelUuid, + pointUuid: material.current.pointUuid, + actionUuid: material.current.actionUuid, + }); + + setNextLocation(material.materialId, { + modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '', + pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '', + }) + + setIsPaused(material.materialId, false); + }, action.actionUuid) + } } else { + + setCurrentLocation(material.materialId, { + modelUuid: material.current.modelUuid, + pointUuid: material.current.pointUuid, + actionUuid: material.current.actionUuid, + }); + setNextLocation(material.materialId, { modelUuid: trigger.triggeredAsset?.triggeredModel.modelUuid || '', pointUuid: trigger.triggeredAsset?.triggeredPoint?.pointUuid || '', diff --git a/app/src/store/simulation/useMaterialStore.ts b/app/src/store/simulation/useMaterialStore.ts index 777bad0..68839a7 100644 --- a/app/src/store/simulation/useMaterialStore.ts +++ b/app/src/store/simulation/useMaterialStore.ts @@ -8,18 +8,21 @@ interface MaterialPosition { position: THREE.Vector3; } +type MaterialPositionsMap = Record; + type MaterialsStore = { materials: MaterialsSchema; materialHistory: MaterialHistorySchema; - materialPositions: MaterialPosition[]; + materialPositions: MaterialPositionsMap; + addMaterial: (material: MaterialSchema) => MaterialSchema | undefined; removeMaterial: (materialId: string) => MaterialSchema | undefined; clearMaterials: () => void; updateMaterial: (materialId: string, updates: Partial) => MaterialSchema | undefined; - setMaterialPositions: (materialPosition: MaterialPosition[]) => void; - getMaterialPosition: (materialId: string) => THREE.Vector3 | undefined; + setMaterialPositions: (modelUuid: string, materialPositions: MaterialPosition[]) => void; + getMaterialPosition: (modelUuid: string, materialId: string) => THREE.Vector3 | undefined; setPreviousLocation: ( materialId: string, @@ -72,7 +75,7 @@ export const createMaterialStore = () => { immer((set, get) => ({ materials: [], materialHistory: [], - materialPositions: [], + materialPositions: {}, addMaterial: (material) => { let updatedMaterial: MaterialSchema | undefined; @@ -274,21 +277,14 @@ export const createMaterialStore = () => { return updatedMaterial; }, - setMaterialPositions: (materials) => { + setMaterialPositions: (modelUuid, positions) => { set((state) => { - state.materialPositions = materials; + state.materialPositions[modelUuid] = positions; }); }, - getMaterialPosition: (materialid) => { - let position: THREE.Vector3 | undefined; - set((state) => { - const material = state.materialPositions.find(m => m.materialId === materialid); - if (material) { - position = material.position; - } - }); - return position; + getMaterialPosition: (modelUuid, materialId) => { + return get().materialPositions[modelUuid]?.find(p => p.materialId === materialId)?.position; }, getMaterialById: (materialId) => {