Prompt specification
Build or customize the Three.js 3D scene "Night-Sky Observation Courtyard" 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: Night-Sky Observation Courtyard
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/night-sky-observation-courtyard-x3791
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing lunar phases as seen from different positions. Build an original courtyard with a schematic moon-phase display and a small telescope. Let users select an orbital position and inspect the corresponding visible illuminated portion. Voxel style.
Working Three.js Starter (night-sky-observation-courtyard-x3791):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lunar Courtyard — Voxel Phase Observatory</title>
<style>
:root {
--ink: #f4ead7;
--muted: #a9b2bb;
--panel: rgba(16, 22, 30, 0.84);
--line: rgba(255, 255, 255, 0.12);
--gold: #f4bd62;
--gold-dark: #9f7135;
--cyan: #8ed6d1;
--shadow: 0 18px 50px rgba(0, 0, 0, 0.32);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101722;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.scope-mask {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
opacity: 0;
transition: opacity 450ms ease;
background:
radial-gradient(
circle at 50% 49%,
transparent 0%,
transparent 25%,
rgba(2, 5, 9, 0.32) 39%,
rgba(2, 5, 9, 0.96) 67%
);
}
.scope-mask.active {
opacity: 1;
}
.reticle {
position: absolute;
top: 50%;
left: 50%;
width: 34px;
height: 34px;
transform: translate(-50%, -50%);
opacity: 0.62;
}
.reticle::before,
.reticle::after {
content: "";
position: absolute;
background: rgba(244, 189, 98, 0.75);
}
.reticle::before {
width: 34px;
height: 1px;
top: 16px;
}
.reticle::after {
width: 1px;
height: 34px;
left: 16px;
}
header {
position: fixed;
z-index: 10;
top: 22px;
left: 24px;
display: flex;
align-items: center;
gap: 13px;
pointer-events: none;
text-shadow: 0 2px 14px rgba(0, 0, 0, 0.55);
}
.brand-mark {
position: relative;
width: 40px;
height: 40px;
border: 2px solid var(--gold);
box-shadow:
inset -10px 0 0 var(--gold),
0 0 24px rgba(244, 189, 98, 0.16);
}
.brand-mark::after {
content: "";
position: absolute;
width: 6px;
height: 6px;
left: 5px;
top: 5px;
background: var(--ink);
box-shadow:
18px 4px 0 rgba(244, 234, 215, 0.55),
4px 17px 0 rgba(244, 234, 215, 0.35),
20px 21px 0 rgba(244, 234, 215, 0.2);
}
.brand-kicker {
margin: 0 0 3px;
color: var(--gold);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.22em;
}
h1 {
margin: 0;
font-size: clamp(18px, 2vw, 26px);
line-height: 1;
letter-spacing: 0.08em;
font-weight: 750;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
background:
linear-gradient(135deg, rgba(255, 255, 255, 0.035), transparent 48%),
var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(13px);
-webkit-backdrop-filter: blur(13px);
}
.phase-panel {
top: 104px;
left: 24px;
width: 246px;
padding: 18px;
}
.eyebrow {
color: var(--gold);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.18em;
text-transform: uppercase;
}
#phaseName {
margin: 10px 0 4px;
font-size: 25px;
line-height: 1.08;
letter-spacing: -0.025em;
}
#phaseDirection {
min-height: 18px;
margin: 0;
color: var(--muted);
font-size: 12px;
}
.illumination {
display: flex;
align-items: flex-end;
justify-content: space-between;
margin-top: 18px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.illumination strong {
color: var(--gold);
font: 700 34px/0.9 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: -0.06em;
}
.illumination span {
color: var(--muted);
font: 700 9px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
text-align: right;
}
.voxel-meter {
height: 11px;
margin-top: 12px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.13);
background:
repeating-linear-gradient(
90deg,
transparent 0 5px,
rgba(0, 0, 0, 0.5) 5px 6px
);
}
#illuminationFill {
width: 0;
height: 100%;
background:
repeating-linear-gradient(
90deg,
var(--gold) 0 5px,
#d69a47 5px 6px
);
box-shadow: 0 0 18px rgba(244, 189, 98, 0.42);
transition: width 120ms ease;
}
.position-row {
display: flex;
justify-content: space-between;
margin-top: 15px;
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.09em;
}
#positionValue {
color: var(--ink);
}
.phase-note {
margin: 15px 0 0;
padding-top: 13px;
border-top: 1px solid var(--line);
color: #c4c8c8;
font-size: 11px;
line-height: 1.5;
}
.orbit-panel {
top: 104px;
right: 24px;
width: 276px;
padding: 18px;
}
.panel-title {
margin: 7px 0 16px;
font-size: 17px;
letter-spacing: -0.01em;
}
.range-labels {
display: flex;
justify-content: space-between;
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 14px 0 17px;
appearance: none;
border-radius: 0;
background: #3b4248;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
appearance: none;
border: 3px solid #151b22;
border-radius: 0;
background: var(--gold);
box-shadow: 0 0 0 1px var(--gold);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #151b22;
border-radius: 0;
background: var(--gold);
box-shadow: 0 0 0 1px var(--gold);
}
.phase-buttons {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 7px;
}
button {
min-height: 36px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 0;
background: rgba(255, 255, 255, 0.045);
color: var(--ink);
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(244, 189, 98, 0.65);
background: rgba(244, 189, 98, 0.1);
}
button:active {
transform: translateY(1px);
}
.phase-buttons button {
padding: 7px;
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
}
.view-buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 16px;
padding-top: 16px;
border-top: 1px solid var(--line);
}
.view-buttons button {
padding: 8px;
font-size: 11px;
font-weight: 700;
}
.view-buttons .wide {
grid-column: 1 / -1;
}
.instructions {
margin: 14px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.instructions b {
color: var(--ink);
font-weight: 650;
}
.bottom-note {
position: fixed;
z-index: 10;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
padding: 9px 14px;
border: 1px solid rgba(255, 255, 255, 0.11);
background: rgba(10, 15, 21, 0.62);
color: #bdc4c7;
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.09em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
backdrop-filter: blur(8px);
}
.bottom-note span {
color: var(--gold);
}
@media (max-width: 760px) {
header {
top: 14px;
left: 14px;
}
.brand-mark {
width: 34px;
height: 34px;
}
.phase-panel {
top: 76px;
left: 12px;
width: 205px;
padding: 14px;
}
#phaseName {
font-size: 20px;
}
.phase-note,
.position-row {
display: none;
}
.orbit-panel {
top: auto;
right: 12px;
bottom: 54px;
width: min(300px, calc(100vw - 24px));
padding: 14px;
}
.orbit-panel .panel-title {
margin-bottom: 10px;
}
.instructions {
display: none;
}
.bottom-note {
bottom: 12px;
max-width: calc(100vw - 24px);
overflow: hidden;
text-overflow: ellipsis;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="scope-mask" id="scopeMask" aria-hidden="true">
<div class="reticle"></div>
</div>
<header>
<div class="brand-mark" aria-hidden="true"></div>
<div>
<p class="brand-kicker">Voxel observatory no. 07</p>
<h1>LUNAR COURTYARD</h1>
</div>
</header>
<section class="panel phase-panel" aria-live="polite">
<div class="eyebrow">Current phase</div>
<h2 id="phaseName">Waxing Gibbous</h2>
<p id="phaseDirection">More than half illuminated</p>
<div class="illumination">
<strong id="illuminationValue">85%</strong>
<span>VISIBLE LIT<br />FRACTION</span>
</div>
<div class="voxel-meter" aria-hidden="true">
<div id="illuminationFill"></div>
</div>
<div class="position-row">
<span>ORBITAL POSITION</span>
<span id="positionValue">135°</span>
</div>
<p class="phase-note">
The observer stands at the courtyard's center. Sunlight arrives from the
eastern marker, so the moon's position determines its illuminated shape.
</p>
</section>
<section class="panel orbit-panel">
<div class="eyebrow">Orbital position</div>
<h2 class="panel-title">Choose a phase</h2>
<div class="range-labels">
<span>NEW</span>
<span>FULL</span>
<span>NEW</span>
</div>
<input
id="phaseRange"
type="range"
min="0"
max="360"
step="1"
value="135"
aria-label="Moon orbital position in degrees"
/>
<div class="phase-buttons">
<button type="button" data-deg="0">NEW MOON</button>
<button type="button" data-deg="90">FIRST QTR</button>
<button type="button" data-deg="180">FULL MOON</button>
<button type="button" data-deg="270">LAST QTR</button>
</div>
<div class="view-buttons">
<button type="button" id="inspectButton">INSPECT PHASE</button>
<button type="button" id="scopeButton">USE TELESCOPE</button>
<button type="button" class="wide" id="resetButton">RETURN TO COURTYARD</button>
</div>
<p class="instructions">
<b>Drag</b> the gold moon around the orbit, click an orbit block, or use
the slider. <b>Drag the scene</b> to orbit the camera and scroll to zoom.
</p>
</section>
<div class="bottom-note">
<span>Observation:</span> courtyard phase array · click the moon display or telescope to inspect
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const host = document.querySelector("#scene");
const phaseRange = document.querySelector("#phaseRange");
const phaseName = document.querySelector("#phaseName");
const phaseDirection = document.querySelector("#phaseDirection");
const illuminationValue = document.querySelector("#illuminationValue");
const illuminationFill = document.querySelector("#illuminationFill");
const positionValue = document.querySelector("#positionValue");
const scopeMask = document.querySelector("#scopeMask");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x111a27);
scene.fog = new THREE.Fog(0x111a27, 23, 43);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.05,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
host.appendChild(renderer.domElement);
const clock = new THREE.Clock();
// ------------------------------------------------------------------
// Materials and reusable geometry
// ------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
...options
});
const mats = {
plaster: material(0xcbbca1),
plasterLight: material(0xe1d4b9),
plasterDark: material(0xa99a83),
terracotta: material(0xa9563e),
terracottaDark: material(0x743b31),
sandstone: material(0x8e806b),
darkStone: material(0x303a43),
blue: material(0x31596a, { metalness: 0.18, roughness: 0.42 }),
water: material(0x3f8191, { metalness: 0.35, roughness: 0.18 }),
gold: material(0xe2a94d, { emissive: 0x5b3510, emissiveIntensity: 0.35 }),
goldBright: material(0xffd37d, {
emissive: 0xb56c1d,
emissiveIntensity: 0.7
}),
foliage: material(0x486c4a),
foliageLight: material(0x63845a),
trunk: material(0x604331),
black: material(0x10151c),
ivory: material(0xe6dbc4),
cyan: material(0x8bcac6, {
emissive: 0x2b7778,
emissiveIntensity: 0.5
}),
lamp: material(0xffc66e, {
emissive: 0xff8b28,
emissiveIntensity: 2.5
})
};
function box(w, h, d, mat, x, y, z, parent = scene, cast = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function beamBetween(a, b, width, mat, parent = scene) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = box(width, length, width, mat, 0, 0, 0, parent);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return mesh;
}
function textPlane(
text,
width,
height,
options = {}
) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (options.background) {
ctx.fillStyle = options.background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
if (options.border) {
ctx.strokeStyle = options.border;
ctx.lineWidth = 10;
ctx.strokeRect(5, 5, canvas.width - 10, canvas.height - 10);
}
ctx.fillStyle = options.color || "#eadfc9";
ctx.font = `${options.weight || 700} ${options.size || 78}px ui-monospace, monospace`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide,
toneMapped: false
})
);
return mesh;
}
// ------------------------------------------------------------------
// Lighting
// ------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb8d9ff, 0x3b2524, 1.65));
const sunLight = new THREE.DirectionalLight(0xffd9a0, 3.1);
sunLight.position.set(-6, 13, 8);
sunLight.castShadow = true;
sunLight.shadow.mapSize.set(2048, 2048);
sunLight.shadow.camera.left = -11;
sunLight.shadow.camera.right = 11;
sunLight.shadow.camera.top = 11;
sunLight.shadow.camera.bottom = -11;
sunLight.shadow.camera.near = 1;
sunLight.shadow.camera.far = 35;
sunLight.shadow.bias = -0.00025;
scene.add(sunLight);
const courtyardLight = new THREE.PointLight(0xffa75d, 35, 12, 2);
courtyardLight.position.set(0, 4.5, 1.5);
scene.add(courtyardLight);
// ------------------------------------------------------------------
// Courtyard architecture and voxel tiles
// ------------------------------------------------------------------
box(14.6, 0.45, 14.6, mats.darkStone, 0, -0.24, 0);
const tileGeometry = new THREE.BoxGeometry(0.96, 0.13, 0.96);
const tileMaterial = material(0xffffff);
const tileCount = 14 * 14;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCount);
const dummy = new THREE.Object3D();
const tileColors = [0xb9aa91, 0xc8b79b, 0xa99982, 0xd0c0a4];
let tileIndex = 0;
for (let z = 0; z < 14; z++) {
for (let x = 0; x < 14; x++) {
dummy.position.set(x - 6.5, 0.06, z - 6.5);
dummy.rotation.y = ((x + z) % 2) * 0.008;
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const colorIndex =
(x * 3 + z * 5 + ((x * z) % 3)) % tileColors.length;
tiles.setColorAt(tileIndex, new THREE.Color(tileColors[colorIndex]));
tileIndex++;
}
}
tiles.receiveShadow = true;
scene.add(tiles);
// Back and side walls.
box(14.5, 3.8, 0.55, mats.plaster, 0, 1.9, -7.0);
box(14.6, 0.46, 0.64, mats.terracotta, 0, 3.18, -6.98);
box(0.55, 2.7, 13.5, mats.plaster, -7.0, 1.35, -0.15);
box(0.55, 2.7, 13.5, mats.plaster, 7.0, 1.35, -0.15);
box(0.66, 0.42, 13.6, mats.terracotta, -6.97, 2.16, -0.15);
box(0.66, 0.42, 13.6, mats.terracotta, 6.97, 2.16, -0.15);
// Front wall is intentionally low and open.
box(4.1, 0.65, 0.48, mats.plaster, -5.1, 0.32, 6.75);
box(4.1, 0.65, 0.48, mats.plaster, 5.1, 0.32, 6.75);
box(2.0, 0.22, 1.15, mats.sandstone, 0, -0.03, 7.05);
box(2.7, 0.22, 1.25, mats.sandstone, 0, -0.24, 7.75);
// Columns and raised lintel blocks.
[-5.9, -2.65, 2.65, 5.9].forEach((x) => {
box(0.68, 4.7, 0.72, mats.plasterLight, x, 2.35, -6.6);
box(0.88, 0.26, 0.9, mats.terracotta, x, 0.18, -6.6);
box(0.88, 0.28, 0.9, mats.terracotta, x, 4.62, -6.6);
});
// Recessed wall niches.
[-4.35, 4.35].forEach((x) => {
box(1.05, 1.65, 0.15, mats.darkStone, x, 2.1, -6.68);
box(1.32, 0.16, 0.28, mats.terracottaDark, x, 3.0, -6.58);
box(0.16, 1.8, 0.26, mats.terracottaDark, x - 0.59, 2.1, -6.58);
box(0.16, 1.8, 0.26, mats.terracottaDark, x + 0.59, 2.1, -6.58);
});
// ------------------------------------------------------------------
// Phase display board
// ------------------------------------------------------------------
const displayPoint = new THREE.Vector3(0, 3.42, -6.03);
box(3.65, 3.65, 0.32, mats.darkStone, 0, 3.42, -6.25);
box(3.92, 0.18, 0.46, mats.terracotta, 0, 5.27, -6.2);
box(3.92, 0.18, 0.46, mats.terracotta, 0, 1.57, -6.2);
box(0.18, 3.88, 0.46, mats.terracotta, -1.87, 3.42, -6.2);
box(0.18, 3.88, 0.46, mats.terracotta, 1.87, 3.42, -6.2);
// Pixel-like halo around the phase window.
for (let i = 0; i < 28; i++) {
const a = (i / 28) * Math.PI * 2;
const halo = box(
0.16,
0.16,
0.14,
i % 2 ? mats.gold : mats.goldBright,
Math.cos(a) * 1.52,
3.42 + Math.sin(a) * 1.52,
-6.01,
scene,
false
);
halo.rotation.z = a;
}
const moonShader = new THREE.ShaderMaterial({
uniforms: {
uTheta: { value: (135 * Math.PI) / 180 }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
precision highp float;
uniform float uTheta;
varying vec2 vUv;
float hash(vec2 p) {
p = fract(p * vec2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float crater(vec2 p) {
vec2 grid = p * 4.2;
vec2 id = floor(grid);
vec2 f = fract(grid) - 0.5;
float h = hash(id);
float radius = mix(0.13, 0.28, step(0.56, h));
float d = length(f);
float shape = 1.0 - smoothstep(radius * 0.72, radius, d);
return shape * step(0.56, h);
}
void main() {
vec2 p = (vUv - 0.5) * 2.0;
// Pixel grid: snap the shader coordinates into square moon cells.
float cells = 30.0;
vec2 pixelP = floor((p + 1.0) * cells) / cells;
pixelP = (pixelP * 2.0 - 1.0) + vec2(1.0 / cells);
pixelP = clamp(pixelP, vec2(-1.0), vec2(1.0));
float radius = length(pixelP);
if (radius > 0.985) discard;
float y = pixelP.y;
float edge = sqrt(max(0.0, 1.0 - y * y));
bool waxing = uTheta <= 3.14159265;
float terminator = waxing
? cos(uTheta) * edge
: -cos(uTheta) * edge;
bool illuminated = waxing
? pixelP.x > terminator
: pixelP.x < terminator;
float mottling = hash(floor(pixelP * 18.0));
float pits = crater(pixelP);
vec3 litColor = mix(
vec3(0.56, 0.60, 0.63),
vec3(0.88, 0.84, 0.74),
mottling * 0.34
);
litColor *= 1.0 - pits * 0.38;
vec3 darkColor = vec3(0.025, 0.034, 0.050);
darkColor += vec3(0.012, 0.015, 0.018) * mottling;
vec3 color = illuminated ? litColor : darkColor;
// Fine voxel seams.
vec2 local = fract((p + 1.0) * cells);
vec2 seamDistance = min(local, 1.0 - local);
float seam = smoothstep(0.0, 0.07, min(seamDistance.x, seamDistance.y));
color *= mix(0.83, 1.0, seam);
// Soft pixelated rim.
float rim = smoothstep(0.91, 0.985, radius);
color = mix(color, vec3(0.95, 0.88, 0.70), rim * 0.22);
gl_FragColor = vec4(color, 1.0);
}
`,
side: THREE.DoubleSide
});
const phaseDisplay = new THREE.Mesh(
new THREE.CircleGeometry(1.34, 64),
moonShader
);
phaseDisplay.position.copy(displayPoint);
phaseDisplay.userData.action = "display";
scene.add(phaseDisplay);
// Display supports.
box(0.2, 1.5, 0.25, mats.terracottaDark, -1.2, 0.78, -6.17);
box(0.2, 1.5, 0.25, mats.terracottaDark, 1.2, 0.78, -6.17);
box(2.8, 0.16, 0.72, mats.darkStone, 0, 0.14, -6.15);
const title = textPlane("SELENE / PHASE ARRAY", 3.9, 0.68, {
background: "#17212a",
border: "#a9563e",
color: "#f2d39b",
size: 64
});
title.position.set(0, 5.67, -6.05);
scene.add(title);
// ------------------------------------------------------------------
// Orbital diagram
// ------------------------------------------------------------------
const orbitCenterZ = 0.65;
const orbitRadius = 3.05;
const orbitBeads = [];
// Square pedestal and observer at the center.
box(2.1, 0.16, 2.1, mats.sandstone, 0, 0.22, orbitCenterZ);
box(1.25, 0.25, 1.25, mats.darkStone, 0, 0.39, orbitCenterZ);
box(0.78, 0.48, 0.78, mats.blue, 0, 0.75, orbitCenterZ);
box(0.55, 0.16, 0.55, mats.cyan, 0, 1.07, orbitCenterZ);
for (let i = 0; i < 32; i++) {
const angle = (i / 32) * Math.PI * 2;
const beadMaterial = mats.gold.clone();
const bead = box(
0.22,
0.18,
0.22,
beadMaterial,
Math.cos(angle) * orbitRadius,
0.28,
orbitCenterZ + Math.sin(angle) * orbitRadius
);
bead.rotation.y = angle;
bead.userData.selectTheta = angle;
orbitBeads.push(bead);
scene.add(bead);
}
// Sun marker to the east.
const sunMarker = new THREE.Group();
sunMarker.position.set(4.95, 0.24, orbitCenterZ);
scene.add(sunMarker);
box(0.62, 0.62, 0.62, mats.goldBright, 0, 0.4, 0, sunMarker);
box(0.92, 0.18, 0.18, mats.gold, 0, 0.4, 0, sunMarker);
box(0.18, 0.92, 0.18, mats.gold, 0, 0.4, 0, sunMarker);
box(0.18, 0.18, 0.92, mats.gold, 0, 0.4, 0, sunMarker);
const sunLabel = textPlane("SUN", 1.15, 0.35, {
background: "#50351f",
color: "#ffd78f",
size: 92
});
sunLabel.rotation.x = -Math.PI / 2;
sunLabel.position.set(5.0, 0.18, 1.55);
scene.add(sunLabel);
// Short sunlight direction blocks.
for (let i = 0; i < 4; i++) {
box(0.34, 0.045, 0.12, mats.gold, 3.45 + i * 0.34, 0.19, orbitCenterZ);
}
const observerLabel = textPlane("OBSERVER", 1.8, 0.38, {
background: "#243642",
color: "#bde2df",
size: 80
});
observerLabel.rotation.x = -Math.PI / 2;
observerLabel.position.set(0, 0.19, 2.0);
scene.add(observerLabel);
[
["N", 0, -3.35],
["E", 3.8, 0.65],
["S", 0, 4.65],
["W", -3.8, 0.65]
].forEach(([text, x, z]) => {
const label = textPlane(text, 0.65, 0.65, {
color: "#d6cbb5",
size: 130
});
label.rotation.x = -Math.PI / 2;
label.position.set(x, 0.18, z);
scene.add(label);
});
// Selected voxel moon.
const moonMarker = new THREE.Group();
moonMarker.userData.action = "dragMoon";
scene.add(moonMarker);
const markerVoxels = [];
for (let y = -1; y <= 1; y++) {
for (let x = -1; x <= 1; x++) {
const distance = Math.hypot(x, y);
if (distance > 1.45) continue;
const scale = 0.19 * (1 - distance * 0.12);
const z = Math.sqrt(Math.max(0.05, 1 - (x / 1.5) ** 2 - (y / 1.5) ** 2)) * 0.17;
const voxelMaterial = material(0xffffff, {
emissive: 0x000000,
emissiveIntensity: 0
});
const voxel = box(
scale,
scale,
scale,
voxelMaterial,
x * 0.205,
y * 0.205,
z,
moonMarker
);
voxel.userData.phaseX = x / 1.45;
voxel.userData.phaseY = y / 1.45;
markerVoxels.push(voxel);
}
}
// ------------------------------------------------------------------
// Garden details
// ------------------------------------------------------------------
function makeTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
box(1.35, 0.5, 1.35, mats.terracottaDark, 0, 0.25, 0, group);
box(1.08, 0.18, 1.08, mats.terracotta, 0, 0.55, 0, group);
box(0.24, 1.65, 0.24, mats.trunk, 0, 1.15, 0, group);
const leaves = [
[-0.38, 1.72, 0.05, 0.78],
[0.38, 1.78, -0.08, 0.9],
[0, 2.28, 0.02, 0.95],
[-0.12, 1.55, 0.35, 0.82],
[0.38, 1.3, 0.3, 0.68],
[-0.45, 1.28, -0.2, 0.72]
];
leaves.forEach(([lx, ly, lz, size], index) => {
box(
size,
size * 0.85,
size,
index % 2 ? mats.foliage : mats.foliageLight,
lx,
ly,
lz,
group
);
});
}
makeTree(-5.65, -3.8, 0.95);
makeTree(5.65, -3.75, 0.92);
// Square reflecting pool.
box(2.65, 0.25, 2.0, mats.sandstone, -4.4, 0.2, 3.35);
box(2.25, 0.13, 1.6, mats.water, -4.4, 0.39, 3.35);
box(2.9, 0.18, 0.22, mats.plasterLight, -4.4, 0.48, 4.28);
box(2.9, 0.18, 0.22, mats.plasterLight, -4.4, 0.48, 2.42);
box(0.22, 0.18, 2.1, mats.plasterLight, -5.78, 0.48, 3.35);
box(0.22, 0.18, 2.1, mats.plasterLight, -3.02, 0.48, 3.35);
for (let i = 0; i < 6; i++) {
box(
0.18,
0.025,
0.46,
mats.cyan,
-5.1 + (i % 3) * 0.47,
0.48,
3.05 + Math.floor(i / 3) * 0.56,
scene,
false
);
}
function makeLantern(x, z) {
box(0.5, 0.18, 0.5, mats.darkStone, x, 0.2, z);
box(0.12, 1.85, 0.12, mats.darkStone, x, 1.12, z);
box(0.44, 0.12, 0.44, mats.terracottaDark, x, 2.08, z);
box(0.34, 0.45, 0.34, mats.lamp, x, 2.36, z);
box(0.48, 0.12, 0.48, mats.terracotta, x, 2.65, z);
box(0.14, 0.25, 0.14, mats.darkStone, x, 2.84, z);
const light = new THREE.PointLight(0xffa348, 8, 5, 2);
light.position.set(x, 2.38, z);
scene.add(light);
}
makeLantern(-6.35, 1.6);
makeLantern(6.35, 1.6);
// Small voxel stools and crates.
box(0.7, 0.18, 0.7, mats.trunk, 3.9, 0.5, 4.8);
box(0.18, 0.45, 0.18, mats.trunk, 3.68, 0.25, 4.58);
box(0.18, 0.45, 0.18, mats.trunk, 4.12, 0.25, 4.58);
box(0.18, 0.45, 0.18, mats.trunk, 3.9, 0.25, 5.02);
box(0.75, 0.75, 0.75, mats.trunk, 5.75, 0.48, 4.25);
box(0.83, 0.12, 0.83, mats.terracottaDark, 5.75, 0.87, 4.25);
// ------------------------------------------------------------------
// Telescope
// ------------------------------------------------------------------
const scopeOrigin = new THREE.Vector3(4.2, 2.08, 2.55);
const scopeDirection = displayPoint.clone().sub(scopeOrigin).normalize();
const scopeTube = new THREE.Group();
scopeTube.position.copy(scopeOrigin);
scopeTube.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
scopeDirection
);
scopeTube.userData.action = "scope";
scene.add(scopeTube);
box(0.46, 1.85, 0.46, mats.ivory, 0, 0, 0, scopeTube);
box(0.54, 0.16, 0.54, mats.gold, 0, 0.42, 0, scopeTube);
box(0.54, 0.16, 0.54, mats.gold, 0, -0.38, 0, scopeTube);
box(0.58, 0.2, 0.58, mats.darkStone, 0, 1.0, 0, scopeTube);
box(0.43, 0.055, 0.43, mats.cyan, 0, 1.12, 0, scopeTube);
box(0.31, 0.32, 0.31, mats.darkStone, 0, -1.05, 0, scopeTube);
box(0.18, 0.18, 0.18, mats.goldBright, 0, -1.27, 0, scopeTube);
box(0.16, 0.32, 0.16, mats.darkStone, 0.36, 0.12, 0, scopeTube);
box(0.28, 0.12, 0.12, mats.gold, 0.5, 0.12, 0, scopeTube);
const tripodTop = new THREE.Vector3(4.2, 1.5, 2.55);
beamBetween(tripodTop, new THREE.Vector3(3.35, 0.18, 3.18), 0.13, mats.darkStone);
beamBetween(tripodTop, new THREE.Vector3(5.0, 0.18, 2.0), 0.13, mats.darkStone);
beamBetween(tripodTop, new THREE.Vector3(4.25, 0.18, 3.75), 0.13, mats.darkStone);
// ------------------------------------------------------------------
// Phase state and UI
// ------------------------------------------------------------------
const TWO_PI = Math.PI * 2;
let theta = (135 * Math.PI) / 180;
function wrappedAngle(a) {
return ((a % TWO_PI) + TWO_PI) % TWO_PI;
}
function angleDistance(a, b) {
return Math.abs(Math.atan2(Math.sin(a - b), Math.cos(a - b)));
}
function phaseDescription(degrees) {
const d = ((degrees % 360) + 360) % 360;
const fraction = (1 - Math.cos((d * Math.PI) / 180)) / 2;
if (d < 1 || d > 359) {
return ["New Moon", "The illuminated face is hidden from the observer"];
}
if (Math.abs(d - 90) < 1) {
return ["First Quarter", "The right half of the visible disk is illuminated"];
}
if (Math.abs(d - 180) < 1) {
return ["Full Moon", "The illuminated face points toward the courtyard"];
}
if (Math.abs(d - 270) < 1) {
return ["Last Quarter", "The left half of the visible disk is illuminated"];
}
if (d < 90) {
return ["Waxing Crescent", "A thin eastern crescent is visible"];
}
if (d < 180) {
return ["Waxing Gibbous", "More than half of the visible disk is illuminated"];
}
if (d < 270) {
return ["Waning Gibbous", "More than half of the visible disk remains lit"];
}
return ["Waning Crescent", "A thin western crescent remains visible"];
}
function updatePhase() {
const degrees = (theta * 180) / Math.PI;
const fraction = (1 - Math.cos(theta)) / 2;
const [name, direction] = phaseDescription(degrees);
const percent = Math.round(fraction * 100);
moonShader.uniforms.uTheta.value = theta;
phaseName.textContent = name;
phaseDirection.textContent = direction;
illuminationValue.textContent = `${percent}%`;
illuminationFill.style.width = `${fraction * 100}%`;
positionValue.textContent = `${Math.round(degrees).toString().padStart(3, "0")}°`;
const shownDegrees = Math.round(degrees) % 360;
phaseRange.value = shownDegrees;
phaseRange.style.background = `linear-gradient(
90deg,
#f4bd62 0%,
#f4bd62 ${(shownDegrees / 360) * 100}%,
#3b4248 ${(shownDegrees / 360) * 100}%,
#3b4248 100%
)`;
// Move the selected moon around the schematic orbit.
moonMarker.position.set(
Math.cos(theta) * orbitRadius,
0.55,
orbitCenterZ + Math.sin(theta) * orbitRadius
);
// Light the small marker according to the same phase geometry.
markerVoxels.forEach((voxel) => {
const x = voxel.userData.phaseX;
const y = voxel.userData.phaseY;
const edge = Math.sqrt(Math.max(0, 1 - y * y));
let illuminated;
if (theta <= Math.PI) {
illuminated = x > Math.cos(theta) * edge;
} else {
illuminated = x < -Math.cos(theta) * edge;
}
voxel.material.color.setHex(illuminated ? 0xf1d39a : 0x25303d);
voxel.material.emissive.setHex(illuminated ? 0x5b3510 : 0x000000);
voxel.material.emissiveIntensity = illuminated ? 0.22 : 0;
});
// Highlight nearby orbit blocks.
orbitBeads.forEach((bead) => {
const close = angleDistance(bead.userData.selectTheta, theta) < 0.09;
bead.material.color.setHex(close ? 0xffd47b : 0x8d6938);
bead.material.emissive.setHex(close ? 0x704017 : 0x000000);
bead.material.emissiveIntensity = close ? 0.45 : 0;
});
}
function setTheta(value) {
theta = wrappedAngle(value);
updatePhase();
}
phaseRange.addEventListener("input", (event) => {
setTheta((Number(event.target.value) * Math.PI) / 180);
});
document.querySelectorAll("[data-deg]").forEach((button) => {
button.addEventListener("click", () => {
setTheta((Number(button.dataset.deg) * Math.PI) / 180);
});
});
updatePhase();
// ------------------------------------------------------------------
// Direct camera controls
// ------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.45, 0.25);
const desiredTarget = cameraTarget.clone();
let yaw = 0.34;
let pitch = 0.48;
let distance = 19.5;
let desiredYaw = yaw;
let desiredPitch = pitch;
let desiredDistance = distance;
let desiredFov = 38;
let scopeMode = false;
function focusDisplay() {
desiredTarget.copy(displayPoint);
desiredYaw = 0;
desiredPitch = 0.025;
desiredDistance = 5.35;
desiredFov = 38;
scopeMode = false;
scopeMask.classList.remove("active");
scopeTube.visible = true;
}
function focusScope() {
const eye = scopeOrigin.clone().addScaledVector(scopeDirection, -1.72);
const offset = eye.clone().sub(displayPoint);
desiredTarget.copy(displayPoint);
desiredDistance = offset.length();
desiredYaw = Math.atan2(offset.x, offset.z);
desiredPitch = Math.asin(offset.y / desiredDistance);
desiredFov = 18;
scopeMode = true;
scopeMask.classList.add("active");
scopeTube.visible = false;
}
function resetView() {
desiredTarget.set(0, 1.45, 0.25);
desiredYaw = 0.34;
desiredPitch = 0.48;
desiredDistance = 19.5;
desiredFov = 38;
scopeMode = false;
scopeMask.classList.remove("active");
scopeTube.visible = true;
}
document.querySelector("#inspectButton").addEventListener("click", focusDisplay);
document.querySelector("#scopeButton").addEventListener("click", focusScope);
document.querySelector("#resetButton").addEventListener("click", resetView);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.65);
const dragPoint = new THREE.Vector3();
let pointerDown = false;
let draggingMoon = false;
let pointerStartX = 0;
let pointerStartY = 0;
let previousX = 0;
let previousY = 0;
let pointerTravel = 0;
const clickRoots = [
...orbitBeads,
moonMarker,
phaseDisplay,
scopeTube
];
function setPointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function findAction(object) {
let current = object;
while (current) {
if (current.userData.action) return { action: current.userData.action };
if (current.userData.selectTheta !== undefined) {
return { action: "select", theta: current.userData.selectTheta };
}
current = current.parent;
}
return null;
}
function hitTest(event) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(clickRoots, true);
return hits.length ? findAction(hits[0].object) : null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerStartX = event.clientX;
pointerStartY = event.clientY;
previousX = event.clientX;
previousY = event.clientY;
pointerTravel = 0;
const hit = hitTest(event);
if (hit?.action === "dragMoon") {
draggingMoon = true;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
return;
}
pointerDown = true;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (draggingMoon) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
const angle = Math.atan2(
dragPoint.z - orbitCenterZ,
dragPoint.x
);
setTheta(angle);
}
return;
}
if (pointerDown) {
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
desiredYaw -= dx * 0.006;
desiredPitch = THREE.MathUtils.clamp(
desiredPitch + dy * 0.005,
0.08,
1.18
);
pointerTravel += Math.hypot(dx, dy);
previousX = event.clientX;
previousY = event.clientY;
return;
}
const hit = hitTest(event);
renderer.domElement.style.cursor = hit ? "pointer" : "grab";
});
function finishPointer(event) {
if (draggingMoon) {
draggingMoon = false;
renderer.domElement.style.cursor = "grab";
return;
}
if (!pointerDown) return;
pointerDown = false;
const travel = Math.hypot(
event.clientX - pointerStartX,
event.clientY - pointerStartY
);
if (travel < 6) {
const hit = hitTest(event);
if (hit?.action === "select") {
setTheta(hit.theta);
} else if (hit?.action === "display") {
focusDisplay();
} else if (hit?.action === "scope") {
focusScope();
}
}
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerup", finishPointer);
renderer.domElement.addEventListener("pointercancel", finishPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredDistance = THREE.MathUtils.clamp(
desiredDistance + event.deltaY * 0.012,
4.8,
28
);
},
{ passive: false }
);
// Keyboard shortcuts.
addEventListener("keydown", (event) => {
if (event.key === "Escape") resetView();
if (event.key === "ArrowRight") {
setTheta(theta + (event.shiftKey ? 0.25 : 0.035));
}
if (event.key === "ArrowLeft") {
setTheta(theta - (event.shiftKey ? 0.25 : 0.035));
}
});
// ------------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 7);
cameraTarget.lerp(desiredTarget, smoothing);
const yawDelta = Math.atan2(
Math.sin(desiredYaw - yaw),
Math.cos(desiredYaw - yaw)
);
yaw += yawDelta * smoothing;
pitch += (desiredPitch - pitch) * smoothing;
distance += (desiredDistance - distance) * smoothing;
camera.position.set(
cameraTarget.x + Math.sin(yaw) * Math.cos(pitch) * distance,
cameraTarget.y + Math.sin(pitch) * distance,
cameraTarget.z + Math.cos(yaw) * Math.cos(pitch) * distance
);
camera.lookAt(cameraTarget);
const nextFov = camera.fov + (desiredFov - camera.fov) * smoothing;
if (Math.abs(nextFov - camera.fov) > 0.001) {
camera.fov = nextFov;
camera.updateProjectionMatrix();
}
}
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
updateCamera(dt);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```