Prompt specification
Build or customize the Three.js 3D scene "Rift fault-step block with offset layers" 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: Rift fault-step block with offset layers
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rift-fault-step-block-with-offset-layers-x3061
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring an East African Rift fault block. Build a teaching block with rock layers offset across a fault plane. Show relative displacement clearly and identify it as a simplified model of one rift-related structure. Stylized solid forms.
Working Three.js Starter (rift-fault-step-block-with-offset-layers-x3061):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>East African Rift — Fault Block Teaching Model</title>
<style>
:root {
--ink: #282824;
--muted: #65665e;
--paper: rgba(250, 247, 239, 0.92);
--line: rgba(50, 49, 43, 0.16);
--green: #477a58;
--orange: #b85d38;
--gold: #e49a2e;
--red: #a8483f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eee8dc;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#scene canvas:active {
cursor: grabbing;
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
overflow: hidden;
pointer-events: none;
}
.scene-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -100%);
display: flex;
flex-direction: column;
gap: 2px;
min-width: 124px;
padding: 8px 10px;
border: 1px solid rgba(40, 40, 36, 0.18);
border-radius: 8px;
background: rgba(250, 247, 239, 0.94);
box-shadow: 0 7px 22px rgba(58, 49, 39, 0.12);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
transition: opacity 120ms ease;
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 13px;
background: rgba(40, 40, 36, 0.45);
}
.scene-label strong {
font-size: 10px;
line-height: 1.1;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.scene-label span {
color: var(--muted);
font-size: 11px;
line-height: 1.25;
}
.scene-label.footwall {
border-color: rgba(71, 122, 88, 0.42);
}
.scene-label.footwall strong {
color: var(--green);
}
.scene-label.hanging {
border-color: rgba(184, 93, 56, 0.42);
}
.scene-label.hanging strong {
color: var(--orange);
}
.scene-label.fault strong {
color: #9c6515;
}
.scene-label.offset {
border-color: rgba(228, 154, 46, 0.58);
background: rgba(255, 246, 222, 0.95);
}
.scene-label.offset strong {
color: #a56812;
}
.scene-label.pin {
min-width: 0;
width: 25px;
height: 25px;
padding: 0;
align-items: center;
justify-content: center;
transform: translate(-50%, -50%);
border: 2px solid #8c5b18;
border-radius: 50%;
background: #f6c35d;
color: #53370f;
font-size: 11px;
font-weight: 800;
box-shadow: 0 3px 9px rgba(58, 49, 39, 0.18);
}
.scene-label.pin::after {
display: none;
}
#intro {
position: fixed;
z-index: 5;
top: 24px;
left: 24px;
width: min(360px, calc(100vw - 48px));
padding: 19px 20px 17px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--paper);
box-shadow: 0 14px 45px rgba(58, 49, 39, 0.12);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
margin: 0 0 8px;
color: #7c664e;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(28px, 3vw, 38px);
font-weight: 500;
line-height: 1;
letter-spacing: -0.025em;
}
.subtitle {
margin: 7px 0 0;
color: #77766d;
font-size: 12px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.intro-copy {
margin: 13px 0 14px;
color: #53544e;
font-size: 12.5px;
line-height: 1.5;
}
.badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-bottom: 12px;
padding: 6px 9px;
border: 1px solid rgba(184, 93, 56, 0.27);
border-radius: 999px;
background: rgba(184, 93, 56, 0.08);
color: #8b4b33;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.badge::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--orange);
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px 12px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
min-width: 0;
color: #5c5c55;
font-size: 10.5px;
line-height: 1.2;
}
.swatch {
flex: 0 0 auto;
width: 20px;
height: 8px;
border-radius: 2px;
background: var(--red);
}
.swatch.fault {
height: 3px;
background: var(--gold);
}
.swatch.up {
background: var(--green);
}
.swatch.down {
background: var(--orange);
}
#reset {
position: fixed;
z-index: 7;
top: 24px;
right: 24px;
padding: 9px 13px;
border: 1px solid rgba(40, 40, 36, 0.2);
border-radius: 9px;
background: rgba(250, 247, 239, 0.9);
color: #454640;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
box-shadow: 0 6px 20px rgba(58, 49, 39, 0.1);
backdrop-filter: blur(8px);
}
#reset:hover {
background: #fffdf7;
color: #22231f;
}
#hint {
position: fixed;
z-index: 5;
right: 24px;
bottom: 20px;
padding: 7px 10px;
border: 1px solid rgba(40, 40, 36, 0.12);
border-radius: 999px;
background: rgba(250, 247, 239, 0.78);
color: #6e6c64;
font-size: 10px;
letter-spacing: 0.03em;
pointer-events: none;
backdrop-filter: blur(6px);
}
@media (max-width: 700px) {
#intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 14px;
}
h1 {
font-size: 25px;
}
.subtitle {
font-size: 10px;
}
.intro-copy,
.legend {
display: none;
}
.badge {
margin: 9px 0 0;
font-size: 8px;
}
#reset {
top: auto;
right: 12px;
bottom: 50px;
}
#hint {
right: 12px;
bottom: 12px;
}
.scene-label {
min-width: 105px;
padding: 6px 8px;
}
.scene-label strong {
font-size: 8px;
}
.scene-label span {
font-size: 9px;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive 3D teaching model of a rift-related normal fault"></main>
<div id="labels" aria-hidden="true"></div>
<section id="intro">
<p class="eyebrow">Geology teaching model</p>
<h1>East African Rift</h1>
<p class="subtitle">Fault-block cutaway</p>
<div class="badge">Simplified rift-related normal fault</div>
<p class="intro-copy">
Once-continuous rock beds are cut and vertically offset. The left
<strong>footwall</strong> is relatively uplifted, while the right
<strong>hanging wall</strong> is downthrown. Matching “A” markers show
displacement along the inclined fault plane.
</p>
<div class="legend" aria-label="Model legend">
<div class="legend-item">
<span class="swatch"></span>
<span>Bed A — matching layer</span>
</div>
<div class="legend-item">
<span class="swatch fault"></span>
<span>Fault plane</span>
</div>
<div class="legend-item">
<span class="swatch up"></span>
<span>Relative uplift</span>
</div>
<div class="legend-item">
<span class="swatch down"></span>
<span>Relative downthrow</span>
</div>
</div>
</section>
<button id="reset" type="button" title="Reset the camera view">↺ Reset view</button>
<div id="hint">Drag to orbit · Scroll or pinch to zoom · R 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";
const sceneElement = document.getElementById("scene");
const labelsElement = document.getElementById("labels");
const resetButton = document.getElementById("reset");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee8dc);
scene.fog = new THREE.Fog(0xeee8dc, 18, 35);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneElement.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
36,
window.innerWidth / window.innerHeight,
0.1,
80
);
// Model dimensions and fault geometry.
const WIDTH = 8.0;
const DEPTH = 3.6;
const LEFT_BOTTOM = -2.4;
const LEFT_TOP = 3.2;
const RIGHT_TOP = 2.2;
// The fault dips toward the left in this cross-section.
const faultX = (y) => 0.34 * y - 0.12;
const leftBoundaries = [
-2.4, -1.35, -0.3, 0.75, 1.8, 3.2
];
// The right block is displaced downward by one model unit.
const rightBoundaries = leftBoundaries.map((y) => y - 1.0);
const stratumColors = [
0x493d3a, // Deep basalt
0x9b5d43, // Rust-brown bed
0xa8483f, // Red marker bed A
0xe5c58a, // Pale limestone
0xc47a3d, // Ochre sandstone
0x7d7559 // Upper rock
];
// Lighting.
scene.add(new THREE.HemisphereLight(0xfff8e9, 0x776e60, 1.7));
const keyLight = new THREE.DirectionalLight(0xfff4df, 3.0);
keyLight.position.set(-6, 10, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb9d4df, 1.0);
fillLight.position.set(8, 3, -7);
scene.add(fillLight);
// Ground plane.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0xeee8dc,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -2.48;
ground.receiveShadow = true;
scene.add(ground);
function makeStratumMaterials(color) {
const front = new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0
});
const side = new THREE.MeshStandardMaterial({
color: new THREE.Color(color).multiplyScalar(0.70),
roughness: 0.95,
metalness: 0
});
return [front, side];
}
function makeRegionShape(side, a, b) {
const shape = new THREE.Shape();
if (side === "left") {
shape.moveTo(-WIDTH, a);
shape.lineTo(faultX(a), a);
shape.lineTo(faultX(b), b);
shape.lineTo(-WIDTH, b);
} else {
shape.moveTo(faultX(a), a);
shape.lineTo(WIDTH, a);
shape.lineTo(WIDTH, b);
shape.lineTo(faultX(b), b);
}
shape.closePath();
return shape;
}
function addExtrudedRegion(side, a, b, materials) {
const geometry = new THREE.ExtrudeGeometry(
makeRegionShape(side, a, b),
{
depth: DEPTH,
bevelEnabled: false,
steps: 1,
curveSegments: 1
}
);
const mesh = new THREE.Mesh(geometry, materials);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function addStrata(side, boundaries, bottom, top) {
for (let i = 0; i < stratumColors.length; i++) {
const a = Math.max(bottom, boundaries[i]);
const b = Math.min(top, boundaries[i + 1]);
if (b - a > 0.001) {
addExtrudedRegion(
side,
a,
b,
makeStratumMaterials(stratumColors[i])
);
}
}
}
addStrata("left", leftBoundaries, LEFT_BOTTOM, LEFT_TOP);
addStrata("right", rightBoundaries, LEFT_BOTTOM, RIGHT_TOP);
// Thin green surface caps make the two block tops read as land surfaces.
const capFront = new THREE.MeshStandardMaterial({
color: 0x829273,
roughness: 0.95
});
const capSide = new THREE.MeshStandardMaterial({
color: 0x647258,
roughness: 1
});
addExtrudedRegion(
"left",
LEFT_TOP,
LEFT_TOP + 0.08,
[capFront, capSide]
);
addExtrudedRegion(
"right",
RIGHT_TOP,
RIGHT_TOP + 0.08,
[capFront, capSide]
);
function cylinderBetween(a, b, radius, material, radialSegments = 10) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments
),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
scene.add(mesh);
return mesh;
}
// Fine horizontal contacts help the offset beds remain legible.
const contactMaterial = new THREE.MeshStandardMaterial({
color: 0x3f332d,
transparent: true,
opacity: 0.38,
roughness: 1
});
function addContact(side, y) {
const z = DEPTH + 0.017;
if (side === "left") {
cylinderBetween(
new THREE.Vector3(-WIDTH, y, z),
new THREE.Vector3(faultX(y), y, z),
0.009,
contactMaterial,
6
);
} else {
cylinderBetween(
new THREE.Vector3(faultX(y), y, z),
new THREE.Vector3(WIDTH, y, z),
0.009,
contactMaterial,
6
);
}
}
leftBoundaries.forEach((y) => addContact("left", y));
rightBoundaries
.filter((y) => y >= LEFT_BOTTOM && y <= RIGHT_TOP)
.forEach((y) => addContact("right", y));
const faultMaterial = new THREE.MeshStandardMaterial({
color: 0xe49a2e,
emissive: 0x4b2600,
emissiveIntensity: 0.15,
roughness: 0.55,
metalness: 0.02
});
// Exposed upper part of the actual inclined fault surface.
const faultPlaneGeometry = new THREE.BufferGeometry();
faultPlaneGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([
faultX(RIGHT_TOP), RIGHT_TOP, 0,
faultX(LEFT_TOP), LEFT_TOP, 0,
faultX(LEFT_TOP), LEFT_TOP, DEPTH,
faultX(RIGHT_TOP), RIGHT_TOP, DEPTH
], 3)
);
faultPlaneGeometry.setIndex([
0, 1, 2,
0, 2, 3
]);
faultPlaneGeometry.computeVertexNormals();
const exposedFaultPlane = new THREE.Mesh(
faultPlaneGeometry,
new THREE.MeshStandardMaterial({
color: 0xc67b19,
roughness: 0.72,
side: THREE.DoubleSide
})
);
exposedFaultPlane.receiveShadow = true;
scene.add(exposedFaultPlane);
// Bright trace where the fault intersects the cut face.
cylinderBetween(
new THREE.Vector3(
faultX(LEFT_BOTTOM),
LEFT_BOTTOM,
DEPTH + 0.045
),
new THREE.Vector3(
faultX(LEFT_TOP),
LEFT_TOP,
DEPTH + 0.045
),
0.026,
faultMaterial,
10
);
function makeArrow(start, end, color) {
const group = new THREE.Group();
const direction = new THREE.Vector3().subVectors(end, start).normalize();
const headLength = 0.3;
const shaftEnd = end.clone().addScaledVector(
direction,
-headLength * 0.75
);
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.08,
roughness: 0.55
});
const shaft = cylinderBetween(
start,
shaftEnd,
0.043,
material,
12
);
shaft.castShadow = true;
group.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.14, headLength, 16),
material
);
head.position.copy(end).addScaledVector(direction, -headLength * 0.5);
head.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction
);
head.castShadow = true;
group.add(head);
scene.add(group);
return group;
}
// Relative motion indicators.
makeArrow(
new THREE.Vector3(-2.45, 0.6, DEPTH + 0.3),
new THREE.Vector3(-2.45, 3.0, DEPTH + 0.3),
0x477a58
);
makeArrow(
new THREE.Vector3(2.8, 2.75, DEPTH + 0.3),
new THREE.Vector3(2.8, 0.0, DEPTH + 0.3),
0xb85d38
);
// Matching Bed A markers.
const leftMarkerY = (leftBoundaries[2] + leftBoundaries[3]) * 0.5;
const rightMarkerY = (rightBoundaries[2] + rightBoundaries[3]) * 0.5;
const markerPositions = [
new THREE.Vector3(-0.17, leftMarkerY, DEPTH + 0.035),
new THREE.Vector3(-0.17, rightMarkerY, DEPTH + 0.035)
];
const markerRingMaterial = new THREE.MeshStandardMaterial({
color: 0xf2b63f,
emissive: 0x704000,
emissiveIntensity: 0.12,
roughness: 0.55,
side: THREE.DoubleSide
});
const markerCenterMaterial = new THREE.MeshStandardMaterial({
color: 0xfff4cf,
roughness: 0.7,
side: THREE.DoubleSide
});
markerPositions.forEach((position) => {
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.09, 0.13, 24),
markerRingMaterial
);
ring.position.copy(position);
const center = new THREE.Mesh(
new THREE.CircleGeometry(0.067, 24),
markerCenterMaterial
);
center.position.copy(position);
center.position.z += 0.003;
scene.add(ring, center);
});
// Vertical offset bracket between the centers of the matching markers.
const bracketX = -0.9;
const bracketZ = DEPTH + 0.12;
cylinderBetween(
new THREE.Vector3(bracketX, rightMarkerY, bracketZ),
new THREE.Vector3(bracketX, leftMarkerY, bracketZ),
0.013,
faultMaterial,
8
);
[rightMarkerY, leftMarkerY].forEach((y) => {
cylinderBetween(
new THREE.Vector3(bracketX - 0.18, y, bracketZ),
new THREE.Vector3(bracketX + 0.18, y, bracketZ),
0.013,
faultMaterial,
8
);
});
// DOM labels projected from 3D anchor points.
const labels = [];
function addSceneLabel(anchor, html, variant = "") {
const element = document.createElement("div");
element.className = `scene-label ${variant}`.trim();
element.innerHTML = html;
labelsElement.appendChild(element);
const label = {
element,
anchor: anchor.clone()
};
labels.push(label);
return label;
}
addSceneLabel(
new THREE.Vector3(-2.45, 3.35, DEPTH + 0.35),
"<strong>Footwall</strong><span>Relatively uplifted</span>",
"footwall"
);
addSceneLabel(
new THREE.Vector3(2.8, 3.05, DEPTH + 0.35),
"<strong>Hanging wall</strong><span>Relatively downthrown</span>",
"hanging"
);
addSceneLabel(
new THREE.Vector3(faultX(2.55), 2.55, DEPTH + 0.1),
"<strong>Fault plane</strong><span>Inclined normal fault</span>",
"fault"
);
addSceneLabel(
new THREE.Vector3(bracketX, leftMarkerY + 0.08, bracketZ),
"<strong>Vertical offset</strong><span>≈ 1 model unit</span>",
"offset"
);
addSceneLabel(
markerPositions[0],
"A",
"pin"
);
addSceneLabel(
markerPositions[1],
"A",
"pin"
);
// Custom camera controls: no addons or examples modules are used.
const orbit = {
target: new THREE.Vector3(0.1, -0.15, 1.55),
theta: 0.72,
phi: 1.08,
radius: window.innerWidth < 700 ? 21.0 : 14.5,
desiredTheta: 0.72,
desiredPhi: 1.08,
desiredRadius: window.innerWidth < 700 ? 21.0 : 14.5
};
function resetView() {
orbit.desiredTheta = 0.72;
orbit.desiredPhi = 1.08;
orbit.desiredRadius =
window.innerWidth < 700 ? 21.0 : 14.5;
}
resetButton.addEventListener("click", resetView);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetView();
}
});
const pointers = new Map();
let previousPinchDistance = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size < 2) {
previousPinchDistance = 0;
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) {
return;
}
const previous = pointers.get(event.pointerId);
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) {
orbit.desiredTheta -= dx * 0.006;
orbit.desiredPhi = THREE.MathUtils.clamp(
orbit.desiredPhi + dy * 0.005,
0.48,
1.46
);
} else if (pointers.size === 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (previousPinchDistance > 0) {
orbit.desiredRadius = THREE.MathUtils.clamp(
orbit.desiredRadius *
(previousPinchDistance / distance),
8,
28
);
}
previousPinchDistance = distance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
previousPinchDistance = 0;
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.desiredRadius = THREE.MathUtils.clamp(
orbit.desiredRadius * Math.exp(event.deltaY * 0.001),
8,
28
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
function updateCamera() {
orbit.theta +=
(orbit.desiredTheta - orbit.theta) * 0.1;
orbit.phi +=
(orbit.desiredPhi - orbit.phi) * 0.1;
orbit.radius +=
(orbit.desiredRadius - orbit.radius) * 0.1;
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 updateLabels() {
const width = canvas.clientWidth;
const height = canvas.clientHeight;
labels.forEach(({ element, anchor }) => {
const projected = anchor.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
element.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
element.style.visibility = visible ? "visible" : "hidden";
});
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
}
window.addEventListener("resize", onResize);
function animate() {
requestAnimationFrame(animate);
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```