Refactor MaterialAnimator to replace sphereRef with materialRef for clarity and consistency
This commit is contained in:
@@ -10,7 +10,7 @@ type MaterialAnimatorProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default function MaterialAnimator({ ikSolver, armBot, currentPhase }: Readonly<MaterialAnimatorProps>) {
|
export default function MaterialAnimator({ ikSolver, armBot, currentPhase }: Readonly<MaterialAnimatorProps>) {
|
||||||
const sphereRef = useRef<any>(null);
|
const materialRef = useRef<any>(null);
|
||||||
const [isRendered, setIsRendered] = useState<boolean>(false);
|
const [isRendered, setIsRendered] = useState<boolean>(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -22,7 +22,7 @@ export default function MaterialAnimator({ ikSolver, armBot, currentPhase }: Rea
|
|||||||
}, [currentPhase]);
|
}, [currentPhase]);
|
||||||
|
|
||||||
useFrame(() => {
|
useFrame(() => {
|
||||||
if (!ikSolver || !sphereRef.current) return;
|
if (!ikSolver || !materialRef.current) return;
|
||||||
|
|
||||||
const boneTarget = ikSolver.mesh.skeleton.bones.find((b: any) => b.name === "Bone004");
|
const boneTarget = ikSolver.mesh.skeleton.bones.find((b: any) => b.name === "Bone004");
|
||||||
const bone = ikSolver.mesh.skeleton.bones.find((b: any) => b.name === "Effector");
|
const bone = ikSolver.mesh.skeleton.bones.find((b: any) => b.name === "Effector");
|
||||||
@@ -39,17 +39,16 @@ export default function MaterialAnimator({ ikSolver, armBot, currentPhase }: Rea
|
|||||||
// Calculate direction
|
// Calculate direction
|
||||||
const direction = new THREE.Vector3();
|
const direction = new THREE.Vector3();
|
||||||
direction.subVectors(boneWorldPos, boneTargetWorldPos).normalize();
|
direction.subVectors(boneWorldPos, boneTargetWorldPos).normalize();
|
||||||
const downwardDirection = direction.clone().negate();
|
|
||||||
|
|
||||||
const adjustedPosition = boneWorldPos.clone().addScaledVector(downwardDirection, -0.01);
|
const adjustedPosition = boneWorldPos.clone().addScaledVector(direction, 0.01);
|
||||||
|
|
||||||
//set position
|
//set position
|
||||||
sphereRef.current.position.copy(adjustedPosition);
|
materialRef.current.position.copy(adjustedPosition);
|
||||||
|
|
||||||
// Set rotation
|
// Set rotation
|
||||||
const worldQuaternion = new THREE.Quaternion();
|
const worldQuaternion = new THREE.Quaternion();
|
||||||
bone.getWorldQuaternion(worldQuaternion);
|
bone.getWorldQuaternion(worldQuaternion);
|
||||||
sphereRef.current.quaternion.copy(worldQuaternion);
|
materialRef.current.quaternion.copy(worldQuaternion);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -58,7 +57,7 @@ export default function MaterialAnimator({ ikSolver, armBot, currentPhase }: Rea
|
|||||||
{isRendered && (
|
{isRendered && (
|
||||||
<MaterialModel
|
<MaterialModel
|
||||||
materialId={armBot.currentAction?.materialId ?? ''}
|
materialId={armBot.currentAction?.materialId ?? ''}
|
||||||
matRef={sphereRef}
|
matRef={materialRef}
|
||||||
materialType={armBot.currentAction?.materialType ?? 'Default material'}
|
materialType={armBot.currentAction?.materialType ?? 'Default material'}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user