Prompt specification
Build or customize the Three.js 3D scene "Beijing courtyard gate and grey-brick wall" 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: Beijing courtyard gate and grey-brick wall
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/beijing-courtyard-gate-and-grey-brick-wall-x2969
Scene Description Prompt:
A low-poly Beijing hutong gate, with grey brick, roof edge, threshold, and courtyard beyond. One HTML file with Three.js from cdnjs.
Working Three.js Starter (beijing-courtyard-gate-and-grey-brick-wall-x2969):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Beijing Hutong Gate</title>
<style>
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8d1c4;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.title {
position: fixed;
top: 28px;
left: 30px;
z-index: 2;
pointer-events: none;
color: #2f2924;
font-family: Georgia, "Times New Roman", serif;
user-select: none;
}
.title::before {
content: "";
display: block;
width: 42px;
height: 3px;
margin-bottom: 12px;
background: #9b3027;
}
.title h1 {
margin: 0;
font-size: clamp(20px, 2.4vw, 34px);
font-weight: 500;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.title p {
margin: 7px 0 0;
color: #6e6256;
font-size: 11px;
letter-spacing: 0.28em;
text-transform: uppercase;
}
.hint {
position: fixed;
right: 24px;
bottom: 20px;
z-index: 2;
pointer-events: none;
color: rgba(55, 47, 39, 0.72);
font: 11px/1.5 system-ui, sans-serif;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 600px) {
.title {
top: 20px;
left: 20px;
}
.hint {
right: 16px;
bottom: 14px;
font-size: 9px;
}
}
</style>
</head>
<body>
<div class="title">
<h1>Hutong Gate</h1>
<p>Beijing · Courtyard Study</p>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene, and camera
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly Beijing hutong gate"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8d1c4);
scene.fog = new THREE.Fog(0xd8d1c4, 17, 35);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
60
);
const target = new THREE.Vector3(0, 1.45, -0.45);
const defaultView = {
theta: 0.055,
phi: 1.38,
radius: 11.8
};
const orbit = { ...defaultView };
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
target.y + orbit.radius * Math.cos(orbit.phi),
target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(target);
}
updateCamera();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xe8eff2,
0x685a4d,
2.1
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffeed2, 3.1);
sun.position.set(-5.5, 10, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 32;
sun.shadow.bias = -0.0004;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const courtyardFill = new THREE.DirectionalLight(0xc9dce3, 0.85);
courtyardFill.position.set(5, 6, -7);
scene.add(courtyardFill);
// ------------------------------------------------------------
// Materials and helpers
// ------------------------------------------------------------
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.9,
metalness: 0,
flatShading: true,
...options
});
}
const materials = {
wall: material(0x747875),
mortar: material(0x555a59),
stone: material(0x8a8981),
darkStone: material(0x656661),
roof: material(0x41494b),
roofEdge: material(0x30383a),
wood: material(0x64251f),
darkWood: material(0x351713),
red: material(0x9d2e25),
redDark: material(0x681b17),
gold: material(0xb68a43, {
metalness: 0.3,
roughness: 0.55
}),
black: material(0x181918),
paver: material(0xffffff),
plaster: material(0xb8aa91),
foliage: material(0x596841),
foliageLight: material(0x718052),
trunk: material(0x5f4632)
};
function addBox(
width,
height,
depth,
x,
y,
z,
mat,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
radiusTop,
radiusBottom,
height,
radialSegments,
x,
y,
z,
mat,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
radialSegments
),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Paving
// ------------------------------------------------------------
function addPaving(centerX, centerZ, width, depth, seed = 1) {
let randomState = seed;
const random = () => {
randomState =
(randomState * 16807) % 2147483647;
return (randomState - 1) / 2147483646;
};
addBox(
width,
0.06,
depth,
centerX,
-0.04,
centerZ,
materials.mortar
);
const columns = Math.floor(width / 0.72);
const rows = Math.floor(depth / 0.72);
const count = columns * rows;
const pavers = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.675, 0.038, 0.675),
materials.paver,
count
);
const dummy = new THREE.Object3D();
const color = new THREE.Color();
let index = 0;
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
const x =
centerX -
width / 2 +
0.36 +
column * 0.72 +
(row % 2 ? 0.035 : 0);
const z =
centerZ -
depth / 2 +
0.36 +
row * 0.72;
dummy.position.set(x, 0.015, z);
dummy.rotation.y = (random() - 0.5) * 0.025;
dummy.updateMatrix();
pavers.setMatrixAt(index, dummy.matrix);
const shade = 0.59 + random() * 0.13;
color.setHSL(
0.11,
0.055 + random() * 0.035,
shade
);
pavers.setColorAt(index, color);
index++;
}
}
pavers.receiveShadow = true;
pavers.instanceMatrix.needsUpdate = true;
if (pavers.instanceColor) {
pavers.instanceColor.needsUpdate = true;
}
scene.add(pavers);
}
addPaving(0, -6, 14, 12, 19);
addPaving(0, 5.2, 14, 10.4, 37);
// ------------------------------------------------------------
// Main hutong wall
// ------------------------------------------------------------
const wallWidth = 9.4;
const wallHeight = 3.25;
const wallDepth = 0.55;
const openingHalf = 1.3;
const openingHeight = 2.56;
const sideWidth = (wallWidth - openingHalf * 2) / 2;
addBox(
sideWidth,
wallHeight,
wallDepth,
-(openingHalf + sideWidth / 2),
wallHeight / 2,
0,
materials.wall
);
addBox(
sideWidth,
wallHeight,
wallDepth,
openingHalf + sideWidth / 2,
wallHeight / 2,
0,
materials.wall
);
addBox(
openingHalf * 2,
wallHeight - openingHeight,
wallDepth,
0,
openingHeight + (wallHeight - openingHeight) / 2,
0,
materials.wall
);
// Stone plinths.
addBox(
sideWidth + 0.04,
0.34,
0.7,
-(openingHalf + sideWidth / 2),
0.17,
0,
materials.darkStone
);
addBox(
sideWidth + 0.04,
0.34,
0.7,
openingHalf + sideWidth / 2,
0.17,
0,
materials.darkStone
);
addBox(
openingHalf * 2,
0.34,
0.7,
0,
0.17,
0,
materials.darkStone
);
// ------------------------------------------------------------
// Individual gray bricks on the front face
// ------------------------------------------------------------
const brickMatrices = [];
const brickColors = [];
const dummy = new THREE.Object3D();
const brickColor = new THREE.Color();
let brickSeed = 73;
function brickRandom() {
brickSeed = (brickSeed * 48271) % 2147483647;
return (brickSeed - 1) / 2147483646;
}
for (let row = 0; row < 15; row++) {
const y = 0.12 + row * 0.215;
const offset = row % 2 ? 0.25 : 0;
for (let column = -10; column <= 10; column++) {
const x = column * 0.5 + offset;
if (Math.abs(x) > 4.66) continue;
if (Math.abs(x) < openingHalf + 0.02 && y < openingHeight) {
continue;
}
dummy.position.set(x, y, 0.304);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
brickMatrices.push(dummy.matrix.clone());
const shade = 0.43 + brickRandom() * 0.12;
brickColor.setHSL(
0.53,
0.025 + brickRandom() * 0.025,
shade
);
brickColors.push(brickColor.clone());
}
}
const bricks = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.465, 0.185, 0.055),
material(0xffffff),
brickMatrices.length
);
brickMatrices.forEach((matrix, index) => {
bricks.setMatrixAt(index, matrix);
bricks.setColorAt(index, brickColors[index]);
});
bricks.castShadow = true;
bricks.receiveShadow = true;
bricks.instanceMatrix.needsUpdate = true;
if (bricks.instanceColor) {
bricks.instanceColor.needsUpdate = true;
}
scene.add(bricks);
// ------------------------------------------------------------
// Tiled roof builder
// ------------------------------------------------------------
function addTiledRoof(
centerX,
centerZ,
width,
eaveY,
ridgeY,
depth = 0.82,
ribSpacing = 0.28
) {
const rise = ridgeY - eaveY;
const slopeLength = Math.hypot(depth, rise);
const angle = Math.atan2(rise, depth);
const midY = (eaveY + ridgeY) / 2;
function addSlope(front) {
const sign = front ? 1 : -1;
const centerZOffset = sign * depth / 2;
const rotation = front ? angle : -angle;
const slab = addBox(
width + 0.55,
0.12,
slopeLength,
centerX,
midY,
centerZ + centerZOffset,
materials.roof
);
slab.rotation.x = rotation;
const direction = front
? new THREE.Vector3(0, rise, -depth).normalize()
: new THREE.Vector3(0, rise, depth).normalize();
const ribCount = Math.floor((width + 0.35) / ribSpacing);
const ribs = new THREE.InstancedMesh(
new THREE.CylinderGeometry(
0.043,
0.043,
slopeLength + 0.03,
6
),
materials.roofEdge,
ribCount
);
const quaternion = new THREE.Quaternion().setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction
);
for (let i = 0; i < ribCount; i++) {
const x =
centerX -
(ribCount - 1) * ribSpacing / 2 +
i * ribSpacing;
dummy.position.set(
x,
midY,
centerZ + centerZOffset
);
dummy.quaternion.copy(quaternion);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
ribs.setMatrixAt(i, dummy.matrix);
}
ribs.castShadow = true;
ribs.receiveShadow = true;
ribs.instanceMatrix.needsUpdate = true;
scene.add(ribs);
// Horizontal tile courses.
[-0.32, -0.08, 0.16, 0.34].forEach((localZ) => {
const yOffset = -Math.sin(rotation) * localZ;
const zOffset = Math.cos(rotation) * localZ;
const course = addBox(
width + 0.3,
0.026,
0.052,
centerX,
midY + yOffset,
centerZ + centerZOffset + zOffset,
materials.roofEdge
);
course.rotation.x = rotation;
});
}
addSlope(true);
addSlope(false);
// Main ridge.
const ridge = addCylinder(
0.105,
0.105,
width + 0.7,
8,
centerX,
ridgeY + 0.075,
centerZ,
materials.roofEdge
);
ridge.rotation.z = Math.PI / 2;
// Upturned eave edges.
function addEave(front) {
const sign = front ? 1 : -1;
const points = [];
for (let i = 0; i <= 10; i++) {
const t = i / 10;
const centered = t * 2 - 1;
const lift = Math.pow(Math.abs(centered), 7) * 0.14;
points.push(
new THREE.Vector3(
centerX - width / 2 - 0.25 + t * (width + 0.5),
eaveY + lift,
centerZ + sign * (depth / 2 + lift * 0.2)
)
);
}
const curve = new THREE.CatmullRomCurve3(points);
const edge = new THREE.Mesh(
new THREE.TubeGeometry(curve, 40, 0.055, 6, false),
materials.roofEdge
);
edge.castShadow = true;
edge.receiveShadow = true;
scene.add(edge);
}
addEave(true);
addEave(false);
}
addTiledRoof(0, 0, 9.45, 3.28, 3.94, 0.82, 0.27);
// ------------------------------------------------------------
// Gate frame, threshold, and doors
// ------------------------------------------------------------
addBox(
0.18,
2.62,
0.2,
-1.3,
1.31,
0.31,
materials.darkWood
);
addBox(
0.18,
2.62,
0.2,
1.3,
1.31,
0.31,
materials.darkWood
);
addBox(
2.88,
0.2,
0.22,
0,
2.57,
0.34,
materials.darkWood
);
// Raised threshold and worn stone step.
addBox(
2.78,
0.16,
0.88,
0,
0.08,
0.04,
materials.stone
);
addBox(
2.95,
0.11,
1.12,
0,
0.015,
0.22,
materials.darkStone
);
function addDoor(left) {
const group = new THREE.Group();
group.position.set(
left ? -1.17 : 1.17,
0.16,
0.025
);
group.rotation.y = left ? 1.04 : -1.04;
scene.add(group);
const direction = left ? 1 : -1;
const panelX = direction * 0.565;
addBox(
1.13,
2.35,
0.11,
panelX,
1.175,
0,
materials.red,
group
);
// Recessed/raised door panels.
[-0.23, 0.23].forEach((offset) => {
[0.67, 1.69].forEach((y) => {
addBox(
0.39,
0.86,
0.035,
panelX + offset,
y,
0.073,
materials.redDark,
group
);
});
});
// Door braces.
addBox(
1.02,
0.075,
0.045,
panelX,
0.33,
0.09,
materials.darkWood,
group
);
addBox(
1.02,
0.075,
0.045,
panelX,
2.02,
0.09,
materials.darkWood,
group
);
// Brass studs.
const studGeometry = new THREE.SphereGeometry(
0.042,
8,
6
);
[-0.23, 0.23].forEach((offset) => {
[0.63, 1.19, 1.75].forEach((y) => {
const stud = new THREE.Mesh(
studGeometry,
materials.gold
);
stud.position.set(
panelX + offset,
y,
0.115
);
stud.castShadow = true;
group.add(stud);
});
});
// Ring handle.
const handle = new THREE.Mesh(
new THREE.TorusGeometry(0.08, 0.018, 6, 12),
materials.gold
);
handle.position.set(
panelX + direction * 0.39,
1.2,
0.14
);
handle.castShadow = true;
group.add(handle);
}
addDoor(true);
addDoor(false);
// ------------------------------------------------------------
// Decorative plaques
// ------------------------------------------------------------
function createPlaqueTexture(characters) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 512;
const context = canvas.getContext("2d");
context.fillStyle = "#7d211c";
context.fillRect(0, 0, 128, 512);
context.strokeStyle = "#c79d58";
context.lineWidth = 5;
context.strokeRect(8, 8, 112, 496);
context.fillStyle = "#d9b56c";
context.textAlign = "center";
context.textBaseline = "middle";
context.font = "bold 65px serif";
const spacing = 430 / characters.length;
[...characters].forEach((character, index) => {
context.fillText(
character,
64,
52 + spacing * (index + 0.25)
);
});
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
4,
renderer.capabilities.getMaxAnisotropy()
);
return texture;
}
const leftPlaque = new THREE.Mesh(
new THREE.PlaneGeometry(0.25, 1.28),
new THREE.MeshStandardMaterial({
map: createPlaqueTexture("出入平安"),
roughness: 0.85
})
);
leftPlaque.position.set(-1.62, 1.56, 0.337);
leftPlaque.castShadow = true;
scene.add(leftPlaque);
const rightPlaque = leftPlaque.clone();
rightPlaque.material = leftPlaque.material.clone();
rightPlaque.material.map = createPlaqueTexture("家和万事兴");
rightPlaque.position.x = 1.62;
scene.add(rightPlaque);
// ------------------------------------------------------------
// Hanging lanterns
// ------------------------------------------------------------
function addLantern(x) {
const group = new THREE.Group();
group.position.set(x, 2.24, 0.67);
scene.add(group);
addBox(
0.035,
0.54,
0.035,
0,
0.42,
0,
materials.black,
group
);
addCylinder(
0.17,
0.17,
0.37,
10,
0,
0,
0,
materials.red,
group
);
addCylinder(
0.12,
0.12,
0.055,
10,
0,
0.205,
0,
materials.black,
group
);
addCylinder(
0.12,
0.12,
0.055,
10,
0,
-0.205,
0,
materials.black,
group
);
const tassel = new THREE.Mesh(
new THREE.ConeGeometry(0.055, 0.25, 6),
materials.gold
);
tassel.position.y = -0.35;
tassel.rotation.z = Math.PI;
tassel.castShadow = true;
group.add(tassel);
}
addLantern(-1.88);
addLantern(1.88);
// ------------------------------------------------------------
// Courtyard walls
// ------------------------------------------------------------
addBox(
0.3,
2.65,
10.8,
-5.55,
1.325,
-5.6,
materials.wall
);
addBox(
0.3,
2.65,
10.8,
5.55,
1.325,
-5.6,
materials.wall
);
addBox(
0.52,
0.16,
10.9,
-5.55,
2.72,
-5.6,
materials.roofEdge
);
addBox(
0.52,
0.16,
10.9,
5.55,
2.72,
-5.6,
materials.roofEdge
);
// ------------------------------------------------------------
// Courtyard tree
// ------------------------------------------------------------
const tree = new THREE.Group();
tree.position.set(-1.25, 0, -4.0);
scene.add(tree);
addCylinder(
0.11,
0.19,
1.85,
7,
0,
0.92,
0,
materials.trunk,
tree
);
const branch1 = addCylinder(
0.055,
0.09,
1.05,
6,
0.24,
1.55,
0,
materials.trunk,
tree
);
branch1.rotation.z = -0.78;
const branch2 = addCylinder(
0.05,
0.08,
0.95,
6,
-0.2,
1.72,
0.06,
materials.trunk,
tree
);
branch2.rotation.z = 0.82;
const leafGeometry = new THREE.IcosahedronGeometry(0.78, 0);
[
[-0.42, 2.22, 0.03, 0.85, materials.foliage],
[0.42, 2.12, 0.04, 0.78, materials.foliageLight],
[0.04, 2.65, -0.08, 0.92, materials.foliage],
[-0.05, 2.03, -0.36, 0.65, materials.foliageLight]
].forEach(([x, y, z, scale, mat]) => {
const leaf = new THREE.Mesh(leafGeometry, mat);
leaf.position.set(x, y, z);
leaf.scale.setScalar(scale);
leaf.rotation.set(
x * 0.8,
y * 0.4,
z * 0.7
);
leaf.castShadow = true;
leaf.receiveShadow = true;
tree.add(leaf);
});
addCylinder(
0.72,
0.82,
0.2,
8,
-1.25,
0.1,
-4.0,
materials.darkStone
);
// ------------------------------------------------------------
// Rear courtyard building
// ------------------------------------------------------------
const rearX = -0.5;
const rearZ = -8.35;
addBox(
7.35,
0.28,
2.35,
rearX,
0.14,
rearZ,
materials.stone
);
addBox(
6.8,
2.85,
0.48,
rearX,
1.425,
rearZ,
materials.wall
);
// Red timber columns.
[-3.25, -1.72, -0.2, 1.3, 2.55].forEach((x) => {
addCylinder(
0.115,
0.13,
2.72,
8,
x,
1.36,
rearZ + 0.34,
materials.red
);
});
addBox(
6.45,
0.14,
0.18,
rearX,
2.58,
rearZ + 0.34,
materials.redDark
);
// Central doors.
addBox(
1.35,
2.24,
0.08,
rearX,
1.12,
rearZ + 0.28,
materials.darkWood
);
addBox(
0.045,
2.2,
0.04,
rearX,
1.12,
rearZ + 0.34,
materials.redDark
);
// Side lattice windows.
[-2.48, 1.48].forEach((x) => {
addBox(
1.12,
1.16,
0.055,
x,
1.45,
rearZ + 0.285,
materials.black
);
for (let i = -2; i <= 2; i++) {
addBox(
0.035,
1.13,
0.035,
x + i * 0.2,
1.45,
rearZ + 0.33,
materials.redDark
);
}
for (let i = -2; i <= 2; i++) {
addBox(
1.08,
0.035,
0.035,
x,
1.45 + i * 0.2,
rearZ + 0.33,
materials.redDark
);
}
});
addTiledRoof(
rearX,
rearZ,
7.05,
3.18,
4.03,
1.02,
0.3
);
// ------------------------------------------------------------
// Small courtyard furnishings
// ------------------------------------------------------------
addBox(
0.58,
0.38,
0.58,
1.72,
0.19,
-3.25,
materials.darkStone
);
addBox(
0.7,
0.09,
0.7,
1.72,
0.42,
-3.25,
materials.stone
);
// A ceramic water jar.
const jar = new THREE.Mesh(
new THREE.LatheGeometry(
[
new THREE.Vector2(0.14, 0),
new THREE.Vector2(0.25, 0.12),
new THREE.Vector2(0.3, 0.3),
new THREE.Vector2(0.22, 0.52),
new THREE.Vector2(0.14, 0.61),
new THREE.Vector2(0.17, 0.66)
],
10
),
material(0x394846)
);
jar.position.set(2.8, 0, -5.0);
jar.castShadow = true;
jar.receiveShadow = true;
scene.add(jar);
// ------------------------------------------------------------
// Direct camera controls — no Three.js addons
// ------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
orbit.theta -= dx * 0.0045;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.0035,
0.72,
1.49
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (
renderer.domElement.hasPointerCapture &&
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 * Math.exp(event.deltaY * 0.001),
7.5,
18
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
Object.assign(orbit, defaultView);
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
</body>
</html>
```