agentleFS
lklynelklyne/skills/react-three-fiber/SKILL.md

react-three-fiber

Skill0 starsChanged 7 months agoMITView on GitHub

React Three Fiber (R3F) — declarative Three.js for React. Use when the user is building 3D scenes with `@react-three/fiber`, working with `<Canvas>`, `useFrame`, `useThree`, `useLoader`, hits performance issues (jank, frozen shaders, GC stutter, re-renders in the render loop), or asks about R3F pitfalls, instancing, ref-based animation, or memoizing uniforms with Leva.

4 other projects use a version of this.

Adopted by

Most recently taken up by

  • Other versions4 projects use a version of this.

    Word for word · 0

    None found.

    Edited · 0

    None found.

    Wrote their own version · 4

    1. vercel-labs/json-render★ 18ksame name
    2. freshtechbro/claudedesignskills★ 975same name
    3. xenodeve/xeno-skills★ 2same name
    4. SkotAlexsander/skills★ 1same name

    How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.

  • In this repository2 other files it gives its agents.

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 4 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Mutate per-frame in useFrame: Update .value in place — no new objects, no setState.

    line 38
  2. Very specific:

    Memoize the Leva control schema: if it's computed from props or state, so useControls doesn't tear down and rebuild the panel.

    line 39

“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

React Three Fiber

Declarative Three.js as React components. The render loop runs outside React — useFrame fires every frame, but React reconciliation does not. Most R3F bugs and perf problems come from forgetting that boundary.

Performance pitfalls

The single most important rule: don't drive the render loop through React state. Everything below is a corollary.

  • No setState in useFrame or fast event handlers. Each setState triggers reconciliation; at 60fps you'll re-render the whole subtree 60 times per second. Mutate refs instead. Use the delta arg from useFrame for refresh-rate-independent motion.
  • Animate in useFrame (lerp/damp) or with react-spring. Both run outside React. Driving an animation through React state is the most common source of R3F jank.
  • Don't reactively subscribe components to fast-changing store state. Selector subscriptions re-render on every change. Inside useFrame, read with store.getState() directly — no subscription, no re-render.
  • Share materials and geometries. Each unique material/geometry is a shader compile and a GPU upload. Reuse them across meshes. For many similar meshes, use instancing (<instancedMesh> or drei's <Instances>).
  • Avoid remounting expensive subtrees. Toggle visible instead of conditionally rendering. Mounting recompiles materials and re-uploads geometry.
  • Hoist temp objects. Allocating a new THREE.Vector3() inside useFrame allocates 60 objects per second per call site. The GC pause shows up as periodic stutter. Hoist Vector3/Matrix4/Color outside the frame callback and reuse via .set().
  • Use useLoader for assets. Loaders cache globally by URL — calling useLoader(GLTFLoader, url) from multiple components hits the cache. For GLTF scenes you reuse heavily, prefer gltfjsx-generated components: the JSX is immutable and the underlying buffers are shared.
  • For expensive state changes, wrap in startTransition. Concurrent mode de-prioritizes the work so the render loop stays responsive while the heavy update lands on a later commit.
  • Memoize uniforms objects. A new uniforms object every render means useFrame mutates a stale reference while the material reads from a fresh one — the shader appears frozen even though uniforms.uTime.value is incrementing. useMemo it once with [] deps.

Leva + uniforms

The pattern for live-tweaking uniforms with Leva without re-rendering. All five rules below collapse to one principle: the uniforms object is mutated, never replaced.

  • Memoize the uniforms object with [] deps so the reference is stable for the lifetime of the component.
  • Drive Leva via onChange, not the return value. Mutate uniforms.*.value directly; never call setState.
  • Mutate per-frame in useFrame. Update .value in place — no new objects, no setState.
  • Memoize the Leva control schema if it's computed from props or state, so useControls doesn't tear down and rebuild the panel.
  • Reuse Color and Vector instances. color.set(hex) and vec.set(x, y, z) mutate in place; assigning a fresh instance allocates and orphans the old one.
import { useMemo } from 'react'
import { useFrame } from '@react-three/fiber'
import { useControls } from 'leva'
import * as THREE from 'three'

function Scene() {
  const uniforms = useMemo(
    () => ({
      uTime: { value: 0 },
      uColor: { value: new THREE.Color('#ff0') },
    }),
    [],
  )

  useControls({
    color: {
      value: '#ff0',
      onChange: (v: string) => uniforms.uColor.value.set(v),
    },
  })

  useFrame((_, delta) => {
    uniforms.uTime.value += delta
  })

  // ...pass `uniforms` into a ShaderMaterial
}

For heavy work triggered by a control change, debounce the onChange or push the work into useFrame via a ref — never block the Leva input thread.

Canonical sources

WebFetch these when a question reaches beyond the curated wisdom above (e.g. specific drei component APIs, post-processing, or render targets).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.