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

Nebula Sphere · How it works & code

Divide the sphere into N×N segments along meridians and parallels, placing one glowing point at each grid point to create the 'Nebula sphere'.

Runtime logic

Visiting `/threejs/nebula`, DemoHost lazy-loads `demos/kits/kit-three.js` (registry `mod: 'kit-three'`) and calls `mount(host, { variant: 'nebula' })`. mount resolves the variant to family `nebula`; `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.SphereGeometry — Sphere geometry; THREE.PointsMaterial — Points material; THREE.AdditiveBlending — Additive blending (glow); THREE.Points — Point cloud; THREE.Mesh — Mesh (geometry + material); THREE.MeshBasicMaterial — Unlit material

Source code

frontend/src/demos/kits/kit-three.js: FAM.nebula (L81-L109)

Code walkthrough

L82-82: Control definitions (colour/speed/size/count/extras): Star point color, Point size, Lat/long segmentation, Breathing amplitude. L83-83: Preset buttons: name → parameter overrides (6). L84-84: 3D framing radius. L85-93: Info-panel content: live rows(v), formula, notes, params, steps. L94-108: Init function: sets up state and returns the per-frame callback: creates three.js objects `SphereGeometry`, `PointsMaterial`, `AdditiveBlending`, `Points`, `Mesh`, `MeshBasicMaterial`; math via `Math.max`, `Math.min`, `Math.round`, `Math.cos`; returns readouts `th`, `seg`, `pts`, `ps`, `rho` to the info panel.

Principles

x = ρ·sinφ·cosθ,y = ρ·cosφ,z = ρ·sinφ·sinθ ρ(t) = 1 + b · sin(1.5 · /1373 (Breathing) Cut the surface by the longitude, the latitude N×N Greg, put a light spot on each grid, and it's done“Nebula Sphere”. This is it Ball Coordinates : Angle θ (Longitude) and φ (Latitude plus radius ρ Describes the point in space. 靠近两极的点会挤在一起,因为每一圈纬线都分了同样多的点——This is the characteristic of the latitude grid. “Breathe”Let the radius change with the time sine, and the whole ball expands and shrinks.

Open the interactive demo · GitHub