Cubic Lattice · How it works & code
Place n small cubes along x, y, and z directions using three nested loops i, j, k to form a cubic lattice (like salt crystal structure).
Runtime logic
Visiting `/threejs/lattice`, DemoHost lazy-loads `demos/kits/kit-three.js` (registry `mod: 'kit-three'`) and calls `mount(host, { variant: 'lattice' })`. mount resolves the variant to family `lattice`; `withControls` builds the control panel (6 presets + sliders). `createThreeStage` sets up WebGLRenderer, Scene, PerspectiveCamera and OrbitControls, plus the info panel. The family’s `build(S)` initialises state and returns the per-frame callback. Every `requestAnimationFrame`: read controls, advance a speed-scaled clock (dt ≤ 50 ms), run the frame callback; returned readouts go to the info panel. Changing parameters: colour/speed/size apply live; structural ones remount; leaving the page cancels rAF, removes listeners and frees resources.
Environment
Canvas 2D: 2D canvas drawing; WebGL (three.js): WebGL through three.js WebGLRenderer; requestAnimationFrame: Display-synced animation loop; ResizeObserver: Canvas follows container size; Pointer Events: Unified mouse/touch/pen events; devicePixelRatio: Crisp HiDPI rendering (capped at 2×) · react 18.3.1, react-router-dom 6.30.6, vite 5.4.21, three 0.170.0
Dependencies
three 0.170.0, three/addons/controls/OrbitControls.js 0.170.0, react 18.3.1
Imported APIs
withControls — Wraps the demo in the shared control panel (presets, sliders, reset); hot-updates or remounts on change; paramsFromSpec — Turns the spec (colour/speed/size/count/extras) into control-panel parameter definitions; createThreeStage — Shared three.js stage: WebGLRenderer, scene, perspective camera, OrbitControls, auto framing and render loop; addLights — Adds ambient + key + fill lights; THREE — three.js namespace; fmt — Number formatting (fixed decimals); deg — Radians → degrees text; F — Formula block HTML; S.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; THREE.Group — Group container; THREE.BoxGeometry — Box geometry; THREE.Mesh — Mesh (geometry + material); THREE.MeshStandardMaterial — PBR standard material; THREE.Color — Colour object
Source code
frontend/src/demos/kits/kit-three.js: FAM.lattice (L24-L55)
Code walkthrough
L25-25: Control definitions (colour/speed/size/count/extras): Base Hue, Cube Size, Count per side n. L26-26: Preset buttons: name → parameter overrides (6). L27-27: 3D framing radius. L28-36: Info-panel content: live rows(v), formula, notes, params, steps. L37-54: Init function: sets up state and returns the per-frame callback: creates three.js objects `Group`, `BoxGeometry`, `Mesh`, `MeshStandardMaterial`, `Color`; math via `Math.max`, `Math.min`, `Math.round`; returns readouts `th`, `n`, `d`, `metal` to the info panel.
Principles
Location = ((i)-(n-1) / 2) · d, (j − (n−1) / 2) · d, (k − (n−1) / 2) · d) Total = n × n × n θ(t) = θ₀ + ω·t, ω = 0.6 × Self-Rotation Speed Triple cycle i、j、k 在 x、y、z All three directions n A little square. Just one Cube crystal (Like the crystal structure of salt. Less (n−1)/2 It's to keep the center of the crystal down. The color of each square i、j 略微偏移色相,方便你分辨行与列。整体作为一个 Group Swing together.