Prompt specification
Build or customize the Three.js 3D scene "Foldable grocery tote pouch" 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: Foldable grocery tote pouch
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/foldable-grocery-tote-pouch-x0057
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Build a reusable tote with broad handles, side gussets, and attached storage pouch; display a packed form beside the unfolded bag for clear size comparison.
Working Three.js Starter (foldable-grocery-tote-pouch-x0057):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Tote — Two Carry States</title>
<style>
:root {
--ink: #183c38;
--muted: #60716c;
--paper: #f3eee3;
--teal: #257a70;
--coral: #e87861;
--gold: #d7a94b;
}
* {
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;
}
button {
font: inherit;
}
#app,
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
.intro {
position: fixed;
z-index: 10;
top: 30px;
left: 34px;
max-width: 390px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 11px;
color: var(--teal);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(31px, 4vw, 55px);
font-weight: 780;
letter-spacing: -0.055em;
line-height: 0.98;
}
.intro-copy {
max-width: 330px;
margin: 16px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.summary {
position: fixed;
z-index: 10;
left: 34px;
bottom: 29px;
display: grid;
grid-template-columns: auto auto;
gap: 4px 16px;
padding: 14px 17px;
border: 1px solid rgba(24, 60, 56, 0.13);
border-radius: 12px;
background: rgba(248, 244, 235, 0.83);
box-shadow: 0 10px 30px rgba(54, 54, 42, 0.08);
backdrop-filter: blur(12px);
pointer-events: none;
}
.summary small {
grid-column: 1 / -1;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.summary strong {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
letter-spacing: -0.03em;
}
.summary strong:last-of-type {
color: var(--teal);
}
.summary em {
align-self: center;
color: var(--coral);
font-size: 17px;
font-style: normal;
}
.controls {
position: fixed;
z-index: 20;
right: 28px;
bottom: 28px;
display: flex;
align-items: center;
gap: 8px;
}
.controls button {
min-height: 38px;
padding: 0 14px;
border: 1px solid rgba(24, 60, 56, 0.18);
border-radius: 9px;
background: rgba(248, 244, 235, 0.9);
color: var(--ink);
cursor: pointer;
font-size: 12px;
font-weight: 700;
box-shadow: 0 5px 18px rgba(54, 54, 42, 0.07);
backdrop-filter: blur(10px);
transition:
background 160ms ease,
color 160ms ease,
transform 160ms ease;
}
.controls button:hover {
background: var(--ink);
color: #fffaf0;
transform: translateY(-1px);
}
.controls button.active {
border-color: var(--teal);
background: var(--teal);
color: white;
}
.control-note {
position: fixed;
z-index: 10;
right: 31px;
bottom: 78px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
#annotations {
position: fixed;
z-index: 7;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.scene-label {
position: absolute;
min-width: 150px;
padding: 10px 12px 9px;
transform: translate(-50%, -100%);
border: 1px solid rgba(24, 60, 56, 0.15);
border-radius: 9px;
background: rgba(248, 244, 235, 0.92);
box-shadow: 0 8px 24px rgba(54, 54, 42, 0.1);
backdrop-filter: blur(10px);
white-space: nowrap;
transition: opacity 120ms linear;
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -6px;
width: 10px;
height: 10px;
transform: translateX(-50%) rotate(45deg);
border-right: 1px solid rgba(24, 60, 56, 0.15);
border-bottom: 1px solid rgba(24, 60, 56, 0.15);
background: rgba(248, 244, 235, 0.92);
}
.scene-label .label-title {
display: block;
margin-bottom: 4px;
color: var(--teal);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.scene-label .label-size {
display: block;
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
font-weight: 800;
letter-spacing: -0.025em;
}
.scene-label.packed .label-title {
color: var(--coral);
}
.floor-label {
position: absolute;
transform: translate(-50%, -50%);
padding: 4px 7px;
border-radius: 5px;
background: rgba(24, 60, 56, 0.84);
color: #fffaf0;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
transition: opacity 120ms linear;
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: var(--paper);
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loader {
display: flex;
align-items: center;
gap: 11px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.loader::before {
content: "";
width: 13px;
height: 13px;
border: 3px solid rgba(37, 122, 112, 0.18);
border-top-color: var(--teal);
border-radius: 50%;
animation: spin 800ms linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@media (max-width: 760px) {
.intro {
top: 20px;
left: 20px;
max-width: 280px;
}
.intro-copy {
display: none;
}
h1 {
font-size: 34px;
}
.summary {
left: 16px;
bottom: 68px;
padding: 11px 13px;
}
.controls {
right: 16px;
bottom: 18px;
}
.control-note {
display: none;
}
.scene-label {
min-width: 124px;
padding: 8px 9px;
}
.scene-label .label-size {
font-size: 10px;
}
}
</style>
</head>
<body>
<main id="app">
<div id="viewport"></div>
<header class="intro">
<p class="eyebrow">Voxel object study / 01</p>
<h1>One tote.<br />Two carry states.</h1>
<p class="intro-copy">
A full-size reusable carry system shown open, then compressed into a
compact block for easy comparison.
</p>
</header>
<aside class="summary">
<small>Approximate packed volume reduction</small>
<strong>54 × 68 × 22 cm</strong>
<em>→</em>
<strong>28 × 24 × 12 cm</strong>
</aside>
<div id="annotations">
<div id="bagLabel" class="scene-label">
<span class="label-title">Unfolded carry</span>
<span class="label-size">54 × 68 × 22 cm</span>
</div>
<div id="packedLabel" class="scene-label packed">
<span class="label-title">Packed form</span>
<span class="label-size">28 × 24 × 12 cm</span>
</div>
<div id="bagWidth" class="floor-label">54 CM WIDE</div>
<div id="packedWidth" class="floor-label">28 CM WIDE</div>
</div>
<div class="control-note">Drag to rotate · Scroll to zoom</div>
<nav class="controls" aria-label="Scene controls">
<button id="resetButton" type="button">Reset view</button>
<button id="orbitButton" type="button">Auto orbit</button>
</nav>
<div id="loading">
<div class="loader">Building voxel scene</div>
</div>
</main>
<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 viewport = document.querySelector("#viewport");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
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;
viewport.appendChild(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel scene comparing an unfolded tote with its packed form"
);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee8dc);
scene.fog = new THREE.Fog(0xeee8dc, 24, 48);
const camera = new THREE.PerspectiveCamera(
37,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 3.45, 0);
const hemisphere = new THREE.HemisphereLight(0xfff9e9, 0x77796f, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff4dc, 4.1);
keyLight.position.set(-7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 14;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.normalBias = 0.035;
keyLight.shadow.bias = -0.0002;
keyLight.target.position.set(0, 2.6, 0);
scene.add(keyLight, keyLight.target);
const fillLight = new THREE.DirectionalLight(0xadd9d2, 1.4);
fillLight.position.set(10, 6, -7);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0xffd6ad, 18, 22, 2);
rimLight.position.set(-9, 7, -6);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Procedural pixel-weave textures
// -------------------------------------------------------------------------
let randomSeed = 81273;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
function makeFabricTexture(baseColor, fleckColor, density = 480) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
context.fillStyle = baseColor;
context.fillRect(0, 0, 128, 128);
for (let i = 0; i < density; i++) {
const light = random() > 0.5;
context.fillStyle = light
? "rgba(255,255,255,0.12)"
: fleckColor;
const size = random() > 0.78 ? 2 : 1;
context.fillRect(
Math.floor(random() * 128),
Math.floor(random() * 128),
size,
size
);
}
context.fillStyle = "rgba(0,0,0,0.055)";
for (let y = 0; y < 128; y += 8) {
context.fillRect(0, y, 128, 1);
}
context.fillStyle = "rgba(255,255,255,0.04)";
for (let x = 0; x < 128; x += 8) {
context.fillRect(x, 0, 1, 128);
}
const texture = new THREE.CanvasTexture(canvas);
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(3.5, 3.5);
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.LinearMipmapLinearFilter;
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
return texture;
}
const tealTexture = makeFabricTexture("#25796f", "rgba(11,64,58,0.55)");
const coralTexture = makeFabricTexture("#df735d", "rgba(121,54,43,0.45)");
const materials = {
teal: new THREE.MeshStandardMaterial({
map: tealTexture,
roughness: 0.93,
metalness: 0
}),
tealSide: new THREE.MeshStandardMaterial({
color: 0x1b615a,
roughness: 0.95
}),
tealLight: new THREE.MeshStandardMaterial({
color: 0x358d80,
roughness: 0.91
}),
tealDark: new THREE.MeshStandardMaterial({
color: 0x164f49,
roughness: 0.95
}),
interior: new THREE.MeshStandardMaterial({
color: 0x173d39,
roughness: 1
}),
coral: new THREE.MeshStandardMaterial({
map: coralTexture,
roughness: 0.9
}),
coralDark: new THREE.MeshStandardMaterial({
color: 0xb64f43,
roughness: 0.92
}),
cream: new THREE.MeshStandardMaterial({
color: 0xf4ead7,
roughness: 0.88
}),
gold: new THREE.MeshStandardMaterial({
color: 0xd7a94b,
roughness: 0.72,
metalness: 0.08
}),
ink: new THREE.MeshStandardMaterial({
color: 0x20332f,
roughness: 0.86
})
};
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
function addBox(
parent,
width,
height,
depth,
x,
y,
z,
material,
castShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addLineSegments(points, color) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(points, 3)
);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.72
});
const lines = new THREE.LineSegments(geometry, material);
scene.add(lines);
return lines;
}
function addFloorBracket(x1, x2, z, color) {
const y = 0.045;
const points = [
x1, y, z,
x2, y, z,
x1, y, z - 0.18,
x1, y, z + 0.18,
x2, y, z - 0.18,
x2, y, z + 0.18
];
addLineSegments(points, color);
}
// -------------------------------------------------------------------------
// Tiled voxel ground
// -------------------------------------------------------------------------
const groundBase = new THREE.Mesh(
new THREE.PlaneGeometry(160, 160),
new THREE.MeshStandardMaterial({
color: 0xded6c7,
roughness: 1
})
);
groundBase.rotation.x = -Math.PI / 2;
groundBase.position.y = -0.13;
groundBase.receiveShadow = true;
scene.add(groundBase);
const tileGeometry = new THREE.BoxGeometry(0.78, 0.2, 0.78);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.97
});
const columns = 29;
const rows = 19;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
columns * rows
);
const matrix = new THREE.Matrix4();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
const x = (column - (columns - 1) / 2) * 0.8;
const z = (row - (rows - 1) / 2) * 0.8;
matrix.makeTranslation(x, -0.09, z);
tiles.setMatrixAt(tileIndex, matrix);
tileColor.set(
(column + row) % 2 === 0 ? 0xe4dccd : 0xdbd2c2
);
tileColor.offsetHSL(0, 0, (random() - 0.5) * 0.025);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
// -------------------------------------------------------------------------
// Unfolded reusable tote
// -------------------------------------------------------------------------
const bag = new THREE.Group();
bag.position.set(-2.55, 0, 0);
scene.add(bag);
// Main front and back panels.
addBox(bag, 6.2, 5.15, 0.3, 0, 2.76, 0.9, materials.teal);
addBox(bag, 6.2, 5.15, 0.28, 0, 2.76, -0.78, materials.tealSide);
// Folded side gussets.
addBox(bag, 0.28, 5.15, 1.82, -3.04, 2.76, 0.08, materials.tealSide);
addBox(bag, 0.28, 5.15, 1.82, 3.04, 2.76, 0.08, materials.tealSide);
// Raised accordion folds on the visible side.
addBox(bag, 0.12, 4.75, 0.42, 3.2, 2.72, 0.42, materials.tealLight);
addBox(bag, 0.12, 4.75, 0.42, 3.2, 2.72, -0.34, materials.tealDark);
addBox(bag, 0.1, 4.7, 0.08, 3.27, 2.72, 0.05, materials.teal);
// Bottom reinforcement.
addBox(bag, 6.1, 0.24, 1.82, 0, 0.25, 0.08, materials.tealDark);
// Dark interior visible through the open top.
addBox(bag, 5.65, 0.08, 1.42, 0, 5.0, 0.08, materials.interior);
addBox(bag, 0.16, 0.75, 1.48, -2.78, 5.04, 0.08, materials.interior);
addBox(bag, 0.16, 0.75, 1.48, 2.78, 5.04, 0.08, materials.interior);
// Bound upper rim.
addBox(bag, 6.3, 0.23, 0.38, 0, 5.39, 0.91, materials.tealDark);
addBox(bag, 6.3, 0.23, 0.36, 0, 5.39, -0.79, materials.tealDark);
addBox(bag, 0.36, 0.23, 1.82, -3.04, 5.39, 0.08, materials.tealDark);
addBox(bag, 0.36, 0.23, 1.82, 3.04, 5.39, 0.08, materials.tealDark);
// Front seams.
addBox(bag, 5.86, 0.09, 0.055, 0, 0.47, 1.075, materials.tealDark);
addBox(bag, 0.07, 4.62, 0.045, -2.72, 2.72, 1.07, materials.tealDark);
addBox(bag, 0.07, 4.62, 0.045, 2.72, 2.72, 1.07, materials.tealDark);
// Broad square-arch handles, one at each side of the opening.
function addHandle(z, front) {
const strap = front ? materials.tealDark : materials.tealSide;
// Vertical legs.
addBox(bag, 0.58, 2.08, 0.28, -1.58, 6.3, z, strap);
addBox(bag, 0.58, 2.08, 0.28, 1.58, 6.3, z, strap);
// Broad top and stepped corners.
addBox(bag, 3.74, 0.52, 0.28, 0, 7.35, z, strap);
addBox(bag, 0.66, 0.64, 0.28, -1.55, 7.04, z, strap);
addBox(bag, 0.66, 0.64, 0.28, 1.55, 7.04, z, strap);
// Reinforced attachment patches.
const patchZ = front ? 1.09 : -0.93;
addBox(
bag,
0.78,
0.62,
0.1,
-1.58,
5.27,
patchZ,
front ? materials.tealLight : materials.tealDark
);
addBox(
bag,
0.78,
0.62,
0.1,
1.58,
5.27,
patchZ,
front ? materials.tealLight : materials.tealDark
);
// Pixel-style stitch marks.
for (const x of [-1.58, 1.58]) {
for (let i = 0; i < 3; i++) {
addBox(
bag,
0.12,
0.035,
0.025,
x,
5.1 + i * 0.16,
front ? 1.155 : -0.995,
materials.cream
);
}
}
}
addHandle(1.16, true);
addHandle(-0.91, false);
// Attached front storage pouch.
const pouch = new THREE.Group();
pouch.position.set(0, 0, 0);
bag.add(pouch);
addBox(pouch, 2.8, 1.82, 0.52, 0, 1.86, 1.32, materials.coral);
addBox(pouch, 2.88, 0.86, 0.16, 0, 2.48, 1.65, materials.coralDark);
// Attachment tabs.
addBox(pouch, 0.22, 0.92, 0.14, -0.9, 3.03, 1.1, materials.coralDark);
addBox(pouch, 0.22, 0.92, 0.14, 0.9, 3.03, 1.1, materials.coralDark);
// Straps and square buckle.
addBox(pouch, 0.18, 1.55, 0.08, -0.76, 1.97, 1.66, materials.tealDark);
addBox(pouch, 0.18, 1.55, 0.08, 0.76, 1.97, 1.66, materials.tealDark);
addBox(pouch, 0.48, 0.46, 0.12, 0, 1.85, 1.76, materials.cream);
addBox(pouch, 0.22, 0.22, 0.12, 0, 1.85, 1.84, materials.gold);
// Pouch edge stitching.
for (let i = 0; i < 7; i++) {
addBox(
pouch,
0.13,
0.035,
0.025,
-1.12 + i * 0.37,
1.06,
1.595,
materials.cream
);
}
// Small maker patch on the tote.
addBox(bag, 0.94, 0.48, 0.07, 2.18, 4.58, 1.105, materials.cream);
addBox(bag, 0.18, 0.18, 0.08, 2.03, 4.58, 1.155, materials.coral);
addBox(bag, 0.34, 0.055, 0.08, 2.31, 4.58, 1.155, materials.tealDark);
// -------------------------------------------------------------------------
// Packed tote form
// -------------------------------------------------------------------------
const packed = new THREE.Group();
packed.position.set(4.15, 0, 0);
scene.add(packed);
// Main compressed block.
addBox(packed, 3.1, 2.9, 1.8, 0, 1.58, 0.1, materials.tealSide);
addBox(packed, 2.96, 2.66, 0.18, 0, 1.58, 1.08, materials.teal);
// Folded upper and lower layers.
addBox(packed, 3.16, 0.26, 1.86, 0, 3.08, 0.1, materials.tealLight);
addBox(packed, 3.05, 0.28, 1.83, 0, 0.16, 0.1, materials.tealDark);
// Stepped side folds.
addBox(packed, 0.2, 2.3, 0.72, 1.56, 1.47, 0.13, materials.tealLight);
addBox(packed, 0.2, 0.84, 1.3, 1.56, 0.69, 0.05, materials.tealDark);
addBox(packed, 0.2, 0.74, 1.28, -1.56, 2.18, 0.04, materials.tealDark);
// Fold creases.
addBox(packed, 2.72, 0.065, 0.06, -0.04, 0.83, 1.2, materials.tealDark);
addBox(packed, 2.72, 0.065, 0.06, -0.04, 2.25, 1.2, materials.tealLight);
// Handles folded around the bundle.
for (const x of [-0.88, 0.88]) {
addBox(packed, 0.2, 3.02, 0.12, x, 1.57, 1.24, materials.tealDark);
addBox(packed, 0.2, 0.12, 1.92, x, 3.23, 0.12, materials.tealDark);
addBox(packed, 0.2, 0.12, 1.92, x, 0.08, 0.12, materials.tealDark);
}
// Packing strap and buckle.
addBox(packed, 3.18, 0.17, 0.11, 0, 1.12, 1.31, materials.gold);
addBox(packed, 0.38, 0.4, 0.13, 0, 1.12, 1.4, materials.cream);
addBox(packed, 0.18, 0.2, 0.13, 0, 1.12, 1.48, materials.ink);
// The attached pouch remains visible as a folded corner.
addBox(packed, 0.72, 0.82, 0.13, 1.03, 0.67, 1.24, materials.coral);
addBox(packed, 0.12, 0.73, 0.13, 0.72, 0.67, 1.25, materials.coralDark);
// Small label on the packed face.
addBox(packed, 0.58, 0.38, 0.08, -0.08, 2.55, 1.24, materials.cream);
addBox(packed, 0.15, 0.15, 0.09, -0.2, 2.55, 1.3, materials.teal);
// -------------------------------------------------------------------------
// Size comparison guides
// -------------------------------------------------------------------------
addFloorBracket(-5.65, 0.55, 2.0, 0x257a70);
addFloorBracket(2.6, 5.7, 2.0, 0xe87861);
// -------------------------------------------------------------------------
// Projected HTML annotations
// -------------------------------------------------------------------------
const annotations = [
{
element: document.querySelector("#bagLabel"),
position: new THREE.Vector3(-2.55, 8.12, 0.1)
},
{
element: document.querySelector("#packedLabel"),
position: new THREE.Vector3(4.15, 3.72, 0.15)
},
{
element: document.querySelector("#bagWidth"),
position: new THREE.Vector3(-2.55, 0.08, 2.2)
},
{
element: document.querySelector("#packedWidth"),
position: new THREE.Vector3(4.15, 0.08, 2.2)
}
];
const projected = new THREE.Vector3();
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const annotation of annotations) {
projected.copy(annotation.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
annotation.element.style.opacity = visible ? "1" : "0";
annotation.element.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
annotation.element.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
// -------------------------------------------------------------------------
// Direct camera controls — no examples or addons
// -------------------------------------------------------------------------
function defaultRadius() {
const aspect = window.innerWidth / window.innerHeight;
if (aspect < 0.78) return 27;
if (aspect < 1.15) return 23;
return 18.5;
}
const initialTheta = 0.22;
const initialPhi = 1.17;
let theta = initialTheta;
let phi = initialPhi;
let radius = defaultRadius();
let targetTheta = theta;
let targetPhi = phi;
let targetRadius = radius;
let dragging = false;
let previousX = 0;
let previousY = 0;
let autoRotate = false;
let userAdjusted = false;
const orbitButton = document.querySelector("#orbitButton");
const resetButton = document.querySelector("#resetButton");
function resetView() {
targetTheta = initialTheta;
targetPhi = initialPhi;
targetRadius = defaultRadius();
autoRotate = false;
userAdjusted = false;
orbitButton.classList.remove("active");
orbitButton.textContent = "Auto orbit";
}
resetButton.addEventListener("click", resetView);
orbitButton.addEventListener("click", () => {
autoRotate = !autoRotate;
orbitButton.classList.toggle("active", autoRotate);
orbitButton.textContent = autoRotate ? "Stop orbit" : "Auto orbit";
});
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
autoRotate = false;
orbitButton.classList.remove("active");
orbitButton.textContent = "Auto orbit";
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
targetTheta -= deltaX * 0.006;
targetPhi = THREE.MathUtils.clamp(
targetPhi - deltaY * 0.005,
0.63,
1.48
);
userAdjusted = true;
});
function endDrag(event) {
dragging = false;
if (
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
targetRadius = THREE.MathUtils.clamp(
targetRadius * Math.exp(event.deltaY * 0.001),
11,
34
);
userAdjusted = true;
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
if (!userAdjusted) {
targetRadius = defaultRadius();
}
}
window.addEventListener("resize", resize);
function animate() {
requestAnimationFrame(animate);
if (autoRotate) {
targetTheta += 0.0025;
}
const smoothing = 0.085;
theta += (targetTheta - theta) * smoothing;
phi += (targetPhi - phi) * smoothing;
radius += (targetRadius - radius) * smoothing;
const sinPhi = Math.sin(phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(theta),
cameraTarget.y + radius * Math.cos(phi),
cameraTarget.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(cameraTarget);
updateAnnotations();
renderer.render(scene, camera);
}
resize();
animate();
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
</script>
</body>
</html>
```