Prompt specification
Build or customize the Three.js 3D scene "Playground swing energy-timing 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: Playground swing energy-timing scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/playground-swing-energy-timing-scene-x1579
Scene Description Prompt:
A voxel playground swing receiving timed pushes, with a phase marker and bounded motion revealing resonance. Build one HTML file using Three.js from cdnjs.
Working Three.js Starter (playground-swing-energy-timing-scene-x1579):
```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="#91cde8">
<title>Voxel Swing Lab</title>
<style>
:root {
--ink: #153846;
--panel: rgba(255, 255, 255, 0.88);
--green: #28c982;
--green-dark: #15875a;
--orange: #ff7657;
--yellow: #ffd34f;
--muted: #69818b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #91cde8;
color: var(--ink);
font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
user-select: none;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
max-width: 360px;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.3);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 7px;
color: #245769;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 10px;
height: 10px;
content: "";
border-radius: 3px;
background: var(--orange);
box-shadow: 4px 4px 0 rgba(21, 56, 70, 0.13);
}
h1 {
margin: 0;
font-size: clamp(29px, 4vw, 48px);
font-weight: 950;
line-height: 0.95;
letter-spacing: -0.055em;
}
.brand p {
max-width: 310px;
margin: 10px 0 0;
color: #315f70;
font-size: 13px;
font-weight: 650;
line-height: 1.45;
}
.stats {
position: absolute;
top: 22px;
right: 22px;
width: 242px;
padding: 16px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 20px;
background: var(--panel);
box-shadow: 0 14px 36px rgba(35, 76, 91, 0.16);
backdrop-filter: blur(14px);
}
.stats-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
color: var(--muted);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.live-dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 5px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(40, 201, 130, 0.13);
animation: live 1.8s infinite;
}
@keyframes live {
50% {
box-shadow: 0 0 0 7px rgba(40, 201, 130, 0);
}
}
.stat-row {
display: flex;
align-items: baseline;
justify-content: space-between;
margin: 9px 0 6px;
}
.stat-row span {
color: var(--muted);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.stat-row strong {
font-size: 16px;
font-variant-numeric: tabular-nums;
}
.meter {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: #dce8ea;
}
.meter i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--green);
transition: width 0.08s linear;
}
#energyFill {
background: var(--yellow);
}
#resonanceFill {
background: linear-gradient(90deg, #47d6a1, #20b778);
}
.timing-label {
display: flex;
justify-content: space-between;
margin: 15px 0 7px;
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.11em;
text-transform: uppercase;
}
#phaseState {
color: var(--green-dark);
}
.timing-track {
position: relative;
height: 14px;
border-radius: 999px;
background:
linear-gradient(90deg, #f1d0cc 0%, #e7eceb 38%, #e7eceb 62%, #f1d0cc 100%);
}
.timing-target {
position: absolute;
top: 0;
bottom: 0;
left: 41%;
width: 18%;
border-radius: inherit;
background: rgba(40, 201, 130, 0.72);
box-shadow: 0 0 13px rgba(40, 201, 130, 0.35);
}
.timing-center {
position: absolute;
top: -3px;
bottom: -3px;
left: 50%;
width: 2px;
background: rgba(21, 56, 70, 0.42);
}
#timingNeedle {
position: absolute;
top: -4px;
left: 50%;
width: 7px;
height: 22px;
border: 2px solid white;
border-radius: 5px;
background: var(--ink);
box-shadow: 0 2px 6px rgba(21, 56, 70, 0.25);
transform: translateX(-50%);
transition: left 0.04s linear;
}
.feedback {
position: absolute;
top: 24%;
left: 50%;
min-width: 220px;
padding: 12px 20px;
border: 2px solid white;
border-radius: 16px;
opacity: 0;
color: white;
text-align: center;
box-shadow: 0 12px 30px rgba(21, 56, 70, 0.2);
transform: translate(-50%, 12px) scale(0.94);
transition: opacity 0.16s, transform 0.16s;
}
.feedback.show {
opacity: 1;
transform: translate(-50%, 0) scale(1);
}
.feedback strong {
display: block;
font-size: 22px;
font-weight: 950;
letter-spacing: -0.03em;
}
.feedback span {
display: block;
margin-top: 2px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
opacity: 0.88;
text-transform: uppercase;
}
.dock {
position: absolute;
bottom: 20px;
left: 50%;
width: min(720px, calc(100% - 28px));
padding: 12px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 24px;
background: rgba(255, 255, 255, 0.91);
box-shadow: 0 18px 50px rgba(35, 76, 91, 0.2);
backdrop-filter: blur(16px);
transform: translateX(-50%);
pointer-events: auto;
}
.dock-main {
display: flex;
align-items: stretch;
gap: 10px;
}
#pushButton {
position: relative;
display: flex;
min-width: 190px;
align-items: center;
justify-content: center;
gap: 13px;
padding: 13px 25px;
overflow: hidden;
border: 0;
border-radius: 16px;
color: white;
cursor: pointer;
background: linear-gradient(135deg, #ff8464, #f25d43);
box-shadow:
0 6px 0 #c84734,
0 11px 20px rgba(242, 93, 67, 0.25);
transition: transform 0.08s, box-shadow 0.08s, filter 0.15s;
}
#pushButton::before {
position: absolute;
top: -35px;
left: -45px;
width: 85px;
height: 100px;
content: "";
background: rgba(255, 255, 255, 0.16);
transform: rotate(22deg);
}
#pushButton:hover {
filter: saturate(1.08) brightness(1.03);
}
#pushButton:active,
#pushButton.pressed {
box-shadow:
0 2px 0 #c84734,
0 5px 10px rgba(242, 93, 67, 0.2);
transform: translateY(4px);
}
.push-icon {
font-size: 25px;
font-weight: 950;
line-height: 1;
}
.push-copy {
text-align: left;
}
.push-copy strong {
display: block;
font-size: 20px;
font-weight: 950;
letter-spacing: 0.02em;
line-height: 1;
}
.push-copy small {
display: block;
margin-top: 5px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
opacity: 0.8;
text-transform: uppercase;
}
.controls {
display: grid;
flex: 1;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.control-button {
border: 1px solid #d8e5e7;
border-radius: 13px;
color: var(--ink);
cursor: pointer;
background: #f6faf9;
transition: 0.15s;
}
.control-button:hover {
border-color: #b7d3d0;
background: white;
}
.control-button.active {
border-color: rgba(40, 201, 130, 0.45);
color: var(--green-dark);
background: #e9fbf4;
}
.control-button strong {
display: block;
font-size: 12px;
font-weight: 900;
}
.control-button small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
}
.damping {
display: flex;
flex-direction: column;
justify-content: center;
padding: 8px 14px;
border-radius: 13px;
background: #f2f7f7;
}
.damping-label {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.09em;
text-transform: uppercase;
}
#dampingValue {
color: var(--ink);
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 99px;
outline: none;
cursor: pointer;
appearance: none;
background: #cbdcdd;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid white;
border-radius: 50%;
appearance: none;
background: var(--green);
box-shadow: 0 2px 6px rgba(21, 56, 70, 0.24);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid white;
border-radius: 50%;
background: var(--green);
box-shadow: 0 2px 6px rgba(21, 56, 70, 0.24);
}
.hint {
position: absolute;
bottom: 25px;
left: 25px;
max-width: 260px;
padding: 13px 15px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 16px;
color: #315d6d;
background: rgba(255, 255, 255, 0.78);
box-shadow: 0 10px 25px rgba(35, 76, 91, 0.12);
backdrop-filter: blur(12px);
font-size: 11px;
font-weight: 700;
line-height: 1.45;
}
.hint strong {
color: var(--ink);
}
.key {
display: inline-block;
padding: 2px 6px;
border: 1px solid #c9d9dc;
border-bottom-width: 3px;
border-radius: 5px;
color: var(--ink);
background: white;
font-size: 9px;
font-weight: 900;
}
.legend {
position: absolute;
right: 24px;
bottom: 25px;
color: #315d6d;
font-size: 10px;
font-weight: 800;
text-align: right;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
}
.legend span {
display: block;
margin-top: 5px;
}
.legend i {
display: inline-block;
width: 9px;
height: 9px;
margin-right: 5px;
border-radius: 2px;
vertical-align: -1px;
}
@media (max-width: 760px) {
.brand {
top: 17px;
left: 17px;
}
.brand p {
display: none;
}
.stats {
top: 14px;
right: 14px;
width: 184px;
padding: 12px;
border-radius: 16px;
}
.stats-title,
.timing-label {
font-size: 8px;
}
.stat-row {
margin-top: 6px;
}
.stat-row strong {
font-size: 13px;
}
.dock {
bottom: 12px;
width: calc(100% - 20px);
padding: 9px;
border-radius: 19px;
}
.dock-main {
gap: 7px;
}
#pushButton {
min-width: 124px;
padding: 11px 13px;
}
.push-copy strong {
font-size: 16px;
}
.push-copy small {
font-size: 7px;
}
.controls {
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr;
}
.damping {
display: none;
}
.hint,
.legend {
display: none;
}
.feedback {
top: 27%;
}
}
@media (max-height: 620px) {
.brand p,
.hint,
.legend {
display: none;
}
.dock {
bottom: 8px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="ui">
<header class="brand">
<div class="eyebrow">Voxel playground / 01</div>
<h1>Swing Lab</h1>
<p>Time a push to the rhythm of the swing. Reinforce the motion and watch resonance build.</p>
</header>
<section class="stats" aria-label="Swing statistics">
<div class="stats-title">
<span><i class="live-dot"></i>Live resonance</span>
<span id="combo">×0</span>
</div>
<div class="stat-row">
<span>Amplitude</span>
<strong id="amplitude">0°</strong>
</div>
<div class="meter"><i id="amplitudeFill"></i></div>
<div class="stat-row">
<span>Energy</span>
<strong id="energy">0%</strong>
</div>
<div class="meter"><i id="energyFill"></i></div>
<div class="stat-row">
<span>Resonance</span>
<strong id="resonance">0%</strong>
</div>
<div class="meter"><i id="resonanceFill"></i></div>
<div class="timing-label">
<span>Drive phase</span>
<span id="phaseState">Waiting</span>
</div>
<div class="timing-track">
<div class="timing-target"></div>
<div class="timing-center"></div>
<div id="timingNeedle"></div>
</div>
</section>
<div id="feedback" class="feedback">
<strong id="feedbackTitle">Perfect push</strong>
<span id="feedbackText">Energy added</span>
</div>
<section class="dock" aria-label="Swing controls">
<div class="dock-main">
<button id="pushButton" type="button" aria-label="Push the swing">
<span class="push-icon">↗</span>
<span class="push-copy">
<strong>PUSH</strong>
<small>Space key</small>
</span>
</button>
<div class="controls">
<button id="autoButton" class="control-button" type="button" aria-pressed="false">
<strong>Auto-drive</strong>
<small>Watch resonance</small>
</button>
<button id="resetButton" class="control-button" type="button">
<strong>Reset</strong>
<small>Calm the swing</small>
</button>
</div>
<label class="damping">
<span class="damping-label">
<span>Damping</span>
<span id="dampingValue">0.14</span>
</span>
<input
id="damping"
type="range"
min="0.04"
max="0.42"
step="0.01"
value="0.14"
aria-label="Swing damping"
>
</label>
</div>
</section>
<aside class="hint">
<strong>How to resonate</strong><br>
Push when the yellow marker enters the green arc.
On-beat pushes add energy; late pushes remove it.
The swing is safely bounded at <strong>45°</strong>.
<br><br>
Drag to orbit · Scroll to zoom · <span class="key">SPACE</span> to push
</aside>
<div class="legend">
<span><i style="background:#ffd34f"></i>Phase marker</span>
<span><i style="background:#28c982"></i>Push window</span>
<span><i style="background:#f06464"></i>Motion limits</span>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const stage = $("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x91cde8);
scene.fog = new THREE.Fog(0x91cde8, 16, 31);
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.08;
stage.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xdff5ff, 0x71834e, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2cf, 3.25);
sun.position.set(-7, 12, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0x91cfff, 0.75);
fillLight.position.set(7, 5, -6);
scene.add(fillLight);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.02,
...options
});
}
const mats = {
grass: material(0x72b75f),
grassDark: material(0x5a9d51),
sandA: material(0xf4d795),
sandB: material(0xedc97f),
border: material(0xdcb66d),
wood: material(0xd99a45),
woodLight: material(0xf0b85c),
woodDark: material(0xa96d32),
steel: material(0x416772, { metalness: 0.32, roughness: 0.48 }),
rope: material(0xf2e0ae),
seat: material(0xef6850, {
emissive: 0x5a1308,
emissiveIntensity: 0.08
}),
shirt: material(0xf39a50),
pants: material(0x3b72a6),
skin: material(0xf3bd89),
hair: material(0x4b3029),
shoe: material(0x263f4d),
dial: material(0x315d68, { emissive: 0x102c34, emissiveIntensity: 0.25 }),
target: material(0x28c982, {
emissive: 0x0a7a4b,
emissiveIntensity: 0.85
}),
marker: material(0xffd34f, {
emissive: 0xa96700,
emissiveIntensity: 1.1
}),
limit: material(0xf06464, {
emissive: 0x8b2020,
emissiveIntensity: 0.35
}),
trunk: material(0x99623e),
leafA: material(0x4e9a56),
leafB: material(0x65b75f),
cloud: material(0xf4fbff),
fence: material(0xe9e2c9),
ball: material(0x5675c7)
};
function addBox(parent, size, position, mat, cast = true, receive = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
parent.add(mesh);
return mesh;
}
function addBeam(parent, a, b, width, mat, segments = 1) {
const av = new THREE.Vector3(...a);
const bv = new THREE.Vector3(...b);
const direction = bv.clone().sub(av);
const length = direction.length();
direction.normalize();
const pieces = [];
for (let i = 0; i < segments; i++) {
const p1 = av.clone().lerp(bv, i / segments);
const p2 = av.clone().lerp(bv, (i + 1) / segments);
const p = p1.clone().add(p2).multiplyScalar(0.5);
const piece = addBox(
parent,
[width, length / segments + 0.018, width],
[p.x, p.y, p.z],
mat
);
piece.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), direction);
pieces.push(piece);
}
return pieces;
}
// Playground ground and tiled sandbox.
addBox(scene, [17, 0.55, 12], [0, -0.36, 0], mats.grass, false, true);
addBox(scene, [10.5, 0.13, 7.2], [0, -0.02, 0], mats.sandA, false, true);
const tileCountX = 14;
const tileCountZ = 10;
const tileW = 0.72;
const tileD = 0.69;
for (let x = 0; x < tileCountX; x++) {
for (let z = 0; z < tileCountZ; z++) {
const tileMat = (x + z) % 2 ? mats.sandA : mats.sandB;
addBox(
scene,
[tileW, 0.055, tileD],
[
(x - (tileCountX - 1) / 2) * tileW,
0.065,
(z - (tileCountZ - 1) / 2) * tileD
],
tileMat,
false,
true
);
}
}
// Sandbox border.
addBox(scene, [10.7, 0.22, 0.18], [0, 0.15, -3.63], mats.border);
addBox(scene, [10.7, 0.22, 0.18], [0, 0.15, 3.63], mats.border);
addBox(scene, [0.18, 0.22, 7.1], [-5.25, 0.15, 0], mats.border);
addBox(scene, [0.18, 0.22, 7.1], [5.25, 0.15, 0], mats.border);
// Swing frame.
const frame = new THREE.Group();
scene.add(frame);
const frameZ = [-1.48, 1.48];
for (const z of frameZ) {
addBeam(frame, [-2.35, 0.22, z], [-0.18, 4.33, z], 0.2, mats.wood, 12);
addBeam(frame, [2.35, 0.22, z], [0.18, 4.33, z], 0.2, mats.wood, 12);
addBeam(frame, [-0.18, 4.33, z], [0.18, 4.33, z], 0.2, mats.woodLight, 2);
addBeam(frame, [-1.92, 0.88, z], [1.92, 0.88, z], 0.13, mats.woodDark, 8);
addBox(frame, [0.62, 0.17, 0.56], [-2.35, 0.16, z], mats.steel);
addBox(frame, [0.62, 0.17, 0.56], [2.35, 0.16, z], mats.steel);
}
addBeam(frame, [0, 4.33, -1.62], [0, 4.33, 1.62], 0.23, mats.woodLight, 10);
addBeam(frame, [0, 3.88, -1.48], [0, 3.88, 1.48], 0.12, mats.steel, 7);
for (const z of frameZ) {
for (const x of [-0.18, 0.18]) {
addBox(frame, [0.25, 0.25, 0.25], [x, 4.33, z], mats.steel);
addBox(frame, [0.08, 0.08, 0.29], [x, 4.33, z], mats.woodDark);
}
}
// Pivot and swinging rider.
const pivotY = 4.16;
const swingLength = 2.25;
const swingPivot = new THREE.Group();
swingPivot.position.set(0, pivotY, 0);
scene.add(swingPivot);
addBox(swingPivot, [0.3, 0.3, 0.75], [0, 0, 0], mats.steel);
addBeam(swingPivot, [0, -0.05, -0.23], [0, -2.18, -0.23], 0.065, mats.rope, 7);
addBeam(swingPivot, [0, -0.05, 0.23], [0, -2.18, 0.23], 0.065, mats.rope, 7);
addBox(swingPivot, [1.02, 0.17, 0.75], [0, -2.26, 0], mats.seat);
addBox(swingPivot, [0.12, 0.35, 0.86], [-0.51, -2.09, 0], mats.woodDark);
addBox(swingPivot, [0.12, 0.35, 0.86], [0.51, -2.09, 0], mats.woodDark);
// Small voxel rider.
addBox(swingPivot, [0.58, 0.68, 0.45], [0, -1.76, 0], mats.shirt);
addBox(swingPivot, [0.48, 0.47, 0.45], [0, -1.18, 0], mats.skin);
addBox(swingPivot, [0.5, 0.16, 0.47], [0, -0.99, 0], mats.hair);
addBox(swingPivot, [0.13, 0.13, 0.06], [-0.12, -1.16, 0.245], mats.hair);
addBox(swingPivot, [0.13, 0.13, 0.06], [0.12, -1.16, 0.245], mats.hair);
addBox(swingPivot, [0.22, 0.42, 0.27], [-0.17, -2.45, 0.13], mats.pants);
addBox(swingPivot, [0.22, 0.42, 0.27], [0.17, -2.45, 0.13], mats.pants);
addBox(swingPivot, [0.29, 0.15, 0.38], [-0.17, -2.69, 0.16], mats.shoe);
addBox(swingPivot, [0.29, 0.15, 0.38], [0.17, -2.69, 0.16], mats.shoe);
addBeam(swingPivot, [-0.27, -1.55, 0.12], [-0.13, -1.9, 0.25], 0.15, mats.shirt, 2);
addBeam(swingPivot, [0.27, -1.55, 0.12], [0.13, -1.9, 0.25], 0.15, mats.shirt, 2);
// Voxel friend providing the timed push.
const pusher = new THREE.Group();
pusher.position.set(2.7, 0.08, 0.35);
scene.add(pusher);
addBox(pusher, [0.5, 0.68, 0.48], [0, 0.9, 0], mats.shirt);
addBox(pusher, [0.48, 0.46, 0.46], [0, 1.47, 0], mats.skin);
addBox(pusher, [0.51, 0.15, 0.49], [0, 1.67, 0], mats.hair);
addBox(pusher, [0.21, 0.58, 0.27], [-0.14, 0.27, 0], mats.pants);
addBox(pusher, [0.21, 0.58, 0.27], [0.14, 0.27, 0], mats.pants);
addBox(pusher, [0.27, 0.14, 0.38], [-0.14, -0.08, 0.05], mats.shoe);
addBox(pusher, [0.27, 0.14, 0.38], [0.14, -0.08, 0.05], mats.shoe);
const pushArm = new THREE.Group();
pushArm.position.set(-0.23, 1.08, 0.03);
pusher.add(pushArm);
addBox(pushArm, [0.67, 0.18, 0.2], [-0.31, 0, 0], mats.shirt);
addBox(pushArm, [0.2, 0.2, 0.2], [-0.68, 0, 0], mats.skin);
// Phase dial around the pivot.
const dialGroup = new THREE.Group();
dialGroup.position.set(0, pivotY, 1.04);
scene.add(dialGroup);
const dialRadius = 1.08;
const targetDialAngle = -Math.PI / 2;
const targetBlocks = [];
for (let i = 0; i < 48; i++) {
const angle = (i / 48) * Math.PI * 2;
let delta = angle - targetDialAngle;
delta = Math.atan2(Math.sin(delta), Math.cos(delta));
const isTarget = Math.abs(delta) < 0.35;
const block = addBox(
dialGroup,
[0.13, 0.13, 0.12],
[
Math.cos(angle) * dialRadius,
Math.sin(angle) * dialRadius,
0
],
isTarget ? mats.target : mats.dial,
false,
false
);
if (isTarget) targetBlocks.push(block);
}
addBox(dialGroup, [0.24, 0.24, 0.2], [0, 0, 0], mats.steel);
const phaseMarker = addBox(
dialGroup,
[0.24, 0.24, 0.25],
[dialRadius, 0, 0.08],
mats.marker,
false,
false
);
// Bounded swing arc and red end stops.
const maxAngle = Math.PI / 4;
for (let i = 0; i <= 34; i++) {
const theta = -maxAngle + (i / 34) * maxAngle * 2;
addBox(
scene,
[0.09, 0.09, 0.09],
[
Math.sin(theta) * swingLength,
pivotY - Math.cos(theta) * swingLength,
-0.66
],
mats.woodLight,
false,
false
);
}
for (const theta of [-maxAngle, maxAngle]) {
addBox(
scene,
[0.32, 0.32, 0.28],
[
Math.sin(theta) * swingLength,
pivotY - Math.cos(theta) * swingLength,
-0.64
],
mats.limit
);
}
// Background fence.
for (let x = -5; x <= 5; x += 0.62) {
addBox(scene, [0.13, 1.25, 0.13], [x, 0.65, -4.25], mats.fence);
}
addBox(scene, [10.6, 0.14, 0.12], [0, 0.35, -4.25], mats.fence);
addBox(scene, [10.6, 0.14, 0.12], [0, 0.9, -4.25], mats.fence);
// Voxel trees.
function addTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
scene.add(tree);
addBox(tree, [0.48, 2.5, 0.48], [0, 1.25, 0], mats.trunk);
addBox(tree, [1.8, 0.85, 1.45], [0, 2.65, 0], mats.leafA);
addBox(tree, [1.3, 0.8, 1.7], [-0.38, 3.2, 0.1], mats.leafB);
addBox(tree, [1.25, 0.75, 1.2], [0.45, 3.48, -0.12], mats.leafA);
addBox(tree, [0.8, 0.7, 0.8], [0.05, 4.0, 0], mats.leafB);
}
addTree(-5.7, -2.6, 1.12);
addTree(5.6, -2.8, 1.0);
addTree(-6.2, 2.3, 0.82);
addTree(6.0, 2.7, 0.78);
// Bench and ball.
addBox(scene, [1.75, 0.18, 0.58], [-3.55, 0.72, 2.45], mats.woodLight);
addBox(scene, [1.75, 0.62, 0.14], [-3.55, 1.13, 2.72], mats.wood);
addBox(scene, [0.18, 0.65, 0.5], [-4.15, 0.34, 2.45], mats.steel);
addBox(scene, [0.18, 0.65, 0.5], [-2.95, 0.34, 2.45], mats.steel);
addBox(scene, [0.48, 0.48, 0.48], [-3.25, 0.35, -1.75], mats.ball);
addBox(scene, [0.49, 0.13, 0.13], [-3.25, 0.35, -1.75], mats.woodLight);
// Blocky clouds.
function addCloud(x, y, z, scale) {
const cloud = new THREE.Group();
cloud.position.set(x, y, z);
cloud.scale.setScalar(scale);
scene.add(cloud);
addBox(cloud, [1.8, 0.62, 0.62], [0, 0, 0], mats.cloud, false, false);
addBox(cloud, [0.8, 1.0, 0.7], [-0.65, 0.12, 0], mats.cloud, false, false);
addBox(cloud, [0.95, 0.8, 0.7], [0.1, 0.22, 0], mats.cloud, false, false);
addBox(cloud, [0.7, 0.85, 0.7], [0.78, 0.03, 0], mats.cloud, false, false);
}
addCloud(-4.4, 7.1, -7, 1.2);
addCloud(4.5, 7.7, -9, 1.45);
addCloud(0.4, 8.7, -12, 0.9);
// Swing motion trail.
const trailDots = [];
const trailMaterial = material(0xff9b77, {
transparent: true,
opacity: 0.28,
depthWrite: false,
emissive: 0x762c16,
emissiveIntensity: 0.4
});
for (let i = 0; i < 18; i++) {
const dot = addBox(
scene,
[0.13 - i * 0.003, 0.13 - i * 0.003, 0.13 - i * 0.003],
[0, -10, 0],
trailMaterial.clone(),
false,
false
);
dot.visible = false;
trailDots.push(dot);
}
const trailHistory = [];
// Spark particles.
const sparks = [];
const sparkGeometry = new THREE.BoxGeometry(0.09, 0.09, 0.09);
function spawnSparks(quality) {
const color = quality > 0.72 ? 0xffd34f : quality > 0 ? 0xff9a67 : 0xf06464;
const x = Math.sin(theta) * swingLength;
const y = pivotY - Math.cos(theta) * swingLength;
for (let i = 0; i < 12; i++) {
const sparkMat = material(color, {
transparent: true,
opacity: 1,
emissive: color,
emissiveIntensity: 0.65
});
const mesh = new THREE.Mesh(sparkGeometry, sparkMat);
mesh.position.set(x, y, 0.42);
scene.add(mesh);
sparks.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 2.3,
Math.random() * 1.8 + 0.4,
(Math.random() - 0.5) * 1.8
),
life: 0.55 + Math.random() * 0.25
});
}
}
// Physics state.
const gravity = 13;
const naturalFrequency = Math.sqrt(gravity / swingLength);
const fixedStep = 1 / 120;
const pushPower = 0.72;
const targetPhase = Math.PI / 2;
let theta = 0.14;
let omega = 0;
let damping = 0.14;
let accumulator = 0;
let combo = 0;
let coherence = 0;
let autoEnabled = false;
let autoArmed = false;
let lastPushTime = -1000;
let pushAnimation = 0;
let feedbackUntil = 0;
let trailTimer = 0;
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function wrapAngle(angle) {
return Math.atan2(Math.sin(angle), Math.cos(angle));
}
function getPhase() {
let phase = Math.atan2(-omega / naturalFrequency, theta);
if (phase < 0) phase += Math.PI * 2;
return phase;
}
function getPhaseError() {
return wrapAngle(getPhase() - targetPhase);
}
function getAmplitude() {
const energy =
0.5 * omega * omega +
naturalFrequency * naturalFrequency * (1 - Math.cos(theta));
return Math.acos(clamp(1 - energy / (naturalFrequency * naturalFrequency), -1, 1));
}
function physicsStep(dt) {
const acceleration =
-naturalFrequency * naturalFrequency * Math.sin(theta) -
damping * omega;
omega += acceleration * dt;
omega = clamp(omega, -3.15, 3.15);
theta += omega * dt;
// Hard but soft-bouncing motion limits.
if (Math.abs(theta) > maxAngle) {
theta = Math.sign(theta) * maxAngle;
omega = -Math.abs(omega) * 0.22;
}
}
function showFeedback(title, text, color = "#28c982") {
$("feedbackTitle").textContent = title;
$("feedbackText").textContent = text;
$("feedback").style.background = color;
$("feedback").classList.add("show");
feedbackUntil = performance.now() + 1050;
}
function performPush(source = "manual") {
const now = performance.now();
if (now - lastPushTime < 110) return;
lastPushTime = now;
const error = getPhaseError();
const quality = Math.cos(error);
const direction = Math.sign(omega) || -1;
omega = clamp(omega + direction * pushPower * quality, -3.15, 3.15);
coherence += (quality - coherence) * 0.24;
if (quality > 0.72) {
combo += 1;
} else {
combo = 0;
}
pushAnimation = 1;
spawnSparks(quality);
$("pushButton").classList.add("pressed");
setTimeout(() => $("pushButton").classList.remove("pressed"), 110);
if (quality > 0.93) {
showFeedback("Perfect push", `${combo} clean beat${combo === 1 ? "" : "s"}`, "#20aa70");
} else if (quality > 0.72) {
showFeedback("Good push", "Energy reinforced", "#299b78");
} else if (quality > 0.08) {
showFeedback(
error < 0 ? "Slightly early" : "Slightly late",
"Almost in phase",
"#d99135"
);
} else if (quality > -0.5) {
showFeedback("Soft push", "Little energy transferred", "#8a8f74");
} else {
showFeedback(
"Off beat",
source === "auto" ? "Try waiting for the green arc" : "This push removed energy",
"#d65b50"
);
}
}
$("pushButton").addEventListener("click", () => performPush("manual"));
$("autoButton").addEventListener("click", () => {
autoEnabled = !autoEnabled;
autoArmed = false;
$("autoButton").classList.toggle("active", autoEnabled);
$("autoButton").setAttribute("aria-pressed", String(autoEnabled));
showFeedback(
autoEnabled ? "Auto-drive on" : "Auto-drive off",
autoEnabled ? "Pushing on every green beat" : "You control the timing",
autoEnabled ? "#20aa70" : "#416772"
);
});
$("resetButton").addEventListener("click", () => {
theta = 0.14;
omega = 0;
combo = 0;
coherence = 0;
autoArmed = false;
trailHistory.length = 0;
showFeedback("Swing reset", "Ready for a new rhythm", "#416772");
});
$("damping").addEventListener("input", (event) => {
damping = Number(event.target.value);
$("dampingValue").textContent = damping.toFixed(2);
});
window.addEventListener("keydown", (event) => {
if (
event.code === "Space" &&
event.target !== $("damping")
) {
event.preventDefault();
performPush("manual");
}
});
// Direct camera controls; no addons are used.
let cameraAzimuth = 0.72;
let cameraPolar = 1.06;
let cameraDistance = 11.3;
let targetAzimuth = cameraAzimuth;
let targetPolar = cameraPolar;
let targetDistance = cameraDistance;
let dragging = false;
let dragX = 0;
let dragY = 0;
const cameraTarget = new THREE.Vector3(0, 2.35, 0);
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
targetAzimuth -= (event.clientX - dragX) * 0.006;
targetPolar = clamp(
targetPolar - (event.clientY - dragY) * 0.005,
0.67,
1.34
);
dragX = event.clientX;
dragY = event.clientY;
});
function endDrag(event) {
dragging = false;
if (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) => {
event.preventDefault();
targetDistance = clamp(
targetDistance + event.deltaY * 0.008,
7.2,
15.5
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
targetAzimuth = 0.72;
targetPolar = 1.06;
targetDistance = 11.3;
});
function updateCamera(dt) {
const smoothing = 1 - Math.pow(0.001, dt);
cameraAzimuth += (targetAzimuth - cameraAzimuth) * smoothing;
cameraPolar += (targetPolar - cameraPolar) * smoothing;
cameraDistance += (targetDistance - cameraDistance) * smoothing;
const sinPolar = Math.sin(cameraPolar);
camera.position.set(
cameraTarget.x + cameraDistance * sinPolar * Math.sin(cameraAzimuth),
cameraTarget.y + cameraDistance * Math.cos(cameraPolar),
cameraTarget.z + cameraDistance * sinPolar * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
}
function updateStats() {
const phase = getPhase();
const error = getPhaseError();
const quality = Math.cos(error);
const amplitude = getAmplitude();
const maxEnergy =
naturalFrequency * naturalFrequency * (1 - Math.cos(maxAngle));
const energy =
0.5 * omega * omega +
naturalFrequency * naturalFrequency * (1 - Math.cos(theta));
const energyRatio = clamp(energy / maxEnergy, 0, 1);
const resonance = clamp(energyRatio * 0.76 + coherence * 0.24, 0, 1);
$("amplitude").textContent = `${Math.round(THREE.MathUtils.radToDeg(amplitude))}°`;
$("amplitudeFill").style.width = `${amplitude / maxAngle * 100}%`;
$("energy").textContent = `${Math.round(energyRatio * 100)}%`;
$("energyFill").style.width = `${energyRatio * 100}%`;
$("resonance").textContent = `${Math.round(resonance * 100)}%`;
$("resonanceFill").style.width = `${resonance * 100}%`;
$("combo").textContent = `×${combo}`;
const needlePosition = 50 + (error / Math.PI) * 50;
$("timingNeedle").style.left = `${needlePosition}%`;
if (quality > 0.88) {
$("phaseState").textContent = "Push now";
$("phaseState").style.color = "#15875a";
} else if (quality > 0.35) {
$("phaseState").textContent = "Approaching";
$("phaseState").style.color = "#bc7a21";
} else {
$("phaseState").textContent = "Waiting";
$("phaseState").style.color = "#69818b";
}
const markerAngle = -phase;
phaseMarker.position.x = Math.cos(markerAngle) * dialRadius;
phaseMarker.position.y = Math.sin(markerAngle) * dialRadius;
phaseMarker.rotation.z = markerAngle;
const targetGlow = 0.35 + Math.max(0, quality) * 1.25;
mats.target.emissiveIntensity = targetGlow;
const seatGlow = quality > 0.8 ? 0.35 : 0.08;
mats.seat.emissiveIntensity +=
(seatGlow - mats.seat.emissiveIntensity) * 0.15;
}
function updateSparks(dt) {
for (let i = sparks.length - 1; i >= 0; i--) {
const spark = sparks[i];
spark.life -= dt;
spark.velocity.y -= 3.6 * dt;
spark.mesh.position.addScaledVector(spark.velocity, dt);
spark.mesh.rotation.x += dt * 5;
spark.mesh.rotation.y += dt * 7;
spark.mesh.material.opacity = clamp(spark.life * 2.2, 0, 1);
spark.mesh.scale.setScalar(0.7 + spark.life);
if (spark.life <= 0) {
scene.remove(spark.mesh);
spark.mesh.material.dispose();
sparks.splice(i, 1);
}
}
}
function updateTrail(dt) {
trailTimer += dt;
if (trailTimer > 0.035) {
trailTimer = 0;
trailHistory.unshift({
x: Math.sin(theta) * swingLength,
y: pivotY - Math.cos(theta) * swingLength
});
if (trailHistory.length > trailDots.length) trailHistory.pop();
}
for (let i = 0; i < trailDots.length; i++) {
const dot = trailDots[i];
if (!trailHistory[i]) {
dot.visible = false;
continue;
}
dot.visible = true;
dot.position.set(
trailHistory[i].x,
trailHistory[i].y,
0.12
);
const scale = 1 - i / trailDots.length;
dot.scale.setScalar(0.45 + scale);
dot.material.opacity = scale * 0.26;
}
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
let lastTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
accumulator += dt;
while (accumulator >= fixedStep) {
physicsStep(fixedStep);
accumulator -= fixedStep;
}
swingPivot.rotation.z = theta;
const quality = Math.cos(getPhaseError());
if (autoEnabled) {
if (quality > 0.965 && !autoArmed) {
autoArmed = true;
performPush("auto");
}
if (quality < 0.65) autoArmed = false;
}
pushAnimation = Math.max(0, pushAnimation - dt * 2.8);
pushArm.scale.x = 1 + pushAnimation * 0.85;
pushArm.rotation.z = pushAnimation * 0.12;
updateCamera(dt);
updateStats();
updateSparks(dt);
updateTrail(dt);
if (feedbackUntil && now > feedbackUntil) {
$("feedback").classList.remove("show");
feedbackUntil = 0;
}
renderer.render(scene, camera);
}
updateCamera(1);
requestAnimationFrame(animate);
</script>
</body>
</html>
```