From 8bcefd93bc73decd8790851b78cfaba899070171 Mon Sep 17 00:00:00 2001 From: crosstyan Date: Thu, 27 Mar 2025 16:14:27 +0800 Subject: [PATCH] Refactor Human3DSkeleton component to support frame-based animation, allowing for customizable start frame and frame rate. Remove deprecated Joints component for cleaner code. Update instance in Scene to reflect new props. --- src/App.tsx | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index a765911..15804bf 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -256,15 +256,39 @@ const Scene = () => { } interface Human3DSkeletonProps { - index: number + startFrame?: number jointRadius?: number boneRadius?: number showJoints?: boolean showBones?: boolean + frameRate?: number } - const Human3DSkeleton = ({ index, jointRadius = 0.01, boneRadius = 0.005, showJoints = true, showBones = true }: Human3DSkeletonProps) => { - const joints = POSE_3D[index] + const Human3DSkeleton = ({ + startFrame = 0, + jointRadius = 0.01, + boneRadius = 0.005, + showJoints = true, + showBones = true, + frameRate = 30 + }: Human3DSkeletonProps) => { + const [frameIndex, setFrameIndex] = useState(startFrame) + const totalFrames = POSE_3D.length + + // Use frame to animate through the skeleton poses + useFrame((state, delta) => { + // Calculate next frame based on desired frame rate and delta time + setFrameIndex(prevFrame => { + // Calculate next frame + const nextFrame = prevFrame + frameRate * delta + // Loop back to start if we reach the end + return nextFrame >= totalFrames ? 0 : nextFrame + }) + }) + + // Get the current frame joints - use Math.floor to get the nearest frame + const currentFrame = Math.floor(frameIndex) % totalFrames + const joints = POSE_3D[currentFrame] // Function to get appropriate color for a joint index const getJointColor = (idx: number) => { @@ -370,16 +394,6 @@ const Scene = () => { ) } - interface JointsProps { - index: number - radius?: number - } - - // Keep the old Joints component for compatibility - const Joints = ({ index, radius }: JointsProps) => { - return - } - const scene = ( {/* */} @@ -393,14 +407,13 @@ const Scene = () => { return })} - + ) return ( // Note that we don't need to import anything, All three.js objects will be treated // as native JSX elements, just like you can just write
or in // regular ReactDOM. The general rule is that Fiber components are available under // the camel-case version of their name in three.js. - <>