Prompt specification
Build or customize the Three.js 3D scene "Periscope cross-section scene" 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: Periscope cross-section scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/periscope-cross-section-scene-x1571
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a simple two-mirror periscope housing with transparent side panels and a target; verify a continuous ray path through both mirrors to the viewer. Voxel style.
Working Three.js Starter (periscope-cross-section-scene-x1571):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Periscope — Ray Path Verification</title>
<style>
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111f;
color: #eaf8ff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid rgba(114, 220, 255, 0.25);
background: rgba(6, 18, 34, 0.82);
box-shadow: 0 14px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: none;
}
#info {
top: 18px;
left: 18px;
width: min(380px, calc(100vw - 36px));
padding: 17px 18px;
border-radius: 12px;
}
h1 {
margin: 0 0 8px;
color: #7fe6ff;
font-size: 18px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.subtitle {
margin: 0;
color: #a9c2d6;
font-size: 12px;
line-height: 1.55;
}
#verification {
display: flex;
align-items: flex-start;
gap: 10px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid rgba(114, 220, 255, 0.16);
}
#status-dot {
flex: 0 0 auto;
width: 11px;
height: 11px;
margin-top: 3px;
border-radius: 2px;
background: #59f5a7;
box-shadow: 0 0 14px #59f5a7;
}
#status-title {
margin-bottom: 5px;
color: #73ffb8;
font-size: 13px;
font-weight: bold;
letter-spacing: 0.06em;
}
#checks {
color: #a9c2d6;
font-size: 11px;
line-height: 1.55;
}
#checks div::before {
content: "✓ ";
color: #59f5a7;
}
#help {
right: 18px;
bottom: 18px;
padding: 12px 14px;
border-radius: 10px;
color: #a9c2d6;
font-size: 11px;
line-height: 1.7;
}
#reset {
pointer-events: auto;
margin-top: 7px;
padding: 7px 10px;
border: 1px solid #39758e;
border-radius: 6px;
background: #102e42;
color: #d9f8ff;
font: inherit;
font-size: 11px;
cursor: pointer;
}
#reset:hover {
background: #17445c;
border-color: #6ee5ff;
}
#legend {
left: 18px;
bottom: 18px;
display: flex;
align-items: center;
gap: 9px;
padding: 10px 12px;
border-radius: 8px;
color: #b9cede;
font-size: 11px;
}
.swatch {
width: 24px;
height: 5px;
border-radius: 1px;
background: #ffe66d;
box-shadow: 0 0 10px rgba(255, 218, 76, 0.85);
}
@media (max-width: 700px) {
#info {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 13px;
}
#legend {
display: none;
}
#help {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<section id="info" class="panel">
<h1>Voxel Periscope</h1>
<p class="subtitle">
A geometrically solved ray leaves the target, reflects from two 45° mirrors,
and enters the viewer's eye.
</p>
<div id="verification">
<div id="status-dot"></div>
<div>
<div id="status-title">CHECKING RAY PATH…</div>
<div id="checks"></div>
</div>
</div>
</section>
<div id="legend" class="panel">
<span class="swatch"></span>
<span>Continuous reflected ray</span>
</div>
<div id="help" class="panel">
<div>Drag: orbit · Wheel: zoom · R: reset</div>
<button id="reset" type="button">Reset camera</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 stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 15, 30);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const clock = new THREE.Clock();
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const materials = {
frame: new THREE.MeshStandardMaterial({
color: 0x173348,
roughness: 0.55,
metalness: 0.55,
flatShading: true
}),
frameAccent: new THREE.MeshStandardMaterial({
color: 0x38b9d8,
emissive: 0x0b6378,
emissiveIntensity: 0.7,
roughness: 0.35,
metalness: 0.35
}),
glass: new THREE.MeshPhongMaterial({
color: 0x74dcff,
transparent: true,
opacity: 0.15,
side: THREE.DoubleSide,
depthWrite: false,
shininess: 100
}),
mirror: new THREE.MeshStandardMaterial({
color: 0xc9e8f3,
roughness: 0.12,
metalness: 0.92,
flatShading: true
}),
mirrorEdge: new THREE.MeshStandardMaterial({
color: 0x37d9ff,
emissive: 0x0b7893,
emissiveIntensity: 0.85,
roughness: 0.25,
metalness: 0.55
}),
targetDark: new THREE.MeshStandardMaterial({
color: 0x15293a,
roughness: 0.8
}),
targetYellow: new THREE.MeshStandardMaterial({
color: 0xffcf45,
emissive: 0x7b4b00,
emissiveIntensity: 0.65,
roughness: 0.55
}),
targetRed: new THREE.MeshStandardMaterial({
color: 0xff5b57,
emissive: 0xb51b18,
emissiveIntensity: 1.15,
roughness: 0.35
}),
viewerBody: new THREE.MeshStandardMaterial({
color: 0xd8e9ef,
roughness: 0.65
}),
viewerAccent: new THREE.MeshStandardMaterial({
color: 0x24b9d4,
emissive: 0x075467,
emissiveIntensity: 0.7,
roughness: 0.4
}),
rayCore: new THREE.MeshBasicMaterial({
color: 0xffe66d,
toneMapped: false
}),
rayGlow: new THREE.MeshBasicMaterial({
color: 0xffb52d,
transparent: true,
opacity: 0.2,
depthWrite: false,
toneMapped: false
}),
pulse: new THREE.MeshBasicMaterial({
color: 0xffffff,
toneMapped: false
})
};
function box(size, material, position, parent = scene, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc9efff, 0x142033, 2.1));
const sun = new THREE.DirectionalLight(0xffffff, 3.1);
sun.position.set(5, 9, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x42dfff, 1.25);
rimLight.position.set(-6, 3, -5);
scene.add(rimLight);
const interiorLight = new THREE.PointLight(0x47dfff, 4.0, 7, 2);
interiorLight.position.set(0, 0, 1.1);
scene.add(interiorLight);
// -------------------------------------------------------------------------
// Voxel ground
// -------------------------------------------------------------------------
box(
[13.2, 0.2, 9.2],
new THREE.MeshStandardMaterial({
color: 0x0d2132,
roughness: 0.95
}),
[0, -2.82, 0],
scene,
false
);
const tileGeometry = new THREE.BoxGeometry(1, 1, 1);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.86
});
const tileCountX = 13;
const tileCountZ = 9;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileCountX * tileCountZ
);
const tileTransform = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let x = -6; x <= 6; x++) {
for (let z = -4; z <= 4; z++) {
tileTransform.position.set(x, -2.68, z);
tileTransform.scale.set(0.96, 0.07, 0.96);
tileTransform.updateMatrix();
tiles.setMatrixAt(tileIndex, tileTransform.matrix);
const checker = (x + z) & 1;
tileColor.set(checker ? 0x173246 : 0x112a3c);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
// -------------------------------------------------------------------------
// Periscope housing
// -------------------------------------------------------------------------
const housing = new THREE.Group();
scene.add(housing);
const housingHalfWidth = 1.1;
const housingHalfDepth = 0.82;
const housingTop = 2.25;
const housingBottom = -2.25;
// Four vertical corner posts.
for (const x of [-housingHalfWidth, housingHalfWidth]) {
for (const z of [-housingHalfDepth, housingHalfDepth]) {
box(
[0.15, housingTop - housingBottom, 0.15],
materials.frame,
[x, 0, z],
housing
);
}
}
// Upper and lower rails running along X.
for (const y of [housingBottom, housingTop]) {
for (const z of [-housingHalfDepth, housingHalfDepth]) {
box(
[2.35, 0.15, 0.15],
materials.frame,
[0, y, z],
housing
);
}
}
// Upper and lower rails running along Z.
for (const y of [housingBottom, housingTop]) {
for (const x of [-housingHalfWidth, housingHalfWidth]) {
box(
[0.15, 0.15, 1.78],
materials.frame,
[x, y, 0],
housing
);
}
}
// Transparent side panels at Z = ±0.82.
for (const z of [-housingHalfDepth, housingHalfDepth]) {
const panel = box(
[2.08, 4.34, 0.035],
materials.glass,
[0, 0, z],
housing,
false
);
panel.renderOrder = 3;
}
// Small voxel corner caps.
for (const x of [-housingHalfWidth, housingHalfWidth]) {
for (const y of [housingBottom, housingTop]) {
for (const z of [-housingHalfDepth, housingHalfDepth]) {
// Intentionally replaced below; this prevents accidental duplicate caps.
break;
}
}
}
for (const x of [-housingHalfWidth, housingHalfWidth]) {
for (const y of [housingBottom, housingTop]) {
for (const z of [-housingHalfDepth, housingHalfDepth]) {
box(
[0.23, 0.23, 0.23],
materials.frameAccent,
[x, y, z],
housing
);
}
}
}
// -------------------------------------------------------------------------
// Geometric ray solution
//
// Both mirrors use the same 45-degree orientation.
// For tangent direction (1,-1,0), the surface normal is (1,1,0).
// -------------------------------------------------------------------------
const TOP_Y = 1.35;
const BOTTOM_Y = -1.35;
const TARGET_FRONT_X = -3.10;
const VIEWER_EYE_X = 2.50;
const topMirrorCenter = new THREE.Vector3(0, TOP_Y, 0);
const bottomMirrorCenter = new THREE.Vector3(0, BOTTOM_Y, 0);
const mirrorNormal = new THREE.Vector3(1, 1, 0).normalize();
const targetOrigin = new THREE.Vector3(
TARGET_FRONT_X,
TOP_Y,
0
);
function reflect(direction, normal) {
return direction
.clone()
.addScaledVector(normal, -2 * direction.dot(normal))
.normalize();
}
function intersectMirrorPlane(origin, direction, planePoint) {
const denominator = mirrorNormal.dot(direction);
if (Math.abs(denominator) < 1e-8) {
return null;
}
const t = mirrorNormal
.dot(planePoint.clone().sub(origin)) / denominator;
return origin.clone().addScaledVector(direction, t);
}
const incomingTop = topMirrorCenter
.clone()
.sub(targetOrigin)
.normalize();
const topHit = intersectMirrorPlane(
targetOrigin,
incomingTop,
topMirrorCenter
);
const reflectedTop = reflect(incomingTop, mirrorNormal);
const bottomHit = intersectMirrorPlane(
topHit,
reflectedTop,
bottomMirrorCenter
);
const incomingBottom = bottomHit
.clone()
.sub(topHit)
.normalize();
const reflectedBottom = reflect(incomingBottom, mirrorNormal);
const viewerDistance =
(VIEWER_EYE_X - bottomHit.x) / reflectedBottom.x;
const viewerHit = bottomHit
.clone()
.addScaledVector(reflectedBottom, viewerDistance);
const rayPath = [
targetOrigin,
topHit,
bottomHit,
viewerHit
];
function pointOnMirror(point, center) {
return Math.abs(
mirrorNormal.dot(point.clone().sub(center))
) < 1e-6;
}
const verificationChecks = [
{
label: "Target ray intersects the first mirror",
pass:
topHit !== null &&
pointOnMirror(topHit, topMirrorCenter)
},
{
label: "First reflection points directly to the second mirror",
pass:
bottomHit !== null &&
reflectedTop.dot(incomingBottom) > 0.9999
},
{
label: "Second reflection points directly to the viewer",
pass:
reflectedBottom.dot(
viewerHit.clone().sub(bottomHit).normalize()
) > 0.9999
},
{
label: "Both mirror intersections and joins are continuous",
pass:
pointOnMirror(bottomHit, bottomMirrorCenter) &&
rayPath.every(point => Number.isFinite(point.lengthSq()))
}
];
const verified = verificationChecks.every(check => check.pass);
document.getElementById("status-title").textContent = verified
? "RAY PATH VERIFIED · 2 REFLECTIONS"
: "RAY PATH ERROR";
document.getElementById("status-dot").style.background = verified
? "#59f5a7"
: "#ff5b57";
document.getElementById("status-dot").style.boxShadow = verified
? "0 0 14px #59f5a7"
: "0 0 14px #ff5b57";
document.getElementById("status-title").style.color = verified
? "#73ffb8"
: "#ff807d";
document.getElementById("checks").innerHTML =
verificationChecks
.map(check => `<div>${check.label}</div>`)
.join("");
// -------------------------------------------------------------------------
// Mirrors
// -------------------------------------------------------------------------
function createMirror(center, name) {
const group = new THREE.Group();
group.position.copy(center);
group.rotation.z = -Math.PI / 4;
scene.add(group);
// Segmented reflective tiles create the voxel look while preserving
// one continuous 45-degree optical surface.
const tileCount = 7;
const tileWidth = 0.25;
for (let i = 0; i < tileCount; i++) {
const localX = (i - (tileCount - 1) / 2) * tileWidth;
box(
[tileWidth - 0.006, 0.11, 0.72],
materials.mirror,
[localX, 0, 0],
group
);
box(
[tileWidth - 0.026, 0.025, 0.735],
materials.mirrorEdge,
[localX, 0.066, 0],
group,
false
);
}
// End caps.
for (const side of [-1, 1]) {
box(
[0.09, 0.16, 0.78],
materials.frameAccent,
[side * 0.88, 0, 0],
group
);
}
return group;
}
createMirror(topMirrorCenter, "M1");
createMirror(bottomMirrorCenter, "M2");
// -------------------------------------------------------------------------
// Target
// -------------------------------------------------------------------------
const target = new THREE.Group();
scene.add(target);
box(
[0.18, 1.72, 1.72],
materials.targetDark,
[-3.45, TOP_Y, 0],
target
);
// Stand and base.
box(
[0.17, 3.25, 0.17],
materials.frame,
[-3.45, -1.0, 0],
target
);
box(
[0.82, 0.12, 0.78],
materials.frameAccent,
[-3.45, -2.57, 0],
target
);
// Bright center of the voxel target.
box(
[0.26, 0.30, 0.30],
materials.targetRed,
[-3.23, TOP_Y, 0],
target
);
// Square yellow target ring.
for (const dy of [-0.43, 0.43]) {
for (const dz of [-0.43, 0, 0.43]) {
box(
[0.23, 0.29, 0.29],
materials.targetYellow,
[-3.24, TOP_Y + dy, dz],
target
);
}
}
for (const dy of [-0.43, 0, 0.43]) {
box(
[0.23, 0.29, 0.29],
materials.targetYellow,
[-3.24, TOP_Y + dy, 0.43],
target
);
}
// -------------------------------------------------------------------------
// Voxel viewer
// -------------------------------------------------------------------------
const viewer = new THREE.Group();
scene.add(viewer);
// Torso and head.
box(
[0.72, 0.86, 0.70],
materials.viewerBody,
[3.15, -2.05, 0],
viewer
);
box(
[0.72, 0.70, 0.72],
materials.viewerBody,
[3.05, -1.25, 0],
viewer
);
// Eye, with its front face exactly at the ray endpoint.
box(
[0.24, 0.24, 0.24],
materials.viewerAccent,
[2.62, BOTTOM_Y, 0],
viewer
);
// Neck.
box(
[0.28, 0.22, 0.30],
materials.frame,
[3.05, -1.70, 0],
viewer
);
// Arms.
for (const z of [-0.50, 0.50]) {
box(
[0.40, 0.68, 0.24],
materials.viewerAccent,
[3.15, -2.03, z],
viewer
);
}
// Legs and feet.
for (const z of [-0.22, 0.22]) {
box(
[0.34, 0.57, 0.28],
materials.viewerBody,
[3.15, -2.62, z],
viewer
);
box(
[0.47, 0.15, 0.32],
materials.frame,
[2.96, -2.86, z],
viewer
);
}
// Small antenna.
box(
[0.10, 0.38, 0.10],
materials.frameAccent,
[3.05, -0.71, 0],
viewer
);
box(
[0.18, 0.18, 0.18],
materials.targetYellow,
[3.05, -0.51, 0],
viewer
);
// -------------------------------------------------------------------------
// Continuous ray beam
// -------------------------------------------------------------------------
const rayGroup = new THREE.Group();
scene.add(rayGroup);
function createBeamSegment(a, b, width, material, renderOrder) {
const direction = b.clone().sub(a);
const length = direction.length();
direction.normalize();
const segment = new THREE.Mesh(
new THREE.BoxGeometry(length, width, width),
material
);
segment.position.copy(a).add(b).multiplyScalar(0.5);
segment.quaternion.setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
direction
);
segment.renderOrder = renderOrder;
rayGroup.add(segment);
return segment;
}
for (let i = 0; i < rayPath.length - 1; i++) {
const a = rayPath[i];
const b = rayPath[i + 1];
createBeamSegment(a, b, 0.14, materials.rayGlow, 4);
createBeamSegment(a, b, 0.055, materials.rayCore, 5);
}
// Bright cubes emphasize the exact reflection and join points.
for (const point of rayPath) {
const joint = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.13, 0.13),
materials.rayCore
);
joint.position.copy(point);
joint.renderOrder = 6;
rayGroup.add(joint);
}
// Moving photon/pulse.
const pulse = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 0.18, 0.18),
materials.pulse
);
pulse.renderOrder = 7;
scene.add(pulse);
const pulseLight = new THREE.PointLight(0xffdf73, 2.0, 2.8, 2);
scene.add(pulseLight);
const totalRayLength = (() => {
let length = 0;
for (let i = 0; i < rayPath.length - 1; i++) {
length += rayPath[i].distanceTo(rayPath[i + 1]);
}
return length;
})();
function pointAlongRay(distance) {
let remaining = distance;
for (let i = 0; i < rayPath.length - 1; i++) {
const a = rayPath[i];
const b = rayPath[i + 1];
const length = a.distanceTo(b);
if (remaining <= length || i === rayPath.length - 2) {
return a.clone().lerp(b, THREE.MathUtils.clamp(remaining / length, 0, 1));
}
remaining -= length;
}
return rayPath[0].clone();
}
// -------------------------------------------------------------------------
// Canvas-generated labels; no additional libraries or assets
// -------------------------------------------------------------------------
function makeLabel(text, position, scale = 1.45) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.fillStyle = "rgba(5, 19, 33, 0.88)";
context.fillRect(0, 10, 512, 108);
context.strokeStyle = "rgba(106, 225, 255, 0.8)";
context.lineWidth = 4;
context.strokeRect(2, 12, 508, 104);
context.fillStyle = "#dff8ff";
context.font = "bold 43px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 64);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.position.copy(position);
sprite.scale.set(scale, scale * 0.25, 1);
sprite.renderOrder = 20;
scene.add(sprite);
}
makeLabel("TARGET", new THREE.Vector3(-3.35, 2.62, 0), 1.4);
makeLabel("M1 · 45°", new THREE.Vector3(0, 2.62, 0), 1.35);
makeLabel("M2 · 45°", new THREE.Vector3(1.25, -2.02, 0), 1.35);
makeLabel("VIEWER", new THREE.Vector3(3.10, -0.52, 0), 1.35);
makeLabel(
"TRANSPARENT PANELS",
new THREE.Vector3(-1.75, 1.88, 0.84),
1.55
);
// -------------------------------------------------------------------------
// Direct camera controls — no OrbitControls or other addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, -0.10, 0);
const cameraState = {
radius: window.innerWidth < 700 ? 14 : 10.8,
theta: 0.68,
phi: 1.18
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraTarget.x + cameraState.radius * sinPhi * Math.cos(cameraState.theta),
cameraTarget.y + cameraState.radius * cosPhi,
cameraTarget.z + cameraState.radius * sinPhi * Math.sin(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraState.radius = window.innerWidth < 700 ? 14 : 10.8;
cameraState.theta = 0.68;
cameraState.phi = 1.18;
updateCamera();
}
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 deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.006,
0.28,
1.48
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
6,
22
);
updateCamera();
},
{ passive: false }
);
document.getElementById("reset").addEventListener("click", resetCamera);
window.addEventListener("keydown", event => {
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updateCamera();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const pulseDistance = (elapsed * 2.35) % totalRayLength;
const pulsePosition = pointAlongRay(pulseDistance);
pulse.position.copy(pulsePosition);
pulse.rotation.x = elapsed * 1.4;
pulse.rotation.y = elapsed * 1.8;
pulseLight.position.copy(pulsePosition);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```