Prompt specification
Build or customize the Three.js 3D scene "Salt crystals beside a coastal bowl" 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: Salt crystals beside a coastal bowl
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/salt-crystals-beside-a-coastal-bowl-x2461
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a restrained stylized solid historical display about salt and the 1930 Salt March. Center the display on a shallow bowl containing coarse geometric salt crystals, with a small coastal sand-and-water tile beside it. Add a simple 1930 date marker and a separate, concise source-based caption identifying the protest context. The bowl and crystals should remain ordinary material objects, with their shallow depth, irregular grain sizes, and pale surfaces clearly visible. Use a short schematic journey line on the display base to suggest the march's spatial dimension without inventing a precise personal route or documentary quotation.
Working Three.js Starter (salt-crystals-beside-a-coastal-bowl-x2461):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Salt and the 1930 Salt March</title>
<style>
:root {
--paper: #e8e4da;
--ink: #302e29;
--muted: #716b61;
--accent: #9a5d43;
--rule: rgba(67, 61, 52, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Arial, Helvetica, sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#stage canvas:focus-visible {
outline: 2px solid var(--accent);
outline-offset: -3px;
}
.intro {
position: fixed;
z-index: 5;
top: clamp(24px, 4vw, 58px);
left: clamp(24px, 4.5vw, 72px);
width: min(370px, calc(100vw - 48px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 16px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 26px;
height: 2px;
background: var(--accent);
}
h1 {
margin: 0;
max-width: 360px;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(36px, 4.2vw, 62px);
font-weight: 400;
line-height: 0.98;
letter-spacing: -0.035em;
}
.subtitle {
max-width: 320px;
margin: 18px 0 0;
color: var(--muted);
font-family: Georgia, "Times New Roman", serif;
font-size: 15px;
line-height: 1.55;
}
.caption {
position: fixed;
z-index: 5;
left: clamp(24px, 4.5vw, 72px);
bottom: clamp(24px, 4vw, 52px);
width: min(430px, calc(100vw - 48px));
padding: 17px 20px 16px;
border-left: 2px solid var(--accent);
background: rgba(241, 238, 230, 0.86);
box-shadow: 0 10px 35px rgba(57, 50, 40, 0.08);
backdrop-filter: blur(8px);
}
.caption-label {
margin: 0 0 7px;
color: var(--accent);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.caption p {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 14px;
line-height: 1.5;
}
.caption .source {
margin-top: 9px;
color: var(--muted);
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
line-height: 1.45;
}
.caption a {
color: inherit;
text-decoration-color: rgba(112, 100, 85, 0.5);
text-underline-offset: 2px;
}
.hint {
position: fixed;
z-index: 5;
top: clamp(22px, 3vw, 42px);
right: clamp(22px, 3vw, 46px);
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
.hint span {
display: inline-block;
margin-right: 8px;
color: var(--accent);
font-size: 15px;
}
@media (max-width: 760px) {
.intro {
top: 18px;
left: 18px;
width: calc(100vw - 36px);
}
.eyebrow {
margin-bottom: 10px;
}
h1 {
max-width: 280px;
font-size: 34px;
}
.subtitle {
display: none;
}
.caption {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 12px 14px;
}
.caption p {
font-size: 12px;
}
.caption .source {
font-size: 9px;
}
.hint {
top: 126px;
right: 16px;
}
}
@media (prefers-reduced-motion: reduce) {
* {
scroll-behavior: auto !important;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive three-dimensional historical display of salt, a coastal tile, and a schematic 1930 Salt March journey"></main>
<header class="intro">
<div class="eyebrow">Material histories · India</div>
<h1>Salt and<br>Civil Disobedience</h1>
<p class="subtitle">
A material study of the 1930 Salt March: coarse salt, a shallow dish,
and the approach to the coast.
</p>
</header>
<aside class="caption" aria-label="Historical context and sources">
<div class="caption-label">Protest context</div>
<p>
Gandhi’s Salt March opened the Civil Disobedience Movement in 1930,
protesting the British salt monopoly and the tax imposed on salt.
</p>
<p class="source">
Source:
<a href="https://www.britannica.com/event/Salt-March"
target="_blank"
rel="noopener noreferrer">Encyclopaedia Britannica, “Salt March”</a>;
<a href="https://www.gandhiheritageportal.org/dandi-march-centenary"
target="_blank"
rel="noopener noreferrer">Gandhi Heritage Portal, “Dandi March Centenary”</a>.
</p>
</aside>
<div class="hint" aria-hidden="true">
<span>↔</span> Drag to orbit · Scroll to zoom
</div>
<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 renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional display. Drag to orbit, scroll to zoom, or use the arrow keys."
);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e4da);
scene.fog = new THREE.Fog(0xe8e4da, 25, 48);
const camera = new THREE.PerspectiveCamera(33, 1, 0.1, 100);
const hemisphere = new THREE.HemisphereLight(
0xfffcf4,
0x8a8274,
2.0
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff7e8, 3.25);
keyLight.position.set(4.5, 10, 7.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xdbe8e6, 1.1);
fillLight.position.set(-7, 5, -4);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0xe8e4da,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.235;
ground.receiveShadow = true;
scene.add(ground);
// Deterministic random values keep the arrangement stable on reload.
function mulberry32(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(1930);
function makeGrainTexture(repeatX = 7, repeatY = 4) {
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 128;
const context = canvas.getContext("2d");
const image = context.createImageData(128, 128);
for (let i = 0; i < image.data.length; i += 4) {
const value = 232 + Math.floor(random() * 23);
image.data[i] = value;
image.data[i + 1] = value;
image.data[i + 2] = value;
image.data[i + 3] = 255;
}
context.putImageData(image, 0, 0);
const texture = new THREE.CanvasTexture(canvas);
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(repeatX, repeatY);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function roundedRectShape(width, height, radius) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = -height / 2;
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function horizontalRoundedGeometry(width, height, radius, depth, bevel = 0) {
const geometry = new THREE.ExtrudeGeometry(
roundedRectShape(width, height, radius),
{
depth,
steps: 1,
curveSegments: 12,
bevelEnabled: bevel > 0,
bevelSegments: 3,
bevelSize: bevel,
bevelThickness: bevel
}
);
geometry.rotateX(-Math.PI / 2);
geometry.computeVertexNormals();
return geometry;
}
const grain = makeGrainTexture();
const base = new THREE.Mesh(
horizontalRoundedGeometry(11.8, 6.6, 0.28, 0.42, 0.055),
new THREE.MeshStandardMaterial({
color: 0xc9c0af,
roughness: 0.9,
map: grain
})
);
base.position.y = -0.16;
base.castShadow = true;
base.receiveShadow = true;
scene.add(base);
const baseTop = new THREE.Mesh(
horizontalRoundedGeometry(11.43, 6.23, 0.22, 0.025, 0.012),
new THREE.MeshStandardMaterial({
color: 0xd9d1c1,
roughness: 0.94,
map: grain
})
);
baseTop.position.y = 0.31;
baseTop.receiveShadow = true;
scene.add(baseTop);
// Shallow ordinary bowl with a visible rim and a recessed interior.
const bowlCenter = new THREE.Vector3(-1.45, 0.35, -0.32);
const bowlProfile = [
new THREE.Vector2(0.00, 0.05),
new THREE.Vector2(0.45, 0.05),
new THREE.Vector2(0.84, 0.08),
new THREE.Vector2(1.24, 0.17),
new THREE.Vector2(1.59, 0.32),
new THREE.Vector2(1.81, 0.48),
new THREE.Vector2(1.88, 0.56),
new THREE.Vector2(1.88, 0.62),
new THREE.Vector2(1.82, 0.62),
new THREE.Vector2(1.75, 0.51),
new THREE.Vector2(1.51, 0.37),
new THREE.Vector2(1.17, 0.25),
new THREE.Vector2(0.77, 0.18),
new THREE.Vector2(0.35, 0.16),
new THREE.Vector2(0.00, 0.16)
];
const bowlMaterial = new THREE.MeshStandardMaterial({
color: 0xd4ccbd,
roughness: 0.76,
side: THREE.DoubleSide
});
const bowl = new THREE.Mesh(
new THREE.LatheGeometry(bowlProfile, 96),
bowlMaterial
);
bowl.position.copy(bowlCenter);
bowl.castShadow = true;
bowl.receiveShadow = true;
scene.add(bowl);
const bowlRim = new THREE.Mesh(
new THREE.TorusGeometry(1.845, 0.045, 12, 96),
bowlMaterial
);
bowlRim.rotation.x = Math.PI / 2;
bowlRim.position.set(bowlCenter.x, bowlCenter.y + 0.61, bowlCenter.z);
bowlRim.castShadow = true;
scene.add(bowlRim);
// Coarse, irregular, roughly cubic halite grains.
function makeCrystalGeometry(seed) {
const rng = mulberry32(seed);
const geometry = new THREE.BoxGeometry(1, 1, 1);
const position = geometry.attributes.position;
const cornerOffsets = new Map();
for (let i = 0; i < position.count; i++) {
const x = position.getX(i);
const y = position.getY(i);
const z = position.getZ(i);
const key = `${x.toFixed(3)},${y.toFixed(3)},${z.toFixed(3)}`;
if (!cornerOffsets.has(key)) {
cornerOffsets.set(key, 0.82 + rng() * 0.29);
}
const factor = cornerOffsets.get(key);
position.setXYZ(i, x * factor, y * factor, z * factor);
}
position.needsUpdate = true;
geometry.computeVertexNormals();
return geometry;
}
const crystalGeometries = [
makeCrystalGeometry(11),
makeCrystalGeometry(29),
makeCrystalGeometry(47),
makeCrystalGeometry(83)
];
const crystalMaterials = [
new THREE.MeshStandardMaterial({
color: 0xf7f5ed,
roughness: 0.74,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xeeece2,
roughness: 0.82,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xdedfd8,
roughness: 0.86,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.68,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xe7e3d7,
roughness: 0.8,
flatShading: true
})
];
const saltGroup = new THREE.Group();
saltGroup.position.copy(bowlCenter);
scene.add(saltGroup);
const grainCount = 138;
for (let i = 0; i < grainCount; i++) {
const angle = random() * Math.PI * 2;
const radius = 1.48 * Math.sqrt(random());
const mound = 0.34 * Math.pow(Math.max(0, 1 - radius / 1.55), 1.55);
const size = 0.09 + random() * 0.13;
const crystal = new THREE.Mesh(
crystalGeometries[Math.floor(random() * crystalGeometries.length)],
crystalMaterials[Math.floor(random() * crystalMaterials.length)]
);
crystal.position.set(
Math.cos(angle) * radius,
0.18 + mound + random() * 0.055,
Math.sin(angle) * radius
);
crystal.scale.set(
size * (0.82 + random() * 0.48),
size * (0.72 + random() * 0.42),
size * (0.82 + random() * 0.48)
);
crystal.rotation.set(
random() * Math.PI,
random() * Math.PI,
random() * Math.PI
);
crystal.castShadow = true;
crystal.receiveShadow = true;
saltGroup.add(crystal);
}
// Small coastal sand-and-water tile.
const tile = new THREE.Group();
tile.position.set(3.38, 0, -0.28);
scene.add(tile);
const tileBase = new THREE.Mesh(
new THREE.BoxGeometry(2.92, 0.17, 2.22),
new THREE.MeshStandardMaterial({
color: 0xb8ad98,
roughness: 0.94
})
);
tileBase.position.y = 0.425;
tileBase.castShadow = true;
tileBase.receiveShadow = true;
tile.add(tileBase);
function polygonShape(points) {
const shape = new THREE.Shape();
shape.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i][0], points[i][1]);
}
shape.closePath();
return shape;
}
function horizontalShapeGeometry(shape, depth) {
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
bevelEnabled: false,
steps: 1
});
geometry.rotateX(-Math.PI / 2);
geometry.computeVertexNormals();
return geometry;
}
const sandShape = polygonShape([
[-1.35, -1.00],
[ 1.35, -1.00],
[ 1.35, -0.75],
[ 0.72, -0.45],
[ 0.88, -0.15],
[ 0.55, 0.20],
[ 0.68, 0.55],
[ 0.35, 1.00],
[-1.35, 1.00]
]);
const waterShape = polygonShape([
[ 1.35, -1.00],
[ 1.35, 1.00],
[ 0.35, 1.00],
[ 0.68, 0.55],
[ 0.55, 0.20],
[ 0.88, -0.15],
[ 0.72, -0.45],
[ 1.35, -0.75]
]);
const sand = new THREE.Mesh(
horizontalShapeGeometry(sandShape, 0.04),
new THREE.MeshStandardMaterial({
color: 0xd5c29b,
roughness: 1,
map: makeGrainTexture(3, 3)
})
);
sand.position.y = 0.505;
sand.castShadow = true;
sand.receiveShadow = true;
tile.add(sand);
const water = new THREE.Mesh(
horizontalShapeGeometry(waterShape, 0.025),
new THREE.MeshStandardMaterial({
color: 0x769a9d,
roughness: 0.36,
metalness: 0.03
})
);
water.position.y = 0.535;
water.castShadow = true;
water.receiveShadow = true;
tile.add(water);
const shoreCoordinates = [
[1.35, -0.75],
[0.72, -0.45],
[0.88, -0.15],
[0.55, 0.20],
[0.68, 0.55],
[0.35, 1.00]
].map(([x, y]) => new THREE.Vector3(x, 0.574, -y));
const shore = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(shoreCoordinates),
new THREE.LineBasicMaterial({
color: 0xe8e4d8,
transparent: true,
opacity: 0.72
})
);
tile.add(shore);
const rippleMaterial = new THREE.LineBasicMaterial({
color: 0xe3ece9,
transparent: true,
opacity: 0.32
});
for (const z of [-0.35, 0.02, 0.4]) {
const curve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0.94, 0.576, z - 0.10),
new THREE.Vector3(1.08, 0.576, z),
new THREE.Vector3(1.23, 0.576, z + 0.08)
]);
const ripple = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(curve.getPoints(18)),
rippleMaterial
);
tile.add(ripple);
}
// Separate upright 1930 marker.
const markerGeometry = new THREE.ExtrudeGeometry(
roundedRectShape(1.72, 1.06, 0.12),
{
depth: 0.14,
steps: 1,
curveSegments: 12,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.025,
bevelThickness: 0.025
}
);
markerGeometry.computeVertexNormals();
const marker = new THREE.Mesh(
markerGeometry,
new THREE.MeshStandardMaterial({
color: 0xe1dacd,
roughness: 0.85
})
);
marker.position.set(-4.35, 0.86, -1.70);
marker.castShadow = true;
marker.receiveShadow = true;
scene.add(marker);
function makeTextTexture({
text,
font = "600 44px Arial",
color = "#514a40",
underline = false
}) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = color;
context.textAlign = "center";
context.textBaseline = "middle";
context.font = font;
context.fillText(text, 512, 112);
if (underline) {
context.strokeStyle = color;
context.lineWidth = 3;
context.globalAlpha = 0.55;
context.beginPath();
context.moveTo(365, 184);
context.lineTo(659, 184);
context.stroke();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const dateTexture = makeTextTexture({
text: "1930",
font: "400 150px Georgia",
color: "#6b3f30",
underline: true
});
const datePlane = new THREE.Mesh(
new THREE.PlaneGeometry(1.48, 0.72),
new THREE.MeshBasicMaterial({
map: dateTexture,
transparent: true,
depthWrite: false
})
);
datePlane.position.set(-4.35, 0.90, marker.position.z + 0.14 + 0.025 + 0.01);
scene.add(datePlane);
// Short, deliberately abstract journey line; it is not a route map.
const journeyCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-4.05, 0.385, 2.17),
new THREE.Vector3(-3.25, 0.385, 2.08),
new THREE.Vector3(-2.35, 0.385, 2.27),
new THREE.Vector3(-1.42, 0.385, 2.16),
new THREE.Vector3(-0.45, 0.385, 2.34),
new THREE.Vector3( 0.78, 0.385, 2.22)
]);
const journeyMaterial = new THREE.MeshStandardMaterial({
color: 0x9a5d43,
roughness: 0.9
});
const journeyTube = new THREE.Mesh(
new THREE.TubeGeometry(journeyCurve, 64, 0.018, 8, false),
journeyMaterial
);
journeyTube.castShadow = true;
scene.add(journeyTube);
const startPoint = journeyCurve.getPoint(0);
const endPoint = journeyCurve.getPoint(1);
const tangent = journeyCurve.getTangent(1).normalize();
for (const point of [startPoint, endPoint]) {
const dot = new THREE.Mesh(
new THREE.SphereGeometry(0.058, 16, 12),
journeyMaterial
);
dot.position.copy(point);
dot.castShadow = true;
scene.add(dot);
}
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.22, 12),
journeyMaterial
);
arrow.position.copy(endPoint).addScaledVector(tangent, 0.10);
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
arrow.castShadow = true;
scene.add(arrow);
function makeHorizontalLabel(text, width, height, x, z, font) {
const texture = makeTextTexture({ text, font });
const label = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
label.rotation.x = -Math.PI / 2;
label.position.set(x, 0.39, z);
scene.add(label);
}
makeHorizontalLabel(
"SABARMATI",
2.35, 0.42,
-3.92, 2.65,
"600 43px Arial"
);
makeHorizontalLabel(
"DANDI",
1.85, 0.42,
0.76, 2.64,
"600 43px Arial"
);
makeHorizontalLabel(
"MARCH · SCHEMATIC · NOT TO SCALE",
3.95, 0.30,
-1.55, 2.93,
"500 30px Arial"
);
// Direct camera controls: no examples/addons are loaded.
const desired = {
theta: 0.62,
phi: 1.03,
radius: 14.5,
target: new THREE.Vector3(-0.35, 0.46, 0)
};
const current = {
theta: desired.theta,
phi: desired.phi,
radius: desired.radius,
target: desired.target.clone()
};
function homeForViewport() {
const aspect = window.innerWidth / window.innerHeight;
if (aspect < 0.78) {
desired.radius = 28;
desired.phi = 1.10;
desired.target.set(0, 0.45, 0);
} else if (aspect < 1.15) {
desired.radius = 17.5;
desired.target.set(-0.1, 0.45, 0);
} else {
desired.radius = 14.5;
desired.phi = 1.03;
desired.target.set(-0.35, 0.46, 0);
}
current.radius = desired.radius;
current.target.copy(desired.target);
}
function updateCamera() {
const sinPhi = Math.sin(current.phi);
camera.position.set(
current.target.x + current.radius * sinPhi * Math.sin(current.theta),
current.target.y + current.radius * Math.cos(current.phi),
current.target.z + current.radius * sinPhi * Math.cos(current.theta)
);
camera.lookAt(current.target);
}
let dragging = false;
let previousX = 0;
let previousY = 0;
let pointerId = null;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging || event.pointerId !== pointerId) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
desired.theta -= dx * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi - dy * 0.005,
0.42,
1.36
);
});
function endDrag(event) {
if (event && pointerId !== null && event.pointerId !== pointerId) return;
dragging = false;
pointerId = null;
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("lostpointercapture", endDrag);
renderer.domElement.style.cursor = "grab";
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
9.5,
34
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
homeForViewport();
desired.theta = 0.62;
});
renderer.domElement.addEventListener("keydown", event => {
const step = 0.08;
if (event.key === "ArrowLeft") {
desired.theta += step;
event.preventDefault();
} else if (event.key === "ArrowRight") {
desired.theta -= step;
event.preventDefault();
} else if (event.key === "ArrowUp") {
desired.phi = Math.max(0.42, desired.phi - step);
event.preventDefault();
} else if (event.key === "ArrowDown") {
desired.phi = Math.min(1.36, desired.phi + step);
event.preventDefault();
} else if (event.key === "+" || event.key === "=") {
desired.radius = Math.max(9.5, desired.radius - 0.8);
event.preventDefault();
} else if (event.key === "-" || event.key === "_") {
desired.radius = Math.min(34, desired.radius + 0.8);
event.preventDefault();
} else if (event.key === "Home") {
homeForViewport();
desired.theta = 0.62;
event.preventDefault();
}
});
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
homeForViewport();
updateCamera();
renderer.setAnimationLoop(() => {
const easing = 0.12;
current.theta += (desired.theta - current.theta) * easing;
current.phi += (desired.phi - current.phi) * easing;
current.radius += (desired.radius - current.radius) * easing;
current.target.lerp(desired.target, easing);
updateCamera();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```