BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/threecn/boids

Boids

threecn/boids
FreeComponent

A living flock steering by separation, alignment and cohesion; each agent tints --accent when slow to --primary at full speed.

Install it

npx shadcn@latest add https://threecn.dev/r/boids.json

Source

components/threecn/boids.tsxthreecn.dev/r/boids.json
1"use client"
2
3import * as React from "react"
4import { useFrame } from "@react-three/fiber"
5import * as THREE from "three"
6
7import {
8 SceneContainer,
9 type SceneContainerProps,
10} from "@/components/threecn/scene-container"
11import {
12 useShadcnTheme,
13 type ThemeMode,
14} from "@/components/hooks/use-shadcn-theme"
15
16const BOUND = 4.2
17const PERCEPTION = 1.4
18const SEPARATION = 0.7
19const MAX_SPEED = 2.2
20
21type Swarm = { pos: Float32Array; vel: Float32Array }
22
23function seedSwarm(count: number): Swarm {
24 const pos = new Float32Array(count * 3)
25 const vel = new Float32Array(count * 3)
26 for (let i = 0; i < count; i++) {
27 const r = Math.cbrt(Math.random()) * BOUND * 0.7
28 const theta = Math.random() * Math.PI * 2
29 const phi = Math.acos(2 * Math.random() - 1)
30 pos[i * 3] = r * Math.sin(phi) * Math.cos(theta)
31 pos[i * 3 + 1] = r * Math.sin(phi) * Math.sin(theta)
32 pos[i * 3 + 2] = r * Math.cos(phi)
33 vel[i * 3] = (Math.random() - 0.5) * 2
34 vel[i * 3 + 1] = (Math.random() - 0.5) * 2
35 vel[i * 3 + 2] = (Math.random() - 0.5) * 2
36 }
37 return { pos, vel }
38}
39
40function Flock({
41 count,
42 speed,
43 theme,
44}: {
45 count: number
46 speed: number
47 theme: ThemeMode
48}) {
49 const { primaryColor, accentColor } = useShadcnTheme(theme)
50 const meshRef = React.useRef<THREE.InstancedMesh>(null)
51 const swarmRef = React.useRef<Swarm | null>(null)
52
53 // The swarm is mutated every frame, so it lives in a ref (never read during
54 // render). Seed it whenever the agent count changes.
55 React.useLayoutEffect(() => {
56 swarmRef.current = seedSwarm(count)
57 }, [count])
58
59 const dummy = React.useMemo(() => new THREE.Object3D(), [])
60 const tmpColor = React.useMemo(() => new THREE.Color(), [])
61 const up = React.useMemo(() => new THREE.Vector3(0, 1, 0), [])
62 const dir = React.useMemo(() => new THREE.Vector3(), [])
63
64 useFrame((_, delta) => {
65 const mesh = meshRef.current
66 const swarm = swarmRef.current
67 if (!mesh || !swarm) return
68 const dt = Math.min(delta, 0.05) * speed
69 const { pos, vel } = swarm
70
71 for (let i = 0; i < count; i++) {
72 const io = i * 3
73 const px = pos[io]
74 const py = pos[io + 1]
75 const pz = pos[io + 2]
76
77 let ax = 0
78 let ay = 0
79 let az = 0 // alignment
80 let cx = 0
81 let cy = 0
82 let cz = 0 // cohesion
83 let sx = 0
84 let sy = 0
85 let sz = 0 // separation
86 let n = 0
87
88 for (let j = 0; j < count; j++) {
89 if (j === i) continue
90 const jo = j * 3
91// … truncated

What it pulls in

npm packages

  • three
  • @react-three/fiber
  • @react-three/drei

Files it writes

  • components/threecn/boids.tsx
  • components/threecn/scene-container.tsx
  • components/hooks/use-shadcn-theme.ts

Facts

Registry
threecn
Type
registry:ui
Access
Free
Files written
3
Licence
MIT
First indexed
2026-08-03
Last confirmed
today

Go to the source

threecn.dev ln-dev7/threecn on GitHub Raw registry item This item as JSON

More from threecn

All 28 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
AuroraRibbonsaurora-ribbonsthreecnComponentsFree3 deps · 3 files
ClothcloththreecnComponentsFree3 deps · 3 files
CrystalcrystalthreecnComponentsFree3 deps · 3 files
CubeWavecube-wavethreecnComponentsFree3 deps · 3 files
CurlFlowcurl-flowthreecnComponentsFree3 deps · 3 files
DnaHelixdna-helixthreecnComponentsFree3 deps · 3 files
FloatingCard3Dfloating-card-3dthreecnComponentsFree3 deps · 3 files
GlobeglobethreecnComponentsFree3 deps · 3 files
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex