Instanced Cubes · How it works & code
Draw hundreds of cubes with a single geometry, storing only position and color per cube — scales well without performance issues.
Runtime logic
Visiting `/threejs/instanced`, DemoHost lazy-loads `demos/kits/kit-three.js` (registry `mod: 'kit-three'`) and calls `mount(host, { variant: 'instanced' })`. mount resolves the variant to family `instanced`; `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.InstancedMesh — Instanced mesh (many copies in one draw call); THREE.BoxGeometry — Box geometry; THREE.MeshStandardMaterial — PBR standard material; THREE.Vector3 — 3D vector; THREE.Matrix4 — 4×4 transform matrix; THREE.Quaternion — Quaternion rotation; THREE.Euler — Euler angles
Source code
frontend/src/demos/kits/kit-three.js: FAM.instanced (L202-L228)
Code walkthrough
L203-203: Control definitions (colour/speed/size/count/extras): Color, Cube Size, Instance count N, Distribution range. L204-204: Preset buttons: name → parameter overrides (6). L205-205: 3D framing radius. L206-214: Info-panel content: live rows(v), formula, notes, params, steps. L215-227: Init function: sets up state and returns the per-frame callback: creates three.js objects `InstancedMesh`, `BoxGeometry`, `MeshStandardMaterial`, `Vector3`, `Matrix4`, `Quaternion` …; math via `Math.max`, `Math.min`, `Math.round`, `Math.random`; returns readouts `th`, `n`, `sp` to the info panel.
Principles
Each example: World Matrix Mi = Horizontal (pi) · s) · Rotation (ωt+i) · Zoom (a) Draw all Ns at one time 如果要画几百个一样的方块,最笨的办法是创建几百个 Mesh,CPU Hundreds of drawing commands to be issued draw call)。 InstancedMesh 只上传一份几何体,再给每个实例一个变换矩阵,GPU It's done once. “Distribution range”把所有实例的位置按比例放大缩小,你会看到它们从中心散开或聚拢。