Prompt specification
Build or customize the Three.js 3D scene "Kitchen pasta drying corner" 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: Kitchen pasta drying corner
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/kitchen-pasta-drying-corner-x0131
Scene Description Prompt:
Kitchen pasta drying corner. Compose a worktop with hand-cut ribbons, semolina tray, drying rods, and portion nests; show how hanging straight strands and coiling nests organize the same material differently. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (kitchen-pasta-drying-corner-x0131):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pasta Drying Corner</title>
<style>
:root {
color-scheme: light;
--ink: #29342f;
--paper: #f5ecdd;
--accent: #c97938;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9dfcf;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.intro {
position: fixed;
top: 28px;
left: 30px;
z-index: 5;
width: min(330px, calc(100vw - 60px));
pointer-events: none;
color: var(--ink);
text-shadow: 0 1px 12px rgba(255, 248, 235, 0.8);
}
.eyebrow {
margin-bottom: 9px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
opacity: 0.72;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(34px, 4vw, 56px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.04em;
}
.intro p {
max-width: 285px;
margin: 14px 0 0;
font-size: 12px;
line-height: 1.55;
color: rgba(41, 52, 47, 0.74);
}
.swatches {
position: fixed;
right: 26px;
bottom: 25px;
z-index: 5;
display: flex;
gap: 18px;
padding: 11px 15px;
border: 1px solid rgba(71, 63, 49, 0.13);
border-radius: 999px;
background: rgba(248, 240, 225, 0.82);
box-shadow: 0 10px 35px rgba(57, 44, 27, 0.1);
backdrop-filter: blur(10px);
color: #4a5149;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
.swatch {
display: flex;
align-items: center;
gap: 7px;
}
.swatch i {
width: 9px;
height: 9px;
border-radius: 50%;
background: #d69a4b;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.swatch.coil i {
background: #e5b86b;
}
.swatch.cut i {
background: #f0d19b;
}
.help {
position: fixed;
left: 28px;
bottom: 28px;
z-index: 5;
display: flex;
align-items: center;
gap: 10px;
color: rgba(45, 55, 49, 0.62);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
.help::before {
content: "";
width: 24px;
height: 1px;
background: currentColor;
}
#reset {
position: fixed;
right: 26px;
top: 26px;
z-index: 8;
border: 1px solid rgba(64, 66, 55, 0.18);
border-radius: 999px;
padding: 10px 16px;
background: rgba(248, 240, 225, 0.84);
color: #414a43;
font: 750 10px/1 Inter, ui-sans-serif, system-ui, sans-serif;
letter-spacing: 0.1em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(10px);
transition: transform 160ms ease, background 160ms ease;
}
#reset:hover {
transform: translateY(-2px);
background: #fff8eb;
}
.scene-label {
position: fixed;
z-index: 4;
display: flex;
align-items: center;
gap: 8px;
transform: translate(-50%, -100%);
padding: 7px 10px;
border: 1px solid rgba(68, 61, 45, 0.14);
border-radius: 7px;
background: rgba(250, 244, 231, 0.87);
box-shadow: 0 6px 18px rgba(61, 49, 31, 0.08);
color: #3c463f;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
backdrop-filter: blur(7px);
}
.scene-label::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: #d18a43;
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 22px;
background: linear-gradient(rgba(73, 70, 56, 0.45), transparent);
}
@media (max-width: 700px) {
.intro {
top: 20px;
left: 20px;
}
.intro p,
.swatches {
display: none;
}
.help {
left: 18px;
bottom: 18px;
font-size: 8px;
}
#reset {
top: 18px;
right: 18px;
}
.scene-label {
font-size: 8px;
}
}
</style>
</head>
<body>
<div class="intro">
<div class="eyebrow">Kitchen / Pasta drying corner</div>
<h1>One ribbon,<br>two rhythms.</h1>
<p>
Long cuts hang straight to dry. The same material curls into
measured, compact portion nests.
</p>
</div>
<button id="reset" type="button">Reset view</button>
<div class="help">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<div class="swatches">
<span class="swatch"><i></i>Hanging cut</span>
<span class="swatch coil"><i></i>Portion nest</span>
<span class="swatch cut"><i></i>Fresh ribbon</span>
</div>
<div id="label-hang" class="scene-label">Hang / straighten</div>
<div id="label-coil" class="scene-label">Coil / portion</div>
<div id="label-cut" class="scene-label">Cut / prepare</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9dfcf);
scene.fog = new THREE.Fog(0xe9dfcf, 17, 30);
const camera = new THREE.PerspectiveCamera(
39,
innerWidth / innerHeight,
0.1,
100
);
let renderer;
try {
renderer = new THREE.WebGLRenderer({ antialias: true });
} catch (error) {
document.body.innerHTML =
'<div style="padding:2rem;font:16px system-ui">' +
"This scene needs a browser with WebGL enabled.</div>";
throw error;
}
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional kitchen pasta drying corner"
);
const materials = {
wall: new THREE.MeshStandardMaterial({
color: 0xf0e7d6,
roughness: 0.95
}),
grout: new THREE.MeshStandardMaterial({
color: 0xd9cfbd,
roughness: 1
}),
floor: new THREE.MeshStandardMaterial({
color: 0xbba98d,
roughness: 0.9
}),
cabinet: new THREE.MeshStandardMaterial({
color: 0x52675d,
roughness: 0.78
}),
cabinetDark: new THREE.MeshStandardMaterial({
color: 0x405248,
roughness: 0.85
}),
wood: new THREE.MeshStandardMaterial({
color: 0xc58a55,
roughness: 0.72
}),
woodLight: new THREE.MeshStandardMaterial({
color: 0xe1b879,
roughness: 0.8
}),
woodDark: new THREE.MeshStandardMaterial({
color: 0x684936,
roughness: 0.75
}),
board: new THREE.MeshStandardMaterial({
color: 0x9b683f,
roughness: 0.88
}),
metal: new THREE.MeshStandardMaterial({
color: 0x777c76,
roughness: 0.36,
metalness: 0.72
}),
brass: new THREE.MeshStandardMaterial({
color: 0xc69b53,
roughness: 0.34,
metalness: 0.65
}),
semolina: new THREE.MeshStandardMaterial({
color: 0xead6a7,
roughness: 1
}),
flour: new THREE.MeshStandardMaterial({
color: 0xf4dfb3,
roughness: 1
}),
pasta: [
new THREE.MeshStandardMaterial({
color: 0xd99b4d,
roughness: 0.74,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0xe1ad60,
roughness: 0.78,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0xc98539,
roughness: 0.76,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0xe8bc75,
roughness: 0.8,
side: THREE.DoubleSide
})
]
};
function box(w, h, d, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(
radiusTop,
radiusBottom,
height,
material,
x,
y,
z,
parent = scene,
segments = 24
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function rod(length, radius, material, x, y, z, parent = scene) {
const mesh = cylinder(
radius,
radius,
length,
material,
x,
y,
z,
parent,
20
);
mesh.rotation.z = Math.PI / 2;
return mesh;
}
// ------------------------------------------------------------------
// Solid ribbon geometry
//
// Each ribbon is a closed strip with four vertices per sample:
// upper edge, lower edge, lower back, upper back.
// ------------------------------------------------------------------
function createRibbonGeometry(
points,
widths,
thickness = 0.025,
mode = "horizontal"
) {
const count = points.length;
const positions = [];
const indices = [];
const halfThickness = thickness * 0.5;
for (let i = 0; i < count; i++) {
const previous = points[Math.max(0, i - 1)];
const next = points[Math.min(count - 1, i + 1)];
const tangent = next.clone().sub(previous).normalize();
let widthDirection;
if (mode === "fixedX") {
widthDirection = new THREE.Vector3(1, 0, 0);
} else {
// The strip lies in the horizontal plane and turns around
// a vertical axis.
widthDirection = new THREE.Vector3(
-tangent.z,
0,
tangent.x
).normalize();
}
const normal = new THREE.Vector3()
.crossVectors(tangent, widthDirection)
.normalize();
const localWidth =
typeof widths === "number" ? widths : widths[i];
const halfWidth = localWidth * 0.5;
const center = points[i];
const right = center
.clone()
.addScaledVector(widthDirection, halfWidth);
const left = center
.clone()
.addScaledVector(widthDirection, -halfWidth);
positions.push(
right.x + normal.x * halfThickness,
right.y + normal.y * halfThickness,
right.z + normal.z * halfThickness,
left.x + normal.x * halfThickness,
left.y + normal.y * halfThickness,
left.z + normal.z * halfThickness,
left.x - normal.x * halfThickness,
left.y - normal.y * halfThickness,
left.z - normal.z * halfThickness,
right.x - normal.x * halfThickness,
right.y - normal.y * halfThickness,
right.z - normal.z * halfThickness
);
}
for (let i = 0; i < count - 1; i++) {
const a = i * 4;
const b = (i + 1) * 4;
// Broad face.
indices.push(
a + 1, b + 1, b,
a + 1, b, a
);
// Reverse broad face.
indices.push(
a + 2, a + 3, b + 3,
a + 2, b + 3, b + 2
);
// Right edge.
indices.push(
a + 3, b, b + 3,
a + 3, b + 3, a
);
// Left edge.
indices.push(
a + 1, a + 2, b + 2,
a + 1, b + 2, b + 1
);
}
const last = (count - 1) * 4;
indices.push(
0, 3, 2,
0, 2, 1,
last + 1, last + 2, last + 3
);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
function ribbonMesh(
points,
widths,
material,
thickness = 0.025,
mode = "horizontal"
) {
const mesh = new THREE.Mesh(
createRibbonGeometry(points, widths, thickness, mode),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function hangingRibbon(
x,
rodY,
rodZ,
bottomY,
width,
material,
phase = 0
) {
const points = [];
const widths = [];
const frontZ = rodZ + 0.28;
const backZ = rodZ - 0.28;
const backBottom = bottomY + 0.18;
// Front hanging leg, from its lower edge upward.
for (let i = 0; i <= 6; i++) {
const t = i / 6;
const y = THREE.MathUtils.lerp(bottomY, rodY - 0.12, t);
const z =
frontZ +
Math.sin(t * 5.2 + phase) * 0.035 * Math.sin(t * Math.PI);
points.push(new THREE.Vector3(x + Math.sin(phase) * 0.018, y, z));
widths.push(width * (0.94 + 0.08 * Math.sin(i * 1.7 + phase)));
}
// Folded section over the cylindrical rod.
points.push(
new THREE.Vector3(x, rodY - 0.01, frontZ - 0.06),
new THREE.Vector3(x, rodY + 0.095, frontZ - 0.16),
new THREE.Vector3(x, rodY + 0.105, rodZ),
new THREE.Vector3(x, rodY + 0.055, rodZ - 0.16),
new THREE.Vector3(x, rodY - 0.11, backZ)
);
for (let i = 0; i < 6; i++) {
widths.push(width * (0.96 + 0.06 * Math.cos(i + phase)));
}
// Back hanging leg.
for (let i = 1; i <= 6; i++) {
const t = i / 6;
const y = THREE.MathUtils.lerp(rodY - 0.11, backBottom, t);
const z = backZ + Math.sin(t * 4 + phase) * 0.025;
points.push(new THREE.Vector3(x, y, z));
widths.push(width * (0.93 + 0.08 * Math.sin(i * 1.4 + phase)));
}
return ribbonMesh(
points,
widths,
material,
0.027,
"fixedX"
);
}
function portionNest(
centerX,
centerZ,
baseY,
radius,
material,
phase = 0
) {
const points = [];
const widths = [];
const turns = 2.55;
const samples = 150;
for (let i = 0; i <= samples; i++) {
const t = i / samples;
const angle = phase + t * Math.PI * 2 * turns;
const r = 0.075 + t * (radius - 0.075);
const y =
baseY +
0.105 * (1 - t) +
0.018 * Math.sin(t * 22 + phase);
points.push(
new THREE.Vector3(
centerX + Math.cos(angle) * r,
y,
centerZ + Math.sin(angle) * r
)
);
widths.push(
radius * 0.205 * (1 + 0.08 * Math.sin(t * 19 + phase))
);
}
// A short visible loose end makes the nest read as cut ribbon.
const endAngle = phase + Math.PI * 2 * turns;
const endX = centerX + Math.cos(endAngle) * radius;
const endZ = centerZ + Math.sin(endAngle) * radius;
points.push(
new THREE.Vector3(
endX + Math.cos(endAngle + 0.5) * 0.24,
baseY + 0.01,
endZ + Math.sin(endAngle + 0.5) * 0.24
),
new THREE.Vector3(
endX + Math.cos(endAngle + 0.85) * 0.41,
baseY + 0.006,
endZ + Math.sin(endAngle + 0.85) * 0.41
)
);
widths.push(radius * 0.2, radius * 0.17);
return ribbonMesh(
points,
widths,
material,
0.035,
"horizontal"
);
}
// ------------------------------------------------------------------
// Kitchen shell
// ------------------------------------------------------------------
box(18, 0.18, 14, materials.floor, 0, -0.09, 0);
box(15, 7.2, 0.22, materials.wall, 0, 3.35, -3.15);
box(0.22, 7.2, 10, materials.wall, -6.85, 3.35, 0);
// Tile grout on the back wall.
for (let i = 0; i < 13; i++) {
const x = -6.2 + i * 1.02;
box(
0.018,
3.15,
0.016,
materials.grout,
x,
3.72,
-3.026
);
}
for (let i = 0; i < 7; i++) {
const y = 2.2 + i * 0.52;
box(
13.1,
0.018,
0.016,
materials.grout,
0,
y,
-3.025
);
}
// Cabinet body and worktop.
box(10.8, 1.43, 4.45, materials.cabinet, 0, 0.77, -0.03);
box(10.45, 0.13, 4.18, materials.cabinetDark, 0, 0.10, -0.03);
box(11.25, 0.22, 4.85, materials.wood, 0, 1.61, 0);
box(11.31, 0.055, 4.91, materials.woodLight, 0, 1.735, 0);
// Cabinet doors and brass pulls.
for (let i = 0; i < 4; i++) {
const x = -4.05 + i * 2.7;
box(2.47, 1.09, 0.06, materials.cabinet, x, 0.85, 2.225);
box(
0.46,
0.055,
0.08,
materials.brass,
x,
1.21,
2.29
);
}
// ------------------------------------------------------------------
// Semolina tray and portion nests
// ------------------------------------------------------------------
const tray = new THREE.Group();
tray.position.set(-3.0, 0, 0.22);
scene.add(tray);
box(3.85, 0.13, 2.25, materials.woodLight, 0, 1.81, 0, tray);
box(3.52, 0.085, 1.92, materials.semolina, 0, 1.90, 0, tray);
box(3.85, 0.22, 0.13, materials.wood, 0, 1.94, -1.06, tray);
box(3.85, 0.22, 0.13, materials.wood, 0, 1.94, 1.06, tray);
box(0.13, 0.22, 2.12, materials.wood, -1.86, 1.94, 0, tray);
box(0.13, 0.22, 2.12, materials.wood, 1.86, 1.94, 0, tray);
portionNest(-3.92, 0.02, 1.96, 0.56, materials.pasta[0], 0.1);
portionNest(-2.84, 0.40, 1.98, 0.61, materials.pasta[1], 1.2);
portionNest(-1.76, -0.10, 1.96, 0.55, materials.pasta[3], 2.4);
// ------------------------------------------------------------------
// Drying rack: two rods, uprights, and hanging straight ribbons
// ------------------------------------------------------------------
const rack = new THREE.Group();
scene.add(rack);
const rackX = 2.25;
const uprightLeft = -0.22;
const uprightRight = 4.78;
for (const x of [uprightLeft, uprightRight]) {
box(0.17, 3.43, 0.17, materials.woodDark, x, 3.47, -1.72, rack);
box(0.46, 0.08, 0.48, materials.metal, x, 1.80, -1.72, rack);
box(0.14, 0.14, 1.0, materials.metal, x, 4.98, -1.38, rack);
}
rod(5.13, 0.065, materials.brass, rackX, 4.82, -1.55, rack);
rod(5.13, 0.06, materials.metal, rackX, 3.97, -1.12, rack);
rod(1.02, 0.055, materials.metal, uprightLeft, 5.08, -1.38, rack);
rod(1.02, 0.055, materials.metal, uprightRight, 5.08, -1.38, rack);
const upperBottoms = [2.70, 2.94, 2.61, 3.05, 2.78, 2.88, 2.67, 3.00];
for (let i = 0; i < 8; i++) {
const x = 0.22 + i * 0.55;
hangingRibbon(
x,
4.82,
-1.55,
upperBottoms[i],
0.225 + (i % 3) * 0.018,
materials.pasta[i % materials.pasta.length],
i * 0.81
);
}
const lowerBottoms = [2.85, 3.02, 2.74, 2.91, 2.80, 3.05];
for (let i = 0; i < 6; i++) {
const x = 0.48 + i * 0.67;
hangingRibbon(
x,
3.97,
-1.12,
lowerBottoms[i],
0.22,
materials.pasta[(i + 1) % materials.pasta.length],
0.5 + i * 1.13
);
}
// ------------------------------------------------------------------
// Cutting board, fresh ribbons, and knife
// ------------------------------------------------------------------
box(
3.45,
0.12,
1.62,
materials.board,
2.78,
1.80,
0.70
);
for (let row = 0; row < 3; row++) {
const points = [];
const widths = [];
const zBase = 0.20 + row * 0.38;
for (let i = 0; i <= 28; i++) {
const t = i / 28;
const x = 1.24 + t * 3.06;
const z =
zBase +
Math.sin(t * Math.PI * 1.45 + row * 0.8) * 0.13;
points.push(
new THREE.Vector3(
x,
1.895 + row * 0.012 + Math.sin(t * 6 + row) * 0.008,
z
)
);
widths.push(
0.27 +
0.035 * Math.sin(t * 8 + row * 1.7) +
(i === 0 || i === 28 ? -0.025 : 0)
);
}
ribbonMesh(
points,
widths,
materials.pasta[(row + 1) % materials.pasta.length],
0.026,
"horizontal"
);
}
// Knife, using simple solid shapes.
const knife = new THREE.Group();
knife.position.set(3.42, 1.95, 1.24);
knife.rotation.y = -0.18;
scene.add(knife);
const blade = box(1.15, 0.035, 0.19, materials.metal, 0, 0, 0, knife);
blade.castShadow = true;
box(0.55, 0.12, 0.22, materials.woodDark, -0.82, 0.035, 0, knife);
// ------------------------------------------------------------------
// Small kitchen details
// ------------------------------------------------------------------
// Wall shelf and jars.
box(3.15, 0.13, 0.61, materials.wood, -4.53, 3.50, -2.77);
const jarColors = [0xb8c7b5, 0xd2b986, 0xbba795];
for (let i = 0; i < 3; i++) {
const x = -5.35 + i * 0.83;
const jarMaterial = new THREE.MeshStandardMaterial({
color: jarColors[i],
roughness: 0.4,
metalness: 0.05
});
cylinder(
0.25,
0.28,
0.69,
jarMaterial,
x,
3.91,
-2.68
);
cylinder(
0.27,
0.27,
0.08,
materials.woodDark,
x,
4.29,
-2.68
);
box(
0.31,
0.25,
0.025,
materials.woodLight,
x,
3.93,
-2.418
);
}
// Small utensil crock.
const crockMaterial = new THREE.MeshStandardMaterial({
color: 0xb86e4d,
roughness: 0.88
});
cylinder(
0.36,
0.29,
0.62,
crockMaterial,
5.05,
2.04,
0.65
);
cylinder(
0.25,
0.25,
0.02,
materials.woodDark,
5.05,
2.36,
0.65
);
for (let i = 0; i < 3; i++) {
const spoon = cylinder(
0.035,
0.035,
1.0,
materials.woodLight,
4.91 + i * 0.14,
2.64,
0.64,
scene,
12
);
spoon.rotation.z = (i - 1) * 0.12;
}
// ------------------------------------------------------------------
// Semolina and flour grains, made from small solid shapes
// ------------------------------------------------------------------
let randomSeed = 873;
function random() {
randomSeed =
(randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
const grainGeometry = new THREE.IcosahedronGeometry(0.025, 0);
const grainMaterial = new THREE.MeshStandardMaterial({
color: 0xf1dfb5,
roughness: 1
});
const trayGrains = new THREE.InstancedMesh(
grainGeometry,
grainMaterial,
150
);
trayGrains.castShadow = true;
trayGrains.receiveShadow = true;
const dummy = new THREE.Object3D();
for (let i = 0; i < 150; i++) {
dummy.position.set(
-4.66 + random() * 3.32,
1.949 + random() * 0.012,
-0.66 + random() * 1.76
);
dummy.scale.set(
0.45 + random() * 1.1,
0.35 + random() * 0.45,
0.45 + random() * 1.1
);
dummy.rotation.set(random() * 3, random() * 3, random() * 3);
dummy.updateMatrix();
trayGrains.setMatrixAt(i, dummy.matrix);
}
scene.add(trayGrains);
const boardGrains = new THREE.InstancedMesh(
grainGeometry,
grainMaterial,
80
);
boardGrains.castShadow = true;
for (let i = 0; i < 80; i++) {
dummy.position.set(
1.25 + random() * 3.05,
1.925,
0.05 + random() * 1.26
);
dummy.scale.set(
0.35 + random() * 0.75,
0.25 + random() * 0.35,
0.35 + random() * 0.75
);
dummy.rotation.set(random() * 3, random() * 3, random() * 3);
dummy.updateMatrix();
boardGrains.setMatrixAt(i, dummy.matrix);
}
scene.add(boardGrains);
// ------------------------------------------------------------------
// Lighting
// ------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xfff3dd,
0x746450,
2.0
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(
0xffedcf,
3.6
);
sun.position.set(-4.5, 9.5, 7.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -6;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 25;
sun.shadow.bias = -0.00025;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const warmFill = new THREE.PointLight(
0xffbf75,
12,
11,
2
);
warmFill.position.set(5.2, 5.2, 2.2);
scene.add(warmFill);
const coolFill = new THREE.DirectionalLight(
0xc5d7df,
0.75
);
coolFill.position.set(5, 4, -6);
scene.add(coolFill);
// ------------------------------------------------------------------
// Custom camera controls
// ------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0.1, 2.65, -0.1),
radius: 13.7,
theta: 0.66,
phi: 1.245
};
const initialControls = {
target: controls.target.clone(),
radius: controls.radius,
theta: controls.theta,
phi: controls.phi
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x +
controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z +
controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
function resetCamera() {
controls.target.copy(initialControls.target);
controls.radius = initialControls.radius;
controls.theta = initialControls.theta;
controls.phi = initialControls.phi;
updateCamera();
}
const canvas = renderer.domElement;
const pointers = new Map();
let previousPinchDistance = 0;
function pinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
previousPinchDistance = pinchDistance();
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (pointers.size === 1) {
if (event.shiftKey || event.buttons === 2) {
const panScale = controls.radius * 0.0009;
const rightX = Math.cos(controls.theta);
const rightZ = -Math.sin(controls.theta);
controls.target.x -= dx * panScale * rightX;
controls.target.z -= dx * panScale * rightZ;
controls.target.y += dy * panScale;
controls.target.y = THREE.MathUtils.clamp(
controls.target.y,
0.7,
5.0
);
} else {
controls.theta -= dx * 0.0052;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.0045,
0.52,
1.43
);
}
} else if (pointers.size >= 2) {
const distance = pinchDistance();
if (previousPinchDistance > 0 && distance > 0) {
controls.radius = THREE.MathUtils.clamp(
controls.radius *
(previousPinchDistance / distance),
8.2,
19.5
);
}
previousPinchDistance = distance;
}
updateCamera();
});
function removePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pinchDistance();
}
canvas.addEventListener("pointerup", removePointer);
canvas.addEventListener("pointercancel", removePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
8.2,
19.5
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("reset").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetCamera();
});
// ------------------------------------------------------------------
// Screen-space callout labels
// ------------------------------------------------------------------
const labels = [
{
element: document.getElementById("label-hang"),
position: new THREE.Vector3(3.05, 4.48, -1.1)
},
{
element: document.getElementById("label-coil"),
position: new THREE.Vector3(-3.02, 2.52, 0.28)
},
{
element: document.getElementById("label-cut"),
position: new THREE.Vector3(2.9, 2.13, 0.72)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of labels) {
projected.copy(label.position).project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.element.style.display = visible ? "flex" : "none";
if (visible) {
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * innerWidth}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
}
}
// ------------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateCamera();
renderer.setAnimationLoop(() => {
updateCamera();
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```