Tesseract
threecn/tesseractFreeComponent
A 4D hypercube in double rotation projected into 3D — the inner cell endlessly swells through the outer, struts shading --accent to --primary by 4D depth.
Install it
npx shadcn@latest add https://threecn.dev/r/tesseract.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"1516/** The 16 corners of a unit hypercube: every sign combination of (±1,±1,±1,±1). */17const VERTS4: [number, number, number, number][] = Array.from(18 { length: 16 },19 (_, i) => [i & 1 ? 1 : -1, i & 2 ? 1 : -1, i & 4 ? 1 : -1, i & 8 ? 1 : -1]20)2122/** The 32 edges connect corners that differ in exactly one coordinate. */23const EDGES: [number, number][] = []24for (let i = 0; i < 16; i++) {25 for (let b = 0; b < 4; b++) {26 const j = i | (1 << b)27 if (j !== i) EDGES.push([i, j])28 }29}3031/** Distance of the virtual 4D "camera" used for the w-perspective projection. */32const K = 333/** Shrinks the projection so the swelling outer cell stays in frame. */34const BASE_SCALE = 0.6235const UP = new THREE.Vector3(0, 1, 0)3637/**38 * Per-frame scratch for each corner's normalized 4D depth. Written in full,39 * then read, inside a single frame callback — safe to share at module level.40 */41const depths = new Float32Array(VERTS4.length)4243function Hypercube({44 size,45 speed,46 thickness,47 theme,48}: {49 size: number50 speed: number51 thickness: number52 theme: ThemeMode53}) {54 const { primaryColor, accentColor } = useShadcnTheme(theme)55 const groupRef = React.useRef<THREE.Group>(null)56 const nodesRef = React.useRef<THREE.InstancedMesh>(null)57 const strutsRef = React.useRef<THREE.InstancedMesh>(null)58 const dummy = React.useMemo(() => new THREE.Object3D(), [])59 const tmpColor = React.useMemo(() => new THREE.Color(), [])60 const tmpDir = React.useMemo(() => new THREE.Vector3(), [])61 const points = React.useMemo(() => VERTS4.map(() => new THREE.Vector3()), [])6263 useFrame(({ clock }) => {64 const nodes = nodesRef.current65 const struts = strutsRef.current66 const group = groupRef.current67 if (!nodes || !struts || !group) return68 const t = clock.getElapsedTime() * speed69 group.rotation.y = t * 0.1670 group.rotation.x = 0.32 + Math.sin(t * 0.2) * 0.127172 // Double rotation in two orthogonal 4D planes (XW and YZ) — the classic73 // tesseract motion where the inner cell turns itself inside out.74 const ca = Math.cos(t * 0.55)75 const sa = Math.sin(t * 0.55)76 const cb = Math.cos(t * 0.38)77 const sb = Math.sin(t * 0.38)7879// … 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 | |
| Clothcloth | 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 |
