2025-05-05 14:26:38 +05:30
|
|
|
import React, { useEffect, useMemo, useRef, useState } from 'react'
|
2025-05-03 18:36:30 +05:30
|
|
|
import * as THREE from 'three';
|
2025-05-05 14:26:38 +05:30
|
|
|
import MaterialAnimator from '../animator/materialAnimator';
|
|
|
|
|
import { useProductStore } from '../../../../../store/simulation/useProductStore';
|
|
|
|
|
import { useSelectedProduct } from '../../../../../store/simulation/useSimulationStore';
|
|
|
|
|
import { MaterialModel } from '../material/materialModel';
|
|
|
|
|
import { useThree } from '@react-three/fiber';
|
2025-04-23 15:29:51 +05:30
|
|
|
|
2025-05-03 18:36:30 +05:30
|
|
|
function MaterialInstance({ material }: { material: MaterialSchema }) {
|
2025-05-05 14:26:38 +05:30
|
|
|
const matRef: any = useRef();
|
|
|
|
|
const { scene } = useThree();
|
|
|
|
|
const { getModelUuidByPointUuid, getPointByUuid, getEventByModelUuid } = useProductStore();
|
|
|
|
|
const { selectedProduct } = useSelectedProduct();
|
|
|
|
|
|
|
|
|
|
const getWorldPositionFromScene = (pointUuid: string): THREE.Vector3 | null => {
|
|
|
|
|
const pointObj = scene.getObjectByProperty("uuid", pointUuid);
|
|
|
|
|
if (!pointObj) return null;
|
|
|
|
|
|
|
|
|
|
const worldPosition = new THREE.Vector3();
|
|
|
|
|
pointObj.getWorldPosition(worldPosition);
|
|
|
|
|
return worldPosition;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { position, rotation, speed } = useMemo(() => {
|
|
|
|
|
if (!material.current?.pointUuid) {
|
|
|
|
|
return { position: new THREE.Vector3(0, 0, 0), rotation: new THREE.Vector3(0, 0, 0), speed: 1 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const modelUuid = getModelUuidByPointUuid(selectedProduct.productId, material.current.pointUuid);
|
|
|
|
|
if (!modelUuid) {
|
|
|
|
|
return { position: new THREE.Vector3(0, 0, 0), rotation: new THREE.Vector3(0, 0, 0), speed: 1 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const speed = getCurrentSpeed(selectedProduct.productId, modelUuid);
|
|
|
|
|
|
|
|
|
|
const point = getPointByUuid(selectedProduct.productId, modelUuid, material.current.pointUuid);
|
|
|
|
|
if (!point) {
|
|
|
|
|
return { position: new THREE.Vector3(0, 0, 0), rotation: new THREE.Vector3(0, 0, 0), speed: 1 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const position = getWorldPositionFromScene(point.uuid);
|
|
|
|
|
if (position) {
|
|
|
|
|
return { position: position, rotation: new THREE.Vector3(0, 0, 0), speed: 1 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
position: new THREE.Vector3(...point.position),
|
|
|
|
|
rotation: new THREE.Vector3(...point.rotation),
|
|
|
|
|
speed: speed || 1
|
|
|
|
|
};
|
|
|
|
|
}, [material, getPointByUuid]);
|
|
|
|
|
|
|
|
|
|
function getCurrentSpeed(productId: string, modelUuid: string) {
|
|
|
|
|
const event = getEventByModelUuid(productId, modelUuid)
|
|
|
|
|
if (event) {
|
|
|
|
|
if (event.type === 'transfer') {
|
|
|
|
|
return event.speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'vehicle') {
|
|
|
|
|
return event.speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'machine') {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'roboticArm') {
|
|
|
|
|
return event.speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'storageUnit') {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-03 18:36:30 +05:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// console.log('material: ', material);
|
|
|
|
|
}, [material])
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
|
2025-05-05 14:26:38 +05:30
|
|
|
<MaterialModel matRef={matRef} materialType={material.materialType} position={position} />
|
2025-05-03 18:36:30 +05:30
|
|
|
|
2025-05-05 14:26:38 +05:30
|
|
|
<MaterialAnimator
|
|
|
|
|
matRef={matRef}
|
|
|
|
|
material={material}
|
|
|
|
|
speed={speed}
|
|
|
|
|
onAnimationComplete={() => { console.log('123');}}
|
|
|
|
|
/>
|
2025-05-03 18:36:30 +05:30
|
|
|
</>
|
|
|
|
|
)
|
2025-04-23 15:29:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MaterialInstance
|