Prompt specification
Build or customize the Three.js 3D scene "Mixed-Material Object Cannot Be Treated as One Substance" 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: Mixed-Material Object Cannot Be Treated as One Substance
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/mixed-material-object-cannot-be-treated-as-one-substan-x3990
Scene Description Prompt:
Separate a low-poly mixed-material package and sort its components using explicit fictional rules. Three.js from cdnjs, one HTML file.
Working Three.js Starter (mixed-material-object-cannot-be-treated-as-one-substan-x3990):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Parcel 07 — Mixed Material Sorter</title>
<style>
:root {
color-scheme: dark;
--bg: #0b1017;
--panel: #111820;
--panel-2: #161f29;
--line: rgba(255, 255, 255, 0.09);
--text: #edf3f7;
--muted: #8c9aa8;
--purple: #a879ff;
--amber: #f2aa55;
--cyan: #58dfe9;
--green: #74e0ad;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
width: 100%;
height: 100%;
display: grid;
grid-template-columns: minmax(0, 1fr) 370px;
}
.stage {
position: relative;
min-width: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 42%, #17212d 0%, #0b1017 66%);
}
#canvas-host,
#world-layer {
position: absolute;
inset: 0;
}
#canvas-host canvas {
width: 100%;
height: 100%;
display: block;
touch-action: none;
}
#world-layer {
pointer-events: none;
overflow: hidden;
}
.stage-head {
position: absolute;
z-index: 4;
top: 0;
left: 0;
right: 0;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
padding: 28px 30px;
pointer-events: none;
background: linear-gradient(
to bottom,
rgba(8, 12, 18, 0.72),
rgba(8, 12, 18, 0)
);
}
.brand {
display: flex;
gap: 14px;
align-items: center;
}
.brand-mark {
width: 44px;
height: 44px;
display: grid;
place-items: center;
border: 1px solid rgba(168, 121, 255, 0.55);
background: rgba(168, 121, 255, 0.09);
color: var(--purple);
font-size: 18px;
font-weight: 800;
clip-path: polygon(
50% 0%,
95% 24%,
95% 76%,
50% 100%,
5% 76%,
5% 24%
);
}
.eyebrow {
margin: 0 0 5px;
color: var(--purple);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 2.2vw, 32px);
line-height: 1;
letter-spacing: -0.045em;
}
.head-right {
display: flex;
align-items: center;
gap: 9px;
pointer-events: auto;
}
.status-pill {
display: flex;
align-items: center;
gap: 9px;
min-height: 38px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(13, 19, 27, 0.8);
backdrop-filter: blur(12px);
color: #dce5eb;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 14px var(--amber);
}
.status-pill[data-phase="unpacked"] .status-dot,
.status-pill[data-phase="sorted"] .status-dot {
background: var(--green);
box-shadow: 0 0 14px var(--green);
}
.icon-button {
width: 38px;
height: 38px;
display: grid;
place-items: center;
padding: 0;
border: 1px solid var(--line);
border-radius: 50%;
background: rgba(13, 19, 27, 0.8);
backdrop-filter: blur(12px);
cursor: pointer;
transition:
border-color 160ms,
background 160ms,
transform 160ms;
}
.icon-button:hover {
border-color: rgba(168, 121, 255, 0.65);
background: rgba(28, 38, 49, 0.95);
transform: translateY(-1px);
}
.stage-foot {
position: absolute;
z-index: 4;
left: 28px;
right: 28px;
bottom: 24px;
display: flex;
justify-content: space-between;
align-items: flex-end;
gap: 20px;
pointer-events: none;
}
.control-hint,
.material-legend {
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 10px;
background: rgba(10, 15, 21, 0.74);
backdrop-filter: blur(10px);
color: var(--muted);
font-size: 10px;
line-height: 1.6;
}
.control-hint strong,
.material-legend strong {
color: #dce5eb;
font-weight: 700;
}
.material-legend {
display: flex;
gap: 13px;
align-items: center;
}
.legend-item {
display: flex;
gap: 6px;
align-items: center;
white-space: nowrap;
}
.swatch {
width: 8px;
height: 8px;
display: inline-block;
border-radius: 50%;
background: var(--swatch);
box-shadow: 0 0 9px color-mix(in srgb, var(--swatch), transparent 35%);
}
.world-label {
position: absolute;
min-width: 112px;
padding: 8px 10px;
transform: translate(-50%, -50%);
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 8px;
background: rgba(10, 15, 21, 0.82);
backdrop-filter: blur(8px);
box-shadow: 0 8px 28px rgba(0, 0, 0, 0.25);
text-align: left;
white-space: nowrap;
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 15px;
background: linear-gradient(rgba(255, 255, 255, 0.2), transparent);
}
.label-code {
display: block;
margin-bottom: 2px;
color: var(--label-color, var(--purple));
font-size: 9px;
font-weight: 900;
letter-spacing: 0.13em;
}
.world-label strong {
display: block;
font-size: 10px;
letter-spacing: 0.04em;
}
.world-label small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.panel {
position: relative;
z-index: 10;
height: 100%;
overflow: hidden;
border-left: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(255, 255, 255, 0.02), transparent 24%),
var(--panel);
box-shadow: -20px 0 60px rgba(0, 0, 0, 0.15);
}
.panel-scroll {
height: 100%;
padding: 25px 23px 20px;
overflow-y: auto;
scrollbar-width: thin;
scrollbar-color: #35414c transparent;
}
.panel-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
margin-bottom: 21px;
}
.panel-head h2 {
margin: 0;
font-size: 18px;
letter-spacing: -0.025em;
}
.panel-head p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.protocol-id {
flex: 0 0 auto;
padding: 5px 7px;
border: 1px solid rgba(168, 121, 255, 0.28);
border-radius: 5px;
color: var(--purple);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
}
.steps {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-bottom: 22px;
}
.step {
position: relative;
min-height: 48px;
padding: 8px 8px 8px 31px;
border: 1px solid var(--line);
border-radius: 8px;
color: #697887;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.step::before {
content: attr(data-number);
position: absolute;
left: 8px;
top: 8px;
width: 17px;
height: 17px;
display: grid;
place-items: center;
border: 1px solid currentColor;
border-radius: 50%;
font-size: 8px;
}
.step.active {
border-color: rgba(168, 121, 255, 0.34);
color: #d8c8ff;
background: rgba(168, 121, 255, 0.06);
}
.step.done {
border-color: rgba(116, 224, 173, 0.25);
color: var(--green);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 10px;
color: #cbd5dc;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-title span {
color: #687684;
font-size: 9px;
font-weight: 700;
letter-spacing: 0;
text-transform: none;
}
.rule-list {
display: grid;
gap: 8px;
}
.rule-card {
position: relative;
display: block;
padding: 12px 13px 12px 39px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.018);
cursor: pointer;
transition:
border-color 160ms,
background 160ms,
opacity 160ms;
}
.rule-card:hover {
border-color: rgba(255, 255, 255, 0.18);
background: rgba(255, 255, 255, 0.03);
}
.rule-card.selected {
border-color: rgba(168, 121, 255, 0.55);
background: rgba(168, 121, 255, 0.075);
}
.rule-card input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.radio-dot {
position: absolute;
left: 13px;
top: 14px;
width: 15px;
height: 15px;
border: 1px solid #566471;
border-radius: 50%;
}
.rule-card.selected .radio-dot {
border-color: var(--purple);
box-shadow: inset 0 0 0 4px var(--panel);
background: var(--purple);
}
.rule-name {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 5px;
color: #e3eaf0;
font-size: 11px;
font-weight: 800;
}
.rule-key {
color: #697887;
font-size: 9px;
font-weight: 700;
}
.rule-description {
color: #8e9ca9;
font-size: 10px;
line-height: 1.55;
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin: 19px 0 23px;
}
.button {
min-height: 43px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 8px;
background: #19232d;
color: #dce5eb;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.045em;
text-transform: uppercase;
cursor: pointer;
transition:
transform 150ms,
border-color 150ms,
background 150ms,
opacity 150ms;
}
.button:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(255, 255, 255, 0.2);
background: #202c37;
}
.button.primary {
border-color: rgba(168, 121, 255, 0.6);
background: var(--purple);
color: #120d1b;
}
.button.primary:hover:not(:disabled) {
border-color: #bc98ff;
background: #b58aff;
}
.button:disabled {
opacity: 0.33;
cursor: not-allowed;
}
.button.full {
grid-column: 1 / -1;
}
.manifest {
overflow: hidden;
border: 1px solid var(--line);
border-radius: 9px;
}
.manifest-head,
.manifest-row {
display: grid;
grid-template-columns: 1fr 78px 49px;
align-items: center;
gap: 7px;
}
.manifest-head {
padding: 8px 10px;
border-bottom: 1px solid var(--line);
background: rgba(255, 255, 255, 0.025);
color: #667582;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.manifest-row {
width: 100%;
padding: 9px 10px;
border: 0;
border-bottom: 1px solid rgba(255, 255, 255, 0.055);
background: transparent;
text-align: left;
cursor: pointer;
}
.manifest-row:last-child {
border-bottom: 0;
}
.manifest-row:hover,
.manifest-row.active {
background: rgba(255, 255, 255, 0.035);
}
.manifest-row.active {
box-shadow: inset 2px 0 var(--purple);
}
.unit {
min-width: 0;
display: flex;
align-items: center;
gap: 7px;
}
.unit-swatch {
width: 7px;
height: 7px;
flex: 0 0 auto;
border-radius: 2px;
background: var(--swatch);
box-shadow: 0 0 8px var(--swatch);
}
.unit-copy {
min-width: 0;
}
.unit-copy strong,
.unit-copy small {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.unit-copy strong {
color: #dce5eb;
font-size: 9px;
font-weight: 800;
}
.unit-copy small {
margin-top: 2px;
color: #667582;
font-size: 8px;
}
.form {
color: #7f8d99;
font-size: 8px;
}
.outcome {
justify-self: end;
min-width: 30px;
padding: 4px 5px;
border: 1px solid color-mix(in srgb, var(--outcome), transparent 50%);
border-radius: 5px;
background: color-mix(in srgb, var(--outcome), transparent 91%);
color: var(--outcome);
font-size: 8px;
font-weight: 900;
text-align: center;
}
.inspection {
margin-top: 14px;
padding: 13px;
border: 1px solid var(--line);
border-radius: 9px;
background:
linear-gradient(135deg, rgba(168, 121, 255, 0.055), transparent 60%),
rgba(255, 255, 255, 0.018);
}
.inspection-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
}
.inspection-head span {
color: #697887;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.inspection-head strong {
color: var(--purple);
font-size: 9px;
letter-spacing: 0.08em;
}
.inspection-name {
margin: 0 0 4px;
font-size: 13px;
font-weight: 800;
}
.inspection-meta {
display: flex;
flex-wrap: wrap;
gap: 5px;
margin-top: 10px;
}
.meta-chip {
padding: 4px 6px;
border: 1px solid var(--line);
border-radius: 5px;
color: #8e9ca9;
font-size: 8px;
}
.inspection-result {
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid var(--line);
color: #aebac4;
font-size: 9px;
line-height: 1.5;
}
.inspection-result b {
color: #e5ecf1;
}
.disclaimer {
margin: 18px 0 0;
color: #596875;
font-size: 9px;
line-height: 1.55;
}
.loading {
position: absolute;
z-index: 8;
inset: 0;
display: grid;
place-items: center;
background: #0b1017;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 250ms;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 900px) {
html,
body {
height: auto;
overflow: auto;
}
.app {
height: auto;
min-height: 100%;
display: flex;
flex-direction: column;
}
.stage {
order: 1;
height: 64vh;
min-height: 480px;
}
.panel {
order: 2;
height: auto;
border-left: 0;
border-top: 1px solid var(--line);
}
.panel-scroll {
height: auto;
overflow: visible;
padding: 22px 18px 30px;
}
.stage-head {
padding: 20px;
}
.stage-foot {
left: 16px;
right: 16px;
bottom: 14px;
}
.material-legend {
display: none;
}
}
@media (max-width: 520px) {
.stage {
height: 58vh;
min-height: 430px;
}
.brand-mark {
width: 36px;
height: 36px;
}
.brand {
gap: 9px;
}
.stage-head {
padding: 15px;
}
.status-pill {
width: 38px;
min-width: 38px;
padding: 0;
justify-content: center;
font-size: 0;
}
.head-right {
gap: 6px;
}
.control-hint {
font-size: 8px;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<main class="app">
<section class="stage" id="stage" aria-label="Interactive 3D sorting scene">
<div id="canvas-host"></div>
<div id="world-layer"></div>
<header class="stage-head">
<div class="brand">
<div class="brand-mark" aria-hidden="true">P</div>
<div>
<p class="eyebrow">Fictional logistics laboratory</p>
<h1>Parcel 07 — Material Sorter</h1>
</div>
</div>
<div class="head-right">
<div
class="status-pill"
id="statusPill"
data-phase="sealed"
aria-live="polite"
>
<span class="status-dot"></span>
<span id="statusText">Sealed · 6 components</span>
</div>
<button
class="icon-button"
id="resetViewButton"
title="Reset camera"
aria-label="Reset camera"
>
↺
</button>
</div>
</header>
<div class="stage-foot">
<div class="control-hint">
<strong>Drag</strong> to orbit · <strong>Scroll</strong> to zoom ·
<strong>Click</strong> a component to inspect
</div>
<div class="material-legend">
<strong>Surface tags</strong>
<span class="legend-item">
<i class="swatch" style="--swatch: #a879ff"></i> Violet
</span>
<span class="legend-item">
<i class="swatch" style="--swatch: #f2aa55"></i> Amber
</span>
<span class="legend-item">
<i class="swatch" style="--swatch: #58dfe9"></i> Cyan
</span>
</div>
</div>
<div class="loading" id="loading">Loading parcel geometry…</div>
</section>
<aside class="panel">
<div class="panel-scroll">
<div class="panel-head">
<div>
<h2>Sort protocol</h2>
<p>Separate the package, select a rule, then route each unit.</p>
</div>
<span class="protocol-id">P-07</span>
</div>
<div class="steps" aria-label="Sorting progress">
<div class="step active" id="stepSeparate" data-number="1">
Separate
</div>
<div class="step" id="stepRule" data-number="2">
Rule
</div>
<div class="step" id="stepRoute" data-number="3">
Route
</div>
</div>
<h3 class="section-title">
Active protocol
<span>Explicit fictional rules</span>
</h3>
<div class="rule-list">
<label class="rule-card selected" data-rule="material">
<input
type="radio"
name="protocol"
value="material"
checked
/>
<span class="radio-dot"></span>
<span class="rule-name">
Material Custody
<span class="rule-key">01 / M</span>
</span>
<span class="rule-description">
Trust the surface tag: Violet → S1, Amber → S2, Cyan → S3.
</span>
</label>
<label class="rule-card" data-rule="shape">
<input type="radio" name="protocol" value="shape" />
<span class="radio-dot"></span>
<span class="rule-name">
Geometry Protocol
<span class="rule-key">02 / G</span>
</span>
<span class="rule-description">
Trust geometry: Faceted → S1, Cylinder → S2, Cone → S3.
</span>
</label>
<label class="rule-card" data-rule="cipher">
<input type="radio" name="protocol" value="cipher" />
<span class="radio-dot"></span>
<span class="rule-name">
Vertex Cipher
<span class="rule-key">03 / V</span>
</span>
<span class="rule-description">
Cyan → S3. Otherwise, over 8 vertices → S2; all remaining → S1.
</span>
</label>
</div>
<div class="actions">
<button class="button" id="unpackButton">Separate package</button>
<button class="button primary" id="sortButton" disabled>
Route units
</button>
<button class="button full" id="resetButton">Reset scenario</button>
</div>
<h3 class="section-title">
Parcel manifest
<span id="manifestRuleName">Material Custody</span>
</h3>
<div class="manifest">
<div class="manifest-head">
<span>Unit</span>
<span>Form</span>
<span>Out</span>
</div>
<div id="manifestRows"></div>
</div>
<div class="inspection">
<div class="inspection-head">
<span>Selected unit</span>
<strong id="inspectionCode">V-01</strong>
</div>
<p class="inspection-name" id="inspectionName">Mica Icosa</p>
<div class="inspection-meta" id="inspectionMeta"></div>
<div class="inspection-result" id="inspectionResult"></div>
</div>
<p class="disclaimer">
All protocols, destination names, material tags, and vertex thresholds
are fictional. Sorting is deterministic and does not simulate physics.
</p>
</div>
</aside>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and application state
// -------------------------------------------------------------------------
const stage = document.querySelector("#stage");
const host = document.querySelector("#canvas-host");
const worldLayer = document.querySelector("#world-layer");
const loading = document.querySelector("#loading");
const statusPill = document.querySelector("#statusPill");
const statusText = document.querySelector("#statusText");
const unpackButton = document.querySelector("#unpackButton");
const sortButton = document.querySelector("#sortButton");
const resetButton = document.querySelector("#resetButton");
const resetViewButton = document.querySelector("#resetViewButton");
const manifestRows = document.querySelector("#manifestRows");
const manifestRuleName = document.querySelector("#manifestRuleName");
const inspectionCode = document.querySelector("#inspectionCode");
const inspectionName = document.querySelector("#inspectionName");
const inspectionMeta = document.querySelector("#inspectionMeta");
const inspectionResult = document.querySelector("#inspectionResult");
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const state = {
phase: "sealed",
rule: "material",
selected: null,
timers: [],
tweens: []
};
const rules = {
material: {
name: "Material Custody",
short: "M",
destinations: {
signal: { label: "VIOLET VAULT", color: 0xa879ff },
core: { label: "BRASS FORGE", color: 0xf2aa55 },
glass: { label: "GLASS ARCHIVE", color: 0x58dfe9 }
},
route(component) {
if (component.material === "violet") return "signal";
if (component.material === "amber") return "core";
return "glass";
}
},
shape: {
name: "Geometry Protocol",
short: "G",
destinations: {
signal: { label: "FACET FIELD", color: 0xa879ff },
core: { label: "ROUND CORE", color: 0xf2aa55 },
glass: { label: "POINT BEACON", color: 0x58dfe9 }
},
route(component) {
if (component.shapeType === "faceted") return "signal";
if (component.shapeType === "cylinder") return "core";
return "glass";
}
},
cipher: {
name: "Vertex Cipher",
short: "V",
destinations: {
signal: { label: "LIGHT BEACON", color: 0xa879ff },
core: { label: "HEAVY CORE", color: 0xf2aa55 },
glass: { label: "GLASS ARCHIVE", color: 0x58dfe9 }
},
route(component) {
if (component.material === "cyan") return "glass";
return component.vertices > 8 ? "core" : "signal";
}
}
};
const componentDefinitions = [
{
id: "V-01",
name: "Mica Icosa",
shape: "Icosahedron",
shapeType: "faceted",
material: "violet",
materialName: "Violet mineral",
vertices: 12,
color: 0xa879ff,
geometry: () =>
new THREE.IcosahedronGeometry(0.43, 0)
},
{
id: "V-02",
name: "Spire Cone",
shape: "Cone",
shapeType: "cone",
material: "violet",
materialName: "Violet mineral",
vertices: 6,
color: 0xa879ff,
geometry: () =>
new THREE.ConeGeometry(0.43, 0.78, 5)
},
{
id: "A-01",
name: "Alloy Octa",
shape: "Octahedron",
shapeType: "faceted",
material: "amber",
materialName: "Amber alloy",
vertices: 6,
color: 0xf2aa55,
geometry: () =>
new THREE.OctahedronGeometry(0.46, 0)
},
{
id: "A-02",
name: "Alloy Drum",
shape: "Cylinder",
shapeType: "cylinder",
material: "amber",
materialName: "Amber alloy",
vertices: 12,
color: 0xf2aa55,
geometry: () =>
new THREE.CylinderGeometry(0.38, 0.38, 0.68, 6)
},
{
id: "G-01",
name: "Glass Dodeca",
shape: "Dodecahedron",
shapeType: "faceted",
material: "cyan",
materialName: "Cyan glass",
vertices: 20,
color: 0x58dfe9,
geometry: () =>
new THREE.DodecahedronGeometry(0.43, 0)
},
{
id: "G-02",
name: "Glass Drum",
shape: "Cylinder",
shapeType: "cylinder",
material: "cyan",
materialName: "Cyan glass",
vertices: 12,
color: 0x58dfe9,
geometry: () =>
new THREE.CylinderGeometry(0.36, 0.36, 0.62, 6)
}
];
const stagePositions = [
new THREE.Vector3(-2.55, 0.66, 3.15),
new THREE.Vector3(-1.53, 0.66, 3.15),
new THREE.Vector3(-0.51, 0.66, 3.15),
new THREE.Vector3(0.51, 0.66, 3.15),
new THREE.Vector3(1.53, 0.66, 3.15),
new THREE.Vector3(2.55, 0.66, 3.15)
];
const packedLocalPositions = [
new THREE.Vector3(-0.9, 0.49, -0.48),
new THREE.Vector3(0.25, 0.49, -0.5),
new THREE.Vector3(0.85, 0.49, -0.36),
new THREE.Vector3(-0.52, 0.49, 0.46),
new THREE.Vector3(0.45, 0.49, 0.4),
new THREE.Vector3(0, 0.49, 0)
];
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1017);
scene.fog = new THREE.Fog(0x0b1017, 18, 38);
const camera = new THREE.PerspectiveCamera(
42,
1,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
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.25;
host.appendChild(renderer.domElement);
const cameraState = {
target: new THREE.Vector3(0, 0.45, -0.15),
radius: window.innerWidth < 900 ? 18.4 : 15.8,
theta: 0.47,
phi: 1.02
};
function updateCamera() {
const { radius, theta, phi, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xc9dcff, 0x18202a, 2.25);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(6, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x9c72ff, 1.7);
rimLight.position.set(-7, 5, -6);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa64d, 18, 13, 2);
warmLight.position.set(4, 5, 3);
scene.add(warmLight);
const cyanLight = new THREE.PointLight(0x42dce9, 14, 12, 2);
cyanLight.position.set(-5, 3, -4);
scene.add(cyanLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x0d141c,
roughness: 0.94,
metalness: 0.08
})
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(36, 36, 0x34414e, 0x202a34);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.25;
scene.add(grid);
// Package platform.
const packagePlatform = new THREE.Mesh(
new THREE.CylinderGeometry(3.35, 3.7, 0.15, 6),
new THREE.MeshStandardMaterial({
color: 0x141d26,
roughness: 0.76,
metalness: 0.22,
flatShading: true
})
);
packagePlatform.position.set(0, 0.075, 0.75);
packagePlatform.receiveShadow = true;
scene.add(packagePlatform);
// Low-poly cargo case.
const packageGroup = new THREE.Group();
packageGroup.position.z = 0.75;
scene.add(packageGroup);
const caseMaterial = new THREE.MeshStandardMaterial({
color: 0x202b35,
roughness: 0.58,
metalness: 0.4,
flatShading: true
});
const caseInsetMaterial = new THREE.MeshStandardMaterial({
color: 0x0e151c,
roughness: 0.86,
metalness: 0.12
});
const strapMaterial = new THREE.MeshStandardMaterial({
color: 0xc68843,
roughness: 0.36,
metalness: 0.68,
flatShading: true
});
function addBox(parent, size, position, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
addBox(
packageGroup,
new THREE.Vector3(3.45, 0.22, 2.35),
new THREE.Vector3(0, 0.18, 0),
caseMaterial
);
addBox(
packageGroup,
new THREE.Vector3(3.12, 0.08, 2.02),
new THREE.Vector3(0, 0.31, 0),
caseInsetMaterial
);
addBox(
packageGroup,
new THREE.Vector3(3.45, 0.56, 0.18),
new THREE.Vector3(0, 0.52, 1.09),
caseMaterial
);
addBox(
packageGroup,
new THREE.Vector3(3.45, 0.56, 0.18),
new THREE.Vector3(0, 0.52, -1.09),
caseMaterial
);
addBox(
packageGroup,
new THREE.Vector3(0.18, 0.56, 2.05),
new THREE.Vector3(-1.64, 0.52, 0),
caseMaterial
);
addBox(
packageGroup,
new THREE.Vector3(0.18, 0.56, 2.05),
new THREE.Vector3(1.64, 0.52, 0),
caseMaterial
);
// Small case corner protectors.
for (const x of [-1.57, 1.57]) {
for (const z of [-1.02, 1.02]) {
const corner = new THREE.Mesh(
new THREE.BoxGeometry(0.27, 0.62, 0.27),
strapMaterial
);
corner.position.set(x, 0.52, z);
corner.castShadow = true;
packageGroup.add(corner);
}
}
const lidHinge = new THREE.Group();
lidHinge.position.set(0, 1.02, -1.1);
packageGroup.add(lidHinge);
const lidGroup = new THREE.Group();
lidGroup.position.z = 1.1;
lidHinge.add(lidGroup);
addBox(
lidGroup,
new THREE.Vector3(3.55, 0.3, 2.42),
new THREE.Vector3(0, 0.1, 0),
caseMaterial
);
addBox(
lidGroup,
new THREE.Vector3(3.08, 0.06, 1.98),
new THREE.Vector3(0, 0.28, 0),
caseInsetMaterial
);
for (const x of [-0.92, 0.92]) {
addBox(
lidGroup,
new THREE.Vector3(0.13, 0.055, 2.42),
new THREE.Vector3(x, 0.33, 0),
strapMaterial
);
}
addBox(
lidGroup,
new THREE.Vector3(0.58, 0.09, 0.25),
new THREE.Vector3(0, 0.36, 0.68),
strapMaterial
);
// Destination bins.
const destinations = [
{
id: "signal",
slot: "S1",
position: new THREE.Vector3(-4.65, 0, -2.75)
},
{
id: "core",
slot: "S2",
position: new THREE.Vector3(0, 0, -3.95)
},
{
id: "glass",
slot: "S3",
position: new THREE.Vector3(4.65, 0, -2.75)
}
];
for (const destination of destinations) {
const group = new THREE.Group();
group.position.copy(destination.position);
scene.add(group);
destination.group = group;
const floor = new THREE.Mesh(
new THREE.CylinderGeometry(0.91, 0.77, 0.15, 8),
new THREE.MeshStandardMaterial({
color: 0x18222c,
roughness: 0.7,
metalness: 0.35,
flatShading: true
})
);
floor.position.y = 0.09;
floor.receiveShadow = true;
group.add(floor);
const wall = new THREE.Mesh(
new THREE.CylinderGeometry(0.98, 0.91, 0.64, 8, 1, true),
new THREE.MeshStandardMaterial({
color: 0x1c2731,
roughness: 0.52,
metalness: 0.42,
side: THREE.DoubleSide,
flatShading: true
})
);
wall.position.y = 0.45;
wall.castShadow = true;
wall.receiveShadow = true;
group.add(wall);
destination.wall = wall;
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.96, 0.055, 4, 8),
new THREE.MeshStandardMaterial({
color: 0xa879ff,
emissive: 0xa879ff,
emissiveIntensity: 0.35,
roughness: 0.34,
metalness: 0.56
})
);
rim.rotation.x = Math.PI / 2;
rim.position.y = 0.78;
rim.castShadow = true;
group.add(rim);
destination.rim = rim;
const centerMark = new THREE.Mesh(
new THREE.CircleGeometry(0.46, 8),
new THREE.MeshBasicMaterial({
color: 0xa879ff,
transparent: true,
opacity: 0.08,
depthWrite: false,
side: THREE.DoubleSide
})
);
centerMark.rotation.x = -Math.PI / 2;
centerMark.position.y = 0.17;
group.add(centerMark);
destination.centerMark = centerMark;
const connectorGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0.025, 0.75),
destination.position.clone().setY(0.025)
]);
const connector = new THREE.Line(
connectorGeometry,
new THREE.LineDashedMaterial({
color: 0xa879ff,
dashSize: 0.22,
gapSize: 0.14,
transparent: true,
opacity: 0.16
})
);
connector.computeLineDistances();
scene.add(connector);
destination.connector = connector;
const label = document.createElement("div");
label.className = "world-label";
worldLayer.appendChild(label);
destination.label = label;
}
// Components.
const components = [];
const clickMeshes = [];
function createComponentMaterial(key, color) {
if (key === "violet") {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.36,
metalness: 0.14,
flatShading: true
});
}
if (key === "amber") {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.23,
metalness: 0.76,
flatShading: true
});
}
return new THREE.MeshPhysicalMaterial({
color,
roughness: 0.12,
metalness: 0.02,
transmission: 0.22,
thickness: 0.45,
transparent: true,
opacity: 0.78,
side: THREE.DoubleSide,
flatShading: true
});
}
componentDefinitions.forEach((definition, index) => {
const group = new THREE.Group();
const material = createComponentMaterial(
definition.material,
definition.color
);
const mesh = new THREE.Mesh(definition.geometry(), material);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 18),
new THREE.LineBasicMaterial({
color: definition.color,
transparent: true,
opacity: definition.material === "cyan" ? 0.7 : 0.38,
depthWrite: false
})
);
mesh.add(edge);
const component = {
...definition,
index,
group,
mesh,
edge,
packed: packedLocalPositions[index]
.clone()
.add(packageGroup.position),
stage: stagePositions[index].clone(),
stageRotation: new THREE.Quaternion().setFromEuler(
new THREE.Euler(0, index * 0.72, 0)
)
};
mesh.userData.component = component;
group.position.copy(component.packed);
group.quaternion.copy(component.stageRotation);
scene.add(group);
components.push(component);
clickMeshes.push(mesh);
});
// Ground marker for the selected component.
const selectionMarker = new THREE.Mesh(
new THREE.TorusGeometry(0.58, 0.022, 4, 32),
new THREE.MeshBasicMaterial({
color: 0xa879ff,
transparent: true,
opacity: 0.7,
depthWrite: false
})
);
selectionMarker.rotation.x = Math.PI / 2;
selectionMarker.visible = false;
scene.add(selectionMarker);
// -------------------------------------------------------------------------
// Tweening
// -------------------------------------------------------------------------
function clearActionTimers() {
for (const timer of state.timers) clearTimeout(timer);
state.timers.length = 0;
}
function clearComponentTweens() {
state.tweens = state.tweens.filter(
(tween) => tween.type !== "component"
);
}
function easeInOutCubic(value) {
return value < 0.5
? 4 * value * value * value
: 1 - Math.pow(-2 * value + 2, 3) / 2;
}
function moveComponent(
component,
destination,
rotation,
duration,
delay = 0,
arc = 1.8
) {
state.tweens.push({
type: "component",
component,
startPosition: component.group.position.clone(),
endPosition: destination.clone(),
startRotation: component.group.quaternion.clone(),
endRotation: rotation.clone(),
startTime: performance.now() + delay,
duration: reducedMotion ? 1 : duration,
arc: reducedMotion ? 0 : arc
});
}
function moveLid(toAngle, duration, delay = 0) {
state.tweens.push({
type: "lid",
startAngle: lidHinge.rotation.x,
endAngle: toAngle,
startTime: performance.now() + delay,
duration: reducedMotion ? 1 : duration
});
}
function updateTweens(now) {
for (let index = state.tweens.length - 1; index >= 0; index--) {
const tween = state.tweens[index];
if (now < tween.startTime) continue;
const progress = THREE.MathUtils.clamp(
(now - tween.startTime) / tween.duration,
0,
1
);
const eased = easeInOutCubic(progress);
if (tween.type === "component") {
tween.component.group.position.lerpVectors(
tween.startPosition,
tween.endPosition,
eased
);
tween.component.group.position.y +=
Math.sin(progress * Math.PI) * tween.arc;
tween.component.group.quaternion.slerpQuaternions(
tween.startRotation,
tween.endRotation,
eased
);
} else if (tween.type === "lid") {
lidHinge.rotation.x = THREE.MathUtils.lerp(
tween.startAngle,
tween.endAngle,
eased
);
}
if (progress >= 1) {
state.tweens.splice(index, 1);
}
}
}
// -------------------------------------------------------------------------
// UI and routing logic
// -------------------------------------------------------------------------
function getRule() {
return rules[state.rule];
}
function destinationFor(component) {
return getRule().route(component);
}
function destinationById(id) {
return destinations.find((destination) => destination.id === id);
}
function applyRuleVisuals() {
const rule = getRule();
for (const destination of destinations) {
const definition = rule.destinations[destination.id];
const color = new THREE.Color(definition.color);
destination.rim.material.color.copy(color);
destination.rim.material.emissive.copy(color);
destination.centerMark.material.color.copy(color);
destination.connector.material.color.copy(color);
destination.wall.material.color.copy(color).multiplyScalar(0.14);
}
refreshDestinationLabels();
}
function refreshDestinationLabels() {
const rule = getRule();
for (const destination of destinations) {
const definition = rule.destinations[destination.id];
const members = components.filter(
(component) => destinationFor(component) === destination.id
);
const countLabel =
state.phase === "sorted"
? `${members.length} received`
: `${members.length} predicted`;
destination.label.style.setProperty(
"--label-color",
`#${definition.color.toString(16).padStart(6, "0")}`
);
destination.label.innerHTML = `
<span class="label-code">${destination.slot} / ${
definition.label
}</span>
<strong>${countLabel}</strong>
<small>${rule.name}</small>
`;
}
}
function renderManifest() {
const rule = getRule();
manifestRows.innerHTML = "";
manifestRuleName.textContent = rule.name;
for (const component of components) {
const destination = destinationById(destinationFor(component));
const definition = rule.destinations[destination.id];
const color = `#${definition.color.toString(16).padStart(6, "0")}`;
const row = document.createElement("button");
row.className =
"manifest-row" +
(state.selected === component ? " active" : "");
row.type = "button";
row.title = `${component.name} → ${definition.label}`;
row.innerHTML = `
<span class="unit">
<i
class="unit-swatch"
style="--swatch:${color}"
></i>
<span class="unit-copy">
<strong>${component.id} · ${component.name}</strong>
<small>${component.materialName}</small>
</span>
</span>
<span class="form">${component.shape}</span>
<span
class="outcome"
style="--outcome:${color}"
>${destination.slot}</span>
`;
row.addEventListener("click", () => selectComponent(component));
manifestRows.appendChild(row);
}
}
function selectComponent(component) {
state.selected = component;
selectionMarker.visible = state.phase !== "sealed";
selectionMarker.material.color.setHex(component.color);
renderManifest();
renderInspection();
}
function renderInspection() {
const component = state.selected;
if (!component) return;
const rule = getRule();
const destination = destinationById(destinationFor(component));
const definition = rule.destinations[destination.id];
inspectionCode.textContent = component.id;
inspectionName.textContent = component.name;
inspectionMeta.innerHTML = `
<span class="meta-chip">${component.materialName}</span>
<span class="meta-chip">${component.shape}</span>
<span class="meta-chip">${component.vertices} vertices</span>
`;
inspectionResult.innerHTML = `
Under <b>${rule.name}</b>, this unit routes to
<b>${destination.slot} · ${definition.label}</b>.
`;
}
function updateProgress() {
const stepSeparate = document.querySelector("#stepSeparate");
const stepRule = document.querySelector("#stepRule");
const stepRoute = document.querySelector("#stepRoute");
stepSeparate.className = "step";
stepRule.className = "step";
stepRoute.className = "step";
if (["unpacking", "unpacked", "sorted"].includes(state.phase)) {
stepSeparate.classList.add("done");
}
if (state.phase === "sealed") {
stepSeparate.classList.add("active");
} else if (state.phase === "unpacking") {
stepSeparate.classList.add("active");
}
if (state.phase === "unpacked") {
stepRule.classList.add("active");
}
if (state.phase === "sorting") {
stepRule.classList.add("done");
stepRoute.classList.add("active");
}
if (state.phase === "sorted") {
stepRule.classList.add("done");
stepRoute.classList.add("done");
}
}
function updateUI() {
const labels = {
sealed: "Sealed · 6 components",
unpacking: "Separating · opening case",
unpacked: "Unpacked · 6 units staged",
sorting: "Routing · applying protocol",
sorted: "Sorted · all units routed"
};
statusPill.dataset.phase = state.phase;
statusText.textContent = labels[state.phase];
unpackButton.disabled = state.phase !== "sealed";
sortButton.disabled = state.phase !== "unpacked";
unpackButton.textContent =
state.phase === "sealed" ? "Separate package" : "Separated";
sortButton.textContent =
state.phase === "sorted" ? "Units routed" : "Route units";
const protocolLocked =
state.phase === "unpacking" || state.phase === "sorting";
document
.querySelectorAll('input[name="protocol"]')
.forEach((input) => {
input.disabled = protocolLocked;
});
document.querySelectorAll(".rule-card").forEach((card) => {
card.classList.toggle(
"selected",
card.dataset.rule === state.rule
);
card.style.opacity = protocolLocked ? "0.58" : "1";
});
updateProgress();
refreshDestinationLabels();
renderManifest();
renderInspection();
}
function separatePackage() {
if (state.phase !== "sealed") return;
clearActionTimers();
clearComponentTweens();
state.phase = "unpacking";
updateUI();
moveLid(-1.43, 950);
components.forEach((component, index) => {
moveComponent(
component,
component.stage,
component.stageRotation,
900,
250 + index * 80,
2.15
);
});
state.timers.push(
setTimeout(() => {
state.phase = "unpacked";
selectionMarker.visible = true;
updateUI();
}, 1750)
);
}
function routeComponents() {
if (state.phase !== "unpacked") return;
clearActionTimers();
clearComponentTweens();
state.phase = "sorting";
updateUI();
const destinationsForComponents = new Map(
destinations.map((destination) => [destination.id, []])
);
for (const component of components) {
destinationsForComponents
.get(destinationFor(component))
.push(component);
}
const offsets = [
new THREE.Vector3(-0.29, 0, -0.06),
new THREE.Vector3(0.29, 0, -0.06),
new THREE.Vector3(0, 0, 0.33)
];
components.forEach((component, index) => {
const destinationId = destinationFor(component);
const destination = destinationById(destinationId);
const members = destinationsForComponents.get(destinationId);
const memberIndex = members.indexOf(component);
const endPosition = destination.position
.clone()
.add(offsets[memberIndex] || offsets[0]);
endPosition.y = 0.72;
const endRotation = new THREE.Quaternion().setFromEuler(
new THREE.Euler(
component.shapeType === "cylinder" ? 0 : index * 0.24,
index * 0.81,
component.shapeType === "cylinder" ? 0 : 0.12
)
);
moveComponent(
component,
endPosition,
endRotation,
1120,
index * 125,
2.8
);
});
state.timers.push(
setTimeout(() => {
state.phase = "sorted";
updateUI();
}, 2150)
);
}
function resetScenario() {
clearActionTimers();
state.tweens.length = 0;
state.phase = "sealed";
lidHinge.rotation.x = 0;
for (const component of components) {
component.group.position.copy(component.packed);
component.group.quaternion.copy(component.stageRotation);
}
selectionMarker.visible = false;
updateUI();
}
function selectRule(ruleId) {
if (
state.phase === "unpacking" ||
state.phase === "sorting"
) {
return;
}
state.rule = ruleId;
const radio = document.querySelector(
`input[name="protocol"][value="${ruleId}"]`
);
if (radio) radio.checked = true;
applyRuleVisuals();
if (state.phase === "sorted") {
clearActionTimers();
clearComponentTweens();
state.phase = "unpacking";
components.forEach((component, index) => {
moveComponent(
component,
component.stage,
component.stageRotation,
650,
index * 65,
1.35
);
});
state.timers.push(
setTimeout(() => {
state.phase = "unpacked";
updateUI();
}, 1050)
);
}
updateUI();
}
unpackButton.addEventListener("click", separatePackage);
sortButton.addEventListener("click", routeComponents);
resetButton.addEventListener("click", resetScenario);
document.querySelectorAll(".rule-card").forEach((card) => {
card.addEventListener("click", () => {
selectRule(card.dataset.rule);
});
});
// -------------------------------------------------------------------------
// Direct camera and selection controls
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pointerStart = new THREE.Vector2();
const pointerLast = new THREE.Vector2();
let pointerIsDown = false;
let pointerMoved = false;
let hoveredComponent = null;
function setPointerFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pickComponent(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(clickMeshes, false);
return hits.length ? hits[0].object.userData.component : null;
}
function setHovered(component) {
if (hoveredComponent === component) return;
if (hoveredComponent) {
hoveredComponent.edge.material.opacity =
hoveredComponent.material === "cyan" ? 0.7 : 0.38;
}
hoveredComponent = component;
if (hoveredComponent) {
hoveredComponent.edge.material.opacity = 1;
renderer.domElement.style.cursor = "pointer";
} else {
renderer.domElement.style.cursor = "grab";
}
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerIsDown = true;
pointerMoved = false;
pointerStart.set(event.clientX, event.clientY);
pointerLast.copy(pointerStart);
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerIsDown) {
const dx = event.clientX - pointerLast.x;
const dy = event.clientY - pointerLast.y;
pointerLast.set(event.clientX, event.clientY);
if (
Math.abs(event.clientX - pointerStart.x) +
Math.abs(event.clientY - pointerStart.y) >
4
) {
pointerMoved = true;
}
cameraState.theta -= dx * 0.0055;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.0045,
0.48,
1.36
);
updateCamera();
} else {
setHovered(pickComponent(event));
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointerIsDown) return;
pointerIsDown = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (!pointerMoved) {
const component = pickComponent(event);
if (component) selectComponent(component);
}
renderer.domElement.style.cursor = hoveredComponent
? "pointer"
: "grab";
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!pointerIsDown) setHovered(null);
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.009,
9,
25
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
function resetCamera() {
cameraState.radius =
stage.clientWidth < 650 ? 18.4 : 15.8;
cameraState.theta = 0.47;
cameraState.phi = 1.02;
updateCamera();
}
resetViewButton.addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
const tag = event.target.tagName;
if (["INPUT", "BUTTON", "TEXTAREA"].includes(tag)) return;
const key = event.key.toLowerCase();
if (key === "u") separatePackage();
if (key === "s") routeComponents();
if (key === "x") resetScenario();
if (key === "c") resetCamera();
if (key === "1") selectRule("material");
if (key === "2") selectRule("shape");
if (key === "3") selectRule("cipher");
if (event.key === "ArrowLeft") {
cameraState.theta -= 0.08;
updateCamera();
}
if (event.key === "ArrowRight") {
cameraState.theta += 0.08;
updateCamera();
}
if (event.key === "ArrowUp") {
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - 0.06,
0.48,
1.36
);
updateCamera();
}
if (event.key === "ArrowDown") {
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + 0.06,
0.48,
1.36
);
updateCamera();
}
});
// -------------------------------------------------------------------------
// Projection, resizing, and render loop
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function placeWorldLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * stage.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * stage.clientHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function resize() {
const width = host.clientWidth;
const height = host.clientHeight;
if (!width || !height) return;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(host);
resize();
function animate(now) {
requestAnimationFrame(animate);
updateTweens(now);
if (state.selected && state.phase !== "sealed") {
selectionMarker.position.set(
state.selected.group.position.x,
0.035,
state.selected.group.position.z
);
const pulse = 1 + Math.sin(now * 0.004) * 0.055;
selectionMarker.scale.setScalar(pulse);
}
for (const destination of destinations) {
placeWorldLabel(
destination.label,
destination.position.clone().add(new THREE.Vector3(0, 1.52, 0))
);
}
renderer.render(scene, camera);
}
state.selected = components[0];
applyRuleVisuals();
updateUI();
requestAnimationFrame(animate);
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 300);
});
</script>
</body>
</html>
```