react-three-fiber-patterns
Build interactive 3D experiences in React using react-three-fiber (R3F), drei helpers, and shader integration. Covers scene composition, performance optimization, and animation patterns. Triggers on R3F development, React + Three.js, or declarative 3D scene requests.
Best use case
react-three-fiber-patterns is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Build interactive 3D experiences in React using react-three-fiber (R3F), drei helpers, and shader integration. Covers scene composition, performance optimization, and animation patterns. Triggers on R3F development, React + Three.js, or declarative 3D scene requests.
Teams using react-three-fiber-patterns should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/react-three-fiber-patterns/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How react-three-fiber-patterns Compares
| Feature / Agent | react-three-fiber-patterns | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Build interactive 3D experiences in React using react-three-fiber (R3F), drei helpers, and shader integration. Covers scene composition, performance optimization, and animation patterns. Triggers on R3F development, React + Three.js, or declarative 3D scene requests.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# React Three Fiber Patterns
Build 3D scenes declaratively with React using react-three-fiber.
## Setup
```bash
npm install three @react-three/fiber @react-three/drei
npm install -D @types/three
```
## Basic Scene
```tsx
import { Canvas } from '@react-three/fiber'
import { OrbitControls, Environment } from '@react-three/drei'
export function Scene() {
return (
<Canvas camera={{ position: [0, 2, 5], fov: 60 }}>
<ambientLight intensity={0.5} />
<directionalLight position={[10, 10, 5]} intensity={1} />
<mesh position={[0, 1, 0]}>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color="hotpink" />
</mesh>
<mesh rotation={[-Math.PI / 2, 0, 0]}>
<planeGeometry args={[10, 10]} />
<meshStandardMaterial color="#333" />
</mesh>
<OrbitControls />
<Environment preset="sunset" />
</Canvas>
)
}
```
## Component Patterns
### Reusable 3D Components
```tsx
import { useRef } from 'react'
import { useFrame } from '@react-three/fiber'
import * as THREE from 'three'
interface SpinningBoxProps {
position: [number, number, number]
color: string
speed?: number
}
export function SpinningBox({ position, color, speed = 1 }: SpinningBoxProps) {
const meshRef = useRef<THREE.Mesh>(null!)
useFrame((state, delta) => {
meshRef.current.rotation.y += delta * speed
meshRef.current.position.y = Math.sin(state.clock.elapsedTime) * 0.5 + 1
})
return (
<mesh ref={meshRef} position={position}>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color={color} />
</mesh>
)
}
```
### Instanced Meshes (Performance)
```tsx
import { useRef, useMemo } from 'react'
import { useFrame } from '@react-three/fiber'
import * as THREE from 'three'
export function ParticleField({ count = 1000 }) {
const meshRef = useRef<THREE.InstancedMesh>(null!)
const dummy = useMemo(() => new THREE.Object3D(), [])
const particles = useMemo(() =>
Array.from({ length: count }, () => ({
position: [
(Math.random() - 0.5) * 20,
(Math.random() - 0.5) * 20,
(Math.random() - 0.5) * 20,
] as [number, number, number],
speed: 0.5 + Math.random() * 2,
})),
[count]
)
useFrame((state) => {
particles.forEach((p, i) => {
dummy.position.set(...p.position)
dummy.position.y += Math.sin(state.clock.elapsedTime * p.speed) * 0.5
dummy.updateMatrix()
meshRef.current.setMatrixAt(i, dummy.matrix)
})
meshRef.current.instanceMatrix.needsUpdate = true
})
return (
<instancedMesh ref={meshRef} args={[undefined, undefined, count]}>
<sphereGeometry args={[0.05, 8, 8]} />
<meshStandardMaterial color="#88ccff" />
</instancedMesh>
)
}
```
## Animation
### Spring Physics (react-spring)
```tsx
import { useSpring, animated } from '@react-spring/three'
export function AnimatedBox({ active }: { active: boolean }) {
const springs = useSpring({
scale: active ? 1.5 : 1,
color: active ? '#ff6b6b' : '#4ecdc4',
})
return (
<animated.mesh scale={springs.scale}>
<boxGeometry />
<animated.meshStandardMaterial color={springs.color} />
</animated.mesh>
)
}
```
### GSAP Integration
```tsx
import { useRef, useEffect } from 'react'
import gsap from 'gsap'
import * as THREE from 'three'
export function GSAPMesh() {
const meshRef = useRef<THREE.Mesh>(null!)
useEffect(() => {
gsap.to(meshRef.current.rotation, {
y: Math.PI * 2,
duration: 4,
repeat: -1,
ease: 'none',
})
gsap.to(meshRef.current.position, {
y: 2,
duration: 2,
yoyo: true,
repeat: -1,
ease: 'power1.inOut',
})
}, [])
return (
<mesh ref={meshRef}>
<torusKnotGeometry args={[1, 0.3, 128, 32]} />
<meshStandardMaterial color="gold" metalness={0.8} roughness={0.2} />
</mesh>
)
}
```
## Drei Helpers
```tsx
import {
Text, Html, Float, MeshDistortMaterial,
useGLTF, useTexture, Sparkles,
} from '@react-three/drei'
// 3D Text
<Text fontSize={0.5} color="white" anchorX="center">
Hello World
</Text>
// HTML overlay in 3D space
<Html position={[0, 2, 0]} center>
<div className="tooltip">Score: 42</div>
</Html>
// Floating animation
<Float speed={2} rotationIntensity={0.5} floatIntensity={1}>
<mesh><sphereGeometry /><meshStandardMaterial /></mesh>
</Float>
// Distortion material
<mesh>
<sphereGeometry args={[1, 64, 64]} />
<MeshDistortMaterial color="#8b5cf6" speed={2} distort={0.3} />
</mesh>
// Particle sparkles
<Sparkles count={200} scale={5} size={2} speed={0.5} />
```
## Custom Shaders
```tsx
import { shaderMaterial } from '@react-three/drei'
import { extend, useFrame } from '@react-three/fiber'
import * as THREE from 'three'
const WaveMaterial = shaderMaterial(
{ uTime: 0, uColor: new THREE.Color('#4ecdc4') },
// Vertex shader
`varying vec2 vUv;
uniform float uTime;
void main() {
vUv = uv;
vec3 pos = position;
pos.z += sin(pos.x * 4.0 + uTime) * 0.2;
gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
}`,
// Fragment shader
`varying vec2 vUv;
uniform vec3 uColor;
void main() {
gl_FragColor = vec4(uColor * vUv.y, 1.0);
}`
)
extend({ WaveMaterial })
export function WavePlane() {
const materialRef = useRef<any>(null!)
useFrame((state) => {
materialRef.current.uTime = state.clock.elapsedTime
})
return (
<mesh rotation={[-Math.PI / 2, 0, 0]}>
<planeGeometry args={[10, 10, 64, 64]} />
<waveMaterial ref={materialRef} />
</mesh>
)
}
```
## Performance
| Technique | When | Savings |
|-----------|------|---------|
| `useFrame` with delta | Always | Framerate-independent |
| `InstancedMesh` | >100 same geometry | 90%+ draw calls |
| `useMemo` for geometry | Static data | Prevents re-creation |
| `<Suspense>` + `useGLTF` | Asset loading | Non-blocking |
| `<Leva>` debug controls | Development | Easy parameter tuning |
| `gl={{ antialias: false }}` | Mobile | GPU savings |
## Anti-Patterns
- **Creating objects in useFrame** — Pre-create with useMemo or useRef
- **No key prop on dynamic lists** — React needs keys to track 3D elements
- **Direct Three.js mutation in render** — Use refs and useFrame instead
- **Loading models synchronously** — Always use Suspense boundaries
- **No dispose** — Clean up geometries and materials when unmounting
- **Pixel-perfect expectations** — WebGL rendering varies by GPU; test across devicesRelated Skills
webhook-integration-patterns
Designs reliable webhook systems with proper delivery guarantees, retry logic, signature verification, and idempotent processing for event-driven integrations.
vector-search-patterns
Implement vector similarity search with embedding generation, index selection, and hybrid retrieval strategies. Covers ChromaDB, pgvector, FAISS, and RAG pipeline design. Triggers on vector search, embeddings, semantic search, or RAG architecture requests.
three-js-interactive-builder
Scaffold and build interactive 3D visualizations using Three.js with emphasis on algorithmic art, sacred geometry, temporal animations, and modular architecture. Use when creating WebGL visualizations, generative art pieces, interactive 3D experiences, particle systems, flow fields, or projects like gravitational spirals, temporal perspective pieces, or illuminated visual narratives. Triggers on requests for Three.js projects, 3D web graphics, algorithmic visualizations, or sacred geometry renders.
testing-patterns
Write effective tests across the stack—unit, integration, E2E, and visual regression. Covers testing philosophy, framework selection, mocking strategies, and CI integration. Triggers on testing, test coverage, TDD, or quality assurance requests.
session-lifecycle-patterns
Manage AI agent session lifecycles with structured phases (FRAME, SHAPE, BUILD, PROVE), context preservation across sessions, handoff protocols, and session metadata tracking. Triggers on session management, agent lifecycle, or multi-session workflow requests.
responsive-design-patterns
Mobile-first responsive design patterns with breakpoints, fluid layouts, and adaptive components
resilience-patterns
Build fault-tolerant systems with circuit breakers, retries with backoff, bulkheads, timeouts, and graceful degradation. Covers distributed system failure modes and recovery strategies. Triggers on reliability engineering, fault tolerance, or distributed system resilience requests.
redis-patterns
Use Redis effectively for caching, pub/sub messaging, rate limiting, distributed locks, and session storage. Covers data structure selection, expiration strategies, and cluster patterns. Triggers on Redis usage, caching architecture, or pub/sub messaging requests.
realtime-websocket-patterns
Implement real-time features with WebSockets, Server-Sent Events, and long polling. Covers connection management, room-based messaging, presence tracking, and scaling strategies. Triggers on WebSocket implementation, real-time communication, or live update feature requests.
python-packaging-patterns
Structure Python projects for distribution with pyproject.toml, src layouts, dependency management, and publishing workflows. Covers packaging tools (hatch, setuptools, flit, poetry), versioning strategies, and editable installs. Triggers on Python project setup, packaging configuration, or dependency management requests.
prompt-engineering-patterns
Design effective prompts for LLM agents with structured input/output formats, chain-of-thought reasoning, few-shot examples, and system prompt architecture. Covers Claude-specific patterns and multi-turn conversation design. Triggers on prompt design, LLM interaction patterns, or system prompt architecture requests.
postgres-advanced-patterns
Advanced PostgreSQL patterns for performance optimization, complex queries, indexing strategies, and database design