Prompt specification
Build or customize the Three.js 3D scene "Move the viewpoint and rebuild the projection" 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: Move the viewpoint and rebuild the projection
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/move-the-viewpoint-and-rebuild-the-projection-x2388
Scene Description Prompt:
I'd like to see this as a voxel Three.js scene: Move the viewpoint and rebuild the projection. Let users shift the eye point beside a perspective frame. Update the projected drawing while retaining the same three-dimensional scene, demonstrating why viewpoint controls the image. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (move-the-viewpoint-and-rebuild-the-projection-x2388):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Viewpoint Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #050a12;
--panel: #0a1420;
--panel-2: #0d1a29;
--line: rgba(151, 201, 231, 0.16);
--text: #eaf6ff;
--muted: #8ba7bb;
--cyan: #59ddff;
--gold: #ffc85a;
--violet: #9a8cff;
--eye-position: 50%;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
background:
radial-gradient(circle at 12% 0%, rgba(46, 133, 173, 0.16), transparent 34rem),
radial-gradient(circle at 90% 20%, rgba(126, 91, 205, 0.13), transparent 30rem),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(17, 33, 48, 0.88);
color: var(--text);
cursor: pointer;
transition: border-color 150ms ease, background 150ms ease, transform 150ms ease;
}
button:hover {
border-color: rgba(89, 221, 255, 0.55);
background: rgba(25, 48, 66, 0.95);
}
button:active {
transform: translateY(1px);
}
.app {
width: min(1500px, calc(100% - 28px));
margin: 0 auto;
padding: 22px 0 30px;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
margin-bottom: 18px;
}
.eyebrow {
margin: 0 0 5px;
color: var(--cyan);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.35rem, 2.6vw, 2.15rem);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
max-width: 720px;
margin: 9px 0 0;
color: var(--muted);
font-size: 0.92rem;
line-height: 1.55;
}
.status {
display: inline-flex;
align-items: center;
gap: 9px;
flex: 0 0 auto;
padding: 9px 12px;
border: 1px solid rgba(89, 221, 255, 0.2);
border-radius: 999px;
background: rgba(9, 24, 36, 0.8);
color: #bfefff;
font-size: 0.72rem;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1.65fr) minmax(360px, 0.85fr);
gap: 16px;
align-items: start;
}
.card {
overflow: hidden;
border: 1px solid var(--line);
border-radius: 16px;
background: linear-gradient(180deg, rgba(14, 28, 42, 0.96), rgba(7, 15, 25, 0.98));
box-shadow: 0 18px 70px rgba(0, 0, 0, 0.24);
}
.card-header {
display: flex;
min-height: 58px;
align-items: center;
justify-content: space-between;
gap: 14px;
padding: 13px 16px;
border-bottom: 1px solid var(--line);
}
.card-title {
display: flex;
align-items: center;
gap: 10px;
font-size: 0.82rem;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.card-index {
display: grid;
width: 25px;
height: 25px;
place-items: center;
border: 1px solid rgba(89, 221, 255, 0.28);
border-radius: 7px;
color: var(--cyan);
font-size: 0.68rem;
}
.small-note {
color: var(--muted);
font-size: 0.72rem;
text-align: right;
}
.viewport {
position: relative;
width: 100%;
aspect-ratio: 12 / 7;
overflow: hidden;
background: #07111c;
}
.viewport canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
.viewport canvas:active {
cursor: grabbing;
}
.overlay {
position: absolute;
z-index: 2;
pointer-events: none;
}
.scene-label {
top: 14px;
left: 14px;
padding: 9px 11px;
border: 1px solid rgba(89, 221, 255, 0.2);
border-radius: 9px;
background: rgba(4, 12, 20, 0.75);
backdrop-filter: blur(8px);
color: #c9effc;
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.eye-readout {
top: 14px;
right: 14px;
min-width: 164px;
padding: 9px 11px;
border: 1px solid rgba(255, 200, 90, 0.28);
border-radius: 9px;
background: rgba(20, 14, 4, 0.74);
backdrop-filter: blur(8px);
color: var(--gold);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.7rem;
line-height: 1.55;
text-align: right;
}
.instructions {
right: 14px;
bottom: 13px;
left: 14px;
display: flex;
justify-content: space-between;
gap: 12px;
color: rgba(210, 233, 245, 0.72);
font-size: 0.69rem;
text-shadow: 0 1px 6px #000;
}
.instructions strong {
color: var(--text);
}
.projection-wrap {
position: relative;
width: 100%;
aspect-ratio: 12 / 7;
background: #06101a;
}
#projectionCanvas {
display: block;
width: 100%;
height: 100%;
}
.controls {
padding: 16px;
border-top: 1px solid var(--line);
}
.eye-console {
display: grid;
grid-template-columns: 145px minmax(0, 1fr);
gap: 14px;
align-items: center;
margin-bottom: 16px;
padding: 12px;
border: 1px solid rgba(255, 200, 90, 0.19);
border-radius: 11px;
background: rgba(27, 22, 10, 0.38);
}
.eye-console-title {
display: flex;
align-items: center;
gap: 9px;
color: var(--gold);
font-size: 0.72rem;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.eye-icon {
position: relative;
width: 27px;
height: 16px;
border: 2px solid var(--gold);
border-radius: 80% 12%;
transform: rotate(45deg);
box-shadow: 0 0 12px rgba(255, 200, 90, 0.22);
}
.eye-icon::after {
position: absolute;
top: 50%;
left: 50%;
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--gold);
content: "";
transform: translate(-50%, -50%);
}
.eye-track {
position: relative;
height: 3px;
margin-top: 12px;
border-radius: 3px;
background: rgba(255, 200, 90, 0.16);
}
.eye-track::before,
.eye-track::after {
position: absolute;
top: -4px;
width: 1px;
height: 11px;
background: rgba(255, 200, 90, 0.35);
content: "";
}
.eye-track::before {
left: 0;
}
.eye-track::after {
right: 0;
}
.eye-knob {
position: absolute;
top: 50%;
left: var(--eye-position);
width: 11px;
height: 11px;
border: 2px solid #211700;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 12px rgba(255, 200, 90, 0.75);
transform: translate(-50%, -50%);
transition: left 40ms linear;
}
.slider-list {
display: grid;
gap: 11px;
}
.slider-row {
display: grid;
grid-template-columns: 80px minmax(90px, 1fr) 54px;
gap: 9px;
align-items: center;
}
.slider-row label {
color: #b9cfdd;
font-size: 0.73rem;
font-weight: 700;
}
.slider-row output {
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.7rem;
text-align: right;
}
input[type="range"] {
width: 100%;
height: 4px;
border-radius: 10px;
outline: none;
accent-color: var(--cyan);
cursor: pointer;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 15px;
}
.button-row button {
padding: 9px 11px;
font-size: 0.74rem;
font-weight: 750;
}
#sweepButton {
border-color: rgba(89, 221, 255, 0.28);
color: var(--cyan);
}
#sweepButton[aria-pressed="true"] {
border-color: rgba(255, 200, 90, 0.45);
color: var(--gold);
background: rgba(51, 39, 13, 0.58);
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 10px 15px;
margin-top: 14px;
color: var(--muted);
font-size: 0.68rem;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--cyan);
}
.legend .gold {
background: var(--gold);
}
.legend .violet {
background: var(--violet);
}
.explanation {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: 20px;
align-items: center;
margin-top: 16px;
padding: 17px 19px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(8, 17, 28, 0.75);
color: var(--muted);
font-size: 0.79rem;
line-height: 1.65;
}
.explanation strong {
color: var(--text);
}
.formula {
padding: 9px 12px;
border: 1px solid rgba(154, 140, 255, 0.2);
border-radius: 8px;
background: rgba(21, 18, 43, 0.55);
color: #c9c1ff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.72rem;
white-space: nowrap;
}
@media (max-width: 1050px) {
.workspace {
grid-template-columns: 1fr;
}
.projection-card {
max-width: none;
}
}
@media (max-width: 650px) {
.app {
width: min(100% - 16px, 1500px);
padding-top: 14px;
}
.topbar {
align-items: flex-start;
}
.status {
display: none;
}
.eye-console {
grid-template-columns: 1fr;
}
.slider-row {
grid-template-columns: 65px minmax(90px, 1fr) 48px;
}
.explanation {
grid-template-columns: 1fr;
}
.formula {
white-space: normal;
}
.instructions span:last-child {
display: none;
}
}
</style>
</head>
<body>
<main class="app">
<header class="topbar">
<div>
<p class="eyebrow">Perspective / Voxel Study</p>
<h1>Move the eye. Rebuild the image.</h1>
<p class="subtitle">
The voxel city, lighting, and fixed perspective frame never move.
Only the eye point changes, revealing how viewpoint controls the projected drawing.
</p>
</div>
<div class="status">
<span class="status-dot"></span>
Same world · live projection
</div>
</header>
<section class="workspace">
<article class="card stage-card">
<div class="card-header">
<div class="card-title">
<span class="card-index">01</span>
Voxel world
</div>
<div class="small-note" id="voxelCount">Fixed voxel data</div>
</div>
<div class="viewport" id="viewport">
<div class="overlay scene-label">3D scene · fixed geometry</div>
<div class="overlay eye-readout" id="eyeReadout" aria-live="polite">
EYE x +0.00<br>
y +2.60 · z 12.00
</div>
<div class="overlay instructions">
<span><strong>Drag</strong> to shift the eye point</span>
<span><strong>Wheel</strong> changes viewing distance</span>
</div>
</div>
</article>
<aside class="card projection-card">
<div class="card-header">
<div class="card-title">
<span class="card-index">02</span>
Fixed perspective frame
</div>
<div class="small-note">Projection plane · z = 0</div>
</div>
<div class="projection-wrap" id="projectionWrap">
<canvas id="projectionCanvas" aria-label="Two-dimensional perspective projection of the voxel scene"></canvas>
</div>
<div class="controls">
<div class="eye-console">
<div>
<div class="eye-console-title">
<span class="eye-icon" aria-hidden="true"></span>
Eye point
</div>
<div class="eye-track" aria-hidden="true">
<span class="eye-knob"></span>
</div>
</div>
<div class="slider-list">
<div class="slider-row">
<label for="eyeX">Horizontal</label>
<input id="eyeX" type="range" min="-6" max="6" step="0.05" value="0">
<output id="eyeXValue" for="eyeX">+0.00</output>
</div>
<div class="slider-row">
<label for="eyeY">Vertical</label>
<input id="eyeY" type="range" min="-0.5" max="5.5" step="0.05" value="2.6">
<output id="eyeYValue" for="eyeY">+2.60</output>
</div>
<div class="slider-row">
<label for="eyeZ">Distance</label>
<input id="eyeZ" type="range" min="8" max="18" step="0.05" value="12">
<output id="eyeZValue" for="eyeZ">12.00</output>
</div>
</div>
</div>
<div class="button-row">
<button id="sweepButton" type="button" aria-pressed="false">Auto sweep</button>
<button id="resetButton" type="button">Reset eye</button>
</div>
<div class="legend">
<span><i></i> fixed frame</span>
<span><i class="gold"></i> eye point</span>
<span><i class="violet"></i> rebuilt raster</span>
</div>
</div>
</aside>
</section>
<footer class="explanation">
<div>
<strong>What changes?</strong> Only the eye coordinates <strong>E = (x, y, z)</strong>.
The scene and the rectangular frame remain fixed. Each projected voxel face is rebuilt by
intersecting its ray from the eye with the frame plane, so lateral movement changes scale,
convergence, and occlusion without altering the 3D model.
</div>
<div class="formula">P′ = E + ((0 − E<sub>z</sub>) / (P<sub>z</sub> − E<sub>z</sub>)) (P − E)</div>
</footer>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const FRAME = {
width: 12,
height: 7,
centerY: 2.1,
z: 0
};
const eye = {
x: 0,
y: 2.6,
z: 12
};
const viewport = document.getElementById("viewport");
const projectionWrap = document.getElementById("projectionWrap");
const projectionCanvas = document.getElementById("projectionCanvas");
const projectionContext = projectionCanvas.getContext("2d");
const eyeXInput = document.getElementById("eyeX");
const eyeYInput = document.getElementById("eyeY");
const eyeZInput = document.getElementById("eyeZ");
const eyeXValue = document.getElementById("eyeXValue");
const eyeYValue = document.getElementById("eyeYValue");
const eyeZValue = document.getElementById("eyeZValue");
const eyeReadout = document.getElementById("eyeReadout");
const sweepButton = document.getElementById("sweepButton");
const resetButton = document.getElementById("resetButton");
const voxelCountLabel = document.getElementById("voxelCount");
const clamp = THREE.MathUtils.clamp;
const signed = value => `${value >= 0 ? "+" : ""}${value.toFixed(2)}`;
let projectionDirty = true;
let sweeping = false;
let projectionSize = { width: 1, height: 1, dpr: 1 };
// ---------------------------------------------------------------------
// Three.js scene
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111c);
scene.fog = new THREE.Fog(0x07111c, 18, 42);
const camera = new THREE.PerspectiveCamera(35, FRAME.width / FRAME.height, 0.1, 80);
camera.quaternion.identity();
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional voxel scene. Drag to move the viewpoint."
);
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xa8e8ff, 0x07101b, 2.2);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(7, 12, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x29c9ff, 34, 26, 2);
cyanLight.position.set(-8, 5, -2);
scene.add(cyanLight);
const coralLight = new THREE.PointLight(0xff657d, 27, 24, 2);
coralLight.position.set(8, 4, -6);
scene.add(coralLight);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x0a1824,
roughness: 0.9,
metalness: 0.05
});
const floor = new THREE.Mesh(
new THREE.BoxGeometry(12.4, 0.24, 7.4),
floorMaterial
);
floor.position.set(0, -0.13, -3.35);
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(26, 26, 0x2a7190, 0x16384b);
grid.position.set(0, -0.26, -4.2);
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
// A fixed, sparse background gives the scene depth without changing the model.
let seed = 9137;
const random = () => {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
};
const dustPositions = [];
for (let i = 0; i < 150; i++) {
dustPositions.push(
(random() - 0.5) * 28,
random() * 13,
-1.5 - random() * 24
);
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x79bad5,
size: 0.045,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
scene.add(dust);
// ---------------------------------------------------------------------
// Immutable voxel city
// ---------------------------------------------------------------------
const VOXEL = 0.78;
const CUBE_SIZE = 0.71;
const heights = [
[1, 1, 2, 3, 4, 5, 4, 3, 2, 2, 1, 1, 1],
[1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 1, 1],
[1, 2, 3, 4, 4, 7, 6, 5, 4, 3, 2, 1, 1],
[1, 2, 3, 4, 5, 7, 7, 6, 5, 4, 3, 2, 1],
[1, 1, 2, 3, 4, 6, 6, 5, 4, 3, 2, 1, 1],
[1, 1, 1, 2, 3, 5, 5, 4, 3, 2, 1, 1, 1],
[1, 1, 1, 1, 2, 4, 4, 3, 2, 1, 1, 1, 1]
];
const localFaces = [
{
normal: [1, 0, 0],
vertices: [[1, -1, -1], [1, -1, 1], [1, 1, 1], [1, 1, -1]]
},
{
normal: [-1, 0, 0],
vertices: [[-1, -1, 1], [-1, -1, -1], [-1, 1, -1], [-1, 1, 1]]
},
{
normal: [0, 1, 0],
vertices: [[-1, 1, -1], [1, 1, -1], [1, 1, 1], [-1, 1, 1]]
},
{
normal: [0, -1, 0],
vertices: [[-1, -1, 1], [1, -1, 1], [1, -1, -1], [-1, -1, -1]]
},
{
normal: [0, 0, 1],
vertices: [[1, -1, 1], [-1, -1, 1], [-1, 1, 1], [1, 1, 1]]
},
{
normal: [0, 0, -1],
vertices: [[-1, -1, -1], [1, -1, -1], [1, 1, -1], [-1, 1, -1]]
}
];
const voxels = [];
const leftColor = new THREE.Color(0x31c9df);
const centerColor = new THREE.Color(0x8d7cf6);
const rightColor = new THREE.Color(0xf56b7d);
const accentColor = new THREE.Color(0xf4bd4f);
const baseColor = new THREE.Color(0x18384b);
for (let row = 0; row < heights.length; row++) {
for (let column = 0; column < heights[row].length; column++) {
const height = heights[row][column];
const worldX = (column - 6) * VOXEL;
const worldZ = -5.6 + row * VOXEL;
for (let layer = 0; layer < height; layer++) {
const worldY = CUBE_SIZE * 0.5 + layer * VOXEL;
let color;
if (layer === 0) {
color = baseColor;
} else if (worldX < -2.3) {
color = leftColor;
} else if (worldX < 1.8) {
color = centerColor;
} else if (worldX < 3.7) {
color = rightColor;
} else {
color = accentColor;
}
const rgb = {
r: color.r * 255,
g: color.g * 255,
b: color.b * 255
};
const half = CUBE_SIZE * 0.5;
const faces = localFaces.map(face => {
const vertices = face.vertices.map(vertex => [
worldX + vertex[0] * half,
worldY + vertex[1] * half,
worldZ + vertex[2] * half
]);
const center = vertices.reduce(
(sum, vertex) => [
sum[0] + vertex[0] / 4,
sum[1] + vertex[1] / 4,
sum[2] + vertex[2] / 4
],
[0, 0, 0]
);
return {
normal: face.normal,
vertices,
center
};
});
voxels.push({ x: worldX, y: worldY, z: worldZ, rgb, faces });
}
}
}
const voxelGeometry = new THREE.BoxGeometry(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE);
const voxelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.68,
metalness: 0.06,
vertexColors: true,
flatShading: true
});
const voxelMesh = new THREE.InstancedMesh(
voxelGeometry,
voxelMaterial,
voxels.length
);
voxelMesh.castShadow = true;
voxelMesh.receiveShadow = true;
voxelMesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
const transform = new THREE.Object3D();
const instanceColor = new THREE.Color();
voxels.forEach((voxel, index) => {
transform.position.set(voxel.x, voxel.y, voxel.z);
transform.updateMatrix();
voxelMesh.setMatrixAt(index, transform.matrix);
instanceColor.setRGB(
voxel.rgb.r / 255,
voxel.rgb.g / 255,
voxel.rgb.b / 255
);
voxelMesh.setColorAt(index, instanceColor);
});
voxelMesh.instanceMatrix.needsUpdate = true;
voxelMesh.instanceColor.needsUpdate = true;
scene.add(voxelMesh);
voxelCountLabel.textContent = `${voxels.length} immutable voxels`;
// The frame is fixed in world space. Its corner brackets make the
// construction visible while remaining close to the viewport edges.
const inset = 0.25;
const arm = 0.5;
const left = -FRAME.width * 0.5 + inset;
const right = FRAME.width * 0.5 - inset;
const bottom = FRAME.centerY - FRAME.height * 0.5 + inset;
const top = FRAME.centerY + FRAME.height * 0.5 - inset;
const framePoints = [
left, bottom, FRAME.z, left + arm, bottom, FRAME.z,
left, bottom, FRAME.z, left, bottom + arm, FRAME.z,
right - arm, bottom, FRAME.z, right, bottom, FRAME.z,
right, bottom, FRAME.z, right, bottom + arm, FRAME.z,
left, top, FRAME.z, left + arm, top, FRAME.z,
left, top, FRAME.z, left, top - arm, FRAME.z,
right - arm, top, FRAME.z, right, top, FRAME.z,
right, top, FRAME.z, right, top - arm, FRAME.z
];
const frameGeometry = new THREE.BufferGeometry();
frameGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(framePoints, 3)
);
const frameLines = new THREE.LineSegments(
frameGeometry,
new THREE.LineBasicMaterial({
color: 0x59ddff,
transparent: true,
opacity: 0.7
})
);
scene.add(frameLines);
// ---------------------------------------------------------------------
// Off-axis perspective camera
// ---------------------------------------------------------------------
//
// The camera keeps looking down -Z, while its frustum is sheared so the
// fixed z=0 frame always occupies the same portion of the viewport.
// This is the 3D equivalent of projecting from a movable eye onto a
// stationary picture plane.
function updateCamera() {
const distance = eye.z - FRAME.z;
const near = camera.near;
const far = camera.far;
const frustumLeft = (-FRAME.width * 0.5 - eye.x) * near / distance;
const frustumRight = (FRAME.width * 0.5 - eye.x) * near / distance;
const frustumBottom =
(FRAME.centerY - FRAME.height * 0.5 - eye.y) * near / distance;
const frustumTop =
(FRAME.centerY + FRAME.height * 0.5 - eye.y) * near / distance;
camera.position.set(eye.x, eye.y, eye.z);
camera.quaternion.identity();
camera.aspect = FRAME.width / FRAME.height;
camera.projectionMatrix.makePerspective(
frustumLeft,
frustumRight,
frustumTop,
frustumBottom,
near,
far
);
camera.projectionMatrixInverse.copy(camera.projectionMatrix).invert();
camera.updateMatrixWorld(true);
camera.matrixWorldInverse.copy(camera.matrixWorld).invert();
}
// ---------------------------------------------------------------------
// 2D projection renderer
// ---------------------------------------------------------------------
const gridSegments = [];
for (let x = -7; x <= 7; x++) {
gridSegments.push([x, 0, -6.4], [x, 0, -0.2]);
}
for (let z = -6.4; z <= -0.2; z += 0.8) {
gridSegments.push([-7, 0, z], [7, 0, z]);
}
function projectToFrame(x, y, z, frameRect) {
const denominator = z - eye.z;
const t = (FRAME.z - eye.z) / denominator;
const projectedX = eye.x + t * (x - eye.x);
const projectedY = eye.y + t * (y - eye.y);
return {
x: frameRect.x +
(projectedX + FRAME.width * 0.5) / FRAME.width * frameRect.width,
y: frameRect.y +
(1 - (projectedY - (FRAME.centerY - FRAME.height * 0.5)) /
FRAME.height) * frameRect.height
};
}
function drawProjection() {
const ctx = projectionContext;
const width = projectionSize.width;
const height = projectionSize.height;
const margin = Math.max(7, Math.min(width, height) * 0.025);
ctx.setTransform(projectionSize.dpr, 0, 0, projectionSize.dpr, 0, 0);
ctx.clearRect(0, 0, width, height);
const frameRect = {
x: margin,
y: margin,
width: width - margin * 2,
height: height - margin * 2
};
const background = ctx.createLinearGradient(
frameRect.x,
frameRect.y,
frameRect.x,
frameRect.y + frameRect.height
);
background.addColorStop(0, "#0b1b2a");
background.addColorStop(1, "#07111b");
ctx.fillStyle = "#050b12";
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = background;
ctx.fillRect(frameRect.x, frameRect.y, frameRect.width, frameRect.height);
ctx.save();
ctx.beginPath();
ctx.rect(frameRect.x, frameRect.y, frameRect.width, frameRect.height);
ctx.clip();
// Perspective grid projected from the world onto the fixed frame.
ctx.strokeStyle = "rgba(89, 221, 255, 0.14)";
ctx.lineWidth = 0.8;
ctx.beginPath();
for (const segment of gridSegments) {
const a = projectToFrame(
segment[0], segment[1], segment[2], frameRect
);
const b = projectToFrame(
segment[3], segment[4], segment[5], frameRect
);
ctx.moveTo(a.x, a.y);
ctx.lineTo(b.x, b.y);
}
ctx.stroke();
// Build and sort visible voxel faces.
const visibleFaces = [];
for (const voxel of voxels) {
for (const face of voxel.faces) {
const toEyeX = eye.x - face.center[0];
const toEyeY = eye.y - face.center[1];
const toEyeZ = eye.z - face.center[2];
const normal = face.normal;
const facing =
normal[0] * toEyeX +
normal[1] * toEyeY +
normal[2] * toEyeZ;
if (facing <= 0) continue;
const projected = face.vertices.map(vertex =>
projectToFrame(vertex[0], vertex[1], vertex[2], frameRect)
);
const depth =
toEyeX * toEyeX +
toEyeY * toEyeY +
toEyeZ * toEyeZ;
visibleFaces.push({
projected,
depth,
rgb: voxel.rgb,
normal
});
}
}
visibleFaces.sort((a, b) => b.depth - a.depth);
ctx.lineJoin = "round";
ctx.lineWidth = 0.55;
for (const face of visibleFaces) {
const normal = face.normal;
let shade;
if (normal[1] > 0.5) shade = 1.18;
else if (normal[1] < -0.5) shade = 0.48;
else if (normal[0] !== 0) shade = 0.70;
else shade = 0.90;
const r = Math.min(255, Math.round(face.rgb.r * shade));
const g = Math.min(255, Math.round(face.rgb.g * shade));
const b = Math.min(255, Math.round(face.rgb.b * shade));
ctx.beginPath();
ctx.moveTo(face.projected[0].x, face.projected[0].y);
for (let index = 1; index < face.projected.length; index++) {
ctx.lineTo(face.projected[index].x, face.projected[index].y);
}
ctx.closePath();
ctx.fillStyle = `rgb(${r}, ${g}, ${b})`;
ctx.fill();
ctx.strokeStyle = "rgba(3, 9, 16, 0.58)";
ctx.stroke();
}
// A restrained vignette keeps the drawing legible.
const vignette = ctx.createRadialGradient(
frameRect.x + frameRect.width * 0.5,
frameRect.y + frameRect.height * 0.45,
frameRect.width * 0.1,
frameRect.x + frameRect.width * 0.5,
frameRect.y + frameRect.height * 0.5,
frameRect.width * 0.75
);
vignette.addColorStop(0, "rgba(0,0,0,0)");
vignette.addColorStop(1, "rgba(0,4,10,0.42)");
ctx.fillStyle = vignette;
ctx.fillRect(
frameRect.x,
frameRect.y,
frameRect.width,
frameRect.height
);
ctx.restore();
// Fixed frame border.
ctx.strokeStyle = "rgba(89, 221, 255, 0.92)";
ctx.lineWidth = 1.4;
ctx.strokeRect(
frameRect.x,
frameRect.y,
frameRect.width,
frameRect.height
);
// Brighter corners.
const corner = Math.min(24, frameRect.width * 0.08);
ctx.strokeStyle = "#ffc85a";
ctx.lineWidth = 2.4;
ctx.beginPath();
ctx.moveTo(frameRect.x, frameRect.y + corner);
ctx.lineTo(frameRect.x, frameRect.y);
ctx.lineTo(frameRect.x + corner, frameRect.y);
ctx.moveTo(frameRect.x + frameRect.width - corner, frameRect.y);
ctx.lineTo(frameRect.x + frameRect.width, frameRect.y);
ctx.lineTo(frameRect.x + frameRect.width, frameRect.y + corner);
ctx.moveTo(frameRect.x, frameRect.y + frameRect.height - corner);
ctx.lineTo(frameRect.x, frameRect.y + frameRect.height);
ctx.lineTo(frameRect.x + corner, frameRect.y + frameRect.height);
ctx.moveTo(frameRect.x + frameRect.width - corner, frameRect.y + frameRect.height);
ctx.lineTo(frameRect.x + frameRect.width, frameRect.y + frameRect.height);
ctx.lineTo(frameRect.x + frameRect.width, frameRect.y + frameRect.height - corner);
ctx.stroke();
ctx.font = "700 9px ui-monospace, SFMono-Regular, Menlo, monospace";
const label = "FIXED FRAME · z = 0";
const labelWidth = ctx.measureText(label).width + 12;
ctx.fillStyle = "rgba(3, 10, 17, 0.78)";
ctx.fillRect(
frameRect.x + 7,
frameRect.y + 7,
labelWidth,
17
);
ctx.fillStyle = "#9edff0";
ctx.fillText(label, frameRect.x + 13, frameRect.y + 19);
}
// ---------------------------------------------------------------------
// UI and input
// ---------------------------------------------------------------------
function stopSweep() {
if (!sweeping) return;
sweeping = false;
sweepButton.setAttribute("aria-pressed", "false");
sweepButton.textContent = "Auto sweep";
}
function syncUI() {
eyeXInput.value = eye.x.toFixed(2);
eyeYInput.value = eye.y.toFixed(2);
eyeZInput.value = eye.z.toFixed(2);
eyeXValue.textContent = signed(eye.x);
eyeYValue.textContent = signed(eye.y);
eyeZValue.textContent = eye.z.toFixed(2);
eyeReadout.innerHTML =
`EYE x ${signed(eye.x)}<br>` +
`y ${signed(eye.y)} · z ${eye.z.toFixed(2)}`;
const percentage = 50 + eye.x / 6 * 50;
document.documentElement.style.setProperty(
"--eye-position",
`${clamp(percentage, 0, 100).toFixed(2)}%`
);
}
eyeXInput.addEventListener("input", () => {
stopSweep();
eye.x = Number(eyeXInput.value);
syncUI();
projectionDirty = true;
});
eyeYInput.addEventListener("input", () => {
stopSweep();
eye.y = Number(eyeYInput.value);
syncUI();
projectionDirty = true;
});
eyeZInput.addEventListener("input", () => {
stopSweep();
eye.z = Number(eyeZInput.value);
syncUI();
projectionDirty = true;
});
sweepButton.addEventListener("click", () => {
sweeping = !sweeping;
sweepButton.setAttribute("aria-pressed", String(sweeping));
sweepButton.textContent = sweeping ? "Pause sweep" : "Auto sweep";
});
resetButton.addEventListener("click", () => {
stopSweep();
eye.x = 0;
eye.y = 2.6;
eye.z = 12;
syncUI();
projectionDirty = true;
});
const pointer = {
active: false,
x: 0,
y: 0
};
renderer.domElement.addEventListener("pointerdown", event => {
stopSweep();
pointer.active = true;
pointer.x = event.clientX;
pointer.y = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.focus({ preventScroll: true });
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointer.active) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
eye.x = clamp(eye.x + dx * 0.035, -6, 6);
eye.y = clamp(eye.y - dy * 0.025, -0.5, 5.5);
syncUI();
projectionDirty = true;
});
function endPointer(event) {
pointer.active = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
stopSweep();
eye.z = clamp(eye.z + event.deltaY * 0.008, 8, 18);
syncUI();
projectionDirty = true;
},
{ passive: false }
);
renderer.domElement.addEventListener("keydown", event => {
let handled = true;
stopSweep();
switch (event.key) {
case "ArrowLeft":
eye.x = clamp(eye.x - 0.2, -6, 6);
break;
case "ArrowRight":
eye.x = clamp(eye.x + 0.2, -6, 6);
break;
case "ArrowUp":
eye.y = clamp(eye.y + 0.2, -0.5, 5.5);
break;
case "ArrowDown":
eye.y = clamp(eye.y - 0.2, -0.5, 5.5);
break;
case "+":
case "=":
eye.z = clamp(eye.z - 0.5, 8, 18);
break;
case "-":
case "_":
eye.z = clamp(eye.z + 0.5, 8, 18);
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
syncUI();
projectionDirty = true;
}
});
function resizeRenderer() {
const bounds = viewport.getBoundingClientRect();
renderer.setSize(
Math.max(1, Math.round(bounds.width)),
Math.max(1, Math.round(bounds.height)),
false
);
projectionDirty = true;
}
function resizeProjection() {
const bounds = projectionWrap.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio || 1, 2);
projectionSize = {
width: Math.max(1, bounds.width),
height: Math.max(1, bounds.height),
dpr
};
projectionCanvas.width = Math.round(bounds.width * dpr);
projectionCanvas.height = Math.round(bounds.height * dpr);
projectionDirty = true;
}
const rendererResizeObserver = new ResizeObserver(resizeRenderer);
rendererResizeObserver.observe(viewport);
const projectionResizeObserver = new ResizeObserver(resizeProjection);
projectionResizeObserver.observe(projectionWrap);
window.addEventListener("resize", () => {
resizeRenderer();
resizeProjection();
});
syncUI();
updateCamera();
resizeRenderer();
resizeProjection();
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
function animate(now) {
requestAnimationFrame(animate);
if (sweeping) {
const phase = now * 0.00072;
eye.x = Math.sin(phase) * 5.15;
eye.y = 2.6 + Math.sin(phase * 0.61) * 0.55;
syncUI();
projectionDirty = true;
}
if (projectionDirty) {
updateCamera();
drawProjection();
projectionDirty = false;
}
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```