Prompt specification
Build or customize the Three.js 3D scene "Neighborhood grocery comparison aisle" 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: Neighborhood grocery comparison aisle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/neighborhood-grocery-comparison-aisle-x0555
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts unit-price comparisons. Compose a compact store aisle with baskets, packages, and shelf labels; selecting two products highlights their common comparison unit while unrelated shelf details remain subdued. Voxel style.
Working Three.js Starter (neighborhood-grocery-comparison-aisle-x0555):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Unit Aisle — Voxel Unit-Price Comparison</title>
<style>
:root {
--ink: #203632;
--paper: #f7f1dc;
--accent: #ffd34e;
--teal: #477d72;
--muted: #72827c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8e1d8;
color: var(--ink);
font-family: "Courier New", Courier, monospace;
}
button {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 43%, transparent 45%, rgba(31, 54, 49, 0.13) 100%);
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 10;
}
.topbar {
position: absolute;
top: 22px;
left: 24px;
right: 24px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
}
.brand-mark {
width: 46px;
height: 46px;
display: grid;
place-items: center;
border: 3px solid var(--ink);
background: var(--accent);
box-shadow: 4px 4px 0 var(--ink);
font-size: 25px;
font-weight: 900;
}
.brand-copy {
padding: 7px 12px 8px;
border: 2px solid var(--ink);
background: rgba(247, 241, 220, 0.92);
box-shadow: 4px 4px 0 rgba(32, 54, 50, 0.8);
}
.brand-copy strong {
display: block;
font-size: 16px;
letter-spacing: 0.08em;
}
.brand-copy span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.06em;
}
.top-actions {
display: flex;
align-items: center;
gap: 9px;
pointer-events: auto;
}
.status {
padding: 10px 12px;
border: 2px solid var(--ink);
background: rgba(247, 241, 220, 0.94);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.05em;
box-shadow: 4px 4px 0 rgba(32, 54, 50, 0.65);
}
.status b {
color: var(--teal);
}
.ui-button {
min-height: 38px;
padding: 9px 12px;
border: 2px solid var(--ink);
color: var(--ink);
background: var(--paper);
box-shadow: 4px 4px 0 rgba(32, 54, 50, 0.75);
cursor: pointer;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.04em;
}
.ui-button:hover {
transform: translate(-1px, -1px);
box-shadow: 6px 6px 0 rgba(32, 54, 50, 0.8);
}
.ui-button:active {
transform: translate(3px, 3px);
box-shadow: 1px 1px 0 rgba(32, 54, 50, 0.8);
}
.help {
position: absolute;
top: 92px;
left: 26px;
padding: 7px 10px;
border-left: 4px solid var(--accent);
background: rgba(247, 241, 220, 0.82);
font-size: 10px;
line-height: 1.55;
letter-spacing: 0.04em;
}
.comparison-panel {
position: absolute;
left: 50%;
bottom: 22px;
width: min(790px, calc(100vw - 36px));
transform: translateX(-50%);
border: 3px solid var(--ink);
background: rgba(247, 241, 220, 0.95);
box-shadow: 9px 9px 0 rgba(32, 54, 50, 0.78);
padding: 16px 18px 13px;
}
.panel-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 12px;
}
.eyebrow {
display: block;
margin-bottom: 4px;
color: var(--teal);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.13em;
}
.panel-header h1 {
margin: 0;
font-size: clamp(18px, 2.5vw, 25px);
letter-spacing: -0.04em;
}
.unit-badge {
flex: 0 0 auto;
padding: 9px 11px;
border: 2px solid var(--ink);
background: var(--accent);
box-shadow: 3px 3px 0 var(--ink);
font-size: 13px;
font-weight: 900;
text-align: center;
}
.unit-badge small {
display: block;
margin-bottom: 2px;
font-size: 8px;
letter-spacing: 0.12em;
}
.results {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 11px;
}
.result {
min-width: 0;
padding: 11px;
border: 2px solid var(--ink);
background: #fffaf0;
}
.result-top {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
width: 15px;
height: 34px;
flex: 0 0 auto;
border: 2px solid var(--ink);
}
.result-name {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 13px;
font-weight: 900;
}
.result-meta {
margin: 4px 0 8px 24px;
color: var(--muted);
font-size: 10px;
}
.unit-price {
margin-left: 24px;
font-size: 20px;
font-weight: 900;
letter-spacing: -0.06em;
}
.unit-price span {
margin-left: 3px;
color: var(--teal);
font-size: 10px;
letter-spacing: 0.02em;
}
.bar-track {
height: 9px;
margin: 9px 0 0 24px;
border: 1px solid var(--ink);
background: #e3dfcf;
}
.bar-fill {
height: 100%;
background: var(--teal);
}
.result.cheaper .bar-fill {
background: var(--accent);
}
.verdict {
grid-column: 1 / -1;
padding: 9px 11px;
border: 2px dashed var(--ink);
background: rgba(71, 125, 114, 0.11);
font-size: 11px;
line-height: 1.5;
}
.verdict strong {
color: var(--teal);
}
.empty-state {
padding: 13px;
border: 2px dashed var(--ink);
text-align: center;
font-size: 12px;
line-height: 1.6;
}
.panel-footer {
margin-top: 10px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.03em;
}
@media (max-width: 650px) {
.topbar {
top: 12px;
left: 12px;
right: 12px;
}
.brand-copy span,
.help,
.status {
display: none;
}
.brand-copy {
padding: 7px 8px;
}
.brand-copy strong {
font-size: 12px;
}
.brand-mark {
width: 38px;
height: 38px;
font-size: 20px;
}
.ui-button {
min-height: 34px;
padding: 7px 8px;
font-size: 9px;
}
.comparison-panel {
bottom: 10px;
padding: 12px;
}
.results {
gap: 7px;
}
.result {
padding: 8px;
}
.result-name {
font-size: 10px;
}
.result-meta,
.unit-price {
margin-left: 0;
}
.unit-price {
font-size: 16px;
}
.bar-track {
margin-left: 0;
}
.verdict {
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="vignette"></div>
<div class="ui">
<div class="topbar">
<div class="brand">
<div class="brand-mark">÷</div>
<div class="brand-copy">
<strong>UNIT AISLE</strong>
<span>VOXEL PANTRY / FAIRER SHELF PRICES</span>
</div>
</div>
<div class="top-actions">
<div class="status"><b id="selectionCount">2 / 2</b> SELECTED</div>
<button class="ui-button" id="demoButton" type="button">EXAMPLE PAIR</button>
<button class="ui-button" id="clearButton" type="button">CLEAR</button>
</div>
</div>
<div class="help">
TAP PACKS TO COMPARE · DRAG TO ORBIT · SCROLL TO ZOOM<br>
DOUBLE-CLICK TO RESET THE VIEW
</div>
<section class="comparison-panel" aria-live="polite">
<div class="panel-header">
<div>
<span class="eyebrow" id="panelEyebrow">COMMON COMPARISON UNIT</span>
<h1 id="panelTitle">Price per 1 kilogram</h1>
</div>
<div class="unit-badge">
<small>COMMON UNIT</small>
<span id="unitText">1 KG</span>
</div>
</div>
<div id="comparisonContent" class="results"></div>
<div class="panel-footer">
SELECTED PACKS REMAIN BRIGHT. UNRELATED SHELF DETAILS ARE MUTED FOR CLARITY.
</div>
</section>
</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 stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
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.05;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel store aisle. Select two packages to compare their unit prices."
);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8e1d8);
scene.fog = new THREE.Fog(0xd8e1d8, 19, 36);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 1.6, -1.5);
let cameraRadius = 14.7;
let cameraTheta = 0.58;
let cameraPhi = 1.02;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemiLight = new THREE.HemisphereLight(0xfff8e6, 0x71877d, 2.15);
scene.add(hemiLight);
const sun = new THREE.DirectionalLight(0xfff0cf, 3.25);
sun.position.set(6, 12, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 32;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0xcce8df, 0.85);
fillLight.position.set(-7, 6, 5);
scene.add(fillLight);
// ---------------------------------------------------------------------
// Materials and reusable helpers
// ---------------------------------------------------------------------
const detailMaterials = [];
const productMaterials = new Map();
const mutedColor = new THREE.Color(0x9ba8a1);
function detailMaterial(parameters = {}) {
const material = new THREE.MeshStandardMaterial(parameters);
detailMaterials.push({
material,
baseColor: material.color.clone(),
baseOpacity: material.opacity
});
return material;
}
function registerProductMaterial(product, material) {
if (!productMaterials.has(product.id)) {
productMaterials.set(product.id, []);
}
productMaterials.get(product.id).push({
material,
baseColor: material.color.clone(),
baseOpacity: material.opacity,
baseEmissive: material.emissive
? material.emissive.clone()
: new THREE.Color(0x000000)
});
}
function addBox(
parent,
width,
height,
depth,
x,
y,
z,
material,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function makeCanvasTexture(width, height, draw) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const context = canvas.getContext("2d");
context.imageSmoothingEnabled = false;
draw(context, width, height);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.NearestFilter;
texture.generateMipmaps = false;
return texture;
}
function wrapText(context, text, maxCharacters) {
const words = text.split(" ");
const lines = [];
let current = "";
for (const word of words) {
const candidate = current ? `${current} ${word}` : word;
if (candidate.length > maxCharacters && current) {
lines.push(current);
current = word;
} else {
current = candidate;
}
}
if (current) lines.push(current);
return lines.slice(0, 2);
}
function makeProductTexture(product) {
return makeCanvasTexture(256, 320, (context, width, height) => {
context.fillStyle = "#f4ecd6";
context.fillRect(0, 0, width, height);
context.fillStyle = product.accent;
context.fillRect(0, 0, width, 43);
context.fillStyle = "#203632";
context.fillRect(0, height - 58, width, 58);
context.textAlign = "center";
context.textBaseline = "middle";
const lines = wrapText(context, product.name.toUpperCase(), 13);
context.fillStyle = "#203632";
context.font = "900 27px monospace";
if (lines.length === 1) {
context.fillText(lines[0], width / 2, 86);
} else {
context.fillText(lines[0], width / 2, 66);
context.fillText(lines[1], width / 2, 99);
}
context.fillStyle = product.accent;
context.fillRect(75, 132, 106, 78);
context.fillStyle = "#f8f1dc";
context.fillRect(93, 150, 27, 27);
context.fillRect(137, 150, 27, 27);
context.fillRect(115, 180, 27, 27);
context.fillStyle = "#203632";
context.fillRect(31, 225, 194, 31);
context.fillStyle = "#f8f1dc";
context.font = "900 23px monospace";
context.fillText(
`${formatWeight(product.grams)} PACK`,
width / 2,
240
);
context.font = "900 28px monospace";
context.fillText(
`$${product.price.toFixed(2)}`,
width / 2,
height - 28
);
});
}
function makeShelfLabelTexture(product) {
const perKg = product.price / (product.grams / 1000);
return makeCanvasTexture(512, 128, (context, width, height) => {
context.fillStyle = "#fff9e9";
context.fillRect(0, 0, width, height);
context.fillStyle = product.accent;
context.fillRect(0, 0, 24, height);
context.textBaseline = "middle";
context.textAlign = "left";
context.fillStyle = "#203632";
context.font = "900 28px monospace";
context.fillText(
product.name.toUpperCase(),
41,
35,
292
);
context.fillStyle = "#63756e";
context.font = "700 22px monospace";
context.fillText(
`${formatWeight(product.grams)} PACK`,
41,
70
);
context.textAlign = "right";
context.fillStyle = "#203632";
context.font = "900 29px monospace";
context.fillText(
`$${perKg.toFixed(2)} / KG`,
width - 17,
47
);
context.fillStyle = "#63756e";
context.font = "700 17px monospace";
context.fillText(
`TOTAL $${product.price.toFixed(2)}`,
width - 17,
81
);
});
}
function formatWeight(grams) {
if (grams >= 1000) {
const kilograms = grams / 1000;
return Number.isInteger(kilograms)
? `${kilograms} KG`
: `${kilograms.toFixed(1)} KG`;
}
return `${grams} G`;
}
// ---------------------------------------------------------------------
// Store architecture
// ---------------------------------------------------------------------
const floorMaterial = detailMaterial({
color: 0xcbd1c5,
roughness: 0.95
});
const tileLineMaterial = detailMaterial({
color: 0xaeb9af,
roughness: 1
});
const wallMaterial = detailMaterial({
color: 0xe3e3d2,
roughness: 0.95
});
const wallInsetMaterial = detailMaterial({
color: 0xcbd9cd,
roughness: 0.9
});
const shelfMaterial = detailMaterial({
color: 0x477d72,
roughness: 0.78
});
const shelfDarkMaterial = detailMaterial({
color: 0x315d56,
roughness: 0.82
});
const shelfBackMaterial = detailMaterial({
color: 0xd7d5bf,
roughness: 0.95
});
const woodMaterial = detailMaterial({
color: 0xb28a60,
roughness: 0.9
});
const basketMaterial = detailMaterial({
color: 0x9e7047,
roughness: 0.92
});
addBox(scene, 13.6, 0.24, 11.1, 0, -0.17, -0.7, floorMaterial, false, true);
addBox(scene, 13.6, 5.7, 0.28, 0, 2.72, -5.35, wallMaterial, false, true);
addBox(scene, 0.28, 5.7, 9.5, -6.68, 2.72, -1.25, wallMaterial, false, true);
addBox(
scene,
13.2,
0.18,
0.13,
0,
0.08,
-5.13,
shelfDarkMaterial,
false,
true
);
addBox(
scene,
0.13,
0.18,
9.2,
-6.47,
0.08,
-1.2,
shelfDarkMaterial,
false,
true
);
// Voxel tile grid.
for (let x = -6; x <= 6; x += 1) {
addBox(
scene,
0.025,
0.018,
10.5,
x,
0.005,
-0.7,
tileLineMaterial,
false,
false
);
}
for (let z = -5; z <= 4; z += 1) {
addBox(
scene,
12.8,
0.018,
0.025,
0,
0.005,
z,
tileLineMaterial,
false,
false
);
}
// Wall panels.
for (let x = -5; x <= 5; x += 2.5) {
addBox(
scene,
2.25,
3.85,
0.045,
x,
2.1,
-5.18,
wallInsetMaterial,
false,
true
);
}
function buildRack(parent, width, uprightPositions) {
const levels = [0.45, 1.72, 2.99];
addBox(
parent,
width,
3.92,
0.11,
0,
2.05,
-0.52,
shelfBackMaterial,
false,
true
);
for (const level of levels) {
addBox(
parent,
width,
0.16,
1.08,
0,
level - 0.08,
0,
shelfMaterial
);
addBox(
parent,
width,
0.18,
0.085,
0,
level - 0.08,
0.555,
shelfDarkMaterial
);
}
for (const x of uprightPositions) {
addBox(
parent,
0.14,
4.06,
0.16,
x,
2.03,
0.44,
shelfDarkMaterial
);
addBox(
parent,
0.11,
4.06,
0.12,
x,
2.03,
-0.48,
shelfDarkMaterial
);
}
addBox(parent, width, 0.16, 1.08, 0, 3.94, 0, shelfMaterial);
return levels;
}
const backRack = new THREE.Group();
backRack.position.z = -4.4;
scene.add(backRack);
const backLevels = buildRack(
backRack,
10.4,
[-5.12, -1.72, 1.72, 5.12]
);
const leftRack = new THREE.Group();
leftRack.position.set(-5.75, 0, -0.7);
leftRack.rotation.y = Math.PI / 2;
scene.add(leftRack);
const leftLevels = buildRack(
leftRack,
5.45,
[-2.68, 2.68]
);
// Store header sign.
const headerTexture = makeCanvasTexture(1024, 256, (context, width, height) => {
context.fillStyle = "#203632";
context.fillRect(0, 0, width, height);
context.fillStyle = "#ffd34e";
context.fillRect(0, 0, width, 24);
context.fillRect(0, height - 24, width, 24);
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = "#fff8e4";
context.font = "900 70px monospace";
context.fillText("UNIT AISLE", width / 2, 102);
context.fillStyle = "#ffd34e";
context.font = "900 27px monospace";
context.fillText(
"PANTRY GOODS / COMPARE BY WEIGHT / PRICE PER 1 KG",
width / 2,
174
);
});
const headerMaterial = new THREE.MeshBasicMaterial({
map: headerTexture,
transparent: true
});
const header = new THREE.Mesh(
new THREE.PlaneGeometry(6.4, 1.6),
headerMaterial
);
header.position.set(0, 4.68, -5.175);
scene.add(header);
// ---------------------------------------------------------------------
// Products
// ---------------------------------------------------------------------
const products = [];
const productById = new Map();
const hitMeshes = [];
const catalog = [
{ id: "coffee", name: "Coffee", grams: 250, price: 4.50, accent: "#765548" },
{ id: "oats", name: "Oat Flakes", grams: 500, price: 4.00, accent: "#d7a934" },
{ id: "pasta", name: "Pasta", grams: 500, price: 1.45, accent: "#d65f3b" },
{ id: "beans", name: "Beans", grams: 400, price: 1.20, accent: "#6e9c67" },
{ id: "rice", name: "Basmati Rice",grams: 1000, price: 5.20, accent: "#4d84a6" },
{ id: "lentils", name: "Lentils", grams: 500, price: 2.60, accent: "#b84f45" },
{ id: "flour", name: "Flour", grams: 1000, price: 2.80, accent: "#b8a36a" },
{ id: "couscous",name: "Couscous", grams: 500, price: 2.20, accent: "#d79a58" },
{ id: "tea", name: "Tea", grams: 250, price: 3.25, accent: "#648b62" },
{ id: "salt", name: "Sea Salt", grams: 500, price: 1.10, accent: "#4c8e9a" },
{ id: "almonds", name: "Almonds", grams: 250, price: 4.25, accent: "#9d6849" },
{ id: "crackers",name: "Crackers", grams: 300, price: 2.10, accent: "#d78348" },
{ id: "quinoa", name: "Quinoa", grams: 500, price: 3.40, accent: "#9e6d9a" },
{ id: "raisins", name: "Raisins", grams: 400, price: 2.20, accent: "#6d405d" },
{ id: "barley", name: "Barley", grams: 1000, price: 2.90, accent: "#d2a55d" }
];
function createProduct(parent, product, x, level, z = 0.22) {
const width = 0.72;
const height = product.id === "tea" ? 0.76 : 0.88;
const depth = 0.48;
const group = new THREE.Group();
group.position.set(x, level, z);
parent.add(group);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0xf0e7d1,
roughness: 0.8
});
const accentMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color(product.accent),
roughness: 0.72
});
const body = addBox(
group,
width,
height,
depth,
0,
height / 2,
0,
bodyMaterial
);
body.userData.productId = product.id;
hitMeshes.push(body);
addBox(
group,
width * 0.78,
0.085,
depth * 0.78,
0,
height + 0.015,
-0.015,
accentMaterial
);
const frontMaterial = new THREE.MeshStandardMaterial({
map: makeProductTexture(product),
roughness: 0.88,
transparent: true
});
const front = new THREE.Mesh(
new THREE.PlaneGeometry(width - 0.055, height - 0.065),
frontMaterial
);
front.position.set(0, height / 2 + 0.005, depth / 2 + 0.007);
front.userData.productId = product.id;
front.receiveShadow = true;
group.add(front);
hitMeshes.push(front);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x263b37,
transparent: true,
opacity: 0.32
});
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(body.geometry),
edgeMaterial
);
edges.position.copy(body.position);
group.add(edges);
const frameMaterial = new THREE.MeshBasicMaterial({
color: 0xffd447,
depthTest: false
});
const frame = new THREE.Group();
const frameY = 0.025;
addBox(frame, 0.05, 0.05, depth + 0.2, -width / 2 - 0.08, frameY, 0, frameMaterial, false, false);
addBox(frame, 0.05, 0.05, depth + 0.2, width / 2 + 0.08, frameY, 0, frameMaterial, false, false);
addBox(frame, width + 0.2, 0.05, 0.05, 0, frameY, -depth / 2 - 0.08, frameMaterial, false, false);
addBox(frame, width + 0.2, 0.05, 0.05, 0, frameY, depth / 2 + 0.08, frameMaterial, false, false);
frame.traverse((object) => {
if (object.isMesh) {
object.renderOrder = 8;
}
});
frame.visible = false;
group.add(frame);
const shelfLabelMaterial = new THREE.MeshStandardMaterial({
map: makeShelfLabelTexture(product),
roughness: 0.95,
transparent: true
});
const shelfLabel = new THREE.Mesh(
new THREE.PlaneGeometry(1.28, 0.31),
shelfLabelMaterial
);
shelfLabel.position.set(x, level - 0.19, 0.558);
parent.add(shelfLabel);
registerProductMaterial(product, bodyMaterial);
registerProductMaterial(product, accentMaterial);
registerProductMaterial(product, frontMaterial);
registerProductMaterial(product, edgeMaterial);
registerProductMaterial(product, shelfLabelMaterial);
product.group = group;
product.body = body;
product.frame = frame;
product.accentColor = new THREE.Color(product.accent);
products.push(product);
productById.set(product.id, product);
}
const backPlacements = [
["coffee", -3.45, 0],
["oats", -3.45, 1],
["pasta", -3.45, 2],
["beans", 0, 0],
["rice", 0, 1],
["lentils", 0, 2],
["flour", 3.45, 0],
["couscous", 3.45, 1],
["tea", 3.45, 2]
];
for (const [id, x, levelIndex] of backPlacements) {
createProduct(
backRack,
productById.get(id) || catalog.find((product) => product.id === id),
x,
backLevels[levelIndex]
);
}
const leftPlacements = [
["salt", -1.72, 0],
["almonds", 0, 0],
["crackers", 1.72, 0],
["quinoa", -1.72, 1],
["raisins", 0, 1],
["barley", 1.72, 1]
];
for (const [id, x, levelIndex] of leftPlacements) {
createProduct(
leftRack,
catalog.find((product) => product.id === id),
x,
leftLevels[levelIndex]
);
}
// ---------------------------------------------------------------------
// Baskets and aisle sign
// ---------------------------------------------------------------------
function createBasket(x, z, accent, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
addBox(group, 1.8, 0.13, 1.32, 0, 0.1, 0, basketMaterial);
for (const sx of [-0.81, 0.81]) {
for (const sz of [-0.55, 0.55]) {
addBox(group, 0.13, 0.86, 0.13, sx, 0.49, sz, basketMaterial);
}
}
for (const y of [0.31, 0.52, 0.73]) {
addBox(group, 1.72, 0.12, 0.09, 0, y, -0.56, basketMaterial);
addBox(group, 1.72, 0.12, 0.09, 0, y, 0.56, basketMaterial);
addBox(group, 0.09, 0.12, 1.1, -0.82, y, 0, basketMaterial);
addBox(group, 0.09, 0.12, 1.1, 0.82, y, 0, basketMaterial);
}
addBox(group, 0.1, 0.72, 0.1, -0.56, 1.03, -0.47, basketMaterial);
addBox(group, 0.1, 0.72, 0.1, 0.56, 1.03, -0.47, basketMaterial);
addBox(group, 1.2, 0.11, 0.11, 0, 1.36, -0.47, basketMaterial);
const contentsMaterial = detailMaterial({
color: new THREE.Color(accent),
roughness: 0.86
});
addBox(group, 0.47, 0.55, 0.42, -0.48, 0.48, 0.04, contentsMaterial);
addBox(group, 0.42, 0.68, 0.4, 0, 0.56, -0.04, contentsMaterial);
addBox(group, 0.5, 0.46, 0.44, 0.5, 0.44, 0.02, contentsMaterial);
}
createBasket(-3.25, 1.35, "#d8a739", -0.08);
createBasket(3.05, 1.65, "#b95543", 0.1);
const aisleSign = new THREE.Group();
aisleSign.position.set(3.2, 0, -1.25);
aisleSign.rotation.y = -0.18;
scene.add(aisleSign);
const signFrameMaterial = detailMaterial({
color: 0x315d56,
roughness: 0.8
});
addBox(aisleSign, 1.35, 0.13, 0.12, -0.47, 0.48, 0, signFrameMaterial);
addBox(aisleSign, 1.35, 0.13, 0.12, 0.47, 0.48, 0, signFrameMaterial);
addBox(aisleSign, 0.13, 0.85, 0.12, 0, 0.48, 0, signFrameMaterial);
const aisleSignTexture = makeCanvasTexture(512, 320, (context, width, height) => {
context.fillStyle = "#ffd34e";
context.fillRect(0, 0, width, height);
context.fillStyle = "#203632";
context.fillRect(0, 0, width, 28);
context.fillRect(0, height - 28, width, 28);
context.textAlign = "center";
context.textBaseline = "middle";
context.font = "900 48px monospace";
context.fillText("SAME", width / 2, 104);
context.fillText("UNIT", width / 2, 157);
context.fillStyle = "#203632";
context.fillRect(94, 190, 324, 65);
context.fillStyle = "#fff8e4";
context.font = "900 49px monospace";
context.fillText("1 KG", width / 2, 223);
});
const aisleSignMaterial = new THREE.MeshBasicMaterial({
map: aisleSignTexture
});
const aisleSignFace = new THREE.Mesh(
new THREE.PlaneGeometry(1.28, 0.8),
aisleSignMaterial
);
aisleSignFace.position.set(0, 0.66, 0.071);
aisleSign.add(aisleSignFace);
// ---------------------------------------------------------------------
// Selection state and highlighting
// ---------------------------------------------------------------------
let selectedIds = ["pasta", "rice"];
const badgeGroup = new THREE.Group();
scene.add(badgeGroup);
const comparisonGroup = new THREE.Group();
scene.add(comparisonGroup);
const comparisonLineMaterial = new THREE.LineDashedMaterial({
color: 0xffd447,
dashSize: 0.19,
gapSize: 0.12,
transparent: true,
opacity: 0.95,
depthTest: false
});
let comparisonLine = null;
let comparisonCurve = [];
const comparisonDotMaterial = new THREE.MeshBasicMaterial({
color: 0xffd447,
depthTest: false
});
const comparisonDots = [];
for (let i = 0; i < 3; i++) {
const dot = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.12, 0.12),
comparisonDotMaterial
);
dot.renderOrder = 9;
comparisonGroup.add(dot);
comparisonDots.push(dot);
}
function createBadgeTexture(text) {
return makeCanvasTexture(512, 128, (context, width, height) => {
context.fillStyle = "#203632";
context.fillRect(0, 0, width, height);
context.fillStyle = "#ffd34e";
context.fillRect(0, 0, 13, height);
context.fillRect(width - 13, 0, 13, height);
context.textAlign = "center";
context.textBaseline = "middle";
context.font = "900 42px monospace";
context.fillStyle = "#fff8e4";
context.fillText(text, width / 2, height / 2 + 1);
});
}
function clearBadges() {
while (badgeGroup.children.length) {
const badge = badgeGroup.children.pop();
badge.material.map.dispose();
badge.material.dispose();
}
}
function updateBadges() {
clearBadges();
scene.updateMatrixWorld(true);
for (const id of selectedIds) {
const product = productById.get(id);
const position = product.group.getWorldPosition(new THREE.Vector3());
const texture = createBadgeTexture(
selectedIds.length === 2 ? "PER 1 KG" : "SELECT 1 MORE"
);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.position.copy(position);
sprite.position.y += 1.52;
sprite.scale.set(2.15, 0.54, 1);
sprite.renderOrder = 12;
badgeGroup.add(sprite);
}
}
function updateComparisonLine() {
if (comparisonLine) {
comparisonGroup.remove(comparisonLine);
comparisonLine.geometry.dispose();
comparisonLine = null;
}
comparisonCurve = [];
comparisonGroup.visible = selectedIds.length === 2;
if (selectedIds.length !== 2) return;
scene.updateMatrixWorld(true);
const start = productById
.get(selectedIds[0])
.group.getWorldPosition(new THREE.Vector3());
const end = productById
.get(selectedIds[1])
.group.getWorldPosition(new THREE.Vector3());
start.y += 1.05;
end.y += 1.05;
for (let i = 0; i <= 48; i++) {
const t = i / 48;
const point = new THREE.Vector3().lerpVectors(start, end, t);
point.y += Math.sin(t * Math.PI) * 1.05;
comparisonCurve.push(point);
}
const geometry = new THREE.BufferGeometry().setFromPoints(comparisonCurve);
comparisonLine = new THREE.Line(geometry, comparisonLineMaterial);
comparisonLine.computeLineDistances();
comparisonLine.renderOrder = 9;
comparisonGroup.add(comparisonLine);
}
function applyVisualState() {
const dimUnrelated = selectedIds.length === 2;
for (const entry of detailMaterials) {
entry.material.color.copy(entry.baseColor);
entry.material.opacity = entry.baseOpacity;
if (dimUnrelated) {
entry.material.color.lerp(mutedColor, 0.66);
}
}
for (const product of products) {
const isSelected = selectedIds.includes(product.id);
const entries = productMaterials.get(product.id) || [];
for (const entry of entries) {
const material = entry.material;
material.color.copy(entry.baseColor);
material.opacity = entry.baseOpacity;
if (material.emissive) {
material.emissive.copy(entry.baseEmissive);
material.emissiveIntensity = 1;
}
if (dimUnrelated && !isSelected) {
material.color.lerp(mutedColor, 0.76);
if (material.map) {
material.opacity = 0.32;
}
}
if (isSelected && material.emissive) {
material.emissive.copy(product.accentColor);
material.emissiveIntensity = material.map ? 0.045 : 0.12;
}
}
product.frame.visible = isSelected;
}
}
// ---------------------------------------------------------------------
// Comparison panel
// ---------------------------------------------------------------------
const comparisonContent = document.getElementById("comparisonContent");
const panelTitle = document.getElementById("panelTitle");
const panelEyebrow = document.getElementById("panelEyebrow");
const unitText = document.getElementById("unitText");
const selectionCount = document.getElementById("selectionCount");
const clearButton = document.getElementById("clearButton");
function currency(value) {
return `$${value.toFixed(2)}`;
}
function updatePanel() {
selectionCount.textContent = `${selectedIds.length} / 2`;
clearButton.disabled = selectedIds.length === 0;
if (selectedIds.length < 2) {
panelEyebrow.textContent =
selectedIds.length === 1 ? "ONE MORE PACK NEEDED" : "CHOOSE TWO PACKS";
panelTitle.textContent =
selectedIds.length === 1
? "Add one more product"
: "Select two products to compare";
unitText.textContent = "—";
if (selectedIds.length === 0) {
comparisonContent.innerHTML = `
<div class="empty-state">
Tap any two packages in the aisle.<br>
Their prices will be normalized to the same unit.
</div>
`;
} else {
const product = productById.get(selectedIds[0]);
const perKg = product.price / (product.grams / 1000);
comparisonContent.innerHTML = `
<div class="empty-state">
<strong>${product.name.toUpperCase()}</strong><br>
${currency(perKg)} per kg — select a second package.
</div>
`;
}
return;
}
const first = productById.get(selectedIds[0]);
const second = productById.get(selectedIds[1]);
const firstPerKg = first.price / (first.grams / 1000);
const secondPerKg = second.price / (second.grams / 1000);
const maximum = Math.max(firstPerKg, secondPerKg);
const cheaper = firstPerKg <= secondPerKg ? first : second;
const dearer = cheaper === first ? second : first;
const difference = Math.abs(firstPerKg - secondPerKg);
panelEyebrow.textContent = "COMMON COMPARISON UNIT";
panelTitle.textContent = "Price per 1 kilogram";
unitText.textContent = "1 KG";
comparisonContent.innerHTML = `
<article class="result ${first === cheaper ? "cheaper" : ""}">
<div class="result-top">
<div class="swatch" style="background:${first.accent}"></div>
<div class="result-name">${first.name}</div>
</div>
<div class="result-meta">
${formatWeight(first.grams)} pack · ${currency(first.price)} total
</div>
<div class="unit-price">
${currency(firstPerKg)}<span>/ kg</span>
</div>
<div class="bar-track">
<div class="bar-fill" style="width:${Math.max(7, firstPerKg / maximum * 100)}%"></div>
</div>
</article>
<article class="result ${second === cheaper ? "cheaper" : ""}">
<div class="result-top">
<div class="swatch" style="background:${second.accent}"></div>
<div class="result-name">${second.name}</div>
</div>
<div class="result-meta">
${formatWeight(second.grams)} pack · ${currency(second.price)} total
</div>
<div class="unit-price">
${currency(secondPerKg)}<span>/ kg</span>
</div>
<div class="bar-track">
<div class="bar-fill" style="width:${Math.max(7, secondPerKg / maximum * 100)}%"></div>
</div>
</article>
<div class="verdict">
<strong>${cheaper.name}</strong> costs ${currency(difference)} less per kilogram
than ${dearer.name}. The yellow packages use the shared <strong>1 KG</strong> basis.
</div>
`;
}
function refreshSelection() {
applyVisualState();
updateBadges();
updateComparisonLine();
updatePanel();
}
function toggleProduct(id) {
const existingIndex = selectedIds.indexOf(id);
if (existingIndex !== -1) {
selectedIds.splice(existingIndex, 1);
} else if (selectedIds.length < 2) {
selectedIds.push(id);
} else {
selectedIds.shift();
selectedIds.push(id);
}
refreshSelection();
}
// ---------------------------------------------------------------------
// Direct mouse / touch camera controls and product picking
// ---------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let pointerMoved = false;
let pointerId = null;
let lastPointerX = 0;
let lastPointerY = 0;
function setPointerCoordinates(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 pickProduct(event) {
setPointerCoordinates(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(hitMeshes, false);
if (intersections.length) {
const id = intersections[0].object.userData.productId;
if (id) toggleProduct(id);
}
}
function updateHover(event) {
if (pointerDown) return;
setPointerCoordinates(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(hitMeshes, false);
renderer.domElement.style.cursor = intersections.length ? "pointer" : "grab";
}
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
pointerId = event.pointerId;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown || event.pointerId !== pointerId) {
updateHover(event);
return;
}
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (Math.abs(deltaX) + Math.abs(deltaY) > 0) {
pointerMoved = true;
}
cameraTheta -= deltaX * 0.006;
cameraPhi -= deltaY * 0.005;
cameraTheta = THREE.MathUtils.clamp(cameraTheta, -0.05, 1.28);
cameraPhi = THREE.MathUtils.clamp(cameraPhi, 0.62, 1.29);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (event.pointerId === pointerId && !pointerMoved) {
pickProduct(event);
}
pointerDown = false;
pointerId = null;
renderer.domElement.style.cursor = "grab";
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
pointerId = null;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!pointerDown) renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius *= Math.exp(event.deltaY * 0.001);
cameraRadius = THREE.MathUtils.clamp(cameraRadius, 9.5, 22);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraRadius = 14.7;
cameraTheta = 0.58;
cameraPhi = 1.02;
updateCamera();
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
document.getElementById("clearButton").addEventListener("click", () => {
selectedIds = [];
refreshSelection();
});
document.getElementById("demoButton").addEventListener("click", () => {
selectedIds = ["pasta", "rice"];
refreshSelection();
});
window.addEventListener("keydown", (event) => {
if (event.key === "Escape") {
selectedIds = [];
refreshSelection();
}
});
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
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();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
if (comparisonGroup.visible && comparisonCurve.length) {
comparisonDots.forEach((dot, index) => {
const t = (elapsed * 0.23 + index / comparisonDots.length) % 1;
const curveIndex = t * (comparisonCurve.length - 1);
const low = Math.floor(curveIndex);
const high = Math.min(low + 1, comparisonCurve.length - 1);
const localT = curveIndex - low;
dot.position.lerpVectors(
comparisonCurve[low],
comparisonCurve[high],
localT
);
const pulse = 1 + Math.sin(elapsed * 5 + index) * 0.14;
dot.scale.setScalar(pulse);
});
}
renderer.render(scene, camera);
}
refreshSelection();
animate();
</script>
</body>
</html>
```