Prompt specification
Build or customize the Three.js 3D scene "Doorbell light feedback" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Doorbell light feedback
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/doorbell-light-feedback-x0014
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: doorbell light feedback. Place the button beside an interior chime and indicator lamp; pressing it visibly depresses the button, flashes the light, and moves a small chime hammer. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (doorbell-light-feedback-x0014):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Low-Poly Doorbell Feedback</title>
<style>
:root {
color-scheme: light;
--ink: #26343b;
--muted: #6c777b;
--paper: rgba(250, 248, 242, 0.92);
--line: rgba(38, 52, 59, 0.14);
--accent: #e36a52;
--amber: #f3a33b;
}
* { box-sizing: border-box; }
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9d5ca;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active { cursor: grabbing; }
.ui {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
max-width: 340px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 38px);
line-height: 1.04;
letter-spacing: -0.045em;
}
.subtitle {
margin-top: 9px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.actions {
position: absolute;
top: 22px;
right: 22px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
border: 1px solid var(--line);
color: var(--ink);
background: var(--paper);
box-shadow: 0 8px 25px rgba(44, 53, 56, 0.09);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
transform 160ms ease,
background 160ms ease,
box-shadow 160ms ease;
}
button:hover {
transform: translateY(-2px);
background: rgba(255, 255, 255, 0.96);
box-shadow: 0 12px 30px rgba(44, 53, 56, 0.13);
}
button:active { transform: translateY(0); }
.icon-button {
width: 42px;
height: 42px;
border-radius: 50%;
font-size: 17px;
}
.status-card {
position: absolute;
left: 24px;
bottom: 24px;
display: flex;
align-items: center;
gap: 12px;
min-width: 190px;
padding: 13px 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 10px 32px rgba(44, 53, 56, 0.1);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.status-dot {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 50%;
background: #94a19f;
box-shadow: 0 0 0 5px rgba(148, 161, 159, 0.13);
transition: 180ms ease;
}
.status-dot.active {
background: var(--amber);
box-shadow:
0 0 0 5px rgba(243, 163, 59, 0.17),
0 0 18px rgba(243, 163, 59, 0.8);
}
.status-copy small {
display: block;
margin-bottom: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.status-copy strong {
display: block;
font-size: 13px;
}
.ring-button {
position: absolute;
right: 24px;
bottom: 24px;
min-width: 168px;
padding: 14px 19px;
border: 0;
border-radius: 999px;
color: white;
background: var(--accent);
box-shadow: 0 12px 28px rgba(227, 106, 82, 0.28);
pointer-events: auto;
}
.ring-button:hover {
color: white;
background: #d95e47;
}
.ring-button.is-active {
transform: scale(0.96);
background: #cf503b;
}
.ring-button span {
margin-right: 8px;
font-size: 16px;
}
.hint {
position: absolute;
right: 26px;
bottom: 80px;
color: rgba(38, 52, 59, 0.62);
font-size: 11px;
font-weight: 650;
letter-spacing: 0.02em;
text-align: right;
}
.scene-label {
position: absolute;
top: 0;
left: 0;
display: flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid rgba(38, 52, 59, 0.12);
border-radius: 999px;
color: var(--ink);
background: rgba(250, 248, 242, 0.86);
box-shadow: 0 6px 18px rgba(38, 52, 59, 0.08);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
will-change: transform;
}
.scene-label::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--accent);
content: "";
}
.scene-label.chime::before { background: #b68143; }
.scene-label.lamp::before { background: var(--amber); }
@media (max-width: 700px) {
.brand {
top: 18px;
left: 18px;
max-width: 230px;
}
.subtitle { display: none; }
.actions {
top: 16px;
right: 16px;
}
.status-card {
left: 16px;
bottom: 16px;
min-width: 0;
padding: 11px 13px;
}
.ring-button {
right: 16px;
bottom: 16px;
min-width: 0;
padding: 13px 16px;
}
.hint { display: none; }
.scene-label {
padding: 5px 8px;
font-size: 8px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly doorbell, interior chime, and indicator lamp"></canvas>
<div class="ui">
<div class="brand">
<div class="eyebrow">Interactive object study</div>
<h1>Doorbell<br />feedback.</h1>
<div class="subtitle">
Press the physical button to depress it, flash the indicator, and strike the mechanical chime.
</div>
</div>
<div class="actions">
<button class="icon-button" id="soundButton" aria-label="Toggle sound" title="Toggle sound">♪</button>
<button class="icon-button" id="resetButton" aria-label="Reset camera" title="Reset camera">↺</button>
</div>
<div class="status-card" aria-live="polite">
<span class="status-dot" id="statusDot"></span>
<div class="status-copy">
<small>System status</small>
<strong id="statusText">Ready to ring</strong>
</div>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Space to ring</div>
<button class="ring-button" id="ringButton">
<span>⌁</span> Press doorbell
</button>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const ringButton = document.querySelector("#ringButton");
const soundButton = document.querySelector("#soundButton");
const resetButton = document.querySelector("#resetButton");
const statusDot = document.querySelector("#statusDot");
const statusText = document.querySelector("#statusText");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9d5ca);
scene.fog = new THREE.Fog(0xd9d5ca, 11, 19);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const camera = new THREE.PerspectiveCamera(
window.innerWidth < 700 ? 46 : 39,
window.innerWidth / window.innerHeight,
0.1,
50
);
const cameraTarget = new THREE.Vector3(0, 1.45, 0);
const defaultCamera = {
radius: window.innerWidth < 700 ? 9.2 : 7.7,
theta: 0.08,
phi: 1.36
};
const orbit = { ...defaultCamera };
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
orbit.radius = defaultCamera.radius;
orbit.theta = defaultCamera.theta;
orbit.phi = defaultCamera.phi;
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xfff4dd, 0x60717a, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3dc, 3.2);
sun.position.set(-3.8, 6.5, 6.2);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -6;
sun.shadow.camera.right = 6;
sun.shadow.camera.top = 6;
sun.shadow.camera.bottom = -2;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8ebbd0, 1.05);
fill.position.set(5, 3, 3);
scene.add(fill);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
flatShading: true,
...options
});
}
function roundedRectShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width, y + height,
x + width - radius, y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function roundedPanel(width, height, depth, radius, mat) {
const geometry = new THREE.ExtrudeGeometry(
roundedRectShape(width, height, radius),
{
depth,
bevelEnabled: false,
curveSegments: 5
}
);
geometry.translate(0, 0, -depth / 2);
const mesh = new THREE.Mesh(geometry, mat);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function box(width, height, depth, mat) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function cylinderZ(radius, depth, mat, segments = 16) {
const geometry = new THREE.CylinderGeometry(
radius, radius, depth, segments, 1, false
);
geometry.rotateX(Math.PI / 2);
const mesh = new THREE.Mesh(geometry, mat);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
// Wall and floor.
const wallMat = material(0xd7d1c4, { roughness: 0.96 });
const wall = box(9.4, 5.35, 0.22, wallMat);
wall.position.set(0, 2.25, -0.31);
scene.add(wall);
const floorMat = material(0xb8aa96, { roughness: 0.95 });
const floor = box(9.4, 0.3, 5.8, floorMat);
floor.position.set(0, -0.48, 2.35);
scene.add(floor);
const trimMat = material(0xf1eadc, { roughness: 0.88 });
const baseboard = box(9.35, 0.27, 0.32, trimMat);
baseboard.position.set(0, -0.17, -0.08);
scene.add(baseboard);
// Exterior backing tile.
const tileMat = material(0x71878d, { roughness: 0.9 });
const tile = roundedPanel(1.65, 2.72, 0.07, 0.1, tileMat);
tile.position.set(-2.28, 1.43, -0.16);
scene.add(tile);
const tileLineMat = material(0x9caeae, { roughness: 0.9 });
for (let i = 0; i < 3; i++) {
const seam = box(1.48, 0.018, 0.014, tileLineMat);
seam.position.set(-2.28, 0.54 + i * 0.89, -0.112);
scene.add(seam);
}
// Doorbell assembly.
const doorbell = new THREE.Group();
doorbell.position.set(-2.28, 1.52, 0);
scene.add(doorbell);
const plateMat = material(0x253842, { roughness: 0.48, metalness: 0.3 });
const plate = roundedPanel(0.92, 1.3, 0.15, 0.12, plateMat);
plate.position.z = -0.065;
doorbell.add(plate);
const brassMat = material(0xb77d3e, {
roughness: 0.36,
metalness: 0.72
});
const darkMetalMat = material(0x1d292f, {
roughness: 0.42,
metalness: 0.5
});
const bezel = cylinderZ(0.335, 0.105, brassMat, 16);
bezel.position.z = 0.04;
doorbell.add(bezel);
const innerBezel = cylinderZ(0.265, 0.12, darkMetalMat, 16);
innerBezel.position.z = 0.075;
doorbell.add(innerBezel);
const movingButton = new THREE.Group();
const buttonRestZ = 0.145;
movingButton.position.z = buttonRestZ;
doorbell.add(movingButton);
const buttonMat = material(0xe36a52, {
roughness: 0.4,
metalness: 0.08
});
const buttonFace = cylinderZ(0.225, 0.105, buttonMat, 16);
movingButton.add(buttonFace);
// Low-poly bell symbol embossed on the button.
const iconMat = material(0x26343b, { roughness: 0.65 });
const bellShape = new THREE.Shape();
bellShape.moveTo(-0.135, 0.055);
bellShape.lineTo(0.135, 0.055);
bellShape.lineTo(0.105, -0.015);
bellShape.quadraticCurveTo(0.075, -0.085, 0.035, -0.105);
bellShape.lineTo(0.025, -0.145);
bellShape.lineTo(-0.025, -0.145);
bellShape.lineTo(-0.035, -0.105);
bellShape.quadraticCurveTo(-0.075, -0.085, -0.105, -0.015);
bellShape.closePath();
const bellGeometry = new THREE.ExtrudeGeometry(bellShape, {
depth: 0.025,
bevelEnabled: false,
curveSegments: 3
});
bellGeometry.translate(0, 0, -0.0125);
const bellIcon = new THREE.Mesh(bellGeometry, iconMat);
bellIcon.position.z = 0.066;
bellIcon.castShadow = true;
movingButton.add(bellIcon);
const buttonIndicatorMat = material(0x9bafb0, {
emissive: 0x6e9999,
emissiveIntensity: 0.2,
roughness: 0.4
});
const tinyIndicator = cylinderZ(0.038, 0.025, buttonIndicatorMat, 10);
tinyIndicator.position.set(0, 0.48, 0.025);
doorbell.add(tinyIndicator);
const buttonHitMeshes = [buttonFace, bellIcon];
// Interior chime housing.
const chime = new THREE.Group();
chime.position.set(1.15, 1.52, 0);
scene.add(chime);
const creamMat = material(0xeee3cf, { roughness: 0.88 });
const housing = roundedPanel(1.72, 1.68, 0.16, 0.19, creamMat);
housing.position.z = -0.055;
chime.add(housing);
const woodMat = material(0x79503a, { roughness: 0.9 });
const woodBase = roundedPanel(1.9, 0.24, 0.43, 0.055, woodMat);
woodBase.position.set(0, -0.76, 0.1);
chime.add(woodBase);
const goldMat = material(0xc78b43, {
roughness: 0.32,
metalness: 0.75
});
const gong = cylinderZ(0.445, 0.09, goldMat, 18);
gong.position.set(0, 0.08, 0.14);
chime.add(gong);
const gongInnerMat = material(0xa96734, {
roughness: 0.4,
metalness: 0.68
});
const gongInner = cylinderZ(0.305, 0.105, gongInnerMat, 18);
gongInner.position.set(0, 0.08, 0.19);
chime.add(gongInner);
const gongRing = new THREE.Mesh(
new THREE.TorusGeometry(0.365, 0.035, 6, 24),
brassMat
);
gongRing.position.set(0, 0.08, 0.218);
gongRing.castShadow = true;
chime.add(gongRing);
// Visible mechanical hammer.
const hammerPivot = new THREE.Group();
hammerPivot.position.set(0.67, -0.43, 0.34);
chime.add(hammerPivot);
const arm = new THREE.Mesh(
new THREE.CylinderGeometry(0.026, 0.026, 0.62, 8),
darkMetalMat
);
arm.position.y = 0.31;
arm.castShadow = true;
hammerPivot.add(arm);
const hammerHead = box(0.22, 0.1, 0.12, darkMetalMat);
hammerHead.position.y = 0.64;
hammerPivot.add(hammerHead);
const pivotPin = cylinderZ(0.07, 0.14, brassMat, 12);
pivotPin.position.set(0.67, -0.43, 0.34);
chime.add(pivotPin);
const hammerRestAngle = 0.34;
hammerPivot.rotation.z = hammerRestAngle;
// Decorative screws.
const screwMat = material(0x879397, {
roughness: 0.36,
metalness: 0.7
});
[
[-0.66, 0.66], [0.66, 0.66],
[-0.66, -0.66], [0.66, -0.66]
].forEach(([x, y]) => {
const screw = cylinderZ(0.034, 0.025, screwMat, 10);
screw.position.set(x, y, 0.04);
chime.add(screw);
});
// Indicator lamp.
const lamp = new THREE.Group();
lamp.position.set(2.82, 1.72, 0);
scene.add(lamp);
const lampBase = cylinderZ(0.31, 0.13, darkMetalMat, 14);
lampBase.position.z = 0.01;
lamp.add(lampBase);
const lampRing = new THREE.Mesh(
new THREE.TorusGeometry(0.25, 0.037, 6, 18),
brassMat
);
lampRing.position.z = 0.09;
lampRing.castShadow = true;
lamp.add(lampRing);
const lampGlassMat = material(0x79512e, {
emissive: 0xff9e35,
emissiveIntensity: 0.22,
roughness: 0.32,
metalness: 0.03
});
const lampGlass = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.245, 1),
lampGlassMat
);
lampGlass.position.z = 0.16;
lampGlass.scale.z = 0.62;
lampGlass.castShadow = true;
lamp.add(lampGlass);
const haloMat = new THREE.MeshBasicMaterial({
color: 0xffad42,
transparent: true,
opacity: 0,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const halo = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.42, 1),
haloMat
);
halo.position.z = 0.17;
halo.scale.z = 0.6;
lamp.add(halo);
const lampLight = new THREE.PointLight(0xffa640, 0.08, 3.2, 2);
lampLight.position.set(0, 0, 0.52);
lamp.add(lampLight);
// Small geometric plant for depth and scene balance.
const plant = new THREE.Group();
plant.position.set(-3.75, -0.24, 0.03);
scene.add(plant);
const potMat = material(0xb56d4d, { roughness: 0.92 });
const pot = new THREE.Mesh(
new THREE.CylinderGeometry(0.28, 0.21, 0.42, 8),
potMat
);
pot.castShadow = true;
plant.add(pot);
const leafMat = material(0x547a64, { roughness: 0.95 });
for (let i = 0; i < 6; i++) {
const leaf = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.68, 5),
leafMat
);
const angle = (i / 6) * Math.PI * 2;
leaf.position.set(
Math.cos(angle) * 0.13,
0.48 + (i % 2) * 0.08,
Math.sin(angle) * 0.08
);
leaf.rotation.z = Math.cos(angle) * 0.28;
leaf.rotation.x = Math.sin(angle) * 0.22;
leaf.castShadow = true;
plant.add(leaf);
}
// HTML scene labels projected from 3D anchor points.
const labelData = [
{
text: "Doorbell button",
className: "",
anchor: new THREE.Vector3(-2.28, 2.43, 0.2)
},
{
text: "Interior chime",
className: "chime",
anchor: new THREE.Vector3(1.15, 2.57, 0.15)
},
{
text: "Indicator",
className: "lamp",
anchor: new THREE.Vector3(2.82, 2.2, 0.2)
}
];
labelData.forEach((item) => {
const element = document.createElement("div");
element.className = `scene-label ${item.className}`.trim();
element.textContent = item.text;
document.querySelector(".ui").appendChild(element);
item.element = element;
});
const projected = new THREE.Vector3();
function updateLabels() {
labelData.forEach((item) => {
projected.copy(item.anchor).project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
item.element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
item.element.style.opacity = projected.z < 1 ? "1" : "0";
});
}
// Audio is synthesized locally with Web Audio.
let audioContext = null;
let muted = false;
function ensureAudio() {
if (!audioContext) {
const AudioContextClass =
window.AudioContext || window.webkitAudioContext;
if (AudioContextClass) audioContext = new AudioContextClass();
}
if (audioContext?.state === "suspended") {
audioContext.resume();
}
return audioContext;
}
function scheduleChime(delay) {
if (muted) return;
const context = ensureAudio();
if (!context) return;
const start = context.currentTime + delay;
const master = context.createGain();
master.gain.value = 0.42;
master.connect(context.destination);
[1, 2.76, 5.42].forEach((ratio, index) => {
const oscillator = context.createOscillator();
const gain = context.createGain();
oscillator.type = "sine";
oscillator.frequency.setValueAtTime(690 * ratio, start);
oscillator.frequency.exponentialRampToValueAtTime(
690 * ratio * 0.985,
start + 0.85
);
gain.gain.setValueAtTime(0.0001, start);
gain.gain.exponentialRampToValueAtTime(
0.13 / (index + 1),
start + 0.008
);
gain.gain.exponentialRampToValueAtTime(
0.0001,
start + 1.05 - index * 0.12
);
oscillator.connect(gain);
gain.connect(master);
oscillator.start(start);
oscillator.stop(start + 1.1);
});
}
function scheduleMechanismThunk(delay) {
if (muted) return;
const context = ensureAudio();
if (!context) return;
const start = context.currentTime + delay;
const oscillator = context.createOscillator();
const gain = context.createGain();
oscillator.type = "triangle";
oscillator.frequency.setValueAtTime(155, start);
oscillator.frequency.exponentialRampToValueAtTime(72, start + 0.12);
gain.gain.setValueAtTime(0.07, start);
gain.gain.exponentialRampToValueAtTime(0.0001, start + 0.15);
oscillator.connect(gain);
gain.connect(context.destination);
oscillator.start(start);
oscillator.stop(start + 0.16);
}
const clock = new THREE.Clock();
const strikeTimes = [0.12, 0.56, 1.0];
let ringStart = -100;
let pressStart = -100;
function ringDoorbell() {
const now = clock.getElapsedTime();
ringStart = now;
pressStart = now;
ringButton.classList.add("is-active");
statusText.textContent = "Button pressed";
statusDot.classList.add("active");
ensureAudio();
scheduleMechanismThunk(0.015);
strikeTimes.forEach((time) => scheduleChime(time));
}
ringButton.addEventListener("click", ringDoorbell);
soundButton.addEventListener("click", () => {
muted = !muted;
soundButton.textContent = muted ? "×" : "♪";
soundButton.setAttribute(
"aria-label",
muted ? "Enable sound" : "Mute sound"
);
soundButton.title = muted ? "Enable sound" : "Mute sound";
});
resetButton.addEventListener("click", resetCamera);
// Custom camera interaction; no external controls module is used.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let dragging = false;
let pointerStartX = 0;
let pointerStartY = 0;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
dragging = false;
pointerStartX = event.clientX;
pointerStartY = event.clientY;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const totalDistance = Math.hypot(
event.clientX - pointerStartX,
event.clientY - pointerStartY
);
if (totalDistance > 5) dragging = true;
if (dragging) {
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.005,
0.93,
1.75
);
updateCamera();
}
previousX = event.clientX;
previousY = event.clientY;
});
canvas.addEventListener("pointerup", (event) => {
pointerDown = false;
if (!dragging) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(buttonHitMeshes, false);
if (hits.length) ringDoorbell();
}
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
5.4,
11.5
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target === document.body) {
event.preventDefault();
if (!event.repeat) ringDoorbell();
}
if (event.key.toLowerCase() === "r") resetCamera();
});
const dimLampColor = new THREE.Color(0x79512e);
const hotLampColor = new THREE.Color(0xffd36b);
const strikeTimesLocal = strikeTimes;
function pulse(elapsed, center, width) {
const distance = Math.abs(elapsed - center);
if (distance >= width) return 0;
const normalized = 1 - distance / width;
return normalized * normalized;
}
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const ringElapsed = elapsed - ringStart;
const pressElapsed = elapsed - pressStart;
// Button depression and return.
let depression = 0;
if (pressElapsed >= 0 && pressElapsed < 0.26) {
depression = Math.sin((pressElapsed / 0.26) * Math.PI);
}
movingButton.position.z = buttonRestZ - depression * 0.068;
if (pressElapsed >= 0 && pressElapsed < 0.28) {
ringButton.classList.add("is-active");
} else {
ringButton.classList.remove("is-active");
}
// Three bright flashes, synchronized with the strikes.
let flash = 0;
if (ringElapsed >= 0 && ringElapsed < 1.25) {
flash = Math.max(
pulse(ringElapsed, 0.12, 0.19),
pulse(ringElapsed, 0.56, 0.19),
pulse(ringElapsed, 1.0, 0.19)
);
}
lampGlassMat.color.copy(dimLampColor).lerp(hotLampColor, flash);
lampGlassMat.emissiveIntensity = 0.22 + flash * 4.5;
lampLight.intensity = 0.08 + flash * 7.5;
haloMat.opacity = flash * 0.18;
halo.scale.setScalar(1 + flash * 0.26);
buttonIndicatorMat.emissiveIntensity = 0.2 + flash * 1.8;
// Hammer motion: three complete, damped-looking swings.
let hammerAngle = hammerRestAngle;
if (ringElapsed >= 0 && ringElapsed < 1.5) {
for (const strikeTime of strikeTimesLocal) {
const age = ringElapsed - strikeTime;
if (age >= 0 && age <= 0.32) {
const swing = Math.sin((age / 0.32) * Math.PI);
hammerAngle = hammerRestAngle + swing * 0.66;
}
}
}
hammerPivot.rotation.z = hammerAngle;
// Status copy.
if (ringElapsed >= 0) {
if (ringElapsed < 0.28) {
statusText.textContent = "Button pressed";
} else if (ringElapsed < 1.25) {
statusText.textContent = "Chime ringing";
} else if (ringElapsed < 1.55) {
statusText.textContent = "Chime complete";
} else {
statusText.textContent = "Ready to ring";
statusDot.classList.remove("active");
}
}
updateLabels();
renderer.render(scene, camera);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.fov = width < 700 ? 46 : 39;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
}
window.addEventListener("resize", resize);
animate();
</script>
</body>
</html>
```