Prompt specification
Build or customize the Three.js 3D scene "Colored shape population tray" 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: Colored shape population tray
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/colored-shape-population-tray-x1777
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a scene with tokens varying in both color and shape with visible totals; select one property and recompute proportions only within the conditioned subset. Low-poly.
Working Three.js Starter (colored-shape-population-tray-x1777):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Token Atlas — Conditional Proportions</title>
<style>
:root {
color-scheme: dark;
--bg: #080d16;
--panel: #0d1522;
--panel-2: #121d2d;
--line: rgba(255, 255, 255, 0.09);
--text: #f4f7fb;
--muted: #8d9bb0;
--accent: #55e1c4;
--accent-soft: rgba(85, 225, 196, 0.12);
--coral: #ff6f61;
--amber: #ffbd59;
--aqua: #35d0ba;
--violet: #927cff;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
select {
font: inherit;
}
button {
color: inherit;
}
.app {
height: 100vh;
display: grid;
grid-template-rows: 72px minmax(0, 1fr);
}
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 0 24px;
border-bottom: 1px solid var(--line);
background: rgba(8, 13, 22, 0.92);
z-index: 5;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 33px;
height: 33px;
flex: 0 0 auto;
background: linear-gradient(145deg, #70f4d9, #4f75d9);
clip-path: polygon(50% 0, 94% 24%, 94% 76%, 50% 100%, 6% 76%, 6% 24%);
box-shadow: 0 0 26px rgba(85, 225, 196, 0.22);
}
.brand h1 {
margin: 0;
font-size: 15px;
line-height: 1.1;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 12px;
}
.header-stats {
display: flex;
align-items: center;
gap: 9px;
white-space: nowrap;
}
.header-chip {
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 999px;
color: #b9c5d5;
background: rgba(255, 255, 255, 0.025);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1fr) 380px;
min-height: 0;
}
.scene-wrap {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #0b1220;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
.scene-heading {
position: absolute;
top: 27px;
left: 28px;
pointer-events: none;
max-width: 430px;
}
.eyebrow {
margin: 0 0 8px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.scene-heading h2 {
margin: 0;
font-size: clamp(25px, 3vw, 40px);
line-height: 1;
letter-spacing: -0.045em;
}
.scene-heading p:last-child {
margin: 11px 0 0;
color: #9dabbd;
font-size: 13px;
line-height: 1.55;
}
.scene-tools {
position: absolute;
top: 25px;
right: 24px;
display: flex;
align-items: center;
gap: 10px;
}
.tool-button {
border: 1px solid var(--line);
border-radius: 9px;
padding: 8px 11px;
background: rgba(12, 21, 34, 0.72);
backdrop-filter: blur(12px);
color: #b9c6d8;
cursor: pointer;
font-size: 11px;
font-weight: 700;
}
.tool-button:hover {
border-color: rgba(85, 225, 196, 0.45);
color: white;
}
.camera-hint {
position: absolute;
right: 25px;
bottom: 128px;
color: rgba(199, 211, 227, 0.62);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
.scene-totals {
position: absolute;
left: 25px;
right: 25px;
bottom: 24px;
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
max-width: 620px;
border: 1px solid var(--line);
border-radius: 14px;
overflow: hidden;
background: rgba(10, 17, 28, 0.78);
backdrop-filter: blur(14px);
pointer-events: none;
}
.scene-total {
padding: 14px 17px;
border-right: 1px solid var(--line);
}
.scene-total:last-child {
border-right: 0;
}
.scene-total span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.scene-total strong {
display: block;
margin-top: 5px;
font-size: 23px;
font-variant-numeric: tabular-nums;
}
.scene-total.subset strong {
color: var(--accent);
}
.panel {
min-height: 0;
overflow-y: auto;
border-left: 1px solid var(--line);
background:
radial-gradient(circle at 90% 0%, rgba(85, 225, 196, 0.07), transparent 28%),
var(--panel);
padding: 24px;
}
.panel-section {
padding-bottom: 23px;
margin-bottom: 23px;
border-bottom: 1px solid var(--line);
}
.panel-section:last-child {
margin-bottom: 0;
border-bottom: 0;
}
.section-label {
margin: 0 0 13px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.token-select {
width: 100%;
height: 42px;
margin-bottom: 12px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 9px;
outline: none;
background: #111b2a;
color: #e7edf5;
cursor: pointer;
}
.token-select:focus {
border-color: rgba(85, 225, 196, 0.65);
}
.selected-card {
display: grid;
grid-template-columns: 48px minmax(0, 1fr) auto;
align-items: center;
gap: 12px;
padding: 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel-2);
}
.selected-glyph {
--token-color: var(--accent);
display: grid;
width: 48px;
height: 48px;
place-items: center;
border: 1px solid color-mix(in srgb, var(--token-color), transparent 62%);
border-radius: 11px;
background: color-mix(in srgb, var(--token-color), transparent 90%);
color: var(--token-color);
}
.selected-copy {
min-width: 0;
}
.selected-copy span {
display: block;
color: var(--muted);
font-size: 10px;
}
.selected-copy strong {
display: block;
overflow: hidden;
margin-top: 3px;
font-size: 14px;
text-overflow: ellipsis;
white-space: nowrap;
}
.selected-copy small {
display: block;
margin-top: 3px;
color: #8d9cb0;
font-size: 10px;
}
.selected-id {
color: #718096;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
}
.property-label {
margin: 21px 0 9px;
color: #cbd5e2;
font-size: 11px;
font-weight: 750;
}
.property-options {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.property-button {
padding: 11px 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: transparent;
color: #9baabd;
cursor: pointer;
font-size: 12px;
font-weight: 750;
text-align: left;
}
.property-button span {
display: block;
margin-top: 3px;
color: #637188;
font-size: 9px;
font-weight: 600;
}
.property-button:hover {
border-color: rgba(255, 255, 255, 0.19);
color: white;
}
.property-button.active {
border-color: rgba(85, 225, 196, 0.55);
background: var(--accent-soft);
color: var(--accent);
}
.property-button.active span {
color: rgba(151, 231, 213, 0.68);
}
.help-text {
margin: 11px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.breakdown-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.breakdown-heading h3 {
margin: 0;
font-size: 17px;
letter-spacing: -0.02em;
}
.breakdown-heading p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.match-badge {
flex: 0 0 auto;
padding: 6px 8px;
border-radius: 7px;
background: var(--accent-soft);
color: var(--accent);
font-size: 10px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.subset-line {
margin: 19px 0 9px;
color: #c4cedb;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.proportion-stack {
display: flex;
width: 100%;
height: 13px;
overflow: hidden;
border-radius: 999px;
background: #182334;
}
.stack-segment {
min-width: 2px;
height: 100%;
transition: width 350ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.breakdown-list {
display: grid;
gap: 8px;
margin-top: 16px;
}
.breakdown-row {
display: grid;
grid-template-columns: 24px minmax(0, 1fr) auto;
align-items: center;
gap: 9px;
padding: 9px 10px;
border: 1px solid rgba(255, 255, 255, 0.055);
border-radius: 9px;
background: rgba(255, 255, 255, 0.022);
}
.row-icon {
display: grid;
width: 24px;
height: 24px;
place-items: center;
color: white;
}
.color-dot {
width: 13px;
height: 13px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.035);
}
.row-name {
font-size: 11px;
font-weight: 700;
}
.row-track {
height: 3px;
margin-top: 5px;
overflow: hidden;
border-radius: 999px;
background: #1c293a;
}
.row-fill {
width: 0;
height: 100%;
border-radius: inherit;
transition: width 350ms ease;
}
.row-value {
color: #e7edf5;
font-size: 12px;
font-weight: 800;
font-variant-numeric: tabular-nums;
text-align: right;
}
.row-value small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 600;
}
.shape-icon {
display: inline-block;
width: 15px;
height: 15px;
flex: 0 0 auto;
background: currentColor;
}
.shape-icon.block {
border-radius: 2px;
transform: rotate(8deg) skewY(-3deg);
}
.shape-icon.diamond {
clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
}
.shape-icon.crystal {
clip-path: polygon(50% 0, 88% 23%, 100% 70%, 50% 100%, 0 70%, 12% 23%);
}
.shape-icon.peak {
clip-path: polygon(50% 0, 100% 100%, 0 100%);
}
.stats-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 8px;
}
.stat-card {
padding: 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.022);
}
.stat-card span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.stat-card strong {
display: block;
margin-top: 6px;
font-size: 19px;
font-variant-numeric: tabular-nums;
}
.stat-card.accent strong {
color: var(--accent);
}
.denominator-note {
margin: 14px 0 0;
padding: 12px;
border-left: 2px solid var(--accent);
border-radius: 0 7px 7px 0;
background: rgba(85, 225, 196, 0.055);
color: #aab8c9;
font-size: 10px;
line-height: 1.55;
}
.tooltip {
position: absolute;
z-index: 10;
width: 175px;
padding: 11px 12px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 9px;
background: rgba(9, 16, 27, 0.94);
box-shadow: 0 14px 36px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: none;
opacity: 0;
transform: translateY(5px);
transition: opacity 120ms ease, transform 120ms ease;
}
.tooltip.visible {
opacity: 1;
transform: translateY(0);
}
.tooltip strong {
display: block;
margin-bottom: 6px;
font-size: 12px;
}
.tooltip-meta {
display: flex;
align-items: center;
gap: 7px;
color: #a4b1c2;
font-size: 10px;
}
.tooltip .shape-icon {
width: 12px;
height: 12px;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100vh;
grid-template-rows: 68px auto;
}
.workspace {
grid-template-columns: 1fr;
}
.scene-wrap {
height: 58vh;
min-height: 470px;
}
.panel {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
}
@media (max-width: 560px) {
header {
padding: 0 15px;
}
.header-stats {
display: none;
}
.scene-heading {
top: 20px;
left: 18px;
right: 18px;
}
.scene-tools {
top: auto;
right: 16px;
bottom: 128px;
}
.camera-hint {
display: none;
}
.scene-totals {
left: 14px;
right: 14px;
bottom: 15px;
}
.scene-total {
padding: 11px;
}
.scene-total strong {
font-size: 19px;
}
.panel {
padding: 20px 16px;
}
}
</style>
</head>
<body>
<div class="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>Token Atlas</h1>
<p>Conditional population visualization</p>
</div>
</div>
<div class="header-stats" aria-label="Dataset summary">
<span class="header-chip" id="headerTokenCount">32 Tokens</span>
<span class="header-chip">4 Colors</span>
<span class="header-chip">4 Shapes</span>
</div>
</header>
<main class="workspace">
<section class="scene-wrap" id="sceneWrap" aria-label="Interactive 3D token scene">
<canvas id="scene" tabindex="0"></canvas>
<div class="scene-heading">
<p class="eyebrow">Conditioned population</p>
<h2 id="sceneTitle">Within Block</h2>
<p id="sceneSubtitle">
Matching tokens remain bright while the rest of the population is dimmed.
</p>
</div>
<div class="scene-tools">
<button class="tool-button" id="resetCamera" type="button">
Reset view
</button>
</div>
<div class="camera-hint">Drag to orbit · Scroll to zoom · Click a token</div>
<div class="scene-totals" aria-live="polite">
<div class="scene-total">
<span>All tokens</span>
<strong id="totalCount">32</strong>
</div>
<div class="scene-total subset">
<span>In subset</span>
<strong id="subsetCount">8</strong>
</div>
<div class="scene-total">
<span>Outside subset</span>
<strong id="outsideCount">24</strong>
</div>
</div>
<div class="tooltip" id="tooltip" role="status"></div>
</section>
<aside class="panel">
<section class="panel-section">
<p class="section-label">01 / Select a token</p>
<label for="tokenSelect" class="property-label">Token</label>
<select class="token-select" id="tokenSelect"></select>
<div class="selected-card">
<div class="selected-glyph" id="selectedGlyph"></div>
<div class="selected-copy">
<span>Selected token</span>
<strong id="selectedName">Coral Block</strong>
<small id="selectedMeta">Block · Coral</small>
</div>
<span class="selected-id" id="selectedId">T01</span>
</div>
<p class="property-label">Condition on one property</p>
<div class="property-options" role="group" aria-label="Condition property">
<button
class="property-button"
type="button"
data-property="color"
aria-pressed="false"
>
Color
<span>Measure shape mix</span>
</button>
<button
class="property-button active"
type="button"
data-property="shape"
aria-pressed="true"
>
Shape
<span>Measure color mix</span>
</button>
</div>
<p class="help-text" id="propertyHelp">
The population is filtered to the selected token's shape. Percentages use
only the matching tokens as their denominator.
</p>
</section>
<section class="panel-section">
<p class="section-label">02 / Conditional proportions</p>
<div class="breakdown-heading">
<div>
<h3 id="breakdownTitle">Color proportions</h3>
<p id="breakdownSubtitle">Distribution within the conditioned subset</p>
</div>
<span class="match-badge" id="matchBadge">25% match</span>
</div>
<p class="subset-line" id="subsetLine">8 of 32 tokens</p>
<div class="proportion-stack" id="proportionStack" aria-label="Proportion bar"></div>
<div class="breakdown-list" id="breakdownList"></div>
</section>
<section class="panel-section">
<p class="section-label">03 / Visible totals</p>
<div class="stats-grid">
<div class="stat-card">
<span>Population</span>
<strong id="statPopulation">32</strong>
</div>
<div class="stat-card accent">
<span>Subset</span>
<strong id="statSubset">8</strong>
</div>
<div class="stat-card">
<span>Excluded</span>
<strong id="statExcluded">24</strong>
</div>
<div class="stat-card">
<span>Match rate</span>
<strong id="statMatchRate">25.0%</strong>
</div>
</div>
<p class="denominator-note" id="denominatorNote">
Denominator: only the 8 tokens matching the selected shape.
No outside tokens contribute to the displayed proportions.
</p>
</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 COLORS = {
coral: { name: "Coral", hex: "#ff6f61" },
amber: { name: "Amber", hex: "#ffbd59" },
aqua: { name: "Aqua", hex: "#35d0ba" },
violet: { name: "Violet", hex: "#927cff" }
};
const SHAPES = {
block: { name: "Block", color: "#cbd5e1" },
diamond: { name: "Diamond", color: "#75e6d2" },
crystal: { name: "Crystal", color: "#ff9e7d" },
peak: { name: "Peak", color: "#b7a5ff" }
};
// Deliberately uneven cross-tab so subsets produce meaningful proportions.
const CROSS_TAB = [
{ color: "coral", shape: "block", count: 1 },
{ color: "coral", shape: "diamond", count: 3 },
{ color: "coral", shape: "crystal", count: 4 },
{ color: "coral", shape: "peak", count: 2 },
{ color: "amber", shape: "block", count: 2 },
{ color: "amber", shape: "diamond", count: 2 },
{ color: "amber", shape: "crystal", count: 2 },
{ color: "amber", shape: "peak", count: 2 },
{ color: "aqua", shape: "block", count: 2 },
{ color: "aqua", shape: "diamond", count: 2 },
{ color: "aqua", shape: "crystal", count: 2 },
{ color: "aqua", shape: "peak", count: 2 },
{ color: "violet", shape: "block", count: 1 },
{ color: "violet", shape: "diamond", count: 1 },
{ color: "violet", shape: "crystal", count: 2 },
{ color: "violet", shape: "peak", count: 2 }
];
const tokens = [];
let serial = 0;
for (const entry of CROSS_TAB) {
for (let i = 0; i < entry.count; i++) {
serial += 1;
tokens.push({
id: `T${String(serial).padStart(2, "0")}`,
color: entry.color,
shape: entry.shape,
inSubset: true,
hovered: false
});
}
}
// Stable shuffle so repeated spatial combinations are not visibly grouped.
let seed = 927;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
for (let i = tokens.length - 1; i > 0; i--) {
const j = Math.floor(random() * (i + 1));
[tokens[i], tokens[j]] = [tokens[j], tokens[i]];
}
const canvas = document.getElementById("scene");
const sceneWrap = document.getElementById("sceneWrap");
const tooltip = document.getElementById("tooltip");
const renderer = new THREE.WebGLRenderer({
canvas,
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.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#0b1220");
scene.fog = new THREE.Fog("#0b1220", 19, 35);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, -0.05, 0);
const cameraState = {
radius: 18,
theta: 0.18,
phi: 1.02
};
function updateCamera() {
const { radius, theta, phi } = cameraState;
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);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xc8e4ff, 0x172033, 2.15);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xffeed4, 3.3);
keyLight.position.set(-7, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x51d9c3, 22, 22, 2);
rimLight.position.set(7, 5, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff8b67, 15, 19, 2);
warmLight.position.set(-7, 3, -4);
scene.add(warmLight);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(14.6, 0.22, 7.45),
new THREE.MeshStandardMaterial({
color: 0x101b2b,
roughness: 0.93,
metalness: 0.08
})
);
platform.position.y = -0.71;
platform.receiveShadow = true;
scene.add(platform);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x080e18,
roughness: 1
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.84;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(34, 34, 0x25405a, 0x16283a);
grid.position.y = -0.59;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const shapeGeometries = {
block: new THREE.BoxGeometry(0.9, 0.9, 0.9),
diamond: new THREE.OctahedronGeometry(0.66, 0),
crystal: new THREE.IcosahedronGeometry(0.63, 0),
peak: new THREE.ConeGeometry(0.59, 1.08, 5, 1)
};
const edgeGeometries = {
block: new THREE.EdgesGeometry(shapeGeometries.block, 20),
diamond: new THREE.EdgesGeometry(shapeGeometries.diamond, 20),
crystal: new THREE.EdgesGeometry(shapeGeometries.crystal, 12),
peak: new THREE.EdgesGeometry(shapeGeometries.peak, 8)
};
const baseGeometry = new THREE.CylinderGeometry(0.69, 0.73, 0.12, 6);
const ringGeometry = new THREE.TorusGeometry(0.78, 0.035, 4, 6);
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0xeafff9,
transparent: true,
opacity: 0.9
});
const tokenGroup = new THREE.Group();
scene.add(tokenGroup);
const pickMeshes = [];
const columns = 8;
const xSpacing = 1.65;
const zSpacing = 1.7;
tokens.forEach((token, index) => {
const group = new THREE.Group();
const column = index % columns;
const row = Math.floor(index / columns);
group.position.set(
(column - (columns - 1) / 2) * xSpacing,
0,
(row - 1.5) * zSpacing
);
const tokenColor = new THREE.Color(COLORS[token.color].hex);
const material = new THREE.MeshStandardMaterial({
color: tokenColor,
roughness: 0.67,
metalness: 0.08,
flatShading: true,
transparent: true,
opacity: 1
});
const shape = new THREE.Mesh(shapeGeometries[token.shape], material);
shape.position.y = 0.17;
shape.castShadow = true;
shape.receiveShadow = true;
shape.userData.token = token;
group.add(shape);
pickMeshes.push(shape);
const edges = new THREE.LineSegments(
edgeGeometries[token.shape],
new THREE.LineBasicMaterial({
color: 0x101827,
transparent: true,
opacity: 0.55
})
);
edges.position.y = 0.17;
group.add(edges);
const baseMaterial = new THREE.MeshStandardMaterial({
color: tokenColor.clone().multiplyScalar(0.28),
roughness: 0.88,
metalness: 0.06,
transparent: true,
opacity: 0.92
});
const base = new THREE.Mesh(baseGeometry, baseMaterial);
base.position.y = -0.52;
base.receiveShadow = true;
base.castShadow = true;
base.userData.token = token;
group.add(base);
pickMeshes.push(base);
const ring = new THREE.Mesh(ringGeometry, ringMaterial);
ring.rotation.x = Math.PI / 2;
ring.position.y = -0.445;
ring.visible = false;
group.add(ring);
token.group = group;
token.shapeMesh = shape;
token.material = material;
token.edgeMaterial = edges.material;
token.baseMaterial = baseMaterial;
token.ring = ring;
token.hovered = false;
tokenGroup.add(group);
});
let selectedToken = tokens[0];
let conditionProperty = "shape";
const tokenSelect = document.getElementById("tokenSelect");
const propertyButtons = [...document.querySelectorAll(".property-button")];
for (const token of tokens) {
const option = document.createElement("option");
option.value = token.id;
option.textContent =
`${token.id} · ${COLORS[token.color].name} / ${SHAPES[token.shape].name}`;
tokenSelect.appendChild(option);
}
function shapeIconMarkup(shapeKey) {
return `<span class="shape-icon ${shapeKey}" aria-hidden="true"></span>`;
}
function groupColor(groupKey, value) {
return groupKey === "color"
? COLORS[value].hex
: SHAPES[value].color;
}
function groupName(groupKey, value) {
return groupKey === "color"
? COLORS[value].name
: SHAPES[value].name;
}
function updateSceneState() {
for (const token of tokens) {
token.inSubset = token[conditionProperty] === selectedToken[conditionProperty];
token.ring.visible = token === selectedToken;
}
}
function updateSelectedCard() {
const color = COLORS[selectedToken.color];
const shape = SHAPES[selectedToken.shape];
document.getElementById("selectedGlyph").style.setProperty(
"--token-color",
color.hex
);
document.getElementById("selectedGlyph").innerHTML =
shapeIconMarkup(selectedToken.shape);
document.getElementById("selectedName").textContent =
`${color.name} ${shape.name}`;
document.getElementById("selectedMeta").textContent =
`${shape.name} · ${color.name}`;
document.getElementById("selectedId").textContent = selectedToken.id;
tokenSelect.value = selectedToken.id;
}
function updateBreakdown() {
const groupKey =
conditionProperty === "color" ? "shape" : "color";
const groupKeys = Object.keys(groupKey === "color" ? COLORS : SHAPES);
const subset = tokens.filter(
(token) => token[conditionProperty] === selectedToken[conditionProperty]
);
const counts = new Map(groupKeys.map((key) => [key, 0]));
for (const token of subset) {
counts.set(token[groupKey], counts.get(token[groupKey]) + 1);
}
const total = tokens.length;
const subsetCount = subset.length;
const outsideCount = total - subsetCount;
const matchRate = total ? (subsetCount / total) * 100 : 0;
const conditionValue = selectedToken[conditionProperty];
const conditionName =
conditionProperty === "color"
? COLORS[conditionValue].name
: SHAPES[conditionValue].name;
const groupLabel =
groupKey === "color" ? "Color proportions" : "Shape proportions";
document.getElementById("breakdownTitle").textContent = groupLabel;
document.getElementById(
"breakdownSubtitle"
).textContent = `${groupKey === "color" ? "Color" : "Shape"} distribution within the conditioned subset`;
document.getElementById("matchBadge").textContent =
`${matchRate.toFixed(matchRate < 10 ? 1 : 0)}% match`;
document.getElementById("subsetLine").textContent =
`${subsetCount} of ${total} tokens · ${matchRate.toFixed(1)}% of population`;
const stack = document.getElementById("proportionStack");
stack.innerHTML = "";
const list = document.getElementById("breakdownList");
list.innerHTML = "";
for (const key of groupKeys) {
const count = counts.get(key);
const percent = subsetCount ? (count / subsetCount) * 100 : 0;
const color = groupColor(groupKey, key);
const segment = document.createElement("div");
segment.className = "stack-segment";
segment.style.width = `${percent}%`;
segment.style.background = color;
segment.title = `${groupName(groupKey, key)}: ${count} (${percent.toFixed(1)}%)`;
stack.appendChild(segment);
const row = document.createElement("div");
row.className = "breakdown-row";
const icon =
groupKey === "color"
? `<span class="color-dot" style="color:${color}"></span>`
: shapeIconMarkup(key);
row.innerHTML = `
<div class="row-icon" style="color:${color}">${icon}</div>
<div>
<div class="row-name">${groupName(groupKey, key)}</div>
<div class="row-track">
<div
class="row-fill"
style="background:${color}; width:${percent}%"
></div>
</div>
</div>
<div class="row-value">
${count}
<small>${percent.toFixed(1)}%</small>
</div>
`;
list.appendChild(row);
}
document.getElementById("totalCount").textContent = total;
document.getElementById("subsetCount").textContent = subsetCount;
document.getElementById("outsideCount").textContent = outsideCount;
document.getElementById("statPopulation").textContent = total;
document.getElementById("statSubset").textContent = subsetCount;
document.getElementById("statExcluded").textContent = outsideCount;
document.getElementById("statMatchRate").textContent =
`${matchRate.toFixed(1)}%`;
document.getElementById("headerTokenCount").textContent =
`${total} Tokens`;
document.getElementById("sceneTitle").textContent =
`Within ${conditionName}`;
document.getElementById("sceneSubtitle").textContent =
conditionProperty === "color"
? `${subsetCount} matching-color tokens. Shape proportions are recalculated inside this subset only.`
: `${subsetCount} matching-shape tokens. Color proportions are recalculated inside this subset only.`;
document.getElementById("propertyHelp").textContent =
`The population is filtered to the selected token's ${conditionProperty}. ` +
`Percentages use only these ${subsetCount} tokens as their denominator.`;
document.getElementById("denominatorNote").textContent =
`Denominator: only the ${subsetCount} tokens matching the selected ` +
`${conditionProperty}. No outside tokens contribute to the displayed proportions.`;
}
function updateAll() {
updateSelectedCard();
updateSceneState();
updateBreakdown();
propertyButtons.forEach((button) => {
const active = button.dataset.property === conditionProperty;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
}
tokenSelect.addEventListener("change", () => {
selectedToken = tokens.find((token) => token.id === tokenSelect.value);
updateAll();
});
propertyButtons.forEach((button) => {
button.addEventListener("click", () => {
conditionProperty = button.dataset.property;
updateAll();
});
});
// Direct camera controls: orbit, zoom, reset, and click selection.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragging = false;
let pointerDown = null;
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function tokenFromPointer(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickMeshes, false);
return hits.length ? hits[0].object.userData.token : null;
}
function showTooltip(token, event) {
if (!token) {
tooltip.classList.remove("visible");
return;
}
const rect = sceneWrap.getBoundingClientRect();
const color = COLORS[token.color];
const shape = SHAPES[token.shape];
tooltip.innerHTML = `
<strong>${token.id} · ${color.name}</strong>
<div class="tooltip-meta">
<span style="color:${color.hex}">${shapeIconMarkup(token.shape)}</span>
<span>${shape.name} · Click to select</span>
</div>
`;
const x = Math.min(
event.clientX - rect.left + 15,
rect.width - 190
);
const y = Math.min(
event.clientY - rect.top + 15,
rect.height - 80
);
tooltip.style.left = `${Math.max(8, x)}px`;
tooltip.style.top = `${Math.max(8, y)}px`;
tooltip.classList.add("visible");
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerDown = {
x: event.clientX,
y: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false,
pointerId: event.pointerId
};
dragging = true;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (dragging && pointerDown) {
const dx = event.clientX - pointerDown.lastX;
const dy = event.clientY - pointerDown.lastY;
const totalDistance = Math.hypot(
event.clientX - pointerDown.x,
event.clientY - pointerDown.y
);
if (totalDistance > 4) pointerDown.moved = true;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.48,
1.4
);
pointerDown.lastX = event.clientX;
pointerDown.lastY = event.clientY;
updateCamera();
tooltip.classList.remove("visible");
return;
}
const token = tokenFromPointer(event);
canvas.style.cursor = token ? "pointer" : "grab";
showTooltip(token, event);
});
canvas.addEventListener("pointerup", (event) => {
if (!dragging || !pointerDown) return;
const wasClick = !pointerDown.moved;
dragging = false;
canvas.classList.remove("dragging");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (wasClick) {
const token = tokenFromPointer(event);
if (token) {
selectedToken = token;
updateAll();
}
}
pointerDown = null;
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
pointerDown = null;
canvas.classList.remove("dragging");
});
canvas.addEventListener("pointerleave", () => {
if (!dragging) tooltip.classList.remove("visible");
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
11,
26
);
updateCamera();
},
{ passive: false }
);
function resetCamera() {
cameraState.radius = 18;
cameraState.theta = 0.18;
cameraState.phi = 1.02;
updateCamera();
}
document.getElementById("resetCamera").addEventListener("click", resetCamera);
canvas.addEventListener("dblclick", resetCamera);
function resize() {
const width = sceneWrap.clientWidth;
const height = sceneWrap.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
}
new ResizeObserver(resize).observe(sceneWrap);
resize();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const smoothing = 1 - Math.exp(-delta * 11);
const pulse = 1 + Math.sin(clock.elapsedTime * 4.2) * 0.08;
for (const token of tokens) {
const isSelected = token === selectedToken;
const targetOpacity = token.inSubset ? 1 : 0.13;
const targetEdgeOpacity = token.inSubset ? 0.55 : 0.055;
const targetBaseOpacity = token.inSubset ? 0.92 : 0.14;
const targetScale = token.inSubset
? isSelected
? 1.12
: 1.025
: 0.8;
token.material.opacity +=
(targetOpacity - token.material.opacity) * smoothing;
token.edgeMaterial.opacity +=
(targetEdgeOpacity - token.edgeMaterial.opacity) * smoothing;
token.baseMaterial.opacity +=
(targetBaseOpacity - token.baseMaterial.opacity) * smoothing;
token.material.depthWrite = token.material.opacity > 0.9;
token.baseMaterial.depthWrite = token.baseMaterial.opacity > 0.8;
token.material.emissive.copy(
new THREE.Color(COLORS[token.color].hex)
);
token.material.emissiveIntensity = isSelected
? 0.34
: token.hovered
? 0.18
: token.inSubset
? 0.065
: 0;
const nextScale =
token.group.scale.x + (targetScale - token.group.scale.x) * smoothing;
token.group.scale.setScalar(nextScale);
const targetY = isSelected ? 0.075 : token.hovered ? 0.045 : 0;
token.group.position.y +=
(targetY - token.group.position.y) * smoothing;
if (isSelected) {
token.ring.visible = true;
token.ring.scale.setScalar(pulse);
} else {
token.ring.visible = false;
}
}
renderer.render(scene, camera);
}
updateAll();
animate();
</script>
</body>
</html>
```