Prompt specification
Build or customize the Three.js 3D scene "Rotation constraint puzzle" 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: Rotation constraint puzzle
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rotation-constraint-puzzle-x1052
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing crop rotation. Create a fictional crop-planning game with declared predecessor rules and resource needs; users arrange sequences that satisfy those rules without interpreting them as universal agronomic recommendations. Low-poly.
Working Three.js Starter (rotation-constraint-puzzle-x1052):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Loam Loop — Fictional Crop Rotation</title>
<style>
:root {
--ink: #17352d;
--muted: #64756d;
--cream: #f6f1df;
--paper: #fffdf4;
--line: #d9d3ba;
--green: #287a5d;
--green-dark: #18503e;
--lime: #b9d66d;
--orange: #ef8c57;
--blue: #58a9bd;
--purple: #9b78c4;
--red: #c95043;
--shadow: 0 12px 30px rgba(34, 63, 51, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
background: var(--cream);
}
body {
overflow: hidden;
}
button {
font: inherit;
}
.app {
height: 100vh;
display: grid;
grid-template-rows: 68px minmax(0, 1fr);
}
.topbar {
z-index: 20;
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 0 22px;
color: #fffdf4;
background: var(--green-dark);
border-bottom: 4px solid var(--lime);
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 0;
}
.brand-mark {
width: 38px;
height: 38px;
display: grid;
place-items: center;
flex: 0 0 auto;
color: var(--green-dark);
background: var(--lime);
border-radius: 12px 4px 12px 4px;
font-size: 24px;
transform: rotate(-4deg);
}
.brand h1 {
margin: 0;
font-size: 19px;
line-height: 1;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.brand p {
margin: 5px 0 0;
color: #cfe0d6;
font-size: 12px;
}
.fiction-badge {
max-width: 540px;
padding: 9px 13px;
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 999px;
color: #e7f3e9;
background: rgba(255, 255, 255, 0.08);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.06em;
text-align: right;
text-transform: uppercase;
}
.layout {
min-height: 0;
display: grid;
grid-template-columns: 350px minmax(0, 1fr);
}
.sidebar {
z-index: 10;
min-height: 0;
overflow-y: auto;
padding: 18px;
background:
linear-gradient(rgba(255, 253, 244, 0.94), rgba(255, 253, 244, 0.94)),
repeating-linear-gradient(
0deg,
transparent 0 23px,
rgba(38, 77, 61, 0.07) 23px 24px
);
border-right: 1px solid var(--line);
scrollbar-width: thin;
scrollbar-color: #a8b8aa transparent;
}
.eyebrow {
margin: 0 0 7px;
color: var(--green);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.mission {
padding: 15px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: var(--shadow);
}
.mission h2 {
margin: 0 0 7px;
font-size: 17px;
}
.mission p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.mission strong {
color: var(--ink);
}
.section-head {
display: flex;
align-items: end;
justify-content: space-between;
gap: 12px;
margin: 20px 1px 9px;
}
.section-head h2 {
margin: 0;
font-size: 14px;
}
.section-head span {
color: var(--muted);
font-size: 10px;
}
.resources {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
}
.resource {
min-width: 0;
padding: 9px 6px;
border: 1px solid var(--line);
border-radius: 11px;
background: var(--paper);
text-align: center;
}
.resource-symbol {
display: block;
margin-bottom: 3px;
color: var(--green);
font-size: 18px;
line-height: 1;
}
.resource-name {
display: block;
overflow: hidden;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.05em;
text-overflow: ellipsis;
text-transform: uppercase;
}
.resource-value {
display: block;
margin-top: 4px;
font-size: 15px;
font-weight: 900;
}
.resource-value small {
color: var(--muted);
font-size: 8px;
font-weight: 700;
}
.resource.shortage {
color: var(--red);
border-color: #e3aaa3;
background: #fff0ec;
}
.resource.shortage .resource-value {
color: var(--red);
}
.catalog {
display: grid;
gap: 9px;
}
.crop-card {
position: relative;
width: 100%;
display: grid;
grid-template-columns: 43px minmax(0, 1fr);
gap: 11px;
padding: 12px;
color: var(--ink);
text-align: left;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--paper);
box-shadow: 0 4px 12px rgba(40, 70, 55, 0.05);
cursor: grab;
transition: transform 150ms ease, border-color 150ms ease, box-shadow 150ms ease;
}
.crop-card:hover {
transform: translateY(-2px);
border-color: #a9bca9;
box-shadow: 0 8px 18px rgba(40, 70, 55, 0.09);
}
.crop-card:focus-visible,
.plot-slot:focus-visible,
.action-button:focus-visible,
.icon-button:focus-visible {
outline: 3px solid rgba(40, 122, 93, 0.3);
outline-offset: 2px;
}
.crop-card.selected {
border-color: var(--green);
box-shadow: 0 0 0 3px rgba(40, 122, 93, 0.13);
transform: translateY(-1px);
}
.crop-card.selected::after {
content: "SELECTED";
position: absolute;
top: 8px;
right: 8px;
padding: 3px 6px;
color: white;
background: var(--green);
border-radius: 999px;
font-size: 7px;
font-weight: 900;
letter-spacing: 0.09em;
}
.crop-swatch {
width: 43px;
height: 43px;
display: grid;
place-items: center;
align-self: start;
color: white;
border-radius: 13px 5px 13px 5px;
font-size: 24px;
text-shadow: 0 2px 6px rgba(0, 0, 0, 0.18);
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.25);
}
.crop-title-row {
display: flex;
align-items: center;
gap: 8px;
padding-right: 48px;
}
.crop-title-row strong {
font-size: 13px;
}
.crop-costs {
display: flex;
flex-wrap: wrap;
gap: 4px;
margin-top: 6px;
}
.cost-pill {
padding: 3px 6px;
color: #3f5c50;
background: #edf1df;
border-radius: 999px;
font-size: 9px;
font-weight: 800;
}
.crop-rule {
margin-top: 8px;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.crop-rule b {
color: var(--ink);
}
.crop-bonus {
margin-top: 4px;
color: #8a621d;
font-size: 9px;
font-weight: 800;
}
.rules-card {
margin-top: 18px;
padding: 14px;
border: 1px dashed #b7bba4;
border-radius: 14px;
background: #f3f2df;
}
.rules-card h2 {
margin: 0 0 8px;
font-size: 13px;
}
.rules-card ol {
margin: 0;
padding-left: 18px;
color: var(--muted);
font-size: 11px;
line-height: 1.6;
}
.fiction-note {
margin: 14px 2px 0;
color: #716f5d;
font-size: 10px;
line-height: 1.5;
}
.feedback {
margin-top: 16px;
padding: 13px;
border: 1px solid var(--line);
border-left: 4px solid var(--green);
border-radius: 12px;
background: var(--paper);
}
.feedback.error {
border-left-color: var(--red);
background: #fff1ed;
}
.feedback.success {
border-left-color: var(--lime);
background: #eff8df;
}
.feedback strong {
display: block;
margin-bottom: 4px;
font-size: 12px;
}
.feedback p {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #dbe8e5;
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.stage-hud {
position: absolute;
z-index: 4;
top: 16px;
left: 16px;
right: 16px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.stage-title,
.camera-box {
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 13px;
background: rgba(255, 253, 244, 0.82);
box-shadow: 0 6px 22px rgba(44, 74, 61, 0.09);
backdrop-filter: blur(8px);
}
.stage-title strong {
display: block;
font-size: 12px;
}
.stage-title span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.camera-box {
display: flex;
align-items: center;
gap: 9px;
pointer-events: auto;
}
.camera-box span {
color: var(--muted);
font-size: 9px;
}
.icon-button {
width: 30px;
height: 30px;
display: grid;
place-items: center;
padding: 0;
color: var(--green-dark);
border: 1px solid #c7cfbc;
border-radius: 9px;
background: white;
cursor: pointer;
}
#plotLabels {
position: absolute;
z-index: 3;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.plot-label {
position: absolute;
top: 0;
left: 0;
min-width: 78px;
padding: 5px 8px;
color: var(--ink);
text-align: center;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 999px;
background: rgba(255, 253, 244, 0.88);
box-shadow: 0 5px 12px rgba(24, 59, 45, 0.12);
transform: translate(-50%, -100%);
will-change: transform;
}
.plot-label span {
margin-right: 4px;
color: var(--green);
font-size: 9px;
font-weight: 900;
}
.plot-label strong {
font-size: 9px;
}
.sequence-dock {
position: absolute;
z-index: 7;
left: 16px;
right: 16px;
bottom: 16px;
padding: 12px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 18px;
background: rgba(255, 253, 244, 0.93);
box-shadow: 0 15px 40px rgba(31, 64, 49, 0.18);
backdrop-filter: blur(10px);
}
.dock-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 9px;
}
.dock-heading strong {
font-size: 12px;
}
.dock-heading span {
color: var(--muted);
font-size: 9px;
}
.slots {
display: grid;
grid-template-columns: repeat(5, minmax(112px, 1fr));
gap: 8px;
overflow-x: auto;
padding-bottom: 3px;
}
.plot-slot {
position: relative;
min-height: 92px;
padding: 9px 8px 8px;
overflow: hidden;
border: 1px solid #d4d3bc;
border-radius: 13px;
background: #f2efdc;
cursor: pointer;
transition: border-color 140ms ease, background 140ms ease, transform 140ms ease;
}
.plot-slot:hover {
transform: translateY(-2px);
border-color: #9aae9b;
}
.plot-slot.drag-over {
border: 2px dashed var(--green);
background: #e6f1dc;
transform: scale(1.02);
}
.plot-slot.has-crop {
background: white;
}
.plot-slot.legal {
border-color: #78aa83;
}
.plot-slot.illegal {
border-color: #d77b6d;
background: #fff0eb;
}
.slot-number {
position: absolute;
top: 7px;
right: 8px;
color: #8a9488;
font-size: 9px;
font-weight: 900;
}
.slot-icon {
width: 29px;
height: 29px;
display: grid;
place-items: center;
margin-bottom: 5px;
color: white;
border-radius: 9px 3px 9px 3px;
background: #aeb9aa;
font-size: 18px;
}
.slot-name {
overflow: hidden;
padding-right: 10px;
font-size: 11px;
font-weight: 900;
text-overflow: ellipsis;
white-space: nowrap;
}
.slot-rule {
overflow: hidden;
margin-top: 4px;
color: var(--muted);
font-size: 8px;
line-height: 1.25;
text-overflow: ellipsis;
}
.slot-rule.ok {
color: var(--green);
font-weight: 800;
}
.slot-rule.bad {
color: var(--red);
font-weight: 800;
}
.clear-slot {
position: absolute;
right: 5px;
bottom: 5px;
width: 19px;
height: 19px;
display: none;
place-items: center;
padding: 0;
color: var(--red);
border: 0;
border-radius: 50%;
background: #f8dcd4;
font-size: 14px;
line-height: 1;
cursor: pointer;
}
.plot-slot.has-crop .clear-slot {
display: grid;
}
.dock-actions {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 8px;
margin-top: 10px;
}
.action-button {
padding: 9px 13px;
color: var(--ink);
border: 1px solid #c8cdb9;
border-radius: 10px;
background: #fffef7;
font-size: 10px;
font-weight: 900;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
.action-button:hover {
transform: translateY(-1px);
background: #f0f0dd;
}
.action-button.primary {
color: white;
border-color: var(--green);
background: var(--green);
}
.action-button.primary:hover {
background: var(--green-dark);
}
#toast {
position: absolute;
z-index: 30;
left: 50%;
top: 78px;
max-width: min(90%, 420px);
padding: 10px 14px;
color: white;
background: rgba(24, 80, 62, 0.94);
border-radius: 999px;
box-shadow: 0 10px 30px rgba(20, 50, 40, 0.2);
font-size: 11px;
font-weight: 800;
text-align: center;
opacity: 0;
transform: translate(-50%, -8px);
pointer-events: none;
transition: opacity 180ms ease, transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#loading {
position: absolute;
z-index: 40;
inset: 0;
display: grid;
place-items: center;
color: var(--green-dark);
background: #dbe8e5;
font-size: 13px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100vh;
grid-template-rows: auto auto;
}
.topbar {
min-height: 68px;
padding: 10px 14px;
}
.fiction-badge {
max-width: 210px;
font-size: 8px;
}
.layout {
display: flex;
flex-direction: column;
}
.stage {
order: -1;
height: 70vh;
min-height: 540px;
}
.sidebar {
overflow: visible;
border-right: 0;
border-top: 1px solid var(--line);
}
.catalog {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.sequence-dock {
left: 8px;
right: 8px;
bottom: 8px;
padding: 9px;
}
.dock-heading span {
display: none;
}
}
@media (max-width: 560px) {
.brand p,
.camera-box span {
display: none;
}
.fiction-badge {
max-width: 155px;
padding: 7px 9px;
}
.stage {
height: 74vh;
min-height: 570px;
}
.stage-title {
max-width: 200px;
}
.catalog {
grid-template-columns: 1fr;
}
.dock-actions {
justify-content: stretch;
}
.action-button {
flex: 1;
padding: 9px 7px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">↻</div>
<div>
<h1>Loam Loop</h1>
<p>A fictional crop-rotation puzzle</p>
</div>
</div>
<div class="fiction-badge">
Fictional arcade system · Not agronomic advice · No real-world recommendations
</div>
</header>
<div class="layout">
<aside class="sidebar" aria-label="Game controls">
<section class="mission">
<p class="eyebrow">Warden's brief</p>
<h2>Build a legal five-bed rotation</h2>
<p>
Arrange the beds from left to right. Every crop has an invented
<strong>predecessor law</strong> and spends abstract inventory tokens.
</p>
</section>
<div class="section-head">
<h2>Inventory tokens</h2>
<span>available / projected left</span>
</div>
<div class="resources" id="resources"></div>
<div class="section-head">
<h2>Crop catalog</h2>
<span>select, then click a bed</span>
</div>
<div class="catalog" id="catalog"></div>
<section class="rules-card">
<h2>How the puzzle works</h2>
<ol>
<li>The predecessor is the crop in the previous bed, read left to right.</li>
<li>Bed 01 has no predecessor; only crops permitting a first bed may start there.</li>
<li>The full sequence must fit inside the abstract token inventory.</li>
<li>All five beds must satisfy their declared predecessor laws.</li>
<li>Drag crops between beds, or select a crop and click a bed.</li>
</ol>
</section>
<p class="fiction-note">
<strong>Game fiction only:</strong> crop names, predecessor laws, resource
costs, yields, and bonuses were invented for this interface. They do not
describe real farming systems and should not be used as agronomic guidance.
</p>
<div class="feedback" id="feedback" aria-live="polite">
<strong id="feedbackTitle">Awaiting a plan</strong>
<p id="feedbackText">Fill all five beds, then submit the sequence for inspection.</p>
</div>
</aside>
<main class="stage" aria-label="Low-poly crop rotation board">
<div id="scene"></div>
<div id="plotLabels" aria-hidden="true"></div>
<div class="stage-hud">
<div class="stage-title">
<strong>Warden's Terrace</strong>
<span>Bed order follows the sequence from left to right.</span>
</div>
<div class="camera-box">
<span>Drag to orbit · wheel to zoom</span>
<button class="icon-button" id="resetCamera" type="button" title="Reset camera">
⌂
</button>
</div>
</div>
<div id="toast" role="status"></div>
<section class="sequence-dock" aria-label="Rotation sequence">
<div class="dock-heading">
<strong>Rotation sequence</strong>
<span>Drag crop cards into beds; drag occupied beds to reorder.</span>
</div>
<div class="slots" id="slots"></div>
<div class="dock-actions">
<button class="action-button" id="hintButton" type="button">Load hint</button>
<button class="action-button" id="clearButton" type="button">Clear</button>
<button class="action-button primary" id="plantButton" type="button">
Inspect plan
</button>
</div>
</section>
<div id="loading">Building the warden's terrace…</div>
</main>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Fictional game data. These rules are invented and have no claim
// about real-world crop rotation, resource needs, or yield.
// ---------------------------------------------------------------------
const RESOURCE_DATA = {
sun: { name: "Sun", symbol: "☀", available: 10 },
water: { name: "Dew", symbol: "◈", available: 8 },
ore: { name: "Ore", symbol: "◆", available: 5 },
fiber: { name: "Fiber", symbol: "⌁", available: 7 }
};
const ALL_CROPS = ["sunwheat", "emberroot", "mistbean", "glassreed", "nightmelon"];
const CROP_DATA = {
sunwheat: {
name: "Sunwheat",
symbol: "✺",
color: "#e7bd3f",
color3d: 0xe7bd3f,
leaf: 0x5e9a52,
cost: { sun: 2, water: 1 },
yield: 3,
accepts: ALL_CROPS,
allowedFirst: true,
ruleText: "Any crop may precede it.",
bonus: null
},
emberroot: {
name: "Emberroot",
symbol: "◆",
color: "#dc694b",
color3d: 0xdc694b,
leaf: 0x477c4f,
cost: { sun: 1, water: 2, ore: 1 },
yield: 4,
accepts: ["sunwheat"],
allowedFirst: false,
ruleText: "Must directly follow Sunwheat.",
bonus: "Harmony +2 after Sunwheat"
},
mistbean: {
name: "Mistbean",
symbol: "●",
color: "#68a56e",
color3d: 0x68a56e,
leaf: 0x39704c,
cost: { sun: 1, water: 2, fiber: 1 },
yield: 3,
accepts: ALL_CROPS.filter(crop => crop !== "emberroot"),
allowedFirst: true,
ruleText: "May follow anything except Emberroot.",
bonus: "Harmony +2 after Sunwheat"
},
glassreed: {
name: "Glassreed",
symbol: "◇",
color: "#58a9bd",
color3d: 0x58a9bd,
leaf: 0x3d806f,
cost: { sun: 2, ore: 1, fiber: 1 },
yield: 5,
accepts: ["emberroot", "mistbean"],
allowedFirst: false,
ruleText: "Must follow Emberroot or Mistbean.",
bonus: "Harmony +2 after either legal predecessor"
},
nightmelon: {
name: "Nightmelon",
symbol: "✦",
color: "#9270bd",
color3d: 0x9270bd,
leaf: 0x4c8059,
cost: { sun: 2, water: 1, fiber: 2 },
yield: 6,
accepts: ["glassreed"],
allowedFirst: false,
ruleText: "Must directly follow Glassreed.",
bonus: "Harmony +3 after Glassreed"
}
};
const sequence = Array(5).fill(null);
let selectedCrop = null;
let planted = false;
let toastTimer = 0;
const catalogElement = document.querySelector("#catalog");
const slotsElement = document.querySelector("#slots");
const resourcesElement = document.querySelector("#resources");
const feedbackElement = document.querySelector("#feedback");
const feedbackTitle = document.querySelector("#feedbackTitle");
const feedbackText = document.querySelector("#feedbackText");
const toastElement = document.querySelector("#toast");
// ---------------------------------------------------------------------
// Interface construction.
// ---------------------------------------------------------------------
function costPills(crop) {
return Object.entries(crop.cost)
.map(([key, amount]) => {
const resource = RESOURCE_DATA[key];
return `<span class="cost-pill">${resource.symbol} ${amount} ${resource.name}</span>`;
})
.join("");
}
function createCatalog() {
catalogElement.innerHTML = "";
for (const [id, crop] of Object.entries(CROP_DATA)) {
const button = document.createElement("button");
button.type = "button";
button.className = "crop-card";
button.dataset.crop = id;
button.draggable = true;
button.setAttribute("aria-pressed", "false");
button.innerHTML = `
<span class="crop-swatch" style="background:${crop.color}">${crop.symbol}</span>
<span>
<span class="crop-title-row"><strong>${crop.name}</strong></span>
<span class="crop-costs">${costPills(crop)}</span>
<span class="crop-rule"><b>Predecessor law:</b> ${crop.ruleText}</span>
${crop.bonus ? `<span class="crop-bonus">✦ ${crop.bonus}</span>` : ""}
</span>
`;
button.addEventListener("click", () => {
selectedCrop = selectedCrop === id ? null : id;
updateCatalogSelection();
});
button.addEventListener("dragstart", event => {
selectedCrop = id;
updateCatalogSelection();
event.dataTransfer.effectAllowed = "copy";
event.dataTransfer.setData("application/x-loam-crop", id);
event.dataTransfer.setData("text/plain", id);
});
catalogElement.appendChild(button);
}
}
function createSlots() {
slotsElement.innerHTML = "";
for (let index = 0; index < sequence.length; index++) {
const slot = document.createElement("div");
slot.className = "plot-slot";
slot.dataset.index = index;
slot.draggable = true;
slot.tabIndex = 0;
slot.setAttribute("role", "button");
slot.setAttribute("aria-label", `Bed ${index + 1}, empty`);
slot.innerHTML = `
<span class="slot-number">${String(index + 1).padStart(2, "0")}</span>
<span class="slot-icon">+</span>
<span class="slot-name">Empty bed</span>
<span class="slot-rule">Drop a crop here</span>
<button class="clear-slot" type="button" aria-label="Clear bed ${index + 1}">×</button>
`;
slot.addEventListener("click", () => {
if (selectedCrop) {
assignCrop(index, selectedCrop);
} else if (!sequence[index]) {
showToast("Select a crop card first, or drag one into this bed.");
}
});
slot.addEventListener("keydown", event => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
if (selectedCrop) assignCrop(index, selectedCrop);
}
});
slot.addEventListener("dragstart", event => {
if (!sequence[index]) {
event.preventDefault();
return;
}
event.dataTransfer.effectAllowed = "move";
event.dataTransfer.setData("application/x-loam-slot", String(index));
});
slot.addEventListener("dragover", event => {
event.preventDefault();
event.dataTransfer.dropEffect = "move";
slot.classList.add("drag-over");
});
slot.addEventListener("dragleave", () => {
slot.classList.remove("drag-over");
});
slot.addEventListener("drop", event => {
event.preventDefault();
slot.classList.remove("drag-over");
const crop = event.dataTransfer.getData("application/x-loam-crop");
const sourceIndex = event.dataTransfer.getData("application/x-loam-slot");
if (crop && CROP_DATA[crop]) {
assignCrop(index, crop);
} else if (sourceIndex !== "") {
reorderSlot(Number(sourceIndex), index);
}
});
slot.querySelector(".clear-slot").addEventListener("click", event => {
event.stopPropagation();
sequence[index] = null;
planted = false;
updateInterface();
});
slotsElement.appendChild(slot);
}
}
function createResources() {
resourcesElement.innerHTML = "";
for (const [id, resource] of Object.entries(RESOURCE_DATA)) {
const element = document.createElement("div");
element.className = "resource";
element.dataset.resource = id;
element.innerHTML = `
<span class="resource-symbol">${resource.symbol}</span>
<span class="resource-name">${resource.name}</span>
<span class="resource-value"></span>
`;
resourcesElement.appendChild(element);
}
}
function updateCatalogSelection() {
document.querySelectorAll(".crop-card").forEach(card => {
const selected = card.dataset.crop === selectedCrop;
card.classList.toggle("selected", selected);
card.setAttribute("aria-pressed", String(selected));
});
}
function assignedTotals() {
const totals = { sun: 0, water: 0, ore: 0, fiber: 0 };
for (const cropId of sequence) {
if (!cropId) continue;
for (const [resource, amount] of Object.entries(CROP_DATA[cropId].cost)) {
totals[resource] += amount;
}
}
return totals;
}
function predecessorIsLegal(index) {
const cropId = sequence[index];
if (!cropId) return true;
const crop = CROP_DATA[cropId];
if (index === 0) {
return crop.allowedFirst;
}
return crop.accepts.includes(sequence[index - 1]);
}
function expectedPredecessor(cropId) {
const crop = CROP_DATA[cropId];
if (crop.accepts.length === ALL_CROPS.length) {
return "Any predecessor";
}
if (crop.accepts.length === ALL_CROPS.length - 1) {
return "Anything except Emberroot";
}
return crop.accepts.map(id => CROP_DATA[id].name).join(" or ");
}
function updateInterface() {
const totals = assignedTotals();
document.querySelectorAll(".resource").forEach(element => {
const id = element.dataset.resource;
const available = RESOURCE_DATA[id].available;
const remaining = available - totals[id];
const value = element.querySelector(".resource-value");
value.innerHTML = `${remaining} <small>/ ${available}</small>`;
element.classList.toggle("shortage", remaining < 0);
});
document.querySelectorAll(".plot-slot").forEach(slot => {
const index = Number(slot.dataset.index);
const cropId = sequence[index];
const crop = cropId ? CROP_DATA[cropId] : null;
slot.classList.remove("has-crop", "legal", "illegal");
slot.querySelector(".clear-slot").style.display = cropId ? "grid" : "none";
const icon = slot.querySelector(".slot-icon");
const name = slot.querySelector(".slot-name");
const rule = slot.querySelector(".slot-rule");
if (!crop) {
icon.textContent = "+";
icon.style.background = "#aeb9aa";
name.textContent = "Empty bed";
rule.textContent = "Drop a crop here";
rule.className = "slot-rule";
slot.setAttribute("aria-label", `Bed ${index + 1}, empty`);
return;
}
icon.textContent = crop.symbol;
icon.style.background = crop.color;
name.textContent = crop.name;
slot.classList.add("has-crop");
if (predecessorIsLegal(index)) {
slot.classList.add("legal");
rule.className = "slot-rule ok";
rule.textContent = index === 0
? (crop.allowedFirst ? "✓ May begin the sequence" : "")
: `✓ Legal after ${CROP_DATA[sequence[index - 1]].name}`;
} else {
slot.classList.add("illegal");
rule.className = "slot-rule bad";
rule.textContent = index === 0
? `✕ Must follow ${expectedPredecessor(cropId)}`
: `✕ Needs ${expectedPredecessor(cropId)}`;
}
slot.setAttribute(
"aria-label",
`Bed ${index + 1}, ${crop.name}, ${rule.textContent || "ready"}`
);
});
updateCatalogSelection();
updateSceneCrops();
updatePlotLabels();
}
function assignCrop(index, cropId) {
if (!CROP_DATA[cropId]) return;
sequence[index] = cropId;
planted = false;
updateInterface();
if (!predecessorIsLegal(index)) {
showToast(`Bed ${index + 1} does not yet satisfy its predecessor law.`);
}
}
function reorderSlot(sourceIndex, targetIndex) {
if (
sourceIndex === targetIndex ||
Number.isNaN(sourceIndex) ||
Number.isNaN(targetIndex) ||
!sequence[sourceIndex]
) return;
const movedCrop = sequence[sourceIndex];
sequence[sourceIndex] = sequence[targetIndex];
sequence[targetIndex] = movedCrop;
planted = false;
updateInterface();
}
function setFeedback(type, title, text) {
feedbackElement.className = `feedback ${type}`;
feedbackTitle.textContent = title;
feedbackText.textContent = text;
}
function showToast(message) {
toastElement.textContent = message;
toastElement.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toastElement.classList.remove("show"), 2600);
}
function analyzePlan() {
const emptyBeds = [];
const ruleErrors = [];
const totals = assignedTotals();
const shortages = [];
sequence.forEach((cropId, index) => {
if (!cropId) {
emptyBeds.push(index + 1);
return;
}
if (!predecessorIsLegal(index)) {
const crop = CROP_DATA[cropId];
if (index === 0) {
ruleErrors.push(
`Bed 01: ${crop.name} cannot begin a sequence; it requires ${expectedPredecessor(cropId)}.`
);
} else {
ruleErrors.push(
`Bed ${String(index + 1).padStart(2, "0")}: ${crop.name} may only follow ${expectedPredecessor(cropId)}; found ${CROP_DATA[sequence[index - 1]]?.name ?? "an empty bed"}.`
);
}
}
});
for (const [id, resource] of Object.entries(RESOURCE_DATA)) {
if (totals[id] > resource.available) {
shortages.push(
`${resource.name}: need ${totals[id]}, but only ${resource.available} are available.`
);
}
}
return { emptyBeds, ruleErrors, shortages, totals };
}
function calculateScore() {
let yieldScore = 0;
let harmony = 0;
sequence.forEach((cropId, index) => {
if (!cropId) return;
const crop = CROP_DATA[cropId];
yieldScore += crop.yield;
const previous = index > 0 ? sequence[index - 1] : null;
if (cropId === "emberroot" && previous === "sunwheat") harmony += 2;
if (cropId === "mistbean" && previous === "sunwheat") harmony += 2;
if (cropId === "glassreed" && ["emberroot", "mistbean"].includes(previous)) harmony += 2;
if (cropId === "nightmelon" && previous === "glassreed") harmony += 3;
});
return { yieldScore, harmony, total: yieldScore + harmony };
}
function inspectPlan() {
const analysis = analyzePlan();
if (analysis.emptyBeds.length) {
setFeedback(
"error",
"The terrace is incomplete",
`Still empty: bed${analysis.emptyBeds.length > 1 ? "s" : ""} ${analysis.emptyBeds.join(", ")}.`
);
return;
}
if (analysis.ruleErrors.length) {
setFeedback(
"error",
"Predecessor laws failed",
analysis.ruleErrors.join(" ")
);
return;
}
if (analysis.shortages.length) {
setFeedback(
"error",
"Not enough abstract tokens",
analysis.shortages.join(" ")
);
return;
}
planted = true;
const score = calculateScore();
setFeedback(
"success",
"Rotation accepted",
`Game yield ${score.yieldScore} + harmony ${score.harmony} = ${score.total} points. This is an invented arcade score, not a yield forecast.`
);
burstConfetti();
showToast("Plan accepted! The fictional Wardens approve this sequence.");
}
document.querySelector("#plantButton").addEventListener("click", inspectPlan);
document.querySelector("#clearButton").addEventListener("click", () => {
sequence.fill(null);
planted = false;
updateInterface();
setFeedback(
"",
"Awaiting a plan",
"Fill all five beds, then submit the sequence for inspection."
);
});
document.querySelector("#hintButton").addEventListener("click", () => {
const hint = ["sunwheat", "emberroot", "glassreed", "nightmelon", "mistbean"];
hint.forEach((crop, index) => sequence[index] = crop);
planted = false;
updateInterface();
setFeedback(
"",
"Hint loaded",
"This example satisfies the game's invented predecessor laws and token budget. Inspect it when ready."
);
showToast("A legal example has been placed in the beds.");
});
createCatalog();
createResources();
createSlots();
// ---------------------------------------------------------------------
// Three.js low-poly scene.
// ---------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const labelsHost = document.querySelector("#plotLabels");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdbe8e5);
scene.fog = new THREE.Fog(0xdbe8e5, 14, 30);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
sceneHost.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xf6f1df, 0x557865, 2.15);
scene.add(hemisphere);
const sunLight = new THREE.DirectionalLight(0xfff1c7, 3.25);
sunLight.position.set(-6, 10, 7);
sunLight.castShadow = true;
sunLight.shadow.mapSize.set(2048, 2048);
sunLight.shadow.camera.left = -10;
sunLight.shadow.camera.right = 10;
sunLight.shadow.camera.top = 10;
sunLight.shadow.camera.bottom = -10;
sunLight.shadow.camera.near = 1;
sunLight.shadow.camera.far = 30;
sunLight.shadow.bias = -0.0005;
scene.add(sunLight);
const plotPositions = [
new THREE.Vector3(-4.0, 0, 0.62),
new THREE.Vector3(-2.0, 0, 0.14),
new THREE.Vector3( 0.0, 0, 0.00),
new THREE.Vector3( 2.0, 0, 0.14),
new THREE.Vector3( 4.0, 0, 0.62)
];
const bedGroups = [];
const visualCropGroups = Array(5).fill(null);
const labelElements = [];
const animatedCrops = [];
const confetti = [];
const islandGeometry = new THREE.CylinderGeometry(8.55, 9.25, 1.4, 12);
const island = new THREE.Mesh(
islandGeometry,
new THREE.MeshStandardMaterial({
color: 0x769a68,
roughness: 1,
flatShading: true
})
);
island.position.y = -0.86;
island.receiveShadow = true;
island.castShadow = true;
scene.add(island);
const topGeometry = new THREE.CylinderGeometry(8.54, 8.58, 0.12, 12);
const islandTop = new THREE.Mesh(
topGeometry,
new THREE.MeshStandardMaterial({
color: 0x91ad72,
roughness: 1,
flatShading: true
})
);
islandTop.position.y = -0.10;
islandTop.receiveShadow = true;
scene.add(islandTop);
const pathMaterial = new THREE.MeshStandardMaterial({
color: 0xd9c99e,
roughness: 1,
flatShading: true
});
for (let i = 0; i < 12; i++) {
const angle = -1.22 + i * 0.18;
const stone = new THREE.Mesh(
new THREE.CylinderGeometry(0.31, 0.35, 0.08, 6),
pathMaterial
);
stone.position.set(
Math.sin(angle) * 6.35,
-0.015,
Math.cos(angle) * 6.15 + 0.4
);
stone.scale.x = 1 + (i % 3) * 0.12;
stone.rotation.y = i * 0.63;
stone.receiveShadow = true;
scene.add(stone);
}
const pond = new THREE.Mesh(
new THREE.CircleGeometry(1.18, 10),
new THREE.MeshStandardMaterial({
color: 0x68b3bd,
roughness: 0.5,
flatShading: true
})
);
pond.rotation.x = -Math.PI / 2;
pond.scale.set(1.35, 0.72, 1);
pond.position.set(-4.45, -0.015, 3.35);
pond.receiveShadow = true;
scene.add(pond);
function createBed(position, index) {
const group = new THREE.Group();
group.position.copy(position);
const frame = new THREE.Mesh(
new THREE.BoxGeometry(2.22, 0.46, 1.72),
new THREE.MeshStandardMaterial({
color: 0x9a6843,
roughness: 0.95,
flatShading: true
})
);
frame.position.y = 0.05;
frame.castShadow = true;
frame.receiveShadow = true;
group.add(frame);
const soil = new THREE.Mesh(
new THREE.BoxGeometry(1.98, 0.15, 1.48),
new THREE.MeshStandardMaterial({
color: 0x5a3d2b,
roughness: 1,
flatShading: true
})
);
soil.position.y = 0.32;
soil.receiveShadow = true;
group.add(soil);
const trimMaterial = new THREE.MeshStandardMaterial({
color: 0xc28d59,
roughness: 0.95,
flatShading: true
});
for (const x of [-1.02, 1.02]) {
for (const z of [-0.75, 0.75]) {
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.68, 0.17),
trimMaterial
);
post.position.set(x, 0.23, z);
post.castShadow = true;
group.add(post);
const cap = new THREE.Mesh(
new THREE.ConeGeometry(0.16, 0.17, 4),
trimMaterial
);
cap.position.set(x, 0.65, z);
cap.rotation.y = Math.PI / 4;
cap.castShadow = true;
group.add(cap);
}
}
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.25, 0.06),
new THREE.MeshStandardMaterial({
color: index % 2 === 0 ? 0xf2ead0 : 0xe6d8b7,
roughness: 1,
flatShading: true
})
);
marker.position.set(-0.76, 0.68, -0.82);
marker.rotation.x = -0.15;
marker.castShadow = true;
group.add(marker);
scene.add(group);
bedGroups.push(group);
const label = document.createElement("div");
label.className = "plot-label";
label.innerHTML = "<span>01</span><strong>Empty</strong>";
labelsHost.appendChild(label);
labelElements.push(label);
}
plotPositions.forEach(createBed);
function addLowPolyTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, -0.04, z);
tree.scale.setScalar(scale);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.19, 1.15, 5),
new THREE.MeshStandardMaterial({
color: 0x805638,
roughness: 1,
flatShading: true
})
);
trunk.position.y = 0.56;
trunk.castShadow = true;
tree.add(trunk);
const crownColors = [0x4f8155, 0x67955d, 0x3f7350];
for (let i = 0; i < 3; i++) {
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.72 - i * 0.10, 0),
new THREE.MeshStandardMaterial({
color: crownColors[i],
roughness: 1,
flatShading: true
})
);
crown.position.set(
(i - 1) * 0.34,
1.08 + (i === 1 ? 0.28 : 0),
(i % 2) * 0.18
);
crown.scale.set(1.15, 0.9, 1);
crown.castShadow = true;
tree.add(crown);
}
scene.add(tree);
}
addLowPolyTree(-6.2, -2.3, 1.05);
addLowPolyTree(5.8, -2.6, 0.9);
addLowPolyTree(6.4, 2.4, 0.7);
addLowPolyTree(-6.7, 3.5, 0.58);
function addRock(x, z, size, color = 0x87958a) {
const rock = new THREE.Mesh(
new THREE.DodecahedronGeometry(size, 0),
new THREE.MeshStandardMaterial({
color,
roughness: 1,
flatShading: true
})
);
rock.position.set(x, size * 0.42, z);
rock.scale.set(1.25, 0.75, 0.9);
rock.rotation.set(0.2, x, 0.13);
rock.castShadow = true;
scene.add(rock);
}
addRock(-5.7, 1.0, 0.33);
addRock(-6.1, 1.5, 0.22, 0xa0a68a);
addRock(5.2, -0.8, 0.31);
addRock(5.8, -0.2, 0.18, 0x9ba28a);
addRock(1.8, -3.15, 0.27);
const sunDisc = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.05, 2),
new THREE.MeshBasicMaterial({ color: 0xffd979 })
);
sunDisc.position.set(-5.4, 7.5, -7.5);
scene.add(sunDisc);
function createCloud(x, y, z, scale) {
const cloud = new THREE.Group();
cloud.position.set(x, y, z);
cloud.scale.setScalar(scale);
const material = new THREE.MeshStandardMaterial({
color: 0xf5f5e9,
roughness: 1,
flatShading: true
});
[
[-0.8, 0, 0, 0.68],
[0, 0.25, 0, 0.88],
[0.85, 0, 0, 0.62],
[0.25, -0.12, 0.45, 0.58]
].forEach(([px, py, pz, radius]) => {
const puff = new THREE.Mesh(
new THREE.IcosahedronGeometry(radius, 1),
material
);
puff.position.set(px, py, pz);
cloud.add(puff);
});
scene.add(cloud);
}
createCloud(3.2, 7.2, -8.5, 0.8);
createCloud(-0.8, 8.2, -10, 0.55);
// ---------------------------------------------------------------------
// Fictional low-poly crop models.
// ---------------------------------------------------------------------
function standardMaterial(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
flatShading: true,
...extra
});
}
function mesh(geometry, material, position, scale, rotation) {
const object = new THREE.Mesh(geometry, material);
if (position) object.position.set(...position);
if (scale) object.scale.set(...scale);
if (rotation) object.rotation.set(...rotation);
object.castShadow = true;
object.receiveShadow = true;
return object;
}
function buildSunwheat() {
const group = new THREE.Group();
const stemMat = standardMaterial(0x72904b);
const grainMat = standardMaterial(CROP_DATA.sunwheat.color3d);
const leafMat = standardMaterial(CROP_DATA.sunwheat.leaf);
for (let i = 0; i < 5; i++) {
const x = -0.62 + i * 0.31;
const z = Math.sin(i * 2.1) * 0.27;
const height = 0.72 + (i % 3) * 0.11;
group.add(mesh(
new THREE.CylinderGeometry(0.035, 0.055, height, 5),
stemMat,
[x, height / 2, z]
));
group.add(mesh(
new THREE.ConeGeometry(0.14, 0.44, 5),
grainMat,
[x, height + 0.13, z]
));
const leaf = mesh(
new THREE.ConeGeometry(0.105, 0.34, 4),
leafMat,
[x + 0.11, height * 0.52, z],
[1, 1, 0.35],
[0, 0, -1.05]
);
group.add(leaf);
}
return group;
}
function buildEmberroot() {
const group = new THREE.Group();
const rootMat = standardMaterial(CROP_DATA.emberroot.color3d);
const leafMat = standardMaterial(CROP_DATA.emberroot.leaf);
const root = mesh(
new THREE.IcosahedronGeometry(0.48, 0),
rootMat,
[0, 0.32, 0],
[0.92, 1.28, 0.92]
);
group.add(root);
for (let i = 0; i < 6; i++) {
const angle = i * Math.PI / 3;
const leaf = mesh(
new THREE.ConeGeometry(0.17, 0.58, 4),
leafMat,
[Math.cos(angle) * 0.34, 0.51, Math.sin(angle) * 0.34],
[1, 1, 0.42],
[0.25, -angle, 0.35]
);
group.add(leaf);
}
const bud = mesh(
new THREE.DodecahedronGeometry(0.17, 0),
standardMaterial(0xf5c457),
[0.02, 0.89, 0]
);
group.add(bud);
return group;
}
function buildMistbean() {
const group = new THREE.Group();
const bushMat = standardMaterial(CROP_DATA.mistbean.color3d);
const podMat = standardMaterial(0xa8c66a);
const leafMat = standardMaterial(CROP_DATA.mistbean.leaf);
[
[-0.42, 0.30, -0.12],
[0.36, 0.31, -0.18],
[0.02, 0.38, 0.25]
].forEach(([x, y, z]) => {
group.add(mesh(
new THREE.IcosahedronGeometry(0.36, 0),
bushMat,
[x, y, z],
[1.1, 0.85, 1]
));
});
for (let i = 0; i < 4; i++) {
const pod = mesh(
new THREE.CapsuleGeometry(0.10, 0.47, 3, 5),
podMat,
[-0.42 + i * 0.27, 0.64, 0.08 + Math.sin(i) * 0.12],
[1, 1, 0.8],
[0.2, i * 0.7, Math.PI / 2.15]
);
group.add(pod);
}
const leaf = mesh(
new THREE.ConeGeometry(0.22, 0.47, 4),
leafMat,
[0.36, 0.55, -0.16],
[1, 1, 0.35],
[0.4, 0, -0.55]
);
group.add(leaf);
return group;
}
function buildGlassreed() {
const group = new THREE.Group();
const reedMat = standardMaterial(CROP_DATA.glassreed.color3d, {
metalness: 0.08,
roughness: 0.55
});
const paleMat = standardMaterial(0xa7d8d3, {
metalness: 0.12,
roughness: 0.45
});
for (let i = 0; i < 5; i++) {
const x = -0.52 + i * 0.26;
const z = Math.cos(i * 1.8) * 0.25;
const height = 0.78 + (i % 3) * 0.24;
const reed = mesh(
new THREE.ConeGeometry(0.18, height, 5),
i % 2 === 0 ? reedMat : paleMat,
[x, height / 2, z],
[1, 1, 0.75],
[Math.sin(i) * 0.10, i * 0.5, Math.cos(i) * 0.10]
);
group.add(reed);
}
const base = mesh(
new THREE.DodecahedronGeometry(0.30, 0),
standardMaterial(0x4c8c78),
[0, 0.19, 0],
[1.4, 0.55, 1]
);
group.add(base);
return group;
}
function buildNightmelon() {
const group = new THREE.Group();
const melonMat = standardMaterial(CROP_DATA.nightmelon.color3d);
const leafMat = standardMaterial(CROP_DATA.nightmelon.leaf);
const starMat = standardMaterial(0xffdb68, {
emissive: 0x8a5b1f,
emissiveIntensity: 0.25
});
const melon = mesh(
new THREE.IcosahedronGeometry(0.52, 1),
melonMat,
[0, 0.45, 0],
[1, 0.92, 1]
);
group.add(melon);
for (let i = 0; i < 5; i++) {
const angle = i * Math.PI * 2 / 5;
const leaf = mesh(
new THREE.ConeGeometry(0.18, 0.49, 4),
leafMat,
[Math.cos(angle) * 0.32, 0.33, Math.sin(angle) * 0.32],
[1, 1, 0.35],
[0.35, -angle, 0.45]
);
group.add(leaf);
}
for (let i = 0; i < 5; i++) {
const angle = i * Math.PI * 2 / 5;
group.add(mesh(
new THREE.OctahedronGeometry(0.065, 0),
starMat,
[Math.cos(angle) * 0.36, 0.63, Math.sin(angle) * 0.36]
));
}
const stem = mesh(
new THREE.CylinderGeometry(0.045, 0.06, 0.25, 5),
leafMat,
[0, 1.0, 0]
);
group.add(stem);
return group;
}
const cropBuilders = {
sunwheat: buildSunwheat,
emberroot: buildEmberroot,
mistbean: buildMistbean,
glassreed: buildGlassreed,
nightmelon: buildNightmelon
};
function disposeObject(object) {
object.traverse(child => {
if (child.geometry) child.geometry.dispose();
if (child.material) {
const materials = Array.isArray(child.material)
? child.material
: [child.material];
materials.forEach(material => material.dispose());
}
});
}
function updateSceneCrops() {
sequence.forEach((cropId, index) => {
if (visualCropGroups[index]?.userData.cropId === cropId) return;
const oldCrop = visualCropGroups[index];
if (oldCrop) {
bedGroups[index].remove(oldCrop);
disposeObject(oldCrop);
const oldIndex = animatedCrops.indexOf(oldCrop);
if (oldIndex >= 0) animatedCrops.splice(oldIndex, 1);
}
if (!cropId) {
visualCropGroups[index] = null;
return;
}
const crop = cropBuilders[cropId]();
crop.userData.cropId = cropId;
crop.userData.phase = index * 0.8;
crop.userData.born = performance.now();
crop.position.y = 0.39;
bedGroups[index].add(crop);
visualCropGroups[index] = crop;
animatedCrops.push(crop);
});
}
function updatePlotLabels() {
sequence.forEach((cropId, index) => {
const label = labelElements[index];
const crop = cropId ? CROP_DATA[cropId] : null;
label.innerHTML = `
<span>${String(index + 1).padStart(2, "0")}</span>
<strong>${crop ? crop.name : "Empty"}</strong>
`;
});
}
function projectPlotLabels() {
const width = sceneHost.clientWidth;
const height = sceneHost.clientHeight;
plotPositions.forEach((position, index) => {
const projected = new THREE.Vector3(position.x, 2.05, position.z).project(camera);
const visible = projected.z > -1 && projected.z < 1;
labelElements[index].style.opacity = visible ? "1" : "0";
labelElements[index].style.transform = `
translate(${(projected.x * 0.5 + 0.5) * width}px, ${(-projected.y * 0.5 + 0.5) * height}px)
translate(-50%, -100%)
`;
});
}
// ---------------------------------------------------------------------
// Camera controls implemented without external addons.
// ---------------------------------------------------------------------
const cameraState = {
theta: 0.28,
phi: 1.02,
radius: 13.7,
target: new THREE.Vector3(0, 0.35, 0)
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.theta = 0.28;
cameraState.phi = 1.02;
cameraState.radius = 13.7;
updateCamera();
}
document.querySelector("#resetCamera").addEventListener("click", resetCamera);
const pointers = new Map();
let lastPinchDistance = null;
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
const values = [...pointers.values()];
lastPinchDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", event => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.004,
0.48,
1.38
);
} else if (pointers.size === 2) {
const values = [...pointers.values()];
const distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (lastPinchDistance && distance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (lastPinchDistance / distance),
8.2,
19
);
}
lastPinchDistance = distance;
}
updateCamera();
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) lastPinchDistance = null;
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("lostpointercapture", endPointer);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.009,
8.2,
19
);
updateCamera();
}, { passive: false });
// ---------------------------------------------------------------------
// Success effect and animation loop.
// ---------------------------------------------------------------------
function burstConfetti() {
const colors = [0xf0c34e, 0x65a86e, 0x58a9bd, 0x9b78c4, 0xef8c57];
for (let i = 0; i < 70; i++) {
const color = colors[i % colors.length];
const piece = new THREE.Mesh(
new THREE.BoxGeometry(0.09, 0.22, 0.05),
standardMaterial(color)
);
const plot = plotPositions[i % plotPositions.length];
piece.position.set(
plot.x + (Math.random() - 0.5) * 1.5,
2.2 + Math.random() * 1.8,
plot.z + (Math.random() - 0.5) * 1.2
);
piece.rotation.set(Math.random(), Math.random(), Math.random());
piece.castShadow = true;
scene.add(piece);
confetti.push({
mesh: piece,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 2.6,
1.4 + Math.random() * 2.2,
(Math.random() - 0.5) * 2.1
),
spin: new THREE.Vector3(
Math.random() * 6,
Math.random() * 6,
Math.random() * 6
)
});
}
}
function resizeRenderer() {
const width = sceneHost.clientWidth;
const height = sceneHost.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resizeRenderer);
resizeObserver.observe(sceneHost);
resizeRenderer();
updateCamera();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
const now = performance.now();
animatedCrops.forEach(crop => {
const age = now - (crop.userData.born || now);
const grow = Math.min(age / 520, 1);
const overshoot = grow < 1
? 1 + Math.sin(grow * Math.PI) * 0.12
: 1;
crop.scale.setScalar(Math.max(0.02, grow * overshoot));
crop.rotation.z = Math.sin(time * 1.35 + crop.userData.phase) * 0.025;
crop.rotation.x = Math.cos(time * 1.1 + crop.userData.phase) * 0.012;
});
for (let i = confetti.length - 1; i >= 0; i--) {
const item = confetti[i];
item.velocity.y -= 3.5 * 0.016;
item.mesh.position.addScaledVector(item.velocity, 0.016);
item.mesh.rotation.x += item.spin.x * 0.016;
item.mesh.rotation.y += item.spin.y * 0.016;
item.mesh.rotation.z += item.spin.z * 0.016;
if (item.mesh.position.y < -0.3) {
scene.remove(item.mesh);
disposeObject(item.mesh);
confetti.splice(i, 1);
}
}
projectPlotLabels();
renderer.render(scene, camera);
}
updateInterface();
animate();
document.querySelector("#loading").remove();
</script>
</body>
</html>
```