Prompt specification
Build or customize the Three.js 3D scene "Cutlery partition arrangement" 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: Cutlery partition arrangement
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/cutlery-partition-arrangement-x0018
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing modular drawer dividers. Place spoons, forks, and knives into a divided drawer; users slide one divider along rails while each utensil group stays inside its own compartment. Voxel style.
Working Three.js Starter (cutlery-partition-arrangement-x0018):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#e9e1d3" />
<title>Modular Drawer — Divider Lab</title>
<style>
:root {
--ink: #273038;
--muted: #6e746f;
--paper: rgba(255, 252, 245, 0.9);
--line: rgba(39, 48, 56, 0.13);
--coral: #ec6b52;
--teal: #54a6a1;
--gold: #d7a452;
--steel: #6f7e85;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e1d3;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
position: relative;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: absolute;
z-index: 10;
border: 1px solid var(--line);
background: var(--paper);
box-shadow: 0 14px 40px rgba(54, 45, 34, 0.12);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.title-panel {
top: 24px;
left: 24px;
width: min(370px, calc(100vw - 48px));
padding: 18px 20px 17px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 9px;
height: 9px;
content: "";
background: var(--coral);
box-shadow: 3px 3px 0 rgba(236, 107, 82, 0.2);
}
h1 {
margin: 0;
font-size: clamp(25px, 3.4vw, 38px);
line-height: 0.98;
letter-spacing: -0.055em;
text-transform: uppercase;
}
.subtitle {
max-width: 310px;
margin: 11px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.legend {
top: 24px;
right: 24px;
display: grid;
gap: 9px;
min-width: 178px;
padding: 14px 16px;
border-radius: 16px;
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.swatch {
width: 13px;
height: 13px;
border: 2px solid rgba(39, 48, 56, 0.18);
box-shadow: 2px 2px 0 rgba(39, 48, 56, 0.08);
}
.swatch.knives {
background: var(--steel);
}
.swatch.forks {
background: var(--teal);
}
.swatch.spoons {
background: var(--gold);
}
.control-panel {
bottom: 24px;
left: 24px;
width: min(430px, calc(100vw - 48px));
padding: 16px 18px 15px;
border-radius: 18px;
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 11px;
}
.control-heading label {
font-size: 10px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
#positionValue {
min-width: 44px;
padding: 4px 7px;
border-radius: 6px;
background: rgba(236, 107, 82, 0.12);
color: #b74331;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
font-weight: 800;
text-align: center;
}
.slider-wrap {
position: relative;
display: flex;
align-items: center;
}
input[type="range"] {
width: 100%;
height: 24px;
margin: 0;
appearance: none;
-webkit-appearance: none;
background: transparent;
cursor: ew-resize;
}
input[type="range"]::-webkit-slider-runnable-track {
height: 8px;
border-radius: 2px;
background:
linear-gradient(90deg, rgba(236, 107, 82, 0.22), rgba(236, 107, 82, 0.62));
box-shadow: inset 0 0 0 1px rgba(39, 48, 56, 0.08);
}
input[type="range"]::-moz-range-track {
height: 8px;
border-radius: 2px;
background:
linear-gradient(90deg, rgba(236, 107, 82, 0.22), rgba(236, 107, 82, 0.62));
box-shadow: inset 0 0 0 1px rgba(39, 48, 56, 0.08);
}
input[type="range"]::-webkit-slider-thumb {
width: 21px;
height: 21px;
margin-top: -6.5px;
appearance: none;
-webkit-appearance: none;
border: 3px solid #fff8ee;
border-radius: 3px;
background: var(--coral);
box-shadow: 0 3px 10px rgba(39, 48, 56, 0.24);
}
input[type="range"]::-moz-range-thumb {
width: 16px;
height: 16px;
border: 3px solid #fff8ee;
border-radius: 3px;
background: var(--coral);
box-shadow: 0 3px 10px rgba(39, 48, 56, 0.24);
}
input[type="range"]:focus-visible {
outline: 2px solid var(--coral);
outline-offset: 4px;
}
.control-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 8px;
}
#status {
min-height: 16px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.045em;
}
button {
border: 1px solid rgba(39, 48, 56, 0.15);
border-radius: 8px;
background: #fffaf1;
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
background: #fff;
transform: translateY(-1px);
}
button:active {
transform: translateY(1px);
}
#resetLayout {
padding: 7px 10px;
}
.hint {
position: absolute;
right: 24px;
bottom: 24px;
z-index: 8;
display: flex;
align-items: center;
gap: 10px;
max-width: 330px;
padding: 10px 13px;
border-radius: 10px;
background: rgba(39, 48, 56, 0.78);
color: #fff9ef;
font-size: 10px;
font-weight: 650;
line-height: 1.45;
pointer-events: none;
backdrop-filter: blur(8px);
}
.hint-icon {
display: grid;
flex: 0 0 auto;
width: 23px;
height: 23px;
place-items: center;
border: 1px solid rgba(255, 255, 255, 0.34);
border-radius: 5px;
font-size: 14px;
}
.camera-tools {
position: absolute;
top: 160px;
right: 24px;
z-index: 12;
display: flex;
gap: 6px;
}
.camera-tools button {
width: 34px;
height: 34px;
padding: 0;
background: rgba(255, 252, 245, 0.9);
box-shadow: 0 8px 20px rgba(54, 45, 34, 0.09);
font-size: 17px;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #e9e1d3;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
.title-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.subtitle {
display: none;
}
.legend {
top: 111px;
right: 12px;
min-width: 0;
padding: 10px 12px;
}
.legend-row {
font-size: 9px;
}
.control-panel {
bottom: 12px;
left: 12px;
width: calc(100vw - 24px);
}
.hint {
display: none;
}
.camera-tools {
top: 111px;
right: 12px;
flex-direction: column;
}
}
</style>
</head>
<body>
<div id="loading">Building the drawer…</div>
<section class="panel title-panel" aria-label="Project title">
<div class="eyebrow">Kitchen organization / 03</div>
<h1>Divider<br />Lab</h1>
<p class="subtitle">
Slide the coral divider along its twin rails. The cutlery groups automatically
rebalance while staying inside their own compartments.
</p>
</section>
<aside class="panel legend" aria-label="Cutlery color legend">
<div class="legend-row"><span class="swatch knives"></span> Knives</div>
<div class="legend-row"><span class="swatch forks"></span> Forks</div>
<div class="legend-row"><span class="swatch spoons"></span> Spoons</div>
</aside>
<div class="camera-tools" aria-label="Camera controls">
<button id="zoomIn" title="Zoom in" aria-label="Zoom in">+</button>
<button id="zoomOut" title="Zoom out" aria-label="Zoom out">−</button>
<button id="resetCamera" title="Reset camera" aria-label="Reset camera">⌂</button>
</div>
<section class="panel control-panel" aria-label="Divider control">
<div class="control-heading">
<label for="dividerRange">Slide the coral divider</label>
<output id="positionValue" for="dividerRange">45%</output>
</div>
<div class="slider-wrap">
<input
id="dividerRange"
type="range"
min="0"
max="100"
step="1"
value="45"
aria-label="Divider position"
/>
</div>
<div class="control-footer">
<div id="status" aria-live="polite">Rail locked · compartments balanced</div>
<button id="resetLayout">Reset layout</button>
</div>
</section>
<div class="hint">
<span class="hint-icon">↔</span>
Drag the coral divider or use the slider. Drag the background to orbit; scroll to zoom.
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const loading = document.querySelector("#loading");
const dividerRange = document.querySelector("#dividerRange");
const positionValue = document.querySelector("#positionValue");
const status = document.querySelector("#status");
const resetLayoutButton = document.querySelector("#resetLayout");
const zoomInButton = document.querySelector("#zoomIn");
const zoomOutButton = document.querySelector("#zoomOut");
const resetCameraButton = document.querySelector("#resetCamera");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e1d3);
scene.fog = new THREE.Fog(0xe9e1d3, 18, 32);
const camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
document.body.prepend(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel-style cutlery drawer. Drag the coral divider to slide it."
);
renderer.domElement.tabIndex = 0;
const hemiLight = new THREE.HemisphereLight(0xfff5df, 0x8b7866, 2.1);
scene.add(hemiLight);
const keyLight = new THREE.DirectionalLight(0xffedca, 3.2);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.0004;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x9fc9db, 1.15);
fillLight.position.set(7, 5, -5);
scene.add(fillLight);
const materials = {
ground: new THREE.MeshStandardMaterial({
color: 0xe9e1d3,
roughness: 1
}),
wood: new THREE.MeshStandardMaterial({
color: 0xc8925d,
roughness: 0.83
}),
woodLight: new THREE.MeshStandardMaterial({
color: 0xe0b77e,
roughness: 0.88
}),
woodDark: new THREE.MeshStandardMaterial({
color: 0xa66f43,
roughness: 0.86
}),
wall: new THREE.MeshStandardMaterial({
color: 0xdaa86f,
roughness: 0.88
}),
rail: new THREE.MeshStandardMaterial({
color: 0x59666c,
roughness: 0.42,
metalness: 0.5
}),
railGroove: new THREE.MeshStandardMaterial({
color: 0x3c464b,
roughness: 0.72,
metalness: 0.18
}),
fixedDivider: new THREE.MeshStandardMaterial({
color: 0x87918b,
roughness: 0.62
}),
movingDivider: new THREE.MeshStandardMaterial({
color: 0xec6b52,
roughness: 0.56,
emissive: 0x58170e,
emissiveIntensity: 0
}),
movingCap: new THREE.MeshStandardMaterial({
color: 0xf48a70,
roughness: 0.5,
emissive: 0x58170e,
emissiveIntensity: 0
}),
handle: new THREE.MeshStandardMaterial({
color: 0x39464d,
roughness: 0.62
}),
steel: new THREE.MeshStandardMaterial({
color: 0xb9c2c3,
roughness: 0.34,
metalness: 0.48
}),
darkSteel: new THREE.MeshStandardMaterial({
color: 0x536168,
roughness: 0.45,
metalness: 0.35
}),
teal: new THREE.MeshStandardMaterial({
color: 0x54a6a1,
roughness: 0.42,
metalness: 0.22
}),
gold: new THREE.MeshStandardMaterial({
color: 0xd7a452,
roughness: 0.42,
metalness: 0.3
}),
knifeHandle: new THREE.MeshStandardMaterial({
color: 0x35434b,
roughness: 0.72
})
};
function makeBox(width, height, depth, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const ground = makeBox(
42,
0.2,
42,
materials.ground,
0,
-0.34,
0
);
ground.castShadow = false;
// Drawer carcass and open interior.
makeBox(8.35, 0.34, 5.35, materials.woodDark, 0, 0.03, 0);
makeBox(7.95, 0.16, 4.98, materials.woodLight, 0, 0.28, 0);
makeBox(0.34, 0.82, 5.35, materials.wall, -4.02, 0.55, 0);
makeBox(0.34, 0.82, 5.35, materials.wall, 4.02, 0.55, 0);
makeBox(8.35, 0.82, 0.34, materials.wall, 0, 0.55, -2.52);
makeBox(8.35, 0.46, 0.34, materials.woodDark, 0, 0.39, 2.52);
// Blocky corner details.
makeBox(0.48, 0.17, 0.48, materials.woodLight, -4.02, 0.98, -2.52);
makeBox(0.48, 0.17, 0.48, materials.woodLight, 4.02, 0.98, -2.52);
makeBox(0.48, 0.17, 0.48, materials.woodLight, -4.02, 0.79, 2.52);
makeBox(0.48, 0.17, 0.48, materials.woodLight, 4.02, 0.79, 2.52);
// Exterior runners.
makeBox(0.4, 0.24, 4.8, materials.railGroove, -4.38, -0.08, 0);
makeBox(0.4, 0.24, 4.8, materials.railGroove, 4.38, -0.08, 0);
// Twin rails running along the direction in which the divider slides.
for (const z of [-1.91, 1.91]) {
makeBox(7.72, 0.055, 0.19, materials.railGroove, 0, 0.385, z);
makeBox(7.56, 0.06, 0.09, materials.rail, 0, 0.425, z);
}
function createDivider(isMoving) {
const group = new THREE.Group();
scene.add(group);
const bodyMaterial = isMoving
? materials.movingDivider
: materials.fixedDivider;
const capMaterial = isMoving
? materials.movingCap
: materials.rail;
const body = makeBox(
0.16,
0.84,
4.13,
bodyMaterial,
0,
0.76,
0,
group
);
body.userData.isMovingDivider = isMoving;
makeBox(
0.25,
0.1,
4.15,
capMaterial,
0,
1.22,
0,
group
);
// Rail shoes at the ends of each divider.
for (const z of [-1.91, 1.91]) {
const shoe = makeBox(
0.38,
0.2,
0.34,
isMoving ? materials.movingCap : materials.rail,
0,
0.48,
z,
group
);
shoe.userData.isMovingDivider = isMoving;
}
if (isMoving) {
const handle = makeBox(
0.44,
0.2,
0.68,
materials.handle,
0,
1.39,
0.22,
group
);
handle.userData.isMovingDivider = true;
for (const z of [0.03, 0.22, 0.41]) {
makeBox(
0.46,
0.055,
0.045,
materials.movingCap,
0,
1.515,
z,
group
);
}
} else {
makeBox(
0.3,
0.11,
0.46,
materials.handle,
0,
1.34,
0.18,
group
);
}
group.traverse((child) => {
if (child.isMesh) {
child.userData.isMovingDivider = isMoving;
}
});
return group;
}
const fixedDivider = createDivider(false);
fixedDivider.position.x = -1.0;
const movingDivider = createDivider(true);
const innerLeft = -3.82;
const innerRight = 3.82;
const fixedX = -1.0;
const minDividerX = 0.05;
const maxDividerX = 2.5;
const startDividerX = 1.1;
movingDivider.position.x = startDividerX;
function createKnife() {
const group = new THREE.Group();
makeBox(0.19, 0.12, 0.76, materials.knifeHandle, 0, 0, -0.52, group);
makeBox(0.23, 0.1, 0.16, materials.darkSteel, 0, 0, -0.1, group);
makeBox(0.31, 0.09, 0.56, materials.steel, 0, 0, 0.24, group);
makeBox(0.22, 0.09, 0.2, materials.steel, 0, 0, 0.6, group);
makeBox(0.11, 0.09, 0.09, materials.steel, 0, 0, 0.73, group);
return group;
}
function createFork() {
const group = new THREE.Group();
makeBox(0.13, 0.09, 1.08, materials.teal, 0, 0, -0.43, group);
makeBox(0.25, 0.09, 0.18, materials.teal, 0, 0, 0.17, group);
makeBox(0.39, 0.09, 0.16, materials.teal, 0, 0, 0.31, group);
for (const x of [-0.135, -0.045, 0.045, 0.135]) {
makeBox(0.058, 0.09, 0.39, materials.teal, x, 0, 0.52, group);
}
return group;
}
function createSpoon() {
const group = new THREE.Group();
makeBox(0.14, 0.09, 1.06, materials.gold, 0, 0, -0.43, group);
makeBox(0.23, 0.09, 0.2, materials.gold, 0, 0, 0.17, group);
makeBox(0.34, 0.085, 0.31, materials.gold, 0, 0, 0.39, group);
makeBox(0.27, 0.09, 0.24, materials.gold, 0, 0.025, 0.56, group);
makeBox(0.17, 0.095, 0.13, materials.gold, 0, 0.05, 0.68, group);
return group;
}
function createGroup(factory, offsets) {
const group = new THREE.Group();
offsets.forEach((offset, index) => {
const utensil = factory();
utensil.position.x = offset.x;
utensil.position.z = offset.z;
utensil.rotation.y = offset.rotation || 0;
group.add(utensil);
});
scene.add(group);
return group;
}
const knifeGroup = createGroup(createKnife, [
{ x: -0.29, z: -0.06, rotation: -0.018 },
{ x: 0, z: 0.06, rotation: 0.012 },
{ x: 0.29, z: -0.02, rotation: 0.02 }
]);
const forkGroup = createGroup(createFork, [
{ x: -0.25, z: 0.04, rotation: 0.012 },
{ x: 0, z: -0.04, rotation: -0.01 },
{ x: 0.25, z: 0.06, rotation: 0.018 }
]);
const spoonGroup = createGroup(createSpoon, [
{ x: -0.27, z: -0.03, rotation: -0.015 },
{ x: 0, z: 0.05, rotation: 0.012 },
{ x: 0.27, z: -0.04, rotation: 0.016 }
]);
knifeGroup.position.set((innerLeft + fixedX) * 0.5, 0.43, 0);
forkGroup.position.set((fixedX + startDividerX) * 0.5, 0.43, 0);
spoonGroup.position.set((startDividerX + innerRight) * 0.5, 0.43, 0);
// A small generated label on the front of the drawer.
function createDrawerLabel() {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#354149";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#f4e6cd";
ctx.fillRect(26, 26, 28, 204);
ctx.fillRect(70, 26, 10, 204);
ctx.fillStyle = "#fff7e8";
ctx.font = "900 86px monospace";
ctx.textBaseline = "middle";
ctx.fillText("MODULAR / 03", 122, 112);
ctx.fillStyle = "#ec6b52";
ctx.font = "700 38px monospace";
ctx.fillText("RAIL SYSTEM", 125, 190);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const labelMaterial = new THREE.MeshBasicMaterial({
map: texture,
toneMapped: false
});
const label = new THREE.Mesh(
new THREE.PlaneGeometry(2.75, 0.69),
labelMaterial
);
label.position.set(-0.05, 0.39, 2.701);
scene.add(label);
}
createDrawerLabel();
// Small color tabs on the drawer front.
const tabDefinitions = [
{ x: (innerLeft + fixedX) * 0.5, color: 0x6f7e85 },
{ x: (fixedX + startDividerX) * 0.5, color: 0x54a6a1 },
{ x: (startDividerX + innerRight) * 0.5, color: 0xd7a452 }
];
for (const tab of tabDefinitions) {
makeBox(
0.62,
0.11,
0.045,
new THREE.MeshStandardMaterial({ color: tab.color, roughness: 0.65 }),
tab.x,
0.4,
2.708
);
}
// Custom camera controller: orbit, zoom, and reset without addons.
const cameraTarget = new THREE.Vector3(0, 0.36, 0);
const defaultCamera = {
azimuth: 0.66,
polar: 0.72,
radius: 12.2
};
let cameraAzimuth = defaultCamera.azimuth;
let cameraPolar = defaultCamera.polar;
let cameraRadius = defaultCamera.radius;
let cameraAzimuthGoal = cameraAzimuth;
let cameraPolarGoal = cameraPolar;
let cameraRadiusGoal = cameraRadius;
function updateCamera() {
const sinPolar = Math.sin(cameraPolar);
camera.position.set(
cameraTarget.x + cameraRadius * sinPolar * Math.sin(cameraAzimuth),
cameraTarget.y + cameraRadius * Math.cos(cameraPolar),
cameraTarget.z + cameraRadius * sinPolar * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// Divider and containment state.
let dividerX = startDividerX;
let targetDividerX = startDividerX;
let isHoveringDivider = false;
let isDraggingDivider = false;
let dragOffset = 0;
let pointerMode = null;
let previousPointerX = 0;
let previousPointerY = 0;
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -1.25);
const dragPoint = new THREE.Vector3();
function rangeToX(value) {
return minDividerX + ((maxDividerX - minDividerX) * value) / 100;
}
function xToRange(x) {
return Math.round(
((x - minDividerX) / (maxDividerX - minDividerX)) * 100
);
}
function setTargetDivider(x) {
targetDividerX = THREE.MathUtils.clamp(x, minDividerX, maxDividerX);
const value = xToRange(targetDividerX);
dividerRange.value = String(value);
positionValue.textContent = `${value}%`;
}
function setPointerFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function dividerHit(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObject(movingDivider, true).length > 0;
}
function damp(current, target, speed, delta) {
return THREE.MathUtils.lerp(current, target, 1 - Math.exp(-speed * delta));
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.focus({ preventScroll: true });
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObject(movingDivider, true)[0];
previousPointerX = event.clientX;
previousPointerY = event.clientY;
if (event.button === 0 && hit) {
pointerMode = "divider";
isDraggingDivider = true;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
dragOffset = targetDividerX - dragPoint.x;
} else {
dragOffset = 0;
}
status.textContent = "Divider sliding · groups remain contained";
} else {
pointerMode = "orbit";
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerMode === "divider") {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
setTargetDivider(dragPoint.x + dragOffset);
}
return;
}
if (pointerMode === "orbit") {
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
cameraAzimuthGoal -= deltaX * 0.006;
cameraPolarGoal = THREE.MathUtils.clamp(
cameraPolarGoal + deltaY * 0.005,
0.3,
1.34
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
return;
}
isHoveringDivider = dividerHit(event);
renderer.domElement.style.cursor = isHoveringDivider ? "grab" : "default";
});
function endPointer(event) {
if (pointerMode === "divider") {
status.textContent = "Rail locked · compartments balanced";
}
pointerMode = null;
isDraggingDivider = false;
renderer.domElement.style.cursor = isHoveringDivider ? "grab" : "default";
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("pointerleave", () => {
if (!pointerMode) {
isHoveringDivider = false;
renderer.domElement.style.cursor = "default";
}
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadiusGoal = THREE.MathUtils.clamp(
cameraRadiusGoal + event.deltaY * 0.008,
8.2,
18
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
dividerRange.addEventListener("input", () => {
setTargetDivider(rangeToX(Number(dividerRange.value)));
status.textContent = "Divider sliding · groups remain contained";
});
dividerRange.addEventListener("change", () => {
status.textContent = "Rail locked · compartments balanced";
});
resetLayoutButton.addEventListener("click", () => {
setTargetDivider(startDividerX);
status.textContent = "Layout reset · compartments balanced";
});
zoomInButton.addEventListener("click", () => {
cameraRadiusGoal = Math.max(8.2, cameraRadiusGoal * 0.9);
});
zoomOutButton.addEventListener("click", () => {
cameraRadiusGoal = Math.min(18, cameraRadiusGoal * 1.1);
});
resetCameraButton.addEventListener("click", () => {
cameraAzimuthGoal = defaultCamera.azimuth;
cameraPolarGoal = defaultCamera.polar;
cameraRadiusGoal = defaultCamera.radius;
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
let firstFrame = true;
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
dividerX = damp(dividerX, targetDividerX, 11, delta);
movingDivider.position.x = dividerX;
// Each group is re-centered between its compartment boundaries.
// Because the divider has a bounded travel range, the clusters never
// cross a divider or leave the drawer.
const forkTargetX = (fixedX + dividerX) * 0.5;
const spoonTargetX = (dividerX + innerRight) * 0.5;
forkGroup.position.x = damp(forkGroup.position.x, forkTargetX, 9, delta);
spoonGroup.position.x = damp(spoonGroup.position.x, spoonTargetX, 9, delta);
cameraAzimuth = damp(cameraAzimuth, cameraAzimuthGoal, 8, delta);
cameraPolar = damp(cameraPolar, cameraPolarGoal, 8, delta);
cameraRadius = damp(cameraRadius, cameraRadiusGoal, 8, delta);
updateCamera();
const highlight = isDraggingDivider ? 0.3 : isHoveringDivider ? 0.17 : 0;
materials.movingDivider.emissiveIntensity = damp(
materials.movingDivider.emissiveIntensity,
highlight,
12,
delta
);
materials.movingCap.emissiveIntensity = materials.movingDivider.emissiveIntensity;
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 350);
}
});
</script>
</body>
</html>
```