AI summary by TwinMind
I can’t believe they built this in React
Theo - t3․gg
16m 7s
Introduction & Context
- Paul Henschel (0xca0a) created 3D physics-enabled badge for Vercel Ship event
- First time he documented process in blog post
- Creator of React 3 Fiber, Zustand, entire pmndrs org
- Badge features dropping lanyard animation with interactive physics
Tech Stack Overview
- Blender: prepare/optimize original models for web
- React + React 3 Fiber: declarative renderer for 3js
- Drei: ecosystem of components/helpers for 3js in React
- React 3 Rapier: declarative physics library based on Rapier physics engine
- MeshLine: shader-based thick line implementation for lanyard
- Implementation: ~80 lines mostly declarative code with math
Code Structure & Setup
- Canvas component as 3D entry point
- Physics provider enables rigid body physics
- Extend function adds MeshLine to React 3 Fiber's JSX catalog
- Gravity configurable via props (-10 for floaty, 100 for heavy drop)
- Hot reloading works with physics changes
Badge Component Architecture
- Uses refs instead of state to avoid re-renders on position changes
- Canvas size important for MeshLine calculations
- CatmullRom curve creates smooth interpolation with 4 points
- Fixed rigid body (unmovable) + 3 dynamic joints for rope segments
Physics Implementation
- useRopeJoint connects rigid bodies with length constraints
- Chain structure: fixed point → J1 → J2 → J3 → card
- Each joint has anchor points (0,0,0 center) and length of 1
- Curve updates at 60-120 FPS based on monitor refresh rate
Animation System
- useFrame hook handles frame-based animations without React state updates
- Curve recalculated every frame using joint positions
- 32 interpolated points for smooth curve rendering
- Avoids React renders during animation for performance
Card Component Details
- Spherical joint allows card rotation at rope end
- Rigid body types: Fixed (unaffected), Dynamic (default physics), Kinematic (user-controlled)
- Card switches between kinematic (when dragged) and dynamic (when released)
- Pointer events toggle drag state and physics mode
Drag Interaction System
- Camera unprojection translates 2D pointer to 3D coordinates
- 3js unproject method handles coordinate transformation math
- Kinematic translation moves card with mouse/trackpad
- Lanyard joints follow card movement during drag
- Inertia preserved when releasing drag
Card Rotation Logic
- Card always rotates back to face user (not physically accurate but better UX)
- Uses rotational velocity and current angle calculations
- Incremental rotation towards front: 0.25 × current rotation
- Y-axis rotation managed to maintain forward-facing orientation
Dynamic Name Rendering
- Separate scene renders user name + base texture
- Text3D component for 3D text rendering
- Font embedded via Geist Regular JSON file
- RenderTexture component renders scene into texture for badge
- MeshPhysicalMaterial applies rendered texture as color map
Performance Optimizations
- Refs prevent unnecessary React re-renders
- Frame-based updates bypass React state system
- Blender models replace basic meshes in production
- Extensive lighting optimization for realistic appearance
- Asset preloading from Contentful CMS
