Prompt specification
Build or customize the Three.js 3D scene "Small Football Training Ground" 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: Small Football Training Ground
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/small-football-training-ground-x3659
Scene Description Prompt:
Build an original practice pitch with a curved-shot target and movable wall markers. Let users adjust the declared toy spin parameter and inspect repeated trajectories. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (small-football-training-ground-x3659):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Bend Lab — Curved Shot Practice</title>
<style>
:root {
color-scheme: dark;
--ink: #eef8f1;
--muted: #a7b8ae;
--panel: rgba(12, 25, 21, 0.88);
--panel-edge: rgba(198, 255, 218, 0.13);
--mint: #8cf6b2;
--cyan: #74dfff;
--gold: #ffd166;
--coral: #ff786f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b8c7ca;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(340px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 20px;
border: 1px solid var(--panel-edge);
border-radius: 20px;
background: var(--panel);
box-shadow: 0 24px 70px rgba(4, 14, 10, 0.32);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #496459 transparent;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: var(--mint);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
content: "";
border-radius: 2px;
background: var(--mint);
box-shadow: 0 0 16px rgba(140, 246, 178, 0.8);
}
h1 {
margin: 0;
font-size: 30px;
line-height: 1;
letter-spacing: -0.055em;
}
.intro {
margin: 10px 0 19px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.section {
padding-top: 16px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.section:first-of-type {
padding-top: 0;
border-top: 0;
}
.control-head,
.legend-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
label,
.legend-head {
font-size: 12px;
font-weight: 750;
letter-spacing: 0.015em;
}
.tag {
display: inline-block;
margin-left: 5px;
padding: 3px 6px;
border: 1px solid rgba(140, 246, 178, 0.22);
border-radius: 999px;
color: var(--mint);
background: rgba(140, 246, 178, 0.07);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
vertical-align: 1px;
}
output {
color: var(--gold);
font: 800 18px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
input[type="range"] {
width: 100%;
height: 22px;
margin: 12px 0 1px;
accent-color: var(--mint);
cursor: pointer;
}
.range-labels {
display: flex;
justify-content: space-between;
color: #72877c;
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.note {
margin: 11px 0 0;
color: #8fa297;
font-size: 10px;
line-height: 1.5;
}
.row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 13px;
}
.row-label {
flex: 1;
color: var(--muted);
font-size: 11px;
}
select {
min-width: 95px;
padding: 8px 28px 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
outline: 0;
color: var(--ink);
background: #172b23;
font: 750 11px/1 system-ui, sans-serif;
}
button {
border: 0;
border-radius: 11px;
padding: 11px 13px;
color: #082016;
background: var(--mint);
font: 850 11px/1 system-ui, sans-serif;
letter-spacing: 0.025em;
cursor: pointer;
transition:
transform 120ms ease,
filter 120ms ease,
background 120ms ease;
}
button:hover {
filter: brightness(1.08);
transform: translateY(-1px);
}
button:active {
transform: translateY(1px);
}
button.secondary {
color: #d9e7df;
background: rgba(255, 255, 255, 0.08);
}
button.wide {
flex: 1;
}
.button-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 9px;
}
.legend-head {
margin-bottom: 9px;
color: #d8e6de;
}
.legend-head span:last-child {
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
#legend {
display: grid;
gap: 6px;
max-height: 155px;
overflow: auto;
scrollbar-width: thin;
}
.empty {
padding: 12px;
border: 1px dashed rgba(255, 255, 255, 0.12);
border-radius: 10px;
color: #778b80;
font-size: 10px;
text-align: center;
}
.pass-row {
display: grid;
grid-template-columns: 9px 1fr auto;
align-items: center;
gap: 8px;
padding: 8px 9px;
border-radius: 9px;
background: rgba(255, 255, 255, 0.045);
font-size: 10px;
}
.pass-dot {
width: 7px;
height: 7px;
border-radius: 2px;
}
.pass-name {
color: #dce9e1;
font-weight: 700;
}
.pass-result {
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.status-card {
position: fixed;
z-index: 8;
top: 18px;
right: 18px;
width: 218px;
padding: 15px 16px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 15px;
color: #193027;
background: rgba(240, 249, 242, 0.82);
box-shadow: 0 15px 45px rgba(16, 39, 29, 0.16);
backdrop-filter: blur(12px);
pointer-events: none;
}
.status-label {
color: #668075;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#lastResult {
margin: 5px 0 12px;
color: #153d29;
font-size: 18px;
font-weight: 850;
letter-spacing: -0.04em;
}
.status-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.status-grid strong {
display: block;
margin-top: 3px;
font: 800 12px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
}
#hint {
position: fixed;
z-index: 7;
right: 18px;
bottom: 18px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 12px;
color: #244236;
background: rgba(242, 249, 244, 0.8);
box-shadow: 0 10px 35px rgba(12, 35, 24, 0.12);
font-size: 10px;
font-weight: 700;
backdrop-filter: blur(10px);
pointer-events: none;
}
#hint b {
color: #147743;
}
#toast {
position: fixed;
z-index: 20;
left: 50%;
bottom: 25px;
padding: 10px 14px;
border-radius: 999px;
color: #092217;
background: var(--gold);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.18);
font-size: 11px;
font-weight: 850;
opacity: 0;
transform: translate(-50%, 12px);
transition:
opacity 180ms ease,
transform 180ms ease;
pointer-events: none;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 700px) {
.panel {
top: 10px;
left: 10px;
width: min(320px, calc(100vw - 20px));
max-height: 62vh;
padding: 16px;
}
h1 {
font-size: 25px;
}
.intro {
display: none;
}
.status-card {
top: auto;
right: 10px;
bottom: 10px;
width: 180px;
padding: 12px;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<aside class="panel">
<div class="eyebrow">Voxel Lab / Set Piece</div>
<h1>Bend Lab</h1>
<p class="intro">
Bend a shot around a movable wall and into the illuminated goal target.
The live cyan cubes preview the current declared spin.
</p>
<section class="section">
<div class="control-head">
<label for="spin">
Declared toy spin <span class="tag">Fictional</span>
</label>
<output id="spinOut" for="spin">+0.62</output>
</div>
<input
id="spin"
type="range"
min="-1"
max="1"
step="0.01"
value="0.62"
aria-label="Declared toy spin"
/>
<div class="range-labels">
<span>− left bend</span>
<span>straight</span>
<span>right bend +</span>
</div>
<p class="note">
A dimensionless playground parameter that controls lateral bend only.
It is not a measured aerodynamic spin coefficient.
</p>
<div class="row">
<span class="row-label">Repeat passes</span>
<select id="repeatCount" aria-label="Number of repeated passes">
<option value="1">1 pass</option>
<option value="3" selected>3 passes</option>
<option value="5">5 passes</option>
</select>
</div>
<div class="row">
<button id="launch" class="wide">Launch repeats</button>
</div>
<div class="button-grid">
<button id="replay" class="secondary">Replay traces</button>
<button id="clear" class="secondary">Clear traces</button>
<button id="resetWall" class="secondary">Reset wall</button>
<button id="resetCamera" class="secondary">Reset camera</button>
</div>
</section>
<section class="section">
<div class="legend-head">
<span>Pass archive</span>
<span id="passCount">0 traces</span>
</div>
<div id="legend">
<div class="empty">No trajectories recorded yet.</div>
</div>
<p class="note">
Repeats keep the declared spin fixed and use tiny launch offsets so their
colored traces remain separately visible.
</p>
</section>
</aside>
<aside class="status-card">
<div class="status-label">Current prediction</div>
<div id="lastResult">ON TARGET</div>
<div class="status-grid">
<div>
<span class="status-label">Aim result</span>
<strong id="aimResult">TARGET HIT</strong>
</div>
<div>
<span class="status-label">Bend</span>
<strong id="bendValue">RIGHT</strong>
</div>
</div>
</aside>
<div id="hint">
<b>Drag pitch</b> to orbit · <b>wheel</b> to zoom · <b>drag red markers</b>
</div>
<div id="toast"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.08;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9c9cb);
scene.fog = new THREE.Fog(0xb9c9cb, 55, 105);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
160
);
const hemisphere = new THREE.HemisphereLight(0xe9fbff, 0x31583c, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4d6, 3.2);
sun.position.set(-20, 35, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -34;
sun.shadow.camera.right = 34;
sun.shadow.camera.top = 34;
sun.shadow.camera.bottom = -34;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 90;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const goalLight = new THREE.PointLight(0xffc85c, 9, 13, 2);
const TARGET_X = 3.25;
const TARGET_Y = 3.2;
goalLight.position.set(TARGET_X, TARGET_Y, -19.2);
scene.add(goalLight);
const GOAL_Z = -20.15;
const BALL_START_Z = 15;
const BALL_START_Y = 0.38;
const GOAL_HALF_WIDTH = 5.5;
const GOAL_HEIGHT = 4.4;
const palette = [
0x8cf6b2,
0x74dfff,
0xffd166,
0xc79cff,
0xff8e78,
0x74f0dd,
0xffa8dc
];
function makeBox(w, h, d, x, y, z, material, cast = true, receive = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
scene.add(mesh);
return mesh;
}
const soilMat = new THREE.MeshStandardMaterial({
color: 0x554d39,
roughness: 1
});
makeBox(29, 1.1, 47, 0, -0.65, 0, soilMat, false, true);
// Voxel grass tiles.
const tileGeometry = new THREE.BoxGeometry(1.94, 0.16, 1.94);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
});
const tileCountX = 13;
const tileCountZ = 22;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileCountX * tileCountZ
);
tiles.receiveShadow = true;
tiles.castShadow = false;
const dummy = new THREE.Object3D();
let tileIndex = 0;
for (let x = 0; x < tileCountX; x++) {
for (let z = 0; z < tileCountZ; z++) {
dummy.position.set(-12 + x * 2, 0.08, -21 + z * 2);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const color = new THREE.Color();
color.setHSL(
0.295 + (((x + z) % 3) - 1) * 0.007,
0.42,
0.29 + (((x * 7 + z * 3) % 5) - 2) * 0.009
);
tiles.setColorAt(tileIndex, color);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
scene.add(tiles);
// Field markings.
const lineMat = new THREE.MeshStandardMaterial({
color: 0xf1f7e9,
roughness: 0.95
});
function fieldLine(w, d, x, z) {
return makeBox(w, 0.035, d, x, 0.185, z, lineMat, false, true);
}
fieldLine(25, 0.1, 0, -20);
fieldLine(25, 0.1, 0, 20);
fieldLine(0.1, 40, -12.5, 0);
fieldLine(0.1, 40, 12.5, 0);
fieldLine(25, 0.1, 0, 0);
// Penalty areas.
fieldLine(0.1, 8, -7, -16);
fieldLine(0.1, 8, 7, -16);
fieldLine(14, 0.1, 0, -12);
// Six-yard area.
fieldLine(0.1, 3.2, -3.7, -18.4);
fieldLine(0.1, 3.2, 3.7, -18.4);
fieldLine(7.4, 0.1, 0, -16.8);
// Center circle, assembled from short voxel-like segments.
for (let i = 0; i < 28; i++) {
const angle = (i / 28) * Math.PI * 2;
const radius = 4.1;
const segment = makeBox(
0.78,
0.035,
0.13,
Math.cos(angle) * radius,
0.185,
Math.sin(angle) * radius,
lineMat,
false,
true
);
segment.rotation.y = -angle;
}
makeBox(0.22, 0.06, 0.22, 0, 0.21, 0, lineMat, false, false);
makeBox(0.22, 0.06, 0.22, 0, 0.21, -15.5, lineMat, false, false);
// ---------------------------------------------------------------------
// Goal and voxel net
// ---------------------------------------------------------------------
const frameMat = new THREE.MeshStandardMaterial({
color: 0xf8fff4,
roughness: 0.5,
metalness: 0.05
});
makeBox(0.2, 4.55, 0.2, -5.6, 2.275, -20.4, frameMat);
makeBox(0.2, 4.55, 0.2, 5.6, 2.275, -20.4, frameMat);
makeBox(11.4, 0.2, 0.2, 0, 4.55, -20.4, frameMat);
const netMat = new THREE.MeshStandardMaterial({
color: 0xe9fff1,
transparent: true,
opacity: 0.48,
roughness: 1,
depthWrite: false
});
for (let x = -5.5; x <= 5.51; x += 0.5) {
makeBox(0.035, 4.35, 0.035, x, 2.18, -21.25, netMat, false, false);
}
for (let y = 0.2; y <= 4.51; y += 0.45) {
makeBox(11, 0.035, 0.035, 0, y, -21.25, netMat, false, false);
}
for (let x = -5.5; x <= 5.51; x += 0.5) {
makeBox(0.035, 0.035, 0.85, x, 4.5, -20.83, netMat, false, false);
}
// Curved, illuminated target ring.
const targetGroup = new THREE.Group();
targetGroup.position.set(TARGET_X, TARGET_Y, -20.48);
scene.add(targetGroup);
const targetMat = new THREE.MeshStandardMaterial({
color: 0xffce59,
emissive: 0xff9e24,
emissiveIntensity: 1.8,
roughness: 0.32
});
const targetPieceGeometry = new THREE.BoxGeometry(0.17, 0.17, 0.17);
for (let i = 0; i < 36; i++) {
const angle = (i / 36) * Math.PI * 2;
const piece = new THREE.Mesh(targetPieceGeometry, targetMat);
piece.position.set(
Math.cos(angle) * 1.05,
Math.sin(angle) * 0.78,
0
);
piece.castShadow = true;
targetGroup.add(piece);
}
const reticleMat = new THREE.MeshBasicMaterial({ color: 0xfff0a6 });
const reticleGeometry = new THREE.BoxGeometry(0.16, 0.16, 0.16);
for (const y of [-0.3, 0.3]) {
const piece = new THREE.Mesh(reticleGeometry, reticleMat);
piece.position.set(0, y, 0.04);
targetGroup.add(piece);
}
// ---------------------------------------------------------------------
// Low voxel surroundings
// ---------------------------------------------------------------------
const standMaterials = [
new THREE.MeshStandardMaterial({ color: 0x346f78, roughness: 1 }),
new THREE.MeshStandardMaterial({ color: 0x8d5260, roughness: 1 }),
new THREE.MeshStandardMaterial({ color: 0x5f704b, roughness: 1 })
];
for (const side of [-1, 1]) {
for (let row = 0; row < 3; row++) {
for (let z = -18; z <= 18; z += 3) {
makeBox(
1.1,
0.5 + row * 0.32,
2.2,
side * (14.2 + row * 0.95),
0.2 + row * 0.16,
z,
standMaterials[(Math.abs(z) + row) % standMaterials.length],
true,
true
);
}
}
}
const trunkMat = new THREE.MeshStandardMaterial({
color: 0x684a32,
roughness: 1
});
const leafMat = new THREE.MeshStandardMaterial({
color: 0x3e7651,
roughness: 1
});
const treeLocations = [
[-18, -25],
[-21, -18],
[18, -25],
[22, -16],
[-19, 22],
[20, 23]
];
for (const [x, z] of treeLocations) {
makeBox(0.55, 2.6, 0.55, x, 1.1, z, trunkMat);
makeBox(2.2, 1.7, 2.2, x, 2.8, z, leafMat);
makeBox(1.5, 1.2, 1.5, x, 3.75, z, leafMat);
}
// ---------------------------------------------------------------------
// Draggable wall markers
// ---------------------------------------------------------------------
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xe6534d,
roughness: 0.75
});
const markerDarkMaterial = new THREE.MeshStandardMaterial({
color: 0x8f3036,
roughness: 0.85
});
const markerSkinMaterial = new THREE.MeshStandardMaterial({
color: 0xf1b88d,
roughness: 0.85
});
const hitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
});
const markers = [];
const hitboxes = [];
function createMarker(index, x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
const base = makeBox(
0.95,
0.12,
0.85,
0,
0.18,
0,
markerMaterial.clone()
);
const body = new THREE.Mesh(
new THREE.BoxGeometry(0.72, 0.95, 0.42),
markerMaterial
);
body.position.y = 1.32;
body.castShadow = true;
group.add(body);
const head = new THREE.Mesh(
new THREE.BoxGeometry(0.43, 0.43, 0.43),
markerSkinMaterial
);
head.position.y = 2.05;
head.castShadow = true;
group.add(head);
for (const side of [-1, 1]) {
const leg = new THREE.Mesh(
new THREE.BoxGeometry(0.23, 0.85, 0.28),
markerDarkMaterial
);
leg.position.set(side * 0.2, 0.58, 0);
leg.castShadow = true;
group.add(leg);
const arm = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 0.78, 0.22),
markerMaterial
);
arm.position.set(side * 0.5, 1.35, 0);
arm.rotation.z = side * 0.1;
arm.castShadow = true;
group.add(arm);
}
const hitbox = new THREE.Mesh(
new THREE.BoxGeometry(1.05, 2.75, 0.95),
hitMaterial
);
hitbox.position.y = 1.4;
hitbox.userData.markerIndex = index;
group.add(hitbox);
hitboxes.push(hitbox);
group.userData.index = index;
group.userData.originalX = x;
group.userData.originalZ = z;
group.userData.base = base;
group.add(base);
scene.add(group);
markers.push({
group,
base,
originalX: x,
originalZ: z
});
}
const initialWallX = [-2.4, -1.2, 0, 1.2, 2.4];
initialWallX.forEach((x, i) => createMarker(i, x, 8.5));
// ---------------------------------------------------------------------
// Voxel ball
// ---------------------------------------------------------------------
function createVoxelBall() {
const group = new THREE.Group();
const white = new THREE.MeshStandardMaterial({
color: 0xf6fff4,
roughness: 0.62
});
const dark = new THREE.MeshStandardMaterial({
color: 0x172229,
roughness: 0.72
});
const geometry = new THREE.BoxGeometry(0.18, 0.18, 0.18);
const blackPattern = new Set([
"1,0,0",
"-1,0,0",
"0,1,0",
"0,-1,0",
"0,0,1",
"0,0,-1"
]);
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
for (let z = -1; z <= 1; z++) {
const cube = new THREE.Mesh(
geometry,
blackPattern.has(`${x},${y},${z}`) ? dark : white
);
cube.position.set(x * 0.18, y * 0.18, z * 0.18);
cube.castShadow = true;
group.add(cube);
}
}
}
return group;
}
const liveBall = createVoxelBall();
scene.add(liveBall);
const replayBall = createVoxelBall();
replayBall.visible = false;
scene.add(replayBall);
const ballShadow = makeBox(
0.64,
0.035,
0.64,
0,
0.205,
BALL_START_Z,
new THREE.MeshBasicMaterial({
color: 0x17251d,
transparent: true,
opacity: 0.26,
depthWrite: false
}),
false,
false
);
// ---------------------------------------------------------------------
// Predicted trajectory made from small voxel dots
// ---------------------------------------------------------------------
const guideMaterial = new THREE.MeshStandardMaterial({
color: 0x74dfff,
emissive: 0x39bde8,
emissiveIntensity: 1.2,
transparent: true,
opacity: 0.86,
roughness: 0.5
});
const guideGeometry = new THREE.BoxGeometry(0.13, 0.13, 0.13);
const guide = new THREE.InstancedMesh(guideGeometry, guideMaterial, 110);
guide.count = 0;
guide.frustumCulled = false;
scene.add(guide);
// ---------------------------------------------------------------------
// Simulation
// ---------------------------------------------------------------------
function markerSnapshot() {
return markers.map((marker) => ({
x: marker.group.position.x,
z: marker.group.position.z
}));
}
function positionAt(t, params) {
return new THREE.Vector3(
params.startX + 0.5 * params.ax * t * t,
params.startY + params.vy * t - 0.5 * params.gravity * t * t,
params.startZ + params.vz * t
);
}
function classifyGoalPoint(point) {
const dx = (point.x - TARGET_X) / 1.05;
const dy = (point.y - TARGET_Y) / 0.78;
if (dx * dx + dy * dy <= 1) {
return "TARGET HIT";
}
if (Math.abs(point.x) > GOAL_HALF_WIDTH) {
return "WIDE";
}
if (point.y > GOAL_HEIGHT) {
return "OVER";
}
return "OFF TARGET";
}
function simulateTrajectory(
spinValue,
speedScale = 1,
startX = 0,
snapshot = markerSnapshot()
) {
const params = {
startX,
startY: BALL_START_Y,
startZ: BALL_START_Z,
ax: spinValue * 3.6,
vz: -20.5 * speedScale,
vy: 10.2,
gravity: 10
};
const nominalDuration =
(params.startZ - GOAL_Z) / Math.abs(params.vz);
const points = [positionAt(0, params)];
let previous = points[0].clone();
let duration = nominalDuration;
let result = "OFF TARGET";
let endPoint = previous.clone();
for (let t = 1 / 120; t <= nominalDuration + 1 / 120; t += 1 / 120) {
let current = positionAt(t, params);
let blocked = false;
for (const marker of snapshot) {
if (previous.z > marker.z && current.z <= marker.z) {
const fraction =
(previous.z - marker.z) /
Math.max(0.00001, previous.z - current.z);
const crossingX = THREE.MathUtils.lerp(
previous.x,
current.x,
fraction
);
const crossingY = THREE.MathUtils.lerp(
previous.y,
current.y,
fraction
);
if (Math.abs(crossingX - marker.x) < 0.66 && crossingY < 2.88) {
current = new THREE.Vector3(crossingX, crossingY, marker.z);
duration = t;
result = "BLOCKED";
blocked = true;
break;
}
}
}
if (!blocked && current.z <= GOAL_Z) {
const fraction =
(previous.z - GOAL_Z) /
Math.max(0.00001, previous.z - current.z);
current = new THREE.Vector3(
THREE.MathUtils.lerp(previous.x, current.x, fraction),
THREE.MathUtils.lerp(previous.y, current.y, fraction),
GOAL_Z
);
duration = t;
result = classifyGoalPoint(current);
}
if (!blocked && current.y <= 0.27 && current.z > GOAL_Z) {
duration = t;
result = "GROUND";
}
points.push(current.clone());
endPoint = current.clone();
previous = current.clone();
if (
result === "BLOCKED" ||
result === "GROUND" ||
current.z <= GOAL_Z
) {
break;
}
}
return {
params,
points,
duration,
result,
endPoint
};
}
function samplePath(points, fraction) {
const clamped = THREE.MathUtils.clamp(fraction, 0, 1);
const scaled = clamped * (points.length - 1);
const index = Math.min(points.length - 2, Math.floor(scaled));
return new THREE.Vector3().lerpVectors(
points[index],
points[index + 1],
scaled - index
);
}
// ---------------------------------------------------------------------
// UI and archive
// ---------------------------------------------------------------------
const spinInput = document.querySelector("#spin");
const spinOutput = document.querySelector("#spinOut");
const repeatCount = document.querySelector("#repeatCount");
const launchButton = document.querySelector("#launch");
const replayButton = document.querySelector("#replay");
const clearButton = document.querySelector("#clear");
const resetWallButton = document.querySelector("#resetWall");
const resetCameraButton = document.querySelector("#resetCamera");
const legend = document.querySelector("#legend");
const passCount = document.querySelector("#passCount");
const lastResult = document.querySelector("#lastResult");
const aimResult = document.querySelector("#aimResult");
const bendValue = document.querySelector("#bendValue");
const toastElement = document.querySelector("#toast");
const archives = [];
const launchQueue = [];
let activeShot = null;
let replayState = null;
let nextLaunchTime = 0;
let launchTotal = 0;
let passNumber = 0;
function showToast(message) {
toastElement.textContent = message;
toastElement.classList.add("show");
clearTimeout(showToast.timeout);
showToast.timeout = setTimeout(() => {
toastElement.classList.remove("show");
}, 1800);
}
function updateSpinLabel() {
const value = Number(spinInput.value);
spinOutput.textContent = `${value >= 0 ? "+" : ""}${value.toFixed(2)}`;
bendValue.textContent =
Math.abs(value) < 0.04
? "STRAIGHT"
: value > 0
? "RIGHT"
: "LEFT";
updateAimGuide();
}
function updateAimGuide() {
const spin = Number(spinInput.value);
const simulation = simulateTrajectory(spin);
const step = Math.max(1, Math.floor(simulation.points.length / 55));
let count = 0;
for (let i = 0; i < simulation.points.length; i += step) {
if (count >= 110) break;
dummy.position.copy(simulation.points[i]);
dummy.scale.setScalar(i % (step * 4) === 0 ? 1.35 : 1);
dummy.updateMatrix();
guide.setMatrixAt(count++, dummy.matrix);
}
guide.count = count;
guide.instanceMatrix.needsUpdate = true;
const blocked = simulation.result === "BLOCKED";
guideMaterial.color.setHex(blocked ? 0xff786f : 0x74dfff);
guideMaterial.emissive.setHex(blocked ? 0xc62e2e : 0x39bde8);
aimResult.textContent = simulation.result;
lastResult.textContent = simulation.result;
}
const endpointGeometry = new THREE.BoxGeometry(0.19, 0.19, 0.19);
function addArchive(sim) {
const color = palette[archives.length % palette.length];
const geometry = new THREE.BufferGeometry().setFromPoints(sim.points);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.78,
depthWrite: false
});
const line = new THREE.Line(geometry, material);
line.renderOrder = 2;
scene.add(line);
const endpointMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.8,
roughness: 0.4
});
const endpoint = new THREE.Mesh(endpointGeometry, endpointMaterial);
endpoint.position.copy(sim.endPoint);
endpoint.castShadow = true;
scene.add(endpoint);
const archive = {
sim,
line,
endpoint,
material,
number: passNumber
};
archives.push(archive);
passNumber++;
const row = document.createElement("div");
row.className = "pass-row";
const dot = document.createElement("span");
dot.className = "pass-dot";
dot.style.background = `#${color.toString(16).padStart(6, "0")}`;
const name = document.createElement("span");
name.className = "pass-name";
name.textContent = `P-${String(archive.number).padStart(2, "0")} · spin ${formatSigned(spinInput.value)}`;
const result = document.createElement("span");
result.className = "pass-result";
result.textContent = sim.result;
row.append(dot, name, result);
legend.append(row);
const empty = legend.querySelector(".empty");
if (empty) empty.remove();
passCount.textContent = `${archives.length} trace${archives.length === 1 ? "" : "s"}`;
}
function formatSigned(value) {
const number = Number(value);
return `${number >= 0 ? "+" : ""}${number.toFixed(2)}`;
}
function startNextShot() {
if (!launchQueue.length) return;
const item = launchQueue.shift();
const simulation = simulateTrajectory(
Number(spinInput.value),
1,
item.offset,
markerSnapshot()
);
addArchive(simulation);
activeShot = {
simulation,
age: 0,
number: archives.length,
total: launchTotal
};
liveBall.visible = true;
replayBall.visible = false;
liveBall.position.copy(simulation.points[0]);
ballShadow.position.z = liveBall.position.z;
lastResult.textContent = `PASS ${activeShot.number} / ${launchTotal}`;
}
function launchRepeats() {
if (activeShot || launchQueue.length || replayState) {
showToast("Finish the current shot or replay first.");
return;
}
const count = Number(repeatCount.value);
const offsets = [-0.12, 0.1, -0.07, 0.14, -0.09];
launchQueue.length = 0;
launchTotal = count;
for (let i = 0; i < count; i++) {
launchQueue.push({ offset: offsets[i] });
}
nextLaunchTime = performance.now() / 1000;
showToast(`${count} pass${count === 1 ? "" : "es"} queued at spin ${formatSigned(spinInput.value)}.`);
}
function replayTraces() {
if (!archives.length) {
showToast("Record a pass before replaying.");
return;
}
if (activeShot || launchQueue.length) {
showToast("Wait for the shot set to finish.");
return;
}
replayState = {
index: 0,
age: 0
};
replayBall.visible = true;
liveBall.visible = false;
for (const archive of archives) {
archive.material.opacity = 0.24;
}
showToast(`Replaying ${archives.length} archived trajectory${archives.length === 1 ? "" : "s"}.`);
}
function clearTraces() {
for (const archive of archives) {
scene.remove(archive.line, archive.endpoint);
archive.line.geometry.dispose();
archive.material.dispose();
archive.endpoint.material.dispose();
}
archives.length = 0;
launchQueue.length = 0;
activeShot = null;
replayState = null;
passNumber = 0;
launchTotal = 0;
legend.innerHTML = '<div class="empty">No trajectories recorded yet.</div>';
passCount.textContent = "0 traces";
liveBall.visible = true;
replayBall.visible = false;
liveBall.position.set(0, BALL_START_Y, BALL_START_Z);
ballShadow.position.set(0, 0.205, BALL_START_Z);
updateAimGuide();
showToast("Trajectory archive cleared.");
}
function resetWall() {
for (const marker of markers) {
marker.group.position.set(marker.originalX, 0, marker.originalZ);
}
updateAimGuide();
showToast("Wall returned to its starting block.");
}
spinInput.addEventListener("input", updateSpinLabel);
launchButton.addEventListener("click", launchRepeats);
replayButton.addEventListener("click", replayTraces);
clearButton.addEventListener("click", clearTraces);
resetWallButton.addEventListener("click", resetWall);
// ---------------------------------------------------------------------
// Custom camera controls: orbit, zoom, and marker dragging
// ---------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, 0, -1),
radius: 40,
theta: 0.31,
phi: 0.95
};
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
const horizontal = Math.sin(phi) * radius;
camera.position.set(
target.x + horizontal * Math.sin(theta),
target.y + Math.cos(phi) * radius,
target.z + horizontal * Math.cos(theta)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.target.set(0, 0, -1);
cameraState.radius = 40;
cameraState.theta = 0.31;
cameraState.phi = 0.95;
updateCamera();
}
resetCameraButton.addEventListener("click", resetCamera);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const dragPoint = new THREE.Vector3();
let pointerDown = false;
let orbiting = false;
let draggingMarker = null;
let dragOffset = new THREE.Vector3();
let lastPointerX = 0;
let lastPointerY = 0;
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function selectMarker(index) {
for (const marker of markers) {
marker.base.material.color.setHex(0xe6534d);
}
if (index !== null && markers[index]) {
markers[index].base.material.color.setHex(0xffd166);
}
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
updatePointer(event);
const hit = raycaster.intersectObjects(hitboxes, false)[0];
pointerDown = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (hit) {
draggingMarker = markers[hit.object.userData.markerIndex];
selectMarker(draggingMarker.group.userData.index);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
dragOffset.set(
draggingMarker.group.position.x - dragPoint.x,
0,
draggingMarker.group.position.z - dragPoint.z
);
}
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
} else {
orbiting = true;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
updatePointer(event);
if (draggingMarker) {
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
draggingMarker.group.position.x = THREE.MathUtils.clamp(
dragPoint.x + dragOffset.x,
-10.5,
10.5
);
draggingMarker.group.position.z = THREE.MathUtils.clamp(
dragPoint.z + dragOffset.z,
2,
14
);
updateAimGuide();
}
return;
}
if (orbiting && pointerDown) {
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.005,
0.38,
1.36
);
updateCamera();
}
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (!pointerDown) {
const hit = raycaster.intersectObjects(hitboxes, false)[0];
renderer.domElement.style.cursor = hit ? "grab" : "grab";
}
});
function endPointer(event) {
pointerDown = false;
orbiting = false;
if (draggingMarker) {
selectMarker(null);
draggingMarker = null;
}
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
21,
68
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const now = performance.now() / 1000;
// Queue and execute shot passes.
if (!activeShot && !replayState && launchQueue.length && now >= nextLaunchTime) {
startNextShot();
}
if (activeShot) {
activeShot.age += delta;
const fraction = THREE.MathUtils.clamp(
activeShot.age / activeShot.simulation.duration,
0,
1
);
const position = samplePath(
activeShot.simulation.points,
fraction
);
liveBall.position.copy(position);
liveBall.rotation.x += delta * 6.5;
liveBall.rotation.z += delta * 4.2;
ballShadow.position.x = position.x;
ballShadow.position.z = position.z;
ballShadow.material.opacity = THREE.MathUtils.clamp(
0.26 - position.y * 0.025,
0.08,
0.24
);
if (activeShot.age >= activeShot.simulation.duration) {
const result = activeShot.simulation.result;
lastResult.textContent = result;
aimResult.textContent = result;
activeShot = null;
if (launchQueue.length) {
nextLaunchTime = now + 0.55;
lastResult.textContent = "NEXT PASS";
} else {
lastResult.textContent = `${launchTotal} PASSES COMPLETE`;
}
}
}
// Replay archived paths with a ghost ball.
if (replayState) {
const archive = archives[replayState.index];
replayState.age += delta;
const fraction = THREE.MathUtils.clamp(
replayState.age / archive.sim.duration,
0,
1
);
const position = samplePath(archive.sim.points, fraction);
replayBall.position.copy(position);
replayBall.rotation.x += delta * 6.5;
replayBall.rotation.z += delta * 4.2;
lastResult.textContent = `REPLAY ${replayState.index + 1} / ${archives.length}`;
if (fraction >= 1) {
replayState.index++;
replayState.age = 0;
if (replayState.index >= archives.length) {
replayState = null;
replayBall.visible = false;
liveBall.visible = true;
for (const saved of archives) {
saved.material.opacity = 0.78;
}
lastResult.textContent = "REPLAY COMPLETE";
}
}
}
// Gentle target pulse.
const pulse = 1 + Math.sin(clock.elapsedTime * 3.2) * 0.035;
targetGroup.scale.setScalar(pulse);
goalLight.intensity = 8.5 + Math.sin(clock.elapsedTime * 3.2) * 1.4;
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateSpinLabel();
updateCamera();
liveBall.position.set(0, BALL_START_Y, BALL_START_Z);
animate();
</script>
</body>
</html>
```