Prompt specification
Build or customize the Three.js 3D scene "Road alignment curve studio" 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: Road alignment curve studio
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/road-alignment-curve-studio-x1715
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows parametric curves and control points. Compose a short curved road centerline, control handles, and roadside markers; keep the road width consistent and distinguish the geometric design from real civil-engineering standards. Voxel style.
Working Three.js Starter (road-alignment-curve-studio-x1715):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Bézier Road Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #0b1118;
--panel: rgba(13, 21, 29, 0.92);
--line: rgba(148, 163, 184, 0.22);
--text: #e5edf5;
--muted: #91a2b5;
--cyan: #5eead4;
--amber: #fbbf24;
--coral: #fb7185;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, 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%;
outline: none;
touch-action: none;
}
.title-block {
position: fixed;
z-index: 3;
top: 24px;
left: 26px;
max-width: 430px;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.65);
}
.eyebrow {
margin-bottom: 7px;
color: var(--cyan);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(26px, 3vw, 44px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
margin-top: 10px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.concept-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 13px;
padding: 6px 9px;
border: 1px solid rgba(251, 191, 36, 0.45);
background: rgba(251, 191, 36, 0.08);
color: #fde68a;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
}
.concept-badge::before {
width: 7px;
height: 7px;
background: var(--amber);
content: "";
box-shadow: 0 0 12px rgba(251, 191, 36, 0.65);
}
.panel {
position: fixed;
z-index: 5;
top: 24px;
right: 24px;
width: 318px;
max-height: calc(100vh - 48px);
overflow: auto;
padding: 18px;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(16px);
}
.panel::-webkit-scrollbar {
width: 8px;
}
.panel::-webkit-scrollbar-thumb {
background: #334252;
}
.panel-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.panel-header strong {
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.live {
color: var(--cyan);
font: 600 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.live::before {
display: inline-block;
width: 6px;
height: 6px;
margin-right: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 10px var(--cyan);
content: "";
vertical-align: 1px;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1px;
overflow: hidden;
border: 1px solid var(--line);
background: var(--line);
}
.stat {
min-height: 61px;
padding: 11px;
background: rgba(22, 33, 44, 0.94);
}
.stat span {
display: block;
margin-bottom: 6px;
color: var(--muted);
font: 600 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.stat strong {
font: 700 13px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.section {
margin-top: 17px;
padding-top: 16px;
border-top: 1px solid var(--line);
}
.section-title {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
color: var(--muted);
font: 600 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.section-title output {
color: var(--cyan);
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 7px 0 12px;
appearance: none;
border-radius: 0;
outline: none;
background: #314252;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 3px solid #102029;
border-radius: 0;
background: var(--cyan);
box-shadow: 0 0 0 1px var(--cyan);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #102029;
border-radius: 0;
background: var(--cyan);
box-shadow: 0 0 0 1px var(--cyan);
}
.readout {
display: grid;
gap: 7px;
padding: 10px;
border: 1px solid var(--line);
background: rgba(6, 12, 18, 0.48);
color: #cbd5e1;
font: 500 11px/1.35 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.equation {
margin-top: 10px;
padding: 10px;
border-left: 3px solid var(--cyan);
background: rgba(94, 234, 212, 0.06);
color: #b8eee4;
font: 600 10px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace;
overflow-wrap: anywhere;
}
.button-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
button {
min-height: 36px;
padding: 8px 9px;
border: 1px solid #344657;
border-radius: 0;
background: #17232e;
color: #cbd5e1;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
cursor: pointer;
transition:
border-color 120ms ease,
background 120ms ease,
color 120ms ease;
}
button:hover {
border-color: #60758a;
background: #20313f;
color: white;
}
button[aria-pressed="true"] {
border-color: rgba(94, 234, 212, 0.72);
background: rgba(94, 234, 212, 0.12);
color: #bdf7ed;
}
.hint {
margin-top: 10px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.warning {
margin-top: 17px;
padding: 12px;
border: 1px solid rgba(251, 191, 36, 0.34);
background: rgba(251, 191, 36, 0.07);
color: #d7cbaa;
font-size: 10.5px;
line-height: 1.55;
}
.warning strong {
display: block;
margin-bottom: 5px;
color: #fde68a;
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend {
position: fixed;
z-index: 3;
bottom: 23px;
left: 26px;
display: grid;
gap: 8px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 9px;
color: #aebdcd;
font: 600 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
}
.swatch {
width: 20px;
height: 4px;
background: var(--cyan);
}
.swatch.handle {
background: var(--amber);
}
.swatch.marker {
width: 8px;
height: 8px;
background: var(--coral);
}
.scene-hint {
position: fixed;
z-index: 3;
right: 26px;
bottom: 25px;
max-width: 390px;
color: #8294a7;
font: 500 10px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace;
text-align: right;
pointer-events: none;
}
.scene-hint strong {
color: #b7c5d3;
}
@media (max-width: 850px) {
.title-block {
top: 15px;
left: 16px;
}
.title-block .subtitle {
display: none;
}
.concept-badge {
margin-top: 8px;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: min(318px, calc(100vw - 24px));
max-height: 52vh;
padding: 14px;
}
.legend,
.scene-hint {
display: none;
}
}
@media (max-width: 480px) {
h1 {
max-width: 270px;
font-size: 29px;
}
.eyebrow {
font-size: 9px;
}
.panel {
max-height: 49vh;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive voxel Bézier road visualization"></main>
<header class="title-block">
<div class="eyebrow">Parametric corridor study / 01</div>
<h1>Voxel Bézier<br>Road Lab</h1>
<div class="subtitle">
A constant-width cubic Bézier ribbon with editable control points,
tangent handles, and roadside survey markers.
</div>
<div class="concept-badge">Geometry visualization only</div>
</header>
<aside class="panel" aria-label="Visualization controls">
<div class="panel-header">
<strong>Curve inspector</strong>
<span class="live">Live model</span>
</div>
<div class="stats">
<div class="stat">
<span>Curve</span>
<strong>Cubic Bézier</strong>
</div>
<div class="stat">
<span>Control points</span>
<strong>4 / draggable</strong>
</div>
<div class="stat">
<span>Visual width</span>
<strong>4.2 units</strong>
</div>
<div class="stat">
<span>Segments</span>
<strong>1 continuous</strong>
</div>
</div>
<section class="section">
<div class="section-title">
<label for="tSlider">Parameter t</label>
<output id="tValue" for="tSlider">0.460</output>
</div>
<input
id="tSlider"
type="range"
min="0"
max="1"
step="0.001"
value="0.46"
>
<div class="readout">
<div id="pointReadout">B(t) = (0.00, 0.00)</div>
<div id="headingReadout">Heading = 0.0°</div>
</div>
<div class="equation">
L/R(t) = B(t) ± n(t) · W/2<br>
W = 4.2 · n(t) is the curve normal
</div>
</section>
<section class="section">
<div class="section-title">Display controls</div>
<div class="button-grid">
<button id="animateBtn" type="button" aria-pressed="false">Animate t</button>
<button id="handlesBtn" type="button" aria-pressed="true">Handles</button>
<button id="markersBtn" type="button" aria-pressed="true">Markers</button>
<button id="resetBtn" type="button">Reset view</button>
<button id="topBtn" type="button" style="grid-column: 1 / -1">Top view</button>
</div>
<div class="hint">
Drag P1 or P2 to reshape the curve. Drag empty space to orbit,
Shift-drag to pan, and use the wheel to zoom.
</div>
</section>
<div class="warning">
<strong>Not a civil-engineering standard</strong>
This is an abstract geometric visualization. The constant width is a
normal offset in the model only. It does not validate real-world
alignment, superelevation, drainage, sight distance, safety,
accessibility, constructability, or jurisdictional code compliance.
</div>
</aside>
<div class="legend" aria-hidden="true">
<div class="legend-item"><span class="swatch"></span> Centerline B(t)</div>
<div class="legend-item"><span class="swatch handle"></span> Bézier tangent handles</div>
<div class="legend-item"><span class="swatch marker"></span> Roadside markers</div>
</div>
<div class="scene-hint">
<strong>DRAG</strong> orbit · <strong>SHIFT + DRAG</strong> pan ·
<strong>SCROLL</strong> zoom<br>
Keyboard: Space = animate, H = handles, M = markers, R = reset
</div>
<noscript>This visualization requires JavaScript and WebGL.</noscript>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1118);
scene.fog = new THREE.Fog(0x0b1118, 34, 68);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setClearColor(0x0b1118);
renderer.domElement.id = "sceneCanvas";
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Voxel road scene. Drag to orbit, shift-drag to pan, and drag P1 or P2 to reshape."
);
stage.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-15, 15, 10, -10, 0.1, 100);
const cameraState = {
azimuth: 0.72,
polar: 0.84,
zoom: 1,
radius: 40,
target: new THREE.Vector3(-0.8, 0, 0.5)
};
const hemi = new THREE.HemisphereLight(0xc7e9ff, 0x18222c, 2.1);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xfff3d7, 3.1);
keyLight.position.set(9, 17, 11);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -23;
keyLight.shadow.camera.right = 23;
keyLight.shadow.camera.top = 20;
keyLight.shadow.camera.bottom = -20;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 55;
keyLight.shadow.normalBias = 0.035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x7dd3fc, 0.85);
fillLight.position.set(-12, 8, -10);
scene.add(fillLight);
const ROAD_WIDTH = 4.2;
const HALF_WIDTH = ROAD_WIDTH / 2;
const ROAD_Y = 0.18;
const CURVE_SAMPLES = 96;
const VOXEL_BANDS = 8;
// Cubic Bézier control points: P0, P1, P2, P3.
const controlPoints = [
new THREE.Vector3(-10.8, 0, 3.8),
new THREE.Vector3(-6.2, 0, -6.4),
new THREE.Vector3(4.2, 0, 8.0),
new THREE.Vector3(8.8, 0, 3.9)
];
const materials = {
road: new THREE.MeshStandardMaterial({
color: 0xffffff,
vertexColors: true,
roughness: 0.96,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
}),
kerb: new THREE.MeshStandardMaterial({
color: 0x91a2b3,
roughness: 0.88,
side: THREE.DoubleSide
}),
dash: new THREE.MeshStandardMaterial({
color: 0xf8e7b0,
emissive: 0x8c6b1b,
emissiveIntensity: 0.18,
roughness: 0.72
}),
centerLine: new THREE.LineBasicMaterial({
color: 0x5eead4,
transparent: true,
opacity: 0.95,
depthTest: false
}),
handle: new THREE.LineBasicMaterial({
color: 0xfbbf24,
transparent: true,
opacity: 0.95,
depthTest: false
}),
controlPolygon: new THREE.LineDashedMaterial({
color: 0xb99a54,
dashSize: 0.34,
gapSize: 0.2,
transparent: true,
opacity: 0.72,
depthTest: false
}),
stem: new THREE.LineBasicMaterial({
color: 0x71879a,
transparent: true,
opacity: 0.75,
depthTest: false
}),
endpoint: new THREE.MeshStandardMaterial({
color: 0x5eead4,
emissive: 0x1c8f7c,
emissiveIntensity: 0.8,
roughness: 0.45
}),
inner: new THREE.MeshStandardMaterial({
color: 0xfbbf24,
emissive: 0x9a5d05,
emissiveIntensity: 0.7,
roughness: 0.45
}),
sampler: new THREE.MeshStandardMaterial({
color: 0xfde68a,
emissive: 0xfbbf24,
emissiveIntensity: 0.85,
roughness: 0.35
}),
markerPost: new THREE.MeshStandardMaterial({
color: 0x304252,
roughness: 0.88
}),
markerBase: new THREE.MeshStandardMaterial({
color: 0x17242e,
roughness: 0.95
}),
markerCap: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.58
})
};
const roadGroup = new THREE.Group();
const guideGroup = new THREE.Group();
const controlGroup = new THREE.Group();
const markerGroup = new THREE.Group();
const widthGroup = new THREE.Group();
const samplerGroup = new THREE.Group();
scene.add(
roadGroup,
guideGroup,
controlGroup,
markerGroup,
widthGroup,
samplerGroup
);
const roadMesh = new THREE.Mesh(new THREE.BufferGeometry(), materials.road);
roadMesh.receiveShadow = true;
roadGroup.add(roadMesh);
const kerbMesh = new THREE.Mesh(new THREE.BufferGeometry(), materials.kerb);
kerbMesh.receiveShadow = true;
roadGroup.add(kerbMesh);
const centerLine = new THREE.Line(new THREE.BufferGeometry(), materials.centerLine);
centerLine.renderOrder = 7;
scene.add(centerLine);
const handleA = new THREE.Line(new THREE.BufferGeometry(), materials.handle);
const handleB = new THREE.Line(new THREE.BufferGeometry(), materials.handle);
const controlPolygon = new THREE.Line(
new THREE.BufferGeometry(),
materials.controlPolygon
);
handleA.renderOrder = 8;
handleB.renderOrder = 8;
controlPolygon.renderOrder = 6;
guideGroup.add(handleA, handleB, controlPolygon);
const pointGeometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
const controlMeshes = [];
const controlLabels = [];
const labelTexts = ["P0 · START", "P1", "P2", "P3 · END"];
const labelColors = ["#5eead4", "#fbbf24", "#fbbf24", "#5eead4"];
function createLabel(text, color = "#dce8f2", width = 2.1) {
const canvas = document.createElement("canvas");
canvas.width = 320;
canvas.height = 80;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(8, 15, 22, 0.88)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = color;
ctx.fillRect(0, 0, 7, canvas.height);
ctx.strokeStyle = "rgba(148, 163, 184, 0.38)";
ctx.strokeRect(0.5, 0.5, canvas.width - 1, canvas.height - 1);
ctx.font = "700 27px ui-monospace, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.fillText(text, canvas.width / 2 + 3, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, 0.525, 1);
sprite.renderOrder = 20;
return sprite;
}
for (let i = 0; i < controlPoints.length; i++) {
const mesh = new THREE.Mesh(
pointGeometry,
i === 0 || i === 3 ? materials.endpoint : materials.inner
);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.controlIndex = i;
controlMeshes.push(mesh);
controlGroup.add(mesh);
const label = createLabel(labelTexts[i], labelColors[i], i === 0 || i === 3 ? 2.15 : 1.35);
controlLabels.push(label);
controlGroup.add(label);
}
const stems = new THREE.LineSegments(
new THREE.BufferGeometry(),
materials.stem
);
stems.renderOrder = 7;
controlGroup.add(stems);
const dashGeometry = new THREE.BoxGeometry(0.19, 0.08, 0.76);
const dashMesh = new THREE.InstancedMesh(
dashGeometry,
materials.dash,
40
);
dashMesh.count = 0;
dashMesh.castShadow = true;
dashMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
roadGroup.add(dashMesh);
const markerPostGeometry = new THREE.BoxGeometry(0.16, 0.66, 0.16);
const markerBaseGeometry = new THREE.BoxGeometry(0.34, 0.1, 0.34);
const markerCapGeometry = new THREE.BoxGeometry(0.23, 0.13, 0.23);
const markerPosts = new THREE.InstancedMesh(
markerPostGeometry,
materials.markerPost,
24
);
const markerBases = new THREE.InstancedMesh(
markerBaseGeometry,
materials.markerBase,
24
);
const markerCaps = new THREE.InstancedMesh(
markerCapGeometry,
materials.markerCap,
24
);
markerPosts.count = 0;
markerBases.count = 0;
markerCaps.count = 0;
markerPosts.castShadow = true;
markerBases.castShadow = true;
markerCaps.castShadow = true;
markerPosts.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
markerBases.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
markerCaps.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
markerGroup.add(markerPosts, markerBases, markerCaps);
const samplerMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.34, 0.34),
materials.sampler
);
samplerMesh.castShadow = true;
samplerGroup.add(samplerMesh);
const samplerStem = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineBasicMaterial({
color: 0xfde68a,
transparent: true,
opacity: 0.8,
depthTest: false
})
);
samplerStem.renderOrder = 10;
samplerGroup.add(samplerStem);
const widthLine = new THREE.LineSegments(
new THREE.BufferGeometry(),
new THREE.LineBasicMaterial({
color: 0xfbbf24,
transparent: true,
opacity: 0.95,
depthTest: false
})
);
widthLine.renderOrder = 11;
widthGroup.add(widthLine);
const widthLabel = createLabel("W = 4.2", "#fbbf24", 1.85);
widthGroup.add(widthLabel);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
new THREE.MeshStandardMaterial({
color: 0x111a22,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.72;
ground.receiveShadow = true;
scene.add(ground);
const TERRAIN_X = 23;
const TERRAIN_Z = 20;
const TERRAIN_TOTAL = TERRAIN_X * TERRAIN_Z;
const terrainGeometry = new THREE.BoxGeometry(1.42, 1, 1.42);
const terrain = new THREE.InstancedMesh(
terrainGeometry,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.97,
metalness: 0
}),
TERRAIN_TOTAL
);
terrain.receiveShadow = true;
terrain.castShadow = true;
terrain.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(terrain);
function addContextBlock(x, z, w, d, h, color) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.91
});
const block = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
material
);
block.position.set(x, -0.1 + h / 2, z);
block.castShadow = true;
block.receiveShadow = true;
scene.add(block);
const roof = new THREE.Mesh(
new THREE.BoxGeometry(w * 0.72, 0.22, d * 0.72),
new THREE.MeshStandardMaterial({
color: 0x435563,
roughness: 0.88
})
);
roof.position.set(x, -0.1 + h + 0.11, z);
roof.castShadow = true;
scene.add(roof);
}
addContextBlock(-13.8, -9.6, 2.2, 2.3, 2.7, 0x2b3b47);
addContextBlock(13.0, -9.0, 2.7, 2.4, 3.6, 0x31424e);
addContextBlock(-13.9, 9.0, 2.6, 2.2, 2.1, 0x293a47);
addContextBlock(13.4, 9.4, 2.1, 2.5, 3.0, 0x354651);
const dummy = new THREE.Object3D();
const curveLUT = [];
let curveLength = 0;
function evaluateCurve(t, target = new THREE.Vector3()) {
const u = 1 - t;
const a = u * u * u;
const b = 3 * u * u * t;
const c = 3 * u * t * t;
const d = t * t * t;
target.set(
a * controlPoints[0].x +
b * controlPoints[1].x +
c * controlPoints[2].x +
d * controlPoints[3].x,
0,
a * controlPoints[0].z +
b * controlPoints[1].z +
c * controlPoints[2].z +
d * controlPoints[3].z
);
return target;
}
function evaluateTangent(t, target = new THREE.Vector3()) {
const u = 1 - t;
target.set(
3 * u * u * (controlPoints[1].x - controlPoints[0].x) +
6 * u * t * (controlPoints[2].x - controlPoints[1].x) +
3 * t * t * (controlPoints[3].x - controlPoints[2].x),
0,
3 * u * u * (controlPoints[1].z - controlPoints[0].z) +
6 * u * t * (controlPoints[2].z - controlPoints[1].z) +
3 * t * t * (controlPoints[3].z - controlPoints[2].z)
);
return target.normalize();
}
function evaluateNormal(t, target = new THREE.Vector3()) {
const tangent = evaluateTangent(t, new THREE.Vector3());
return target.set(-tangent.z, 0, tangent.x).normalize();
}
function buildCurveLUT() {
curveLUT.length = 0;
curveLength = 0;
let previous = evaluateCurve(0, new THREE.Vector3());
curveLUT.push({
t: 0,
length: 0,
point: previous.clone()
});
for (let i = 1; i <= 320; i++) {
const t = i / 320;
const point = evaluateCurve(t, new THREE.Vector3());
curveLength += point.distanceTo(previous);
curveLUT.push({
t,
length: curveLength,
point: point.clone()
});
previous = point;
}
}
function sampleAtDistance(distance, pointTarget, tangentTarget) {
const d = THREE.MathUtils.clamp(distance, 0, curveLength);
let low = 0;
let high = curveLUT.length - 1;
while (low < high - 1) {
const mid = (low + high) >> 1;
if (curveLUT[mid].length < d) {
low = mid;
} else {
high = mid;
}
}
const a = curveLUT[low];
const b = curveLUT[high];
const segmentLength = b.length - a.length;
const fraction = segmentLength > 0 ? (d - a.length) / segmentLength : 0;
const t = THREE.MathUtils.lerp(a.t, b.t, fraction);
evaluateCurve(t, pointTarget);
evaluateTangent(t, tangentTarget);
return t;
}
function setLinePoints(line, points) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
line.geometry.dispose();
line.geometry = geometry;
}
function pushTriangle(positions, a, b, c) {
positions.push(
a.x, a.y, a.z,
b.x, b.y, b.z,
c.x, c.y, c.z
);
}
function updateRoadSurface() {
const positions = [];
const colors = [];
const rows = [];
for (let i = 0; i <= CURVE_SAMPLES; i++) {
const t = i / CURVE_SAMPLES;
rows.push({
point: evaluateCurve(t, new THREE.Vector3()),
normal: evaluateNormal(t, new THREE.Vector3())
});
}
for (let i = 0; i < CURVE_SAMPLES; i++) {
const rowA = rows[i];
const rowB = rows[i + 1];
for (let band = 0; band < VOXEL_BANDS; band++) {
const s0 = -HALF_WIDTH + (band / VOXEL_BANDS) * ROAD_WIDTH;
const s1 = -HALF_WIDTH + ((band + 1) / VOXEL_BANDS) * ROAD_WIDTH;
const v00 = rowA.point.clone().addScaledVector(rowA.normal, s0);
const v01 = rowA.point.clone().addScaledVector(rowA.normal, s1);
const v10 = rowB.point.clone().addScaledVector(rowB.normal, s0);
const v11 = rowB.point.clone().addScaledVector(rowB.normal, s1);
v00.y = ROAD_Y;
v01.y = ROAD_Y;
v10.y = ROAD_Y;
v11.y = ROAD_Y;
pushTriangle(positions, v00, v01, v10);
pushTriangle(positions, v01, v11, v10);
const edgeBand = band === 0 || band === VOXEL_BANDS - 1;
const color = new THREE.Color(edgeBand ? 0x35434d : 0x283640);
const variation = (i + band * 3) % 7 === 0 ? 0.045 : 0;
color.offsetHSL(0, 0, variation);
for (let vertex = 0; vertex < 6; vertex++) {
colors.push(color.r, color.g, color.b);
}
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
roadMesh.geometry.dispose();
roadMesh.geometry = geometry;
const kerbPositions = [];
for (const side of [1, -1]) {
for (let i = 0; i < CURVE_SAMPLES; i++) {
const rowA = rows[i];
const rowB = rows[i + 1];
const outerA = rowA.point.clone()
.addScaledVector(rowA.normal, side * HALF_WIDTH);
const innerA = rowA.point.clone()
.addScaledVector(rowA.normal, side * (HALF_WIDTH - 0.12));
const outerB = rowB.point.clone()
.addScaledVector(rowB.normal, side * HALF_WIDTH);
const innerB = rowB.point.clone()
.addScaledVector(rowB.normal, side * (HALF_WIDTH - 0.12));
outerA.y = innerA.y = outerB.y = innerB.y = ROAD_Y + 0.018;
pushTriangle(kerbPositions, outerA, innerA, outerB);
pushTriangle(kerbPositions, innerA, innerB, outerB);
}
}
const kerbGeometry = new THREE.BufferGeometry();
kerbGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(kerbPositions, 3)
);
kerbGeometry.computeVertexNormals();
kerbGeometry.computeBoundingSphere();
kerbMesh.geometry.dispose();
kerbMesh.geometry = kerbGeometry;
}
function updateCurveGuides() {
const centerPoints = [];
for (let i = 0; i <= 180; i++) {
const p = evaluateCurve(i / 180, new THREE.Vector3());
p.y = ROAD_Y + 0.125;
centerPoints.push(p);
}
setLinePoints(centerLine, centerPoints);
const handleAPoints = [
controlPoints[0].clone().setY(0.46),
controlPoints[1].clone().setY(0.46)
];
const handleBPoints = [
controlPoints[2].clone().setY(0.46),
controlPoints[3].clone().setY(0.46)
];
const polygonPoints = controlPoints.map(p => p.clone().setY(0.37));
setLinePoints(handleA, handleAPoints);
setLinePoints(handleB, handleBPoints);
setLinePoints(controlPolygon, polygonPoints);
controlPolygon.computeLineDistances();
const stemPositions = [];
for (let i = 0; i < controlPoints.length; i++) {
const p = controlPoints[i];
controlMeshes[i].position.set(p.x, 0.55, p.z);
controlLabels[i].position.set(p.x, 1.13, p.z);
stemPositions.push(
p.x, 0.08, p.z,
p.x, 0.37, p.z
);
}
stems.geometry.dispose();
stems.geometry = new THREE.BufferGeometry();
stems.geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(stemPositions, 3)
);
}
function updateDecorations() {
let dashCount = 0;
const dashSpacing = 1.35;
const dashPoint = new THREE.Vector3();
const dashTangent = new THREE.Vector3();
for (
let distance = 0.55;
distance < curveLength - 0.35;
distance += dashSpacing
) {
sampleAtDistance(distance, dashPoint, dashTangent);
dummy.position.set(dashPoint.x, ROAD_Y + 0.06, dashPoint.z);
dummy.rotation.set(
0,
Math.atan2(dashTangent.x, dashTangent.z),
0
);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
dashMesh.setMatrixAt(dashCount, dummy.matrix);
dashCount++;
}
dashMesh.count = dashCount;
dashMesh.instanceMatrix.needsUpdate = true;
const markerPoint = new THREE.Vector3();
const markerTangent = new THREE.Vector3();
const markerNormal = new THREE.Vector3();
let markerIndex = 0;
const markerStations = 9;
for (let station = 0; station < markerStations; station++) {
const distance = ((station + 0.5) / markerStations) * curveLength;
sampleAtDistance(distance, markerPoint, markerTangent);
markerNormal.set(-markerTangent.z, 0, markerTangent.x).normalize();
const yaw = Math.atan2(markerTangent.x, markerTangent.z);
for (const side of [1, -1]) {
const offset = HALF_WIDTH + 0.48;
const x = markerPoint.x + markerNormal.x * offset * side;
const z = markerPoint.z + markerNormal.z * offset * side;
dummy.position.set(x, 0.21, z);
dummy.rotation.set(0, yaw, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
markerPosts.setMatrixAt(markerIndex, dummy.matrix);
dummy.position.set(x, -0.035, z);
dummy.updateMatrix();
markerBases.setMatrixAt(markerIndex, dummy.matrix);
dummy.position.set(x, 0.59, z);
dummy.updateMatrix();
markerCaps.setMatrixAt(markerIndex, dummy.matrix);
markerCaps.setColorAt(
markerIndex,
new THREE.Color(side === 1 ? 0xfb7185 : 0xfacc15)
);
markerIndex++;
}
}
markerPosts.count = markerIndex;
markerBases.count = markerIndex;
markerCaps.count = markerIndex;
markerPosts.instanceMatrix.needsUpdate = true;
markerBases.instanceMatrix.needsUpdate = true;
markerCaps.instanceMatrix.needsUpdate = true;
if (markerCaps.instanceColor) {
markerCaps.instanceColor.needsUpdate = true;
}
}
function updateWidthGuide() {
const t = 0.46;
const point = evaluateCurve(t, new THREE.Vector3());
const tangent = evaluateTangent(t, new THREE.Vector3());
const normal = new THREE.Vector3(-tangent.z, 0, tangent.x).normalize();
const left = point.clone().addScaledVector(normal, HALF_WIDTH);
const right = point.clone().addScaledVector(normal, -HALF_WIDTH);
left.y = right.y = ROAD_Y + 0.43;
const tick = tangent.clone().multiplyScalar(0.34);
const positions = [
left.x, left.y, left.z,
right.x, right.y, right.z,
left.x - tick.x, left.y, left.z - tick.z,
left.x + tick.x, left.y, left.z + tick.z,
right.x - tick.x, right.y, right.z - tick.z,
right.x + tick.x, right.y, right.z + tick.z
];
widthLine.geometry.dispose();
widthLine.geometry = new THREE.BufferGeometry();
widthLine.geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
widthLabel.position.set(point.x, ROAD_Y + 1.05, point.z);
}
function updateTerrain() {
let index = 0;
const spacing = 1.48;
for (let ix = 0; ix < TERRAIN_X; ix++) {
for (let iz = 0; iz < TERRAIN_Z; iz++) {
const x = (ix - (TERRAIN_X - 1) / 2) * spacing;
const z = (iz - (TERRAIN_Z - 1) / 2) * spacing;
let minDistance = Infinity;
for (const sample of curveLUT) {
const dx = x - sample.point.x;
const dz = z - sample.point.z;
const distance = Math.sqrt(dx * dx + dz * dz);
minDistance = Math.min(minDistance, distance);
}
const nearRoad = minDistance < HALF_WIDTH + 1.1;
let top = -0.11;
if (!nearRoad) {
const wave =
Math.sin(x * 0.55) +
Math.cos(z * 0.43) +
Math.sin((x + z) * 0.22);
const step = wave > 1.25 ? 2 : wave > 0.05 ? 1 : 0;
top += step * 0.19;
}
dummy.position.set(x, top - 0.5, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, top + 0.5, 1);
dummy.updateMatrix();
terrain.setMatrixAt(index, dummy.matrix);
const color = new THREE.Color(
nearRoad
? 0x26343d
: ((ix + iz) % 3 === 0 ? 0x2b3942 : 0x24313a)
);
color.offsetHSL(0, 0, ((ix * 7 + iz * 11) % 5 - 2) * 0.008);
terrain.setColorAt(index, color);
index++;
}
}
terrain.instanceMatrix.needsUpdate = true;
if (terrain.instanceColor) {
terrain.instanceColor.needsUpdate = true;
}
}
const tSlider = document.querySelector("#tSlider");
const tValue = document.querySelector("#tValue");
const pointReadout = document.querySelector("#pointReadout");
const headingReadout = document.querySelector("#headingReadout");
let currentT = Number(tSlider.value);
let playing = false;
let showHandles = true;
let showMarkers = true;
function updateSampler() {
const point = evaluateCurve(currentT, new THREE.Vector3());
const tangent = evaluateTangent(currentT, new THREE.Vector3());
samplerMesh.position.set(point.x, ROAD_Y + 0.46, point.z);
samplerMesh.rotation.y += 0.018;
setLinePoints(samplerStem, [
new THREE.Vector3(point.x, ROAD_Y + 0.04, point.z),
new THREE.Vector3(point.x, ROAD_Y + 0.36, point.z)
]);
tValue.value = currentT.toFixed(3);
pointReadout.textContent =
`B(t) = (${point.x.toFixed(2)}, ${point.z.toFixed(2)})`;
const heading =
(THREE.MathUtils.radToDeg(Math.atan2(tangent.x, tangent.z)) + 360) % 360;
headingReadout.textContent = `Heading = ${heading.toFixed(1)}°`;
}
function rebuildModel() {
buildCurveLUT();
updateRoadSurface();
updateCurveGuides();
updateDecorations();
updateWidthGuide();
updateTerrain();
updateSampler();
}
rebuildModel();
const animateBtn = document.querySelector("#animateBtn");
const handlesBtn = document.querySelector("#handlesBtn");
const markersBtn = document.querySelector("#markersBtn");
const resetBtn = document.querySelector("#resetBtn");
const topBtn = document.querySelector("#topBtn");
animateBtn.addEventListener("click", () => {
playing = !playing;
animateBtn.setAttribute("aria-pressed", String(playing));
animateBtn.textContent = playing ? "Pause t" : "Animate t";
});
handlesBtn.addEventListener("click", () => {
showHandles = !showHandles;
guideGroup.visible = showHandles;
handlesBtn.setAttribute("aria-pressed", String(showHandles));
});
markersBtn.addEventListener("click", () => {
showMarkers = !showMarkers;
markerGroup.visible = showMarkers;
markersBtn.setAttribute("aria-pressed", String(showMarkers));
});
tSlider.addEventListener("input", () => {
currentT = Number(tSlider.value);
updateSampler();
});
function resetCamera() {
cameraState.azimuth = 0.72;
cameraState.polar = 0.84;
cameraState.zoom = 1;
cameraState.target.set(-0.8, 0, 0.5);
updateProjection();
}
resetBtn.addEventListener("click", resetCamera);
topBtn.addEventListener("click", () => {
cameraState.azimuth = 0;
cameraState.polar = 0.16;
cameraState.zoom = 1.05;
cameraState.target.set(-0.8, 0, 0.5);
updateProjection();
});
// Camera controls are implemented directly, without Three.js addons.
function updateProjection() {
const aspect = stage.clientWidth / Math.max(stage.clientHeight, 1);
const baseHeight = Math.max(19, 26 / aspect);
const height = baseHeight / cameraState.zoom;
const width = height * aspect;
camera.left = -width / 2;
camera.right = width / 2;
camera.top = height / 2;
camera.bottom = -height / 2;
camera.updateProjectionMatrix();
}
function updateCamera() {
const { azimuth, polar, radius, target } = cameraState;
const sinPolar = Math.sin(polar);
camera.position.set(
target.x + radius * sinPolar * Math.sin(azimuth),
target.y + radius * Math.cos(polar),
target.z + radius * sinPolar * Math.cos(azimuth)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
updateProjection();
}
window.addEventListener("resize", resize);
resize();
updateCamera();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const dragOffset = new THREE.Vector3();
const dragHit = new THREE.Vector3();
let pointerMode = null;
let draggedControl = -1;
let previousX = 0;
let previousY = 0;
let rebuildPending = false;
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function requestModelRebuild() {
if (rebuildPending) return;
rebuildPending = true;
requestAnimationFrame(() => {
rebuildPending = false;
rebuildModel();
});
}
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
previousX = event.clientX;
previousY = event.clientY;
draggedControl = -1;
updatePointer(event);
if (event.button === 0 && !event.shiftKey) {
const intersections = raycaster.intersectObjects(controlMeshes, false);
if (intersections.length) {
draggedControl = intersections[0].object.userData.controlIndex;
if (raycaster.ray.intersectPlane(dragPlane, dragHit)) {
dragOffset.copy(controlPoints[draggedControl]).sub(dragHit);
} else {
dragOffset.set(0, 0, 0);
}
renderer.domElement.style.cursor = "grabbing";
return;
}
}
pointerMode = event.shiftKey || event.button === 2 ? "pan" : "orbit";
renderer.domElement.style.cursor = pointerMode === "pan" ? "move" : "grabbing";
});
renderer.domElement.addEventListener("pointermove", event => {
updatePointer(event);
if (draggedControl >= 0) {
if (raycaster.ray.intersectPlane(dragPlane, dragHit)) {
const p = controlPoints[draggedControl];
p.x = THREE.MathUtils.clamp(dragHit.x + dragOffset.x, -14.5, 14.5);
p.z = THREE.MathUtils.clamp(dragHit.z + dragOffset.z, -11.5, 11.5);
requestModelRebuild();
}
return;
}
if (!pointerMode) {
const intersections = raycaster.intersectObjects(controlMeshes, false);
renderer.domElement.style.cursor = intersections.length ? "grab" : "default";
for (let i = 0; i < controlMeshes.length; i++) {
const hovered = intersections.length &&
intersections[0].object.userData.controlIndex === i;
controlMeshes[i].scale.setScalar(hovered ? 1.16 : 1);
}
return;
}
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (pointerMode === "orbit") {
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar + dy * 0.005,
0.14,
1.38
);
} else if (pointerMode === "pan") {
const rect = renderer.domElement.getBoundingClientRect();
const worldPerPixel =
(camera.top - camera.bottom) /
(rect.height * cameraState.zoom);
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0);
const up = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 1);
cameraState.target
.addScaledVector(right, -dx * worldPerPixel)
.addScaledVector(up, dy * worldPerPixel);
}
});
function endPointer(event) {
pointerMode = null;
draggedControl = -1;
renderer.domElement.style.cursor = "default";
if (event && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.0011),
0.65,
2.6
);
updateProjection();
},
{ passive: false }
);
window.addEventListener("keydown", event => {
const tag = event.target.tagName;
if (tag === "INPUT" || tag === "BUTTON") return;
if (event.code === "Space") {
event.preventDefault();
animateBtn.click();
} else if (event.key.toLowerCase() === "h") {
handlesBtn.click();
} else if (event.key.toLowerCase() === "m") {
markersBtn.click();
} else if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (playing) {
currentT = (currentT + delta * 0.085) % 1;
tSlider.value = String(currentT);
updateSampler();
}
updateCamera();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```