Prompt specification
Build or customize the Three.js 3D scene "Desk lamp hand-shadow study" 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: Desk lamp hand-shadow study
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/desk-lamp-hand-shadow-study-x0193
Scene Description Prompt:
Create a standalone stylized scene in one HTML file using Three.js from cdnjs. Model a lamp, notebook, and simplified writing hand; move the lamp between two sides to reveal how the hand's shadow falls across the page.
Working Three.js Starter (desk-lamp-hand-shadow-study-x0193):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Moving Light — Desk Scene</title>
<style>
:root {
color-scheme: dark;
--ink: #f5f1e8;
--muted: rgba(245, 241, 232, 0.66);
--glass: rgba(19, 33, 32, 0.76);
--line: rgba(255, 255, 255, 0.14);
--accent: #f0bd6a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #9b9b8e;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.intro {
position: absolute;
top: 30px;
left: 34px;
max-width: 430px;
color: var(--ink);
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.35);
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 13px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 1px;
content: "";
background: currentColor;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(34px, 5vw, 68px);
font-weight: 400;
line-height: 0.95;
letter-spacing: -0.04em;
}
.intro p {
max-width: 360px;
margin: 18px 0 0;
color: rgba(255, 255, 255, 0.78);
font-size: 14px;
line-height: 1.6;
}
.help {
position: absolute;
top: 34px;
right: 34px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(20, 31, 30, 0.48);
color: rgba(255, 255, 255, 0.72);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.panel {
position: absolute;
right: 34px;
bottom: 30px;
left: 34px;
display: grid;
grid-template-columns: minmax(220px, 0.75fr) minmax(340px, 1.4fr) auto;
align-items: center;
gap: 28px;
padding: 18px 20px;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--glass);
color: var(--ink);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.readout {
min-width: 0;
}
.readout small {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#lampLabel {
display: block;
font-family: Georgia, "Times New Roman", serif;
font-size: 24px;
font-weight: 400;
}
#shadowLabel {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 12px;
}
.slider-wrap {
display: flex;
flex-direction: column;
gap: 9px;
}
.slider-labels {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 5px 0;
appearance: none;
border-radius: 999px;
background:
linear-gradient(
90deg,
rgba(240, 189, 106, 0.2),
rgba(240, 189, 106, 0.72),
rgba(240, 189, 106, 0.2)
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 19px;
height: 19px;
appearance: none;
border: 3px solid #273a38;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.35);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #273a38;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.35);
}
.buttons {
display: flex;
gap: 9px;
}
button {
min-height: 42px;
padding: 0 15px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.07);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
white-space: nowrap;
cursor: pointer;
transition:
background 160ms ease,
transform 160ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.13);
transform: translateY(-1px);
}
#autoBtn {
display: flex;
align-items: center;
gap: 9px;
border-color: rgba(240, 189, 106, 0.3);
background: rgba(240, 189, 106, 0.12);
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 12px rgba(240, 189, 106, 0.8);
}
@media (max-width: 800px) {
.intro {
top: 22px;
left: 22px;
}
.intro p,
.help {
display: none;
}
.panel {
right: 14px;
bottom: 14px;
left: 14px;
grid-template-columns: 1fr auto;
gap: 14px;
padding: 15px;
}
.slider-wrap {
grid-column: 1 / -1;
grid-row: 2;
}
.readout {
grid-column: 1;
}
.buttons {
grid-column: 2;
}
#resetBtn {
display: none;
}
}
</style>
</head>
<body>
<div class="overlay">
<header class="intro">
<div class="eyebrow">Light study · 01</div>
<h1>Moving light,<br />moving shadows.</h1>
<p>
A lamp travels from side to side while a hand writes. Watch the same
gesture produce a completely different shadow on the page.
</p>
</header>
<div class="help">Drag to orbit · Scroll to zoom</div>
<section class="panel" aria-label="Scene controls">
<div class="readout">
<small>Lamp position</small>
<span id="lampLabel">Center</span>
<span id="shadowLabel">The shadow divides across the page</span>
</div>
<div class="slider-wrap">
<div class="slider-labels">
<span>Left side</span>
<span>Light position</span>
<span>Right side</span>
</div>
<input
id="lampPosition"
type="range"
min="-4.5"
max="4.5"
step="0.01"
value="0"
aria-label="Move lamp left or right"
/>
</div>
<div class="buttons">
<button id="autoBtn" type="button">
<span class="dot"></span>
<span id="autoText">Pause sweep</span>
</button>
<button id="resetBtn" type="button">Reset view</button>
</div>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xaaa99d);
scene.fog = new THREE.Fog(0xaaa99d, 12, 25);
const camera = new THREE.PerspectiveCamera(
39,
innerWidth / innerHeight,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance",
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const material = {
desk: new THREE.MeshStandardMaterial({
color: 0x304541,
roughness: 0.88,
metalness: 0.02,
}),
mat: new THREE.MeshStandardMaterial({
color: 0x4b625b,
roughness: 0.96,
}),
cover: new THREE.MeshStandardMaterial({
color: 0xc45e4d,
roughness: 0.76,
}),
coverEdge: new THREE.MeshStandardMaterial({
color: 0x873e36,
roughness: 0.8,
}),
paper: new THREE.MeshStandardMaterial({
color: 0xf3e8d0,
roughness: 0.92,
}),
rule: new THREE.MeshStandardMaterial({
color: 0x78939a,
roughness: 1,
transparent: true,
opacity: 0.32,
depthWrite: false,
}),
ink: new THREE.MeshBasicMaterial({
color: 0x284b52,
}),
lamp: new THREE.MeshStandardMaterial({
color: 0x235d59,
roughness: 0.38,
metalness: 0.48,
}),
lampDark: new THREE.MeshStandardMaterial({
color: 0x173f3d,
roughness: 0.45,
metalness: 0.45,
}),
brass: new THREE.MeshStandardMaterial({
color: 0xdcae61,
roughness: 0.3,
metalness: 0.72,
}),
shadeInside: new THREE.MeshStandardMaterial({
color: 0xf1cf91,
roughness: 0.52,
metalness: 0.16,
side: THREE.DoubleSide,
}),
bulb: new THREE.MeshStandardMaterial({
color: 0xffe1a2,
emissive: 0xffb84f,
emissiveIntensity: 4,
roughness: 0.3,
}),
skin: new THREE.MeshStandardMaterial({
color: 0xcc8667,
roughness: 0.83,
}),
skinLight: new THREE.MeshStandardMaterial({
color: 0xd99776,
roughness: 0.82,
}),
pencil: new THREE.MeshStandardMaterial({
color: 0xe5a83f,
roughness: 0.68,
}),
graphite: new THREE.MeshStandardMaterial({
color: 0x333638,
roughness: 0.74,
}),
eraser: new THREE.MeshStandardMaterial({
color: 0xe78678,
roughness: 0.9,
}),
metal: new THREE.MeshStandardMaterial({
color: 0xb8c0ba,
roughness: 0.3,
metalness: 0.75,
}),
};
// ------------------------------------------------------------
// Geometry helpers
// ------------------------------------------------------------
const Y_AXIS = new THREE.Vector3(0, 1, 0);
function mesh(geometry, mat, position, parent = scene) {
const object = new THREE.Mesh(geometry, mat);
if (position) object.position.copy(position);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
function makeRod(radius, mat, parent = scene) {
const geometry = new THREE.CylinderGeometry(radius, radius, 1, 16);
const rod = mesh(geometry, mat, null, parent);
return rod;
}
function setRod(rod, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
rod.position.copy(a).add(b).multiplyScalar(0.5);
rod.quaternion.setFromUnitVectors(Y_AXIS, direction.normalize());
rod.scale.set(1, length, 1);
}
function makeCapsule(radius, mat, parent = scene) {
const geometry = new THREE.CapsuleGeometry(
radius,
Math.max(0.02, 1),
5,
10
);
const object = mesh(geometry, mat, null, parent);
object.userData.radius = radius;
return object;
}
function setCapsule(capsule, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const radius = capsule.userData.radius;
capsule.position.copy(a).add(b).multiplyScalar(0.5);
capsule.quaternion.setFromUnitVectors(Y_AXIS, direction.normalize());
capsule.scale.set(1, Math.max(0.02, length - radius * 2), 1);
}
function addBox(size, position, mat, parent = scene) {
return mesh(new THREE.BoxGeometry(...size), mat, position, parent);
}
// ------------------------------------------------------------
// Desk, blotter, and notebook
// ------------------------------------------------------------
addBox(
[16, 0.4, 11],
new THREE.Vector3(0, -0.2, 0),
material.desk
);
addBox(
[7.25, 0.035, 4.85],
new THREE.Vector3(0, 0.0175, 0.08),
material.mat
);
const notebook = new THREE.Group();
notebook.position.set(-0.15, 0, -0.08);
scene.add(notebook);
addBox(
[4.9, 0.12, 3.3],
new THREE.Vector3(0, 0.09, 0),
material.coverEdge,
notebook
);
addBox(
[4.78, 0.1, 3.18],
new THREE.Vector3(0, 0.125, 0),
material.cover,
notebook
);
addBox(
[4.55, 0.18, 2.95],
new THREE.Vector3(0.03, 0.23, 0),
material.paper,
notebook
);
// Ruled page.
for (let z = -1.13; z <= 1.13; z += 0.28) {
addBox(
[4.0, 0.008, 0.012],
new THREE.Vector3(0.1, 0.324, z),
material.rule,
notebook
);
}
// Notebook binding rings.
for (let z = -1.12; z <= 1.12; z += 0.56) {
const ring = mesh(
new THREE.TorusGeometry(0.17, 0.025, 8, 28),
material.metal,
new THREE.Vector3(-2.27, 0.25, z),
notebook
);
ring.rotation.y = Math.PI / 2;
}
// A small bookmark peeking out from the bottom.
const bookmark = addBox(
[0.34, 0.018, 0.66],
new THREE.Vector3(1.3, 0.11, 1.73),
material.coverEdge,
notebook
);
bookmark.rotation.x = -0.04;
// ------------------------------------------------------------
// Writing hand
// ------------------------------------------------------------
const handRoot = new THREE.Group();
handRoot.position.set(-0.15, 0.795, 0.55);
scene.add(handRoot);
// Palm.
const palm = mesh(
new THREE.SphereGeometry(1, 28, 18),
material.skin,
new THREE.Vector3(0.12, 0.015, 0),
handRoot
);
palm.scale.set(0.46, 0.22, 0.31);
// Wrist.
const wrist = makeCapsule(0.2, material.skin, handRoot);
setCapsule(
wrist,
new THREE.Vector3(0.35, 0.015, 0.08),
new THREE.Vector3(0.82, 0.05, 0.18)
);
// Forearm extending out of frame.
const forearm = mesh(
new THREE.CylinderGeometry(0.29, 0.2, 1, 20),
material.skin,
null,
handRoot
);
setRod(
forearm,
new THREE.Vector3(0.76, 0.04, 0.17),
new THREE.Vector3(2.85, 0.3, 0.92)
);
const forearmEnd = mesh(
new THREE.SphereGeometry(0.21, 18, 12),
material.skinLight,
new THREE.Vector3(0.78, 0.04, 0.17),
handRoot
);
forearmEnd.scale.set(1, 0.85, 1);
// Curled fingers, constructed from short connected capsules.
const fingerChains = [
[
[-0.05, 0.14, -0.05],
[-0.28, 0.25, 0.03],
[-0.12, 0.34, 0.13],
],
[
[0.08, 0.13, 0.0],
[-0.15, 0.25, 0.09],
[0.01, 0.32, 0.2],
],
[
[0.2, 0.11, 0.04],
[0.02, 0.22, 0.13],
[0.18, 0.27, 0.23],
],
[
[0.3, 0.08, 0.1],
[0.18, 0.18, 0.18],
[0.28, 0.2, 0.27],
],
];
fingerChains.forEach((points, index) => {
const radius = 0.092 - index * 0.006;
for (let i = 0; i < points.length - 1; i++) {
const segment = makeCapsule(radius, material.skinLight, handRoot);
setCapsule(
segment,
new THREE.Vector3(...points[i]),
new THREE.Vector3(...points[i + 1])
);
}
});
// Thumb.
const thumb = makeCapsule(0.115, material.skin, handRoot);
setCapsule(
thumb,
new THREE.Vector3(0.05, -0.03, -0.22),
new THREE.Vector3(-0.25, 0.03, -0.19)
);
const thumbTip = makeCapsule(0.1, material.skinLight, handRoot);
setCapsule(
thumbTip,
new THREE.Vector3(-0.25, 0.03, -0.19),
new THREE.Vector3(-0.34, 0.14, -0.06)
);
// Pencil.
const pencilTip = new THREE.Vector3(-0.75, -0.46, -0.28);
const pencilBack = new THREE.Vector3(-0.08, 0.36, 0.15);
const pencilDirection = new THREE.Vector3()
.subVectors(pencilBack, pencilTip)
.normalize();
const graphite = mesh(
new THREE.ConeGeometry(0.047, 0.16, 8),
material.graphite,
null,
handRoot
);
graphite.position.copy(pencilTip).addScaledVector(pencilDirection, -0.075);
graphite.quaternion.setFromUnitVectors(
Y_AXIS,
pencilDirection.clone().negate()
);
const pencilShaft = makeRod(0.048, material.pencil, handRoot);
setRod(
pencilShaft,
pencilTip.clone().addScaledVector(pencilDirection, 0.04),
pencilBack
);
const eraser = mesh(
new THREE.CylinderGeometry(0.052, 0.052, 0.13, 12),
material.eraser,
pencilBack,
handRoot
);
eraser.quaternion.setFromUnitVectors(Y_AXIS, pencilDirection);
// Growing handwritten line.
const maxInkPoints = 120;
const inkPositions = new Float32Array(maxInkPoints * 3);
const inkGeometry = new THREE.BufferGeometry();
inkGeometry.setAttribute(
"position",
new THREE.BufferAttribute(inkPositions, 3).setUsage(
THREE.DynamicDrawUsage
)
);
inkGeometry.setDrawRange(0, 0);
const inkLine = new THREE.Line(inkGeometry, material.ink);
scene.add(inkLine);
// ------------------------------------------------------------
// Articulated lamp
// ------------------------------------------------------------
const lampRig = new THREE.Group();
scene.add(lampRig);
const base = mesh(
new THREE.CylinderGeometry(0.69, 0.82, 0.16, 48),
material.lampDark,
new THREE.Vector3(0, 0.11, -1.65),
lampRig
);
const baseRing = mesh(
new THREE.TorusGeometry(0.64, 0.035, 10, 48),
material.brass,
new THREE.Vector3(0, 0.195, -1.65),
lampRig
);
baseRing.rotation.x = Math.PI / 2;
const lowerArm = makeRod(0.105, material.lamp, lampRig);
setRod(
lowerArm,
new THREE.Vector3(0, 0.19, -1.65),
new THREE.Vector3(0, 2.35, -1.55)
);
const lowerJoint = mesh(
new THREE.SphereGeometry(0.17, 20, 14),
material.brass,
new THREE.Vector3(0, 2.35, -1.55),
lampRig
);
const upperArm = makeRod(0.09, material.lamp, lampRig);
const upperJoint = mesh(
new THREE.SphereGeometry(0.14, 20, 14),
material.brass,
null,
lampRig
);
const headPosition = new THREE.Vector3(0, 3.25, -0.42);
const shade = new THREE.Group();
lampRig.add(shade);
const shadeShell = mesh(
new THREE.CylinderGeometry(0.26, 0.63, 0.72, 36, 1, true),
material.shadeInside,
null,
shade
);
shadeShell.castShadow = false;
const shadeRim = mesh(
new THREE.TorusGeometry(0.63, 0.035, 10, 48),
material.lampDark,
new THREE.Vector3(0, -0.36, 0),
shade
);
shadeRim.rotation.x = Math.PI / 2;
shadeRim.castShadow = false;
const shadeCap = mesh(
new THREE.CylinderGeometry(0.19, 0.26, 0.14, 28),
material.lamp,
new THREE.Vector3(0, 0.39, 0),
shade
);
shadeCap.castShadow = false;
const bulb = mesh(
new THREE.SphereGeometry(0.145, 22, 16),
material.bulb,
headPosition,
lampRig
);
bulb.castShadow = false;
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xdde6df, 0x27342f, 0.55));
const ambient = new THREE.AmbientLight(0xfff2dc, 0.18);
scene.add(ambient);
const fill = new THREE.DirectionalLight(0xb9d2d1, 0.45);
fill.position.set(-5, 8, 6);
scene.add(fill);
const spotTarget = new THREE.Object3D();
spotTarget.position.set(0, 0.32, 0);
scene.add(spotTarget);
const spot = new THREE.SpotLight(
0xffd18a,
320,
18,
0.72,
0.5,
1.45
);
spot.position.copy(headPosition);
spot.target = spotTarget;
spot.castShadow = true;
spot.shadow.mapSize.set(2048, 2048);
spot.shadow.camera.near = 0.15;
spot.shadow.camera.far = 16;
spot.shadow.bias = -0.00025;
spot.shadow.normalBias = 0.018;
spot.shadow.radius = 3;
scene.add(spot);
const bulbGlow = new THREE.PointLight(0xffc76b, 2.5, 2.4, 2);
bulbGlow.position.copy(headPosition);
lampRig.add(bulbGlow);
// ------------------------------------------------------------
// Custom camera controls
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.62, 0);
let cameraTheta = 0.58;
let cameraPhi = 0.83;
let cameraRadius = 10.8;
let cameraHasBeenMoved = false;
function updateCamera() {
const horizontal = Math.sin(cameraPhi) * cameraRadius;
camera.position.set(
cameraTarget.x + Math.sin(cameraTheta) * horizontal,
cameraTarget.y + Math.cos(cameraPhi) * cameraRadius,
cameraTarget.z + Math.cos(cameraTheta) * horizontal
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraTheta = 0.58;
cameraPhi = 0.83;
cameraRadius = innerWidth / innerHeight < 1 ? 15 : 10.8;
cameraHasBeenMoved = false;
updateCamera();
}
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraTheta -= deltaX * 0.005;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.0045,
0.38,
1.34
);
cameraHasBeenMoved = true;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
6.2,
19
);
cameraHasBeenMoved = true;
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ------------------------------------------------------------
// UI
// ------------------------------------------------------------
const lampPositionInput = document.querySelector("#lampPosition");
const autoButton = document.querySelector("#autoBtn");
const autoText = document.querySelector("#autoText");
const lampLabel = document.querySelector("#lampLabel");
const shadowLabel = document.querySelector("#shadowLabel");
const resetButton = document.querySelector("#resetBtn");
let autoSweep = true;
let autoTime = 0;
let lampX = 0;
function updateSweepButton() {
autoText.textContent = autoSweep ? "Pause sweep" : "Start sweep";
}
autoButton.addEventListener("click", () => {
autoSweep = !autoSweep;
updateSweepButton();
});
lampPositionInput.addEventListener("input", () => {
autoSweep = false;
lampX = Number(lampPositionInput.value);
updateSweepButton();
});
resetButton.addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (
event.code === "Space" &&
event.target.tagName !== "INPUT" &&
event.target.tagName !== "BUTTON"
) {
event.preventDefault();
autoSweep = !autoSweep;
updateSweepButton();
}
});
function updateReadout() {
if (lampX < -0.65) {
lampLabel.textContent = "Left";
shadowLabel.textContent = "The shadow falls to the right";
} else if (lampX > 0.65) {
lampLabel.textContent = "Right";
shadowLabel.textContent = "The shadow falls to the left";
} else {
lampLabel.textContent = "Center";
shadowLabel.textContent = "The shadow divides across the page";
}
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
const worldBulb = new THREE.Vector3();
const lightDirection = new THREE.Vector3();
const armEnd = new THREE.Vector3();
const elbow = new THREE.Vector3(0, 2.35, -1.55);
function updateInk(currentX) {
const startX = -1.18;
const count = 120;
const visiblePoints = Math.max(
2,
Math.min(
count,
Math.ceil(((currentX - startX) / 2.05) * (count - 1))
)
);
for (let i = 0; i < visiblePoints; i++) {
const t = i / (visiblePoints - 1);
const x = THREE.MathUtils.lerp(startX, currentX, t);
const z =
0.27 +
Math.sin(t * Math.PI * 5.2) * 0.012 +
Math.sin(t * Math.PI * 11.0) * 0.004;
inkPositions[i * 3] = x;
inkPositions[i * 3 + 1] = 0.332;
inkPositions[i * 3 + 2] = z;
}
inkGeometry.attributes.position.needsUpdate = true;
inkGeometry.setDrawRange(0, visiblePoints);
inkGeometry.computeBoundingSphere();
}
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (autoSweep) {
autoTime += delta;
lampX = 4.5 * Math.sin(autoTime * 0.62);
lampPositionInput.value = lampX.toFixed(2);
}
lampRig.position.x = lampX;
lampRig.updateMatrixWorld(true);
// Aim the lamp and spotlight toward the notebook.
bulb.getWorldPosition(worldBulb);
lightDirection
.subVectors(spotTarget.position, worldBulb)
.normalize();
// Move the shade so its open mouth surrounds the bulb.
shade.position
.copy(headPosition)
.addScaledVector(lightDirection, -0.36);
shade.quaternion.setFromUnitVectors(
new THREE.Vector3(0, -1, 0),
lightDirection
);
// Attach the upper arm to the rear of the moving shade.
armEnd
.copy(headPosition)
.addScaledVector(lightDirection, -0.56);
setRod(upperArm, elbow, armEnd);
upperJoint.position.copy(armEnd);
spot.position.copy(worldBulb).addScaledVector(lightDirection, 0.035);
// Writing cycle.
const writingTime = clock.elapsedTime % 8;
const writingProgress = writingTime / 8;
const currentTipX = -0.9 + writingProgress * 1.9;
handRoot.position.x = currentTipX + 0.75;
handRoot.position.y =
0.795 + Math.sin(writingProgress * Math.PI * 12) * 0.008;
handRoot.rotation.z =
Math.sin(writingProgress * Math.PI * 8) * 0.012;
updateInk(currentTipX);
updateReadout();
renderer.render(scene, camera);
}
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
if (!cameraHasBeenMoved) {
cameraRadius = camera.aspect < 1 ? 15 : 10.8;
}
updateCamera();
}
window.addEventListener("resize", resize);
resize();
updateSweepButton();
updateReadout();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```