--- name: r3f-materials description: React Three Fiber materials - PBR materials, Drei materials, shader materials, material properties. Use when styling meshes, creating custom materials, working with textures, or implementing visual effects. --- # React Three Fiber Materials ## Quick Start ```tsx import { Canvas } from '@react-three/fiber' function Scene() { return ( ) } ``` ## Material Types Overview | Material | Use Case | Lighting | |----------|----------|----------| | meshBasicMaterial | Unlit, flat colors | No | | meshLambertMaterial | Matte surfaces, fast | Yes (diffuse) | | meshPhongMaterial | Shiny, specular | Yes | | meshStandardMaterial | PBR, realistic | Yes (PBR) | | meshPhysicalMaterial | Advanced PBR | Yes (PBR+) | | meshToonMaterial | Cel-shaded | Yes (toon) | | meshNormalMaterial | Debug normals | No | | shaderMaterial | Custom GLSL | Custom | ## meshBasicMaterial No lighting calculations. Always visible, fast. ```tsx ``` ## meshStandardMaterial (PBR) Physically-based rendering. Recommended for realistic results. ```tsx import { useTexture } from '@react-three/drei' import * as THREE from 'three' function PBRMesh() { // Load PBR texture set const [colorMap, normalMap, roughnessMap, metalnessMap, aoMap] = useTexture([ '/textures/color.jpg', '/textures/normal.jpg', '/textures/roughness.jpg', '/textures/metalness.jpg', '/textures/ao.jpg', ]) return ( ) } ``` ## meshPhysicalMaterial (Advanced PBR) Extends Standard with clearcoat, transmission, sheen, etc. ```tsx // Glass material function Glass() { return ( ) } // Car paint function CarPaint() { return ( ) } // Fabric/velvet (sheen) function Fabric() { return ( ) } // Iridescent (soap bubbles) function Iridescent() { return ( ) } ``` ## Drei Special Materials ### MeshReflectorMaterial Realistic mirror-like reflections. ```tsx import { MeshReflectorMaterial } from '@react-three/drei' function ReflectiveFloor() { return ( ) } ``` ### MeshWobbleMaterial Animated wobble effect. ```tsx import { MeshWobbleMaterial } from '@react-three/drei' function WobblyMesh() { return ( ) } ``` ### MeshDistortMaterial Perlin noise distortion. ```tsx import { MeshDistortMaterial } from '@react-three/drei' function DistortedMesh() { return ( ) } ``` ### MeshTransmissionMaterial Better glass/refractive materials. ```tsx import { MeshTransmissionMaterial } from '@react-three/drei' function GlassSphere() { return ( ) } ``` ### MeshDiscardMaterial Discard fragments - useful for shadows only. ```tsx import { MeshDiscardMaterial } from '@react-three/drei' function ShadowOnlyMesh() { return ( {/* Invisible but casts shadows */} ) } ``` ## Points and Lines Materials ```tsx // Points material // Line materials ``` ## Common Material Properties All materials share these base properties: ```tsx ``` ## Dynamic Materials ### Updating Material Properties ```tsx import { useRef } from 'react' import { useFrame } from '@react-three/fiber' function AnimatedMaterial() { const materialRef = useRef() useFrame(({ clock }) => { const t = clock.elapsedTime // Update color materialRef.current.color.setHSL((t * 0.1) % 1, 1, 0.5) // Update properties materialRef.current.roughness = (Math.sin(t) + 1) / 2 }) return ( ) } ``` ### Shared Materials ```tsx import { useMemo } from 'react' import * as THREE from 'three' function SharedMaterial() { // Create once, use many times const material = useMemo(() => new THREE.MeshStandardMaterial({ color: 'red' }), []) return ( <> ) } ``` ## Multiple Materials ```tsx // Different materials per face (BoxGeometry has 6 material groups) {/* +X */} {/* -X */} {/* +Y */} {/* -Y */} {/* +Z */} {/* -Z */} ``` ## Material with Textures ```tsx import { useTexture } from '@react-three/drei' import * as THREE from 'three' function TexturedMaterial() { // Named object pattern (recommended) const textures = useTexture({ map: '/textures/color.jpg', normalMap: '/textures/normal.jpg', roughnessMap: '/textures/roughness.jpg', }) // Set texture properties Object.values(textures).forEach(texture => { texture.wrapS = texture.wrapT = THREE.RepeatWrapping texture.repeat.set(2, 2) }) return ( ) } ``` ## Emissive Materials (Glow) ```tsx // For bloom effect, emissive colors should exceed normal range 1 for bloom toneMapped={false} // Required for colors > 1 /> // With emissive map ``` ## Environment Maps ```tsx import { useEnvironment } from '@react-three/drei' function EnvMappedMaterial() { const envMap = useEnvironment({ preset: 'sunset' }) return ( ) } ``` ## Custom Shader Materials See `r3f-shaders` for detailed shader material usage. ```tsx import { shaderMaterial } from '@react-three/drei' import { extend } from '@react-three/fiber' const CustomMaterial = shaderMaterial( { time: 0, color: new THREE.Color('hotpink') }, // Vertex shader ` varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); } `, // Fragment shader ` uniform float time; uniform vec3 color; varying vec2 vUv; void main() { gl_FragColor = vec4(color * (sin(time + vUv.x * 10.0) * 0.5 + 0.5), 1.0); } ` ) extend({ CustomMaterial }) function CustomShaderMesh() { const materialRef = useRef() useFrame(({ clock }) => { materialRef.current.time = clock.elapsedTime }) return ( ) } ``` ## Performance Tips 1. **Reuse materials**: Same material instance = batched draw calls 2. **Avoid transparent**: Requires sorting, slower 3. **Use alphaTest over transparent**: When possible, faster 4. **Simpler materials**: Basic > Lambert > Phong > Standard > Physical 5. **Limit texture sizes**: 1024-2048 usually sufficient ```tsx // Material caching pattern const materialCache = new Map() function getCachedMaterial(color) { const key = color.toString() if (!materialCache.has(key)) { materialCache.set(key, new THREE.MeshStandardMaterial({ color })) } return materialCache.get(key) } ``` ## See Also - `r3f-textures` - Texture loading and configuration - `r3f-shaders` - Custom shader development - `r3f-lighting` - Light interaction with materials