Prompt specification
Build or customize the Three.js 3D scene "Shadow size distance experiment" 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: Shadow size distance experiment
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/shadow-size-distance-experiment-x0370
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a low-poly shadow-puppet experiment. Arrange a small lamp at one end of a narrow table and a vertical white screen at the other, with a flat rabbit-shaped puppet between them. A slider moves the puppet along the line from lamp to screen while the other two objects stay fixed. Its projected shadow should enlarge near the lamp and shrink as it approaches the screen. Above the table, draw a few thin guide rays passing through the puppet's outline to the screen. Keep the physical puppet and projected silhouette visible together throughout the movement.
Working Three.js Starter (shadow-size-distance-experiment-x0370):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Shadow Puppet Experiment</title>
<style>
:root {
color-scheme: light;
--ink: #29252d;
--muted: #706a73;
--panel: rgba(255, 252, 246, 0.88);
--border: rgba(62, 48, 55, 0.16);
--orange: #df693e;
--ray: #e89a2f;
--shadow: #353039;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e4dc;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
}
.info {
position: fixed;
top: 22px;
left: 22px;
width: min(330px, calc(100vw - 44px));
padding: 17px 18px 15px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 12px 35px rgba(55, 42, 48, 0.11);
backdrop-filter: blur(10px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 5px;
color: var(--orange);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 23px;
line-height: 1.1;
letter-spacing: -0.035em;
}
.info p {
margin: 9px 0 13px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 14px;
color: #5f5862;
font-size: 11px;
font-weight: 650;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--orange);
}
.dot.shadow {
background: var(--shadow);
}
.dot.ray {
background: var(--ray);
}
.controls {
position: fixed;
left: 50%;
bottom: 22px;
width: min(690px, calc(100vw - 32px));
transform: translateX(-50%);
padding: 15px 18px 13px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 12px 35px rgba(55, 42, 48, 0.13);
backdrop-filter: blur(10px);
}
.control-top {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 14px;
margin-bottom: 10px;
}
.control-top label {
font-size: 13px;
font-weight: 750;
}
.readouts {
display: flex;
gap: 14px;
color: var(--muted);
font-size: 11px;
font-weight: 650;
white-space: nowrap;
}
.readouts strong {
color: var(--ink);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 22px;
margin: 0;
accent-color: var(--orange);
cursor: ew-resize;
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: 3px;
color: #8a828b;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.04em;
text-transform: uppercase;
}
@media (max-width: 650px) {
.info {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 14px;
}
h1 {
font-size: 19px;
}
.info p {
margin: 6px 0 9px;
font-size: 11px;
}
.controls {
bottom: 12px;
width: calc(100vw - 24px);
padding: 12px 14px 10px;
}
.control-top {
align-items: flex-start;
flex-direction: column;
gap: 3px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section class="info">
<div class="eyebrow">Optics demonstration</div>
<h1>Shadow Puppet Lab</h1>
<p>
Slide the rabbit away from the lamp and toward the screen. Its
projected silhouette grows near the lamp and shrinks near the screen.
</p>
<div class="legend">
<span><i class="dot"></i> Physical puppet</span>
<span><i class="dot shadow"></i> Projection</span>
<span><i class="dot ray"></i> Guide rays</span>
</div>
</section>
<section class="controls">
<div class="control-top">
<label for="position">Puppet position</label>
<div class="readouts">
<span>Position <strong id="positionValue">25%</strong></span>
<span>Shadow scale <strong id="scaleValue">3.00×</strong></span>
</div>
</div>
<input
id="position"
type="range"
min="0"
max="100"
step="1"
value="25"
aria-label="Move the puppet between the lamp and screen"
/>
<div class="range-labels">
<span>Near lamp</span>
<span>Near screen</span>
</div>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneElement = document.getElementById("scene");
const slider = document.getElementById("position");
const positionValue = document.getElementById("positionValue");
const scaleValue = document.getElementById("scaleValue");
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0xe9e4dc);
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly shadow puppet experiment"
);
sceneElement.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e4dc);
scene.fog = new THREE.Fog(0xe9e4dc, 15, 30);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
camera.position.set(-7.6, 5.2, 8.3);
camera.lookAt(-0.15, 1.45, 0);
const hemisphere = new THREE.HemisphereLight(
0xfff5df,
0x5d6570,
2.1
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffedcf, 2.7);
keyLight.position.set(-3.5, 7.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 22;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
// -------------------------------------------------------------------------
// Scene constants
// -------------------------------------------------------------------------
const TABLE_TOP = 0.72;
const PUPPET_BASE = 0.76;
const PUPPET_MIN = -1.05;
const PUPPET_MAX = 2.65;
const SCREEN_CENTER_X = 3.45;
const SCREEN_FRONT_X = 3.405;
const PROJECTION_X = 3.397;
const RAY_END_X = 3.382;
// The lamp's effective point source.
const lampSource = new THREE.Vector3(-2.83, 0.89, 0);
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0x855333,
roughness: 0.88,
metalness: 0.02,
flatShading: true
});
const darkWoodMaterial = new THREE.MeshStandardMaterial({
color: 0x4b3429,
roughness: 0.9,
flatShading: true
});
const screenMaterial = new THREE.MeshStandardMaterial({
color: 0xf6f1e7,
roughness: 0.95,
metalness: 0
});
const lampMaterial = new THREE.MeshStandardMaterial({
color: 0x34343a,
roughness: 0.58,
metalness: 0.45,
flatShading: true
});
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
function addBox(
width,
height,
depth,
material,
x,
y,
z,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
scene.add(mesh);
return mesh;
}
function polygonShape(points) {
const shape = new THREE.Shape();
shape.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i][0], points[i][1]);
}
shape.closePath();
return shape;
}
function ellipseShape(cx, cy, rx, ry) {
const shape = new THREE.Shape();
shape.absellipse(
cx,
cy,
rx,
ry,
0,
Math.PI * 2,
false,
0
);
return shape;
}
// -------------------------------------------------------------------------
// Floor and narrow wooden table
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0xd8d2c9,
roughness: 1
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.025;
floor.receiveShadow = true;
scene.add(floor);
// Three narrow planks make up the tabletop.
const plankMaterials = [
new THREE.MeshStandardMaterial({
color: 0x9a6742,
roughness: 0.88,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x8b5b39,
roughness: 0.9,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xa16c45,
roughness: 0.88,
flatShading: true
})
];
for (let i = -1; i <= 1; i++) {
addBox(
8.2,
0.16,
0.67,
plankMaterials[i + 1],
-0.05,
TABLE_TOP - 0.08,
i * 0.69
);
}
// Table apron and legs.
addBox(
8.0,
0.12,
1.94,
darkWoodMaterial,
-0.05,
0.49,
0
);
for (const x of [-3.4, 3.12]) {
for (const z of [-0.78, 0.78]) {
addBox(
0.18,
0.55,
0.18,
darkWoodMaterial,
x,
0.275,
z
);
}
}
// -------------------------------------------------------------------------
// Small low-poly lamp at the left end of the table
// -------------------------------------------------------------------------
const lampBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.32, 0.37, 0.10, 10),
lampMaterial
);
lampBase.position.set(-3.22, 0.77, 0);
lampBase.castShadow = true;
lampBase.receiveShadow = true;
scene.add(lampBase);
addBox(
0.46,
0.28,
0.40,
lampMaterial,
-3.25,
0.94,
0
);
const lampShade = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.16, 0.36, 8, 1, true),
lampMaterial
);
lampShade.rotation.z = -Math.PI / 2;
lampShade.position.set(-3.03, 0.90, 0);
lampShade.castShadow = true;
scene.add(lampShade);
const lampLens = new THREE.Mesh(
new THREE.CircleGeometry(0.16, 16),
new THREE.MeshBasicMaterial({
color: 0xffd27a,
side: THREE.DoubleSide
})
);
lampLens.rotation.y = Math.PI / 2;
lampLens.position.copy(lampSource);
scene.add(lampLens);
const lampLight = new THREE.PointLight(0xffb75d, 1.4, 3.2, 2);
lampLight.position.copy(lampSource);
scene.add(lampLight);
const lampPool = new THREE.Mesh(
new THREE.CircleGeometry(0.48, 24),
new THREE.MeshBasicMaterial({
color: 0xffb35b,
transparent: true,
opacity: 0.13,
depthWrite: false
})
);
lampPool.rotation.x = -Math.PI / 2;
lampPool.position.set(-2.98, TABLE_TOP + 0.002, 0);
scene.add(lampPool);
// -------------------------------------------------------------------------
// Vertical white screen at the far end
// -------------------------------------------------------------------------
addBox(
0.09,
3.35,
3.12,
screenMaterial,
SCREEN_CENTER_X,
2.075,
0,
false,
true
);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x59443a,
roughness: 0.82,
flatShading: true
});
for (const z of [-1.60, 1.60]) {
addBox(
0.11,
3.43,
0.08,
frameMaterial,
SCREEN_FRONT_X - 0.015,
2.075,
z
);
}
for (const y of [0.36, 3.79]) {
addBox(
0.11,
0.08,
3.28,
frameMaterial,
SCREEN_FRONT_X - 0.015,
y,
0
);
}
// -------------------------------------------------------------------------
// Flat, low-poly rabbit puppet
// -------------------------------------------------------------------------
const puppetGroup = new THREE.Group();
puppetGroup.position.set(PUPPET_MIN, PUPPET_BASE, 0);
// The puppet is a vertical plane parallel to the screen.
puppetGroup.rotation.y = Math.PI / 2;
scene.add(puppetGroup);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0xc95d3b,
roughness: 0.82,
side: THREE.DoubleSide,
flatShading: true
});
const headMaterial = new THREE.MeshStandardMaterial({
color: 0xe4a05a,
roughness: 0.8,
side: THREE.DoubleSide,
flatShading: true
});
const earMaterial = new THREE.MeshStandardMaterial({
color: 0xd77843,
roughness: 0.82,
side: THREE.DoubleSide,
flatShading: true
});
const detailMaterial = new THREE.MeshBasicMaterial({
color: 0x38272b,
side: THREE.DoubleSide
});
const innerEarMaterial = new THREE.MeshStandardMaterial({
color: 0xb84e4d,
roughness: 0.85,
side: THREE.DoubleSide,
flatShading: true
});
const shadowMaterial = new THREE.MeshBasicMaterial({
color: 0x302b33,
transparent: true,
opacity: 0.8,
side: THREE.DoubleSide,
depthWrite: false
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x6d3326,
transparent: true,
opacity: 0.48
});
const parts = [];
function addPuppetPart(shape, material, depthOffset = 0) {
const geometry = new THREE.ShapeGeometry(shape, 10);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.z = depthOffset;
mesh.castShadow = true;
mesh.receiveShadow = true;
puppetGroup.add(mesh);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 12),
edgeMaterial
);
mesh.add(edges);
const shadowGeometry = geometry.clone();
shadowGeometry.attributes.position.setUsage(
THREE.DynamicDrawUsage
);
const shadowMesh = new THREE.Mesh(
shadowGeometry,
shadowMaterial
);
shadowMesh.frustumCulled = false;
shadowMesh.renderOrder = 4;
scene.add(shadowMesh);
parts.push({
geometry,
shadowGeometry,
shadowMesh,
depthOffset
});
}
const bodyShape = polygonShape([
[0.00, 0.00],
[0.16, 0.02],
[0.29, 0.14],
[0.33, 0.34],
[0.29, 0.54],
[0.17, 0.70],
[0.00, 0.74],
[-0.17, 0.70],
[-0.29, 0.54],
[-0.33, 0.34],
[-0.29, 0.14],
[-0.16, 0.02]
]);
const headShape = ellipseShape(0, 0.48, 0.16, 0.16);
const leftEarShape = polygonShape([
[-0.15, 0.56],
[-0.21, 0.82],
[-0.13, 0.92],
[-0.055, 0.84],
[-0.065, 0.58]
]);
const rightEarShape = polygonShape([
[0.15, 0.56],
[0.065, 0.58],
[0.055, 0.84],
[0.13, 0.92],
[0.21, 0.82]
]);
const tailShape = ellipseShape(0.34, 0.29, 0.075, 0.075);
const leftFootShape = ellipseShape(-0.16, 0.025, 0.10, 0.055);
const rightFootShape = ellipseShape(0.16, 0.025, 0.10, 0.055);
addPuppetPart(bodyShape, bodyMaterial, 0.0);
addPuppetPart(headShape, headMaterial, -0.004);
addPuppetPart(leftEarShape, earMaterial, -0.007);
addPuppetPart(rightEarShape, earMaterial, -0.007);
addPuppetPart(tailShape, headMaterial, -0.004);
addPuppetPart(leftFootShape, bodyMaterial, -0.004);
addPuppetPart(rightFootShape, bodyMaterial, -0.004);
// Small physical details help identify the front of the puppet.
for (const x of [-0.058, 0.058]) {
const eye = new THREE.Mesh(
new THREE.CircleGeometry(0.018, 8),
detailMaterial
);
eye.position.set(x, 0.515, -0.015);
puppetGroup.add(eye);
}
const nose = new THREE.Mesh(
new THREE.ShapeGeometry(
polygonShape([
[0, 0],
[0.025, 0.018],
[-0.025, 0.018]
]),
1
),
detailMaterial
);
nose.position.set(0, 0.405, -0.016);
puppetGroup.add(nose);
const innerLeftEar = new THREE.Mesh(
new THREE.ShapeGeometry(
polygonShape([
[-0.135, 0.64],
[-0.165, 0.80],
[-0.125, 0.86],
[-0.095, 0.81],
[-0.10, 0.66]
]),
1
),
innerEarMaterial
);
innerLeftEar.position.z = -0.015;
puppetGroup.add(innerLeftEar);
const innerRightEar = new THREE.Mesh(
new THREE.ShapeGeometry(
polygonShape([
[0.135, 0.64],
[0.10, 0.66],
[0.095, 0.81],
[0.125, 0.86],
[0.165, 0.80]
]),
1
),
innerEarMaterial
);
innerRightEar.position.z = -0.015;
puppetGroup.add(innerRightEar);
// -------------------------------------------------------------------------
// Guide rays through selected points on the rabbit outline
// -------------------------------------------------------------------------
const outlineRayPoints = [
[-0.11, 0.91],
[0.11, 0.91],
[0.00, 0.64],
[-0.29, 0.30],
[0.29, 0.30]
];
const rayMaterial = new THREE.LineBasicMaterial({
color: 0xe9952b,
transparent: true,
opacity: 0.72,
depthTest: false,
depthWrite: false
});
const rayMarkerMaterial = new THREE.MeshBasicMaterial({
color: 0xf2ad42,
transparent: true,
opacity: 0.95,
depthTest: false,
depthWrite: false
});
const rays = [];
const rayMarkers = [];
const markerGeometry = new THREE.SphereGeometry(0.019, 7, 5);
for (let i = 0; i < outlineRayPoints.length; i++) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(9), 3)
);
const line = new THREE.Line(geometry, rayMaterial);
line.frustumCulled = false;
line.renderOrder = 10;
scene.add(line);
const marker = new THREE.Mesh(
markerGeometry,
rayMarkerMaterial
);
marker.renderOrder = 11;
scene.add(marker);
rays.push(line);
rayMarkers.push(marker);
}
// -------------------------------------------------------------------------
// Projection math
// -------------------------------------------------------------------------
function projectPoint(pointX, pointY, pointZ, targetX, target) {
const t =
(targetX - lampSource.x) /
(pointX - lampSource.x);
target.set(
targetX,
lampSource.y + (pointY - lampSource.y) * t,
lampSource.z + (pointZ - lampSource.z) * t
);
return target;
}
const projectedPoint = new THREE.Vector3();
function updateExperiment() {
const percent = Number(slider.value);
const normalized = percent / 100;
const puppetX = THREE.MathUtils.lerp(
PUPPET_MIN,
PUPPET_MAX,
normalized
);
puppetGroup.position.x = puppetX;
// Reproject every polygon vertex from the lamp through the puppet.
for (const part of parts) {
const source = part.geometry.attributes.position;
const destination = part.shadowGeometry.attributes.position;
for (let i = 0; i < source.count; i++) {
const localX = source.getX(i);
const localY = source.getY(i);
// The puppet's local X maps to world -Z after its 90° rotation.
const worldY = PUPPET_BASE + localY;
const worldZ = -localX;
const worldX = puppetX + part.depthOffset;
const t =
(PROJECTION_X - lampSource.x) /
(worldX - lampSource.x);
const projectedY =
lampSource.y + (worldY - lampSource.y) * t;
const projectedZ =
lampSource.z + (worldZ - lampSource.z) * t;
destination.setXYZ(
i,
PROJECTION_X,
projectedY,
projectedZ
);
}
destination.needsUpdate = true;
}
// Update the guide rays and their small outline hit markers.
for (let i = 0; i < outlineRayPoints.length; i++) {
const [localX, localY] = outlineRayPoints[i];
const hit = new THREE.Vector3(
puppetX - 0.014,
PUPPET_BASE + localY,
-localX
);
projectPoint(
hit.x,
hit.y,
hit.z,
RAY_END_X,
projectedPoint
);
const positions = rays[i].geometry.attributes.position;
positions.setXYZ(0, lampSource.x, lampSource.y, lampSource.z);
positions.setXYZ(1, hit.x, hit.y, hit.z);
positions.setXYZ(
2,
projectedPoint.x,
projectedPoint.y,
projectedPoint.z
);
positions.needsUpdate = true;
rayMarkers[i].position.copy(hit);
}
const projectionScale =
(PROJECTION_X - lampSource.x) /
(puppetX - lampSource.x);
positionValue.textContent = `${percent}%`;
scaleValue.textContent = `${projectionScale.toFixed(2)}×`;
}
slider.addEventListener("input", updateExperiment);
updateExperiment();
// -------------------------------------------------------------------------
// Responsive rendering
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.fov = camera.aspect < 1 ? 54 : 39;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
</body>
</html>
```