Prompt specification
Build or customize the Three.js 3D scene "Button passes through a slit" 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: Button passes through a slit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/button-passes-through-a-slit-x0570
Scene Description Prompt:
Animate a button tilting through a simplified flexible buttonhole, then returning flat against the fabric; keep the sewing thread attached throughout the motion. Use Three.js from cdnjs in a single HTML file, with visible voxel blocks.
Working Three.js Starter (button-passes-through-a-slit-x0570):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Buttonhole</title>
<style>
:root {
color-scheme: light;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9e3dd;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid rgba(255, 255, 255, 0.58);
background: rgba(250, 252, 248, 0.78);
box-shadow: 0 12px 35px rgba(30, 65, 57, 0.14);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title {
top: 22px;
left: 22px;
padding: 15px 17px 14px;
border-radius: 15px;
pointer-events: none;
}
#title h1 {
margin: 0 0 4px;
color: #17483f;
font-size: 15px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#title p {
margin: 0;
color: #547168;
font-size: 12px;
line-height: 1.45;
}
#status {
margin-top: 10px;
color: #d84b70;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
}
#controls {
right: 22px;
bottom: 22px;
width: min(330px, calc(100vw - 44px));
padding: 13px;
border-radius: 15px;
}
.button-row {
display: flex;
gap: 8px;
}
.button-row button {
min-height: 38px;
padding: 0 14px;
border: 0;
border-radius: 10px;
background: #205c50;
color: white;
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease;
}
.button-row button:hover {
background: #17483f;
transform: translateY(-1px);
}
.button-row button.secondary {
background: #e2ebe6;
color: #28584e;
}
.button-row button.secondary:hover {
background: #d5e2db;
}
.track {
height: 4px;
margin-top: 12px;
overflow: hidden;
border-radius: 999px;
background: #d5e1db;
}
#progress {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: #d84b70;
}
#hint {
margin-top: 9px;
color: #607a71;
font-size: 10px;
letter-spacing: 0.04em;
text-align: center;
}
@media (max-width: 620px) {
#title {
top: 12px;
left: 12px;
padding: 12px 14px;
}
#title p {
display: none;
}
#controls {
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
}
}
</style>
</head>
<body>
<div id="stage"></div>
<section id="title" class="panel">
<h1>Voxel Buttonhole</h1>
<p>A flexible stitched opening keeps the sewing thread attached.</p>
<div id="status">FLAT / REST</div>
</section>
<section id="controls" class="panel">
<div class="button-row">
<button id="pause" type="button">Pause</button>
<button id="replay" class="secondary" type="button">Replay</button>
</div>
<div class="track"><i id="progress"></i></div>
<div id="hint">Drag to orbit · Scroll to zoom · Double-click to reset view</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 stage = document.querySelector("#stage");
const pauseButton = document.querySelector("#pause");
const replayButton = document.querySelector("#replay");
const progressBar = document.querySelector("#progress");
const statusText = document.querySelector("#status");
// -----------------------------------------------------------------------
// Renderer, scene, lighting, and camera
// -----------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9e3dd);
scene.fog = new THREE.Fog(0xd9e3dd, 12, 24);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
50
);
const cameraState = {
azimuth: 0.72,
polar: 0.72,
radius: 8.4,
target: new THREE.Vector3(0, -0.05, 0)
};
function updateCamera() {
const { azimuth, polar, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(polar) * Math.sin(azimuth),
target.y + radius * Math.cos(polar),
target.z + radius * Math.sin(polar) * Math.cos(azimuth)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
0xf4fff9,
0x78877f,
2.1
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2d9, 3.6);
keyLight.position.set(-4.5, 8, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb9dfff, 1.3);
fillLight.position.set(6, 3, -5);
scene.add(fillLight);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xbcc9c1,
roughness: 1,
metalness: 0
});
const floor = new THREE.Mesh(
new THREE.BoxGeometry(20, 0.2, 20),
floorMaterial
);
floor.position.y = -1.36;
floor.receiveShadow = true;
scene.add(floor);
// -----------------------------------------------------------------------
// Voxel fabric panel
// -----------------------------------------------------------------------
const TILE = 0.3;
const COLS = 23;
const ROWS = 15;
const CENTER_COL = 11;
const CENTER_ROW = 7;
// These are actual, visually open dimensions after accounting for tile size.
const HOLE_HALF_WIDTH = 0.318;
const HOLE_HALF_DEPTH = 0.459;
const fabricGeometry = new THREE.BoxGeometry(TILE * 0.94, 0.17, TILE * 0.94);
const fabricMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96,
metalness: 0,
flatShading: true
});
const fabricTiles = [];
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const inHole =
Math.abs(col - CENTER_COL) <= 1 &&
Math.abs(row - CENTER_ROW) <= 2;
if (inHole) continue;
fabricTiles.push({
x: (col - CENTER_COL) * TILE,
z: (row - CENTER_ROW) * TILE,
y: Math.sin(col * 2.13 + row * 1.71) * 0.008
});
}
}
const fabric = new THREE.InstancedMesh(
fabricGeometry,
fabricMaterial,
fabricTiles.length
);
fabric.receiveShadow = true;
fabric.castShadow = false;
fabric.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(fabric);
const dummy = new THREE.Object3D();
const fabricPalette = [
new THREE.Color(0x7eb7a2),
new THREE.Color(0x8bc1ad),
new THREE.Color(0x72aa98),
new THREE.Color(0x95c9b4),
new THREE.Color(0x82bba7)
];
fabricTiles.forEach((tile, index) => {
const color =
fabricPalette[
Math.floor(Math.abs(
Math.sin(index * 12.9898 + 4.141) * 43758.5453
)) % fabricPalette.length
];
fabric.setColorAt(index, color);
});
if (fabric.instanceColor) fabric.instanceColor.needsUpdate = true;
// -----------------------------------------------------------------------
// Flexible buttonhole binding
// -----------------------------------------------------------------------
const bindingData = [];
const bindingGeometry = new THREE.BoxGeometry(1, 1, 1);
const bindingMaterial = new THREE.MeshStandardMaterial({
color: 0x225b53,
roughness: 0.82,
metalness: 0,
flatShading: true
});
// Long sides of the slit.
for (const side of [-1, 1]) {
for (let i = 0; i < 9; i++) {
bindingData.push({
side,
axis: "x",
along: -0.46 + i * 0.115,
scale: new THREE.Vector3(0.085, 0.105, 0.105)
});
}
}
// Ends of the slit.
for (const side of [-1, 1]) {
for (let i = 0; i < 3; i++) {
bindingData.push({
side,
axis: "z",
along: -0.12 + i * 0.12,
scale: new THREE.Vector3(0.105, 0.105, 0.085)
});
}
}
const binding = new THREE.InstancedMesh(
bindingGeometry,
bindingMaterial,
bindingData.length
);
binding.castShadow = true;
binding.receiveShadow = true;
binding.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(binding);
function updateBinding(flex) {
bindingData.forEach((piece, index) => {
let x = 0;
let y = 0.102;
let z = 0;
if (piece.axis === "x") {
x = piece.side * (0.365 + flex * 0.052);
z = piece.along;
y += Math.cos(piece.along * 5.0) * flex * 0.025;
} else {
z = piece.side * (0.515 + flex * 0.061);
x = piece.along;
y += Math.cos(piece.along * 8.0) * flex * 0.032;
}
dummy.position.set(x, y, z);
dummy.scale.copy(piece.scale);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
binding.setMatrixAt(index, dummy.matrix);
});
binding.instanceMatrix.needsUpdate = true;
}
// -----------------------------------------------------------------------
// Voxel button
// -----------------------------------------------------------------------
const button = new THREE.Group();
scene.add(button);
const buttonPalette = [
0xe9b84b,
0xf2c75e,
0xdca43b,
0xf6d475
];
const buttonGeometry = new THREE.BoxGeometry(0.168, 0.15, 0.168);
for (let row = 0; row < 5; row++) {
for (let col = 0; col < 3; col++) {
const block = new THREE.Mesh(
buttonGeometry,
new THREE.MeshStandardMaterial({
color: buttonPalette[(row + col * 2) % buttonPalette.length],
roughness: 0.48,
metalness: 0.12,
flatShading: true
})
);
block.position.set(
(col - 1) * 0.175,
0,
(row - 2) * 0.175
);
block.castShadow = true;
block.receiveShadow = true;
button.add(block);
}
}
// Dark thread holes on the face of the button.
const holeMaterial = new THREE.MeshStandardMaterial({
color: 0x48351f,
roughness: 1
});
for (const x of [-0.105, 0.105]) {
const hole = new THREE.Mesh(
new THREE.BoxGeometry(0.068, 0.018, 0.068),
holeMaterial
);
hole.position.set(x, 0.082, 0);
button.add(hole);
}
// A small voxel shank underneath.
const shankMaterial = new THREE.MeshStandardMaterial({
color: 0xc68f2e,
roughness: 0.55,
metalness: 0.15
});
for (const x of [-0.065, 0.065]) {
for (const z of [-0.065, 0.065]) {
const shankBlock = new THREE.Mesh(
new THREE.BoxGeometry(0.105, 0.105, 0.105),
shankMaterial
);
shankBlock.position.set(x, -0.125, z);
shankBlock.castShadow = true;
button.add(shankBlock);
}
}
// -----------------------------------------------------------------------
// Sewing thread: voxel cubes forming two continuously attached strands
// -----------------------------------------------------------------------
const threadMaterial = new THREE.MeshStandardMaterial({
color: 0xdc4e72,
roughness: 0.75,
metalness: 0,
flatShading: true
});
const THREAD_POINTS_PER_STRAND = 36;
const thread = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.034, 0.034, 0.034),
threadMaterial,
THREAD_POINTS_PER_STRAND * 2
);
thread.castShadow = true;
thread.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(thread);
const threadColorA = new THREE.Color(0xdb496e);
const threadColorB = new THREE.Color(0xf06a89);
for (let i = 0; i < thread.count; i++) {
thread.setColorAt(
i,
i % 2 === 0 ? threadColorA : threadColorB
);
}
if (thread.instanceColor) thread.instanceColor.needsUpdate = true;
// Thread bridge across the two holes. It is parented to the button, so
// it remains physically attached during every transform.
for (let i = 0; i < 11; i++) {
const bridgeCube = new THREE.Mesh(
new THREE.BoxGeometry(0.026, 0.026, 0.026),
threadMaterial
);
bridgeCube.position.set(
-0.105 + (0.21 * i) / 10,
0.106,
0
);
bridgeCube.castShadow = true;
button.add(bridgeCube);
}
const fabricAnchors = [
new THREE.Vector3(-0.08, 0.135, -0.495),
new THREE.Vector3(0.08, 0.135, 0.495)
];
const buttonAnchors = [
new THREE.Vector3(-0.105, 0.108, 0),
new THREE.Vector3(0.105, 0.108, 0)
];
const upAxis = new THREE.Vector3(0, 1, 0);
const threadPoint = new THREE.Vector3();
const threadTangent = new THREE.Vector3();
function cubicBezier(out, a, b, c, d, t) {
const s = 1 - t;
out.set(
s * s * s * a.x +
3 * s * s * t * b.x +
3 * s * t * t * c.x +
t * t * t * d.x,
s * s * s * a.y +
3 * s * s * t * b.y +
3 * s * t * t * c.y +
t * t * t * d.y,
s * s * s * a.z +
3 * s * s * t * b.z +
3 * s * t * t * c.z +
t * t * t * d.z
);
return out;
}
function updateThread() {
let instanceIndex = 0;
for (let strand = 0; strand < 2; strand++) {
const a = fabricAnchors[strand];
const d = button.localToWorld(buttonAnchors[strand].clone());
const controlA = a.clone().add(new THREE.Vector3(0, 0.19, 0));
const controlB = d.clone().add(new THREE.Vector3(0, 0.22, 0));
for (let i = 0; i < THREAD_POINTS_PER_STRAND; i++) {
const t = i / (THREAD_POINTS_PER_STRAND - 1);
cubicBezier(
threadPoint,
a,
controlA,
controlB,
d,
t
);
const nextT = Math.min(1, t + 0.008);
const previousT = Math.max(0, t - 0.008);
cubicBezier(
threadTangent,
a,
controlA,
controlB,
d,
nextT
);
const previousPoint = new THREE.Vector3();
cubicBezier(
previousPoint,
a,
controlA,
controlB,
d,
previousT
);
threadTangent.sub(previousPoint).normalize();
dummy.position.copy(threadPoint);
dummy.quaternion.setFromUnitVectors(upAxis, threadTangent);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
thread.setMatrixAt(instanceIndex++, dummy.matrix);
}
}
thread.instanceMatrix.needsUpdate = true;
}
// -----------------------------------------------------------------------
// Animation tracks
// -----------------------------------------------------------------------
const duration = 4.8;
const tiltTrack = [
[0.00, 0.00],
[0.45, 0.00],
[1.20, 1.00],
[2.40, 1.00],
[3.30, 0.00],
[4.80, 0.00]
];
const heightTrack = [
[0.00, 0.17],
[0.45, 0.17],
[1.05, 0.16],
[1.62, -0.55],
[2.40, -0.55],
[2.85, -0.18],
[3.30, 0.17],
[4.80, 0.17]
];
const depthTrack = [
[0.00, 0.00],
[0.45, 0.00],
[1.20, 0.30],
[1.62, 0.18],
[2.40, 0.18],
[3.10, 0.02],
[3.30, 0.00],
[4.80, 0.00]
];
function sampleTrack(track, time) {
for (let i = 0; i < track.length - 1; i++) {
const [timeA, valueA] = track[i];
const [timeB, valueB] = track[i + 1];
if (time >= timeA && time <= timeB) {
const raw = (time - timeA) / (timeB - timeA);
const eased = raw * raw * (3 - 2 * raw);
return THREE.MathUtils.lerp(valueA, valueB, eased);
}
}
return track[track.length - 1][1];
}
function updateFabric(flex) {
fabricTiles.forEach((tile, index) => {
const absX = Math.abs(tile.x);
const absZ = Math.abs(tile.z);
const sideWeight =
Math.max(0, 1 - absX / 1.2) *
Math.max(0, 1 - absZ / 1.15);
const endWeight =
Math.max(0, 1 - absZ / 1.45) *
Math.max(0, 1 - absX / 1.1);
const xShift =
Math.sign(tile.x) *
flex *
0.065 *
sideWeight;
const zShift =
Math.sign(tile.z) *
flex *
0.045 *
endWeight;
dummy.position.set(
tile.x + xShift,
tile.y + flex * 0.024 * sideWeight,
tile.z + zShift
);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
fabric.setMatrixAt(index, dummy.matrix);
});
fabric.instanceMatrix.needsUpdate = true;
}
let motionTime = 0;
let paused = false;
let previousTime = performance.now();
function updateMotion() {
const tilt = sampleTrack(tiltTrack, motionTime);
const height = sampleTrack(heightTrack, motionTime);
const depth = sampleTrack(depthTrack, motionTime);
button.position.set(0, height, depth);
button.rotation.x = tilt;
button.updateMatrixWorld(true);
const tiltFlex = Math.sin(tilt) * 0.62;
const depthFlex =
THREE.MathUtils.clamp((0.17 - height) / 0.72, 0, 1) * 0.38;
const flex = Math.min(1, tiltFlex + depthFlex);
updateFabric(flex);
updateBinding(flex);
updateThread();
progressBar.style.width = `${(motionTime / duration) * 100}%`;
if (motionTime < 0.45) {
statusText.textContent = "FLAT / REST";
} else if (motionTime < 1.62) {
statusText.textContent = "EDGE ENTERING";
} else if (motionTime < 2.40) {
statusText.textContent = "THREAD MAINTAINED";
} else if (motionTime < 3.30) {
statusText.textContent = "RETURNING";
} else {
statusText.textContent = "FLAT / LOCKED";
}
}
// -----------------------------------------------------------------------
// Direct camera controls — no addon imports
// -----------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.azimuth -= deltaX * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar + deltaY * 0.005,
0.32,
1.28
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
4.8,
13.5
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.azimuth = 0.72;
cameraState.polar = 0.72;
cameraState.radius = 8.4;
updateCamera();
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
replayButton.addEventListener("click", () => {
motionTime = 0;
paused = false;
pauseButton.textContent = "Pause";
});
// -----------------------------------------------------------------------
// Resize and render loop
// -----------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
motionTime = (motionTime + delta) % duration;
}
updateMotion();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateFabric(0);
updateBinding(0);
updateMotion();
requestAnimationFrame(animate);
</script>
</body>
</html>
```