Prompt specification
Build or customize the Three.js 3D scene "Ribeira facade balcony and quay step" 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: Ribeira facade balcony and quay step
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/ribeira-facade-balcony-and-quay-step-x2853
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts Porto's Ribeira waterfront. Build a referenced narrow Porto facade fragment with balcony and a step down to the quay. Show the vertical stacking of windows and the ground-level public route. Voxel style.
Working Three.js Starter (ribeira-facade-balcony-and-quay-step-x2853):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Porto · Ribeira Voxel Waterfront</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9d2d7;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.title-card {
position: fixed;
top: 24px;
left: 24px;
z-index: 10;
max-width: min(360px, calc(100vw - 48px));
padding: 17px 20px 16px;
color: #fff7e7;
background: rgba(29, 45, 49, 0.82);
border: 1px solid rgba(255, 255, 255, 0.18);
box-shadow: 0 12px 35px rgba(18, 31, 34, 0.2);
backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 6px;
color: #eab86f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(26px, 4vw, 42px);
font-weight: 750;
line-height: 0.96;
letter-spacing: -0.045em;
}
.subtitle {
margin-top: 9px;
color: rgba(255, 247, 231, 0.72);
font-size: 12px;
line-height: 1.5;
}
.controls {
position: fixed;
right: 22px;
bottom: 22px;
z-index: 10;
display: flex;
align-items: center;
gap: 12px;
padding: 10px 12px 10px 15px;
color: rgba(255, 255, 255, 0.9);
background: rgba(29, 45, 49, 0.78);
border: 1px solid rgba(255, 255, 255, 0.16);
box-shadow: 0 10px 30px rgba(18, 31, 34, 0.18);
backdrop-filter: blur(12px);
font-size: 11px;
}
.controls span {
white-space: nowrap;
}
button {
padding: 7px 10px;
color: #fff7e7;
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 4px;
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
}
button:hover {
background: rgba(255, 255, 255, 0.2);
}
@media (max-width: 620px) {
.title-card {
top: 14px;
left: 14px;
padding: 14px 16px;
}
.controls {
right: 12px;
bottom: 12px;
}
.controls span {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive voxel view of Porto's Ribeira waterfront"></main>
<header class="title-card">
<div class="eyebrow">Porto · Douro River</div>
<h1>RIBEIRA</h1>
<div class="subtitle">
A narrow, tile-fronted facade fragment with stacked windows,
an iron balcony, public pavement, and steps descending to the quay.
</div>
</header>
<div class="controls">
<span>Drag to orbit · Shift-drag to pan · Scroll to zoom</span>
<button id="reset" type="button">Reset view</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const mount = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9d2d7);
scene.fog = new THREE.Fog(0xb9d2d7, 42, 88);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
180
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance",
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
mount.appendChild(renderer.domElement);
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
flatShading: true,
...options,
});
const M = {
water: material(0x477f91, { roughness: 0.46, metalness: 0.08 }),
quay: material(0x8d8374),
quayLight: material(0xa39886),
quayDark: material(0x736b61),
paving: [
material(0xc6bba6),
material(0xb8ad98),
material(0xd0c4af),
material(0xaaa08d),
],
cream: material(0xe7cf9d),
yellow: material(0xd59a55),
yellowLight: material(0xe4b66e),
trim: material(0xf0d7a4),
coral: material(0xb96352),
coralLight: material(0xc9785f),
teal: material(0x668789),
tealLight: material(0x76999a),
mutedBlue: material(0x788f98),
paleBlue: material(0xa2afb0),
peach: material(0xc88d70),
roof: material(0xa84f3f),
roofLight: material(0xc15d43),
dark: material(0x18313a),
darker: material(0x101f25),
shutter: material(0x315b52),
shutterBlue: material(0x405f68),
iron: material(0x26363a, { roughness: 0.65, metalness: 0.35 }),
stone: material(0xc5bca8),
stoneDark: material(0x8c897e),
wood: material(0x85563d),
leaf: material(0x3f7753),
leafLight: material(0x60915b),
pot: material(0xa65d40),
lamp: material(0xffd995, {
emissive: 0xffad45,
emissiveIntensity: 1.4,
}),
skin: material(0xd7a579),
white: material(0xf5edda),
red: material(0xb84c42),
blue: material(0x4f758c),
};
// ------------------------------------------------------------
// Box helper: every visible object is deliberately voxel-like.
// ------------------------------------------------------------
function box(
width,
height,
depth,
x,
y,
z,
mat,
parent = scene,
castShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function groupAt(x, y, z, parent = scene) {
const group = new THREE.Group();
group.position.set(x, y, z);
parent.add(group);
return group;
}
// ------------------------------------------------------------
// River, quay structure, and public pavement
// ------------------------------------------------------------
const water = new THREE.Mesh(
new THREE.PlaneGeometry(140, 130),
M.water
);
water.rotation.x = -Math.PI / 2;
water.position.set(0, -0.82, 20);
water.receiveShadow = true;
scene.add(water);
// Upper quay mass. Its front face is the retaining wall.
box(42, 2.05, 12, 0, 0.175, 1, M.quay);
box(42.3, 0.12, 12.2, 0, 1.13, 1, M.stone);
// Voxel masonry courses along the quay wall.
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 21; col++) {
const x = -20 + col * 2 + (row % 2 ? 0.3 : 0);
if (Math.abs(x) < 1.85) continue;
const stoneMat =
row === 1 ? M.quayLight : (col + row) % 3 ? M.quay : M.quayDark;
box(
1.9,
0.48,
0.12,
x,
-0.35 + row * 0.57,
7.04,
stoneMat,
scene,
false
);
}
}
// Public route: a broad field of small stone pavers.
for (let row = 0; row < 7; row++) {
for (let col = 0; col < 38; col++) {
const x = -18.5 + col;
const z = 2.15 + row * 0.67;
const mat = M.paving[(row * 7 + col * 3) % M.paving.length];
box(
0.94,
0.055,
0.61,
x,
1.225,
z,
mat,
scene,
false
);
}
}
// A narrow dark drainage line makes the route read as infrastructure.
box(36, 0.025, 0.09, 0, 1.272, 4.27, M.stoneDark, scene, false);
// Pale central passage leading under the narrow facade.
for (let i = 0; i < 6; i++) {
box(
2.05,
0.06,
0.58,
0,
1.225,
-1.75 + i * 0.62,
i % 2 ? M.paving[0] : M.paving[2],
scene,
false
);
}
// ------------------------------------------------------------
// Central narrow Porto facade fragment
// ------------------------------------------------------------
const GROUND = 1.2;
const MAIN_WIDTH = 5;
const MAIN_TOP = 13.3;
const PORTAL_TOP = 4.2;
// Side piers and upper wall leave a genuine open passage at ground level.
box(
1.4,
MAIN_TOP - GROUND,
3.6,
-1.8,
(MAIN_TOP + GROUND) / 2,
0,
M.yellow
);
box(
1.4,
MAIN_TOP - GROUND,
3.6,
1.8,
(MAIN_TOP + GROUND) / 2,
0,
M.yellow
);
box(
MAIN_WIDTH,
MAIN_TOP - PORTAL_TOP,
3.6,
0,
(MAIN_TOP + PORTAL_TOP) / 2,
0,
M.yellow
);
// Dark rear wall gives depth to the open ground-floor passage.
box(4.8, 3.0, 0.22, 0, 2.7, -1.55, M.darker);
// Stone portal surround.
box(0.22, 3.0, 0.22, -1.08, 2.7, 1.91, M.trim);
box(0.22, 3.0, 0.22, 1.08, 2.7, 1.91, M.trim);
box(2.38, 0.28, 0.25, 0, 4.08, 1.91, M.trim);
// Corner pilasters and horizontal string courses.
box(0.22, MAIN_TOP - GROUND, 0.2, -2.38, 7.25, 1.91, M.trim);
box(0.22, MAIN_TOP - GROUND, 0.2, 2.38, 7.25, 1.91, M.trim);
box(5.25, 0.16, 0.24, 0, 4.32, 1.92, M.trim);
box(5.25, 0.16, 0.24, 0, 9.42, 1.92, M.trim);
box(5.3, 0.16, 0.26, 0, 13.3, 1.92, M.trim);
// Roof expressed as stepped voxel courses.
box(5.45, 0.38, 4.05, 0, 13.58, 0, M.roof);
box(4.65, 0.36, 4.2, 0, 13.94, 0, M.roofLight);
box(3.75, 0.34, 4.35, 0, 14.29, 0, M.roof);
box(2.8, 0.32, 4.5, 0, 14.61, 0, M.roofLight);
box(1.8, 0.3, 4.65, 0, 14.91, 0, M.roof);
box(0.72, 0.5, 1.0, 0.85, 14.45, -0.8, M.stoneDark);
// ------------------------------------------------------------
// Windows and balconies
// ------------------------------------------------------------
function addWindow(
x,
y,
z,
scale = 1,
shutterMat = M.shutter,
trimMat = M.trim
) {
const w = 1.48 * scale;
const h = 1.82 * scale;
box(w, h, 0.16, x, y, z, trimMat);
box(
w * 0.69,
h * 0.76,
0.08,
x,
y + 0.02,
z + 0.12,
M.dark
);
// Window mullions.
box(
0.07 * scale,
h * 0.76,
0.07,
x,
y + 0.02,
z + 0.19,
trimMat
);
box(
w * 0.69,
0.065 * scale,
0.07,
x,
y - 0.12 * scale,
z + 0.19,
trimMat
);
// Paired shutters.
box(
0.27 * scale,
h * 0.82,
0.11,
x - w * 0.39,
y,
z + 0.13,
shutterMat
);
box(
0.27 * scale,
h * 0.82,
0.11,
x + w * 0.39,
y,
z + 0.13,
shutterMat
);
// Sill.
box(
w + 0.22 * scale,
0.14 * scale,
0.34,
x,
y - h / 2,
z + 0.08,
trimMat
);
}
function addBalcony(
x,
floorY,
z,
width = 2.8,
depth = 1.18,
plant = false
) {
const frontZ = z + depth / 2;
// Stone balcony slab.
box(
width,
0.17,
depth,
x,
floorY - 0.085,
z,
M.stone
);
// Iron top rail and middle rail.
box(width, 0.08, 0.08, x, floorY + 0.88, frontZ, M.iron);
box(width, 0.06, 0.06, x, floorY + 0.44, frontZ, M.iron);
// Vertical balusters.
const bars = Math.max(5, Math.round(width / 0.34));
for (let i = 0; i <= bars; i++) {
const bx = x - width / 2 + (width * i) / bars;
box(
0.045,
0.88,
0.045,
bx,
floorY + 0.44,
frontZ,
M.iron
);
}
// Side rails.
for (const side of [-1, 1]) {
box(
0.055,
0.88,
depth,
x + side * width * 0.49,
floorY + 0.44,
z,
M.iron
);
}
if (plant) {
const px = x + width * 0.32;
box(0.42, 0.42, 0.42, px, floorY + 0.22, z + 0.12, M.pot);
box(0.48, 0.38, 0.42, px, floorY + 0.57, z + 0.12, M.leaf);
box(0.3, 0.36, 0.32, px - 0.16, floorY + 0.78, z + 0.12, M.leafLight);
}
}
// A single vertical stack emphasizes the narrow facade.
const mainWindowRows = [5.2, 7.9, 10.45, 12.25];
mainWindowRows.forEach((y, index) => {
addWindow(0, y, 1.98, 1, index % 2 ? M.shutter : M.shutterBlue);
});
// The featured balcony sits immediately below the upper window.
addBalcony(0, 7.03, 2.43, 3.15, 1.28, true);
// Small ground-floor shop panels in the side piers.
addWindow(-1.8, 2.75, 1.98, 0.72, M.shutter, M.trim);
addWindow(1.8, 2.75, 1.98, 0.72, M.shutterBlue, M.trim);
// Warm light inside the public passage.
box(0.32, 0.42, 0.22, 0, 3.45, -1.16, M.lamp);
const passageLight = new THREE.PointLight(0xffb75f, 8, 7, 2);
passageLight.position.set(0, 3.15, -0.75);
scene.add(passageLight);
// Small cafe awning beside the passage.
box(1.65, 0.13, 0.78, 1.8, 3.63, 2.16, M.red);
for (let i = 0; i < 5; i++) {
box(
0.22,
0.15,
0.8,
1.08 + i * 0.36,
3.6,
2.18,
M.white
);
}
// ------------------------------------------------------------
// Neighboring Ribeira houses
// ------------------------------------------------------------
function addFacadeBlock({
x,
width,
height,
wallMat,
trimMat,
rows = 4,
columns = 2,
shutterMat = M.shutter,
balconyFloor = null,
}) {
const top = GROUND + height;
box(
width,
height,
3.6,
x,
GROUND + height / 2,
0,
wallMat
);
// Eaves and stepped roof.
box(width + 0.28, 0.18, 3.9, x, top + 0.06, 0, trimMat);
box(width + 0.42, 0.36, 4.0, x, top + 0.31, 0, M.roof);
box(width - 0.35, 0.34, 4.15, x, top + 0.65, 0, M.roofLight);
box(width - 1.2, 0.32, 4.3, x, top + 0.97, 0, M.roof);
const usable = height - 4.3;
for (let row = 0; row < rows; row++) {
const y = 4.8 + (rows === 1 ? 0 : (usable * row) / (rows - 1));
for (let col = 0; col < columns; col++) {
const spread = width / (columns + 0.65);
const wx =
x + (col - (columns - 1) / 2) * spread;
addWindow(
wx,
y,
1.98,
Math.min(1, width / 6.2),
row % 2 ? shutterMat : M.shutterBlue,
trimMat
);
if (
balconyFloor !== null &&
row === 1 &&
col === Math.floor(columns / 2)
) {
addBalcony(wx, y - 0.86, 2.36, spread * 0.92, 1.05, row % 2 === 0);
}
}
}
// Ground-level doors and recessed shop fronts.
const doorX = x - width * 0.23;
box(0.95, 2.25, 0.12, doorX, 2.34, 1.91, M.darker);
box(1.12, 0.16, 0.28, doorX, 3.53, 1.98, trimMat);
const shopX = x + width * 0.25;
box(1.35, 1.7, 0.12, shopX, 2.28, 1.92, M.dark);
box(1.55, 0.16, 0.26, shopX, 3.2, 1.98, trimMat);
}
addFacadeBlock({
x: -6.05,
width: 6.7,
height: 11.45,
wallMat: M.coral,
trimMat: M.cream,
rows: 4,
columns: 2,
shutterMat: M.shutterBlue,
balconyFloor: 6.3,
});
addFacadeBlock({
x: 5.95,
width: 6.45,
height: 12.05,
wallMat: M.teal,
trimMat: M.paleBlue,
rows: 4,
columns: 2,
shutterMat: M.shutter,
balconyFloor: 6.2,
});
addFacadeBlock({
x: -13.0,
width: 5.1,
height: 9.25,
wallMat: M.mutedBlue,
trimMat: M.stone,
rows: 3,
columns: 2,
shutterMat: M.shutterBlue,
});
addFacadeBlock({
x: 12.4,
width: 5.7,
height: 9.8,
wallMat: M.peach,
trimMat: M.cream,
rows: 3,
columns: 2,
shutterMat: M.shutter,
});
// ------------------------------------------------------------
// Steps descending from the public route to the lower quay
// ------------------------------------------------------------
const stairWidth = 3.4;
const stepCount = 8;
const treadDepth = 0.48;
for (let i = 0; i < stepCount; i++) {
const top = 1.2 - (i + 1) * 0.15;
const bottom = -0.86;
const z = 7.0 + (i + 0.5) * treadDepth;
box(
stairWidth,
top - bottom,
treadDepth + 0.015,
0,
(top + bottom) / 2,
z,
i % 2 ? M.stone : M.quayLight
);
}
// Lower waterside quay landing.
box(
7.2,
0.78,
2.9,
0,
-0.48,
12.42,
M.quay
);
box(
7.35,
0.12,
3.0,
0,
-0.03,
12.42,
M.stone
);
// Stone edge around the lower landing.
for (const side of [-1, 1]) {
box(
0.28,
0.7,
2.9,
side * 3.5,
-0.43,
12.42,
M.stoneDark
);
}
// Upper quay bollards.
for (const x of [-12, -8, -4, 4, 8, 12]) {
box(0.28, 0.8, 0.28, x, 1.7, 6.55, M.iron);
box(0.42, 0.16, 0.42, x, 2.12, 6.55, M.iron);
}
// Lower mooring posts.
for (const x of [-3.05, 3.05]) {
box(0.34, 1.25, 0.34, x, 0.26, 13.48, M.wood);
box(0.42, 0.15, 0.42, x, 0.9, 13.48, M.iron);
}
// ------------------------------------------------------------
// Distant townscape
// ------------------------------------------------------------
const distantColors = [
material(0x87999a),
material(0x9aa2a0),
material(0x9e8d83),
material(0x7f9298),
];
for (let i = 0; i < 12; i++) {
const x = -24 + i * 4.4;
const h = 5.5 + ((i * 7) % 5);
box(
3.6 + (i % 3) * 0.4,
h,
3,
x,
GROUND + h / 2,
-8.2 - (i % 2),
distantColors[i % distantColors.length]
);
}
// A compact old-town tower behind the row.
box(2.25, 11.8, 2.3, -11.3, 7.1, -5.2, M.cream);
box(2.45, 0.2, 2.5, -11.3, 13.05, -5.2, M.trim);
box(1.8, 0.42, 1.9, -11.3, 13.35, -5.2, M.roof);
box(1.15, 0.38, 1.3, -11.3, 13.73, -5.2, M.roofLight);
box(0.65, 0.7, 0.12, -11.3, 11.7, -3.98, M.dark);
// ------------------------------------------------------------
// Small voxel people establish the scale of the public route
// ------------------------------------------------------------
function addPerson(x, z, shirtMat, scale = 1) {
const person = groupAt(x, 1.25, z);
box(0.17 * scale, 0.54 * scale, 0.18 * scale,
-0.13 * scale, 0.27 * scale, 0, M.darker, person);
box(0.17 * scale, 0.54 * scale, 0.18 * scale,
0.13 * scale, 0.27 * scale, 0, M.darker, person);
box(0.54 * scale, 0.65 * scale, 0.34 * scale,
0, 0.86 * scale, 0, shirtMat, person);
box(0.36 * scale, 0.36 * scale, 0.34 * scale,
0, 1.37 * scale, 0, M.skin, person);
box(0.38 * scale, 0.14 * scale, 0.36 * scale,
0, 1.54 * scale, 0, M.darker, person);
}
addPerson(-4.6, 4.5, M.red, 1);
addPerson(-3.9, 4.7, M.blue, 0.9);
addPerson(5.9, 3.1, M.cream, 1.05);
addPerson(8.2, 5.2, M.shutter, 0.86);
addPerson(0.9, 2.8, M.yellowLight, 0.92);
// ------------------------------------------------------------
// Voxel boat and river reflections
// ------------------------------------------------------------
function addBoat(x, z, scale = 1, rotation = 0) {
const boat = groupAt(x, -0.35, z);
boat.rotation.y = rotation;
boat.scale.setScalar(scale);
box(2.8, 0.48, 1.15, 0, 0, 0, M.wood, boat);
box(0.75, 0.4, 0.9, 1.55, 0.04, 0, M.wood, boat);
box(2.35, 0.16, 1.02, -0.15, 0.29, 0, M.cream, boat);
box(1.1, 0.72, 0.8, -0.35, 0.65, 0, M.white, boat);
box(0.7, 0.38, 0.03, -0.35, 0.75, 0.42, M.dark, boat);
box(0.08, 2.25, 0.08, 0.72, 1.28, 0, M.wood, boat);
box(0.9, 0.05, 0.05, 0.35, 1.95, 0, M.red, boat);
}
addBoat(-6.5, 18.3, 1, -0.12);
addBoat(9.2, 22.5, 0.68, 0.2);
const reflections = [];
for (let i = 0; i < 17; i++) {
const x = -18 + i * 2.3;
const z = 15.2 + ((i * 7) % 9) * 1.15;
const width = 1.1 + ((i * 3) % 5) * 0.42;
const mat = new THREE.MeshBasicMaterial({
color: [0xd8a15c, 0x7aa0a0, 0xb96955][i % 3],
transparent: true,
opacity: 0.16,
depthWrite: false,
});
const reflection = box(
width,
0.015,
0.16,
x,
-0.805,
z,
mat,
scene,
false
);
reflections.push(reflection);
}
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xd9f1ff, 0x6f5a4a, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe1ad, 3.4);
sun.position.set(-12, 24, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -25;
sun.shadow.camera.right = 25;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 65;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9dc4d2, 0.75);
fill.position.set(16, 9, -12);
scene.add(fill);
// ------------------------------------------------------------
// Custom camera controls — no Three.js addons are used.
// ------------------------------------------------------------
const orbit = {
theta: 0.16,
phi: 1.19,
radius: 30,
target: new THREE.Vector3(0, 5.4, 1.5),
};
const desired = {
theta: orbit.theta,
phi: orbit.phi,
radius: orbit.radius,
target: orbit.target.clone(),
};
const home = {
theta: 0.16,
phi: 1.19,
radius: 30,
target: new THREE.Vector3(0, 5.4, 1.5),
};
const pointers = new Map();
let previousPinchDistance = 0;
let panning = false;
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function updateCamera(instant = false) {
const smoothing = instant ? 1 : 0.13;
orbit.theta += (desired.theta - orbit.theta) * smoothing;
orbit.phi += (desired.phi - orbit.phi) * smoothing;
orbit.radius += (desired.radius - orbit.radius) * smoothing;
orbit.target.lerp(desired.target, smoothing);
const sinPhi = Math.sin(orbit.phi);
const cosPhi = Math.cos(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * cosPhi,
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
function panCamera(dx, dy) {
const right = new THREE.Vector3(1, 0, 0).applyQuaternion(camera.quaternion);
const up = new THREE.Vector3(0, 1, 0).applyQuaternion(camera.quaternion);
const panScale = orbit.radius * 0.0015;
desired.target
.addScaledVector(right, -dx * panScale)
.addScaledVector(up, dy * panScale);
desired.target.x = clamp(desired.target.x, -13, 13);
desired.target.y = clamp(desired.target.y, 1.5, 10.5);
desired.target.z = clamp(desired.target.z, -4, 8);
}
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
});
panning = event.shiftKey || event.button === 2;
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
previousPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
});
if (pointers.size === 1) {
if (panning) {
panCamera(dx, dy);
} else {
desired.theta -= dx * 0.005;
desired.phi = clamp(desired.phi + dy * 0.004, 0.48, 1.48);
}
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (previousPinchDistance > 0) {
desired.radius = clamp(
desired.radius * (previousPinchDistance / distance),
12,
48
);
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = 0;
panning = false;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
12,
48
);
},
{ passive: false }
);
function resetView() {
desired.theta = home.theta;
desired.phi = home.phi;
desired.radius = home.radius;
desired.target.copy(home.target);
}
canvas.addEventListener("dblclick", resetView);
document.querySelector("#reset").addEventListener("click", resetView);
addEventListener("keydown", (event) => {
switch (event.key.toLowerCase()) {
case "arrowleft":
desired.theta += 0.08;
break;
case "arrowright":
desired.theta -= 0.08;
break;
case "arrowup":
desired.phi = clamp(desired.phi - 0.06, 0.48, 1.48);
break;
case "arrowdown":
desired.phi = clamp(desired.phi + 0.06, 0.48, 1.48);
break;
case "+":
case "=":
desired.radius = clamp(desired.radius - 2, 12, 48);
break;
case "-":
desired.radius = clamp(desired.radius + 2, 12, 48);
break;
case "r":
resetView();
break;
}
});
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateCamera(true);
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
reflections.forEach((reflection, index) => {
reflection.position.x += Math.sin(elapsed * 0.35 + index) * 0.0015;
reflection.material.opacity =
0.13 + Math.sin(elapsed * 0.55 + index * 1.7) * 0.035;
});
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```