Loading...
Loading...
Three.js and React Three Fiber sub-skill - 3D scenes, shaders, postprocessing.
npx skill4agent add athevon/genjutsu threejs-r3f3D on the web. Three.js is the engine, R3F is the React renderer. Concise rules here. Deep-dive in.references/
| Need | Tool | Why |
|---|---|---|
| Full 3D scene (models, lights, physics) | R3F + drei | Declarative, React-friendly, ecosystem |
| Vanilla 3D (no React) | Three.js direct | Lighter, no React overhead |
| Simple 3D transforms on UI | CSS | GPU-composited, no WebGL context |
| 2D particles / generative | Canvas 2D | Simpler API, less GPU overhead |
| Shader-only visuals (no scene graph) | Raw WebGL / ShaderMaterial | Maximum control, minimal abstraction |
import { Canvas } from '@react-three/fiber'
import { Environment, OrbitControls } from '@react-three/drei'
import { Suspense } from 'react'
<Canvas camera={{ position: [0, 2, 5], fov: 45 }} dpr={[1, 2]} gl={{ antialias: true }}>
<Suspense fallback={null}>
<Environment preset="studio" />
<OrbitControls makeDefault />
<Scene />
</Suspense>
</Canvas><Suspense>dpr={[1, 2]}| Hook | Purpose | Gotcha |
|---|---|---|
| Per-frame logic (animation, physics) | Never setState inside |
| Access gl, scene, camera, size, viewport, pointer | Destructure only what you need |
| Load any Three.js resource | Wrap parent in Suspense |
| Extract nodes/materials from loaded scene | Useful after useGLTF |
useFrame((state, delta) => {
// Use delta for framerate-independent animation
meshRef.current.rotation.y += delta * 0.5
// Access clock for time-based effects
material.uniforms.uTime.value = state.clock.elapsedTime
})| Component | Use Case |
|---|---|
| HDRI lighting (presets: studio, sunset, city, forest, dawn) |
| Idle floating animation (speed, rotationIntensity, floatIntensity) |
| Extruded 3D text (needs JSON font from Facetype.js) |
| Load .glb/.gltf models (returns { nodes, materials, scene }) |
| Preload model before component mounts |
| Glass/crystal/liquid refraction effects |
| Drag-to-rotate for product showcases |
| Auto-center any group of meshes |
| LOD -- swap geometry by camera distance |
| Load textures with Suspense support |
| Declarative instancing for repeated meshes |
import { EffectComposer, Bloom, ChromaticAberration } from '@react-three/postprocessing'
import { BlendFunction } from 'postprocessing'
<EffectComposer>
<Bloom
luminanceThreshold={1}
luminanceSmoothing={0.4}
intensity={0.6}
/>
<ChromaticAberration
blendFunction={BlendFunction.NORMAL}
offset={[0.002, 0.002]}
/>
</EffectComposer>luminanceThreshold={1}| Pattern | When |
|---|---|
| 100+ identical meshes (particles, trees, crowds) |
| LOD: swap hi/lo models by distance |
| Prevent auto-dispose when reusing shared geometry |
| Compress .glb models (70-90% size reduction) |
| Compressed GPU textures (1/4 VRAM) |
| Only render when something changes (static scenes) |
| Trigger a render in demand mode |
Offscreen canvas ( | Run rendering off main thread |
stats-glr3f-perf// BAD
useFrame(() => {
setRotation(prev => prev + 0.01) // React re-render every frame
})
// GOOD
useFrame((_, delta) => {
meshRef.current.rotation.y += delta * 0.5 // Direct mutation, zero re-renders
})new Vector3()// BAD
useFrame((state) => {
const target = new THREE.Vector3(0, Math.sin(state.clock.elapsedTime), 0)
meshRef.current.position.copy(target)
})
// GOOD
const _target = useMemo(() => new THREE.Vector3(), [])
useFrame((state) => {
_target.set(0, Math.sin(state.clock.elapsedTime), 0)
meshRef.current.position.copy(_target)
})// BAD -- texture stays in VRAM after unmount
const texture = useLoader(TextureLoader, '/big-texture.jpg')
// GOOD -- R3F auto-disposes when using JSX primitives
// For manual resources, dispose in cleanup:
useEffect(() => {
return () => {
texture.dispose()
geometry.dispose()
material.dispose()
}
}, [])// BAD
function App() {
const [uiState, setUiState] = useState(false) // re-renders remount Canvas
return (
<>
<button onClick={() => setUiState(!uiState)}>Toggle</button>
<Canvas><Scene config={uiState} /></Canvas>
</>
)
}
// GOOD -- isolate Canvas in its own component
function App() {
return (
<>
<UI />
<SceneCanvas />
</>
)
}// BAD
<Canvas>
<Model /> {/* useGLTF inside -- will throw */}
</Canvas>
// GOOD
<Canvas>
<Suspense fallback={<Loader />}>
<Model />
</Suspense>
</Canvas>| Need | Load |
|---|---|
| Scene boilerplate, lighting rigs, controls | |
| Custom shaders, GLSL patterns, uniforms | |
| Animation principles, easing, timing | |
| GSAP + Three.js integration | |