Prompt specification
Build or customize the Three.js 3D scene "Solar Panel Normal and Sun Direction" 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: Solar Panel Normal and Sun Direction
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/solar-panel-normal-and-sun-direction-x3977
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: solar panel normal and Sun direction. Model a panel with a surface-normal arrow and a separate sunlight direction. Show the angle between them without presenting rendered brightness as electrical output. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (solar-panel-normal-and-sun-direction-x3977):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Solar Panel Normal and Sun Direction</title>
<style>
:root {
--ink: #18313d;
--muted: #63757d;
--normal: #16c7c9;
--sun: #ffb62e;
--panel: #eef5f2;
--line: rgba(24, 49, 61, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e7eeeb;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.card {
position: absolute;
background: rgba(248, 251, 249, 0.9);
border: 1px solid rgba(255, 255, 255, 0.8);
box-shadow: 0 16px 45px rgba(35, 60, 68, 0.13);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
border-radius: 18px;
}
.info {
top: 24px;
left: 24px;
width: min(350px, calc(100vw - 48px));
padding: 22px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
color: #6a7f87;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 22px;
height: 3px;
border-radius: 999px;
background: linear-gradient(90deg, var(--normal), var(--sun));
}
h1 {
margin: 0;
font-size: clamp(27px, 3vw, 38px);
line-height: 1.02;
letter-spacing: -0.045em;
}
.subtitle {
margin: 10px 0 17px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.angle-card {
display: grid;
grid-template-columns: auto 1fr;
align-items: center;
gap: 14px;
padding: 14px 0 16px;
border-top: 1px solid var(--line);
border-bottom: 1px solid var(--line);
}
.angle-number {
min-width: 98px;
color: var(--ink);
font-size: 46px;
font-weight: 780;
line-height: 0.95;
letter-spacing: -0.055em;
}
.angle-copy strong {
display: block;
margin-bottom: 4px;
font-size: 13px;
}
.angle-copy span {
display: block;
color: var(--muted);
font-size: 12px;
line-height: 1.35;
}
.legend {
display: grid;
gap: 10px;
margin-top: 16px;
}
.legend-item {
display: flex;
align-items: center;
gap: 10px;
color: #40565f;
font-size: 12px;
}
.swatch {
width: 25px;
height: 6px;
flex: 0 0 auto;
border-radius: 999px;
}
.swatch.normal {
background: var(--normal);
box-shadow: 0 0 0 4px rgba(22, 199, 201, 0.12);
}
.swatch.sun {
background: var(--sun);
box-shadow: 0 0 0 4px rgba(255, 182, 46, 0.13);
}
.swatch.rays {
height: 0;
border-top: 3px dotted rgba(245, 165, 20, 0.8);
border-radius: 0;
}
.notice {
margin-top: 16px;
padding: 11px 12px;
border-radius: 11px;
background: rgba(24, 49, 61, 0.055);
color: #63757d;
font-size: 11px;
line-height: 1.42;
}
.readout {
right: 24px;
bottom: 24px;
width: min(340px, calc(100vw - 48px));
padding: 19px 20px;
}
.readout h2 {
margin: 0 0 12px;
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.readout p {
margin: 8px 0;
color: #536971;
font-size: 12px;
line-height: 1.5;
}
.formula {
margin: 12px 0;
padding: 10px 12px;
border-left: 3px solid var(--sun);
background: rgba(255, 182, 46, 0.09);
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
}
.controls {
position: absolute;
bottom: 24px;
left: 24px;
display: flex;
align-items: center;
gap: 12px;
pointer-events: auto;
}
.hint {
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 999px;
background: rgba(248, 251, 249, 0.8);
color: #62777f;
font-size: 11px;
box-shadow: 0 8px 25px rgba(35, 60, 68, 0.08);
}
button {
border: 0;
border-radius: 999px;
padding: 10px 14px;
background: var(--ink);
color: white;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
}
button:hover {
background: #274c5a;
}
@media (max-width: 760px) {
.info {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 17px;
}
.subtitle {
margin-bottom: 12px;
}
.angle-number {
font-size: 38px;
}
.legend,
.notice {
display: none;
}
.readout {
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.readout p:nth-of-type(2),
.readout p:nth-of-type(3) {
display: none;
}
.controls {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly solar panel showing its surface normal and the direction toward the Sun"></canvas>
<div class="hud">
<section class="card info">
<div class="eyebrow">Low-poly solar study</div>
<h1>Panel normal &<br>Sun direction</h1>
<p class="subtitle">A geometric view of the angle between the panel’s outward surface normal and the direction from the panel toward the Sun.</p>
<div class="angle-card">
<div class="angle-number">42°</div>
<div class="angle-copy">
<strong>Sun-relative angle</strong>
<span>θ = angle between the normal and the direction to the Sun</span>
</div>
</div>
<div class="legend">
<div class="legend-item">
<span class="swatch normal"></span>
<span>Outward surface normal</span>
</div>
<div class="legend-item">
<span class="swatch sun"></span>
<span>Direction from the panel toward the Sun</span>
</div>
<div class="legend-item">
<span class="swatch rays"></span>
<span>Incoming ray travel, opposite the Sun-direction arrow</span>
</div>
</div>
<div class="notice">Rendered brightness is illustrative only. It is not an electrical-output, power, or irradiance estimate.</div>
</section>
<section class="card readout">
<h2>How to read the diagram</h2>
<p>The cyan arrow leaves the panel perpendicular to its front surface. The gold arrow points from the panel toward the Sun.</p>
<div class="formula">θ = arccos(normal · sunDirection)</div>
<p>At 0°, the panel normal points directly toward the Sun. Incoming rays travel in the opposite direction to the gold arrow.</p>
<p>This visualization describes orientation and angle only; it does not model electrical production.</p>
</section>
<div class="controls">
<span class="hint">Drag to orbit · Scroll to zoom</span>
<button id="reset" type="button">Reset view</button>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe7eeeb);
scene.fog = new THREE.Fog(0xe7eeeb, 17, 30);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const target = new THREE.Vector3(0, -0.15, 0.35);
const spherical = {
radius: 12.5,
theta: 0.57,
phi: 1.25
};
function updateCamera() {
const sinPhi = Math.sin(spherical.phi);
camera.position.set(
target.x + spherical.radius * sinPhi * Math.sin(spherical.theta),
target.y + spherical.radius * Math.cos(spherical.phi),
target.z + spherical.radius * sinPhi * Math.cos(spherical.theta)
);
camera.lookAt(target);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xf9fffc, 0x6c7d7b, 2.2));
const fillLight = new THREE.DirectionalLight(0xcce9ff, 1.25);
fillLight.position.set(-7, 5, 6);
scene.add(fillLight);
const palette = {
normal: 0x16c7c9,
sun: 0xffb62e,
rays: 0xf0a51d,
frame: 0x18333f,
frameEdge: 0x294957,
cellA: 0x174b6c,
cellB: 0x0e4265,
cellC: 0x205878,
metal: 0x38515c
};
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.05,
flatShading: true,
...options
});
}
// Ground and low-poly platform.
const groundMaterial = standardMaterial(0xdde5e0);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -2.38;
ground.receiveShadow = true;
scene.add(ground);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(5.3, 5.65, 0.34, 12, 1),
standardMaterial(0xcbd8d2)
);
platform.position.y = -2.2;
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
// The panel's local +Z axis is its outward surface normal.
const panel = new THREE.Group();
panel.position.set(-0.65, -0.05, 0);
panel.rotation.set(-0.18, 0.10, -0.04);
scene.add(panel);
const backing = new THREE.Mesh(
new THREE.BoxGeometry(4.98, 3.12, 0.14),
standardMaterial(palette.frame)
);
backing.castShadow = true;
backing.receiveShadow = true;
panel.add(backing);
const cellGeometry = new THREE.BoxGeometry(0.69, 0.61, 0.045);
const cellMaterials = [
standardMaterial(palette.cellA, { roughness: 0.48, metalness: 0.18 }),
standardMaterial(palette.cellB, { roughness: 0.48, metalness: 0.18 }),
standardMaterial(palette.cellC, { roughness: 0.48, metalness: 0.18 })
];
const columns = 6;
const rows = 4;
const gap = 0.075;
const startX = -((columns - 1) * (0.69 + gap)) / 2;
const startY = -((rows - 1) * (0.61 + gap)) / 2;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < columns; col++) {
const cell = new THREE.Mesh(
cellGeometry,
cellMaterials[(row + col * 2) % cellMaterials.length]
);
cell.position.set(
startX + col * (0.69 + gap),
startY + row * (0.61 + gap),
0.092
);
cell.castShadow = true;
cell.receiveShadow = true;
panel.add(cell);
}
}
const busbarMaterial = standardMaterial(0x7195a8, {
roughness: 0.45,
metalness: 0.35
});
for (let col = 1; col < columns; col++) {
const x = startX + col * (0.69 + gap) - (0.69 + gap) / 2;
const busbar = new THREE.Mesh(
new THREE.BoxGeometry(0.012, 2.73, 0.012),
busbarMaterial
);
busbar.position.set(x, 0, 0.121);
panel.add(busbar);
}
for (let row = 1; row < rows; row++) {
const y = startY + row * (0.61 + gap) - (0.61 + gap) / 2;
const busbar = new THREE.Mesh(
new THREE.BoxGeometry(4.54, 0.012, 0.012),
busbarMaterial
);
busbar.position.set(0, y, 0.121);
panel.add(busbar);
}
const frameMaterial = standardMaterial(palette.frameEdge, {
roughness: 0.55,
metalness: 0.25
});
const horizontalRailGeometry = new THREE.BoxGeometry(5.06, 0.14, 0.20);
const verticalRailGeometry = new THREE.BoxGeometry(0.14, 3.12, 0.20);
for (const y of [-1.54, 1.54]) {
const rail = new THREE.Mesh(horizontalRailGeometry, frameMaterial);
rail.position.set(0, y, 0.045);
rail.castShadow = true;
panel.add(rail);
}
for (const x of [-2.46, 2.46]) {
const rail = new THREE.Mesh(verticalRailGeometry, frameMaterial);
rail.position.set(x, 0, 0.045);
rail.castShadow = true;
panel.add(rail);
}
function cylinderBetween(start, end, radius, material, radialSegments = 6) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments,
1,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
const supportMaterial = standardMaterial(palette.metal, {
metalness: 0.35,
roughness: 0.58
});
for (const x of [-2.15, 0.85]) {
scene.add(cylinderBetween(
new THREE.Vector3(x, -2.03, -0.25),
new THREE.Vector3(x, 0.12, -0.25),
0.065,
supportMaterial
));
scene.add(cylinderBetween(
new THREE.Vector3(x, -1.98, -1.18),
new THREE.Vector3(x, 0.06, -0.25),
0.045,
supportMaterial
));
}
scene.add(cylinderBetween(
new THREE.Vector3(-2.2, 0.10, -0.25),
new THREE.Vector3(0.9, 0.10, -0.25),
0.075,
supportMaterial
));
panel.updateMatrixWorld(true);
const panelCenter = panel.position.clone();
const panelQuaternion = panel.getWorldQuaternion(new THREE.Quaternion());
const normal = new THREE.Vector3(0, 0, 1)
.applyQuaternion(panelQuaternion)
.normalize();
// Choose the Sun direction so the true 42-degree angle is visible from
// the initial camera while the scene remains directly orbitable.
const initialCameraDirection = new THREE.Vector3(6.4, 3.4, 10).normalize();
const angleTangent = new THREE.Vector3()
.crossVectors(initialCameraDirection, normal)
.normalize();
const angle = THREE.MathUtils.degToRad(42);
const sunDirection = normal.clone()
.multiplyScalar(Math.cos(angle))
.addScaledVector(angleTangent, Math.sin(angle))
.normalize();
const sunLight = new THREE.DirectionalLight(0xffe6b3, 3.4);
sunLight.position.copy(panelCenter).addScaledVector(sunDirection, 12);
sunLight.target.position.copy(panelCenter);
sunLight.castShadow = true;
sunLight.shadow.mapSize.set(2048, 2048);
sunLight.shadow.camera.left = -8;
sunLight.shadow.camera.right = 8;
sunLight.shadow.camera.top = 8;
sunLight.shadow.camera.bottom = -8;
sunLight.shadow.camera.near = 1;
sunLight.shadow.camera.far = 28;
sunLight.shadow.bias = -0.00025;
scene.add(sunLight, sunLight.target);
function makeArrow(
origin,
direction,
length,
color,
{
shaftRadius = 0.034,
headRadius = 0.12,
headLength = 0.28,
opacity = 1
} = {}
) {
const group = new THREE.Group();
const shaftLength = Math.max(0.01, length - headLength);
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.16,
roughness: 0.48,
metalness: 0.05,
flatShading: true,
transparent: opacity < 1,
opacity
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(
shaftRadius,
shaftRadius,
shaftLength,
8,
1,
false
),
material
);
shaft.position.y = shaftLength / 2;
shaft.castShadow = true;
const head = new THREE.Mesh(
new THREE.ConeGeometry(headRadius, headLength, 8, 1, false),
material
);
head.position.y = shaftLength + headLength / 2;
head.castShadow = true;
group.add(shaft, head);
group.position.copy(origin);
group.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
return group;
}
const vectorOrigin = panelCenter.clone().addScaledVector(normal, 0.15);
const normalArrow = makeArrow(
vectorOrigin,
normal,
3.35,
palette.normal,
{
shaftRadius: 0.038,
headRadius: 0.135,
headLength: 0.34
}
);
scene.add(normalArrow);
const sunArrow = makeArrow(
vectorOrigin,
sunDirection,
4.3,
palette.sun,
{
shaftRadius: 0.042,
headRadius: 0.15,
headLength: 0.37
}
);
scene.add(sunArrow);
// Parallel incoming rays travel opposite the direction-to-Sun arrow.
const incomingDirection = sunDirection.clone().negate();
const rayAxis = new THREE.Vector3()
.crossVectors(sunDirection, normal)
.normalize();
const rayUp = new THREE.Vector3()
.crossVectors(rayAxis, sunDirection)
.normalize();
const rayOffsets = [
[-1.08, 0.10],
[1.08, -0.35],
[0.05, 1.18]
];
for (const [axisOffset, upOffset] of rayOffsets) {
const start = panelCenter.clone()
.addScaledVector(sunDirection, 4.85)
.addScaledVector(rayAxis, axisOffset)
.addScaledVector(rayUp, upOffset);
scene.add(makeArrow(
start,
incomingDirection,
3.7,
palette.rays,
{
shaftRadius: 0.018,
headRadius: 0.072,
headLength: 0.18,
opacity: 0.58
}
));
}
// Low-poly Sun marker.
const sunPosition = panelCenter.clone().addScaledVector(sunDirection, 4.78);
const sunMaterial = new THREE.MeshStandardMaterial({
color: 0xffc13d,
emissive: 0xff9e16,
emissiveIntensity: 0.58,
roughness: 0.5,
flatShading: true
});
const sunMarker = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.39, 1),
sunMaterial
);
sunMarker.position.copy(sunPosition);
sunMarker.castShadow = true;
scene.add(sunMarker);
const sunGlow = new THREE.Mesh(
new THREE.TorusGeometry(0.58, 0.025, 6, 28),
new THREE.MeshBasicMaterial({
color: 0xffbf3b,
transparent: true,
opacity: 0.75
})
);
sunGlow.position.copy(sunPosition);
scene.add(sunGlow);
// Angle arc and translucent sector, measured in the true 3D plane.
const arcRadius = 1.25;
const arcSegments = 36;
const identityQuaternion = new THREE.Quaternion();
const arcQuaternion = new THREE.Quaternion().setFromUnitVectors(
normal,
sunDirection
);
const arcPoints = [];
const sectorPositions = [];
for (let i = 0; i <= arcSegments; i++) {
const t = i / arcSegments;
const q = new THREE.Quaternion().slerpQuaternions(
identityQuaternion,
arcQuaternion,
t
);
const point = normal.clone().applyQuaternion(q).multiplyScalar(arcRadius);
arcPoints.push(point.clone().add(panelCenter));
if (i < arcSegments) {
const nextQ = new THREE.Quaternion().slerpQuaternions(
identityQuaternion,
arcQuaternion,
(i + 1) / arcSegments
);
const next = normal.clone()
.applyQuaternion(nextQ)
.multiplyScalar(arcRadius);
sectorPositions.push(
0, 0, 0,
point.x, point.y, point.z,
next.x, next.y, next.z
);
}
}
const arcGeometry = new THREE.BufferGeometry().setFromPoints(arcPoints);
const arc = new THREE.Line(
arcGeometry,
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.95,
depthTest: false,
depthWrite: false
})
);
arc.position.copy(panelCenter);
arc.renderOrder = 8;
scene.add(arc);
const sectorGeometry = new THREE.BufferGeometry();
sectorGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(sectorPositions, 3)
);
const sector = new THREE.Mesh(
sectorGeometry,
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.12,
side: THREE.DoubleSide,
depthTest: false,
depthWrite: false
})
);
sector.position.copy(panelCenter);
sector.renderOrder = 7;
scene.add(sector);
function roundedRect(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.arcTo(x + width, y, x + width, y + height, r);
context.arcTo(x + width, y + height, x, y + height, r);
context.arcTo(x, y + height, x, y, r);
context.arcTo(x, y, x + width, y, r);
context.closePath();
}
function makeLabel(text, color, worldWidth = 2.0) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 112;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 512, 112);
roundedRect(context, 5, 7, 502, 98, 24);
context.fillStyle = "rgba(20, 43, 52, 0.90)";
context.fill();
context.strokeStyle = color;
context.lineWidth = 4;
context.stroke();
context.fillStyle = color;
context.font = "800 38px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 57);
const texture = new THREE.CanvasTexture(labelCanvas);
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(worldWidth, worldWidth * 112 / 512, 1);
sprite.renderOrder = 12;
return sprite;
}
const normalLabel = makeLabel("SURFACE NORMAL", "#28d7d9", 2.15);
normalLabel.position.copy(panelCenter)
.addScaledVector(normal, 3.63)
.add(new THREE.Vector3(0, 0.22, 0));
scene.add(normalLabel);
const sunLabel = makeLabel("SUN DIRECTION", "#ffc447", 2.0);
sunLabel.position.copy(sunPosition).add(new THREE.Vector3(0, 0.68, 0));
scene.add(sunLabel);
const panelLabel = makeLabel("PANEL", "#d8e8ed", 1.25);
panelLabel.position.set(1.82, -1.22, 0.24);
panel.localToWorld(panelLabel.position);
scene.add(panelLabel);
const angleMidpoint = normal.clone().applyQuaternion(
new THREE.Quaternion().slerpQuaternions(
identityQuaternion,
arcQuaternion,
0.5
)
);
const angleLabel = makeLabel("θ = 42°", "#ffffff", 1.35);
angleLabel.position.copy(panelCenter).addScaledVector(angleMidpoint, 1.58);
scene.add(angleLabel);
// Direct, dependency-free orbit and zoom controls.
let dragging = false;
let lastX = 0;
let lastY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
spherical.theta -= dx * 0.006;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi - dy * 0.005,
0.38,
2.72
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius + event.deltaY * 0.008,
7.5,
19
);
updateCamera();
}, { passive: false });
function resetView() {
spherical.radius = 12.5;
spherical.theta = 0.57;
spherical.phi = 1.25;
updateCamera();
}
document.querySelector("#reset").addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
</body>
</html>
```