Disco Lights

Patterned color-cycling lights with tempo and patterns.

What this code does

- Beat-Synchronized Lighting: three colored lights (red, blue, green) pulse to musical tempo.
- Pattern Functions: sine waves, pulse beats, or random flashing modes.
- BPM Control: adjustable tempo from 40-200 beats per minute.
- Phase Offset: each light has different phase timing for dynamic patterns.
- Intensity Modulation: lights fade between dim (0.4) and bright (2.0) intensities.
- Interactive Controls: real-time tempo and pattern switching via GUI.

JavaScript (plain)

import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'

const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(70, width / height, 0.1, 200)
camera.position.set(0, 2, 5)

const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(width, height)

document.querySelector('#app').appendChild(renderer.domElement)

const controls = new OrbitControls(camera, renderer.domElement)
controls.target.set(0, 0.5, 0)
controls.update()

const plane = new THREE.Mesh(
  new THREE.PlaneGeometry(12, 12),
  new THREE.MeshStandardMaterial({ color: 0x101010 })
)
plane.rotation.x = -Math.PI / 2
plane.receiveShadow = true
scene.add(plane)

const sphere = new THREE.Mesh(
  new THREE.SphereGeometry(0.7, 32, 32),
  new THREE.MeshStandardMaterial({
    color: 0xffffff,
    roughness: 0.4,
    metalness: 0.1
  })
)
sphere.position.set(0, 0.7, 0)
scene.add(sphere)

const lights = [
  new THREE.PointLight(0xff0040, 1.2, 10, 2),
  new THREE.PointLight(0x0040ff, 1.2, 10, 2),
  new THREE.PointLight(0x80ff80, 1.2, 10, 2)
]
lights[0].position.set(3, 2, 0)
lights[1].position.set(-3, 2, 0)
lights[2].position.set(0, 2, 3)
lights.forEach(l => scene.add(l))

let time = 0
const tempo = 120

function animate(timeMs) {
  requestAnimationFrame(animate)
  const dt = (timeMs || 16) / 1000
  time += dt
  const bpm = tempo
  const beat = time * (bpm / 60)
  const f = x => Math.sin(x) * 0.5 + 0.5
  lights[0].intensity = 0.4 + 1.6 * f(beat * 2)
  lights[1].intensity = 0.4 + 1.6 * f(beat * 2 + 2)
  lights[2].intensity = 0.4 + 1.6 * f(beat * 2 + 4)
  controls.update()
  renderer.render(scene, camera)
}
animate()