Spotlight Follow Cursor

Spotlight target follows the mouse; tweak angle and penumbra.

What this code does

- SpotLight: directional cone of light with adjustable angle and penumbra.
- Spot Target: SpotLight.target object that defines where the light points.
- SpotLightHelper: wireframe visualization showing the light cone and direction.
- Mouse Tracking: spotlight target follows cursor when "Follow pointer" is enabled.
- Penumbra: soft edge falloff around the spotlight cone (0 = hard, 1 = soft).
- Interactive Controls: real-time adjustment of cone angle and penumbra values.

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, 100)
camera.position.set(0, 2, 5)

const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.shadowMap.enabled = 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.5, 32, 32),
  new THREE.MeshStandardMaterial({ color: 0xdddddd, roughness: 0.6 })
)
sphere.position.set(0, 0.5, 0)
sphere.castShadow = true
scene.add(sphere)

const spot = new THREE.SpotLight(0xffffff, 2, 20, Math.PI / 6, 0.2, 1)
spot.position.set(2, 4, 2)
spot.castShadow = true
scene.add(spot)

scene.add(spot.target)

spot.target.position.set(0, 0.5, 0)

const helper = new THREE.SpotLightHelper(spot)
scene.add(helper)

function animate() {
  requestAnimationFrame(animate)
  helper.update()
  controls.update()
  renderer.render(scene, camera)
}
animate()