Prompt specification
Build or customize the Three.js 3D scene "Cyclist turning through a chamfered intersection" 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: Cyclist turning through a chamfered intersection
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/cyclist-turning-through-a-chamfered-intersection-x2810
Scene Description Prompt:
Use Three.js from cdnjs to create a compact Eixample vignette with crosswalk, cycle path, and turning cyclist. Keep the route geometrically aligned with the intersection and separate from the pedestrian waiting area. Deliver one self-contained HTML file with visible voxel blocks.
Working Three.js Starter (cyclist-turning-through-a-chamfered-intersection-x2810):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Eixample Junction — Voxel Vignette</title>
<style>
:root {
color-scheme: light;
--ink: #263238;
--paper: rgba(250, 247, 238, 0.92);
--red: #b94436;
--yellow: #e8b94b;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cbdde3;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(330px, calc(100vw - 36px));
padding: 15px 16px 14px;
border: 1px solid rgba(38, 50, 56, 0.15);
border-radius: 13px;
background: var(--paper);
box-shadow: 0 12px 30px rgba(44, 56, 60, 0.13);
backdrop-filter: blur(8px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 4px;
color: #718087;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 22px;
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 7px 0 13px;
color: #65747a;
font-size: 12px;
line-height: 1.45;
}
.legend {
display: grid;
gap: 7px;
padding-top: 11px;
border-top: 1px solid rgba(38, 50, 56, 0.12);
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
font-size: 11px;
font-weight: 650;
}
.swatch {
width: 22px;
height: 8px;
flex: 0 0 auto;
border-radius: 2px;
}
.swatch.cycle { background: var(--red); }
.swatch.crossing { background: #f4f1e7; border: 1px solid #c9c4b8; }
.swatch.waiting { background: var(--yellow); }
.note {
margin-top: 11px;
color: #68777c;
font-size: 10px;
line-height: 1.4;
}
.controls {
position: fixed;
z-index: 10;
right: 18px;
bottom: 18px;
display: flex;
gap: 7px;
}
button {
border: 1px solid rgba(38, 50, 56, 0.16);
border-radius: 9px;
padding: 9px 12px;
background: var(--paper);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
box-shadow: 0 6px 16px rgba(44, 56, 60, 0.1);
backdrop-filter: blur(8px);
}
button:hover { background: #fffdf7; }
.help {
position: fixed;
z-index: 9;
left: 18px;
bottom: 18px;
padding: 9px 11px;
border-radius: 9px;
background: rgba(250, 247, 238, 0.84);
color: #65747a;
font-size: 10px;
font-weight: 650;
pointer-events: none;
}
.compass {
position: fixed;
z-index: 9;
top: 20px;
right: 20px;
display: grid;
place-items: center;
width: 42px;
height: 42px;
border: 1px solid rgba(38, 50, 56, 0.15);
border-radius: 50%;
background: var(--paper);
color: var(--ink);
font-size: 10px;
font-weight: 900;
box-shadow: 0 8px 20px rgba(44, 56, 60, 0.1);
pointer-events: none;
}
.compass::after {
content: "";
width: 0;
height: 0;
margin-top: -2px;
border-right: 4px solid transparent;
border-bottom: 10px solid var(--red);
border-left: 4px solid transparent;
}
.compass span { margin-top: -7px; }
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #cbdde3;
color: #526269;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
@media (max-width: 620px) {
.panel {
top: 10px;
left: 10px;
width: 250px;
padding: 12px;
}
h1 { font-size: 18px; }
.subtitle { margin-bottom: 9px; }
.note, .help { display: none; }
.controls { right: 10px; bottom: 10px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel model of an Eixample junction"></canvas>
<section class="panel">
<div class="eyebrow">Barcelona · 40.7128° N</div>
<h1>Eixample Junction</h1>
<p class="subtitle">
Chamfered corner blocks, an offset crossing, and a tangent-aligned protected cycle turn.
</p>
<div class="legend">
<div class="legend-row">
<span class="swatch cycle"></span>
Continuous cycle route and turning arc
</div>
<div class="legend-row">
<span class="swatch crossing"></span>
Zebra crossing on the north arm
</div>
<div class="legend-row">
<span class="swatch waiting"></span>
Pedestrian waiting and tactile zone
</div>
</div>
<div class="note">
The route occupies the south and east arms; the pedestrian waiting area is placed on the
opposite, north-west arm.
</div>
</section>
<div class="compass"><span>N</span></div>
<div class="help">Drag to orbit · Shift-drag to pan · Wheel to zoom</div>
<div class="controls">
<button id="reset" type="button">Reset view</button>
<button id="top" type="button">Plan view</button>
</div>
<div id="loading">Building Eixample…</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 camera
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcbdde3);
scene.fog = new THREE.Fog(0xcbdde3, 40, 82);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
140
);
const orbit = {
radius: 31,
theta: 0.69,
phi: 0.83,
target: new THREE.Vector3(0, 0.45, 0)
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
updateCamera();
// Custom pointer controls: no addons are used.
let dragging = false;
let lastX = 0;
let lastY = 0;
let panning = false;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
dragging = true;
panning = event.shiftKey || event.button === 2;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (panning) {
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0)
.setY(0)
.normalize();
const forward = camera.getWorldDirection(new THREE.Vector3());
forward.y = 0;
forward.normalize();
const scale = orbit.radius * 0.00125;
orbit.target.addScaledVector(right, -dx * scale);
orbit.target.addScaledVector(forward, -dy * scale);
} else {
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.005, 0.13, 1.43);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", event => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
13,
58
);
updateCamera();
}, { passive: false });
document.querySelector("#reset").addEventListener("click", () => {
orbit.radius = 31;
orbit.theta = 0.69;
orbit.phi = 0.83;
orbit.target.set(0, 0.45, 0);
updateCamera();
});
document.querySelector("#top").addEventListener("click", () => {
orbit.radius = 34;
orbit.theta = 0;
orbit.phi = 0.14;
orbit.target.set(0, 0, 0);
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Lighting and reusable geometry
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xe8f5f7, 0x8b7964, 2.15));
const sun = new THREE.DirectionalLight(0xfff0d3, 3.25);
sun.position.set(-13, 22, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 24;
sun.shadow.camera.bottom = -24;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 60;
sun.shadow.bias = -0.00035;
scene.add(sun);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function material(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...extra
});
}
const mats = {
ground: material(0xb7aa96),
asphalt: material(0x424b50),
sidewalk: material(0xd7cbb8),
curb: material(0xe4dac8),
paving: material(0xffffff),
cycle: material(0xb74435),
white: material(0xf4f1e8),
yellow: material(0xe8b94b),
dark: material(0x253035),
steel: material(0x596568, { metalness: 0.35, roughness: 0.5 }),
trunk: material(0x74503a),
leaves: material(0x66845a),
leavesLight: material(0x829866),
glass: material(0x37565e, { roughness: 0.3 }),
skin: material(0xc98e6f),
jacket: material(0x2f6970),
trousers: material(0x36404a)
};
function cube(w, h, d, x, y, z, mat, parent = scene, castShadow = true) {
const mesh = new THREE.Mesh(unitBox, mat);
mesh.scale.set(w, h, d);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(a, b, radius, mat, parent = scene, segments = 8) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius, radius, length, segments, 1, false
);
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function groundBeam(a, b, width, height, y, mat) {
const dx = b[0] - a[0];
const dz = b[1] - a[1];
const length = Math.hypot(dx, dz);
const beam = cube(
length, height, width,
(a[0] + b[0]) / 2,
y,
(a[1] + b[1]) / 2,
mat
);
beam.rotation.y = -Math.atan2(dz, dx);
return beam;
}
// A convex polygon extruded into a block or sidewalk.
function polygonPrism(points, yBottom, yTop, mat) {
const p = points.map(([x, z]) => new THREE.Vector2(x, z));
let area = 0;
for (let i = 0; i < p.length; i++) {
const next = p[(i + 1) % p.length];
area += p[i].x * next.y - next.x * p[i].y;
}
// Ensure the top winding gives an upward normal.
if (area > 0) p.reverse();
const positions = [];
function triangle(a, b, c, y) {
positions.push(a.x, y, a.y, b.x, y, b.y, c.x, y, c.y);
}
for (let i = 1; i < p.length - 1; i++) {
triangle(p[0], p[i], p[i + 1], yTop);
triangle(p[0], p[i + 1], p[i], yBottom);
}
for (let i = 0; i < p.length; i++) {
const j = (i + 1) % p.length;
const a = p[i];
const b = p[j];
positions.push(
a.x, yBottom, a.y,
b.x, yBottom, b.y,
a.x, yTop, a.y,
b.x, yBottom, b.y,
b.x, yTop, b.y,
a.x, yTop, a.y
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, mat);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Ground, Eixample streets, and chamfered corner blocks
// -------------------------------------------------------------------------
cube(48, 0.35, 48, 0, -0.28, 0, mats.ground, scene, false);
// Two eight-metre-wide carriageways crossing at the origin.
cube(40, 0.14, 8, 0, 0, 0, mats.asphalt, scene, false);
cube(8, 0.14, 40, 0, 0, 0, mats.asphalt, scene, false);
const positiveCorner = [
[4.25, 5.75],
[5.75, 4.25],
[13.5, 4.25],
[13.5, 13.5],
[4.25, 13.5]
];
const quadrants = [
[1, 1], [-1, 1], [-1, -1], [1, -1]
];
for (const [sx, sz] of quadrants) {
const polygon = positiveCorner.map(([x, z]) => [x * sx, z * sz]);
polygonPrism(polygon, 0.02, 0.30, mats.sidewalk);
for (let i = 0; i < polygon.length; i++) {
const a = polygon[i];
const b = polygon[(i + 1) % polygon.length];
groundBeam(a, b, 0.13, 0.10, 0.31, mats.curb);
}
}
// Instanced paving creates a legible, block-scale grid.
const pavingTransforms = [];
for (const [sx, sz] of quadrants) {
for (let x = 4.8; x <= 13.1; x += 1.08) {
for (let z = 4.8; z <= 13.1; z += 1.08) {
if (x + z < 10.95) continue;
pavingTransforms.push({
x: x * sx,
z: z * sz,
shade: (Math.round(x * 10) + Math.round(z * 10)) % 3
});
}
}
}
const pavingMesh = new THREE.InstancedMesh(
unitBox,
mats.paving,
pavingTransforms.length
);
const dummy = new THREE.Object3D();
const pavingColors = [0xd5c8b5, 0xdfd3c1, 0xcdc1ae];
pavingTransforms.forEach((tile, index) => {
dummy.position.set(tile.x, 0.315, tile.z);
dummy.scale.set(0.96, 0.025, 0.96);
dummy.updateMatrix();
pavingMesh.setMatrixAt(index, dummy.matrix);
pavingMesh.setColorAt(index, new THREE.Color(pavingColors[tile.shade]));
});
pavingMesh.instanceMatrix.needsUpdate = true;
if (pavingMesh.instanceColor) pavingMesh.instanceColor.needsUpdate = true;
pavingMesh.receiveShadow = true;
scene.add(pavingMesh);
// -------------------------------------------------------------------------
// Voxel buildings and instanced window details
// -------------------------------------------------------------------------
const facadeMaterials = [
material(0xd5bea0),
material(0xc98d72),
material(0xd1ae69),
material(0xd9d3c2),
material(0xb96c58),
material(0xc5b49a)
];
const roofMaterial = material(0xaaa293);
const windowMaterial = material(0x29464f, { roughness: 0.42 });
const windowInstances = [];
const buildingLayout = [
[6.30, 11.70, 2.60, 2.50, 3.20],
[9.00, 11.70, 2.30, 2.50, 2.55],
[11.70, 11.70, 2.50, 2.50, 3.65],
[11.70, 6.35, 2.50, 2.40, 2.80],
[11.70, 9.00, 2.50, 2.40, 3.15],
[6.55, 6.55, 1.70, 1.70, 2.40],
[5.40, 8.45, 1.60, 3.20, 2.25],
[7.85, 8.65, 2.00, 2.35, 2.65]
];
function addWindow(x, y, z, sx, sy, sz) {
windowInstances.push({ x, y, z, sx, sy, sz });
}
function addBuilding(localX, localZ, w, d, h, sx, sz, index) {
const x = localX * sx;
const z = localZ * sz;
const base = 0.33;
const facade = facadeMaterials[index % facadeMaterials.length];
cube(w, h, d, x, base + h / 2, z, facade);
cube(w + 0.18, 0.16, d + 0.18, x, base + h + 0.02, z, roofMaterial);
cube(0.72, 0.46, 0.72, x + 0.23, base + h + 0.31, z - 0.18, roofMaterial);
const floors = Math.max(1, Math.floor((h - 0.20) / 0.80));
for (let row = 0; row < floors; row++) {
const y = base + 0.58 + row * 0.79;
const columnsX = Math.max(1, Math.floor(w / 0.88));
for (let col = 0; col < columnsX; col++) {
const localWindowX =
localX - w / 2 + ((col + 1) * w) / (columnsX + 1);
addWindow(
localWindowX * sx,
y,
(localZ - d / 2 - 0.035) * sz,
0.43,
0.49,
0.045
);
}
const columnsZ = Math.max(1, Math.floor(d / 0.88));
for (let col = 0; col < columnsZ; col++) {
const localWindowZ =
localZ - d / 2 + ((col + 1) * d) / (columnsZ + 1);
addWindow(
(localX - w / 2 - 0.035) * sx,
y,
localWindowZ * sz,
0.045,
0.49,
0.43
);
}
// A shallow balcony on selected rows.
if (row === 1 && index % 2 === 0) {
cube(
w * 0.46, 0.11, 0.48,
x,
y - 0.30,
(localZ - d / 2 - 0.22) * sz,
roofMaterial
);
cube(
w * 0.46, 0.36, 0.045,
x,
y - 0.12,
(localZ - d / 2 - 0.45) * sz,
mats.steel
);
}
}
// Ground-floor awning and doorway.
const awningMaterial = facadeMaterials[(index + 3) % facadeMaterials.length];
cube(
w * 0.68, 0.12, 0.58,
x,
base + 0.96,
(localZ - d / 2 - 0.22) * sz,
awningMaterial
);
cube(
0.045, 0.72, 0.58,
(localX - w / 2 - 0.035) * sx,
base + 0.36,
z,
mats.dark,
scene,
false
);
}
let buildingIndex = 0;
for (const [sx, sz] of quadrants) {
for (const building of buildingLayout) {
addBuilding(...building, sx, sz, buildingIndex++);
}
}
const windowMesh = new THREE.InstancedMesh(
unitBox,
windowMaterial,
windowInstances.length
);
windowInstances.forEach((window, index) => {
dummy.position.set(window.x, window.y, window.z);
dummy.scale.set(window.sx, window.sy, window.sz);
dummy.updateMatrix();
windowMesh.setMatrixAt(index, dummy.matrix);
});
windowMesh.instanceMatrix.needsUpdate = true;
scene.add(windowMesh);
// -------------------------------------------------------------------------
// Trees, lamps, and compact street details
// -------------------------------------------------------------------------
function voxelTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.30, z);
group.scale.setScalar(scale);
scene.add(group);
cube(0.24, 1.05, 0.24, 0, 0.52, 0, mats.trunk, group);
cube(0.88, 0.76, 0.88, 0, 1.32, 0, mats.leaves, group);
cube(0.66, 0.70, 0.68, -0.42, 1.46, 0.08, mats.leavesLight, group);
cube(0.65, 0.72, 0.66, 0.40, 1.52, -0.10, mats.leaves, group);
cube(0.58, 0.58, 0.62, 0.02, 1.88, 0.04, mats.leavesLight, group);
}
function streetLamp(x, z, inwardX = 1, inwardZ = 0) {
cylinderBetween([x, 0.30, z], [x, 2.75, z], 0.055, mats.steel);
cube(
0.70, 0.08, 0.08,
x + inwardX * 0.30,
2.72,
z + inwardZ * 0.30,
mats.steel
);
cube(
0.24, 0.12, 0.18,
x + inwardX * 0.63,
2.64,
z + inwardZ * 0.63,
mats.dark
);
}
for (const [sx, sz] of quadrants) {
voxelTree(9.65 * sx, 9.65 * sz, 0.88 + 0.04 * (sx + sz));
voxelTree(8.55 * sx, 6.85 * sz, 0.78);
streetLamp(4.75 * sx, 9.85 * sz, -sx, 0);
streetLamp(9.85 * sx, 4.75 * sz, 0, -sz);
}
// -------------------------------------------------------------------------
// North-arm crosswalk and separated pedestrian waiting area
// -------------------------------------------------------------------------
const crossingZ = 5.50;
for (let x = -3.28; x <= 3.30; x += 0.82) {
cube(
0.47, 0.045, 2.20,
x, 0.105, crossingZ,
mats.white,
scene,
false
);
}
// A stop line behind the crossing.
cube(
7.5, 0.045, 0.13,
0, 0.105, 7.00,
mats.white,
scene,
false
);
// Tactile waiting zones at the west and east curb.
cube(
0.76, 0.065, 2.55,
-4.72, 0.355, crossingZ,
mats.yellow
);
cube(
0.76, 0.065, 2.55,
4.72, 0.355, crossingZ,
mats.yellow
);
for (let z = crossingZ - 1.05; z <= crossingZ + 1.05; z += 0.22) {
cube(
0.42, 0.018, 0.035,
-4.72, 0.397, z,
mats.dark,
scene,
false
);
}
// Small curb bollards emphasize the waiting pocket.
for (const z of [crossingZ - 1.32, crossingZ + 1.32]) {
cylinderBetween(
[-5.20, 0.30, z],
[-5.20, 0.98, z],
0.055,
mats.steel
);
}
function voxelPerson(x, z, shirtMaterial, scale = 1, heading = 0) {
const group = new THREE.Group();
group.position.set(x, 0.39, z);
group.rotation.y = heading;
group.scale.setScalar(scale);
scene.add(group);
cube(0.13, 0.43, 0.15, -0.10, 0.23, 0, mats.trousers, group);
cube(0.13, 0.43, 0.15, 0.10, 0.23, 0, mats.trousers, group);
cube(0.38, 0.55, 0.25, 0, 0.70, 0, shirtMaterial, group);
cube(0.12, 0.43, 0.15, -0.27, 0.67, 0, shirtMaterial, group);
cube(0.12, 0.43, 0.15, 0.27, 0.67, 0, shirtMaterial, group);
cube(0.28, 0.29, 0.27, 0, 1.10, 0, mats.skin, group);
cube(0.30, 0.10, 0.29, 0, 1.27, 0, mats.dark, group);
}
voxelPerson(-4.88, 5.05, mats.jacket, 0.94, -0.25);
voxelPerson(-4.93, 6.10, material(0xb46e55), 0.87, 0.55);
// -------------------------------------------------------------------------
// Exact cycle-route geometry
//
// South approach: x = 2.5, travelling north.
// Turning arc: radius 4, centre (6.5, -4), tangent to both legs.
// East exit: z = 0, travelling east.
// -------------------------------------------------------------------------
class JunctionCycleRoute extends THREE.Curve {
constructor() {
super();
this.approachLength = 9;
this.arcLength = Math.PI * 2;
this.exitLength = 6.5;
this.totalLength = this.approachLength + this.arcLength + this.exitLength;
}
getPoint(t, target = new THREE.Vector3()) {
const distance = THREE.MathUtils.clamp(t, 0, 1) * this.totalLength;
if (distance <= this.approachLength) {
return target.set(2.5, 0, -13 + distance);
}
if (distance <= this.approachLength + this.arcLength) {
const arcDistance = distance - this.approachLength;
const theta = Math.PI - arcDistance / 4;
return target.set(
6.5 + Math.cos(theta) * 4,
0,
-4 + Math.sin(theta) * 4
);
}
return target.set(
6.5 + distance - this.approachLength - this.arcLength,
0,
0
);
}
getTangent(t, target = new THREE.Vector3()) {
const a = this.getPoint(Math.max(0, t - 0.0005), new THREE.Vector3());
const b = this.getPoint(Math.min(1, t + 0.0005), new THREE.Vector3());
return target.copy(b).sub(a).normalize();
}
}
const route = new JunctionCycleRoute();
const routeWidth = 1.28;
function createRouteRibbon(curve, width) {
const positions = [];
const indices = [];
const segments = 160;
for (let i = 0; i <= segments; i++) {
const t = i / segments;
const point = curve.getPoint(t, new THREE.Vector3());
const tangent = curve.getTangent(t, new THREE.Vector3());
const perpendicular = new THREE.Vector3(
-tangent.z,
0,
tangent.x
);
const left = point.clone().addScaledVector(perpendicular, width / 2);
const right = point.clone().addScaledVector(perpendicular, -width / 2);
positions.push(left.x, 0.105, left.z);
positions.push(right.x, 0.105, right.z);
}
for (let i = 0; i < segments; i++) {
const a = i * 2;
indices.push(a, a + 2, a + 1);
indices.push(a + 1, a + 2, a + 3);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(
geometry,
material(0xb74435, { side: THREE.DoubleSide })
);
mesh.receiveShadow = true;
scene.add(mesh);
}
createRouteRibbon(route, routeWidth);
// Fine white edge markings make the path's alignment legible.
for (let distance = 0.65; distance < route.totalLength; distance += 1.24) {
const t = distance / route.totalLength;
const point = route.getPoint(t, new THREE.Vector3());
const tangent = route.getTangent(t, new THREE.Vector3());
const perpendicular = new THREE.Vector3(-tangent.z, 0, tangent.x);
for (const side of [-1, 1]) {
const mark = point.clone().addScaledVector(
perpendicular,
side * (routeWidth / 2 - 0.075)
);
const dash = cube(
0.48, 0.025, 0.065,
mark.x,
0.122,
mark.z,
mats.white,
scene,
false
);
dash.rotation.y = -Math.atan2(tangent.z, tangent.x);
}
}
// Low separators along the protected south approach.
for (let z = -12.6; z <= -5.4; z += 1.18) {
cube(
0.14, 0.17, 0.72,
3.31,
0.155,
z,
mats.curb
);
}
// Flat directional arrows.
function groundArrow(x, z, rotationY) {
const group = new THREE.Group();
group.position.set(x, 0.137, z);
group.rotation.y = rotationY;
scene.add(group);
cube(0.80, 0.026, 0.18, -0.22, 0, 0, mats.white, group, false);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([
0.18, 0, -0.40,
0.72, 0, 0,
0.18, 0, 0.40
], 3)
);
geometry.computeVertexNormals();
const head = new THREE.Mesh(
geometry,
material(0xffffff, { side: THREE.DoubleSide })
);
group.add(head);
}
groundArrow(2.5, -8.25, -Math.PI / 2);
groundArrow(9.15, 0, 0);
// -------------------------------------------------------------------------
// Turning cyclist, aligned to the route's tangent
// -------------------------------------------------------------------------
const cyclist = new THREE.Group();
const tyreMaterial = material(0x22292d, { roughness: 0.65 });
const bikeMaterial = material(0x243f49, { metalness: 0.25, roughness: 0.45 });
const helmetMaterial = material(0xe2b84f);
const shortsMaterial = material(0x38434a);
function wheel(x) {
const geometry = new THREE.TorusGeometry(0.48, 0.055, 6, 18);
const mesh = new THREE.Mesh(geometry, tyreMaterial);
mesh.position.set(x, 0.52, 0);
mesh.castShadow = true;
cyclist.add(mesh);
}
wheel(-0.70);
wheel(0.70);
const rear = [-0.70, 0.52, 0];
const front = [0.70, 0.52, 0];
const crank = [-0.06, 0.42, 0];
const seat = [-0.30, 0.93, 0];
const head = [0.43, 0.91, 0];
cylinderBetween(rear, seat, 0.045, bikeMaterial, cyclist);
cylinderBetween(seat, crank, 0.045, bikeMaterial, cyclist);
cylinderBetween(crank, rear, 0.045, bikeMaterial, cyclist);
cylinderBetween(seat, head, 0.045, bikeMaterial, cyclist);
cylinderBetween(head, crank, 0.045, bikeMaterial, cyclist);
cylinderBetween(head, front, 0.045, bikeMaterial, cyclist);
cube(0.27, 0.10, 0.15, -0.30, 0.99, 0, mats.dark, cyclist);
cylinderBetween([0.43, 0.91, 0], [0.55, 1.02, 0], 0.035, mats.steel, cyclist);
cylinderBetween([0.55, 1.02, -0.24], [0.55, 1.02, 0.24], 0.035, mats.dark, cyclist);
// Blocky rider.
const torso = cube(0.37, 0.52, 0.30, -0.17, 1.34, 0, mats.jacket, cyclist);
torso.rotation.z = -0.24;
cube(0.34, 0.24, 0.31, -0.05, 1.65, 0, shortsMaterial, cyclist);
cube(0.28, 0.29, 0.27, 0.02, 1.88, 0, mats.skin, cyclist);
cube(0.32, 0.13, 0.30, 0.02, 2.04, 0, helmetMaterial, cyclist);
cylinderBetween(
[-0.04, 1.46, -0.19],
[0.37, 1.12, -0.22],
0.065,
mats.jacket,
cyclist
);
cylinderBetween(
[-0.04, 1.46, 0.19],
[0.37, 1.12, 0.22],
0.065,
mats.jacket,
cyclist
);
cylinderBetween(
[-0.24, 1.14, -0.10],
[0.07, 0.79, -0.12],
0.075,
shortsMaterial,
cyclist
);
cylinderBetween(
[0.07, 0.79, -0.12],
[-0.10, 0.43, -0.10],
0.065,
mats.skin,
cyclist
);
cylinderBetween(
[-0.24, 1.14, 0.10],
[-0.40, 0.72, 0.12],
0.075,
shortsMaterial,
cyclist
);
cylinderBetween(
[-0.40, 0.72, 0.12],
[-0.12, 0.43, 0.10],
0.065,
mats.skin,
cyclist
);
// Place the bicycle directly on the arc at a readable turning angle.
const cyclistTheta = 2.15;
const cyclistDistance =
route.approachLength + (Math.PI - cyclistTheta) * 4;
const cyclistT = cyclistDistance / route.totalLength;
const cyclistPosition = route.getPoint(cyclistT, new THREE.Vector3());
const cyclistTangent = route.getTangent(cyclistT, new THREE.Vector3());
cyclist.position.set(
cyclistPosition.x,
0.115,
cyclistPosition.z
);
// Local bicycle forward direction is +X.
cyclist.rotation.y = -Math.atan2(cyclistTangent.z, cyclistTangent.x);
scene.add(cyclist);
// -------------------------------------------------------------------------
// A single compact car for scale
// -------------------------------------------------------------------------
function voxelCar(x, z, color) {
const group = new THREE.Group();
group.position.set(x, 0.08, z);
scene.add(group);
const bodyMaterial = material(color);
cube(3.05, 0.48, 1.42, 0, 0.48, 0, bodyMaterial, group);
cube(1.65, 0.48, 1.25, -0.10, 0.91, 0, bodyMaterial, group);
cube(1.34, 0.33, 1.28, -0.12, 0.96, 0, mats.glass, group);
cube(1.50, 0.10, 1.30, -0.10, 1.22, 0, bodyMaterial, group);
for (const wheelX of [-1.02, 1.02]) {
for (const wheelZ of [-0.71, 0.71]) {
cube(
0.48, 0.46, 0.20,
wheelX, 0.28, wheelZ,
mats.dark,
group
);
}
}
cube(0.08, 0.17, 0.34, 1.55, 0.55, 0.47, mats.white, group, false);
cube(0.08, 0.17, 0.34, 1.55, 0.55, -0.47, mats.white, group, false);
}
voxelCar(-10.4, 2.20, 0x5f8e98);
// -------------------------------------------------------------------------
// Render
// -------------------------------------------------------------------------
function render() {
renderer.render(scene, camera);
requestAnimationFrame(render);
}
renderer.render(scene, camera);
document.querySelector("#loading").remove();
requestAnimationFrame(render);
</script>
</body>
</html>
```