Cloth
threecn/clothFreeComponent
A real spring-mass cloth (Verlet) hanging from its top edge and rippling under procedural wind, with the theme woven --primary to --accent.
Install it
npx shadcn@latest add https://threecn.dev/r/cloth.jsonSource
1"use client"23import * as React from "react"4import { useFrame } from "@react-three/fiber"5import * as THREE from "three"67import {8 SceneContainer,9 type SceneContainerProps,10} from "@/components/threecn/scene-container"11import {12 useShadcnTheme,13 type ThemeMode,14} from "@/components/hooks/use-shadcn-theme"1516const WIDTH = 4.217const HEIGHT = 318const DAMPING = 0.9719const ITERATIONS = 42021type ClothSim = {22 positions: Float32Array23 colors: Float32Array24 indices: Uint32Array25 pinned: Uint8Array26 homeX: Float32Array27 homeY: Float32Array28 cA: Uint32Array29 cB: Uint32Array30 cRest: Float32Array31 cols: number32 rows: number33}3435function buildCloth(segments: number): ClothSim {36 const cols = segments + 137 const rows = segments + 138 const n = cols * rows39 const positions = new Float32Array(n * 3)40 const colors = new Float32Array(n * 3)41 const pinned = new Uint8Array(n)42 const homeX = new Float32Array(n)43 const homeY = new Float32Array(n)4445 for (let v = 0; v < rows; v++) {46 for (let u = 0; u < cols; u++) {47 const i = v * cols + u48 const x = (u / segments - 0.5) * WIDTH49 const y = (0.5 - v / segments) * HEIGHT50 positions[i * 3] = x51 positions[i * 3 + 1] = y52 positions[i * 3 + 2] = 053 homeX[i] = x54 homeY[i] = y55 if (v === 0) pinned[i] = 1 // top row hangs the cloth56 }57 }5859 // Structural constraints: each particle links to its right and lower neighbor.60 const restX = WIDTH / segments61 const restY = HEIGHT / segments62 const cA: number[] = []63 const cB: number[] = []64 const cRest: number[] = []65 for (let v = 0; v < rows; v++) {66 for (let u = 0; u < cols; u++) {67 const i = v * cols + u68 if (u < cols - 1) {69 cA.push(i)70 cB.push(i + 1)71 cRest.push(restX)72 }73 if (v < rows - 1) {74 cA.push(i)75 cB.push(i + cols)76 cRest.push(restY)77 }78 }79 }8081 // Two triangles per cell.82 const indices = new Uint32Array(segments * segments * 6)83 let o = 084 for (let v = 0; v < segments; v++) {85 for (let u = 0; u < segments; u++) {86 const a = v * cols + u87 const b = a + 188 const c = a + cols89 const d = c + 190 indices[o++] = a91 indices[o++] = c92 indices[o++] = b93 indices[o++] = b94 indices[o++] = c95 indices[o++] = d96 }97 }9899 return {100 positions,101 colors,102 indices,103 pinned,104 homeX,105 homeY,106 cA: Uint32Array.from(cA),107 cB: Uint32Array.from(cB),108// … truncated
What it pulls in
npm packages
Files it writes
More from threecn
All 28 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| AuroraRibbonsaurora-ribbons | threecn | Components | Free | 3 deps · 3 files | |
| Boidsboids | threecn | Components | Free | 3 deps · 3 files | |
| Crystalcrystal | threecn | Components | Free | 3 deps · 3 files | |
| CubeWavecube-wave | threecn | Components | Free | 3 deps · 3 files | |
| CurlFlowcurl-flow | threecn | Components | Free | 3 deps · 3 files | |
| DnaHelixdna-helix | threecn | Components | Free | 3 deps · 3 files | |
| FloatingCard3Dfloating-card-3d | threecn | Components | Free | 3 deps · 3 files | |
| Globeglobe | threecn | Components | Free | 3 deps · 3 files |
