Prompt specification
Build or customize the Three.js 3D scene "Slippery sandwich balance" 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: Slippery sandwich balance
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/slippery-sandwich-balance-x0150
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this low-poly scene: Build two sandwich stacks with identical ingredients arranged differently; a gentle tilt causes only the explicitly low-friction layer stack to slide in a simplified contact demonstration.
Working Three.js Starter (slippery-sandwich-balance-x0150):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Low-Poly Friction Sandwich Lab</title>
<style>
:root {
--ink: #25342d;
--muted: #66736b;
--paper: #f5f0e5;
--panel: rgba(255, 252, 244, 0.88);
--line: rgba(37, 52, 45, 0.14);
--cyan: #24b8c4;
--cyan-dark: #087f8b;
--orange: #ef8354;
--yellow: #f1bf48;
--green: #70a85a;
--red: #db5947;
--brown: #805039;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#ui {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
background: var(--panel);
border: 1px solid rgba(255, 255, 255, 0.82);
box-shadow:
0 18px 45px rgba(61, 54, 40, 0.12),
0 2px 8px rgba(61, 54, 40, 0.05);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.intro {
position: absolute;
top: 28px;
left: 30px;
width: min(430px, calc(100vw - 390px));
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--cyan-dark);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 3px;
border-radius: 99px;
background: var(--cyan);
content: "";
}
h1 {
margin: 0;
max-width: 420px;
font-size: clamp(31px, 4vw, 57px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.intro p {
max-width: 390px;
margin: 15px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.controls {
position: absolute;
top: 28px;
right: 28px;
width: 292px;
padding: 17px;
border-radius: 18px;
pointer-events: auto;
}
.control-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#angleReadout {
color: var(--cyan-dark);
font-size: 20px;
letter-spacing: -0.04em;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 7px 0 14px;
appearance: none;
border-radius: 99px;
outline: none;
background: linear-gradient(
to right,
var(--cyan) 0%,
var(--cyan) var(--fill, 0%),
#d9ddd6 var(--fill, 0%),
#d9ddd6 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
appearance: none;
border: 3px solid white;
border-radius: 50%;
background: var(--cyan-dark);
box-shadow: 0 2px 7px rgba(0, 0, 0, 0.22);
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid white;
border-radius: 50%;
background: var(--cyan-dark);
box-shadow: 0 2px 7px rgba(0, 0, 0, 0.22);
cursor: pointer;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 38px;
border: 0;
border-radius: 10px;
background: var(--ink);
color: white;
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: transform 150ms ease, background 150ms ease;
}
button:hover {
transform: translateY(-1px);
background: #34483e;
}
button.secondary {
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.55);
color: var(--ink);
}
button.secondary:hover {
background: white;
}
.stack-label {
position: absolute;
z-index: 3;
min-width: 150px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.95);
border-radius: 12px;
background: rgba(255, 252, 244, 0.92);
box-shadow: 0 10px 24px rgba(47, 48, 39, 0.14);
transform: translate(-50%, -100%);
transition: opacity 120ms ease;
backdrop-filter: blur(8px);
}
.stack-label::after {
position: absolute;
bottom: -6px;
left: calc(50% - 6px);
width: 12px;
height: 12px;
transform: rotate(45deg);
border-right: 1px solid rgba(255, 255, 255, 0.95);
border-bottom: 1px solid rgba(255, 255, 255, 0.95);
background: rgba(255, 252, 244, 0.92);
content: "";
}
.stack-label.slip {
border-top: 4px solid var(--cyan);
}
.stack-label.grip {
border-top: 4px solid var(--orange);
}
.label-kicker {
margin-bottom: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.15em;
}
.stack-label strong {
display: block;
font-size: 14px;
letter-spacing: -0.02em;
}
.stack-label span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.bottom-grid {
position: absolute;
right: 28px;
bottom: 26px;
left: 28px;
display: grid;
grid-template-columns: minmax(430px, 1fr) 292px;
gap: 14px;
align-items: end;
}
.recipes {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 18px;
padding: 18px;
border-radius: 18px;
}
.recipe + .recipe {
padding-left: 18px;
border-left: 1px solid var(--line);
}
.recipe-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 11px;
}
.recipe-heading strong {
font-size: 13px;
}
.status {
padding: 4px 7px;
border-radius: 99px;
background: #e8ece6;
color: var(--muted);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
white-space: nowrap;
}
.status.moving {
background: rgba(36, 184, 196, 0.17);
color: var(--cyan-dark);
}
.order {
display: flex;
flex-wrap: wrap;
gap: 5px;
align-items: center;
}
.food {
display: inline-flex;
align-items: center;
gap: 5px;
padding: 5px 7px;
border: 1px solid var(--line);
border-radius: 7px;
background: rgba(255, 255, 255, 0.62);
color: #4a574f;
font-size: 9px;
font-weight: 750;
}
.food i {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--c);
}
.recipe-note {
margin: 10px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.recipe-note b {
color: var(--cyan-dark);
}
.model {
padding: 18px;
border-radius: 18px;
pointer-events: auto;
}
.model h2 {
margin: 0 0 12px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.formula {
margin-bottom: 12px;
padding: 9px 11px;
border-radius: 9px;
background: rgba(37, 52, 45, 0.055);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 12px;
}
.model-row {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: var(--muted);
font-size: 10px;
}
.model-row b {
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.hint {
position: absolute;
bottom: 8px;
left: 50%;
color: rgba(37, 52, 45, 0.48);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
transform: translateX(-50%);
white-space: nowrap;
}
@media (max-width: 900px) {
.intro {
width: calc(100vw - 350px);
}
.bottom-grid {
grid-template-columns: 1fr;
}
.model {
display: none;
}
.recipes {
width: 100%;
}
}
@media (max-width: 650px) {
.intro {
top: 18px;
left: 18px;
width: calc(100vw - 36px);
}
.intro p {
display: none;
}
h1 {
max-width: 250px;
font-size: 35px;
}
.controls {
top: 150px;
right: 18px;
width: 220px;
padding: 13px;
}
.bottom-grid {
right: 12px;
bottom: 12px;
left: 12px;
}
.recipes {
grid-template-columns: 1fr;
gap: 12px;
padding: 13px;
}
.recipe + .recipe {
padding-top: 12px;
padding-left: 0;
border-top: 1px solid var(--line);
border-left: 0;
}
.stack-label {
min-width: 118px;
padding: 7px 9px;
}
.stack-label span {
display: none;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly sandwich friction demonstration"></canvas>
<div id="ui">
<header class="intro">
<div class="eyebrow">Contact Lab · Experiment 04</div>
<h1>Same ingredients.<br />Different grip.</h1>
<p>
Both sandwiches use the same food layers in a different order.
Only Stack A has an explicitly low-friction contact layer.
</p>
</header>
<section class="controls panel" aria-label="Simulation controls">
<div class="control-title">
<span>Gentle board tilt</span>
<strong id="angleReadout">0.0°</strong>
</div>
<input
id="tilt"
type="range"
min="0"
max="8"
step="0.1"
value="7.5"
aria-label="Board tilt angle"
/>
<div class="button-row">
<button id="replay" type="button">Replay tilt</button>
<button id="pause" class="secondary" type="button">Pause</button>
</div>
</section>
<div id="labelA" class="stack-label slip">
<div class="label-kicker">Stack A</div>
<strong>Low friction</strong>
<span>μ 0.08 · cyan contact layer</span>
</div>
<div id="labelB" class="stack-label grip">
<div class="label-kicker">Stack B</div>
<strong>Standard grip</strong>
<span>μ 0.58 · direct board contact</span>
</div>
<div class="bottom-grid">
<section class="recipes panel">
<article class="recipe">
<div class="recipe-heading">
<strong>Stack A · slides</strong>
<span id="statusA" class="status">Tilt to test</span>
</div>
<div class="order" aria-label="Stack A ingredient order">
<span class="food"><i style="--c:#c9823f"></i>Bun</span>
<span class="food"><i style="--c:#805039"></i>Patty</span>
<span class="food"><i style="--c:#f1bf48"></i>Cheese</span>
<span class="food"><i style="--c:#db5947"></i>Tomato</span>
<span class="food"><i style="--c:#70a85a"></i>Lettuce</span>
<span class="food"><i style="--c:#c9823f"></i>Bun</span>
</div>
<p class="recipe-note">
<b>Explicit low-friction layer:</b> a thin cyan contact sheet
separates the bottom bun from the board.
</p>
</article>
<article class="recipe">
<div class="recipe-heading">
<strong>Stack B · locked</strong>
<span id="statusB" class="status">Locked</span>
</div>
<div class="order" aria-label="Stack B ingredient order">
<span class="food"><i style="--c:#c9823f"></i>Bun</span>
<span class="food"><i style="--c:#70a85a"></i>Lettuce</span>
<span class="food"><i style="--c:#db5947"></i>Tomato</span>
<span class="food"><i style="--c:#f1bf48"></i>Cheese</span>
<span class="food"><i style="--c:#805039"></i>Patty</span>
<span class="food"><i style="--c:#c9823f"></i>Bun</span>
</div>
<p class="recipe-note">
The same five ingredient types are rearranged, but the sandwich
contacts the board directly and does not overcome static friction.
</p>
</article>
</section>
<aside class="model panel">
<h2>Simplified contact model</h2>
<div class="formula">slide when tan(θ) > μ</div>
<div class="model-row">
<span>Current critical angle</span>
<b id="criticalAngle">0.0°</b>
</div>
<div class="model-row">
<span>Stack A threshold</span>
<b>4.6°</b>
</div>
<div class="model-row">
<span>Stack B threshold</span>
<b>30.1°</b>
</div>
<div class="model-row">
<span>Gravity</span>
<b>9.81 m/s²</b>
</div>
</aside>
</div>
<div class="hint">Drag to orbit · scroll to zoom</div>
</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
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf3eee4);
scene.fog = new THREE.Fog(0xf3eee4, 18, 35);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.85, 0);
let cameraYaw = 0.62;
let cameraPitch = 0.58;
let cameraDistance = 15.2;
function updateCamera() {
const horizontal = Math.cos(cameraPitch) * cameraDistance;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraDistance,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateCamera();
// Direct, dependency-free camera controls.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraYaw -= dx * 0.005;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.004,
0.24,
1.15
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance + event.deltaY * 0.008,
10,
21
);
updateCamera();
},
{ passive: false }
);
// ------------------------------------------------------------
// Lighting and low-poly environment
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xfffbef, 0x819080, 2.25);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d4, 3.4);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb8d9dc, 1.25);
fillLight.position.set(8, 5, -6);
scene.add(fillLight);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
flatShading: true,
...options
});
}
const groundMaterial = standardMaterial(0xe9e2d5);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.3;
ground.receiveShadow = true;
scene.add(ground);
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(7.3, 7.6, 0.28, 12),
standardMaterial(0xd9d0bf)
);
pedestal.position.y = -1.16;
pedestal.receiveShadow = true;
scene.add(pedestal);
const support = new THREE.Mesh(
new THREE.ConeGeometry(1.15, 1.65, 4),
standardMaterial(0xc2a678)
);
support.position.y = -0.42;
support.rotation.y = Math.PI / 4;
support.castShadow = true;
support.receiveShadow = true;
scene.add(support);
// ------------------------------------------------------------
// Tilting board
// ------------------------------------------------------------
const board = new THREE.Group();
board.position.y = 0.55;
scene.add(board);
const boardBase = new THREE.Mesh(
new THREE.BoxGeometry(10.6, 0.44, 6.2),
standardMaterial(0xb98550)
);
boardBase.position.y = -0.22;
boardBase.castShadow = true;
boardBase.receiveShadow = true;
board.add(boardBase);
const boardTop = new THREE.Mesh(
new THREE.BoxGeometry(10.2, 0.1, 5.8),
standardMaterial(0x74866d)
);
boardTop.position.y = 0.01;
boardTop.castShadow = true;
boardTop.receiveShadow = true;
board.add(boardTop);
const seamMaterial = new THREE.LineBasicMaterial({
color: 0x405344,
transparent: true,
opacity: 0.18
});
for (let z = -2.4; z <= 2.4; z += 0.8) {
const seamGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-4.95, 0.061, z),
new THREE.Vector3(4.95, 0.061, z)
]);
board.add(new THREE.Line(seamGeometry, seamMaterial));
}
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.16, 6.75, 10),
standardMaterial(0x4a5148, {
metalness: 0.35,
roughness: 0.48
})
);
axle.rotation.x = Math.PI / 2;
axle.position.y = 0.55;
axle.castShadow = true;
scene.add(axle);
for (const z of [-3.42, 3.42]) {
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(0.28, 0.28, 0.18, 10),
standardMaterial(0xd5a968, {
metalness: 0.45,
roughness: 0.4
})
);
cap.rotation.x = Math.PI / 2;
cap.position.set(0, 0.55, z);
cap.castShadow = true;
scene.add(cap);
}
// ------------------------------------------------------------
// Ingredient helpers
// ------------------------------------------------------------
const breadCrustMaterial = standardMaterial(0xc9823f);
const breadTopMaterial = standardMaterial(0xe6b362);
const breadCrumbMaterial = standardMaterial(0xf2d9a5);
const pattyMaterial = standardMaterial(0x754633);
const charMaterial = standardMaterial(0x3d2b24);
const cheeseMaterial = standardMaterial(0xf1bf48, {
roughness: 0.66
});
const tomatoMaterial = standardMaterial(0xdb5947, {
roughness: 0.62
});
const tomatoInsideMaterial = standardMaterial(0xf38b72, {
roughness: 0.8
});
const lettuceMaterial = standardMaterial(0x70a85a, {
roughness: 0.9,
side: THREE.DoubleSide
});
const seedMaterial = standardMaterial(0xf4dfb1);
const filmMaterial = standardMaterial(0x24b8c4, {
roughness: 0.18,
metalness: 0.05,
transparent: true,
opacity: 0.82,
emissive: 0x07545a,
emissiveIntensity: 0.14
});
let randomSeed = 48321;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function addBread(parent, y, isTop) {
const bread = new THREE.Group();
bread.position.y = y;
parent.add(bread);
const crust = new THREE.Mesh(
new THREE.BoxGeometry(3.24, 0.3, 2.14),
breadCrustMaterial
);
crust.castShadow = true;
crust.receiveShadow = true;
bread.add(crust);
const face = new THREE.Mesh(
new THREE.BoxGeometry(2.98, 0.075, 1.88),
isTop ? breadTopMaterial : breadCrumbMaterial
);
face.position.y = 0.15;
face.castShadow = true;
bread.add(face);
if (isTop) {
const sesameGeometry = new THREE.BoxGeometry(0.2, 0.035, 0.075);
for (let i = 0; i < 10; i++) {
const seed = new THREE.Mesh(sesameGeometry, seedMaterial);
seed.position.set(
(random() - 0.5) * 2.55,
0.205,
(random() - 0.5) * 1.35
);
seed.rotation.y = (random() - 0.5) * Math.PI;
seed.castShadow = true;
bread.add(seed);
}
}
}
function addPatty(parent, y) {
const geometry = new THREE.CylinderGeometry(1.48, 1.48, 0.24, 9);
geometry.scale(1, 1, 0.62);
const patty = new THREE.Mesh(geometry, pattyMaterial);
patty.position.y = y;
patty.rotation.y = 0.12;
patty.castShadow = true;
patty.receiveShadow = true;
parent.add(patty);
const fleckGeometry = new THREE.BoxGeometry(0.16, 0.018, 0.055);
for (let i = 0; i < 8; i++) {
const fleck = new THREE.Mesh(fleckGeometry, charMaterial);
const angle = random() * Math.PI * 2;
const radius = 0.35 + random() * 0.78;
fleck.position.set(
Math.cos(angle) * radius,
y + 0.126,
Math.sin(angle) * radius * 0.58
);
fleck.rotation.y = random() * Math.PI;
parent.add(fleck);
}
}
function addCheese(parent, y) {
const shape = new THREE.Shape();
shape.moveTo(-1.45, -0.72);
shape.lineTo(-1.02, -0.87);
shape.lineTo(-0.2, -0.75);
shape.lineTo(0.72, -0.89);
shape.lineTo(1.43, -0.7);
shape.lineTo(1.5, 0.68);
shape.lineTo(0.92, 0.8);
shape.lineTo(0.08, 0.88);
shape.lineTo(-0.9, 0.76);
shape.lineTo(-1.47, 0.64);
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.065,
bevelEnabled: false,
curveSegments: 1,
steps: 1
});
const cheese = new THREE.Mesh(geometry, cheeseMaterial);
cheese.rotation.x = -Math.PI / 2;
cheese.rotation.z = -0.045;
cheese.position.y = y;
cheese.castShadow = true;
cheese.receiveShadow = true;
parent.add(cheese);
}
function addTomato(parent, y) {
const sliceGeometry = new THREE.CylinderGeometry(
0.45,
0.45,
0.14,
10
);
const insideGeometry = new THREE.CircleGeometry(0.29, 10);
const positions = [
[-0.94, -0.42],
[0, -0.42],
[0.94, -0.42],
[-0.94, 0.42],
[0, 0.42],
[0.94, 0.42]
];
positions.forEach(([x, z], index) => {
const slice = new THREE.Mesh(sliceGeometry, tomatoMaterial);
slice.position.set(x, y, z);
slice.rotation.y = index * 0.41;
slice.castShadow = true;
parent.add(slice);
const inside = new THREE.Mesh(
insideGeometry,
tomatoInsideMaterial
);
inside.rotation.x = -Math.PI / 2;
inside.position.set(x, y + 0.073, z);
parent.add(inside);
});
}
function addLettuce(parent, y) {
const shape = new THREE.Shape();
const points = 22;
for (let i = 0; i <= points; i++) {
const angle = (i / points) * Math.PI * 2;
const radius = i % 2 === 0 ? 1 : 0.86;
const x = Math.cos(angle) * 1.52 * radius;
const z = Math.sin(angle) * 0.94 * radius;
if (i === 0) {
shape.moveTo(x, z);
} else {
shape.lineTo(x, z);
}
}
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.055,
bevelEnabled: false,
curveSegments: 1,
steps: 1
});
const lettuce = new THREE.Mesh(geometry, lettuceMaterial);
lettuce.rotation.x = -Math.PI / 2;
lettuce.rotation.z = 0.035;
lettuce.position.y = y;
lettuce.castShadow = true;
lettuce.receiveShadow = true;
parent.add(lettuce);
}
function createStack({ x, order, lowFriction }) {
const group = new THREE.Group();
group.position.set(x, 0.065, 0.08);
board.add(group);
if (lowFriction) {
const film = new THREE.Mesh(
new THREE.BoxGeometry(3.48, 0.035, 2.36),
filmMaterial
);
film.position.y = 0.0175;
film.castShadow = true;
film.receiveShadow = true;
group.add(film);
}
let cursor = lowFriction ? 0.035 : 0;
addBread(group, cursor + 0.15, false);
cursor += 0.3;
order.forEach((ingredient) => {
if (ingredient === "patty") {
addPatty(group, cursor + 0.11);
cursor += 0.24;
} else if (ingredient === "cheese") {
addCheese(group, cursor + 0.035);
cursor += 0.07;
} else if (ingredient === "tomato") {
addTomato(group, cursor + 0.07);
cursor += 0.14;
} else if (ingredient === "lettuce") {
addLettuce(group, cursor + 0.045);
cursor += 0.09;
}
});
addBread(group, cursor + 0.15, true);
const anchor = new THREE.Object3D();
anchor.position.set(0, 1.82, 0);
group.add(anchor);
return { group, anchor };
}
const A_INITIAL_X = -2.25;
const B_INITIAL_X = 2.25;
const stackA = createStack({
x: A_INITIAL_X,
order: ["patty", "cheese", "tomato", "lettuce"],
lowFriction: true
});
const stackB = createStack({
x: B_INITIAL_X,
order: ["lettuce", "tomato", "cheese", "patty"],
lowFriction: false
});
// ------------------------------------------------------------
// Board markings and slide trail
// ------------------------------------------------------------
function makeFootprint(x, color) {
const y = 0.072;
const halfWidth = 1.76;
const halfDepth = 1.2;
const points = [
new THREE.Vector3(x - halfWidth, y, 0.08 - halfDepth),
new THREE.Vector3(x + halfWidth, y, 0.08 - halfDepth),
new THREE.Vector3(x + halfWidth, y, 0.08 + halfDepth),
new THREE.Vector3(x - halfWidth, y, 0.08 + halfDepth),
new THREE.Vector3(x - halfWidth, y, 0.08 - halfDepth)
];
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineDashedMaterial({
color,
dashSize: 0.12,
gapSize: 0.08,
transparent: true,
opacity: 0.45
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
board.add(line);
}
makeFootprint(A_INITIAL_X, 0x24b8c4);
makeFootprint(B_INITIAL_X, 0xef8354);
const trailPositions = new Float32Array([
A_INITIAL_X, 0.085, 2.45,
A_INITIAL_X, 0.085, 2.45
]);
const trailGeometry = new THREE.BufferGeometry();
trailGeometry.setAttribute(
"position",
new THREE.BufferAttribute(trailPositions, 3)
);
const trailMaterial = new THREE.LineDashedMaterial({
color: 0x087f8b,
dashSize: 0.12,
gapSize: 0.08,
transparent: true,
opacity: 0.8
});
const trail = new THREE.Line(trailGeometry, trailMaterial);
board.add(trail);
const slideArrow = new THREE.ArrowHelper(
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(A_INITIAL_X, 0.09, 2.45),
1,
0x087f8b,
0.2,
0.13
);
slideArrow.visible = false;
slideArrow.line.material.transparent = true;
slideArrow.cone.material.transparent = true;
board.add(slideArrow);
// ------------------------------------------------------------
// Simplified contact physics
// ------------------------------------------------------------
const GRAVITY = 9.81;
const MU_A = 0.08;
const MU_B = 0.58;
const MAX_ANGLE = THREE.MathUtils.degToRad(8);
const ACCELERATION_SCALE = 0.48;
const MAX_SLIDE_SPEED = 0.56;
const MAX_A_SLIDE = 1.48;
let currentAngle = 0;
let targetAngle = THREE.MathUtils.degToRad(7.5);
let slideOffset = 0;
let slideVelocity = 0;
let paused = false;
function updatePhysics(delta) {
const smoothing = 1 - Math.exp(-delta * 1.2);
currentAngle += (targetAngle - currentAngle) * smoothing;
// Positive board angle raises the right side, so downhill is -X.
const downhillForce = -GRAVITY * Math.sin(currentAngle);
const normalForce = GRAVITY * Math.cos(currentAngle);
const staticLimit = MU_A * normalForce;
if (Math.abs(slideVelocity) < 0.001) {
if (Math.abs(downhillForce) <= staticLimit) {
slideVelocity = 0;
} else {
slideVelocity += downhillForce * ACCELERATION_SCALE * delta;
}
} else {
const frictionDirection = Math.sign(slideVelocity);
const frictionForce = frictionDirection * MU_A * normalForce;
const netForce = downhillForce - frictionForce;
slideVelocity += netForce * ACCELERATION_SCALE * delta;
}
// Coulomb-style stopping: do not let friction reverse the motion.
if (
slideVelocity !== 0 &&
Math.sign(slideVelocity) !== Math.sign(downhillForce) &&
Math.abs(downhillForce) < staticLimit
) {
slideVelocity = 0;
}
slideVelocity = THREE.MathUtils.clamp(
slideVelocity,
-MAX_SLIDE_SPEED,
MAX_SLIDE_SPEED
);
slideOffset += slideVelocity * delta;
if (slideOffset <= -MAX_A_SLIDE) {
slideOffset = -MAX_A_SLIDE;
slideVelocity = 0;
}
if (slideOffset >= 0) {
slideOffset = 0;
slideVelocity = Math.min(0, slideVelocity);
}
stackA.group.position.x = A_INITIAL_X + slideOffset;
stackB.group.position.x = B_INITIAL_X;
board.rotation.z = currentAngle;
// Update the motion trail.
trailPositions[3] = A_INITIAL_X + slideOffset;
trailGeometry.attributes.position.needsUpdate = true;
trail.computeLineDistances();
slideArrow.visible = slideOffset < -0.04;
if (slideArrow.visible) {
slideArrow.setLength(
Math.abs(slideOffset),
0.2,
0.13
);
slideArrow.line.material.opacity = 0.8;
slideArrow.cone.material.opacity = 0.8;
}
}
// ------------------------------------------------------------
// Controls and interface updates
// ------------------------------------------------------------
const tiltInput = document.querySelector("#tilt");
const angleReadout = document.querySelector("#angleReadout");
const criticalAngle = document.querySelector("#criticalAngle");
const statusA = document.querySelector("#statusA");
const statusB = document.querySelector("#statusB");
const replayButton = document.querySelector("#replay");
const pauseButton = document.querySelector("#pause");
function updateSliderFill() {
const percent = (Number(tiltInput.value) / 8) * 100;
tiltInput.style.setProperty("--fill", `${percent}%`);
}
updateSliderFill();
tiltInput.addEventListener("input", () => {
targetAngle = THREE.MathUtils.degToRad(Number(tiltInput.value));
updateSliderFill();
});
replayButton.addEventListener("click", () => {
currentAngle = 0;
slideOffset = 0;
slideVelocity = 0;
paused = false;
pauseButton.textContent = "Pause";
stackA.group.position.x = A_INITIAL_X;
});
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
function updateReadouts() {
const degrees = THREE.MathUtils.radToDeg(currentAngle);
const thresholdDegrees = THREE.MathUtils.radToDeg(
Math.atan(Math.tan(currentAngle))
);
angleReadout.textContent = `${degrees.toFixed(1)}°`;
criticalAngle.textContent = `${thresholdDegrees.toFixed(1)}°`;
const aMoving = Math.abs(slideVelocity) > 0.015;
const aCanSlide = Math.tan(currentAngle) > MU_A;
statusA.textContent = aMoving
? "Sliding"
: aCanSlide
? "Slip threshold"
: "Static";
statusA.classList.toggle("moving", aMoving);
statusB.textContent =
Math.tan(currentAngle) > MU_B ? "Ready to slip" : "Locked";
statusB.classList.remove("moving");
}
const projected = new THREE.Vector3();
function positionLabel(element, anchor) {
anchor.getWorldPosition(projected);
projected.project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
// ------------------------------------------------------------
// Resize and animation loop
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.04);
if (!paused) {
updatePhysics(delta);
}
updateReadouts();
positionLabel(document.querySelector("#labelA"), stackA.anchor);
positionLabel(document.querySelector("#labelB"), stackB.anchor);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```