From 65ef6839a0235990f88645c73baf030be17a68c9 Mon Sep 17 00:00:00 2001 From: Jerald-Golden-B Date: Wed, 9 Jul 2025 13:38:06 +0530 Subject: [PATCH] feat: Enhance IK handling in Model and IKInstance components with improved data structure and validation --- .../builder/asset/models/model/model.tsx | 5 +- .../instances/ikInstance/ikInstance.tsx | 46 +++++++------------ app/src/types/simulationTypes.d.ts | 21 ++++++++- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/app/src/modules/builder/asset/models/model/model.tsx b/app/src/modules/builder/asset/models/model/model.tsx index 2b5df51..7baa4b7 100644 --- a/app/src/modules/builder/asset/models/model/model.tsx +++ b/app/src/modules/builder/asset/models/model/model.tsx @@ -81,7 +81,8 @@ function Model({ asset }: { readonly asset: Asset }) { if (!ikData && asset.eventData && asset.eventData.type === 'ArmBot') { getAssetIksApi(asset.assetId).then((data) => { if (data.iks) { - setIkData(data.iks); + const iks: IK[] = data.iks; + setIkData(iks); } }) } @@ -463,7 +464,7 @@ function Model({ asset }: { readonly asset: Asset }) { position={asset.position} rotation={asset.rotation} visible={asset.isVisible} - userData={asset} + userData={{ ...asset, iks: ikData }} onDoubleClick={(e) => { e.stopPropagation(); if (!toggleView) { diff --git a/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx b/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx index d78077a..897c6d1 100644 --- a/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx +++ b/app/src/modules/simulation/roboticArm/instances/ikInstance/ikInstance.tsx @@ -22,7 +22,7 @@ function IKInstance({ setIkSolver, armBot }: IKInstanceProps) { const trySetup = () => { const targetMesh = scene?.getObjectByProperty("uuid", armBot.modelUuid); - if (!targetMesh) { + if (!targetMesh || !targetMesh.userData.iks || targetMesh.userData.iks.length < 1) { retryId = setTimeout(trySetup, 100); return; } @@ -33,34 +33,22 @@ function IKInstance({ setIkSolver, armBot }: IKInstanceProps) { if (n.name === skinnedMeshName) OOI.Skinned_Mesh = n; }); if (!OOI.Target_Bone || !OOI.Skinned_Mesh) return; - const iks = [ - { - target: 7, - effector: 6, - links: [ - { - index: 5, - enabled: true, - rotationMin: new THREE.Vector3(-Math.PI / 2, 0, 0), - rotationMax: new THREE.Vector3(Math.PI / 2, 0, 0), - }, - { - index: 4, - enabled: true, - rotationMin: new THREE.Vector3(-Math.PI / 2, 0, 0), - rotationMax: new THREE.Vector3(0, 0, 0), - }, - { - index: 3, - enabled: true, - rotationMin: new THREE.Vector3(0, 0, 0), - rotationMax: new THREE.Vector3(2, 0, 0), - }, - { index: 1, enabled: true, limitation: new THREE.Vector3(0, 1, 0) }, - { index: 0, enabled: false, limitation: new THREE.Vector3(0, 0, 0) }, - ], - }, - ]; + + const rawIks: IK[] = targetMesh.userData.iks; + const iks = rawIks.map((ik) => ({ + target: ik.target, + effector: ik.effector, + links: ik.links.map((link) => ({ + index: link.index, + enabled: link.enabled, + rotationMin: link.rotationMin ? new THREE.Vector3(...link.rotationMin) : undefined, + rotationMax: link.rotationMax ? new THREE.Vector3(...link.rotationMax) : undefined, + limitation: link.limitation ? new THREE.Vector3(...link.limitation) : undefined, + })), + ringRadius: ik.ringRadius, + maxheight: ik.maxheight, + minheight: ik.minheight, + })); const solver = new CCDIKSolver(OOI.Skinned_Mesh, iks); setIkSolver(solver); diff --git a/app/src/types/simulationTypes.d.ts b/app/src/types/simulationTypes.d.ts index 133cb74..a39a3ec 100644 --- a/app/src/types/simulationTypes.d.ts +++ b/app/src/types/simulationTypes.d.ts @@ -286,4 +286,23 @@ interface MaterialHistoryEntry { removedAt: string; } -type MaterialHistorySchema = MaterialHistoryEntry[]; \ No newline at end of file +type MaterialHistorySchema = MaterialHistoryEntry[]; + +//IK + +type Link = { + index: number; + enabled: boolean; + rotationMin?: [number, number, number]; + rotationMax?: [number, number, number]; + limitation?: [number, number, number]; +}; + +type IK = { + target: number; + effector: number; + links: Link[]; + ringRadius?: number; + maxheight?: number; + minheight?: number; +} ;