Prompt specification
Build or customize the Three.js 3D scene "Neighborhood Tokens in Unequal Groups" 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: Neighborhood Tokens in Unequal Groups
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/neighborhood-tokens-in-unequal-groups-x3257
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts population groups and their relative sizes. Arrange colored and shaped resident tokens into three differently sized blocks. Show both group counts and individual tokens so aggregate proportions remain inspectable. Stylized solid forms.
Working Three.js Starter (neighborhood-tokens-in-unequal-groups-x3257):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Population Mosaic</title>
<style>
:root {
color-scheme: dark;
--bg: #07101c;
--panel: rgba(10, 20, 35, 0.84);
--border: rgba(157, 187, 220, 0.18);
--text: #f5f8fc;
--muted: #9aadc3;
--coral: #ff6b70;
--teal: #46d7c3;
--gold: #ffc857;
}
* {
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;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.copy {
position: absolute;
top: 28px;
left: 30px;
max-width: 430px;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.65);
}
.eyebrow {
margin: 0 0 9px;
color: #7fdccf;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 56px);
font-weight: 800;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.panel {
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 22px 60px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.aggregate {
position: absolute;
top: 28px;
right: 30px;
width: 292px;
padding: 18px 20px 17px;
}
.aggregate-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
}
.aggregate-label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.aggregate-total {
font-size: 30px;
font-weight: 800;
letter-spacing: -0.04em;
}
.aggregate-total small {
color: var(--muted);
font-size: 12px;
font-weight: 600;
letter-spacing: 0;
}
.aggregate-bar {
display: flex;
width: 100%;
height: 9px;
margin-top: 15px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.06);
}
.aggregate-bar span {
display: block;
height: 100%;
}
.bar-a {
width: 50%;
background: var(--coral);
}
.bar-b {
width: 31.25%;
background: var(--teal);
}
.bar-c {
width: 18.75%;
background: var(--gold);
}
.aggregate-caption {
margin-top: 10px;
color: #8298af;
font-size: 11px;
line-height: 1.4;
}
.legend {
position: absolute;
bottom: 28px;
left: 30px;
width: 320px;
padding: 17px 18px 14px;
}
.legend-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.legend-title span:last-child {
color: #61758b;
font-weight: 600;
letter-spacing: 0;
text-transform: none;
}
.legend-row {
display: grid;
grid-template-columns: 27px 1fr auto;
align-items: center;
gap: 10px;
min-height: 54px;
border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.shape {
display: block;
width: 16px;
height: 16px;
margin: auto;
background: currentColor;
box-shadow: 0 0 18px color-mix(in srgb, currentColor 35%, transparent);
}
.shape.circle {
border-radius: 50%;
}
.shape.square {
border-radius: 3px;
transform: rotate(7deg);
}
.shape.triangle {
width: 0;
height: 0;
background: transparent;
border-right: 9px solid transparent;
border-bottom: 17px solid currentColor;
border-left: 9px solid transparent;
box-shadow: none;
}
.legend-name {
font-size: 13px;
font-weight: 750;
}
.legend-description {
display: block;
margin-top: 2px;
color: #8298af;
font-size: 10px;
}
.legend-metric {
text-align: right;
}
.legend-metric strong {
display: block;
font-size: 17px;
line-height: 1;
}
.legend-metric small {
color: var(--muted);
font-size: 10px;
}
.controls {
position: absolute;
right: 30px;
bottom: 28px;
display: flex;
align-items: center;
gap: 8px;
pointer-events: auto;
}
.control-button {
display: grid;
width: 42px;
height: 42px;
padding: 0;
place-items: center;
border: 1px solid var(--border);
border-radius: 13px;
background: var(--panel);
color: var(--text);
cursor: pointer;
font: 700 19px/1 system-ui, sans-serif;
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.25);
transition:
transform 150ms ease,
border-color 150ms ease,
background 150ms ease;
}
.control-button:hover {
border-color: rgba(127, 220, 207, 0.55);
background: rgba(20, 40, 58, 0.92);
transform: translateY(-2px);
}
.control-button:active {
transform: translateY(0);
}
.control-button.reset {
width: auto;
padding: 0 15px;
gap: 7px;
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.instructions {
position: absolute;
bottom: 31px;
left: 50%;
color: #70859c;
font-size: 11px;
text-align: center;
transform: translateX(-50%);
pointer-events: none;
white-space: nowrap;
}
.instructions strong {
color: #a8bacd;
font-weight: 650;
}
.tooltip {
position: fixed;
z-index: 20;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 10px;
background: rgba(5, 12, 22, 0.94);
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.34);
color: #edf5fb;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.025em;
opacity: 0;
pointer-events: none;
transform: translateY(3px);
transition:
opacity 100ms ease,
transform 100ms ease;
white-space: nowrap;
}
.tooltip.visible {
opacity: 1;
transform: translateY(0);
}
#fallback {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
padding: 30px;
place-items: center;
background: var(--bg);
color: var(--text);
text-align: center;
}
#fallback[hidden] {
display: none;
}
@media (max-width: 760px) {
.copy {
top: 16px;
left: 16px;
max-width: calc(100vw - 32px);
}
h1 {
font-size: 31px;
}
.subtitle {
max-width: 255px;
margin-top: 9px;
font-size: 11px;
}
.aggregate {
top: auto;
right: 14px;
bottom: 154px;
width: 190px;
padding: 13px 14px;
}
.aggregate-total {
font-size: 24px;
}
.aggregate-caption {
display: none;
}
.aggregate-bar {
margin-top: 10px;
}
.legend {
right: 14px;
bottom: 14px;
left: 14px;
width: auto;
padding: 12px 13px 10px;
}
.legend-title {
margin-bottom: 7px;
}
.legend-row {
grid-template-columns: 20px 1fr auto;
min-height: 47px;
}
.legend-description {
display: none;
}
.legend-name {
font-size: 11px;
}
.legend-metric strong {
font-size: 14px;
}
.controls {
right: 14px;
bottom: 155px;
}
.control-button.reset {
display: none;
}
.instructions {
display: none;
}
}
@media (max-height: 620px) and (min-width: 761px) {
.aggregate {
top: 18px;
right: 18px;
}
.copy {
top: 18px;
left: 20px;
}
.legend {
bottom: 18px;
left: 20px;
}
.controls {
right: 18px;
bottom: 18px;
}
}
</style>
</head>
<body>
<div
id="stage"
role="img"
aria-label="Three-dimensional population mosaic with three blocks and 96 individual resident tokens."
></div>
<div class="hud">
<header class="copy">
<p class="eyebrow">Community census / 2025</p>
<h1>Population Mosaic</h1>
<p class="subtitle">
96 residents across three groups. Each token represents one person;
block area and resident count share the same proportions.
</p>
</header>
<section class="panel aggregate" aria-label="Aggregate population">
<div class="aggregate-heading">
<span class="aggregate-label">Total residents</span>
<span class="aggregate-total">96 <small>people</small></span>
</div>
<div class="aggregate-bar" aria-hidden="true">
<span class="bar-a"></span>
<span class="bar-b"></span>
<span class="bar-c"></span>
</div>
<div class="aggregate-caption">
Population share: 50% · 31.25% · 18.75%
</div>
</section>
<section class="panel legend" aria-label="Population group legend">
<div class="legend-title">
<span>Population groups</span>
<span>Count / share</span>
</div>
<div class="legend-row">
<span class="shape circle" style="color: var(--coral)"></span>
<div>
<div class="legend-name">Group A</div>
<span class="legend-description">Coral · sphere tokens</span>
</div>
<div class="legend-metric">
<strong>48</strong>
<small>50%</small>
</div>
</div>
<div class="legend-row">
<span class="shape square" style="color: var(--teal)"></span>
<div>
<div class="legend-name">Group B</div>
<span class="legend-description">Teal · cube tokens</span>
</div>
<div class="legend-metric">
<strong>30</strong>
<small>31.25%</small>
</div>
</div>
<div class="legend-row">
<span class="shape triangle" style="color: var(--gold)"></span>
<div>
<div class="legend-name">Group C</div>
<span class="legend-description">Gold · cone tokens</span>
</div>
<div class="legend-metric">
<strong>18</strong>
<small>18.75%</small>
</div>
</div>
</section>
<div class="controls" aria-label="Camera controls">
<button class="control-button" id="zoom-out" aria-label="Zoom out">−</button>
<button class="control-button" id="zoom-in" aria-label="Zoom in">+</button>
<button class="control-button reset" id="reset-view">
<span aria-hidden="true">↺</span>
Reset
</button>
</div>
<div class="instructions">
<strong>Drag</strong> to orbit ·
<strong>Scroll</strong> to zoom ·
<strong>Hover</strong> a token
</div>
</div>
<div id="tooltip" class="tooltip" role="status"></div>
<div id="fallback" hidden>
<div>
<h2>WebGL is unavailable</h2>
<p>This scene needs a browser with WebGL enabled.</p>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const tooltip = document.querySelector("#tooltip");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
} catch (error) {
document.querySelector("#fallback").hidden = false;
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x07101c, 1);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101c);
const camera = new THREE.OrthographicCamera(-10, 10, 8, -8, 0.1, 100);
const controls = {
theta: 0.62,
phi: 0.73,
zoom: 1
};
const cameraTarget = new THREE.Vector3(-0.6, 0.25, 0.55);
const cameraRadius = 30;
const hemisphere = new THREE.HemisphereLight(0xb9d9ff, 0x18202d, 1.65);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1dc, 3.1);
keyLight.position.set(-5, 15, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -19;
keyLight.shadow.camera.right = 19;
keyLight.shadow.camera.top = 18;
keyLight.shadow.camera.bottom = -18;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 45;
keyLight.shadow.bias = -0.00025;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x75bfff, 1.15);
fillLight.position.set(12, 7, -10);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0xff7f9d, 18, 24, 2);
rimLight.position.set(-8, 5, -5);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(52, 42),
new THREE.MeshStandardMaterial({
color: 0x081321,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.055;
ground.receiveShadow = true;
scene.add(ground);
const floorGrid = new THREE.GridHelper(
48,
48,
0x1b3a54,
0x10263a
);
floorGrid.position.y = -0.035;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.46;
scene.add(floorGrid);
const groups = [
{
id: "A",
name: "Group A",
count: 48,
columns: 8,
rows: 6,
shape: "sphere",
token: 0xff6b70,
top: 0x512936,
side: 0x182333,
percent: "50%"
},
{
id: "B",
name: "Group B",
count: 30,
columns: 6,
rows: 5,
shape: "cube",
token: 0x46d7c3,
top: 0x173e44,
side: 0x132735,
percent: "31.25%"
},
{
id: "C",
name: "Group C",
count: 18,
columns: 6,
rows: 3,
shape: "cone",
token: 0xffc857,
top: 0x493a20,
side: 0x252437,
percent: "18.75%"
}
];
const tokenMeshes = [];
const groupObjects = [];
function roundedRectShape(width, height, radius) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = -height / 2;
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function roundedPath(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.arcTo(x + width, y, x + width, y + height, r);
context.arcTo(x + width, y + height, x, y + height, r);
context.arcTo(x, y + height, x, y, r);
context.arcTo(x, y, x + width, y, r);
context.closePath();
}
function createLabelTexture(group) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 240;
const context = canvas.getContext("2d");
const color = `#${group.token.toString(16).padStart(6, "0")}`;
const gradient = context.createLinearGradient(0, 0, 1024, 240);
gradient.addColorStop(0, "rgba(11, 23, 40, 0.98)");
gradient.addColorStop(1, "rgba(7, 16, 28, 0.96)");
roundedPath(context, 3, 3, 1018, 234, 32);
context.fillStyle = gradient;
context.fill();
context.strokeStyle = color;
context.globalAlpha = 0.55;
context.lineWidth = 3;
context.stroke();
context.globalAlpha = 1;
context.fillStyle = color;
roundedPath(context, 3, 3, 12, 234, 6);
context.fill();
context.textAlign = "left";
context.fillStyle = "#a9bbce";
context.font = "800 28px system-ui, sans-serif";
context.letterSpacing = "3px";
context.fillText(group.name.toUpperCase(), 48, 56);
context.fillStyle = "#f7fbff";
context.font = "800 82px system-ui, sans-serif";
context.fillText(String(group.count), 44, 157);
context.fillStyle = "#9fb2c7";
context.font = "750 25px system-ui, sans-serif";
context.fillText("RESIDENTS", 205, 142);
context.textAlign = "right";
context.fillStyle = color;
context.font = "800 60px system-ui, sans-serif";
context.fillText(`${group.percent}%`, 970, 142);
context.fillStyle = "#8399af";
context.font = "700 21px system-ui, sans-serif";
context.fillText("POPULATION SHARE", 970, 181);
context.textAlign = "left";
context.strokeStyle = "rgba(255,255,255,0.10)";
context.lineWidth = 1;
context.beginPath();
context.moveTo(44, 194);
context.lineTo(980, 194);
context.stroke();
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
return texture;
}
function createTokenGeometry(shape) {
if (shape === "sphere") {
return new THREE.SphereGeometry(0.29, 18, 12);
}
if (shape === "cube") {
return new THREE.BoxGeometry(0.5, 0.5, 0.5, 2, 2, 2);
}
return new THREE.ConeGeometry(0.34, 0.68, 18, 1);
}
let residentNumber = 1;
function createGroup(group, groupIndex) {
const container = new THREE.Group();
container.userData.group = group;
container.userData.groupIndex = groupIndex;
const width = group.columns;
const depth = group.rows;
const height = 0.62;
const radius = 0.26;
const shape = roundedRectShape(width, depth, radius);
const sideMaterial = new THREE.MeshStandardMaterial({
color: group.side,
roughness: 0.68,
metalness: 0.06
});
const topMaterial = new THREE.MeshStandardMaterial({
color: group.top,
roughness: 0.83,
metalness: 0.02
});
const baseGeometry = new THREE.ExtrudeGeometry(shape, {
depth: height,
bevelEnabled: false,
curveSegments: 12
});
baseGeometry.rotateX(-Math.PI / 2);
const base = new THREE.Mesh(baseGeometry, sideMaterial);
base.castShadow = true;
base.receiveShadow = true;
container.add(base);
const topGeometry = new THREE.ShapeGeometry(
roundedRectShape(width, depth, radius),
12
);
topGeometry.rotateX(-Math.PI / 2);
const top = new THREE.Mesh(topGeometry, topMaterial);
top.position.y = height + 0.011;
top.receiveShadow = true;
container.add(top);
const outlinePoints = shape.getPoints(64).map((point) => {
return new THREE.Vector3(point.x, height + 0.03, point.y);
});
const outline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(outlinePoints),
new THREE.LineBasicMaterial({
color: group.token,
transparent: true,
opacity: 0.48
})
);
container.add(outline);
const gridPoints = [];
for (let column = 1; column < group.columns; column++) {
const x = -width / 2 + column;
gridPoints.push(
new THREE.Vector3(x, height + 0.026, -depth / 2 + 0.28),
new THREE.Vector3(x, height + 0.026, depth / 2 - 0.28)
);
}
for (let row = 1; row < group.rows; row++) {
const z = -depth / 2 + row;
gridPoints.push(
new THREE.Vector3(-width / 2 + 0.28, height + 0.026, z),
new THREE.Vector3(width / 2 - 0.28, height + 0.026, z)
);
}
const grid = new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints(gridPoints),
new THREE.LineBasicMaterial({
color: group.token,
transparent: true,
opacity: 0.15
})
);
container.add(grid);
const tokenGeometry = createTokenGeometry(group.shape);
for (let row = 0; row < group.rows; row++) {
for (let column = 0; column < group.columns; column++) {
const material = new THREE.MeshStandardMaterial({
color: group.token,
roughness: 0.43,
metalness: 0.08,
emissive: group.token,
emissiveIntensity: 0
});
const token = new THREE.Mesh(tokenGeometry, material);
token.position.set(
column - (group.columns - 1) / 2,
height + (
group.shape === "sphere" ? 0.29 :
group.shape === "cube" ? 0.25 :
0.34
),
row - (group.rows - 1) / 2
);
if (group.shape === "cube") {
token.rotation.y = ((column + row) % 3) * 0.08;
}
if (group.shape === "cone") {
token.rotation.y = (column * 0.23 + row * 0.17) % (Math.PI / 2);
}
token.castShadow = true;
token.receiveShadow = true;
token.userData.groupIndex = groupIndex;
token.userData.residentId =
`R-${String(residentNumber++).padStart(3, "0")}`;
tokenMeshes.push(token);
container.add(token);
}
}
const label = new THREE.Sprite(
new THREE.SpriteMaterial({
map: createLabelTexture(group),
transparent: true,
depthTest: false,
depthWrite: false
})
);
label.position.set(0, 1.85, 0);
label.scale.set(4.8, 1.125, 1);
label.renderOrder = 20;
container.add(label);
scene.add(container);
groupObjects.push(container);
}
groups.forEach(createGroup);
let currentPortrait = null;
function layoutGroups(portrait) {
if (portrait) {
groupObjects[0].position.set(0, 0, 3.8);
groupObjects[1].position.set(0, 0, -4.0);
groupObjects[2].position.set(0, 0, -9.5);
} else {
groupObjects[0].position.set(-5.2, 0, 0.7);
groupObjects[1].position.set(4.4, 0, -1.8);
groupObjects[2].position.set(4.6, 0, 3.7);
}
}
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
const cosPhi = Math.cos(controls.phi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(controls.theta),
cameraTarget.y + cameraRadius * cosPhi,
cameraTarget.z + cameraRadius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(cameraTarget);
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
const aspect = width / height;
const portrait = aspect < 0.82;
renderer.setSize(width, height, false);
const minimumWidth = portrait ? 10.5 : 22;
const preferredHeight = portrait ? 22 : 15;
const viewHeight = Math.max(preferredHeight, minimumWidth / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
layoutGroups(portrait);
if (currentPortrait !== portrait) {
currentPortrait = portrait;
controls.theta = portrait ? 0.18 : 0.62;
controls.phi = portrait ? 0.74 : 0.73;
controls.zoom = 1;
cameraTarget.set(
portrait ? 0 : -0.6,
0.25,
portrait ? -3.15 : 0.55
);
}
updateCamera();
}
window.addEventListener("resize", resize);
resize();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let hoveredToken = null;
function clearHover() {
if (hoveredToken) {
hoveredToken.material.emissiveIntensity = 0;
hoveredToken.scale.setScalar(1);
hoveredToken = null;
}
tooltip.classList.remove("visible");
}
function setHover(token) {
if (hoveredToken === token) {
return;
}
clearHover();
if (token) {
hoveredToken = token;
hoveredToken.material.emissiveIntensity = 0.52;
hoveredToken.scale.setScalar(1.17);
}
}
function updateHover(event) {
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(tokenMeshes, false);
if (intersections.length === 0) {
setHover(null);
renderer.domElement.style.cursor = "grab";
return;
}
const token = intersections[0].object;
setHover(token);
renderer.domElement.style.cursor = "pointer";
const group = groups[token.userData.groupIndex];
tooltip.textContent =
`${group.name} · Resident ${token.userData.residentId}`;
const tooltipWidth = 220;
const left = Math.min(
event.clientX + 16,
window.innerWidth - tooltipWidth - 10
);
const top = Math.min(
event.clientY + 16,
window.innerHeight - 50
);
tooltip.style.left = `${Math.max(8, left)}px`;
tooltip.style.top = `${top}px`;
tooltip.classList.add("visible");
}
let dragging = false;
let pointerId = null;
let previousX = 0;
let previousY = 0;
let moved = false;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.pointerType === "mouse" && event.button !== 0) {
return;
}
dragging = true;
moved = false;
pointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
clearHover();
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== pointerId) {
updateHover(event);
return;
}
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (Math.abs(deltaX) + Math.abs(deltaY) > 1) {
moved = true;
}
controls.theta -= deltaX * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi + deltaY * 0.005,
0.28,
1.28
);
updateCamera();
});
function endDrag(event) {
if (!dragging) {
return;
}
dragging = false;
if (
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
pointerId = null;
renderer.domElement.style.cursor = "grab";
updateHover(event);
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
pointerId = null;
clearHover();
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!dragging) {
clearHover();
}
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.4
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
function zoomBy(factor) {
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * factor,
0.65,
2.4
);
updateCamera();
}
function resetView() {
const portrait = currentPortrait;
controls.theta = portrait ? 0.18 : 0.62;
controls.phi = portrait ? 0.74 : 0.73;
controls.zoom = 1;
cameraTarget.set(
portrait ? 0 : -0.6,
0.25,
portrait ? -3.15 : 0.55
);
updateCamera();
}
document.querySelector("#zoom-in").addEventListener("click", () => {
zoomBy(1.15);
});
document.querySelector("#zoom-out").addEventListener("click", () => {
zoomBy(1 / 1.15);
});
document.querySelector("#reset-view").addEventListener("click", resetView);
window.addEventListener("keydown", (event) => {
if (event.key === "+" || event.key === "=") {
zoomBy(1.1);
}
if (event.key === "-" || event.key === "_") {
zoomBy(1 / 1.1);
}
if (event.key === "0") {
resetView();
}
if (event.key === "ArrowLeft") {
controls.theta -= 0.08;
updateCamera();
}
if (event.key === "ArrowRight") {
controls.theta += 0.08;
updateCamera();
}
if (event.key === "ArrowUp") {
controls.phi = Math.max(0.28, controls.phi - 0.06);
updateCamera();
}
if (event.key === "ArrowDown") {
controls.phi = Math.min(1.28, controls.phi + 0.06);
updateCamera();
}
});
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```