Prompt specification
Build or customize the Three.js 3D scene "Seed-row spacing template" 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: Seed-row spacing template
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/seed-row-spacing-template-x1105
Scene Description Prompt:
A voxel seed tray with evenly spaced planting holes and separate row and column measurements. Create standalone HTML using Three.js from cdnjs.
Working Three.js Starter (seed-row-spacing-template-x1105):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Seed Tray</title>
<style>
:root {
--bg: #07120f;
--panel: rgba(10, 25, 21, 0.86);
--panel-border: rgba(188, 243, 74, 0.18);
--text: #f3f7ef;
--muted: #91a69d;
--lime: #bcf34a;
--cyan: #54d8ff;
--orange: #ff946f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 65% 35%, #14352b 0%, transparent 42%),
linear-gradient(145deg, #07120f, #0a1814 60%, #06100d);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
[hidden] {
display: none !important;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.panel {
position: fixed;
top: 24px;
left: 24px;
width: min(320px, calc(100vw - 48px));
padding: 22px;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
pointer-events: auto;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--lime);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--lime);
box-shadow: 0 0 14px rgba(188, 243, 74, 0.7);
content: "";
}
h1 {
margin: 0;
font-size: clamp(25px, 4vw, 35px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1.03;
}
.subtitle {
margin: 10px 0 18px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.summary {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 10px;
}
.summary-item {
padding: 10px 9px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
}
.summary-item strong {
display: block;
color: #fff;
font-size: 16px;
font-variant-numeric: tabular-nums;
}
.summary-item span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.measurements {
display: grid;
gap: 8px;
}
.measurement {
padding: 12px 13px;
border: 1px solid rgba(255, 255, 255, 0.075);
border-radius: 11px;
background: rgba(255, 255, 255, 0.03);
}
.measurement-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 6px;
}
.measurement-name {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.measurement-value {
font-size: 15px;
font-weight: 760;
font-variant-numeric: tabular-nums;
}
.measurement.row .measurement-value {
color: var(--orange);
}
.measurement.column .measurement-value {
color: var(--cyan);
}
.measurement small {
color: #82988f;
font-size: 11px;
}
.controls {
position: fixed;
top: 24px;
right: 24px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: calc(100vw - 380px);
}
button {
min-height: 38px;
padding: 0 14px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 10px;
background: rgba(10, 25, 21, 0.83);
color: var(--text);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(188, 243, 74, 0.55);
background: rgba(28, 52, 40, 0.94);
transform: translateY(-1px);
}
button[aria-pressed="true"] {
border-color: rgba(188, 243, 74, 0.5);
color: var(--lime);
}
.measure-label {
position: fixed;
z-index: 5;
left: 0;
top: 0;
padding: 7px 10px;
border: 1px solid currentColor;
border-radius: 8px;
background: rgba(5, 14, 11, 0.92);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.1em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
}
.measure-label.column {
color: var(--cyan);
}
.measure-label.row {
color: var(--orange);
}
.legend {
position: fixed;
bottom: 22px;
left: 24px;
display: flex;
align-items: center;
gap: 16px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 10px;
background: rgba(6, 16, 13, 0.76);
color: var(--muted);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.05em;
pointer-events: none;
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 16px;
height: 3px;
border-radius: 2px;
}
.swatch.column {
background: var(--cyan);
box-shadow: 0 0 8px rgba(84, 216, 255, 0.6);
}
.swatch.row {
background: var(--orange);
box-shadow: 0 0 8px rgba(255, 148, 111, 0.6);
}
.tooltip {
position: fixed;
z-index: 10;
min-width: 132px;
padding: 10px 12px;
border: 1px solid rgba(188, 243, 74, 0.35);
border-radius: 9px;
background: rgba(6, 16, 13, 0.94);
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
color: #fff;
font-size: 11px;
line-height: 1.5;
pointer-events: none;
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
.tooltip strong {
display: block;
color: var(--lime);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 760px) {
.panel {
top: 14px;
left: 14px;
width: min(285px, calc(100vw - 28px));
padding: 16px;
}
.subtitle {
margin-bottom: 12px;
}
.summary {
display: none;
}
.controls {
top: auto;
right: 14px;
bottom: 14px;
max-width: calc(100vw - 28px);
}
.legend {
display: none;
}
h1 {
font-size: 25px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D voxel seed tray"></canvas>
<section class="panel">
<div class="eyebrow">Voxel horticulture system</div>
<h1>Seed Tray 7×5</h1>
<p class="subtitle">
Modular planting grid with individually recessed cells and uniform
center-to-center spacing.
</p>
<div class="summary">
<div class="summary-item">
<strong id="traySize">315 × 225</strong>
<span>Overall mm</span>
</div>
<div class="summary-item">
<strong id="cellCount">35</strong>
<span>Planting cells</span>
</div>
<div class="summary-item">
<strong id="pitchValue">45</strong>
<span>Pitch mm</span>
</div>
</div>
<div class="measurements">
<div class="measurement row">
<div class="measurement-top">
<span class="measurement-name">Row measurement</span>
<span class="measurement-value" id="rowSpan">180 mm</span>
</div>
<small id="rowDescription">5 rows · front-to-back center span</small>
</div>
<div class="measurement column">
<div class="measurement-top">
<span class="measurement-name">Column measurement</span>
<span class="measurement-value" id="columnSpan">270 mm</span>
</div>
<small id="columnDescription">7 columns · left-to-right center span</small>
</div>
</div>
</section>
<nav class="controls" aria-label="Scene controls">
<button id="dimensionButton" type="button" aria-pressed="true">
Dimensions on
</button>
<button id="viewButton" type="button">Top view</button>
<button id="resetButton" type="button">Reset</button>
</nav>
<div id="columnLabel" class="measure-label column">
Column pitch · 45 mm
</div>
<div id="rowLabel" class="measure-label row">
Row pitch · 45 mm
</div>
<div class="legend">
<div class="legend-item">
<span class="swatch column"></span>
Column / left-to-right
</div>
<div class="legend-item">
<span class="swatch row"></span>
Row / front-to-back
</div>
<div class="legend-item">Drag to orbit · Scroll to zoom</div>
</div>
<div id="tooltip" class="tooltip" hidden></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -----------------------------------------------------------------------
// Configuration
// -----------------------------------------------------------------------
const ROWS = 5;
const COLUMNS = 7;
const PITCH_MM = 45;
const PITCH = 1.35;
// One scene unit corresponds to 45 / 1.35 = 33.333 millimeters.
const MM_PER_UNIT = PITCH_MM / PITCH;
const canvas = document.querySelector("#scene");
const dimensionButton = document.querySelector("#dimensionButton");
const viewButton = document.querySelector("#viewButton");
const resetButton = document.querySelector("#resetButton");
const tooltip = document.querySelector("#tooltip");
const rowSpan = document.querySelector("#rowSpan");
const columnSpan = document.querySelector("#columnSpan");
const rowDescription = document.querySelector("#rowDescription");
const columnDescription = document.querySelector("#columnDescription");
const traySize = document.querySelector("#traySize");
const cellCount = document.querySelector("#cellCount");
const pitchValue = document.querySelector("#pitchValue");
const trayWidth = COLUMNS * PITCH;
const trayDepth = ROWS * PITCH;
const rowSpanMM = (ROWS - 1) * PITCH_MM;
const columnSpanMM = (COLUMNS - 1) * PITCH_MM;
const trayWidthMM = COLUMNS * PITCH_MM;
const trayDepthMM = ROWS * PITCH_MM;
rowSpan.textContent = `${rowSpanMM} mm`;
columnSpan.textContent = `${columnSpanMM} mm`;
rowDescription.textContent =
`${ROWS} rows · front-to-back center span`;
columnDescription.textContent =
`${COLUMNS} columns · left-to-right center span`;
traySize.textContent = `${trayWidthMM} × ${trayDepthMM}`;
cellCount.textContent = String(ROWS * COLUMNS);
pitchValue.textContent = String(PITCH_MM);
// -----------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// -----------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x07120f, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x07120f, 19, 37);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const target = new THREE.Vector3(0, -0.05, 0);
const hemisphere = new THREE.HemisphereLight(
0xc9f4ff,
0x33472f,
1.7
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff4d6, 3.1);
keyLight.position.set(-5, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x54d8ff, 16, 20, 2);
cyanLight.position.set(6, 5, 5);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xff946f, 11, 18, 2);
warmLight.position.set(-7, 3, -4);
scene.add(warmLight);
// -----------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------
const cellX = (column) =>
(column - (COLUMNS - 1) / 2) * PITCH;
const cellZ = (row) =>
(ROWS - 1) / 2 * PITCH - row * PITCH;
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -----------------------------------------------------------------------
// Ground
// -----------------------------------------------------------------------
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x091713,
roughness: 0.96,
metalness: 0.02
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.75;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(40, 40, 0x315146, 0x172c24);
grid.position.y = -0.742;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
// -----------------------------------------------------------------------
// Voxel tray
// -----------------------------------------------------------------------
const tray = new THREE.Group();
scene.add(tray);
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x173a31,
roughness: 0.82,
metalness: 0.04
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x2b5b4d,
roughness: 0.72,
metalness: 0.03
});
const cellMaterial = new THREE.MeshStandardMaterial({
color: 0x397361,
roughness: 0.78,
metalness: 0.02
});
const footMaterial = new THREE.MeshStandardMaterial({
color: 0x102a23,
roughness: 0.9
});
const seedMaterial = new THREE.MeshStandardMaterial({
color: 0xc99a65,
roughness: 0.74
});
// Base slab.
addBox(
tray,
[trayWidth, 0.44, trayDepth],
[0, -0.22, 0],
baseMaterial
);
// Outer raised rim.
addBox(
tray,
[trayWidth, 0.36, 0.24],
[0, 0.18, trayDepth / 2 - 0.12],
rimMaterial
);
addBox(
tray,
[trayWidth, 0.36, 0.24],
[0, 0.18, -trayDepth / 2 + 0.12],
rimMaterial
);
addBox(
tray,
[0.24, 0.36, trayDepth - 0.48],
[trayWidth / 2 - 0.12, 0.18, 0],
rimMaterial
);
addBox(
tray,
[0.24, 0.36, trayDepth - 0.48],
[-trayWidth / 2 + 0.12, 0.18, 0],
rimMaterial
);
// Four voxel-style feet.
const footInsetX = trayWidth / 2 - 0.58;
const footInsetZ = trayDepth / 2 - 0.58;
for (const x of [-footInsetX, footInsetX]) {
for (const z of [-footInsetZ, footInsetZ]) {
addBox(
tray,
[0.48, 0.28, 0.48],
[x, -0.57, z],
footMaterial
);
}
}
const soilMeshes = [];
const soilGeometry = new THREE.BoxGeometry(0.78, 0.08, 0.78);
const seedGeometry = new THREE.BoxGeometry(0.085, 0.06, 0.085);
const holeSize = 0.78;
const wallThickness = 0.11;
const outerCellSize = holeSize + wallThickness * 2;
const wallOffset = (holeSize + wallThickness) / 2;
const wallHeight = 0.52;
// Recessed square planting cells.
for (let row = 0; row < ROWS; row++) {
for (let column = 0; column < COLUMNS; column++) {
const x = cellX(column);
const z = cellZ(row);
// X-direction cell walls.
addBox(
tray,
[wallThickness, wallHeight, outerCellSize],
[x - wallOffset, 0.26, z],
cellMaterial
);
addBox(
tray,
[wallThickness, wallHeight, outerCellSize],
[x + wallOffset, 0.26, z],
cellMaterial
);
// Z-direction cell walls.
addBox(
tray,
[holeSize, wallHeight, wallThickness],
[x, 0.26, z - wallOffset],
cellMaterial
);
addBox(
tray,
[holeSize, wallHeight, wallThickness],
[x, 0.26, z + wallOffset],
cellMaterial
);
// Dark recessed soil.
const soilMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color().setHSL(
0.075,
0.36,
0.18 + ((row + column) % 3) * 0.015
),
roughness: 1,
emissive: 0x000000,
emissiveIntensity: 0
});
const soil = new THREE.Mesh(soilGeometry, soilMaterial);
soil.position.set(x, 0.08, z);
soil.receiveShadow = true;
soil.userData = {
row: row + 1,
column: column + 1
};
tray.add(soil);
soilMeshes.push(soil);
// Small voxel seed centered in each planting pocket.
const seed = new THREE.Mesh(seedGeometry, seedMaterial);
seed.position.set(x, 0.15, z);
seed.rotation.y = Math.PI * 0.25;
seed.castShadow = true;
tray.add(seed);
}
}
// -----------------------------------------------------------------------
// Dimension geometry
// -----------------------------------------------------------------------
const dimensions = new THREE.Group();
scene.add(dimensions);
const columnColor = 0x54d8ff;
const rowColor = 0xff946f;
const dimensionY = -0.67;
const columnMaterial = new THREE.MeshBasicMaterial({
color: columnColor
});
const rowMaterial = new THREE.MeshBasicMaterial({
color: rowColor
});
const dropColumnMaterial = new THREE.MeshBasicMaterial({
color: columnColor,
transparent: true,
opacity: 0.38
});
const dropRowMaterial = new THREE.MeshBasicMaterial({
color: rowColor,
transparent: true,
opacity: 0.38
});
function dimensionBox(size, position, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
dimensions.add(mesh);
return mesh;
}
function dimensionArrow(position, rotation, material) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.095, 0.24, 8),
material
);
arrow.position.set(position[0], position[1], position[2]);
arrow.rotation.set(rotation[0], rotation[1], rotation[2]);
dimensions.add(arrow);
return arrow;
}
const x0 = cellX(0);
const x1 = cellX(1);
const z0 = cellZ(0);
const z1 = cellZ(1);
const dimensionZ = trayDepth / 2 + 1.16;
const dimensionX = trayWidth / 2 + 1.16;
// COLUMN PITCH: horizontal distance between adjacent column centers.
dimensionBox(
[x1 - x0, 0.035, 0.035],
[(x0 + x1) / 2, dimensionY, dimensionZ],
columnMaterial
);
for (const x of [x0, x1]) {
dimensionBox(
[0.025, 0.025, dimensionZ - z0],
[x, dimensionY, (dimensionZ + z0) / 2],
columnMaterial
);
// Vertical projection from the measured planting center.
dimensionBox(
[0.025, 0.82, 0.025],
[x, -0.24, z0],
dropColumnMaterial
);
}
dimensionArrow(
[x0 - 0.12, dimensionY, dimensionZ],
[0, 0, -Math.PI / 2],
columnMaterial
);
dimensionArrow(
[x1 + 0.12, dimensionY, dimensionZ],
[0, 0, Math.PI / 2],
columnMaterial
);
// ROW PITCH: depth distance between adjacent row centers.
dimensionBox(
[0.035, 0.035, z0 - z1],
[dimensionX, dimensionY, (z0 + z1) / 2],
rowMaterial
);
for (const z of [z0, z1]) {
dimensionBox(
[dimensionX - cellX(COLUMNS - 1), 0.025, 0.025],
[
(dimensionX + cellX(COLUMNS - 1)) / 2,
dimensionY,
z
],
rowMaterial
);
// Vertical projection from the measured planting center.
dimensionBox(
[0.025, 0.82, 0.025],
[cellX(COLUMNS - 1), -0.24, z],
dropRowMaterial
);
}
dimensionArrow(
[dimensionX, dimensionY, z0 + 0.12],
[-Math.PI / 2, 0, 0],
rowMaterial
);
dimensionArrow(
[dimensionX, dimensionY, z1 - 0.12],
[Math.PI / 2, 0, 0],
rowMaterial
);
// -----------------------------------------------------------------------
// Projected HTML measurement labels
// -----------------------------------------------------------------------
const columnLabelElement = document.querySelector("#columnLabel");
const rowLabelElement = document.querySelector("#rowLabel");
const labelPositions = [
{
element: columnLabelElement,
position: new THREE.Vector3(
(x0 + x1) / 2,
dimensionY + 0.24,
dimensionZ
)
},
{
element: rowLabelElement,
position: new THREE.Vector3(
dimensionX,
dimensionY + 0.24,
(z0 + z1) / 2
)
}
];
const projected = new THREE.Vector3();
function updateLabelPositions() {
for (const label of labelPositions) {
if (!dimensions.visible) {
label.element.hidden = true;
continue;
}
projected.copy(label.position).project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.element.hidden = !visible;
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// -----------------------------------------------------------------------
// Custom orbit controls
// -----------------------------------------------------------------------
const orbit = {
theta: 0.56,
phi: 0.67,
radius: 15.6,
goalTheta: 0.56,
goalPhi: 0.67,
goalRadius: 15.6
};
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
target.y + orbit.radius * Math.cos(orbit.phi),
target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(target);
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
tooltip.hidden = true;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (dragging) {
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
orbit.goalTheta -= deltaX * 0.006;
orbit.goalPhi = THREE.MathUtils.clamp(
orbit.goalPhi - deltaY * 0.005,
0.03,
1.28
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
return;
}
updateHover(event);
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.goalRadius = THREE.MathUtils.clamp(
orbit.goalRadius * Math.exp(event.deltaY * 0.001),
8,
25
);
},
{ passive: false }
);
canvas.addEventListener("pointerleave", () => {
if (!dragging) {
clearHover();
}
});
// -----------------------------------------------------------------------
// Cell hover feedback
// -----------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let hoveredSoil = null;
function updateHover(event) {
const rect = canvas.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(soilMeshes, false);
if (hits.length === 0) {
clearHover();
return;
}
const soil = hits[0].object;
if (hoveredSoil !== soil) {
clearHover();
hoveredSoil = soil;
hoveredSoil.material.emissive.setHex(0x54d8ff);
hoveredSoil.material.emissiveIntensity = 0.32;
}
const { row, column } = hoveredSoil.userData;
tooltip.innerHTML = `
<strong>Row ${row} · Column ${column}</strong>
${PITCH_MM} × ${PITCH_MM} mm cell pitch
`;
tooltip.style.left = `${Math.min(event.clientX + 18, window.innerWidth - 170)}px`;
tooltip.style.top = `${Math.min(event.clientY + 18, window.innerHeight - 70)}px`;
tooltip.hidden = false;
}
function clearHover() {
if (hoveredSoil) {
hoveredSoil.material.emissiveIntensity = 0;
hoveredSoil = null;
}
tooltip.hidden = true;
}
// -----------------------------------------------------------------------
// Buttons
// -----------------------------------------------------------------------
dimensionButton.addEventListener("click", () => {
dimensions.visible = !dimensions.visible;
dimensionButton.setAttribute(
"aria-pressed",
String(dimensions.visible)
);
dimensionButton.textContent = dimensions.visible
? "Dimensions on"
: "Dimensions off";
});
let topView = false;
viewButton.addEventListener("click", () => {
topView = !topView;
if (topView) {
orbit.goalTheta = 0;
orbit.goalPhi = 0.03;
orbit.goalRadius = 13.2;
viewButton.textContent = "3D view";
} else {
orbit.goalTheta = 0.56;
orbit.goalPhi = 0.67;
orbit.goalRadius = 15.6;
viewButton.textContent = "Top view";
}
});
resetButton.addEventListener("click", () => {
topView = false;
orbit.goalTheta = 0.56;
orbit.goalPhi = 0.67;
orbit.goalRadius = 15.6;
viewButton.textContent = "Top view";
clearHover();
});
// -----------------------------------------------------------------------
// Resize and render loop
// -----------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
function animate() {
requestAnimationFrame(animate);
const smoothing = 0.11;
orbit.theta += (orbit.goalTheta - orbit.theta) * smoothing;
orbit.phi += (orbit.goalPhi - orbit.phi) * smoothing;
orbit.radius += (orbit.goalRadius - orbit.radius) * smoothing;
updateCamera();
renderer.render(scene, camera);
updateLabelPositions();
}
animate();
</script>
</body>
</html>
```