Merge pull request 'simulation-arm' (#62) from simulation-arm into main

Reviewed-on: http://185.100.212.76:7776/Dwinzo-Beta/Dwinzo_dev/pulls/62
This commit is contained in:
Vishnu 2025-04-14 04:14:54 +00:00
commit ba215dd0d3
5 changed files with 146 additions and 0 deletions

Binary file not shown.

View File

@ -55,6 +55,7 @@ import DrieHtmlTemp from "../mqttTemp/drieHtmlTemp";
import ZoneGroup from "../../builder/groups/zoneGroup";
import useModuleStore from "../../../store/useModuleStore";
import NavMeshCreator from "../../builder/agv/navMeshCreator";
import ArmReplace from "../../simulation/ik/ArmReplace";
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.
@ -370,6 +371,9 @@ export default function World() {
<NavMeshCreator lines={lines} />
{/* replacing exsisting arms with rigged ones */}
<ArmReplace />
</>
);
}

View File

@ -0,0 +1,56 @@
import React, { useEffect } from "react";
import { useThree } from "@react-three/fiber";
// store
import useModuleStore from "../../../store/useModuleStore";
// functions
import { findLinkObjects } from "./functions/findLinkObjects";
// components
import { MultiGLTFInstances } from "./MultiGLTFInstances";
// impory model from model folder
import armModel from "../../../assets/gltf-glb/rigged/ik_arm_4.glb";
// Main component to include the logic
const ArmReplace: React.FC = () => {
const { activeModule } = useModuleStore();
const { scene } = useThree(); // Access the Three.js scene from the React Fiber context
// State to store positions, rotations, and count
const [positions, setPositions] = React.useState<[number, number, number][]>(
[]
);
const [rotations, setRotations] = React.useState<[number, number, number][]>(
[]
);
const [count, setCount] = React.useState<string[]>([]);
useEffect(() => {
// Call the function to find objects and update states
findLinkObjects(
scene,
setPositions,
setRotations,
setCount,
activeModule === "simulation" ? false : true
);
}, [scene, activeModule]); // Re-run this effect if the scene changes or activeModule changes
return (
<>
{useModuleStore.getState().activeModule === "simulation" &&
count.map((_, i: number) => (
<MultiGLTFInstances
index={i}
modelUrl={armModel}
position={positions[i]}
rotation={rotations[i]}
/>
))}
</>
);
};
export default ArmReplace;

View File

@ -0,0 +1,43 @@
import { useLoader } from "@react-three/fiber";
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { clone } from "three/examples/jsm/utils/SkeletonUtils";
interface MultiGLTFInstancesProps {
index: number;
modelUrl: string;
position: [number, number, number];
rotation: [number, number, number];
}
export const MultiGLTFInstances: React.FC<MultiGLTFInstancesProps> = ({
index,
modelUrl,
position,
rotation,
}) => {
// Load GLTF model with DRACO loader for compression
const originalGltf = useLoader(GLTFLoader, modelUrl, (loader) => {
const draco = new DRACOLoader();
draco.setDecoderPath(
"https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/libs/draco/"
);
loader.setDRACOLoader(draco);
});
// Clone the model for independent transformations
const cloned = clone(originalGltf.scene);
// Render the cloned model
return (
<mesh>
<primitive
name="rigged_arm"
key={index}
object={cloned}
position={position}
rotation={rotation}
/>
</mesh>
);
};

View File

@ -0,0 +1,43 @@
import { Object3D, Vector3 } from "three";
// Function to find objects named 'link_0' and update positions, rotations, and count
export const findLinkObjects = (
scene: Object3D,
setPositions: React.Dispatch<
React.SetStateAction<[number, number, number][]>
>,
setRotations: React.Dispatch<
React.SetStateAction<[number, number, number][]>
>,
setCount: React.Dispatch<React.SetStateAction<string[]>>,
visibility: boolean
) => {
const positions: [number, number, number][] = [];
const rotations: [number, number, number][] = [];
const count: string[] = [];
let i = 0;
scene.traverse((object) => {
if (object.name === "link_0") {
if (object.parent && object.type !== "SkinnedMesh") {
// count
count[i] = object.uuid;
i++;
// Save the position and rotation of the parent object
const { x: px, y: py, z: pz } = object.parent.position;
positions.push([px, py, pz]);
const { x: rx, y: ry, z: rz } = object.parent.rotation;
rotations.push([rx, ry, rz]);
// Change visibility of the object
object.visible = visibility;
}
}
});
// Update the state with the collected positions, rotations, and count
setPositions(positions);
setRotations(rotations);
setCount(count);
};