Prompt specification
Build or customize the Three.js 3D scene "Boccia Jack and Distance Rings" 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: Boccia Jack and Distance Rings
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/boccia-jack-and-distance-rings-x3661
Scene Description Prompt:
I'd like to see this as a stylized Three.js scene: boccia jack and distance rings. Place a jack and three colored balls on a marked tabletop court. Show measured center distances and keep the nearest-ball comparison independent of viewing perspective. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (boccia-jack-and-distance-rings-x3661):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Boccia — Jack & Distance Rings</title>
<style>
:root {
color-scheme: dark;
--bg: #081017;
--panel: rgba(10, 18, 27, 0.86);
--line: rgba(203, 226, 235, 0.14);
--text: #edf7f8;
--muted: #91a7b3;
--accent: #f7ca55;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 30% 20%, #172935 0%, transparent 42%),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene,
#label-layer {
position: fixed;
inset: 0;
}
#scene {
z-index: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas.dragging {
cursor: grabbing;
}
#label-layer {
z-index: 2;
pointer-events: none;
overflow: hidden;
}
.brand {
position: fixed;
z-index: 4;
top: 24px;
left: 24px;
display: flex;
align-items: center;
gap: 12px;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.5);
}
.brand-mark {
display: grid;
place-items: center;
width: 42px;
height: 42px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 13px;
background: rgba(10, 18, 27, 0.62);
color: #f7ca55;
font-size: 27px;
line-height: 1;
backdrop-filter: blur(14px);
}
.brand strong {
display: block;
font-size: 13px;
letter-spacing: 0.16em;
}
.brand span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 12px;
}
.panel {
position: fixed;
z-index: 5;
top: 24px;
right: 24px;
width: min(352px, calc(100vw - 48px));
padding: 20px;
border: 1px solid var(--line);
border-radius: 20px;
background:
linear-gradient(145deg, rgba(25, 43, 54, 0.72), rgba(8, 15, 23, 0.92)),
var(--panel);
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.38),
inset 0 1px 0 rgba(255, 255, 255, 0.06);
backdrop-filter: blur(20px);
}
.eyebrow {
color: #f7ca55;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 7px;
font-size: 27px;
line-height: 1.05;
letter-spacing: -0.035em;
}
.intro {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.nearest-card {
position: relative;
margin: 18px 0 14px;
padding: 15px 16px;
overflow: hidden;
border: 1px solid color-mix(in srgb, var(--card-accent) 50%, transparent);
border-radius: 14px;
background:
linear-gradient(
100deg,
color-mix(in srgb, var(--card-accent) 14%, transparent),
rgba(255, 255, 255, 0.025)
);
}
.nearest-card::after {
position: absolute;
top: -28px;
right: -20px;
width: 90px;
height: 90px;
border: 1px solid color-mix(in srgb, var(--card-accent) 30%, transparent);
border-radius: 50%;
content: "";
}
.nearest-label {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.nearest-main {
display: flex;
align-items: baseline;
gap: 9px;
margin-top: 4px;
}
.nearest-main strong {
font-size: 23px;
letter-spacing: -0.03em;
}
.nearest-main span {
color: var(--card-accent);
font-size: 14px;
font-weight: 800;
}
.comparison {
margin: 4px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.section-label {
display: flex;
justify-content: space-between;
margin: 16px 2px 8px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.section-label span:last-child {
color: #617986;
}
.distance-list {
display: grid;
gap: 6px;
}
.distance-row {
display: grid;
grid-template-columns: 22px 12px 1fr auto;
align-items: center;
min-height: 45px;
padding: 8px 10px;
border: 1px solid transparent;
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
}
.distance-row.nearest {
border-color: color-mix(in srgb, var(--row-accent) 45%, transparent);
background: color-mix(in srgb, var(--row-accent) 9%, rgba(255, 255, 255, 0.035));
}
.rank {
color: #647b87;
font-size: 10px;
font-weight: 800;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--row-accent);
box-shadow: 0 0 12px color-mix(in srgb, var(--row-accent) 65%, transparent);
}
.row-name {
font-size: 12px;
font-weight: 750;
letter-spacing: 0.06em;
}
.row-tag {
margin-left: 6px;
color: var(--row-accent);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.row-distance {
font-size: 16px;
font-weight: 750;
font-variant-numeric: tabular-nums;
letter-spacing: -0.025em;
}
.method-note {
display: flex;
align-items: flex-start;
gap: 9px;
margin-top: 13px;
padding: 10px 11px;
border: 1px solid rgba(125, 184, 195, 0.16);
border-radius: 11px;
background: rgba(89, 160, 171, 0.065);
color: #91aab4;
font-size: 10px;
line-height: 1.45;
}
.method-note b {
color: #b8d9de;
letter-spacing: 0.07em;
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 7px;
margin-top: 15px;
}
button {
min-height: 37px;
padding: 0 10px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 10px;
background: rgba(255, 255, 255, 0.055);
color: #d9e9eb;
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.05em;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(255, 255, 255, 0.24);
background: rgba(255, 255, 255, 0.1);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid #f7ca55;
outline-offset: 2px;
}
button[aria-pressed="true"] {
border-color: rgba(247, 202, 85, 0.45);
background: rgba(247, 202, 85, 0.12);
color: #ffe59c;
}
.hint {
position: fixed;
z-index: 4;
left: 24px;
bottom: 22px;
display: flex;
align-items: center;
gap: 10px;
color: #718994;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.11em;
pointer-events: none;
}
.hint::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: #f7ca55;
box-shadow: 0 0 10px rgba(247, 202, 85, 0.65);
content: "";
}
.world-label {
position: absolute;
top: 0;
left: 0;
white-space: nowrap;
will-change: transform;
}
.measure-label {
display: flex;
align-items: center;
gap: 8px;
padding: 6px 9px;
border: 1px solid var(--accent-border);
border-radius: 9px;
background: rgba(7, 13, 20, 0.88);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(10px);
transform: translate(-50%, -50%);
}
.measure-label::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 9px var(--accent);
content: "";
}
.measure-name {
color: var(--accent);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.1em;
}
.measure-value {
color: #f1f8f9;
font-size: 11px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.ring-label {
padding: 2px 5px;
border-radius: 5px;
background: rgba(8, 18, 24, 0.66);
color: rgba(214, 240, 241, 0.78);
font-size: 8px;
font-weight: 750;
font-variant-numeric: tabular-nums;
letter-spacing: 0.04em;
transform: translate(-50%, -50%);
}
.jack-label,
.nearest-tag {
transform: translate(-50%, -50%);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.13em;
}
.jack-label {
padding: 4px 7px;
border: 1px solid rgba(255, 255, 255, 0.24);
border-radius: 7px;
background: rgba(7, 13, 20, 0.76);
color: #f4f4ed;
}
.nearest-tag {
padding: 5px 8px;
border: 1px solid rgba(247, 202, 85, 0.6);
border-radius: 999px;
background: rgba(50, 39, 9, 0.86);
color: #ffe28e;
box-shadow: 0 0 20px rgba(247, 202, 85, 0.2);
}
@media (max-width: 700px) {
.brand {
top: 14px;
left: 14px;
}
.brand-mark {
width: 36px;
height: 36px;
border-radius: 11px;
font-size: 23px;
}
.brand span {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 14px;
border-radius: 16px;
}
h1 {
margin-top: 4px;
font-size: 21px;
}
.intro,
.method-note {
display: none;
}
.nearest-card {
margin: 10px 0;
padding: 10px 12px;
}
.section-label {
margin-top: 10px;
}
.distance-row {
min-height: 36px;
padding: 5px 8px;
}
.actions {
margin-top: 10px;
}
.hint,
.ring-label {
display: none !important;
}
.measure-label {
gap: 5px;
padding: 4px 6px;
}
.measure-name {
font-size: 8px;
}
.measure-value {
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="label-layer"></div>
<header class="brand">
<div class="brand-mark">◎</div>
<div>
<strong>BOCCIA / GEOMETRY LAB</strong>
<span>Jack and three measured balls</span>
</div>
</header>
<aside class="panel" aria-label="Distance measurements">
<div class="eyebrow">Center-distance comparison</div>
<h1>Distance rings</h1>
<p class="intro">
A stylized tabletop court with measured center-to-center distances from the jack.
</p>
<div class="nearest-card" id="nearest-card">
<div class="nearest-label">Nearest to jack</div>
<div class="nearest-main">
<strong id="nearest-name">YELLOW</strong>
<span id="nearest-distance">0.00 m</span>
</div>
<p class="comparison" id="comparison-text"></p>
</div>
<div class="section-label">
<span>Measured balls</span>
<span>Sorted by world distance</span>
</div>
<div class="distance-list" id="distance-list" aria-live="polite"></div>
<div class="method-note">
<b>VIEW-INDEPENDENT</b>
<span>Distances are calculated from 3D object centers in model space. Camera movement never changes the result or ordering.</span>
</div>
<div class="actions">
<button id="view-button" type="button" aria-pressed="false">Top view</button>
<button id="rings-button" type="button" aria-pressed="true">Rings</button>
<button id="home-button" type="button">Reset</button>
</div>
</aside>
<div class="hint">DRAG TO ORBIT · SCROLL TO ZOOM · SHIFT-DRAG TO PAN</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.querySelector("#scene");
const labelLayer = document.querySelector("#label-layer");
const distanceList = document.querySelector("#distance-list");
const nearestCard = document.querySelector("#nearest-card");
const nearestName = document.querySelector("#nearest-name");
const nearestDistance = document.querySelector("#nearest-distance");
const comparisonText = document.querySelector("#comparison-text");
const viewButton = document.querySelector("#view-button");
const ringsButton = document.querySelector("#rings-button");
const homeButton = document.querySelector("#home-button");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.localClippingEnabled = true;
renderer.domElement.setAttribute("aria-label", "Interactive 3D boccia court");
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x081017);
scene.fog = new THREE.Fog(0x081017, 11, 29);
const perspectiveCamera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.05,
60
);
const orthoCamera = new THREE.OrthographicCamera(-4, 4, 2.2, -2.2, 0.05, 40);
orthoCamera.position.set(0, 10, 0);
orthoCamera.up.set(0, 0, -1);
orthoCamera.lookAt(0, 0, 0);
let activeCamera = perspectiveCamera;
let topView = false;
const cameraState = {
theta: 0.52,
phi: 1.0,
radius: window.innerWidth < 700 ? 14 : 8.0,
target: new THREE.Vector3(0, 0.85, 0)
};
function updatePerspectiveCamera() {
const sinPhi = Math.sin(cameraState.phi);
perspectiveCamera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
perspectiveCamera.lookAt(cameraState.target);
}
updatePerspectiveCamera();
const hemisphere = new THREE.HemisphereLight(0xc5ecff, 0x10151a, 2.05);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff5df, 3.3);
keyLight.position.set(-3.5, 7.5, 4.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 18;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const coolLight = new THREE.PointLight(0x4ca9ff, 22, 13, 2);
coolLight.position.set(4, 3.2, -3);
scene.add(coolLight);
const warmLight = new THREE.PointLight(0xff7c64, 17, 11, 2);
warmLight.position.set(-4, 2.7, 2);
scene.add(warmLight);
const COURT = {
minX: -3,
maxX: 3,
minZ: -1.25,
maxZ: 1.25,
surfaceY: 0.947
};
function makeRadialFloorTexture() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 512;
const context = canvas.getContext("2d");
const gradient = context.createRadialGradient(256, 256, 10, 256, 256, 255);
gradient.addColorStop(0, "#263d49");
gradient.addColorStop(0.48, "#101a22");
gradient.addColorStop(1, "#05080c");
context.fillStyle = gradient;
context.fillRect(0, 0, 512, 512);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
map: makeRadialFloorTexture(),
color: 0xffffff,
roughness: 0.96,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.012;
floor.receiveShadow = true;
scene.add(floor);
const tableFrameMaterial = new THREE.MeshStandardMaterial({
color: 0x18222a,
roughness: 0.46,
metalness: 0.52
});
const tableEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0x35454d,
roughness: 0.3,
metalness: 0.72
});
const legGeometry = new THREE.CylinderGeometry(0.11, 0.145, 0.72, 12);
for (const x of [-2.9, 2.9]) {
for (const z of [-1.22, 1.22]) {
const leg = new THREE.Mesh(legGeometry, tableFrameMaterial);
leg.position.set(x, 0.36, z);
leg.castShadow = true;
leg.receiveShadow = true;
scene.add(leg);
const foot = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.16, 0.045, 16),
tableEdgeMaterial
);
foot.position.set(x, 0.023, z);
foot.castShadow = true;
scene.add(foot);
}
}
const tableTop = new THREE.Mesh(
new THREE.BoxGeometry(6.85, 0.18, 3.32),
tableFrameMaterial
);
tableTop.position.y = 0.81;
tableTop.castShadow = true;
tableTop.receiveShadow = true;
scene.add(tableTop);
const edgeTrim = new THREE.Mesh(
new THREE.BoxGeometry(6.72, 0.045, 3.18),
tableEdgeMaterial
);
edgeTrim.position.y = 0.91;
edgeTrim.castShadow = true;
scene.add(edgeTrim);
const courtMaterial = new THREE.MeshStandardMaterial({
color: 0x174c50,
roughness: 0.91,
metalness: 0.02
});
const courtSurface = new THREE.Mesh(
new THREE.BoxGeometry(6.34, 0.044, 2.72),
courtMaterial
);
courtSurface.position.y = 0.925;
courtSurface.receiveShadow = true;
scene.add(courtSurface);
const markingMaterial = new THREE.MeshBasicMaterial({
color: 0xd9eeee,
transparent: true,
opacity: 0.76,
depthWrite: false
});
const subtleMarkingMaterial = new THREE.MeshBasicMaterial({
color: 0xb6d8d8,
transparent: true,
opacity: 0.34,
depthWrite: false
});
function addCourtSegment(x1, z1, x2, z2, width = 0.025, material = markingMaterial) {
const dx = x2 - x1;
const dz = z2 - z1;
const length = Math.hypot(dx, dz);
const segment = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.003, width),
material
);
segment.position.set(
(x1 + x2) * 0.5,
COURT.surfaceY + 0.004,
(z1 + z2) * 0.5
);
segment.rotation.y = -Math.atan2(dz, dx);
scene.add(segment);
return segment;
}
addCourtSegment(-3, -1.25, 3, -1.25);
addCourtSegment(-3, 1.25, 3, 1.25);
addCourtSegment(-3, -1.25, -3, 1.25);
addCourtSegment(3, -1.25, 3, 1.25);
for (let z = -1.12; z <= 1.12; z += 0.27) {
addCourtSegment(0, z, 0, Math.min(z + 0.145, 1.25), 0.018, subtleMarkingMaterial);
}
addCourtSegment(-2.48, -1.16, -2.48, 1.16, 0.025, subtleMarkingMaterial);
for (const x of [-2, -1, 1, 2]) {
addCourtSegment(x, -1.25, x, -1.14, 0.018, subtleMarkingMaterial);
addCourtSegment(x, 1.14, x, 1.25, 0.018, subtleMarkingMaterial);
}
for (const x of [-3, 3]) {
for (const z of [-1.25, 1.25]) {
const corner = new THREE.Mesh(
new THREE.RingGeometry(0.052, 0.07, 32),
markingMaterial
);
corner.rotation.x = -Math.PI / 2;
corner.position.set(x, COURT.surfaceY + 0.005, z);
scene.add(corner);
}
}
function makeCourtLogoTexture() {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(220, 242, 242, 0.85)";
context.font = "800 78px system-ui, sans-serif";
context.fillText("BOCCIA", 28, 100);
context.fillStyle = "rgba(220, 242, 242, 0.58)";
context.font = "600 30px system-ui, sans-serif";
context.fillText("6.0 × 2.5 m / DISTANCE COURT", 31, 164);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const logo = new THREE.Mesh(
new THREE.PlaneGeometry(2.25, 0.56),
new THREE.MeshBasicMaterial({
map: makeCourtLogoTexture(),
transparent: true,
opacity: 0.23,
depthWrite: false
})
);
logo.rotation.x = -Math.PI / 2;
logo.position.set(2.12, COURT.surfaceY + 0.006, 0.91);
scene.add(logo);
const courtClipPlanes = [
new THREE.Plane(new THREE.Vector3(1, 0, 0), 3),
new THREE.Plane(new THREE.Vector3(-1, 0, 0), 3),
new THREE.Plane(new THREE.Vector3(0, 0, 1), 1.25),
new THREE.Plane(new THREE.Vector3(0, 0, -1), 1.25)
];
const ringsGroup = new THREE.Group();
scene.add(ringsGroup);
const ringRadii = [0.5, 1, 1.5, 2];
const ringLabels = [];
for (const radius of ringRadii) {
const ring = new THREE.Mesh(
new THREE.RingGeometry(radius - 0.007, radius + 0.007, 160),
new THREE.MeshBasicMaterial({
color: 0xb9e6e4,
transparent: true,
opacity: radius === 1 ? 0.32 : 0.23,
side: THREE.DoubleSide,
depthWrite: false,
clippingPlanes: courtClipPlanes
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.y = COURT.surfaceY + 0.007;
ringsGroup.add(ring);
const label = document.createElement("div");
label.className = "world-label ring-label";
label.textContent = `${radius.toFixed(1)} m`;
labelLayer.appendChild(label);
ringLabels.push(label);
}
function addContactDisc(group, radius, floorOffset) {
const disc = new THREE.Mesh(
new THREE.CircleGeometry(radius * 1.22, 48),
new THREE.MeshBasicMaterial({
color: 0x000000,
transparent: true,
opacity: 0.17,
depthWrite: false
})
);
disc.rotation.x = -Math.PI / 2;
disc.position.y = floorOffset + 0.003;
group.add(disc);
}
function addCenterCross(group, radius, floorOffset, color) {
const crossMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.75,
depthWrite: false
});
const a = new THREE.Mesh(
new THREE.BoxGeometry(radius * 1.15, 0.002, 0.009),
crossMaterial
);
a.position.y = floorOffset + 0.006;
const b = new THREE.Mesh(
new THREE.BoxGeometry(0.009, 0.002, radius * 1.15),
crossMaterial
);
b.position.y = floorOffset + 0.006;
group.add(a, b);
}
function addBallSeams(group, radius, seamColor) {
const seamMaterial = new THREE.MeshStandardMaterial({
color: seamColor,
roughness: 0.5,
metalness: 0.02
});
const equator = new THREE.Mesh(
new THREE.TorusGeometry(radius * 0.986, 0.0027, 7, 72),
seamMaterial
);
equator.rotation.x = Math.PI / 2;
equator.castShadow = true;
group.add(equator);
const vertical = new THREE.Mesh(
new THREE.TorusGeometry(radius * 0.986, 0.0025, 7, 72),
seamMaterial
);
vertical.rotation.y = Math.PI / 2;
vertical.castShadow = true;
group.add(vertical);
}
const jackDefinition = {
id: "jack",
label: "JACK",
x: -0.45,
z: -0.12,
radius: 0.075
};
const jackGroup = new THREE.Group();
jackGroup.position.set(
jackDefinition.x,
COURT.surfaceY + jackDefinition.radius,
jackDefinition.z
);
const jackMaterial = new THREE.MeshPhysicalMaterial({
color: 0xf5f2e8,
roughness: 0.24,
metalness: 0.01,
clearcoat: 0.85,
clearcoatRoughness: 0.2
});
const jackSphere = new THREE.Mesh(
new THREE.SphereGeometry(jackDefinition.radius, 48, 32),
jackMaterial
);
jackSphere.castShadow = true;
jackSphere.receiveShadow = true;
jackGroup.add(jackSphere);
const jackStripe = new THREE.Mesh(
new THREE.TorusGeometry(0.072, 0.006, 10, 72),
new THREE.MeshStandardMaterial({ color: 0xe6504d, roughness: 0.34 })
);
jackStripe.rotation.x = Math.PI / 2;
jackStripe.castShadow = true;
jackGroup.add(jackStripe);
const jackDot = new THREE.Mesh(
new THREE.SphereGeometry(0.009, 16, 12),
new THREE.MeshStandardMaterial({ color: 0xe6504d, roughness: 0.3 })
);
jackDot.position.y = jackDefinition.radius * 0.96;
jackGroup.add(jackDot);
const jackFloorOffset = COURT.surfaceY - jackGroup.position.y;
addContactDisc(jackGroup, jackDefinition.radius, jackFloorOffset);
addCenterCross(jackGroup, jackDefinition.radius, jackFloorOffset, 0xf5f2e8);
scene.add(jackGroup);
const jackCenter = jackGroup.position.clone();
const ballDefinitions = [
{
id: "yellow",
label: "YELLOW",
color: 0xf5c542,
css: "#f5c542",
seam: 0x8a6410,
x: 0.58,
z: -0.67,
radius: 0.115
},
{
id: "red",
label: "RED",
color: 0xe34f50,
css: "#ef5b5d",
seam: 0x761d28,
x: -1.82,
z: 0.48,
radius: 0.115
},
{
id: "blue",
label: "BLUE",
color: 0x3479ee,
css: "#4b8cff",
seam: 0x153a8f,
x: 1.35,
z: 0.54,
radius: 0.115
}
];
const ballRecords = ballDefinitions.map((definition, index) => {
const group = new THREE.Group();
group.position.set(
definition.x,
COURT.surfaceY + definition.radius,
definition.z
);
group.rotation.y = index * 0.87 + 0.24;
const material = new THREE.MeshPhysicalMaterial({
color: definition.color,
roughness: 0.22,
metalness: 0.015,
clearcoat: 0.9,
clearcoatRoughness: 0.16
});
const sphere = new THREE.Mesh(
new THREE.SphereGeometry(definition.radius, 56, 36),
material
);
sphere.castShadow = true;
sphere.receiveShadow = true;
group.add(sphere);
addBallSeams(group, definition.radius, definition.seam);
const floorOffset = COURT.surfaceY - group.position.y;
addContactDisc(group, definition.radius, floorOffset);
addCenterCross(group, definition.radius, floorOffset, definition.color);
scene.add(group);
return {
definition,
group,
center: group.position.clone(),
halo: null
};
});
function addMeasurementConnector(start, end, color) {
const direction = end.clone().sub(start);
const length = direction.length();
const midpoint = start.clone().add(end).multiplyScalar(0.5);
const normalized = direction.normalize();
const glow = new THREE.Mesh(
new THREE.CylinderGeometry(0.016, 0.016, length, 10),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.09,
depthWrite: false
})
);
glow.position.copy(midpoint);
glow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
normalized
);
glow.renderOrder = 1;
scene.add(glow);
const connector = new THREE.Mesh(
new THREE.CylinderGeometry(0.007, 0.007, length, 10),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.9,
depthWrite: false
})
);
connector.position.copy(midpoint);
connector.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
normalized
);
connector.renderOrder = 2;
scene.add(connector);
}
const measurements = ballRecords.map((record) => {
const distance = record.center.distanceTo(jackCenter);
const midpoint = jackCenter.clone().add(record.center).multiplyScalar(0.5);
addMeasurementConnector(
jackCenter,
record.center,
record.definition.color
);
const label = document.createElement("div");
label.className = "world-label measure-label";
label.style.setProperty("--accent", record.definition.css);
label.style.setProperty("--accent-border", `${record.definition.css}77`);
label.innerHTML = `
<span class="measure-name">${record.definition.label}</span>
<span class="measure-value">${distance.toFixed(2)} m</span>
`;
labelLayer.appendChild(label);
return {
record,
distance,
midpoint,
label
};
});
measurements.sort((a, b) => a.distance - b.distance);
const nearestMeasurement = measurements[0];
const secondMeasurement = measurements[1];
function renderDistanceList() {
distanceList.replaceChildren();
measurements.forEach((measurement, index) => {
const row = document.createElement("div");
row.className = `distance-row${index === 0 ? " nearest" : ""}`;
row.style.setProperty("--row-accent", measurement.record.definition.css);
row.innerHTML = `
<span class="rank">${String(index + 1).padStart(2, "0")}</span>
<span class="swatch"></span>
<span>
<span class="row-name">${measurement.record.definition.label}</span>
${index === 0 ? '<span class="row-tag">Nearest</span>' : ""}
</span>
<span class="row-distance">${measurement.distance.toFixed(2)} m</span>
`;
distanceList.appendChild(row);
});
}
renderDistanceList();
nearestCard.style.setProperty(
"--card-accent",
nearestMeasurement.record.definition.css
);
nearestName.textContent = nearestMeasurement.record.definition.label;
nearestDistance.textContent = `${nearestMeasurement.distance.toFixed(2)} m`;
const lead = secondMeasurement.distance - nearestMeasurement.distance;
comparisonText.textContent =
`${nearestMeasurement.record.definition.label} is ${lead.toFixed(2)} m ` +
`closer than ${secondMeasurement.record.definition.label}.`;
const nearestHalo = new THREE.Mesh(
new THREE.TorusGeometry(
nearestMeasurement.record.definition.radius + 0.052,
0.009,
10,
80
),
new THREE.MeshBasicMaterial({
color: 0xffdc73,
transparent: true,
opacity: 0.92,
depthWrite: false
})
);
nearestHalo.rotation.x = Math.PI / 2;
nearestHalo.position.y =
COURT.surfaceY -
nearestMeasurement.record.center.y +
0.013;
nearestHalo.renderOrder = 3;
nearestMeasurement.record.group.add(nearestHalo);
nearestMeasurement.record.halo = nearestHalo;
const jackLabel = document.createElement("div");
jackLabel.className = "world-label jack-label";
jackLabel.textContent = "JACK";
labelLayer.appendChild(jackLabel);
const nearestTag = document.createElement("div");
nearestTag.className = "world-label nearest-tag";
nearestTag.textContent = "NEAREST";
labelLayer.appendChild(nearestTag);
const projected = new THREE.Vector3();
function placeWorldLabel(element, worldPosition, visible = true) {
projected.copy(worldPosition).project(activeCamera);
const onScreen =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.25 &&
projected.x < 1.25 &&
projected.y > -1.25 &&
projected.y < 1.25;
element.style.display = visible && onScreen ? "block" : "none";
if (!visible || !onScreen) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.transform = `translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
function updateWorldLabels() {
const ringAngle = 2.6;
ringLabels.forEach((label, index) => {
const radius = ringRadii[index];
const position = new THREE.Vector3(
jackDefinition.x + Math.cos(ringAngle) * radius,
COURT.surfaceY + 0.018,
jackDefinition.z + Math.sin(ringAngle) * radius
);
placeWorldLabel(label, position, ringsGroup.visible);
});
for (const measurement of measurements) {
const position = measurement.midpoint.clone();
position.y += 0.14;
placeWorldLabel(measurement.label, position);
}
placeWorldLabel(
jackLabel,
jackCenter.clone().add(new THREE.Vector3(0, 0.18, 0))
);
placeWorldLabel(
nearestTag,
nearestMeasurement.record.center.clone().add(new THREE.Vector3(0, 0.24, 0))
);
}
function setTopView(enabled) {
topView = enabled;
activeCamera = enabled ? orthoCamera : perspectiveCamera;
viewButton.textContent = enabled ? "Orbit view" : "Top view";
viewButton.setAttribute("aria-pressed", String(enabled));
if (!enabled) updatePerspectiveCamera();
activeCamera.updateMatrixWorld();
}
viewButton.addEventListener("click", () => {
setTopView(!topView);
});
ringsButton.addEventListener("click", () => {
ringsGroup.visible = !ringsGroup.visible;
ringsButton.setAttribute("aria-pressed", String(ringsGroup.visible));
ringsButton.textContent = ringsGroup.visible ? "Rings" : "Hidden";
});
homeButton.addEventListener("click", () => {
cameraState.theta = 0.52;
cameraState.phi = 1.0;
cameraState.radius = window.innerWidth < 700 ? 14 : 8.0;
cameraState.target.set(0, 0.85, 0);
setTopView(false);
updatePerspectiveCamera();
});
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
if (topView) return;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging || topView) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
const panning = event.shiftKey || (event.buttons & 2) !== 0;
if (panning) {
const forward = new THREE.Vector3();
perspectiveCamera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, perspectiveCamera.up)
.normalize();
const panScale = cameraState.radius * 0.00155;
cameraState.target.addScaledVector(right, -dx * panScale);
cameraState.target.addScaledVector(forward, dy * panScale);
cameraState.target.x = THREE.MathUtils.clamp(cameraState.target.x, -2, 2);
cameraState.target.z = THREE.MathUtils.clamp(cameraState.target.z, -1, 1);
} else {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.24,
1.42
);
}
updatePerspectiveCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (event && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
if (topView) return;
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
4,
19
);
updatePerspectiveCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.theta = 0.52;
cameraState.phi = 1.0;
cameraState.radius = window.innerWidth < 700 ? 14 : 8.0;
cameraState.target.set(0, 0.85, 0);
setTopView(false);
updatePerspectiveCamera();
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
perspectiveCamera.aspect = aspect;
perspectiveCamera.fov = aspect < 0.85 ? 48 : 38;
perspectiveCamera.updateProjectionMatrix();
const halfHeight = aspect >= 1
? Math.max(2.15, 3.9 / aspect)
: 3.9 / aspect;
orthoCamera.left = -halfHeight * aspect;
orthoCamera.right = halfHeight * aspect;
orthoCamera.top = halfHeight;
orthoCamera.bottom = -halfHeight;
orthoCamera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
function animate(time) {
requestAnimationFrame(animate);
const pulse = 1 + Math.sin(time * 0.0035) * 0.055;
nearestHalo.scale.set(pulse, pulse, pulse);
renderer.render(scene, activeCamera);
updateWorldLabels();
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```