Prompt specification
Build or customize the Three.js 3D scene "Courtyard ball-throw teaching scene" 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: Courtyard ball-throw teaching scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/courtyard-ball-throw-teaching-scene-x1419
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows projectile trajectories under gravity. Compose a small courtyard with a basket target and visible trajectory ghost markers; use analytic flight and stop the ball at the first ground collision. Voxel style.
Working Three.js Starter (courtyard-ball-throw-teaching-scene-x1419):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#102c3b">
<title>Arc Courtyard — Voxel Projectile Lab</title>
<style>
:root {
--ink: #102c3b;
--paper: #f4ecd8;
--cyan: #65e0df;
--orange: #ff7c3d;
--yellow: #ffc861;
--glass: rgba(11, 34, 47, 0.9);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #91c8d8;
color: var(--paper);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
body {
height: 100dvh;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
canvas:active {
cursor: grabbing;
}
#hud {
position: fixed;
top: 24px;
left: 24px;
z-index: 5;
width: min(360px, calc(100vw - 48px));
pointer-events: none;
text-shadow: 0 2px 0 rgba(10, 29, 39, 0.22);
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
padding: 6px 9px;
border: 2px solid rgba(244, 236, 216, 0.75);
background: var(--glass);
color: var(--paper);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
box-shadow: 4px 4px 0 rgba(16, 44, 59, 0.2);
}
.eyebrow::before {
content: "";
width: 7px;
height: 7px;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
h1 {
margin: 0;
max-width: 340px;
color: var(--paper);
font-size: clamp(32px, 5vw, 58px);
line-height: 0.88;
letter-spacing: -0.07em;
text-transform: uppercase;
}
.subtitle {
max-width: 325px;
margin: 13px 0 17px;
color: rgba(244, 236, 216, 0.92);
font-size: 11px;
line-height: 1.55;
}
.equation {
display: inline-block;
padding: 7px 9px;
border-left: 4px solid var(--orange);
background: rgba(16, 44, 59, 0.82);
color: #fff4d8;
font-size: 11px;
letter-spacing: 0.03em;
}
.metrics {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
margin-top: 12px;
}
.metric {
min-width: 0;
padding: 8px 7px;
border-top: 2px solid rgba(244, 236, 216, 0.72);
background: rgba(16, 44, 59, 0.76);
box-shadow: 3px 3px 0 rgba(16, 44, 59, 0.16);
}
.metric span {
display: block;
overflow: hidden;
color: rgba(244, 236, 216, 0.65);
font-size: 8px;
letter-spacing: 0.12em;
text-overflow: ellipsis;
white-space: nowrap;
}
.metric strong {
display: block;
margin-top: 4px;
color: var(--paper);
font-size: 14px;
white-space: nowrap;
}
#status {
color: var(--cyan);
}
#controls {
position: fixed;
z-index: 8;
bottom: 24px;
left: 50%;
display: flex;
align-items: center;
gap: 9px;
width: max-content;
max-width: calc(100vw - 32px);
padding: 10px;
border: 2px solid rgba(244, 236, 216, 0.42);
background: var(--glass);
box-shadow: 7px 7px 0 rgba(16, 44, 59, 0.25);
transform: translateX(-50%);
}
button {
min-height: 40px;
padding: 0 15px;
border: 0;
border-radius: 0;
background: var(--paper);
color: var(--ink);
font: 800 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
cursor: pointer;
transition: transform 100ms ease, background 100ms ease;
}
button:hover {
background: #fff9e9;
transform: translateY(-2px);
}
button:active {
transform: translateY(1px);
}
button.primary {
background: var(--orange);
color: #28170e;
box-shadow: inset 0 -4px 0 rgba(111, 42, 14, 0.22);
}
button.primary:hover {
background: #ff9360;
}
button.secondary {
background: #244555;
color: var(--paper);
}
button.secondary:hover {
background: #315a6d;
}
.gravity-control {
display: grid;
grid-template-columns: auto 92px auto;
align-items: center;
gap: 9px;
min-height: 40px;
padding: 0 10px;
border-left: 2px solid rgba(244, 236, 216, 0.18);
color: rgba(244, 236, 216, 0.75);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
}
input[type="range"] {
width: 92px;
accent-color: var(--cyan);
cursor: ew-resize;
}
#gravity-value {
min-width: 62px;
color: var(--paper);
font-size: 10px;
white-space: nowrap;
}
#help {
position: fixed;
right: 24px;
bottom: 24px;
z-index: 5;
max-width: 210px;
padding: 9px 11px;
border-right: 3px solid var(--cyan);
background: rgba(16, 44, 59, 0.76);
color: rgba(244, 236, 216, 0.78);
font-size: 9px;
line-height: 1.65;
text-align: right;
pointer-events: none;
}
kbd {
padding: 1px 4px;
border: 1px solid rgba(244, 236, 216, 0.4);
background: rgba(244, 236, 216, 0.1);
color: var(--paper);
font: inherit;
}
#toast {
position: fixed;
top: 28px;
right: 28px;
z-index: 12;
padding: 14px 18px;
border: 2px solid var(--yellow);
background: var(--ink);
color: var(--yellow);
font-size: 16px;
font-weight: 900;
letter-spacing: 0.12em;
box-shadow: 7px 7px 0 rgba(16, 44, 59, 0.25);
opacity: 0;
pointer-events: none;
transform: translateY(-18px) rotate(1deg);
transition: opacity 180ms ease, transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translateY(0) rotate(-1deg);
}
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #102c3b;
color: var(--paper);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.2em;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
#hud {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
}
h1 {
font-size: 34px;
}
.subtitle {
display: none;
}
.equation {
margin-top: 10px;
}
.metrics {
max-width: 350px;
}
#controls {
bottom: 12px;
flex-wrap: wrap;
justify-content: center;
width: calc(100vw - 24px);
}
.gravity-control {
border-left: 0;
border-top: 1px solid rgba(244, 236, 216, 0.18);
width: 100%;
justify-content: center;
}
#help {
display: none;
}
#toast {
top: auto;
right: 14px;
bottom: 125px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel projectile courtyard"></canvas>
<div id="hud">
<div class="eyebrow">ANALYTIC FLIGHT LAB</div>
<h1>Arc<br>Courtyard</h1>
<p class="subtitle">
A voxel projectile study. Each shot is sampled directly from its
analytic parabola, then frozen at the first positive ground root.
</p>
<div class="equation">p(t) = p₀ + v₀t − ½gt²</div>
<div class="metrics">
<div class="metric">
<span>VELOCITY</span>
<strong id="speed">—</strong>
</div>
<div class="metric">
<span>APEX</span>
<strong id="apex">—</strong>
</div>
<div class="metric">
<span>FLIGHT</span>
<strong id="flight-time">—</strong>
</div>
<div class="metric">
<span>STATUS</span>
<strong id="status">READY</strong>
</div>
</div>
</div>
<div id="controls">
<button id="launch" class="primary">▶ LAUNCH SHOT</button>
<button id="clear" class="secondary">CLEAR TRAILS</button>
<button id="reset-camera" class="secondary">RESET VIEW</button>
<label class="gravity-control" for="gravity">
GRAVITY
<input id="gravity" type="range" min="3" max="16" step="0.1" value="9.8">
<span id="gravity-value">9.8 m/s²</span>
</label>
</div>
<div id="help">
<kbd>DRAG</kbd> orbit <kbd>SHIFT + DRAG</kbd> pan<br>
<kbd>WHEEL</kbd> zoom <kbd>CLICK</kbd> launch<br>
<kbd>ARROWS</kbd> orbit <kbd>R</kbd> reset
</div>
<div id="toast">BUCKET +100</div>
<div id="loading">BUILDING COURTYARD…</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 canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.6));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x91c8d8);
scene.fog = new THREE.Fog(0x91c8d8, 19, 39);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
80
);
const hemisphere = new THREE.HemisphereLight(0xccefff, 0x59675e, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2ce, 3.25);
sun.position.set(-8, 15, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 11;
sun.shadow.camera.bottom = -11;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.0005;
scene.add(sun);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function getMaterial(color) {
const key = String(color);
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness: 0.92,
metalness: 0
})
);
}
return materialCache.get(key);
}
function box(parent, size, position, color, options = {}) {
const mesh = new THREE.Mesh(
unitBox,
options.material || getMaterial(color)
);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = options.cast !== false;
mesh.receiveShadow = options.receive !== false;
parent.add(mesh);
return mesh;
}
const dummy = new THREE.Object3D();
const upAxis = new THREE.Vector3(0, 1, 0);
function beam(parent, a, b, width, color, material) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
direction.normalize();
const midpoint = start.clone().add(end).multiplyScalar(0.5);
const mesh = box(
parent,
[width, length, width],
midpoint.toArray(),
color,
{ material }
);
mesh.quaternion.setFromUnitVectors(upAxis, direction);
return mesh;
}
// -------------------------------------------------------------------------
// Voxel courtyard
// -------------------------------------------------------------------------
const courtyard = new THREE.Group();
scene.add(courtyard);
box(
courtyard,
[12.4, 0.36, 12.4],
[0, -0.26, 0],
0x536a64
);
const tileCount = 144;
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
});
const tiles = new THREE.InstancedMesh(
unitBox,
floorMaterial,
tileCount
);
const tileColors = [
0x71847d,
0x778a82,
0x687c76,
0x7d8e84,
0x6f817b
];
let tileIndex = 0;
let seed = 84127;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
for (let x = 0; x < 12; x++) {
for (let z = 0; z < 12; z++) {
dummy.position.set(-5.5 + x, -0.055, -5.5 + z);
dummy.scale.set(0.96, 0.11, 0.96);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
tiles.setColorAt(
tileIndex,
new THREE.Color(tileColors[Math.floor(random() * tileColors.length)])
);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
courtyard.add(tiles);
// Painted court markings.
box(
courtyard,
[0.075, 0.018, 8.5],
[-0.7, 0.012, 0.2],
0xe8dfc4
);
box(
courtyard,
[2.6, 0.018, 0.075],
[2.4, 0.013, -4.35],
0xe8dfc4
);
box(
courtyard,
[0.075, 0.018, 2.5],
[1.1, 0.013, -3.1],
0xe8dfc4
);
box(
courtyard,
[0.075, 0.018, 2.5],
[3.7, 0.013, -3.1],
0xe8dfc4
);
// Back wall.
for (let i = 0; i < 12; i++) {
const height = 1.75 + (i % 3 === 0 ? 0.28 : 0);
const color = i % 4 === 0 ? 0xd9c5a5 : 0xe5d4b7;
box(
courtyard,
[0.97, height, 0.34],
[-5.5 + i, height / 2, -5.97],
color
);
box(
courtyard,
[1.01, 0.16, 0.43],
[-5.5 + i, height + 0.08, -5.97],
0xbfa98d
);
}
// Left wall.
for (let i = 0; i < 10; i++) {
const height = 1.25 + (i % 3 === 0 ? 0.22 : 0);
const color = i % 4 === 0 ? 0xcdb897 : 0xdfcba9;
box(
courtyard,
[0.34, height, 0.97],
[-5.97, height / 2, -5.5 + i],
color
);
box(
courtyard,
[0.43, 0.15, 1.01],
[-5.97, height + 0.075, -5.5 + i],
0xb79f82
);
}
// Small wall accents.
for (let i = 0; i < 4; i++) {
box(
courtyard,
[0.54, 0.54, 0.08],
[-4.6 + i * 1.75, 1.05, -5.77],
i % 2 === 0 ? 0x5ba7aa : 0xe19362
);
}
// Distant block skyline.
const skylineColors = [
0x789ba5,
0x87a9ad,
0x6d909c,
0x9bb8b8
];
for (let i = 0; i < 12; i++) {
const height = 1.8 + random() * 4.2;
const x = -10 + i * 1.75;
const z = -9.2 - random() * 2.2;
box(
scene,
[1.25 + random() * 0.5, height, 1.5],
[x, height / 2, z],
skylineColors[i % skylineColors.length],
{ cast: false, receive: false }
);
}
function makeCloud(x, y, z, scale) {
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0xf5f1df,
roughness: 1,
transparent: true,
opacity: 0.82,
depthWrite: false
});
box(
scene,
[1.8 * scale, 0.42 * scale, 0.48 * scale],
[x, y, z],
0xffffff,
{ material: cloudMaterial, cast: false, receive: false }
);
box(
scene,
[0.8 * scale, 0.72 * scale, 0.48 * scale],
[x - 0.48 * scale, y + 0.12 * scale, z],
0xffffff,
{ material: cloudMaterial, cast: false, receive: false }
);
box(
scene,
[0.9 * scale, 0.58 * scale, 0.48 * scale],
[x + 0.62 * scale, y + 0.06 * scale, z],
0xffffff,
{ material: cloudMaterial, cast: false, receive: false }
);
}
makeCloud(-5, 8.2, -7, 1.1);
makeCloud(5.8, 9.1, -10, 1.35);
makeCloud(9.5, 7.4, -3, 0.8);
const sunMaterial = new THREE.MeshStandardMaterial({
color: 0xffd56b,
emissive: 0xffa62f,
emissiveIntensity: 1.2,
roughness: 1
});
box(
scene,
[0.82, 0.82, 0.18],
[-8.3, 8.6, -13],
0xffd56b,
{ material: sunMaterial, cast: false, receive: false }
);
function makeTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
courtyard.add(tree);
box(tree, [0.9, 0.48, 0.9], [0, 0.24, 0], 0xb76f4c);
box(tree, [0.78, 0.08, 0.78], [0, 0.51, 0], 0x4a4034);
box(tree, [0.24, 1.25, 0.24], [0, 1.08, 0], 0x79563c);
box(tree, [0.95, 0.8, 0.95], [0, 1.85, 0], 0x4e8a64);
box(tree, [0.75, 0.75, 0.75], [-0.5, 1.65, 0.18], 0x65a16e);
box(tree, [0.72, 0.72, 0.72], [0.48, 1.72, -0.12], 0x78ad70);
box(tree, [0.62, 0.62, 0.62], [0.1, 2.35, 0.08], 0x8fbd78);
}
makeTree(-4.65, -4.35, 0.95);
makeTree(4.75, 4.25, 0.85);
// Bench.
const bench = new THREE.Group();
bench.position.set(5.02, 0, 1.05);
courtyard.add(bench);
box(bench, [0.65, 0.18, 2.35], [0, 0.68, 0], 0x9c6948);
box(bench, [0.16, 0.8, 0.16], [0, 0.32, -0.75], 0x35505a);
box(bench, [0.16, 0.8, 0.16], [0, 0.32, 0.75], 0x35505a);
box(bench, [0.15, 0.8, 2.1], [-0.28, 1.1, 0], 0x9c6948);
// -------------------------------------------------------------------------
// Voxel basket target
// -------------------------------------------------------------------------
const target = new THREE.Vector3(2.4, 3.15, -2.65);
const basket = new THREE.Group();
courtyard.add(basket);
const boardMaterial = new THREE.MeshStandardMaterial({
color: 0xf5edda,
roughness: 0.8
});
const outlineMaterial = new THREE.MeshStandardMaterial({
color: 0x2ca4ad,
emissive: 0x126b74,
emissiveIntensity: 0.25,
roughness: 0.7
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0xff7433,
emissive: 0x6d1f0c,
emissiveIntensity: 0.12,
roughness: 0.62
});
const netMaterial = new THREE.MeshStandardMaterial({
color: 0xf7efd9,
roughness: 1
});
// Support pole and arm.
box(basket, [0.28, 4.25, 0.28], [4.05, 2.125, -3.5], 0x315562);
box(basket, [1.25, 0.22, 0.26], [3.48, 3.64, -3.5], 0x315562);
box(basket, [0.48, 0.12, 0.48], [4.05, 0.12, -3.5], 0x263f49);
// Backboard.
box(
basket,
[2.2, 1.45, 0.18],
[2.4, 3.63, -3.35],
0xf5edda,
{ material: boardMaterial }
);
// Target square on the backboard.
box(basket, [0.075, 0.78, 0.045], [1.93, 3.15, -3.245], 0x2ca4ad, {
material: outlineMaterial
});
box(basket, [0.075, 0.78, 0.045], [2.87, 3.15, -3.245], 0x2ca4ad, {
material: outlineMaterial
});
box(basket, [1.02, 0.075, 0.045], [2.4, 2.76, -3.245], 0x2ca4ad, {
material: outlineMaterial
});
box(basket, [1.02, 0.075, 0.045], [2.4, 3.54, -3.245], 0x2ca4ad, {
material: outlineMaterial
});
// Rim connector.
box(basket, [0.18, 0.14, 0.68], [2.4, 3.15, -2.98], 0xff7433, {
material: rimMaterial
});
// Square voxel rim.
const halfRim = 0.61;
box(
basket,
[0.13, 0.13, 1.35],
[target.x - halfRim, target.y, target.z],
0xff7433,
{ material: rimMaterial }
);
box(
basket,
[0.13, 0.13, 1.35],
[target.x + halfRim, target.y, target.z],
0xff7433,
{ material: rimMaterial }
);
box(
basket,
[1.35, 0.13, 0.13],
[target.x, target.y, target.z - halfRim],
0xff7433,
{ material: rimMaterial }
);
box(
basket,
[1.35, 0.13, 0.13],
[target.x, target.y, target.z + halfRim],
0xff7433,
{ material: rimMaterial }
);
// Net corners.
const netTop = [
[target.x - 0.54, 3.1, target.z - 0.54],
[target.x + 0.54, 3.1, target.z - 0.54],
[target.x + 0.54, 3.1, target.z + 0.54],
[target.x - 0.54, 3.1, target.z + 0.54]
];
const netMiddle = [
[target.x - 0.46, 2.72, target.z - 0.46],
[target.x + 0.46, 2.72, target.z - 0.46],
[target.x + 0.46, 2.72, target.z + 0.46],
[target.x - 0.46, 2.72, target.z + 0.46]
];
const netBottom = [
[target.x - 0.35, 2.34, target.z - 0.35],
[target.x + 0.35, 2.34, target.z - 0.35],
[target.x + 0.35, 2.34, target.z + 0.35],
[target.x - 0.35, 2.34, target.z + 0.35]
];
for (let i = 0; i < 4; i++) {
const next = (i + 1) % 4;
beam(basket, netTop[i], netMiddle[i], 0.045, 0xf7efd9, netMaterial);
beam(basket, netMiddle[i], netBottom[i], 0.045, 0xf7efd9, netMaterial);
beam(basket, netMiddle[i], netMiddle[next], 0.04, 0xf7efd9, netMaterial);
beam(basket, netBottom[i], netBottom[next], 0.04, 0xf7efd9, netMaterial);
// Crossed net strands.
beam(basket, netTop[i], netBottom[next], 0.026, 0xe9dfc7, netMaterial);
}
// -------------------------------------------------------------------------
// Launch pad and voxel basketball
// -------------------------------------------------------------------------
const start = new THREE.Vector3(-3.4, 1.18, 4.15);
const BALL_RADIUS = 0.28;
const pad = new THREE.Group();
pad.position.set(start.x, 0, start.z);
courtyard.add(pad);
box(pad, [1.05, 0.32, 1.05], [0, 0.16, 0], 0x315562);
box(pad, [0.58, 0.5, 0.58], [0, 0.56, 0], 0x4b7479);
box(pad, [0.78, 0.16, 0.78], [0, 0.87, 0], 0xf0b55c);
for (const x of [-0.43, 0.43]) {
for (const z of [-0.43, 0.43]) {
box(pad, [0.13, 0.1, 0.13], [x, 0.36, z], 0xff7c3d);
}
}
const ball = new THREE.Group();
scene.add(ball);
const ballMaterial = new THREE.MeshStandardMaterial({
color: 0xf27732,
roughness: 0.78
});
const seamMaterial = new THREE.MeshStandardMaterial({
color: 0x3b2924,
roughness: 1
});
// A cubic approximation of a sphere.
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
for (let z = -1; z <= 1; z++) {
if (x * x + y * y + z * z <= 2) {
box(
ball,
[0.145, 0.145, 0.145],
[x * 0.145, y * 0.145, z * 0.145],
0xf27732,
{ material: ballMaterial }
);
}
}
}
}
// Dark voxel seams.
box(ball, [0.47, 0.025, 0.034], [0, 0, 0], 0x3b2924, {
material: seamMaterial
});
box(ball, [0.034, 0.47, 0.026], [0, 0, 0], 0x3b2924, {
material: seamMaterial
});
box(ball, [0.028, 0.028, 0.47], [0, 0, 0], 0x3b2924, {
material: seamMaterial
});
ball.position.copy(start);
// -------------------------------------------------------------------------
// Analytic projectile system
// -------------------------------------------------------------------------
const gravityInput = document.querySelector("#gravity");
const gravityValue = document.querySelector("#gravity-value");
const launchButton = document.querySelector("#launch");
const clearButton = document.querySelector("#clear");
const resetCameraButton = document.querySelector("#reset-camera");
const speedElement = document.querySelector("#speed");
const apexElement = document.querySelector("#apex");
const flightTimeElement = document.querySelector("#flight-time");
const statusElement = document.querySelector("#status");
const toast = document.querySelector("#toast");
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
transparent: true,
opacity: 0.42,
depthWrite: false
});
const MARKER_COUNT = 66;
const paths = [];
let latestPath = null;
let activeFlight = null;
let elapsed = 0;
let scoreCount = 0;
let hoopGlow = 0;
let toastTimer = 0;
const effects = [];
function positionAt(flight, time, output = new THREE.Vector3()) {
output.copy(flight.start);
output.addScaledVector(flight.velocity, time);
output.y -= 0.5 * flight.gravity * time * time;
return output;
}
function makeFlight() {
const gravity = Number(gravityInput.value);
// The aim time is chosen so that the analytic path passes through
// the center of the basket at exactly t = aimTime.
const aimTime = 1.22;
const velocity = new THREE.Vector3(
(target.x - start.x) / aimTime,
0,
(target.z - start.z) / aimTime
);
velocity.y =
(target.y - start.y + 0.5 * gravity * aimTime * aimTime) /
aimTime;
// Solve the positive ground-crossing root:
//
// radius = start.y + vy*t - 0.5*g*t^2
//
// Only this positive root is used, so the ball stops at the first
// ground collision and never continues to the later parabola branch.
const heightAboveGround = start.y - BALL_RADIUS;
const duration =
(velocity.y +
Math.sqrt(
velocity.y * velocity.y +
2 * gravity * heightAboveGround
)) /
gravity;
return {
start: start.clone(),
velocity,
gravity,
duration,
aimTime,
grounded: false,
scored: false
};
}
function addPath(flight) {
const mesh = new THREE.InstancedMesh(
unitBox,
markerMaterial,
MARKER_COUNT
);
mesh.frustumCulled = false;
mesh.renderOrder = 2;
const times = [];
const point = new THREE.Vector3();
for (let i = 0; i < MARKER_COUNT; i++) {
const time = (i / (MARKER_COUNT - 1)) * flight.duration;
times.push(time);
positionAt(flight, time, point);
dummy.position.copy(point);
dummy.rotation.set(0, i * 0.13, 0);
const isEndpoint = i === 0 || i === MARKER_COUNT - 1;
const scale = isEndpoint ? 0.1 : 0.067;
dummy.scale.set(scale, scale, scale);
dummy.updateMatrix();
mesh.setMatrixAt(i, dummy.matrix);
const color =
time <= flight.aimTime + 0.02
? new THREE.Color(0x62e3df)
: new THREE.Color(0xffc467);
mesh.setColorAt(i, color);
}
mesh.instanceMatrix.needsUpdate = true;
mesh.instanceColor.needsUpdate = true;
scene.add(mesh);
const path = { mesh, times, flight };
paths.push(path);
while (paths.length > 4) {
const oldPath = paths.shift();
scene.remove(oldPath.mesh);
oldPath.mesh.dispose?.();
}
return path;
}
function clearPaths() {
for (const path of paths) {
scene.remove(path.mesh);
path.mesh.dispose?.();
}
paths.length = 0;
latestPath = null;
}
function updateLatestPath() {
if (!latestPath || !activeFlight) return;
const point = new THREE.Vector3();
for (let i = 0; i < latestPath.times.length; i++) {
const time = latestPath.times[i];
const pulse = Math.abs(time - elapsed) < 0.1;
positionAt(latestPath.flight, time, point);
dummy.position.copy(point);
const scale = pulse ? 0.13 : 0.067;
dummy.scale.set(scale, scale, scale);
dummy.updateMatrix();
latestPath.mesh.setMatrixAt(i, dummy.matrix);
}
latestPath.mesh.instanceMatrix.needsUpdate = true;
}
function spawnImpact(position) {
for (let i = 0; i < 10; i++) {
const angle = (i / 10) * Math.PI * 2 + random() * 0.25;
const speed = 0.7 + random() * 1.1;
const particle = box(
scene,
[0.075, 0.075, 0.075],
[position.x, position.y + 0.03, position.z],
i % 2 === 0 ? 0xd9c6a0 : 0xbba985,
{ cast: false }
);
effects.push({
mesh: particle,
velocity: new THREE.Vector3(
Math.cos(angle) * speed,
1.1 + random() * 1.2,
Math.sin(angle) * speed
),
life: 0.58,
maxLife: 0.58
});
}
}
function showScore() {
scoreCount++;
hoopGlow = 1;
activeFlight.scored = true;
statusElement.textContent = "BUCKET +1";
toast.textContent = `BUCKET +${scoreCount * 100}`;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, 1200);
}
function launchShot() {
activeFlight = makeFlight();
elapsed = 0;
ball.visible = true;
ball.position.copy(start);
ball.rotation.set(0, 0, 0);
latestPath = addPath(activeFlight);
const speed = activeFlight.velocity.length();
const apex =
activeFlight.start.y +
(activeFlight.velocity.y * activeFlight.velocity.y) /
(2 * activeFlight.gravity);
speedElement.textContent = `${speed.toFixed(1)} m/s`;
apexElement.textContent = `${apex.toFixed(2)} m`;
flightTimeElement.textContent = `${activeFlight.duration.toFixed(2)} s`;
statusElement.textContent = "IN FLIGHT";
toast.classList.remove("show");
clearTimeout(toastTimer);
}
gravityInput.addEventListener("input", () => {
gravityValue.textContent = `${Number(gravityInput.value).toFixed(1)} m/s²`;
});
launchButton.addEventListener("click", launchShot);
clearButton.addEventListener("click", clearPaths);
resetCameraButton.addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, zoom, keyboard
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, 1.15, -0.35),
radius: 14.4,
theta: 0.69,
phi: 1.02
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.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)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.target.set(0, 1.15, -0.35);
cameraState.radius = 14.4;
cameraState.theta = 0.69;
cameraState.phi = 1.02;
updateCamera();
}
const pointer = {
active: false,
id: null,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: false,
mode: "orbit"
};
const cameraForward = new THREE.Vector3();
const cameraRight = new THREE.Vector3();
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
pointer.active = true;
pointer.id = event.pointerId;
pointer.startX = pointer.lastX = event.clientX;
pointer.startY = pointer.lastY = event.clientY;
pointer.moved = false;
pointer.mode =
event.button === 2 || event.shiftKey ? "pan" : "orbit";
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointer.active || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.lastX;
const dy = event.clientY - pointer.lastY;
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
if (
Math.hypot(
event.clientX - pointer.startX,
event.clientY - pointer.startY
) > 5
) {
pointer.moved = true;
}
if (pointer.mode === "orbit") {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.006,
0.34,
1.43
);
} else {
camera.getWorldDirection(cameraForward);
cameraRight
.crossVectors(cameraForward, camera.up)
.normalize();
cameraState.target.addScaledVector(
cameraRight,
-dx * cameraState.radius * 0.0013
);
cameraState.target.y += dy * cameraState.radius * 0.0011;
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-4.5,
4.5
);
cameraState.target.y = THREE.MathUtils.clamp(
cameraState.target.y,
0.2,
4
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-4.5,
4.5
);
}
});
canvas.addEventListener("pointerup", (event) => {
if (!pointer.active || event.pointerId !== pointer.id) return;
pointer.active = false;
canvas.releasePointerCapture(event.pointerId);
if (!pointer.moved && event.button === 0) {
launchShot();
}
});
canvas.addEventListener("pointercancel", () => {
pointer.active = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7.5,
25
);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.target.tagName === "INPUT") return;
const step = 0.1;
if (event.key === "ArrowLeft") cameraState.theta += step;
if (event.key === "ArrowRight") cameraState.theta -= step;
if (event.key === "ArrowUp") cameraState.phi -= step;
if (event.key === "ArrowDown") cameraState.phi += step;
if (event.key === "+" || event.key === "=") {
cameraState.radius = Math.max(7.5, cameraState.radius - 0.8);
}
if (event.key === "-" || event.key === "_") {
cameraState.radius = Math.min(25, cameraState.radius + 0.8);
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const previousPosition = new THREE.Vector3();
const currentPosition = new THREE.Vector3();
const crossingPosition = new THREE.Vector3();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (activeFlight && !activeFlight.grounded) {
const previousTime = elapsed;
positionAt(activeFlight, previousTime, previousPosition);
elapsed = Math.min(elapsed + dt, activeFlight.duration);
positionAt(activeFlight, elapsed, currentPosition);
// No numerical integration: the ball is placed directly from the
// analytic equation at the current time.
ball.position.copy(currentPosition);
ball.rotation.x += dt * 3.8;
ball.rotation.z += dt * 2.25;
// Basket-plane crossing test.
if (
!activeFlight.scored &&
previousPosition.z > target.z &&
currentPosition.z <= target.z
) {
const denominator = previousPosition.z - currentPosition.z;
const fraction =
denominator > 0
? (previousPosition.z - target.z) / denominator
: 0;
const crossingTime =
previousTime + (elapsed - previousTime) * fraction;
positionAt(activeFlight, crossingTime, crossingPosition);
if (
Math.abs(crossingPosition.x - target.x) < 0.52 &&
Math.abs(crossingPosition.y - target.y) < 0.34
) {
showScore();
}
}
// Freeze at the first positive ground collision.
if (elapsed >= activeFlight.duration) {
elapsed = activeFlight.duration;
positionAt(activeFlight, elapsed, currentPosition);
ball.position.copy(currentPosition);
activeFlight.grounded = true;
spawnImpact(currentPosition);
statusElement.textContent = activeFlight.scored
? "BUCKET · STOPPED"
: "GROUND STOP";
}
}
updateLatestPath();
// Impact dust.
for (let i = effects.length - 1; i >= 0; i--) {
const effect = effects[i];
effect.life -= dt;
effect.velocity.y -= 6 * dt;
effect.mesh.position.addScaledVector(effect.velocity, dt);
const scale = Math.max(0.05, effect.life / effect.maxLife);
effect.mesh.scale.setScalar(scale);
if (effect.life <= 0) {
scene.remove(effect.mesh);
effects.splice(i, 1);
}
}
// Basket flash after a successful shot.
hoopGlow = Math.max(0, hoopGlow - dt * 1.2);
rimMaterial.emissiveIntensity = 0.12 + hoopGlow * 1.4;
outlineMaterial.emissiveIntensity = 0.25 + hoopGlow * 1.1;
updateCamera();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.6));
renderer.setSize(window.innerWidth, window.innerHeight);
});
resetCamera();
animate();
requestAnimationFrame(() => {
const loading = document.querySelector("#loading");
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
});
// Begin with a visible shot so the courtyard immediately demonstrates
// the analytic arc and ghost markers.
setTimeout(() => {
if (!activeFlight) launchShot();
}, 450);
</script>
</body>
</html>
```