加载应用…
云澜 Clyune Clyune Component Gallery · 中文

Light Pillar · How it works & code

CylinderGeometry can have different top and bottom radii, resulting in a frustum (a cone with the tip cut off).

Runtime logic

Visiting `/threejs/pillar`, DemoHost lazy-loads `demos/kits/kit-three.js` (registry `mod: 'kit-three'`) and calls `mount(host, { variant: 'pillar' })`. mount resolves the variant to family `pillar`; `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.CylinderGeometry — Cylinder geometry; THREE.MeshStandardMaterial — PBR standard material; THREE.DoubleSide — Render both faces; THREE.Mesh — Mesh (geometry + material); THREE.RingGeometry — Ring geometry; THREE.MeshBasicMaterial — Unlit material

Source code

frontend/src/demos/kits/kit-three.js: FAM.pillar (L287-L315)

Code walkthrough

L288-288: Control definitions (colour/speed/size/count/extras): Pillar color, Thickness, Height h, Emission intensity. L289-289: Preset buttons: name → parameter overrides (6). L290-290: 3D framing radius. L290-290: Camera target. L291-299: Info-panel content: live rows(v), formula, notes, params, steps. L300-314: Init function: sets up state and returns the per-frame callback: creates three.js objects `CylinderGeometry`, `MeshStandardMaterial`, `DoubleSide`, `Mesh`, `RingGeometry`, `MeshBasicMaterial`; math via `Math.sin`; returns readouts `th`, `r1`, `r2`, `h`, `V`, `glow` to the info panel.

Principles

Circle size V = π·h·(r₁² + r₁r₂ + r₂²) / 3 Final Color = Wandering Reflection + Autolight emissive × Strength CylinderGeometry The upper and lower radius can be different, so get one Circle (It's been cut off. The volume formula is in the column πr²h And a cone πr²h/3 Between. Autolight emissive) Let objects have colour even in the shadows, with translucency, like a luminous energy column; the optical circle on the ground moves lightly over time.

Open the interactive demo · GitHub