Prompt specification
Build or customize the Three.js 3D scene "Moving-day doorway challenge" 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: Moving-day doorway challenge
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/moving-day-doorway-challenge-x0012
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a rubber door wedge. Create an apartment entrance with boxes, trolley, and swinging door; users place a wedge and guide the load through a route wide enough for passage. Low-poly.
Working Three.js Starter (moving-day-doorway-challenge-x0012):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Doorway Delivery — Rubber Wedge Puzzle</title>
<style>
:root {
--ink: #21312d;
--muted: #64716d;
--panel: rgba(248, 247, 240, 0.92);
--line: rgba(33, 49, 45, 0.14);
--accent: #ed8a42;
--green: #3b9b70;
--red: #c65f4b;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #c5d1cc;
color: var(--ink);
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;
}
#hud {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(350px, calc(100vw - 36px));
padding: 20px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(35, 47, 43, 0.17);
backdrop-filter: blur(14px);
user-select: none;
}
.eyebrow {
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 8px;
font-size: 31px;
line-height: 0.98;
letter-spacing: -0.045em;
}
h1 span { color: var(--accent); }
.intro {
margin: 0 0 17px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.steps {
display: grid;
gap: 8px;
margin-bottom: 16px;
}
.step {
display: flex;
align-items: center;
gap: 11px;
padding: 10px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.48);
transition: 160ms ease;
}
.step > span {
display: grid;
flex: 0 0 27px;
width: 27px;
height: 27px;
place-items: center;
border-radius: 8px;
background: #d9ddd8;
color: #53615c;
font-size: 12px;
font-weight: 800;
}
.step strong {
display: block;
margin-bottom: 2px;
font-size: 12px;
}
.step small {
display: block;
color: var(--muted);
font-size: 10.5px;
line-height: 1.35;
}
.step.active {
border-color: rgba(237, 138, 66, 0.6);
background: rgba(237, 138, 66, 0.08);
}
.step.active > span {
background: var(--accent);
color: white;
}
.step.done {
border-color: rgba(59, 155, 112, 0.38);
background: rgba(59, 155, 112, 0.08);
}
.step.done > span {
background: var(--green);
color: white;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 15px;
}
.metric {
min-width: 0;
padding: 10px 8px;
border-radius: 10px;
background: rgba(33, 49, 45, 0.065);
}
.metric small {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
text-transform: uppercase;
letter-spacing: 0.07em;
}
.metric b {
font-size: 14px;
white-space: nowrap;
}
.metric.good b { color: var(--green); }
.metric.bad b { color: var(--red); }
.reach {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 7px;
font-size: 11px;
font-weight: 700;
}
output {
color: var(--accent);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
margin: 0 0 15px;
accent-color: var(--accent);
cursor: pointer;
}
.buttons {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 10px;
padding: 9px 12px;
background: white;
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: 150ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(33, 49, 45, 0.28);
}
button.primary {
border-color: var(--accent);
background: var(--accent);
color: white;
}
button.active {
box-shadow: 0 0 0 3px rgba(237, 138, 66, 0.18);
}
#status {
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
#status strong {
display: block;
margin-bottom: 2px;
color: var(--ink);
font-size: 12px;
}
#toast {
position: fixed;
z-index: 20;
top: 22px;
left: 50%;
max-width: min(480px, calc(100vw - 40px));
padding: 10px 15px;
border-radius: 999px;
background: #263732;
color: white;
box-shadow: 0 10px 30px rgba(25, 40, 35, 0.22);
font-size: 12px;
opacity: 0;
pointer-events: none;
transform: translate(-50%, -12px);
transition: 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#help {
position: fixed;
z-index: 5;
bottom: 16px;
left: 50%;
width: max-content;
max-width: calc(100vw - 30px);
padding: 9px 14px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 999px;
background: rgba(248, 247, 240, 0.8);
color: #53615c;
box-shadow: 0 8px 24px rgba(35, 47, 43, 0.12);
font-size: 10.5px;
text-align: center;
pointer-events: none;
transform: translateX(-50%);
backdrop-filter: blur(10px);
}
#success {
position: fixed;
z-index: 30;
top: 50%;
left: 50%;
width: min(360px, calc(100vw - 40px));
padding: 27px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 20px;
background: rgba(248, 247, 240, 0.96);
box-shadow: 0 30px 80px rgba(30, 45, 39, 0.25);
text-align: center;
transform: translate(-50%, -50%);
backdrop-filter: blur(16px);
}
#success[hidden] { display: none; }
.success-icon {
display: grid;
width: 54px;
height: 54px;
margin: 0 auto 13px;
place-items: center;
border-radius: 50%;
background: var(--green);
color: white;
font-size: 28px;
font-weight: 800;
}
#success h2 {
margin: 0 0 7px;
font-size: 24px;
letter-spacing: -0.03em;
}
#success p {
margin: 0 0 18px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
@media (max-width: 650px) {
#hud {
top: 10px;
left: 10px;
width: min(320px, calc(100vw - 20px));
padding: 15px;
}
h1 { font-size: 25px; }
.intro { margin-bottom: 11px; }
.steps { gap: 6px; margin-bottom: 11px; }
.step { padding: 8px; }
.metrics { margin-bottom: 11px; }
#help { bottom: 10px; font-size: 9px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive apartment delivery scene"></canvas>
<aside id="hud">
<div class="eyebrow">Low-poly delivery puzzle</div>
<h1>Doorway<br><span>Delivery</span></h1>
<p class="intro">
Position the rubber wedge to hold the apartment door open, then guide the
loaded trolley through the narrow box lane.
</p>
<div class="steps">
<div class="step active" id="stepWedge">
<span>1</span>
<div>
<strong>Set the wedge</strong>
<small>Move it toward the latch until the door gap is at least 1.40 m.</small>
</div>
</div>
<div class="step" id="stepTrolley">
<span>2</span>
<div>
<strong>Guide the load</strong>
<small>Drag the trolley through the doorway and into the green delivery zone.</small>
</div>
</div>
</div>
<div class="metrics">
<div class="metric bad" id="doorMetric">
<small>Door gap</small>
<b id="doorValue">0.00 m</b>
</div>
<div class="metric good" id="laneMetric">
<small>Lane gap</small>
<b id="laneValue">1.40 m</b>
</div>
<div class="metric" id="angleMetric">
<small>Door</small>
<b id="angleValue">0°</b>
</div>
</div>
<label class="reach" for="wedgeSlider">
<span>Wedge reach along door</span>
<output id="wedgeOutput">62 cm</output>
</label>
<input
id="wedgeSlider"
type="range"
min="35"
max="205"
step="1"
value="62"
aria-label="Wedge reach along door"
>
<div class="buttons">
<button class="primary" id="placeButton">Place wedge</button>
<button id="cameraButton">Reset view</button>
<button id="resetButton">Reset</button>
</div>
<div id="status">
<strong id="statusTitle">The door is too narrow</strong>
<span id="statusText">
Move the wedge toward the latch side, or drag its orange reach marker.
</span>
</div>
</aside>
<div id="toast"></div>
<div id="help">
Drag orange trolley ring · Drag empty space to orbit · Scroll to zoom · Arrow keys move trolley
</div>
<div id="success" hidden>
<div class="success-icon">✓</div>
<h2>Delivery complete</h2>
<p>
The wedge holds a safe opening and the trolley has cleared the apartment
box lane.
</p>
<button class="primary" id="successReset">Deliver another load</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, camera and lighting
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc5d1cc);
scene.fog = new THREE.Fog(0xc5d1cc, 19, 42);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 0.85, -1.65);
let cameraAzimuth = 0.52;
let cameraPolar = 0.91;
let cameraDistance = 15.5;
function updateCamera() {
const sinPolar = Math.sin(cameraPolar);
camera.position.set(
cameraTarget.x + Math.sin(cameraAzimuth) * sinPolar * cameraDistance,
cameraTarget.y + Math.cos(cameraPolar) * cameraDistance,
cameraTarget.z + Math.cos(cameraAzimuth) * sinPolar * cameraDistance
);
camera.lookAt(cameraTarget);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xf7fbff, 0x786d5f, 2.0));
const sun = new THREE.DirectionalLight(0xfff0d9, 3.0);
sun.position.set(-5, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -11;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00035;
scene.add(sun);
const interiorLight = new THREE.PointLight(0xd8ecdf, 12, 11, 2);
interiorLight.position.set(0, 3.4, -5.4);
scene.add(interiorLight);
// -------------------------------------------------------------------------
// Helpers and materials
// -------------------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.84,
metalness: 0,
flatShading: true,
...options
});
}
const materials = {
outsideFloor: standardMaterial(0xc7beb0),
insideFloor: standardMaterial(0xaebbb0),
wall: standardMaterial(0xe2dccd),
wallTrim: standardMaterial(0xd2ccbd),
facade: standardMaterial(0xdad4c7),
frame: standardMaterial(0x46524d),
door: standardMaterial(0x48675d),
doorPanel: standardMaterial(0x55776c),
metal: standardMaterial(0x38413e, { metalness: 0.5, roughness: 0.48 }),
rubber: standardMaterial(0x202624, { roughness: 0.98 }),
orange: standardMaterial(0xed8a42, { roughness: 0.65 }),
cardboardA: standardMaterial(0xbd8c59),
cardboardB: standardMaterial(0xcb9c68),
cardboardC: standardMaterial(0xa9784f),
tape: standardMaterial(0xe6c997, { roughness: 0.7 }),
green: standardMaterial(0x5a9b76)
};
function addBox(parent, size, position, material, cast = true, receive = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
parent.add(mesh);
return mesh;
}
function makeLabel(text, background = "#263732", foreground = "#ffffff") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const context = labelCanvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, 512, 128);
context.fillStyle = foreground;
context.font = "800 56px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 67);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(1.7, 0.425, 1);
sprite.renderOrder = 5;
return sprite;
}
function createCrate(parent, size, position, material) {
const group = new THREE.Group();
group.position.set(position[0], position[1], position[2]);
parent.add(group);
addBox(
group,
[size[0], size[1], size[2]],
[0, size[1] / 2, 0],
material
);
addBox(
group,
[0.13, 0.018, size[2] * 0.99],
[0, size[1] + 0.01, 0],
materials.tape
);
addBox(
group,
[0.13, size[1] * 0.98, 0.018],
[0, size[1] / 2, size[2] / 2 + 0.01],
materials.tape
);
return group;
}
// -------------------------------------------------------------------------
// Apartment and corridor
// -------------------------------------------------------------------------
addBox(
scene,
[6.4, 0.18, 8.4],
[0, -0.09, 4.2],
materials.outsideFloor,
false,
true
);
addBox(
scene,
[6.4, 0.18, 8.15],
[0, -0.09, -4.075],
materials.insideFloor,
false,
true
);
// Facade wall around the doorway.
addBox(scene, [2.02, 3.2, 0.24], [-2.19, 1.6, 0], materials.facade);
addBox(scene, [2.02, 3.2, 0.24], [2.19, 1.6, 0], materials.facade);
addBox(scene, [2.36, 0.62, 0.24], [0, 2.89, 0], materials.facade);
// Interior cutaway walls.
addBox(scene, [6.4, 3.2, 0.18], [0, 1.6, -8.18], materials.wall);
addBox(scene, [0.16, 2.6, 8.15], [-3.25, 1.3, -4.075], materials.wall);
addBox(scene, [0.16, 0.64, 8.15], [3.25, 0.32, -4.075], materials.wallTrim);
// Corridor edging and skirting.
addBox(scene, [0.16, 0.32, 8.4], [-3.25, 0.16, 4.2], materials.wallTrim);
addBox(scene, [0.16, 0.32, 8.4], [3.25, 0.16, 4.2], materials.wallTrim);
addBox(scene, [6.4, 0.12, 0.06], [0, 0.06, -8.05], materials.frame);
// Door frame.
addBox(scene, [0.12, 2.68, 0.34], [-1.19, 1.34, 0.03], materials.frame);
addBox(scene, [0.12, 2.68, 0.34], [1.19, 1.34, 0.03], materials.frame);
addBox(scene, [2.5, 0.12, 0.34], [0, 2.64, 0.03], materials.frame);
addBox(scene, [2.3, 0.025, 0.34], [0, 0.012, 0.03], materials.metal);
const apartmentSign = makeLabel("APT 4B");
apartmentSign.position.set(-2.08, 2.38, 0.2);
apartmentSign.scale.set(1.35, 0.34, 1);
scene.add(apartmentSign);
const deliveryLabel = makeLabel("DELIVERY", "#347e5d");
deliveryLabel.position.set(0, 1.1, -6.9);
deliveryLabel.scale.set(1.55, 0.39, 1);
scene.add(deliveryLabel);
// -------------------------------------------------------------------------
// Swinging door
// -------------------------------------------------------------------------
const HINGE_X = -1.13;
const DOOR_WIDTH = 2.22;
const DOOR_HEIGHT = 2.52;
const doorPivot = new THREE.Group();
doorPivot.position.set(HINGE_X, 0, 0);
scene.add(doorPivot);
addBox(
doorPivot,
[DOOR_WIDTH, DOOR_HEIGHT, 0.12],
[DOOR_WIDTH / 2, 0.08 + DOOR_HEIGHT / 2, 0],
materials.door
);
addBox(
doorPivot,
[1.55, 0.83, 0.018],
[DOOR_WIDTH * 0.51, 1.76, 0.071],
materials.doorPanel
);
addBox(
doorPivot,
[1.55, 0.58, 0.018],
[DOOR_WIDTH * 0.51, 0.64, 0.071],
materials.doorPanel
);
const handle = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 0.13, 10),
materials.metal
);
handle.rotation.x = Math.PI / 2;
handle.position.set(1.98, 1.27, 0.13);
handle.castShadow = true;
doorPivot.add(handle);
const handleKnob = new THREE.Mesh(
new THREE.SphereGeometry(0.085, 10, 8),
materials.metal
);
handleKnob.position.set(1.98, 1.27, 0.22);
handleKnob.castShadow = true;
doorPivot.add(handleKnob);
for (const y of [0.45, 1.3, 2.15]) {
const hinge = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 0.18, 8),
materials.metal
);
hinge.position.set(0, y, 0);
doorPivot.add(hinge);
}
// -------------------------------------------------------------------------
// Rubber wedge and placement zone
// -------------------------------------------------------------------------
const wedgeGroup = new THREE.Group();
scene.add(wedgeGroup);
const wedgeShape = new THREE.Shape();
wedgeShape.moveTo(-0.28, 0);
wedgeShape.lineTo(0.28, 0);
wedgeShape.lineTo(0.16, 0.17);
wedgeShape.closePath();
const wedgeGeometry = new THREE.ExtrudeGeometry(wedgeShape, {
depth: 0.36,
bevelEnabled: false,
steps: 1
});
wedgeGeometry.translate(0, 0, -0.18);
const wedgeBody = new THREE.Mesh(wedgeGeometry, materials.rubber);
wedgeBody.castShadow = true;
wedgeBody.receiveShadow = true;
wedgeGroup.add(wedgeBody);
// Raised rubber ribs on the sloped face.
const ribAngle = Math.atan2(0.17, 0.44);
for (const x of [-0.13, -0.02, 0.09]) {
const y = (x + 0.28) * (0.17 / 0.44);
const rib = addBox(
wedgeGroup,
[0.055, 0.025, 0.31],
[x, y + 0.015, 0],
materials.rubber
);
rib.rotation.z = ribAngle;
}
const zoneMaterial = new THREE.MeshBasicMaterial({
color: 0xf0b24f,
transparent: true,
opacity: 0.1,
depthWrite: false
});
const placementZone = addBox(
scene,
[1.9, 0.012, 1.7],
[0.1, 0.015, -0.83],
zoneMaterial,
false,
false
);
const zoneBorderMaterial = new THREE.MeshBasicMaterial({
color: 0xe6a43c,
transparent: true,
opacity: 0.35,
depthWrite: false
});
addBox(scene, [1.9, 0.014, 0.035], [0.1, 0.022, -1.67], zoneBorderMaterial, false, false);
addBox(scene, [1.9, 0.014, 0.035], [0.1, 0.022, 0.01], zoneBorderMaterial, false, false);
addBox(scene, [0.035, 0.014, 1.7], [-0.84, 0.022, -0.83], zoneBorderMaterial, false, false);
addBox(scene, [0.035, 0.014, 1.7], [1.04, 0.022, -0.83], zoneBorderMaterial, false, false);
// -------------------------------------------------------------------------
// Apartment boxes and the 1.40 m passage
// -------------------------------------------------------------------------
const obstacles = [];
function addObstacle(x, z, width, height, depth, material) {
createCrate(scene, [width, height, depth], [x, 0, z], material);
obstacles.push({ x, z, width, depth });
}
addObstacle(-2.25, -3.2, 1.45, 1.45, 1.35, materials.cardboardA);
addObstacle(-2.45, -4.35, 1.05, 1.0, 1.15, materials.cardboardC);
addObstacle(2.15, -3.25, 1.55, 1.3, 1.4, materials.cardboardB);
addObstacle(2.45, -4.55, 0.95, 1.1, 1.25, materials.cardboardA);
// These two boxes define a route exactly 1.40 m wide.
addObstacle(-1.2, -5.15, 1.0, 1.05, 1.0, materials.cardboardB);
addObstacle(1.2, -5.15, 1.0, 0.9, 1.0, materials.cardboardC);
// The visible facade also acts as a collision surface.
const fixedObstacles = [
{ x: -2.19, z: 0, width: 2.02, depth: 0.24 },
{ x: 2.19, z: 0, width: 2.02, depth: 0.24 }
];
// Floor route dashes.
const dashMaterial = new THREE.MeshBasicMaterial({
color: 0x4c9c73,
transparent: true,
opacity: 0.48,
depthWrite: false
});
for (let z = 7.1; z > 0.7; z -= 0.62) {
addBox(scene, [0.07, 0.014, 0.27], [0.28, 0.015, z], dashMaterial, false, false);
}
for (let z = -0.65; z > -7.8; z -= 0.62) {
addBox(scene, [0.07, 0.014, 0.27], [0.08, 0.015, z], dashMaterial, false, false);
}
const goalMaterial = new THREE.MeshBasicMaterial({
color: 0x52a879,
transparent: true,
opacity: 0.2,
depthWrite: false
});
addBox(
scene,
[1.45, 0.018, 1.25],
[0, 0.016, -6.9],
goalMaterial,
false,
false
);
// -------------------------------------------------------------------------
// Loaded trolley
// -------------------------------------------------------------------------
const TROLLEY_WIDTH = 1.08;
const TROLLEY_LENGTH = 1.42;
const REQUIRED_CLEARANCE = 1.4;
const trolley = new THREE.Group();
trolley.position.set(0.28, 0, 5.6);
scene.add(trolley);
addBox(
trolley,
[TROLLEY_WIDTH, 0.12, TROLLEY_LENGTH],
[0, 0.27, 0],
materials.metal
);
addBox(
trolley,
[0.93, 0.72, 1.02],
[-0.02, 0.69, -0.12],
materials.cardboardA
);
addBox(
trolley,
[0.68, 0.48, 0.72],
[0.07, 1.29, -0.15],
materials.cardboardB
);
// Handle frame.
addBox(trolley, [0.065, 1.03, 0.065], [-0.47, 0.81, 0.64], materials.metal);
addBox(trolley, [0.065, 1.03, 0.065], [0.47, 0.81, 0.64], materials.metal);
addBox(trolley, [1.02, 0.075, 0.075], [0, 1.29, 0.64], materials.orange);
addBox(trolley, [0.48, 0.09, 0.09], [0, 1.29, 0.64], materials.rubber);
// Low-poly wheels.
for (const x of [-0.55, 0.55]) {
for (const z of [-0.51, 0.51]) {
const wheel = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.16, 0.12, 10),
materials.rubber
);
wheel.rotation.z = Math.PI / 2;
wheel.position.set(x, 0.17, z);
wheel.castShadow = true;
trolley.add(wheel);
}
}
const trolleyRing = new THREE.Mesh(
new THREE.TorusGeometry(0.79, 0.026, 6, 36),
new THREE.MeshBasicMaterial({
color: 0xed8a42,
transparent: true,
opacity: 0.75,
depthWrite: false
})
);
trolleyRing.rotation.x = Math.PI / 2;
trolleyRing.position.y = 0.035;
trolleyRing.renderOrder = 4;
trolley.add(trolleyRing);
const interactiveObjects = [];
function markInteractive(root, action) {
root.traverse((object) => {
if (object.isMesh) {
object.userData.action = action;
interactiveObjects.push(object);
}
});
}
markInteractive(wedgeGroup, "wedge");
markInteractive(trolley, "trolley");
// -------------------------------------------------------------------------
// Interaction state
// -------------------------------------------------------------------------
const ui = {
placeButton: document.querySelector("#placeButton"),
cameraButton: document.querySelector("#cameraButton"),
resetButton: document.querySelector("#resetButton"),
successReset: document.querySelector("#successReset"),
slider: document.querySelector("#wedgeSlider"),
wedgeOutput: document.querySelector("#wedgeOutput"),
doorValue: document.querySelector("#doorValue"),
laneValue: document.querySelector("#laneValue"),
angleValue: document.querySelector("#angleValue"),
doorMetric: document.querySelector("#doorMetric"),
laneMetric: document.querySelector("#laneMetric"),
angleMetric: document.querySelector("#angleMetric"),
stepWedge: document.querySelector("#stepWedge"),
stepTrolley: document.querySelector("#stepTrolley"),
statusTitle: document.querySelector("#statusTitle"),
statusText: document.querySelector("#statusText"),
toast: document.querySelector("#toast"),
success: document.querySelector("#success")
};
const WEDGE_MIN = 0.35;
const WEDGE_MAX = 2.05;
let wedgeReach = 0.62;
let currentDoorAngle = THREE.MathUtils.degToRad(
THREE.MathUtils.mapLinear(wedgeReach, WEDGE_MIN, WEDGE_MAX, 12, 86)
);
let targetDoorAngle = currentDoorAngle;
let placing = false;
let dragMode = null;
let dragPointerId = null;
let lastCollisionTime = -10000;
let success = false;
let toastTimer = 0;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const floorPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const floorPoint = new THREE.Vector3();
const trolleyOffset = new THREE.Vector3();
const wedgeDragStart = new THREE.Vector3();
let wedgeDragStartReach = wedgeReach;
function setPlacing(value) {
placing = value;
ui.placeButton.classList.toggle("active", value);
ui.placeButton.textContent = value ? "Choose a spot" : "Place wedge";
if (value) {
flash("Click the yellow strip. Farther right toward the latch opens more space.");
}
}
function flash(message) {
ui.toast.textContent = message;
ui.toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => ui.toast.classList.remove("show"), 2400);
}
function reachToAngle(reach) {
return THREE.MathUtils.degToRad(
THREE.MathUtils.mapLinear(reach, WEDGE_MIN, WEDGE_MAX, 12, 86)
);
}
function setWedgeReach(reach) {
wedgeReach = THREE.MathUtils.clamp(reach, WEDGE_MIN, WEDGE_MAX);
targetDoorAngle = reachToAngle(wedgeReach);
ui.slider.value = Math.round(wedgeReach * 100);
ui.wedgeOutput.textContent = `${Math.round(wedgeReach * 100)} cm`;
}
function updateRay(event) {
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);
}
function floorIntersection(event) {
updateRay(event);
return raycaster.ray.intersectPlane(floorPlane, floorPoint);
}
function segmentIntersectsRect(p0, p1, minX, maxX, minZ, maxZ) {
let tMin = 0;
let tMax = 1;
const dx = p1.x - p0.x;
const dz = p1.y - p0.y;
function clip(p, q) {
if (Math.abs(p) < 1e-8) return q >= 0;
const r = q / p;
if (p < 0) {
if (r > tMax) return false;
if (r > tMin) tMin = r;
} else {
if (r < tMin) return false;
if (r < tMax) tMax = r;
}
return true;
}
return (
clip(-dx, p0.x - minX) &&
clip(dx, maxX - p0.x) &&
clip(-dz, p0.y - minZ) &&
clip(dz, maxZ - p0.y)
);
}
function trolleyIsValid(position) {
const halfWidth = TROLLEY_WIDTH / 2;
const halfLength = TROLLEY_LENGTH / 2;
for (const obstacle of [...fixedObstacles, ...obstacles]) {
const overlapsX =
Math.abs(position.x - obstacle.x) <
halfWidth + obstacle.width / 2 - 0.005;
const overlapsZ =
Math.abs(position.z - obstacle.z) <
halfLength + obstacle.depth / 2 - 0.005;
if (overlapsX && overlapsZ) return false;
}
// Door slab, treated as a segment in the XZ plane.
const doorStart = new THREE.Vector2(HINGE_X, 0);
const doorEnd = new THREE.Vector2(
HINGE_X + Math.cos(currentDoorAngle) * DOOR_WIDTH,
-Math.sin(currentDoorAngle) * DOOR_WIDTH
);
const margin = 0.035;
if (
segmentIntersectsRect(
doorStart,
doorEnd,
position.x - halfWidth - margin,
position.x + halfWidth + margin,
position.z - halfLength - margin,
position.z + halfLength + margin
)
) {
return false;
}
return true;
}
function moveTrolleyTo(x, z) {
const candidate = new THREE.Vector3(
THREE.MathUtils.clamp(x, -2.67, 2.67),
0,
THREE.MathUtils.clamp(z, -7.45, 7.45)
);
if (trolleyIsValid(candidate)) {
trolley.position.copy(candidate);
} else {
lastCollisionTime = performance.now();
}
}
function resetScene(resetCamera = false) {
trolley.position.set(0.28, 0, 5.6);
setWedgeReach(0.62);
setPlacing(false);
success = false;
ui.success.hidden = true;
lastCollisionTime = -10000;
if (resetCamera) {
cameraAzimuth = 0.52;
cameraPolar = 0.91;
cameraDistance = 15.5;
updateCamera();
}
flash("Scene reset. Move the wedge toward the latch first.");
}
ui.placeButton.addEventListener("click", () => setPlacing(!placing));
ui.cameraButton.addEventListener("click", () => {
cameraAzimuth = 0.52;
cameraPolar = 0.91;
cameraDistance = 15.5;
updateCamera();
});
ui.resetButton.addEventListener("click", () => resetScene(false));
ui.successReset.addEventListener("click", () => resetScene(false));
ui.slider.addEventListener("input", () => {
setWedgeReach(Number(ui.slider.value) / 100);
setPlacing(false);
});
// -------------------------------------------------------------------------
// Pointer controls: object dragging and direct camera orbit
// -------------------------------------------------------------------------
canvas.addEventListener("pointerdown", event => {
if (event.pointerType === "mouse" && event.button !== 0) return;
const point = floorIntersection(event);
if (placing) {
if (
point &&
point.x > -0.84 &&
point.x < 1.04 &&
point.z > -1.67 &&
point.z < 0.01
) {
const u = THREE.MathUtils.clamp(
(point.x + 0.84) / 1.88,
0,
1
);
setWedgeReach(THREE.MathUtils.lerp(WEDGE_MIN, WEDGE_MAX, u));
setPlacing(false);
flash("Wedge positioned. Check the door gap, then drag the trolley.");
} else {
flash("Choose a point on the yellow wedge strip near the doorway.");
}
return;
}
updateRay(event);
const hits = raycaster.intersectObjects(interactiveObjects, true);
if (hits.length && point) {
const action = hits[0].object.userData.action;
if (action === "trolley") {
dragMode = "trolley";
trolleyOffset.copy(trolley.position).sub(point);
canvas.style.cursor = "grabbing";
} else if (action === "wedge") {
dragMode = "wedge";
wedgeDragStart.copy(point);
wedgeDragStartReach = wedgeReach;
canvas.style.cursor = "grabbing";
}
dragPointerId = event.pointerId;
canvas.setPointerCapture(event.pointerId);
} else {
dragMode = "camera";
dragPointerId = event.pointerId;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
}
});
canvas.addEventListener("pointermove", event => {
if (dragMode === "camera") {
cameraAzimuth = THREE.MathUtils.clamp(
cameraAzimuth - event.movementX * 0.006,
-1.18,
1.28
);
cameraPolar = THREE.MathUtils.clamp(
cameraPolar - event.movementY * 0.0045,
0.38,
1.24
);
updateCamera();
return;
}
const point = floorIntersection(event);
if (!point) return;
if (dragMode === "trolley") {
moveTrolleyTo(
point.x + trolleyOffset.x,
point.z + trolleyOffset.z
);
} else if (dragMode === "wedge") {
const reachChange = (point.x - wedgeDragStart.x) * 1.25;
setWedgeReach(wedgeDragStartReach + reachChange);
} else {
updateRay(event);
const hits = raycaster.intersectObjects(interactiveObjects, true);
canvas.style.cursor = hits.length ? "grab" : "default";
}
});
function endPointerInteraction(event) {
dragMode = null;
if (dragPointerId !== null) {
if (canvas.hasPointerCapture(dragPointerId)) {
canvas.releasePointerCapture(dragPointerId);
}
dragPointerId = null;
}
canvas.style.cursor = "default";
}
canvas.addEventListener("pointerup", endPointerInteraction);
canvas.addEventListener("pointercancel", endPointerInteraction);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance + event.deltaY * 0.012,
8,
24
);
updateCamera();
},
{ passive: false }
);
// Keyboard trolley movement.
window.addEventListener("keydown", event => {
if (["INPUT", "BUTTON", "TEXTAREA"].includes(document.activeElement.tagName)) {
return;
}
const step = event.shiftKey ? 0.32 : 0.16;
let x = trolley.position.x;
let z = trolley.position.z;
if (event.key === "ArrowUp") z -= step;
else if (event.key === "ArrowDown") z += step;
else if (event.key === "ArrowLeft") x -= step;
else if (event.key === "ArrowRight") x += step;
else if (event.key.toLowerCase() === "r") {
resetScene(false);
return;
} else {
return;
}
event.preventDefault();
moveTrolleyTo(x, z);
});
// -------------------------------------------------------------------------
// Animation and status updates
// -------------------------------------------------------------------------
function updateWedgeAndDoor() {
const angleDelta = targetDoorAngle - currentDoorAngle;
currentDoorAngle += angleDelta * 0.12;
doorPivot.rotation.y = currentDoorAngle;
// The wedge's raised side contacts the door at `wedgeReach`.
const contactDistance = Math.max(0, wedgeReach - 0.16);
wedgeGroup.position.set(
HINGE_X + Math.cos(currentDoorAngle) * contactDistance,
0.014,
-Math.sin(currentDoorAngle) * contactDistance
);
wedgeGroup.rotation.y = currentDoorAngle;
}
function setMetricState(element, good) {
element.classList.toggle("good", good);
element.classList.toggle("bad", !good);
}
function updateHUD() {
const doorGap = DOOR_WIDTH * (1 - Math.cos(currentDoorAngle));
const doorSafe = doorGap >= REQUIRED_CLEARANCE;
const laneSafe = 1.4 >= REQUIRED_CLEARANCE;
const collisionRecently = performance.now() - lastCollisionTime < 1800;
ui.doorValue.textContent = `${doorGap.toFixed(2)} m`;
ui.laneValue.textContent = "1.40 m";
ui.angleValue.textContent = `${Math.round(THREE.MathUtils.radToDeg(currentDoorAngle))}°`;
ui.wedgeOutput.textContent = `${Math.round(wedgeReach * 100)} cm`;
setMetricState(ui.doorMetric, doorSafe);
setMetricState(ui.laneMetric, laneSafe);
ui.angleMetric.classList.remove("good", "bad");
ui.stepWedge.classList.toggle("done", doorSafe);
ui.stepWedge.classList.toggle("active", !doorSafe);
ui.stepTrolley.classList.toggle("active", doorSafe && !success);
ui.stepTrolley.classList.toggle("done", success);
if (success) {
ui.statusTitle.textContent = "Delivery complete";
ui.statusText.textContent = "The trolley is safely inside the apartment.";
} else if (collisionRecently) {
ui.statusTitle.textContent = "That path is blocked";
ui.statusText.textContent =
"Reverse or steer around the door leaf and the cardboard boxes.";
} else if (!doorSafe) {
ui.statusTitle.textContent = "The door is too narrow";
ui.statusText.textContent =
"Move the wedge toward the latch side until the gap reaches 1.40 m.";
} else if (trolley.position.z > 1.4) {
ui.statusTitle.textContent = "Door clearance is ready";
ui.statusText.textContent =
"Drag the trolley through the opening, just right of the open door leaf.";
} else if (trolley.position.z > -4.8) {
ui.statusTitle.textContent = "Guide into the center lane";
ui.statusText.textContent =
"Steer between the stacked boxes. The green dashes mark the usable route.";
} else {
ui.statusTitle.textContent = "The lane is clear";
ui.statusText.textContent =
"Continue into the green delivery zone behind the final pair of boxes.";
}
if (
!success &&
doorSafe &&
trolley.position.z < -6.28 &&
Math.abs(trolley.position.x) < 0.28
) {
success = true;
ui.success.hidden = false;
}
}
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
updateWedgeAndDoor();
const pulse = 0.5 + 0.5 * Math.sin(time * 3);
trolleyRing.scale.setScalar(1 + pulse * 0.035);
trolleyRing.material.opacity = 0.58 + pulse * 0.22;
goalMaterial.opacity = 0.16 + pulse * 0.08;
zoneMaterial.opacity = placing
? 0.19 + pulse * 0.11
: 0.075 + pulse * 0.025;
zoneBorderMaterial.opacity = placing
? 0.55 + pulse * 0.25
: 0.25 + pulse * 0.08;
updateHUD();
renderer.render(scene, camera);
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Initialize the visible controls.
setWedgeReach(wedgeReach);
updateWedgeAndDoor();
</script>
</body>
</html>
```