agentleFS
aykyusufaykyusuf/crew-chief/skills/web-3d-perf/SKILL.md

web-3d-perf

Skill8 starsChanged 11 days agoMITView on GitHub

Frame-budget rules for 3D and heavy canvas work on the web (React Three Fiber, Three.js, WebGL): pixel-ratio cap, pause when offscreen or hidden, instancing, disposal, lazy loading, reduced-motion fallback, and how to measure. Use when building or reviewing a 3D scene, a shader background or a canvas animation, and always under the immersive UI profile.

The file

We split this skill into the rules it gives an agent

11 rules. About 0 are common, 5 are distinctive, and 6 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 6

    Very specific

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

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

    Before writing the scene, write down (in the profile's perfBudget or the PR notes):

    line 16
  2. Very specific:

    Render on demand: when the scene is mostly still: frameloop="demand" and call invalidate() after a change.

    line 26
  3. Very specific:

    Do not allocate in the loop: no new Vector3(), clone(), array or object literals inside useFrame.

    line 29
  4. Distinctive:

    Textures: use compressed GPU formats (KTX2/Basis) for anything large, power-of-two sizes, no larger than it will appear (a 4096 map on a …

    line 36
  5. Distinctive:

    Shaders: keep fragment shaders short.

    line 38
  6. Very specific:

    When a scene, route or object goes away, call dispose() on its geometries, materials, textures and render targets (drei and R3F dispose …

    line 43
  7. Very specific:

    Check the result: renderer.info.memory (geometries, textures) and renderer.info.render (calls, triangles) must return to their earlier …

    line 44
  8. Distinctive:

    Do not mount the canvas until it is near the viewport.

    line 51
  9. Distinctive:

    Keep the page usable without the scene: text, links and controls live in the DOM, not inside the canvas.

    line 57
  10. Very specific:

    Use Stats (stats.js, or drei Stats) for a live frame time, and renderer.info for draw calls, triangles and memory.

    line 62
  11. Distinctive:

    Test with CPU throttling (4x) and a phone, not only the development machine.

    line 63

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “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

Web 3D performance

Written for crew-chief in its own words. The goal is a scene that holds its frame rate on a mid-range laptop and a phone, costs nothing while nobody sees it, and still works for people who ask for less motion. Every rule below is checkable: when you finish, go through the list and name the ones you could not meet.

0. Real 3D only

Under the immersive profile the hero or main scene must be real 3D: Three.js/WebGL, with react-three-fiber when the project is React. A CSS or SVG imitation (layered gradients, rotated divs, a looped video of a scene) is not acceptable. If you cannot build real 3D (a stated constraint, no WebGL available), say so explicitly in your report with the reason, and do not substitute without saying.

1. Set the budget first

Before writing the scene, write down (in the profile's perfBudget or the PR notes):

  • target frame time: 16.6 ms for 60 fps (8.3 ms for 120 Hz screens, but do not promise it);
  • a draw-call ceiling (about 100 on desktop, 50 on phones is a sane start), a triangle ceiling, a texture memory ceiling;
  • the weakest device you care about.

A scene with no number is a scene nobody can call too slow.

2. Render only what is needed

  • Cap the pixel ratio. dpr={[1, 2]} in R3F, or renderer.setPixelRatio(Math.min(devicePixelRatio, 2)). Rendering at 3x on a phone costs 2.25 times the pixels of 2x and nobody sees the difference. Drop to 1.25 or lower if frames are late (R3F's PerformanceMonitor can step it down and up).
  • Render on demand when the scene is mostly still: frameloop="demand" and call invalidate() after a change. A constantly running loop for a static product shot burns a battery for nothing.
  • Pause when it cannot be seen. Stop the loop when the canvas is offscreen (IntersectionObserver) and when the tab is hidden (document.visibilityState, or frameloop="never" while hidden). Resume cleanly, without a time jump: use the clock delta, clamp it (about 0.05 s), and never advance animation by wall-clock time.
  • Do per-frame work in the frame callback, not in React state. Never call setState inside useFrame; write to refs, uniforms, or instance matrices. One state update per frame re-renders the tree and ruins the budget.
  • Do not allocate in the loop: no new Vector3(), clone(), array or object literals inside useFrame. Create the scratch objects once outside and reuse them.

3. Cut the work the GPU does

  • Instance repeats. The same mesh more than a few times is one InstancedMesh (or drei Instances), updated through the instance matrix, not hundreds of meshes. Merge static geometry that never moves on its own (BufferGeometryUtils.mergeGeometries).
  • Share geometry and materials. One geometry and one material object per kind, not one per mesh. Different material objects mean extra draw calls and extra shader programs.
  • Keep shadows, transmission, and post-processing deliberate. Each is a full extra pass. Bake static shadows and lighting into textures where you can; use one or two real lights; limit the shadow map size; add post effects one at a time and measure each.
  • Textures: use compressed GPU formats (KTX2/Basis) for anything large, power-of-two sizes, no larger than it will appear (a 4096 map on a 300 px object is waste), mipmaps on. Prefer a texture atlas over many small ones.
  • Models: compress meshes (Draco or Meshopt), strip unused attributes, decimate what is far away, and use level of detail where the camera range is wide.
  • Shaders: keep fragment shaders short; avoid branching on varying values and loops with a runtime bound; do not recompute in the fragment what the vertex stage can give you.

4. Free what you stop using

Three.js does not free GPU memory when a JavaScript reference disappears.

  • When a scene, route or object goes away, call dispose() on its geometries, materials, textures and render targets (drei and R3F dispose automatically for objects they mount; anything you create by hand with new is yours).
  • Check the result: renderer.info.memory (geometries, textures) and renderer.info.render (calls, triangles) must return to their earlier numbers after leaving and re-entering a screen. A count that only grows is a leak.
  • One WebGL context per page where possible; browsers drop old contexts when many exist.

5. Load it late

  • The 3D bundle is heavy: load it with a dynamic import (next/dynamic with ssr: false, or React.lazy) only where the scene appears, not in the main bundle.
  • Show a lightweight placeholder (a poster image of the same size, so the layout does not jump) until the scene is ready; load models and textures after the first paint, with a progress state.
  • Do not mount the canvas until it is near the viewport.

6. Respect reduced motion and weak hardware

  • prefers-reduced-motion: reduce must change the result: show a still frame (or render once with frameloop="demand"), stop camera drift, parallax and autoplay, keep only motion the user starts.
  • Provide a fallback when WebGL is missing or the context is lost: a static image, not a blank box. Listen for webglcontextlost and webglcontextrestored.
  • Keep the page usable without the scene: text, links and controls live in the DOM, not inside the canvas; give the canvas an accessible name or mark it decorative.

7. Measure, do not guess

  1. Open the browser's Performance panel, record 5 to 10 seconds of the real interaction, and look at the frame bars and the main-thread and GPU rows. Frames over the budget are the work list.
  2. Use Stats (stats.js, or drei Stats) for a live frame time, and renderer.info for draw calls, triangles and memory.
  3. Test with CPU throttling (4x) and a phone, not only the development machine; throttle the GPU by a smaller window and a larger pixel ratio.
  4. Change one thing at a time and re-measure. Keep the number you saw in the report ("120 draw calls to 18 after instancing; 9.8 ms per frame").

Checklist before you report done

  • A written budget exists (frame time, draw calls, memory, weakest device).
  • Pixel ratio is capped; on-demand rendering is used when the scene is mostly still.
  • The loop stops offscreen and in a hidden tab, and resumes without a time jump.
  • No setState and no allocation inside the frame callback.
  • Repeats are instanced; geometry and materials are shared.
  • Created resources are disposed; renderer.info.memory returns to baseline after a route change.
  • The scene loads lazily with a same-size placeholder.
  • prefers-reduced-motion gives a still or minimal result; there is a no-WebGL fallback; the page works without the canvas.
  • A measurement from the performance panel or renderer.info is quoted in the report; if none was taken, the report says not verified.

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.