Files
Dwinzo_dev/app/src/modules/simulation/process/processAnimator.tsx

425 lines
13 KiB
TypeScript
Raw Normal View History

2025-04-10 10:21:24 +05:30
import React, { useRef, useEffect, useMemo } from "react";
2025-04-01 18:54:04 +05:30
import { useLoader, useFrame } from "@react-three/fiber";
2025-04-10 10:21:24 +05:30
import { GLTFLoader } from "three-stdlib";
2025-04-01 18:54:04 +05:30
import * as THREE from "three";
import { GLTF } from "three-stdlib";
2025-04-04 18:02:53 +05:30
import crate from "../../../assets/gltf-glb/crate_box.glb";
2025-04-01 18:54:04 +05:30
2025-04-10 10:21:24 +05:30
import { useProcessAnimation } from "./useProcessAnimations";
import ProcessObject from "./processObject";
import { ProcessData } from "./types";
import { useSimulationStates } from "../../../store/store";
2025-04-10 17:46:11 +05:30
import { retrieveGLTF } from "../../../utils/indexDB/idbUtils";
2025-04-01 18:54:04 +05:30
2025-04-10 10:21:24 +05:30
interface ProcessContainerProps {
processes: ProcessData[];
setProcesses: React.Dispatch<React.SetStateAction<any[]>>;
agvRef: any;
2025-04-10 17:46:11 +05:30
MaterialRef: any;
2025-04-01 18:54:04 +05:30
}
2025-04-10 10:21:24 +05:30
const ProcessAnimator: React.FC<ProcessContainerProps> = ({
2025-04-01 18:54:04 +05:30
processes,
2025-04-10 10:21:24 +05:30
setProcesses,
agvRef,
2025-04-10 17:46:11 +05:30
MaterialRef,
2025-04-01 18:54:04 +05:30
}) => {
2025-04-04 18:02:53 +05:30
const gltf = useLoader(GLTFLoader, crate) as GLTF;
2025-04-01 18:54:04 +05:30
const groupRef = useRef<THREE.Group>(null);
2025-04-10 10:21:24 +05:30
const {
animationStates,
setAnimationStates,
clockRef,
elapsedBeforePauseRef,
speedRef,
debugRef,
findSpawnPoint,
createSpawnedObject,
handlePointActions,
hasNonInheritActions,
getPointDataForAnimationIndex,
processes: processedProcesses,
checkAndCountTriggers,
} = useProcessAnimation(processes, setProcesses, agvRef);
2025-04-03 10:28:13 +05:30
const baseMaterials = useMemo(
2025-04-01 18:54:04 +05:30
() => ({
2025-04-04 09:54:08 +05:30
Box: new THREE.MeshStandardMaterial({ color: 0x8b4513 }),
Crate: new THREE.MeshStandardMaterial({ color: 0x00ff00 }),
2025-04-10 17:46:11 +05:30
// Default: new THREE.MeshStandardMaterial({ color: 0x00ff00 }),
Default: new THREE.MeshStandardMaterial(),
2025-04-01 18:54:04 +05:30
}),
[]
);
2025-04-03 10:28:13 +05:30
useEffect(() => {
2025-04-10 17:46:11 +05:30
// Update material references for all spawned objects
Object.entries(animationStates).forEach(([processId, processState]) => {
Object.keys(processState.spawnedObjects).forEach((objectId) => {
const entry = {
processId,
objectId,
2025-04-04 18:02:53 +05:30
};
2025-04-01 18:54:04 +05:30
2025-04-10 17:46:11 +05:30
const materialType =
processState.spawnedObjects[objectId]?.currentMaterialType;
2025-04-01 18:54:04 +05:30
2025-04-10 17:46:11 +05:30
if (!materialType) {
return;
}
2025-04-01 18:54:04 +05:30
2025-04-10 17:46:11 +05:30
const matRefArray = MaterialRef.current;
2025-04-01 18:54:04 +05:30
2025-04-10 17:46:11 +05:30
// Find existing material group
const existing = matRefArray.find(
(entryGroup: { material: string; objects: any[] }) =>
entryGroup.material === materialType
);
2025-04-01 18:54:04 +05:30
2025-04-10 17:46:11 +05:30
if (existing) {
// Check if this processId + objectId already exists
const alreadyExists = existing.objects.some(
(o: any) =>
o.processId === entry.processId && o.objectId === entry.objectId
);
2025-04-03 10:28:13 +05:30
2025-04-10 17:46:11 +05:30
if (!alreadyExists) {
existing.objects.push(entry);
}
} else {
// Create new group for this material type
matRefArray.push({
material: materialType,
objects: [entry],
});
}
});
});
}, [animationStates, MaterialRef, agvRef]);
2025-04-01 18:54:04 +05:30
2025-04-10 10:21:24 +05:30
// In processAnimator.tsx - only the relevant spawn logic part that needs fixes
2025-04-03 10:28:13 +05:30
2025-04-04 18:02:53 +05:30
useFrame(() => {
2025-04-10 10:21:24 +05:30
// Spawn logic frame
2025-04-04 18:02:53 +05:30
const currentTime =
clockRef.current.getElapsedTime() - elapsedBeforePauseRef.current;
2025-04-03 10:28:13 +05:30
setAnimationStates((prev) => {
const newStates = { ...prev };
2025-04-10 10:21:24 +05:30
processedProcesses.forEach((process) => {
2025-04-03 10:28:13 +05:30
const processState = newStates[process.id];
if (!processState) return;
2025-04-03 18:24:06 +05:30
if (processState.isProcessDelaying) {
2025-04-10 10:21:24 +05:30
// Existing delay handling logic...
2025-04-04 09:54:08 +05:30
return;
2025-04-03 18:24:06 +05:30
}
2025-04-03 10:28:13 +05:30
const spawnPoint = findSpawnPoint(process);
if (!spawnPoint || !spawnPoint.actions) return;
const spawnAction = spawnPoint.actions.find(
(a) => a.isUsed && a.type === "Spawn"
);
if (!spawnAction) return;
const spawnInterval =
typeof spawnAction.spawnInterval === "number"
? spawnAction.spawnInterval
2025-04-03 18:24:06 +05:30
: parseFloat(spawnAction.spawnInterval as string) || 0;
2025-04-03 10:28:13 +05:30
2025-04-10 10:21:24 +05:30
// Check if this is a zero interval spawn and we already spawned an object
if (
spawnInterval === 0 &&
processState.hasSpawnedZeroIntervalObject === true
) {
return; // Don't spawn more objects for zero interval
}
2025-04-04 18:02:53 +05:30
const effectiveSpawnInterval = spawnInterval / speedRef.current;
2025-04-03 10:28:13 +05:30
if (currentTime >= processState.nextSpawnTime) {
const objectId = `obj-${process.id}-${processState.objectIdCounter}`;
2025-04-03 18:24:06 +05:30
const newObject = createSpawnedObject(
process,
currentTime,
spawnAction.material || "Default",
2025-04-10 10:21:24 +05:30
spawnPoint,
baseMaterials
2025-04-03 18:24:06 +05:30
);
2025-04-10 10:21:24 +05:30
// Update state with the new object and flag for zero interval
2025-04-03 10:28:13 +05:30
newStates[process.id] = {
...processState,
spawnedObjects: {
...processState.spawnedObjects,
2025-04-03 18:24:06 +05:30
[objectId]: newObject,
2025-04-03 10:28:13 +05:30
},
objectIdCounter: processState.objectIdCounter + 1,
2025-04-04 18:02:53 +05:30
nextSpawnTime: currentTime + effectiveSpawnInterval,
2025-04-10 10:21:24 +05:30
// Mark that we've spawned an object for zero interval case
hasSpawnedZeroIntervalObject:
spawnInterval === 0
? true
: processState.hasSpawnedZeroIntervalObject,
2025-04-03 10:28:13 +05:30
};
2025-04-01 18:54:04 +05:30
}
2025-04-03 10:28:13 +05:30
});
return newStates;
});
});
2025-04-04 18:02:53 +05:30
useFrame((_, delta) => {
2025-04-10 10:21:24 +05:30
// Animation logic frame
2025-04-04 18:02:53 +05:30
const currentTime =
clockRef.current.getElapsedTime() - elapsedBeforePauseRef.current;
2025-04-01 18:54:04 +05:30
2025-04-03 10:28:13 +05:30
setAnimationStates((prev) => {
const newStates = { ...prev };
2025-04-10 10:21:24 +05:30
processedProcesses.forEach((process) => {
2025-04-03 10:28:13 +05:30
const processState = newStates[process.id];
if (!processState) return;
2025-04-03 18:24:06 +05:30
if (processState.isProcessDelaying) {
2025-04-04 18:02:53 +05:30
const effectiveDelayTime =
processState.processDelayDuration / speedRef.current;
2025-04-03 18:24:06 +05:30
if (
currentTime - processState.processDelayStartTime >=
2025-04-04 18:02:53 +05:30
effectiveDelayTime
2025-04-03 18:24:06 +05:30
) {
newStates[process.id] = {
...processState,
isProcessDelaying: false,
spawnedObjects: Object.entries(
processState.spawnedObjects
).reduce(
(acc, [id, obj]) => ({
...acc,
[id]: {
...obj,
state: {
...obj.state,
isDelaying: false,
delayComplete: true,
2025-04-04 09:54:08 +05:30
isAnimating: true,
2025-04-03 18:24:06 +05:30
progress:
obj.state.progress === 0 ? 0.005 : obj.state.progress,
},
},
}),
{}
),
};
return newStates;
} else {
return newStates;
}
}
2025-04-03 10:28:13 +05:30
const path =
process.animationPath?.map((p) => new THREE.Vector3(p.x, p.y, p.z)) ||
[];
if (path.length < 2) return;
const updatedObjects = { ...processState.spawnedObjects };
Object.entries(processState.spawnedObjects).forEach(
([objectId, obj]) => {
2025-04-03 18:24:06 +05:30
if (!obj.visible) return;
2025-04-03 10:28:13 +05:30
const currentRef = gltf?.scene ? obj.ref.current : obj.ref.current;
if (!currentRef) return;
2025-04-03 18:24:06 +05:30
if (
obj.position &&
obj.state.currentIndex === 0 &&
obj.state.progress === 0
) {
currentRef.position.copy(obj.position);
}
2025-04-03 10:28:13 +05:30
const stateRef = obj.state;
2025-04-03 18:24:06 +05:30
if (stateRef.isDelaying) {
2025-04-04 18:02:53 +05:30
const effectiveDelayTime =
stateRef.currentDelayDuration / speedRef.current;
if (currentTime - stateRef.delayStartTime >= effectiveDelayTime) {
2025-04-03 18:24:06 +05:30
stateRef.isDelaying = false;
stateRef.delayComplete = true;
2025-04-04 09:54:08 +05:30
stateRef.isAnimating = true;
2025-04-03 18:24:06 +05:30
if (stateRef.progress === 0) {
stateRef.progress = 0.005;
}
const nextPointIdx = stateRef.currentIndex + 1;
if (nextPointIdx < path.length) {
const slightProgress = Math.max(stateRef.progress, 0.005);
currentRef.position.lerpVectors(
path[stateRef.currentIndex],
nextPointIdx < path.length
? path[nextPointIdx]
: path[stateRef.currentIndex],
slightProgress
);
}
} else {
updatedObjects[objectId] = { ...obj, state: { ...stateRef } };
return;
}
}
if (!stateRef.isAnimating) return;
2025-04-03 10:28:13 +05:30
const currentPointData = getPointDataForAnimationIndex(
process,
stateRef.currentIndex
);
2025-04-10 10:21:24 +05:30
// Handle point actions when first arriving at point
2025-04-03 10:28:13 +05:30
if (stateRef.progress === 0 && currentPointData?.actions) {
const shouldStop = handlePointActions(
process.id,
objectId,
currentPointData.actions,
2025-04-10 10:21:24 +05:30
currentTime,
processedProcesses,
baseMaterials
2025-04-03 10:28:13 +05:30
);
2025-04-03 18:24:06 +05:30
if (shouldStop) {
updatedObjects[objectId] = { ...obj, state: { ...stateRef } };
return;
2025-04-03 10:28:13 +05:30
}
}
const nextPointIdx = stateRef.currentIndex + 1;
const isLastPoint = nextPointIdx >= path.length;
2025-04-10 17:46:11 +05:30
// if (isLastPoint) {
// if (currentPointData?.actions) {
// const shouldStop = !hasNonInheritActions(
// currentPointData.actions
// );
// if (shouldStop) {
// return;
// }
// }
// }
2025-04-03 10:28:13 +05:30
if (isLastPoint) {
if (currentPointData?.actions) {
2025-04-10 17:46:11 +05:30
const hasNonInherit = hasNonInheritActions(
2025-04-03 10:28:13 +05:30
currentPointData.actions
);
2025-04-10 17:46:11 +05:30
if (!hasNonInherit) {
// Remove the object if all actions are inherit
updatedObjects[objectId] = {
...obj,
visible: false,
state: { ...stateRef, isAnimating: false },
};
2025-04-03 10:28:13 +05:30
return;
}
2025-04-10 17:46:11 +05:30
} else {
// No actions at last point - remove the object
updatedObjects[objectId] = {
...obj,
visible: false,
state: { ...stateRef, isAnimating: false },
};
return;
2025-04-03 10:28:13 +05:30
}
}
if (!isLastPoint) {
const nextPoint = path[nextPointIdx];
const distance =
path[stateRef.currentIndex].distanceTo(nextPoint);
2025-04-04 18:02:53 +05:30
const effectiveSpeed = stateRef.speed * speedRef.current;
const movement = effectiveSpeed * delta;
2025-04-03 18:24:06 +05:30
if (stateRef.delayComplete && stateRef.progress < 0.01) {
2025-04-04 09:54:08 +05:30
stateRef.progress = 0.05;
stateRef.delayComplete = false;
2025-04-03 18:24:06 +05:30
} else {
stateRef.progress += movement / distance;
}
2025-04-03 10:28:13 +05:30
if (stateRef.progress >= 1) {
stateRef.currentIndex = nextPointIdx;
stateRef.progress = 0;
currentRef.position.copy(nextPoint);
2025-04-03 18:24:06 +05:30
2025-04-10 10:21:24 +05:30
// TRIGGER CHECK - When object arrives at new point
checkAndCountTriggers(
process.id,
objectId,
stateRef.currentIndex, // The new point index
processedProcesses,
currentTime
);
2025-04-03 18:24:06 +05:30
const newPointData = getPointDataForAnimationIndex(
process,
stateRef.currentIndex
);
2025-04-03 10:28:13 +05:30
} else {
currentRef.position.lerpVectors(
path[stateRef.currentIndex],
nextPoint,
stateRef.progress
);
}
}
updatedObjects[objectId] = { ...obj, state: { ...stateRef } };
}
2025-04-01 18:54:04 +05:30
);
2025-04-03 10:28:13 +05:30
newStates[process.id] = {
...processState,
spawnedObjects: updatedObjects,
};
});
return newStates;
});
2025-04-01 18:54:04 +05:30
});
2025-04-10 10:21:24 +05:30
if (!processedProcesses || processedProcesses.length === 0) {
2025-04-01 18:54:04 +05:30
return null;
}
2025-04-03 10:28:13 +05:30
return (
2025-04-10 10:21:24 +05:30
<group ref={groupRef}>
2025-04-03 10:28:13 +05:30
{Object.entries(animationStates).flatMap(([processId, processState]) =>
Object.entries(processState.spawnedObjects)
.filter(([_, obj]) => obj.visible)
.map(([objectId, obj]) => {
2025-04-10 10:21:24 +05:30
const process = processedProcesses.find((p) => p.id === processId);
2025-04-04 09:54:08 +05:30
2025-04-03 10:28:13 +05:30
const renderAs = process?.renderAs || "custom";
2025-04-10 10:21:24 +05:30
return (
<ProcessObject
key={objectId}
objectId={objectId}
obj={obj}
renderAs={renderAs}
gltf={gltf}
/>
);
2025-04-03 10:28:13 +05:30
})
)}
2025-04-10 10:21:24 +05:30
</group>
2025-04-03 10:28:13 +05:30
);
2025-04-01 18:54:04 +05:30
};
export default ProcessAnimator;