R3f Animation

enzed/r3f-skills/skills/r3f-animation

作者 enzed4a11805f0973无许可证128 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5周前更新

Animate React Three Fiber objects with useFrame, damping, and GLTF clips. Use for procedural motion, animation transitions, or demand-loop animation; use physics guidance for collision-driven movement.

AI 生成的概览

指导使用 useFrame、阻尼和 GLTF 片段为 React Three Fiber 对象制作动画。

功能
提供为 React Three Fiber 场景制作动画的指导和代码示例,涵盖使用 useFrame 与 ref 实现连续变换、可回到空闲状态的阻尼,以及用 Drei useAnimations 播放制作好的 GLTF 片段。还涉及计时、性能、按需渲染循环的失效触发,以及在不同帧率下验证运动效果。另有一个参考文件说明片段播放与清理。
适用场景
适用于 React Three Fiber 项目中的程序化运动、动画过渡或按需循环动画。也适合在判断动画应由 useFrame、补间库还是物理引擎负责时使用。
运行要求
无脚本,仅包含说明和代码示例。假定已有 React Three Fiber 项目,并涉及已安装的 Fiber、Drei、React 和 Three.js 版本,以及可选的补间库或物理引擎。

React Three Fiber animation

Check installed Fiber, Drei, and React versions first. These examples target Fiber 9 / React 19. Keep the project's animation library when it already meets the task.

Choose an owner

  • Use useFrame and refs for continuous transforms/uniforms; React state describes discrete goals such as selected, open, or active.
  • Use Drei useAnimations for authored GLTF clips. Read clip playback [blocked] for independent animated instances and cleanup.
  • Use the project's spring/tween library for coordinated transitions; check its React peer dependencies before adding it. Do not introduce a state manager merely to animate a mesh.
  • Let Rapier own simulated body transforms. Animate kinematic targets through physics APIs rather than fighting the simulation with mesh positions.

Damping that can return to idle

Mount below Canvas; this also works with frameloop="demand". Click the box to change its target.

tsx
import { useEffect, useRef, useState } from 'react'import { useFrame, useThree } from '@react-three/fiber'import { MathUtils, type Mesh } from 'three'
export default function Example() {  const mesh = useRef<Mesh>(null)  const [active, setActive] = useState(false)  const invalidate = useThree((state) => state.invalidate)  const target = active ? 1 : -1  useEffect(() => { invalidate() }, [target, invalidate])
  useFrame((_, delta) => {    if (!mesh.current) return    // Cap a visual transition's resume jump, not a physics simulation step.    const x = MathUtils.damp(mesh.current.position.x, target, 6, Math.min(delta, 0.1))    const moving = Math.abs(x - target) > 0.001    mesh.current.position.x = moving ? x : target    if (moving) invalidate()  })
  return (    <mesh ref={mesh} name="moving-box" onClick={() => setActive((value) => !value)}>      <boxGeometry />      <meshStandardMaterial color={active ? 'orange' : 'coral'} />    </mesh>  )}

Timing and performance

  • delta is seconds. Integrate velocity as position += velocity * delta; use MathUtils.damp or lerp with 1 - exp(-lambda * delta) for exponential smoothing. A fixed lerp fraction depends on refresh rate; delta * speed can overshoot.
  • Reuse vectors/quaternions; avoid allocating them in every frame. Use quaternion interpolation for orientation instead of independently lerping Euler angles across wrap boundaries.
  • Use one timeline/clock owner. Fiber 9 still supplies state.clock; do not replace Fiber internals because Three.js deprecated new standalone Clock instances. For independent Three.js timing, consult Timer in the installed release.
  • On demand, external animation systems need invalidation while active. Invalidate before starting a synchronous animation and arrange the start on the next frame if the first frame would otherwise jump.
  • Read fast external state directly in the frame callback when supported (e.g. a store's getState()); subscribe React only to UI-relevant changes. Clean up external subscriptions.
  • Do not claim a normal React re-render stops animation. Profile actual work and subscription frequency before memoizing components.
  • Hiding an object does not stop its frame callback or mixer. Pause expensive animation explicitly when appropriate; respect reduced-motion preferences for decorative motion.
  • Repeated oscillations and shader motion can derive phase from elapsed time; simulations needing reproducibility require a fixed-step design, not arbitrary delta clamping.

Verify

Compare motion at different frame rates, background/resume behavior, and repeated target changes. On demand, confirm the object wakes, reaches its goal, and stops requesting frames. For clips, test two instances playing different actions.

Sources

来源与署名

来源:enzed/r3f-skills位于skills/r3f-animation提交4a11805

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架

R3f Animation · skills/r3f-animation 智能体技能 | SourceWeft