Glass Sphere · How it works & code
MeshPhysicalMaterial's transmission (transmission) allows light to pass through the object, ior (refractive index)…
Runtime logic
Visiting `/threejs/glass`, DemoHost lazy-loads `demos/kits/kit-three.js` (registry `mod: 'kit-three'`) and calls `mount(host, { variant: 'glass' })`. mount resolves the variant to family `glass`; `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.Mesh — Mesh (geometry + material); THREE.SphereGeometry — Sphere geometry; THREE.MeshPhysicalMaterial — Physical material (clearcoat/transmission); THREE.Group — Group container; THREE.BoxGeometry — Box geometry; THREE.MeshStandardMaterial — PBR standard material; THREE.Color — Colour object
Source code
frontend/src/demos/kits/kit-three.js: FAM.glass (L157-L182)
Code walkthrough
L158-158: Control definitions (colour/speed/size/count/extras): Glass tint, ball radius, Transmittance, Refraction index n. L159-159: Preset buttons: name → parameter overrides (6). L160-160: 3D framing radius. L161-169: Info-panel content: live rows(v), formula, notes, params, steps. L170-181: Init function: sets up state and returns the per-frame callback: creates three.js objects `Mesh`, `SphereGeometry`, `MeshPhysicalMaterial`, `Group`, `BoxGeometry`, `MeshStandardMaterial` …; math via `Math.cos`, `Math.sin`; returns readouts `th`, `tr`, `n`, `r` to the info panel.
Principles
Snel Law: n1 · sinθ₁ = n₂ · sinθ₂ Critical angle θac = arcsin1 avail / n) (Glass to air) MeshPhysicalMaterial 的 transmission (Transmitting) Let the light pass through the object ior (Refraction rate) Decide how much light turns: Air n≈1, Water 1.33, Glass Produce 1.5, Diamond 2.42. 球后面放了一圈彩色小方块,透过玻璃球看它们会被放大、颠倒、扭曲——That's a reflection. The greater the refraction rate, the greater the distortion.