Prompt specification
Build or customize the Three.js 3D scene "Crossing One Periodic Gate" 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: Crossing One Periodic Gate
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/crossing-one-periodic-gate-x3478
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: crossing one periodic gate. Create a short walkway interrupted by a smoothly opening gate. Show the cycle timer and require enough open time for the player proxy to cross. Return one self-contained HTML file.
Working Three.js Starter (crossing-one-periodic-gate-x3478):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Periodic Gate — Low-Poly Crossing</title>
<style>
:root {
--accent: #ef6a67;
--panel: rgba(15, 29, 37, 0.82);
--line: rgba(255, 255, 255, 0.13);
--text: #f4fbfb;
--muted: #9fb5bb;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9e8eb;
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
user-select: none;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 42%, transparent 45%, rgba(17, 40, 48, 0.13) 100%),
linear-gradient(180deg, rgba(255, 255, 255, 0.04), transparent 30%);
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 24px;
width: min(360px, calc(100vw - 48px));
padding: 17px 19px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(24, 45, 53, 0.18);
backdrop-filter: blur(12px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 16px var(--accent);
}
h1 {
margin: 8px 0 4px;
font-size: clamp(20px, 2.4vw, 28px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.brand p {
margin: 8px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.cycle-panel {
position: absolute;
top: 24px;
right: 24px;
width: min(370px, calc(100vw - 48px));
padding: 17px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(24, 45, 53, 0.2);
backdrop-filter: blur(12px);
}
.cycle-top {
display: grid;
grid-template-columns: 88px 1fr;
gap: 16px;
align-items: center;
}
.dial {
position: relative;
display: grid;
place-items: center;
width: 88px;
height: 88px;
border-radius: 50%;
background: conic-gradient(var(--accent) 0deg, rgba(255, 255, 255, 0.1) 0deg);
box-shadow: 0 0 28px rgba(0, 0, 0, 0.13);
}
.dial::after {
content: "";
position: absolute;
inset: 6px;
border-radius: inherit;
background: #12262e;
border: 1px solid rgba(255, 255, 255, 0.08);
}
.dial-inner {
position: relative;
z-index: 1;
text-align: center;
}
#dial-value {
display: block;
font-size: 23px;
font-weight: 850;
line-height: 1;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
#dial-caption {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.phase-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
#phase-name {
color: var(--accent);
font-size: 17px;
font-weight: 900;
letter-spacing: 0.08em;
}
#phase-hint {
margin-top: 5px;
color: var(--muted);
font-size: 12px;
}
.segments {
display: flex;
gap: 5px;
margin-top: 15px;
}
.segment {
position: relative;
height: 7px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.09);
transition: background 180ms ease;
}
.segment.done {
background: rgba(255, 255, 255, 0.29);
}
.segment.current {
background: var(--accent);
box-shadow: 0 0 12px color-mix(in srgb, var(--accent), transparent 45%);
}
.cycle-meta {
display: flex;
justify-content: space-between;
gap: 16px;
margin-top: 11px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.035em;
}
.cycle-meta strong {
color: var(--text);
font-weight: 800;
}
.controls {
position: absolute;
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 10px;
transform: translateX(-50%);
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(15, 29, 37, 0.82);
box-shadow: 0 14px 36px rgba(24, 45, 53, 0.17);
backdrop-filter: blur(10px);
white-space: nowrap;
}
.key {
display: inline-grid;
min-width: 27px;
height: 27px;
place-items: center;
padding: 0 7px;
border: 1px solid rgba(255, 255, 255, 0.23);
border-bottom-width: 3px;
border-radius: 7px;
color: white;
background: rgba(255, 255, 255, 0.08);
font-size: 11px;
font-weight: 900;
}
.controls span {
color: var(--muted);
font-size: 11px;
}
.divider {
width: 1px;
height: 24px;
background: var(--line);
}
button {
pointer-events: auto;
border: 0;
color: white;
font: inherit;
cursor: pointer;
}
#reset-button {
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 9px;
background: rgba(255, 255, 255, 0.09);
font-size: 11px;
font-weight: 800;
}
#reset-button:hover {
background: rgba(255, 255, 255, 0.16);
}
#toast {
position: absolute;
left: 50%;
bottom: 91px;
max-width: min(560px, calc(100vw - 40px));
padding: 10px 15px;
border: 1px solid var(--line);
border-radius: 999px;
color: white;
background: rgba(15, 29, 37, 0.88);
box-shadow: 0 10px 30px rgba(20, 40, 47, 0.18);
font-size: 12px;
font-weight: 700;
text-align: center;
opacity: 0;
transform: translate(-50%, 8px);
transition: opacity 180ms ease, transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.success {
border-color: rgba(71, 201, 154, 0.45);
}
#toast.warning {
border-color: rgba(240, 139, 85, 0.55);
}
#result {
position: absolute;
top: 50%;
left: 50%;
width: min(390px, calc(100vw - 40px));
padding: 25px;
border: 1px solid rgba(115, 234, 193, 0.35);
border-radius: 20px;
background: rgba(15, 35, 39, 0.94);
box-shadow: 0 28px 80px rgba(10, 30, 34, 0.34);
text-align: center;
opacity: 0;
pointer-events: none;
transform: translate(-50%, -46%) scale(0.96);
transition: opacity 220ms ease, transform 220ms ease;
}
#result.show {
opacity: 1;
pointer-events: auto;
transform: translate(-50%, -50%) scale(1);
}
.result-mark {
display: grid;
width: 54px;
height: 54px;
margin: 0 auto 13px;
place-items: center;
border-radius: 50%;
color: #102a2d;
background: #73eabf;
font-size: 28px;
font-weight: 950;
box-shadow: 0 0 35px rgba(115, 234, 191, 0.35);
}
#result h2 {
margin: 0;
font-size: 27px;
letter-spacing: -0.04em;
}
#result p {
margin: 9px 0 19px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
#again-button {
padding: 11px 18px;
border-radius: 10px;
color: #102a2d;
background: #73eabf;
font-size: 12px;
font-weight: 900;
}
#dpad {
position: absolute;
right: 18px;
bottom: 20px;
display: none;
grid-template-columns: repeat(3, 48px);
grid-template-rows: repeat(2, 48px);
gap: 5px;
pointer-events: auto;
}
#dpad button {
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(15, 29, 37, 0.83);
color: white;
font-size: 19px;
font-weight: 900;
touch-action: none;
}
#dpad button:active {
background: rgba(71, 201, 154, 0.45);
}
#dpad .up {
grid-column: 2;
}
#dpad .left {
grid-column: 1;
grid-row: 2;
}
#dpad .down {
grid-column: 2;
grid-row: 2;
}
#dpad .right {
grid-column: 3;
grid-row: 2;
}
@media (max-width: 760px) {
.brand {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
.brand p {
display: none;
}
.cycle-panel {
top: 105px;
right: 12px;
width: calc(100vw - 24px);
padding: 13px;
}
.cycle-top {
grid-template-columns: 66px 1fr;
gap: 12px;
}
.dial {
width: 66px;
height: 66px;
}
#dial-value {
font-size: 18px;
}
.controls {
right: 12px;
bottom: 16px;
left: auto;
transform: none;
}
.controls .desktop-hint,
.controls .divider,
#reset-button {
display: none;
}
#toast {
bottom: 77px;
}
#dpad {
display: grid;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<div class="hud">
<section class="brand">
<div class="eyebrow">Low-poly cycle study</div>
<h1>Periodic Crossing Gate</h1>
<p>
Wait for the green window, then cross before the gate begins closing.
Drag to orbit and use the wheel to zoom.
</p>
</section>
<section class="cycle-panel">
<div class="cycle-top">
<div class="dial" id="dial">
<div class="dial-inner">
<span id="dial-value">3.2</span>
<span id="dial-caption">seconds</span>
</div>
</div>
<div>
<div class="phase-row">
<span id="phase-name">LOCKED</span>
</div>
<div id="phase-hint">Gate opens in 3.2s</div>
</div>
</div>
<div class="segments" id="segments">
<div class="segment current" style="flex: 3.2"></div>
<div class="segment" style="flex: 1.4"></div>
<div class="segment" style="flex: 3.8"></div>
<div class="segment" style="flex: 1.2"></div>
</div>
<div class="cycle-meta">
<span>Cycle <strong id="cycle-readout">0.0 / 9.6s</strong></span>
<span>Open window <strong>3.8s</strong></span>
<span>Crossing <strong>~2.7s</strong></span>
</div>
</section>
<div id="toast"></div>
<div class="controls">
<span class="key">W</span>
<span class="key">A</span>
<span class="key">S</span>
<span class="key">D</span>
<span class="desktop-hint">or arrow keys to move</span>
<span class="divider"></span>
<span class="desktop-hint">Drag to orbit · Scroll to zoom</span>
<button id="reset-button" type="button">Restart cycle</button>
</div>
<div id="dpad" aria-label="Touch movement controls">
<button class="up" data-code="KeyW" type="button" aria-label="Move forward">↑</button>
<button class="left" data-code="KeyA" type="button" aria-label="Move left">←</button>
<button class="down" data-code="KeyS" type="button" aria-label="Move backward">↓</button>
<button class="right" data-code="KeyD" type="button" aria-label="Move right">→</button>
</div>
<section id="result">
<div class="result-mark">✓</div>
<h2>Gate crossed</h2>
<p>
You reached the far side before the open window expired.
The next cycle is already running.
</p>
<button id="again-button" type="button">Run it again</button>
</section>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const toast = document.getElementById("toast");
const result = document.getElementById("result");
const dial = document.getElementById("dial");
const dialValue = document.getElementById("dial-value");
const dialCaption = document.getElementById("dial-caption");
const phaseName = document.getElementById("phase-name");
const phaseHint = document.getElementById("phase-hint");
const cycleReadout = document.getElementById("cycle-readout");
const segmentElements = [...document.querySelectorAll(".segment")];
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9e8eb);
scene.fog = new THREE.Fog(0xd9e8eb, 25, 65);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
120
);
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;
renderer.domElement.setAttribute("aria-label", "Interactive low-poly periodic gate scene");
sceneHost.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xeafcff, 0x526765, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2d5, 3.2);
sun.position.set(-10, 17, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -22;
sun.shadow.camera.right = 22;
sun.shadow.camera.top = 22;
sun.shadow.camera.bottom = -22;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 55;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x8fc7d1, 1.1);
rimLight.position.set(12, 8, -15);
scene.add(rimLight);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.04,
flatShading: true,
...options
});
}
const materials = {
ground: standardMaterial(0x91aaa0),
river: standardMaterial(0x6ea8ad, { roughness: 0.55, metalness: 0.08 }),
deckA: standardMaterial(0x60777a),
deckB: standardMaterial(0x6b8182),
underside: standardMaterial(0x344d52),
rail: standardMaterial(0x30494e),
tower: standardMaterial(0x334c52),
towerTop: standardMaterial(0x22383e),
gate: standardMaterial(0x35575c),
gatePanel: standardMaterial(0x3e6970, {
transparent: true,
opacity: 0.78,
roughness: 0.58
}),
orange: standardMaterial(0xf4a34d),
cream: standardMaterial(0xf4edcf),
teal: standardMaterial(0x45c5a0),
dark: standardMaterial(0x173138),
player: standardMaterial(0x47c9a5),
playerDark: standardMaterial(0x20454c),
visor: standardMaterial(0xf8c85e, {
emissive: 0xa95d15,
emissiveIntensity: 0.32
}),
rock: standardMaterial(0x738781),
rockDark: standardMaterial(0x5e716e),
foliage: standardMaterial(0x4e8373),
foliageDark: standardMaterial(0x396d62),
cloud: standardMaterial(0xf4faf8)
};
function addBox(parent, width, height, depth, material, x, y, z, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(width, height, depth), material);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
sides,
material,
x,
y,
z,
castShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, sides),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Distant terrain and water.
const ground = new THREE.Mesh(new THREE.PlaneGeometry(100, 100), materials.ground);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -3.55;
ground.receiveShadow = true;
scene.add(ground);
const river = addBox(
scene,
90,
0.12,
27,
materials.river,
0,
-3.38,
-9,
false
);
river.receiveShadow = true;
// Walkway deck.
const deckGroup = new THREE.Group();
scene.add(deckGroup);
for (let i = 0; i < 20; i++) {
const material = i % 2 === 0 ? materials.deckA : materials.deckB;
addBox(deckGroup, 4.5, 0.34, 0.96, material, 0, -0.17, -9.5 + i);
}
addBox(deckGroup, 0.26, 0.45, 20.4, materials.underside, -1.85, -0.48, 0);
addBox(deckGroup, 0.26, 0.45, 20.4, materials.underside, 1.85, -0.48, 0);
for (const z of [-8, -4, 4, 8]) {
for (const x of [-1.55, 1.55]) {
addBox(deckGroup, 0.34, 3.0, 0.34, materials.underside, x, -1.82, z);
}
addBox(deckGroup, 3.4, 0.25, 0.38, materials.underside, 0, -0.48, z);
}
// Railings.
for (const x of [-2.32, 2.32]) {
addBox(deckGroup, 0.08, 0.08, 20.2, materials.rail, x, 0.86, 0);
for (let z = -9.5; z <= 9.5; z += 1.9) {
addBox(deckGroup, 0.09, 0.95, 0.09, materials.rail, x, 0.43, z);
}
}
// Painted center markers.
for (let z = -8.8; z <= 8.8; z += 1.75) {
if (Math.abs(z) < 1.4) continue;
addBox(deckGroup, 0.09, 0.018, 0.58, materials.cream, 0, 0.025, z, false);
}
// Gate structure.
const gateGroup = new THREE.Group();
scene.add(gateGroup);
for (const x of [-2.72, 2.72]) {
addBox(gateGroup, 0.76, 4.25, 0.9, materials.tower, x, 2.1, 0);
addBox(gateGroup, 0.98, 0.25, 1.12, materials.towerTop, x, 4.25, 0);
addBox(gateGroup, 1.05, 0.18, 1.2, materials.towerTop, x, 0.09, 0);
for (let y = 0.55; y < 1.75; y += 0.38) {
const stripe = addBox(gateGroup, 0.79, 0.15, 0.93, materials.orange, x, y, 0);
stripe.rotation.z = -0.34;
}
}
addBox(gateGroup, 6.22, 0.55, 0.72, materials.towerTop, 0, 4.46, 0);
addBox(gateGroup, 6.45, 0.16, 0.92, materials.orange, 0, 4.73, 0);
function createGateLeaf() {
const leaf = new THREE.Group();
const leafWidth = 2.1;
addBox(leaf, leafWidth - 0.12, 2.72, 0.12, materials.gatePanel, 0, 1.53, 0);
addBox(leaf, 0.12, 3.2, 0.24, materials.gate, -leafWidth / 2 + 0.06, 1.58, 0);
addBox(leaf, 0.12, 3.2, 0.24, materials.gate, leafWidth / 2 - 0.06, 1.58, 0);
addBox(leaf, leafWidth, 0.13, 0.24, materials.gate, 0, 0.08, 0);
addBox(leaf, leafWidth, 0.13, 0.24, materials.gate, 0, 3.08, 0);
for (const x of [-0.5, 0, 0.5]) {
addBox(leaf, 0.07, 2.9, 0.18, materials.gate, x, 1.58, 0.02);
}
for (const x of [-0.62, 0.62]) {
const stripe = addBox(leaf, 0.16, 2.15, 0.17, materials.orange, x, 1.55, 0.13);
stripe.rotation.z = -0.38;
}
return leaf;
}
const leftLeaf = createGateLeaf();
const rightLeaf = createGateLeaf();
gateGroup.add(leftLeaf, rightLeaf);
// Signal lamps.
const signalMaterial = standardMaterial(0xef5f5f, {
emissive: 0xef5f5f,
emissiveIntensity: 1.8,
roughness: 0.35
});
for (const x of [-2.72, 2.72]) {
const bulb = new THREE.Mesh(new THREE.SphereGeometry(0.16, 10, 7), signalMaterial);
bulb.position.set(x, 3.58, 0.56);
bulb.castShadow = false;
gateGroup.add(bulb);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.2, 0.045, 6, 12),
materials.dark
);
rim.position.set(x, 3.58, 0.52);
gateGroup.add(rim);
}
const gateGlow = new THREE.PointLight(0xef5f5f, 1.2, 7, 2);
gateGlow.position.set(0, 3.4, 0.8);
scene.add(gateGlow);
// Sign texture made entirely in the page.
function createSignTexture() {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const context = canvas.getContext("2d");
context.fillStyle = "#173138";
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = "#f4a34d";
context.lineWidth = 12;
context.strokeRect(12, 12, canvas.width - 24, canvas.height - 24);
context.textAlign = "center";
context.fillStyle = "#f6f3dc";
context.font = "900 78px system-ui, sans-serif";
context.fillText("CYCLE GATE 01", 512, 112);
context.fillStyle = "#87aaa9";
context.font = "700 30px system-ui, sans-serif";
context.fillText("WAIT FOR GREEN • CROSS BEFORE CLOSING", 512, 181);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(3.15, 0.79),
new THREE.MeshBasicMaterial({ map: createSignTexture(), side: THREE.DoubleSide })
);
sign.position.set(0, 4.45, 0.47);
gateGroup.add(sign);
// Far-side landing marker.
const goalGroup = new THREE.Group();
goalGroup.position.z = -7.35;
scene.add(goalGroup);
const goalPad = new THREE.Mesh(
new THREE.CylinderGeometry(1.12, 1.12, 0.055, 32),
standardMaterial(0x43b993, {
emissive: 0x1e6d58,
emissiveIntensity: 0.38,
transparent: true,
opacity: 0.72
})
);
goalPad.position.y = 0.035;
goalPad.receiveShadow = true;
goalGroup.add(goalPad);
const goalRing = new THREE.Mesh(
new THREE.RingGeometry(0.8, 0.95, 32),
new THREE.MeshBasicMaterial({
color: 0x9cf6d3,
transparent: true,
opacity: 0.8,
side: THREE.DoubleSide
})
);
goalRing.rotation.x = -Math.PI / 2;
goalRing.position.y = 0.075;
goalGroup.add(goalRing);
for (const x of [-0.92, 0.92]) {
addBox(goalGroup, 0.08, 1.35, 0.08, materials.teal, x, 0.68, 0);
}
addBox(goalGroup, 1.92, 0.09, 0.1, materials.teal, 0, 1.32, 0);
// Player proxy.
const player = new THREE.Group();
scene.add(player);
const playerBody = addCylinder(player, 0.3, 0.38, 0.72, 6, materials.player, 0, 0.64, 0);
const playerHead = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.255, 0),
materials.visor
);
playerHead.position.y = 1.18;
playerHead.castShadow = true;
player.add(playerHead);
addBox(player, 0.32, 0.15, 0.09, materials.dark, 0, 1.2, 0.22);
addBox(player, 0.16, 0.32, 0.18, materials.playerDark, -0.18, 0.2, 0);
addBox(player, 0.16, 0.32, 0.18, materials.playerDark, 0.18, 0.2, 0);
addBox(player, 0.44, 0.09, 0.22, materials.cream, 0, 0.73, 0.27);
const playerShadow = new THREE.Mesh(
new THREE.CircleGeometry(0.43, 20),
new THREE.MeshBasicMaterial({
color: 0x203d3f,
transparent: true,
opacity: 0.22,
depthWrite: false
})
);
playerShadow.rotation.x = -Math.PI / 2;
playerShadow.position.y = 0.035;
scene.add(playerShadow);
// Low-poly landscape.
let randomSeed = 1729;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
for (let i = 0; i < 18; i++) {
const side = i % 2 === 0 ? -1 : 1;
const x = side * (8 + random() * 24);
const z = -32 + random() * 62;
const radius = 1.1 + random() * 3.4;
const height = 2 + random() * 6;
const mountain = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, 5 + Math.floor(random() * 3)),
i % 3 === 0 ? materials.rockDark : materials.rock
);
mountain.position.set(x, -3.45 + height / 2, z);
mountain.rotation.y = random() * Math.PI;
mountain.castShadow = true;
mountain.receiveShadow = true;
scene.add(mountain);
}
for (let i = 0; i < 30; i++) {
const side = i % 2 === 0 ? -1 : 1;
const x = side * (6.5 + random() * 14);
const z = -27 + random() * 54;
const rock = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.35 + random() * 0.8, 0),
i % 4 === 0 ? materials.rockDark : materials.rock
);
rock.position.set(x, -3.05, z);
rock.scale.y = 0.65 + random() * 0.55;
rock.rotation.set(random(), random(), random());
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
for (let i = 0; i < 17; i++) {
const side = i % 2 === 0 ? -1 : 1;
const x = side * (5.8 + random() * 8);
const z = -26 + random() * 52;
const height = 1.7 + random() * 2.5;
addCylinder(
scene,
0.1,
0.14,
height,
5,
materials.foliageDark,
x,
-3.4 + height / 2,
z
);
const crown = new THREE.Mesh(
new THREE.ConeGeometry(0.75 + random() * 0.5, height * 0.8, 6),
i % 3 === 0 ? materials.foliageDark : materials.foliage
);
crown.position.set(x, -3.4 + height * 0.86, z);
crown.rotation.y = random() * Math.PI;
crown.castShadow = true;
scene.add(crown);
}
const clouds = [];
for (let i = 0; i < 8; i++) {
const cloud = new THREE.Group();
const puffCount = 3 + Math.floor(random() * 3);
for (let j = 0; j < puffCount; j++) {
const puff = new THREE.Mesh(
new THREE.DodecahedronGeometry(1.2 + random() * 0.8, 0),
materials.cloud
);
puff.position.set(j * 1.35, random() * 0.3, random() * 0.5);
puff.scale.y = 0.55;
cloud.add(puff);
}
cloud.position.set(-26 + random() * 52, 12 + random() * 9, -35 + random() * 50);
scene.add(cloud);
clouds.push(cloud);
}
// Cycle configuration.
const PHASES = {
closed: { duration: 3.2, next: "opening", label: "LOCKED", color: "#ef6a67" },
opening: { duration: 1.4, next: "open", label: "OPENING", color: "#f5b84b" },
open: { duration: 3.8, next: "closing", label: "OPEN", color: "#47c99a" },
closing: { duration: 1.2, next: "closed", label: "CLOSING", color: "#f08b55" }
};
const phaseOrder = ["closed", "opening", "open", "closing"];
const totalCycle = phaseOrder.reduce((sum, key) => sum + PHASES[key].duration, 0);
const playerSpeed = 2.9;
const playerRadius = 0.29;
const gateHalfWidth = 2.1;
let phase = "closed";
let phaseTime = 0;
let cycleTime = 0;
let gateOpenness = 0;
let attemptActive = false;
let crossed = false;
let attempts = 0;
let toastTimeout;
const velocity = new THREE.Vector3();
const desiredVelocity = new THREE.Vector3();
const keys = new Set();
function smooth01(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function showToast(message, type = "") {
clearTimeout(toastTimeout);
toast.textContent = message;
toast.className = `show ${type}`.trim();
toastTimeout = setTimeout(() => {
toast.className = "";
}, 3300);
}
function resetPlayer() {
player.position.set(0, 0, 6.75);
player.rotation.y = Math.PI;
velocity.set(0, 0, 0);
attemptActive = false;
crossed = false;
}
function resetRun() {
phase = "closed";
phaseTime = 0;
cycleTime = 0;
gateOpenness = 0;
attempts = 0;
result.classList.remove("show");
resetPlayer();
showToast("Cycle restarted. Wait for the green OPEN signal.");
}
function failAttempt() {
resetPlayer();
showToast("The gate closed before you cleared it. Reset to the waiting pad.", "warning");
}
function completeCrossing() {
if (crossed) return;
crossed = true;
attemptActive = false;
attempts += 1;
result.classList.add("show");
showToast("Safe crossing confirmed.", "success");
}
function setPhase(nextPhase) {
const previousPhase = phase;
phase = nextPhase;
phaseTime = 0;
if (previousPhase === "open" && nextPhase === "closing" && attemptActive && !crossed) {
failAttempt();
}
}
function updateCycle(dt) {
phaseTime += dt;
cycleTime += dt;
if (cycleTime >= totalCycle) cycleTime -= totalCycle;
const duration = PHASES[phase].duration;
if (phaseTime >= duration) {
phaseTime -= duration;
setPhase(PHASES[phase].next);
}
if (phase === "closed") {
gateOpenness = 0;
} else if (phase === "opening") {
gateOpenness = smooth01(phaseTime / PHASES.opening.duration);
} else if (phase === "open") {
gateOpenness = 1;
} else {
gateOpenness = 1 - smooth01(phaseTime / PHASES.closing.duration);
}
}
function gateBlocks(x, z) {
if (Math.abs(z) > 0.43) return false;
const clearHalfWidth = gateOpenness * gateHalfWidth;
return Math.abs(x) + playerRadius > clearHalfWidth;
}
function updatePlayer(dt, elapsed) {
let inputX = 0;
let inputZ = 0;
if (keys.has("KeyA") || keys.has("ArrowLeft")) inputX -= 1;
if (keys.has("KeyD") || keys.has("ArrowRight")) inputX += 1;
if (keys.has("KeyW") || keys.has("ArrowUp")) inputZ -= 1;
if (keys.has("KeyS") || keys.has("ArrowDown")) inputZ += 1;
desiredVelocity.set(inputX, 0, inputZ);
if (desiredVelocity.lengthSq() > 1) desiredVelocity.normalize();
desiredVelocity.multiplyScalar(playerSpeed);
velocity.lerp(desiredVelocity, 1 - Math.exp(-11 * dt));
const oldX = player.position.x;
const oldZ = player.position.z;
let nextX = THREE.MathUtils.clamp(oldX + velocity.x * dt, -1.92, 1.92);
let nextZ = THREE.MathUtils.clamp(oldZ + velocity.z * dt, -9.25, 9.25);
if (!gateBlocks(nextX, nextZ)) {
player.position.x = nextX;
player.position.z = nextZ;
} else {
if (!gateBlocks(nextX, oldZ)) player.position.x = nextX;
if (!gateBlocks(oldX, nextZ)) player.position.z = nextZ;
}
const speed = Math.hypot(velocity.x, velocity.z);
if (speed > 0.08) {
const targetYaw = Math.atan2(velocity.x, velocity.z);
let yawDelta = targetYaw - player.rotation.y;
yawDelta = Math.atan2(Math.sin(yawDelta), Math.cos(yawDelta));
player.rotation.y += yawDelta * (1 - Math.exp(-12 * dt));
}
player.position.y = speed > 0.12 ? Math.abs(Math.sin(elapsed * 9)) * 0.045 : 0;
playerShadow.position.x = player.position.x;
playerShadow.position.z = player.position.z;
if (!attemptActive && !crossed && gateOpenness >= 0.74 &&
player.position.z < 1.25 && player.position.z > -1.25) {
attemptActive = true;
}
if (attemptActive && player.position.z <= -1.05) {
completeCrossing();
} else if (attemptActive && player.position.z > 1.8) {
attemptActive = false;
}
}
// Directly implemented orbit and zoom controls; no addon modules are used.
const orbit = {
theta: 0.36,
elevation: 0.69,
radius: 16.4
};
const cameraTarget = new THREE.Vector3(0, 0, 1.25);
const desiredTarget = new THREE.Vector3();
const desiredCameraPosition = new THREE.Vector3();
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.elevation = THREE.MathUtils.clamp(orbit.elevation + dy * 0.0045, 0.35, 1.08);
});
function stopDragging(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(orbit.radius + event.deltaY * 0.012, 10, 24);
},
{ passive: false }
);
const movementCodes = new Set([
"KeyW", "KeyA", "KeyS", "KeyD",
"ArrowUp", "ArrowLeft", "ArrowDown", "ArrowRight"
]);
window.addEventListener("keydown", (event) => {
if (movementCodes.has(event.code)) {
event.preventDefault();
keys.add(event.code);
}
if (event.code === "KeyR") resetRun();
});
window.addEventListener("keyup", (event) => {
keys.delete(event.code);
});
window.addEventListener("blur", () => keys.clear());
document.querySelectorAll("#dpad button").forEach((button) => {
const code = button.dataset.code;
button.addEventListener("pointerdown", (event) => {
event.preventDefault();
keys.add(code);
button.setPointerCapture(event.pointerId);
});
const release = (event) => {
keys.delete(code);
if (button.hasPointerCapture(event.pointerId)) {
button.releasePointerCapture(event.pointerId);
}
};
button.addEventListener("pointerup", release);
button.addEventListener("pointercancel", release);
});
document.getElementById("reset-button").addEventListener("click", resetRun);
document.getElementById("again-button").addEventListener("click", resetRun);
function updateHUD() {
const info = PHASES[phase];
const remaining = Math.max(0, info.duration - phaseTime);
const progress = cycleTime / totalCycle;
const color = info.color;
document.documentElement.style.setProperty("--accent", color);
phaseName.textContent = info.label;
phaseName.style.color = color;
dial.style.background =
`conic-gradient(${color} ${progress * 360}deg, rgba(255,255,255,0.10) 0deg)`;
dialValue.textContent = remaining.toFixed(1);
if (phase === "closed") {
dialCaption.textContent = "to opening";
phaseHint.textContent = `Gate opens in ${remaining.toFixed(1)}s`;
} else if (phase === "opening") {
dialCaption.textContent = "to open";
phaseHint.textContent = `Fully open in ${remaining.toFixed(1)}s`;
} else if (phase === "open") {
dialCaption.textContent = "open left";
phaseHint.textContent = `${remaining.toFixed(1)}s left to cross safely`;
} else {
dialCaption.textContent = "to reset";
phaseHint.textContent = `Next cycle in ${remaining.toFixed(1)}s`;
}
cycleReadout.textContent = `${cycleTime.toFixed(1)} / ${totalCycle.toFixed(1)}s`;
const activeIndex = phaseOrder.indexOf(phase);
segmentElements.forEach((segment, index) => {
segment.classList.toggle("current", index === activeIndex);
segment.classList.toggle("done", index < activeIndex);
});
}
function updateVisuals(elapsed) {
leftLeaf.position.x = -(1.05 + gateOpenness * 2.15);
rightLeaf.position.x = 1.05 + gateOpenness * 2.15;
const phaseColor = new THREE.Color(PHASES[phase].color);
signalMaterial.color.copy(phaseColor);
signalMaterial.emissive.copy(phaseColor);
gateGlow.color.copy(phaseColor);
let intensity = 1.7;
if (phase === "opening") {
intensity = 1.05 + Math.max(0, Math.sin(elapsed * 13)) * 1.5;
} else if (phase === "closing") {
intensity = 1.1 + Math.max(0, Math.sin(elapsed * 17)) * 1.15;
}
signalMaterial.emissiveIntensity = intensity;
gateGlow.intensity = phase === "open" ? 1.65 : 1.05;
const pulse = 1 + Math.sin(elapsed * 2.8) * 0.045;
goalRing.scale.setScalar(pulse);
goalRing.material.opacity = 0.65 + Math.sin(elapsed * 2.8) * 0.14;
for (let i = 0; i < clouds.length; i++) {
const cloud = clouds[i];
cloud.position.x += 0.11 * (1 + i * 0.03);
if (cloud.position.x > 31) cloud.position.x = -31;
}
}
function updateCamera(dt) {
desiredTarget.set(player.position.x * 0.26, 0.05, player.position.z * 0.28 - 0.55);
cameraTarget.lerp(desiredTarget, 1 - Math.exp(-3.5 * dt));
const horizontalRadius = Math.cos(orbit.elevation) * orbit.radius;
desiredCameraPosition.set(
cameraTarget.x + Math.sin(orbit.theta) * horizontalRadius,
cameraTarget.y + Math.sin(orbit.elevation) * orbit.radius,
cameraTarget.z + Math.cos(orbit.theta) * horizontalRadius
);
camera.position.lerp(desiredCameraPosition, 1 - Math.exp(-5 * dt));
camera.lookAt(cameraTarget);
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", onResize);
resetPlayer();
player.rotation.y = Math.PI;
showToast("Wait for green. The 3.8s open window is long enough for a ~2.7s crossing.");
const clock = new THREE.Clock();
let elapsed = 0;
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
updateCycle(dt);
updatePlayer(dt, elapsed);
updateVisuals(elapsed);
updateCamera(dt);
updateHUD();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```