Prompt specification
Build or customize the Three.js 3D scene "Ratio consistency challenge" 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: Ratio consistency challenge
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/ratio-consistency-challenge-x1732
Scene Description Prompt:
Match a target ratio with voxel colored groups, demonstrating different totals and equivalent proportions. Use Three.js from cdnjs to create one HTML file.
Working Three.js Starter (ratio-consistency-challenge-x1732):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#0a1020" />
<title>Voxel Ratio Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #080d18;
--panel: #111a2c;
--panel-soft: #172239;
--line: rgba(255, 255, 255, 0.09);
--text: #f5f8ff;
--muted: #95a3bd;
--coral: #ff6b6b;
--coral-soft: rgba(255, 107, 107, 0.14);
--teal: #39d7c5;
--teal-soft: rgba(57, 215, 197, 0.13);
--gold: #ffd166;
--success: #70e6a5;
--danger: #ff7d8e;
}
* {
box-sizing: border-box;
}
html,
body {
min-height: 100%;
margin: 0;
}
body {
background:
radial-gradient(circle at 12% 10%, rgba(76, 99, 180, 0.17), transparent 28rem),
radial-gradient(circle at 88% 85%, rgba(28, 185, 166, 0.10), transparent 28rem),
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: min(1500px, 100%);
min-height: 100vh;
margin: 0 auto;
padding: 22px;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
margin-bottom: 18px;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
}
.brand-mark {
position: relative;
width: 48px;
height: 48px;
flex: 0 0 auto;
}
.brand-mark span {
position: absolute;
width: 22px;
height: 22px;
border-radius: 6px;
transform: rotate(-8deg);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}
.brand-mark span:nth-child(1) {
left: 1px;
top: 2px;
background: var(--coral);
}
.brand-mark span:nth-child(2) {
right: 1px;
bottom: 1px;
background: var(--teal);
}
.brand h1 {
margin: 0;
font-size: clamp(21px, 3vw, 30px);
line-height: 1.05;
letter-spacing: -0.04em;
}
.brand p {
margin: 5px 0 0;
color: var(--muted);
font-size: 13px;
}
.target-pill {
display: flex;
align-items: center;
gap: 12px;
min-width: 178px;
padding: 10px 14px;
border: 1px solid rgba(255, 209, 102, 0.22);
border-radius: 14px;
background: rgba(255, 209, 102, 0.08);
}
.target-pill small {
display: block;
color: #c7b98e;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.target-pill strong {
display: block;
margin-top: 2px;
color: var(--gold);
font-size: 21px;
letter-spacing: 0.08em;
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1fr) 380px;
gap: 18px;
min-height: 700px;
}
.stage {
position: relative;
min-height: 700px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 24px;
background:
linear-gradient(180deg, rgba(255, 255, 255, 0.025), transparent 30%),
#0c1424;
box-shadow: 0 28px 70px rgba(0, 0, 0, 0.25);
}
#scene {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
#scene:focus-visible {
outline: 2px solid var(--teal);
outline-offset: -4px;
}
.stage-header {
position: absolute;
z-index: 2;
top: 18px;
left: 18px;
right: 18px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.stage-title {
padding: 11px 14px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 13px;
background: rgba(7, 12, 24, 0.68);
backdrop-filter: blur(12px);
}
.stage-title strong {
display: block;
font-size: 13px;
}
.stage-title span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 11px;
}
.view-button {
pointer-events: auto;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 12px;
background: rgba(7, 12, 24, 0.74);
cursor: pointer;
backdrop-filter: blur(12px);
transition:
border-color 160ms ease,
transform 160ms ease,
background 160ms ease;
}
.view-button:hover {
border-color: rgba(57, 215, 197, 0.45);
background: rgba(57, 215, 197, 0.11);
transform: translateY(-1px);
}
.stage-footer {
position: absolute;
z-index: 2;
right: 18px;
bottom: 18px;
left: 18px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 16px;
pointer-events: none;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 10px;
background: rgba(7, 12, 24, 0.7);
color: #c7d0e1;
font-size: 11px;
backdrop-filter: blur(10px);
}
.legend-dot {
width: 9px;
height: 9px;
border-radius: 3px;
}
.legend-dot.coral {
background: var(--coral);
box-shadow: 0 0 14px rgba(255, 107, 107, 0.55);
}
.legend-dot.teal {
background: var(--teal);
box-shadow: 0 0 14px rgba(57, 215, 197, 0.55);
}
.gesture-hint {
color: #73819a;
font-size: 11px;
text-align: right;
}
.panel {
display: flex;
flex-direction: column;
gap: 16px;
padding: 20px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 24px;
background: rgba(17, 26, 44, 0.93);
box-shadow: 0 28px 70px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(18px);
}
.panel.shake {
animation: shake 350ms ease;
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
25% { transform: translateX(-7px); }
50% { transform: translateX(6px); }
75% { transform: translateX(-3px); }
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.eyebrow {
margin: 0;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.section-heading h2 {
margin: 4px 0 0;
font-size: 20px;
letter-spacing: -0.025em;
}
.ratio-badge {
padding: 7px 10px;
border: 1px solid rgba(255, 209, 102, 0.2);
border-radius: 10px;
background: rgba(255, 209, 102, 0.08);
color: var(--gold);
font-size: 18px;
font-weight: 800;
letter-spacing: 0.08em;
}
.preset-list {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
.preset {
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
color: var(--muted);
cursor: pointer;
font-size: 12px;
font-weight: 700;
transition: 150ms ease;
}
.preset:hover,
.preset.active {
border-color: rgba(255, 209, 102, 0.35);
background: rgba(255, 209, 102, 0.1);
color: var(--gold);
}
.divider {
height: 1px;
background: var(--line);
}
.count-card {
padding: 14px;
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(255, 255, 255, 0.022);
}
.count-card.coral {
box-shadow: inset 3px 0 0 var(--coral);
}
.count-card.teal {
box-shadow: inset 3px 0 0 var(--teal);
}
.count-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.count-name {
display: flex;
align-items: center;
gap: 9px;
font-size: 13px;
font-weight: 800;
}
.count-name i {
width: 11px;
height: 11px;
border-radius: 4px;
}
.coral .count-name i {
background: var(--coral);
box-shadow: 0 0 12px rgba(255, 107, 107, 0.45);
}
.teal .count-name i {
background: var(--teal);
box-shadow: 0 0 12px rgba(57, 215, 197, 0.45);
}
.stepper {
display: grid;
grid-template-columns: 32px 44px 32px;
align-items: center;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(4, 9, 18, 0.38);
}
.stepper button {
height: 32px;
padding: 0;
border: 0;
background: transparent;
color: #dbe4f5;
cursor: pointer;
font-size: 18px;
transition: background 140ms ease;
}
.stepper button:hover {
background: rgba(255, 255, 255, 0.08);
}
.stepper output {
text-align: center;
font-size: 17px;
font-weight: 800;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 17px 0 2px;
border-radius: 999px;
accent-color: var(--accent);
cursor: pointer;
}
.ratio-card {
padding: 16px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 16px;
background: linear-gradient(145deg, rgba(255, 255, 255, 0.045), rgba(255, 255, 255, 0.015));
}
.ratio-card-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
}
.current-ratio {
display: flex;
align-items: center;
gap: 9px;
font-size: 29px;
font-weight: 850;
letter-spacing: 0.06em;
}
.current-ratio .coral-text {
color: var(--coral);
}
.current-ratio .teal-text {
color: var(--teal);
}
.current-ratio .separator {
color: #526079;
}
.fraction-line {
width: 28px;
height: 3px;
border-radius: 3px;
background: #64718a;
}
.cross-product {
display: grid;
grid-template-columns: 1fr auto 1fr;
gap: 8px;
align-items: center;
margin-top: 14px;
color: var(--muted);
font-size: 11px;
text-align: center;
}
.cross-product span {
padding: 7px;
border-radius: 8px;
background: rgba(0, 0, 0, 0.16);
}
.status {
display: flex;
align-items: flex-start;
gap: 10px;
min-height: 53px;
margin-top: 12px;
padding: 11px 12px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.status-icon {
display: grid;
width: 24px;
height: 24px;
flex: 0 0 auto;
place-items: center;
border-radius: 50%;
background: rgba(255, 255, 255, 0.07);
color: var(--muted);
font-size: 12px;
font-weight: 900;
}
.status strong {
display: block;
font-size: 12px;
}
.status p {
margin: 3px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.status.success {
border-color: rgba(112, 230, 165, 0.22);
background: rgba(112, 230, 165, 0.07);
}
.status.success .status-icon {
background: rgba(112, 230, 165, 0.14);
color: var(--success);
}
.status.success strong {
color: var(--success);
}
.status.pending .status-icon {
color: var(--gold);
}
.primary-button {
width: 100%;
padding: 12px 16px;
border: 0;
border-radius: 12px;
background: linear-gradient(135deg, #6be6d7, #3bbec1);
box-shadow: 0 10px 24px rgba(37, 198, 190, 0.18);
color: #062323;
cursor: pointer;
font-weight: 850;
transition:
transform 150ms ease,
filter 150ms ease;
}
.primary-button:hover {
filter: brightness(1.08);
transform: translateY(-1px);
}
.primary-button:active {
transform: translateY(0);
}
.examples {
min-height: 0;
}
.examples h3 {
margin: 0 0 4px;
font-size: 14px;
}
.examples > p {
margin: 0 0 11px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.example-list {
display: grid;
gap: 8px;
}
.example-card {
display: grid;
grid-template-columns: 1fr auto;
gap: 12px;
align-items: center;
width: 100%;
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.022);
text-align: left;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
.example-card:hover {
border-color: rgba(57, 215, 197, 0.35);
background: rgba(57, 215, 197, 0.055);
transform: translateX(2px);
}
.example-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 7px;
font-size: 12px;
font-weight: 800;
}
.example-title span {
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.mini-groups {
display: flex;
align-items: center;
gap: 10px;
}
.mini-side {
display: flex;
flex-wrap: wrap;
gap: 3px;
width: 76px;
}
.mini-side.coral-side {
justify-content: flex-end;
}
.mini-block {
width: 7px;
height: 7px;
border-radius: 2px;
}
.mini-block.coral {
background: var(--coral);
}
.mini-block.teal {
background: var(--teal);
}
.mini-divider {
width: 1px;
height: 19px;
background: rgba(255, 255, 255, 0.13);
}
.total-badge {
min-width: 49px;
padding: 6px 7px;
border-radius: 8px;
background: rgba(255, 255, 255, 0.055);
color: #bdc8dc;
font-size: 9px;
font-weight: 750;
text-align: center;
}
@media (max-width: 1000px) {
.app {
padding: 14px;
}
.workspace {
grid-template-columns: 1fr;
}
.stage {
min-height: 570px;
}
.panel {
overflow: visible;
}
.example-list {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
.example-card {
grid-template-columns: 1fr;
}
.total-badge {
width: 100%;
}
}
@media (max-width: 650px) {
.topbar {
align-items: flex-start;
}
.brand p {
display: none;
}
.target-pill {
min-width: auto;
padding: 9px 11px;
}
.stage {
min-height: 500px;
border-radius: 18px;
}
.panel {
padding: 16px;
border-radius: 18px;
}
.stage-footer {
align-items: flex-start;
flex-direction: column;
}
.gesture-hint {
text-align: left;
}
.example-list {
grid-template-columns: 1fr;
}
.example-card {
grid-template-columns: 1fr auto;
}
.total-badge {
width: auto;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<span></span>
<span></span>
</div>
<div>
<h1>Voxel Ratio Lab</h1>
<p>Build the same proportion at any size.</p>
</div>
</div>
<div class="target-pill" aria-label="Current target ratio">
<div>
<small>Target</small>
<strong id="headerTargetRatio">2 : 1</strong>
</div>
</div>
</header>
<main class="workspace">
<section class="stage" aria-label="Interactive three-dimensional voxel scene">
<canvas
id="scene"
tabindex="0"
aria-label="3D voxel groups. Drag to rotate, scroll to zoom, and use arrow keys to rotate."
></canvas>
<div class="stage-header">
<div class="stage-title">
<strong id="stageRatioTitle">Target proportion · 2 : 1</strong>
<span>Two colored groups, one proportional relationship.</span>
</div>
<button class="view-button" id="resetView" type="button">
Reset view
</button>
</div>
<div class="stage-footer">
<div class="legend" aria-label="Color legend">
<div class="legend-item">
<span class="legend-dot coral"></span>
Coral group
</div>
<div class="legend-item">
<span class="legend-dot teal"></span>
Teal group
</div>
</div>
<div class="gesture-hint">Drag to orbit · Scroll or pinch to zoom</div>
</div>
</section>
<aside class="panel" id="panel">
<section>
<div class="section-heading">
<div>
<p class="eyebrow">Choose a proportion</p>
<h2>Target ratio</h2>
</div>
<div class="ratio-badge" id="targetRatioBadge">2 : 1</div>
</div>
<div class="preset-list" id="presetList" aria-label="Target ratio presets">
<button class="preset active" type="button" data-a="1" data-b="1">1 : 1</button>
<button class="preset" type="button" data-a="2" data-b="1">2 : 1</button>
<button class="preset" type="button" data-a="3" data-b="2">3 : 2</button>
<button class="preset" type="button" data-a="4" data-b="3">4 : 3</button>
<button class="preset" type="button" data-a="5" data-b="4">5 : 4</button>
</div>
</section>
<div class="divider"></div>
<section>
<div class="section-heading">
<div>
<p class="eyebrow">Adjust the voxels</p>
<h2>Your build</h2>
</div>
</div>
<div class="count-card coral" style="--accent: var(--coral)">
<div class="count-head">
<div class="count-name">
<i></i>
Coral voxels
</div>
<div class="stepper">
<button type="button" data-group="a" data-delta="-1" aria-label="Remove one coral voxel">−</button>
<output id="countAOutput">3</output>
<button type="button" data-group="a" data-delta="1" aria-label="Add one coral voxel">+</button>
</div>
</div>
<input
id="countA"
type="range"
min="1"
max="18"
value="3"
aria-label="Number of coral voxels"
/>
</div>
<div class="count-card teal" style="--accent: var(--teal)">
<div class="count-head">
<div class="count-name">
<i></i>
Teal voxels
</div>
<div class="stepper">
<button type="button" data-group="b" data-delta="-1" aria-label="Remove one teal voxel">−</button>
<output id="countBOutput">2</output>
<button type="button" data-group="b" data-delta="1" aria-label="Add one teal voxel">+</button>
</div>
</div>
<input
id="countB"
type="range"
min="1"
max="18"
value="2"
aria-label="Number of teal voxels"
/>
</div>
</section>
<section class="ratio-card">
<div class="ratio-card-top">
<div>
<p class="eyebrow">Your simplified ratio</p>
<div class="current-ratio" aria-label="Current ratio">
<span class="coral-text" id="currentA">3</span>
<span class="separator">:</span>
<span class="teal-text" id="currentB">2</span>
</div>
</div>
<div class="fraction-line" aria-hidden="true"></div>
</div>
<div class="cross-product" aria-label="Cross-product comparison">
<span id="crossLeft">3 × 1</span>
<span>=</span>
<span id="crossRight">2 × 2</span>
</div>
<div class="status pending" id="status" role="status" aria-live="polite">
<div class="status-icon" id="statusIcon">?</div>
<div>
<strong id="statusTitle">Not a match yet</strong>
<p id="statusDetail">Change the voxel totals until the cross-products are equal.</p>
</div>
</div>
</section>
<button class="primary-button" id="checkMatch" type="button">
Check my ratio
</button>
<section class="examples">
<h3>Equivalent builds</h3>
<p>
These ratios all reduce to the target, even though their totals differ.
Select one to load it into the scene.
</p>
<div class="example-list" id="exampleList"></div>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const state = {
target: { a: 2, b: 1 },
counts: { a: 3, b: 2 },
camera: {
theta: 0.12,
phi: 1.02,
radius: 16,
targetTheta: 0.12,
targetPhi: 1.02,
targetRadius: 16
}
};
const MIN_VOXELS = 1;
const MAX_VOXELS = 18;
// ------------------------------------------------------------------
// Interface
// ------------------------------------------------------------------
function greatestCommonDivisor(a, b) {
a = Math.abs(a);
b = Math.abs(b);
while (b) {
const remainder = a % b;
a = b;
b = remainder;
}
return a || 1;
}
function simplifyRatio(a, b) {
const divisor = greatestCommonDivisor(a, b);
return { a: a / divisor, b: b / divisor };
}
function setTarget(a, b) {
state.target = { a, b };
document.querySelectorAll(".preset").forEach((button) => {
button.classList.toggle(
"active",
Number(button.dataset.a) === a && Number(button.dataset.b) === b
);
});
const ratioText = `${a} : ${b}`;
$("#headerTargetRatio").textContent = ratioText;
$("#targetRatioBadge").textContent = ratioText;
$("#stageRatioTitle").textContent = `Target proportion · ${ratioText}`;
buildEquivalentExamples();
updateCounts(true);
}
function updateCounts(animateVoxels = true) {
const { a, b } = state.counts;
$("#countA").value = a;
$("#countB").value = b;
$("#countAOutput").value = a;
$("#countBOutput").value = b;
const simplified = simplifyRatio(a, b);
$("#currentA").textContent = simplified.a;
$("#currentB").textContent = simplified.b;
$("#crossLeft").textContent = `${a} × ${state.target.b}`;
$("#crossRight").textContent = `${b} × ${state.target.a}`;
updateVoxelGroup(redVoxelGroup, a, "CORAL", "#ff6b6b", animateVoxels);
updateVoxelGroup(tealVoxelGroup, b, "TEAL", "#39d7c5", animateVoxels);
evaluateMatch();
}
function evaluateMatch() {
const { a, b } = state.counts;
const { a: targetA, b: targetB } = state.target;
const matched = a * targetB === b * targetA;
const status = $("#status");
status.classList.toggle("success", matched);
status.classList.toggle("pending", !matched);
if (matched) {
const simplified = simplifyRatio(a, b);
$("#statusIcon").textContent = "✓";
$("#statusTitle").textContent = "Proportions match";
$("#statusDetail").textContent =
`${simplified.a} : ${simplified.b} is equivalent to ${targetA} : ${targetB}, ` +
`even when the total voxel count is different.`;
} else {
$("#statusIcon").textContent = "?";
$("#statusTitle").textContent = "Not a match yet";
$("#statusDetail").textContent =
"Change the voxel totals until the two cross-products are equal.";
}
return matched;
}
function buildEquivalentExamples() {
const list = $("#exampleList");
list.replaceChildren();
for (let scale = 1; scale <= 3; scale++) {
const a = state.target.a * scale;
const b = state.target.b * scale;
const total = a + b;
const card = document.createElement("button");
card.className = "example-card";
card.type = "button";
card.setAttribute(
"aria-label",
`Load ${a} coral voxels and ${b} teal voxels, ${total} total`
);
const coralBlocks = Array.from(
{ length: a },
() => '<i class="mini-block coral"></i>'
).join("");
const tealBlocks = Array.from(
{ length: b },
() => '<i class="mini-block teal"></i>'
).join("");
card.innerHTML = `
<div>
<div class="example-title">
${a} : ${b}
<span>Scale ×${scale}</span>
</div>
<div class="mini-groups">
<div class="mini-side coral-side">${coralBlocks}</div>
<div class="mini-divider"></div>
<div class="mini-side teal-side">${tealBlocks}</div>
</div>
</div>
<div class="total-badge">${total}<br>voxels</div>
`;
card.addEventListener("click", () => {
state.counts = { a, b };
updateCounts(true);
checkMatch();
});
list.appendChild(card);
}
}
function checkMatch() {
if (evaluateMatch()) {
pulseMatch();
return;
}
const panel = $("#panel");
panel.classList.remove("shake");
void panel.offsetWidth;
panel.classList.add("shake");
}
document.querySelectorAll(".preset").forEach((button) => {
button.addEventListener("click", () => {
setTarget(Number(button.dataset.a), Number(button.dataset.b));
});
});
document.querySelectorAll("[data-group][data-delta]").forEach((button) => {
button.addEventListener("click", () => {
const group = button.dataset.group;
const delta = Number(button.dataset.delta);
state.counts[group] = THREE.MathUtils.clamp(
state.counts[group] + delta,
MIN_VOXELS,
MAX_VOXELS
);
updateCounts(true);
});
});
$("#countA").addEventListener("input", (event) => {
state.counts.a = Number(event.target.value);
updateCounts(false);
});
$("#countB").addEventListener("input", (event) => {
state.counts.b = Number(event.target.value);
updateCounts(false);
});
$("#checkMatch").addEventListener("click", checkMatch);
// ------------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------------
const canvas = $("#scene");
const stage = canvas.parentElement;
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0c1424);
scene.fog = new THREE.Fog(0x0c1424, 20, 38);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.55, 0);
const hemisphereLight = new THREE.HemisphereLight(
0xb9d7ff,
0x182033,
2.15
);
scene.add(hemisphereLight);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-5, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const coralLight = new THREE.PointLight(0xff6b6b, 16, 12, 2);
coralLight.position.set(-6, 3.5, 4);
scene.add(coralLight);
const tealLight = new THREE.PointLight(0x39d7c5, 16, 12, 2);
tealLight.position.set(6, 3.5, 3);
scene.add(tealLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x0b1322,
roughness: 0.95,
metalness: 0.05
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.39;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(
38,
38,
0x40516e,
0x26344c
);
grid.position.y = -0.375;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
const halo = new THREE.Mesh(
new THREE.CircleGeometry(8.5, 80),
new THREE.MeshBasicMaterial({
color: 0x172842,
transparent: true,
opacity: 0.26,
depthWrite: false
})
);
halo.rotation.x = -Math.PI / 2;
halo.position.y = -0.36;
scene.add(halo);
const boxGeometry = new THREE.BoxGeometry(0.58, 0.58, 0.58);
const edgeGeometry = new THREE.EdgesGeometry(boxGeometry, 22);
const coralMaterial = new THREE.MeshStandardMaterial({
color: 0xff6b6b,
roughness: 0.3,
metalness: 0.08,
emissive: 0x4a0d12,
emissiveIntensity: 0.16
});
const tealMaterial = new THREE.MeshStandardMaterial({
color: 0x39d7c5,
roughness: 0.3,
metalness: 0.08,
emissive: 0x073f3a,
emissiveIntensity: 0.16
});
const coralEdgeMaterial = new THREE.LineBasicMaterial({
color: 0xffd0d0,
transparent: true,
opacity: 0.28
});
const tealEdgeMaterial = new THREE.LineBasicMaterial({
color: 0xc6fff8,
transparent: true,
opacity: 0.28
});
const pedestalMaterial = new THREE.MeshStandardMaterial({
color: 0x1a2941,
roughness: 0.7,
metalness: 0.18
});
const pedestalTopMaterial = new THREE.MeshStandardMaterial({
color: 0x203451,
roughness: 0.55,
metalness: 0.14
});
function createPedestal() {
const root = new THREE.Group();
const base = new THREE.Mesh(
new THREE.CylinderGeometry(2.55, 2.7, 0.28, 64),
pedestalMaterial
);
base.position.y = -0.21;
base.receiveShadow = true;
root.add(base);
const top = new THREE.Mesh(
new THREE.CylinderGeometry(2.43, 2.48, 0.065, 64),
pedestalTopMaterial
);
top.position.y = -0.035;
top.receiveShadow = true;
root.add(top);
const trim = new THREE.Mesh(
new THREE.TorusGeometry(2.48, 0.025, 8, 96),
new THREE.MeshBasicMaterial({
color: 0x506684,
transparent: true,
opacity: 0.42
})
);
trim.rotation.x = Math.PI / 2;
trim.position.y = 0.005;
root.add(trim);
return root;
}
function createTextSprite(initialText, color) {
const spriteCanvas = document.createElement("canvas");
spriteCanvas.width = 512;
spriteCanvas.height = 128;
const context = spriteCanvas.getContext("2d");
const texture = new THREE.CanvasTexture(spriteCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(2.55, 0.64, 1);
sprite.renderOrder = 10;
sprite.userData.canvas = spriteCanvas;
sprite.userData.context = context;
sprite.userData.texture = texture;
updateTextSprite(sprite, initialText, color);
return sprite;
}
function updateTextSprite(sprite, text, color) {
const context = sprite.userData.context;
const canvas = sprite.userData.canvas;
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(7, 13, 26, 0.88)";
context.strokeStyle = color;
context.lineWidth = 3;
const x = 8;
const y = 8;
const width = canvas.width - 16;
const height = canvas.height - 16;
const radius = 24;
context.beginPath();
context.moveTo(x + radius, y);
context.lineTo(x + width - radius, y);
context.quadraticCurveTo(x + width, y, x + width, y + radius);
context.lineTo(x + width, y + height - radius);
context.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
context.lineTo(x + radius, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - radius);
context.lineTo(x, y + radius);
context.quadraticCurveTo(x, y, x + radius, y);
context.closePath();
context.fill();
context.stroke();
context.fillStyle = color;
context.font = "800 43px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
sprite.userData.texture.needsUpdate = true;
}
const redRoot = new THREE.Group();
const tealRoot = new THREE.Group();
scene.add(redRoot, tealRoot);
redRoot.add(createPedestal());
tealRoot.add(createPedestal());
const redVoxelGroup = new THREE.Group();
const tealVoxelGroup = new THREE.Group();
redRoot.add(redVoxelGroup);
tealRoot.add(tealVoxelGroup);
const redLabel = createTextSprite("CORAL · 3", "#ff8585");
const tealLabel = createTextSprite("TEAL · 2", "#58e7d7");
redLabel.position.set(0, 2.85, 0);
tealLabel.position.set(0, 2.85, 0);
redRoot.add(redLabel);
tealRoot.add(tealLabel);
const activeVoxelMeshes = [];
const pulseRings = [];
function voxelLayout(count) {
const columns =
count <= 6 ? 3 : count <= 12 ? 4 : 5;
const rows = Math.ceil(count / columns);
const spacing = 0.68;
return Array.from({ length: count }, (_, index) => {
const column = index % columns;
const row = Math.floor(index / columns);
return {
x: (column - (columns - 1) / 2) * spacing,
y: 0.3,
z: (row - (rows - 1) / 2) * spacing
};
});
}
function updateVoxelGroup(group, count, labelText, labelColor, animate) {
group.clear();
const positions = voxelLayout(count);
const material = group === redVoxelGroup
? coralMaterial
: tealMaterial;
const edgeMaterial = group === redVoxelGroup
? coralEdgeMaterial
: tealEdgeMaterial;
positions.forEach((position, index) => {
const cube = new THREE.Mesh(boxGeometry, material);
cube.position.set(position.x, position.y, position.z);
cube.castShadow = true;
cube.receiveShadow = true;
cube.userData.isVoxel = true;
const edges = new THREE.LineSegments(edgeGeometry, edgeMaterial);
cube.add(edges);
if (animate) {
cube.userData.animationStart = performance.now() + index * 24;
cube.scale.setScalar(0.001);
} else {
cube.scale.setScalar(1);
}
group.add(cube);
});
const label = group === redVoxelGroup ? redLabel : tealLabel;
updateTextSprite(label, `${labelText} · ${count}`, labelColor);
}
function updateSceneLayout() {
const width = stage.clientWidth;
const height = stage.clientHeight;
const aspect = width / Math.max(height, 1);
renderer.setSize(width, height, false);
camera.aspect = aspect;
camera.updateProjectionMatrix();
const narrow = aspect < 0.95;
const spacing = narrow ? 2.9 : 4.05;
const rootScale = narrow ? 0.87 : 1;
redRoot.position.x = -spacing;
tealRoot.position.x = spacing;
redRoot.scale.setScalar(rootScale);
tealRoot.scale.setScalar(rootScale);
const defaultRadius = narrow ? 20.5 : 16;
state.camera.targetRadius = defaultRadius;
state.camera.radius = defaultRadius;
}
function resetCamera() {
const narrow = camera.aspect < 0.95;
state.camera.targetTheta = 0.12;
state.camera.targetPhi = 1.02;
state.camera.targetRadius = narrow ? 20.5 : 16;
}
$("#resetView").addEventListener("click", resetCamera);
// Direct camera controls implemented without addons.
let dragging = false;
let pointerId = null;
let previousX = 0;
let previousY = 0;
let pinchDistance = null;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== pointerId) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
state.camera.targetTheta -= deltaX * 0.006;
state.camera.targetPhi = THREE.MathUtils.clamp(
state.camera.targetPhi - deltaY * 0.005,
0.55,
1.43
);
});
function endPointer(event) {
if (event.pointerId !== pointerId) return;
dragging = false;
pointerId = null;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("lostpointercapture", () => {
dragging = false;
pointerId = null;
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
state.camera.targetRadius = THREE.MathUtils.clamp(
state.camera.targetRadius + event.deltaY * 0.012,
10,
25
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
const keyStep = 0.12;
if (event.key === "ArrowLeft") {
state.camera.targetTheta += keyStep;
} else if (event.key === "ArrowRight") {
state.camera.targetTheta -= keyStep;
} else if (event.key === "ArrowUp") {
state.camera.targetPhi = THREE.MathUtils.clamp(
state.camera.targetPhi - keyStep,
0.55,
1.43
);
} else if (event.key === "ArrowDown") {
state.camera.targetPhi = THREE.MathUtils.clamp(
state.camera.targetPhi + keyStep,
0.55,
1.43
);
} else if (event.key === "+" || event.key === "=") {
state.camera.targetRadius = Math.max(
10,
state.camera.targetRadius - 1
);
} else if (event.key === "-") {
state.camera.targetRadius = Math.min(
25,
state.camera.targetRadius + 1
);
} else {
return;
}
event.preventDefault();
});
// A small field of points adds depth without obscuring the voxels.
const particlePositions = new Float32Array(120 * 3);
for (let i = 0; i < 120; i++) {
particlePositions[i * 3] = (Math.random() - 0.5) * 24;
particlePositions[i * 3 + 1] = Math.random() * 8 - 0.2;
particlePositions[i * 3 + 2] = (Math.random() - 0.5) * 18;
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3)
);
const particles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0x6682aa,
size: 0.035,
transparent: true,
opacity: 0.38,
depthWrite: false
})
);
scene.add(particles);
function pulseMatch() {
const now = performance.now();
[redRoot, tealRoot].forEach((root, index) => {
const geometry = new THREE.TorusGeometry(
2.28 * root.scale.x,
0.038,
8,
96
);
const material = new THREE.MeshBasicMaterial({
color: index === 0 ? 0xff8b8b : 0x58e7d7,
transparent: true,
opacity: 0.9,
depthWrite: false
});
const ring = new THREE.Mesh(geometry, material);
ring.rotation.x = Math.PI / 2;
ring.position.set(root.position.x, -0.005, 0);
scene.add(ring);
pulseRings.push({
mesh: ring,
start: now + index * 80,
duration: 850
});
});
}
const clock = new THREE.Clock();
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
function animate() {
requestAnimationFrame(animate);
const now = performance.now();
const delta = Math.min(clock.getDelta(), 0.05);
const cameraEase = 1 - Math.pow(0.0007, delta);
state.camera.theta = THREE.MathUtils.lerp(
state.camera.theta,
state.camera.targetTheta,
cameraEase
);
state.camera.phi = THREE.MathUtils.lerp(
state.camera.phi,
state.camera.targetPhi,
cameraEase
);
state.camera.radius = THREE.MathUtils.lerp(
state.camera.radius,
state.camera.targetRadius,
cameraEase
);
const { theta, phi, radius } = state.camera;
camera.position.set(
cameraTarget.x + radius * Math.sin(phi) * Math.sin(theta),
cameraTarget.y + radius * Math.cos(phi),
cameraTarget.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(cameraTarget);
for (const cube of redVoxelGroup.children.concat(tealVoxelGroup.children)) {
if (!cube.userData.isVoxel || !cube.userData.animationStart) continue;
const progress = THREE.MathUtils.clamp(
(now - cube.userData.animationStart) / 420,
0,
1
);
const eased = reducedMotion
? 1
: 1 - Math.pow(1 - progress, 3);
cube.scale.setScalar(Math.max(0.001, eased));
}
for (let i = pulseRings.length - 1; i >= 0; i--) {
const pulse = pulseRings[i];
const progress = THREE.MathUtils.clamp(
(now - pulse.start) / pulse.duration,
0,
1
);
const scale = 0.85 + progress * 0.65;
pulse.mesh.scale.setScalar(scale);
pulse.mesh.material.opacity = (1 - progress) * 0.9;
if (progress >= 1) {
scene.remove(pulse.mesh);
pulse.mesh.geometry.dispose();
pulse.mesh.material.dispose();
pulseRings.splice(i, 1);
}
}
particles.rotation.y += delta * 0.012;
renderer.render(scene, camera);
}
const resizeObserver = new ResizeObserver(updateSceneLayout);
resizeObserver.observe(stage);
canvas.style.cursor = "grab";
setTarget(2, 1);
updateSceneLayout();
animate();
</script>
</body>
</html>
```