Prompt specification
Build or customize the Three.js 3D scene "Schematic Tidal Deformation Display" 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: Schematic Tidal Deformation Display
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/schematic-tidal-deformation-display-x3838
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Animate an exaggerated idealized tidal shape with clear differential-force arrows. Label the deformation schematic and avoid treating real ocean tides as two simple moving water piles.
Working Three.js Starter (schematic-tidal-deformation-display-x3838):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Connected Ocean — Tidal Deformation Schematic</title>
<style>
:root {
color-scheme: dark;
--bg: #050a13;
--panel: rgba(10, 18, 31, 0.78);
--panel-border: rgba(164, 214, 255, 0.18);
--text: #eaf5ff;
--muted: #9eb2c7;
--cyan: #46d8f2;
--cyan-soft: #169ec8;
--orange: #ffad43;
--white: #dceaff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#stage,
#stage canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
}
#stage canvas {
display: block;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.glass {
background:
linear-gradient(
135deg,
rgba(18, 33, 52, 0.86),
rgba(7, 14, 25, 0.76)
);
border: 1px solid var(--panel-border);
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.3),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}
#title-card {
position: absolute;
z-index: 10;
top: 24px;
left: 24px;
width: min(430px, calc(100vw - 48px));
padding: 20px 22px 19px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 14px rgba(70, 216, 242, 0.75);
content: "";
}
h1 {
margin: 0;
font-size: clamp(26px, 3vw, 39px);
line-height: 1.02;
letter-spacing: -0.045em;
}
#title-card p {
margin: 12px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
#title-card strong {
color: var(--text);
}
#science-panel {
position: absolute;
z-index: 10;
top: 24px;
right: 24px;
width: min(326px, calc(100vw - 48px));
padding: 18px;
border-radius: 18px;
pointer-events: none;
}
#science-panel h2 {
margin: 0 0 8px;
font-size: 15px;
letter-spacing: -0.01em;
}
#science-panel p {
margin: 9px 0;
color: var(--muted);
font-size: 12.5px;
line-height: 1.52;
}
.formula {
overflow: hidden;
margin: 13px 0;
padding: 10px 11px;
border: 1px solid rgba(255, 173, 67, 0.22);
border-radius: 10px;
background: rgba(255, 173, 67, 0.055);
color: #ffd196;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
line-height: 1.5;
white-space: nowrap;
}
.not-piles {
padding: 11px 12px;
border-left: 3px solid var(--cyan);
border-radius: 0 9px 9px 0;
background: rgba(70, 216, 242, 0.07);
color: #c8efff;
font-size: 12.5px;
line-height: 1.5;
}
#legend {
position: absolute;
z-index: 10;
left: 24px;
bottom: 24px;
width: min(390px, calc(100vw - 48px));
padding: 14px 16px;
border-radius: 15px;
pointer-events: none;
}
.legend-row {
display: flex;
align-items: flex-start;
gap: 11px;
margin: 8px 0;
color: var(--muted);
font-size: 12px;
line-height: 1.4;
}
.legend-row:first-child {
margin-top: 0;
}
.legend-row:last-child {
margin-bottom: 0;
}
.swatch {
flex: 0 0 auto;
width: 20px;
height: 10px;
margin-top: 3px;
border-radius: 3px;
}
.swatch.water {
background: var(--cyan);
box-shadow: 0 0 10px rgba(70, 216, 242, 0.45);
}
.swatch.force {
background: var(--orange);
box-shadow: 0 0 10px rgba(255, 173, 67, 0.45);
}
.swatch.reference {
height: 6px;
margin-top: 5px;
background: var(--white);
opacity: 0.7;
}
.legend-row b {
color: var(--text);
font-weight: 700;
}
#controls {
position: absolute;
z-index: 11;
right: 24px;
bottom: 24px;
display: flex;
align-items: center;
gap: 12px;
pointer-events: none;
}
#reset {
padding: 10px 13px;
border: 1px solid rgba(160, 214, 255, 0.22);
border-radius: 10px;
background: rgba(12, 23, 38, 0.84);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
pointer-events: auto;
}
#reset:hover {
border-color: rgba(70, 216, 242, 0.65);
color: var(--cyan);
}
#hint {
color: #8499ad;
font-size: 11px;
letter-spacing: 0.02em;
}
#leaders {
position: absolute;
z-index: 4;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
#leaders line {
stroke-width: 1.25;
stroke-dasharray: 4 4;
opacity: 0.65;
}
#callouts {
position: absolute;
z-index: 5;
inset: 0;
pointer-events: none;
}
.callout {
position: absolute;
min-width: 126px;
max-width: 205px;
padding: 8px 10px;
border: 1px solid rgba(255, 173, 67, 0.36);
border-radius: 9px;
background: rgba(8, 15, 26, 0.86);
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.3);
transform: translate(-50%, -50%);
white-space: nowrap;
}
.callout-title,
.callout-detail {
display: block;
}
.callout-title {
color: var(--orange);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.12em;
}
.callout-detail {
margin-top: 3px;
color: #d8e4f0;
font-size: 11px;
line-height: 1.25;
}
.callout.water {
border-color: rgba(70, 216, 242, 0.42);
}
.callout.water .callout-title {
color: var(--cyan);
}
.callout.moon {
border-color: rgba(220, 234, 255, 0.3);
}
.callout.moon .callout-title {
color: var(--white);
}
@media (max-width: 900px) {
#science-panel {
top: auto;
right: 16px;
bottom: 72px;
width: 285px;
}
#science-panel .formula,
#science-panel .long-copy {
display: none;
}
#title-card {
top: 16px;
left: 16px;
}
#legend {
left: 16px;
bottom: 16px;
}
#controls {
right: 16px;
bottom: 16px;
}
}
@media (max-width: 650px) {
#title-card {
width: calc(100vw - 32px);
}
#title-card p {
font-size: 12px;
}
#science-panel,
#legend {
display: none;
}
#controls {
right: 12px;
bottom: 12px;
}
#hint {
display: none;
}
.callout {
min-width: 105px;
padding: 6px 8px;
}
.callout-detail {
font-size: 9.5px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header id="title-card" class="glass">
<div class="eyebrow">Deformation schematic · exaggerated</div>
<h1>One connected ocean, not two piles.</h1>
<p>
The cyan voxels form a <strong>single global ocean shell</strong>.
Lunar forcing produces two opposite high regions, while the shell and
its reference shape remain one continuous system.
</p>
</header>
<aside id="science-panel" class="glass">
<h2>Reading the differential-force arrows</h2>
<p>
Orange arrows show the Moon’s pull <em>relative to Earth’s center</em>,
not the absolute direction of gravity.
</p>
<div class="formula">
Δa(x) = GM[(x<sub>M</sub>−x)/|x<sub>M</sub>−x|³ − x<sub>M</sub>/R³]
</div>
<p class="long-copy">
The near side is pulled outward more strongly than the center. The far
side is pulled outward relative to the center because the Moon’s pull
there is weaker. At 90°, the differential acceleration points inward.
</p>
<div class="not-piles">
The orange vectors explain the deformation pattern. They do not depict
two water piles moving around Earth.
</div>
</aside>
<section id="legend" class="glass">
<div class="legend-row">
<span class="swatch water"></span>
<span><b>Connected ocean shell:</b> its shape is intentionally exaggerated.</span>
</div>
<div class="legend-row">
<span class="swatch force"></span>
<span><b>Differential lunar force:</b> shown relative to Earth’s center.</span>
</div>
<div class="legend-row">
<span class="swatch reference"></span>
<span><b>White pinpoints:</b> an undeformed reference radius.</span>
</div>
</section>
<div id="controls">
<span id="hint">Drag to orbit · Scroll to zoom · Double-click to reset</span>
<button id="reset" type="button">Reset view</button>
</div>
<svg id="leaders" aria-hidden="true"></svg>
<div id="callouts" aria-hidden="true"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene setup
// ------------------------------------------------------------
const stage = document.getElementById("stage");
const leaderLayer = document.getElementById("leaders");
const calloutLayer = document.getElementById("callouts");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x050a13);
scene.fog = new THREE.FogExp2(0x050a13, 0.018);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
radius: 8.8,
theta: 0.72,
phi: 1.05
};
const cameraTarget = new THREE.Vector3(0, 0, 0);
function updateCamera() {
const { radius, theta, phi } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
radius * sinPhi * Math.cos(theta),
radius * Math.cos(phi),
radius * sinPhi * Math.sin(theta)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
updateCamera();
scene.add(new THREE.HemisphereLight(0x9fd7ff, 0x101521, 1.35));
const keyLight = new THREE.DirectionalLight(0xd9efff, 3.2);
keyLight.position.set(-4, 7, 5);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x438dff, 1.9);
rimLight.position.set(5, -2, -6);
scene.add(rimLight);
const moonLight = new THREE.PointLight(0xffe0ae, 2.3, 9, 1.4);
scene.add(moonLight);
// ------------------------------------------------------------
// Small deterministic helpers
// ------------------------------------------------------------
const UP = new THREE.Vector3(0, 1, 0);
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
const tempVector = new THREE.Vector3();
function hash(x, y, z) {
const value = Math.sin(x * 12.9898 + y * 78.233 + z * 37.719) * 43758.5453;
return value - Math.floor(value);
}
function makeInstanceMesh(count, size, material) {
const geometry = new THREE.BoxGeometry(size, size, size);
const mesh = new THREE.InstancedMesh(geometry, material, count);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
return mesh;
}
function setInstance(mesh, index, position, scale = 1, quaternion = null) {
dummy.position.copy(position);
dummy.quaternion.identity();
if (quaternion) {
dummy.quaternion.copy(quaternion);
}
dummy.scale.setScalar(scale);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
}
// ------------------------------------------------------------
// Voxelized solid planet
// ------------------------------------------------------------
const corePositions = [];
const coreStep = 0.16;
for (let x = -8; x <= 8; x++) {
for (let y = -8; y <= 8; y++) {
for (let z = -8; z <= 8; z++) {
const px = x * coreStep;
const py = y * coreStep;
const pz = z * coreStep;
if (px * px + py * py + pz * pz <= 1.23 * 1.23) {
corePositions.push({
position: new THREE.Vector3(px, py, pz),
n: hash(x, y, z)
});
}
}
}
}
const coreMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.92,
metalness: 0.02
});
const coreMesh = makeInstanceMesh(
corePositions.length,
0.155,
coreMaterial
);
const corePalette = [
new THREE.Color(0x354d42),
new THREE.Color(0x526349),
new THREE.Color(0x777452),
new THREE.Color(0x394b59),
new THREE.Color(0x5b604d)
];
corePositions.forEach((item, index) => {
setInstance(
coreMesh,
index,
item.position,
0.92 + item.n * 0.1
);
const colorIndex =
Math.floor(item.n * corePalette.length) % corePalette.length;
coreMesh.setColorAt(index, corePalette[colorIndex]);
});
coreMesh.instanceMatrix.needsUpdate = true;
coreMesh.instanceColor.needsUpdate = true;
scene.add(coreMesh);
// ------------------------------------------------------------
// One connected, deformable voxel ocean shell
// ------------------------------------------------------------
const waterCount = 2800;
const waterBaseRadius = 1.56;
const waterGeometry = new THREE.BoxGeometry(0.13, 0.13, 0.13);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.5,
metalness: 0.06,
emissive: 0x06283b,
emissiveIntensity: 0.3
});
const waterMesh = new THREE.InstancedMesh(
waterGeometry,
waterMaterial,
waterCount
);
waterMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(waterMesh);
const waterNormals = [];
// Fibonacci sphere gives an even voxel distribution without polar bunching.
for (let i = 0; i < waterCount; i++) {
const y = 1 - ((i + 0.5) / waterCount) * 2;
const radial = Math.sqrt(Math.max(0, 1 - y * y));
const angle = Math.PI * (3 - Math.sqrt(5)) * i;
waterNormals.push(
new THREE.Vector3(
Math.cos(angle) * radial,
y,
Math.sin(angle) * radial
)
);
}
const waterDeep = new THREE.Color(0x126aa4);
const waterBright = new THREE.Color(0x56e7f5);
// ------------------------------------------------------------
// Undeformed reference shell, made from small white voxels
// ------------------------------------------------------------
const referenceCount = 430;
const referenceMaterial = new THREE.MeshBasicMaterial({
color: 0xdceaff,
transparent: true,
opacity: 0.42,
depthWrite: false
});
const referenceMesh = makeInstanceMesh(
referenceCount,
0.026,
referenceMaterial
);
for (let i = 0; i < referenceCount; i++) {
const y = 1 - ((i + 0.5) / referenceCount) * 2;
const radial = Math.sqrt(Math.max(0, 1 - y * y));
const angle = Math.PI * (3 - Math.sqrt(5)) * i;
const point = new THREE.Vector3(
Math.cos(angle) * radial,
y,
Math.sin(angle) * radial
).multiplyScalar(waterBaseRadius);
setInstance(referenceMesh, i, point);
}
referenceMesh.instanceMatrix.needsUpdate = true;
scene.add(referenceMesh);
// ------------------------------------------------------------
// Voxel Moon and orbital path
// ------------------------------------------------------------
const moonGroup = new THREE.Group();
scene.add(moonGroup);
const moonPositions = [];
const moonStep = 0.105;
for (let x = -4; x <= 4; x++) {
for (let y = -4; y <= 4; y++) {
for (let z = -4; z <= 4; z++) {
const px = x * moonStep;
const py = y * moonStep;
const pz = z * moonStep;
if (px * px + py * py + pz * pz <= 0.4 * 0.4) {
moonPositions.push({
position: new THREE.Vector3(px, py, pz),
n: hash(x + 20, y, z)
});
}
}
}
}
const moonMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
metalness: 0
});
const moonMesh = makeInstanceMesh(
moonPositions.length,
0.105,
moonMaterial
);
moonPositions.forEach((item, index) => {
setInstance(
moonMesh,
index,
item.position,
0.9 + item.n * 0.12
);
const shade = 0.54 + item.n * 0.3;
tempColor.setRGB(shade, shade * 0.98, shade * 0.91);
moonMesh.setColorAt(index, tempColor);
});
moonMesh.instanceMatrix.needsUpdate = true;
moonMesh.instanceColor.needsUpdate = true;
moonGroup.add(moonMesh);
const orbitRadius = 4.8;
const orbitCount = 180;
const orbitMaterial = new THREE.MeshBasicMaterial({
color: 0x7191ad,
transparent: true,
opacity: 0.26,
depthWrite: false
});
const orbitMesh = makeInstanceMesh(
orbitCount,
0.026,
orbitMaterial
);
for (let i = 0; i < orbitCount; i++) {
const angle = (i / orbitCount) * Math.PI * 2;
const point = new THREE.Vector3(
Math.cos(angle) * orbitRadius,
0,
Math.sin(angle) * orbitRadius
);
setInstance(orbitMesh, i, point);
}
orbitMesh.instanceMatrix.needsUpdate = true;
scene.add(orbitMesh);
// Dashed voxel line marking the Earth–Moon axis.
const axisCount = 48;
const axisMaterial = new THREE.MeshBasicMaterial({
color: 0xffbd67,
transparent: true,
opacity: 0.34,
depthWrite: false
});
const axisMesh = makeInstanceMesh(axisCount, 0.045, axisMaterial);
scene.add(axisMesh);
// ------------------------------------------------------------
// Voxel star field
// ------------------------------------------------------------
const starCount = 330;
const starMaterial = new THREE.MeshBasicMaterial({
color: 0xbdd9ff,
transparent: true,
opacity: 0.72
});
const starMesh = makeInstanceMesh(starCount, 0.035, starMaterial);
for (let i = 0; i < starCount; i++) {
const angle = hash(i, 1, 4) * Math.PI * 2;
const y = hash(i, 8, 2) * 2 - 1;
const radius = 12 + hash(i, 5, 9) * 15;
const radial = Math.sqrt(Math.max(0, 1 - y * y));
const point = new THREE.Vector3(
Math.cos(angle) * radial * radius,
y * radius,
Math.sin(angle) * radial * radius
);
const size = 0.45 + hash(i, 3, 7) * 1.3;
setInstance(starMesh, i, point, size);
}
starMesh.instanceMatrix.needsUpdate = true;
scene.add(starMesh);
// ------------------------------------------------------------
// Block-built force arrows
// Local +Y points along the arrow direction.
// ------------------------------------------------------------
function createVoxelArrow(color) {
const group = new THREE.Group();
const cubeSize = 0.082;
const localPositions = [];
// Stem.
for (let i = 0; i < 7; i++) {
localPositions.push(new THREE.Vector3(0, 0.04 + i * 0.075, 0));
}
// Stepped 3x3 arrowhead base.
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) {
localPositions.push(new THREE.Vector3(x * cubeSize, 0.56, z * cubeSize));
}
}
// Plus-shaped second layer.
localPositions.push(
new THREE.Vector3(0, 0.65, 0),
new THREE.Vector3(cubeSize, 0.65, 0),
new THREE.Vector3(-cubeSize, 0.65, 0),
new THREE.Vector3(0, 0.65, cubeSize),
new THREE.Vector3(0, 0.65, -cubeSize)
);
// Pixel tip.
localPositions.push(new THREE.Vector3(0, 0.75, 0));
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.48,
roughness: 0.38,
metalness: 0.04,
depthTest: false,
depthWrite: false
});
const mesh = makeInstanceMesh(
localPositions.length,
cubeSize,
material
);
localPositions.forEach((position, index) => {
setInstance(mesh, index, position);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.renderOrder = 20;
group.add(mesh);
group.userData.length = 0.82;
return group;
}
const nearArrow = createVoxelArrow(0xffad43);
const farArrow = createVoxelArrow(0xffad43);
const sideArrow = createVoxelArrow(0xffad43);
sideArrow.scale.setScalar(0.72);
scene.add(nearArrow, farArrow, sideArrow);
// ------------------------------------------------------------
// DOM callouts and leader lines
// ------------------------------------------------------------
const callouts = [];
function createCallout(title, detail, className = "") {
const element = document.createElement("div");
element.className = `callout ${className}`.trim();
element.innerHTML = `
<span class="callout-title">${title}</span>
<span class="callout-detail">${detail}</span>
`;
calloutLayer.appendChild(element);
const line = document.createElementNS(
"http://www.w3.org/2000/svg",
"line"
);
if (className === "water") {
line.style.stroke = "#46d8f2";
} else if (className === "moon") {
line.style.stroke = "#dceaff";
} else {
line.style.stroke = "#ffad43";
}
leaderLayer.appendChild(line);
const callout = {
element,
line,
anchor: new THREE.Vector3(),
offset: new THREE.Vector2(100, -35)
};
callouts.push(callout);
return callout;
}
const nearCallout = createCallout(
"Near side · ΔF outward",
"Toward the Moon",
""
);
const farCallout = createCallout(
"Far side · ΔF outward",
"Away from the Moon",
""
);
const sideCallout = createCallout(
"90° sides · ΔF inward",
"Toward Earth",
""
);
const oceanCallout = createCallout(
"One connected ocean shell",
"Continuous deformation",
"water"
);
const moonCallout = createCallout(
"Moon",
"Lunar forcing body",
"moon"
);
function projectToScreen(point) {
const projected = point.clone().project(camera);
return {
x: (projected.x * 0.5 + 0.5) * window.innerWidth,
y: (-projected.y * 0.5 + 0.5) * window.innerHeight,
z: projected.z
};
}
function placeCallout(callout, offsetX, offsetY) {
const screen = projectToScreen(callout.anchor);
if (screen.z < -1 || screen.z > 1) {
callout.element.style.display = "none";
callout.line.style.display = "none";
return screen;
}
callout.element.style.display = "block";
callout.line.style.display = "block";
const width = callout.element.offsetWidth;
const height = callout.element.offsetHeight;
const minX = width * 0.5 + 12;
const maxX = window.innerWidth - width * 0.5 - 12;
const minY = height * 0.5 + 80;
const maxY = window.innerHeight - height * 0.5 - 64;
const labelX = THREE.MathUtils.clamp(
screen.x + offsetX,
minX,
Math.max(minX, maxX)
);
const labelY = THREE.MathUtils.clamp(
screen.y + offsetY,
minY,
Math.max(minY, maxY)
);
callout.element.style.left = `${labelX}px`;
callout.element.style.top = `${labelY}px`;
callout.line.setAttribute("x1", screen.x.toFixed(1));
callout.line.setAttribute("y1", screen.y.toFixed(1));
callout.line.setAttribute("x2", labelX.toFixed(1));
callout.line.setAttribute("y2", labelY.toFixed(1));
}
// ------------------------------------------------------------
// Direct camera controls — no examples/addons are imported.
// ------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function resetView() {
cameraState.radius = 8.8;
cameraState.theta = 0.72;
cameraState.phi = 1.05;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.0055;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.0045,
0.35,
Math.PI - 0.35
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"pointerleave",
event => {
if (!dragging) return;
stopDragging(event);
}
);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
5.3,
15
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
document.getElementById("reset").addEventListener("click", resetView);
window.addEventListener("keydown", event => {
if (event.key === "r" || event.key === "R") {
resetView();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
leaderLayer.setAttribute(
"viewBox",
`0 0 ${window.innerWidth} ${window.innerHeight}`
);
});
leaderLayer.setAttribute(
"viewBox",
`0 0 ${window.innerWidth} ${window.innerHeight}`
);
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
const moonPosition = new THREE.Vector3();
const tidalAxis = new THREE.Vector3(1, 0, 0);
const sideA = new THREE.Vector3();
const sideB = new THREE.Vector3();
const sideNormal = new THREE.Vector3();
const cameraDirection = new THREE.Vector3();
const nearOrigin = new THREE.Vector3();
const farOrigin = new THREE.Vector3();
const sideOrigin = new THREE.Vector3();
const nearTip = new THREE.Vector3();
const farTip = new THREE.Vector3();
const sideTip = new THREE.Vector3();
const oceanAnchor = new THREE.Vector3();
const moonAnchor = new THREE.Vector3();
function radiusInDirection(normal, amplitude) {
const c = normal.dot(tidalAxis);
const p2 = 0.5 * (3 * c * c - 1);
return waterBaseRadius + amplitude * p2;
}
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
// The Moon orbits slowly; the connected ocean's long axis follows it.
const moonAngle = time * 0.12;
moonPosition.set(
Math.cos(moonAngle) * orbitRadius,
0,
Math.sin(moonAngle) * orbitRadius
);
moonGroup.position.copy(moonPosition);
moonLight.position.copy(moonPosition).multiplyScalar(0.7);
tidalAxis.copy(moonPosition).normalize();
// A small breathing motion makes the exaggerated deformation legible.
// This is a schematic, not a physical tide simulation.
const amplitude =
0.315 + 0.045 * (0.5 + 0.5 * Math.sin(time * 0.72));
// Update all connected ocean voxels.
for (let i = 0; i < waterCount; i++) {
const normal = waterNormals[i];
const c = normal.dot(tidalAxis);
const p2 = 0.5 * (3 * c * c - 1);
const radius = waterBaseRadius + amplitude * p2;
tempVector.copy(normal).multiplyScalar(radius);
setInstance(waterMesh, i, tempVector);
// Bulges are brighter, making the two opposite lobes obvious.
const normalizedStrain = THREE.MathUtils.clamp(
(p2 + 0.5) / 1.5,
0,
1
);
tempColor.lerpColors(
waterDeep,
waterBright,
0.2 + normalizedStrain * 0.8
);
waterMesh.setColorAt(i, tempColor);
}
waterMesh.instanceMatrix.needsUpdate = true;
waterMesh.instanceColor.needsUpdate = true;
// Update the dashed Earth–Moon axis.
for (let i = 0; i < axisCount; i++) {
const fraction = i / (axisCount - 1);
const radius = 2.0 + fraction * (orbitRadius - 2.15);
tempVector.copy(tidalAxis).multiplyScalar(radius);
setInstance(axisMesh, i, tempVector, i % 2 === 0 ? 1 : 0);
}
axisMesh.instanceMatrix.needsUpdate = true;
// Near-side differential force: outward toward the Moon.
const nearRadius = radiusInDirection(tidalAxis, amplitude);
nearOrigin.copy(tidalAxis).multiplyScalar(nearRadius + 0.03);
nearArrow.position.copy(nearOrigin);
nearArrow.quaternion.setFromUnitVectors(UP, tidalAxis);
nearTip.copy(tidalAxis).multiplyScalar(nearArrow.userData.length).add(nearOrigin);
// Far-side differential force: outward away from the Moon.
const farRadius = radiusInDirection(
tempVector.copy(tidalAxis).negate(),
amplitude
);
farOrigin.copy(tidalAxis).multiplyScalar(-farRadius - 0.03);
farArrow.position.copy(farOrigin);
farArrow.quaternion.setFromUnitVectors(
UP,
tempVector.copy(tidalAxis).negate()
);
farTip.copy(tempVector).multiplyScalar(farArrow.userData.length).add(farOrigin);
// Choose the quadrature point most visible to the current camera.
cameraDirection.copy(camera.position).normalize();
sideA.crossVectors(tidalAxis, UP).normalize();
sideB.crossVectors(tidalAxis, sideA).normalize();
const candidates = [
sideA,
tempVector.copy(sideA).negate(),
sideB,
tempVector.copy(sideB).negate()
];
sideNormal.set(0, 0, 0);
let bestFacing = -Infinity;
for (const candidate of candidates) {
const facing = candidate.dot(cameraDirection);
if (facing > bestFacing) {
bestFacing = facing;
sideNormal.copy(candidate);
}
}
const sideRadius = radiusInDirection(sideNormal, amplitude);
sideOrigin.copy(sideNormal).multiplyScalar(sideRadius + 0.025);
sideArrow.position.copy(sideOrigin);
sideArrow.quaternion.setFromUnitVectors(
UP,
tempVector.copy(sideNormal).negate()
);
sideTip.copy(
tempVector.copy(sideNormal).multiplyScalar(
-sideArrow.userData.length * sideArrow.scale.y
)
).add(sideOrigin);
// Ocean label follows a visible point on the same connected shell.
const visibleDirection = camera.position.clone().normalize();
oceanAnchor
.copy(visibleDirection)
.multiplyScalar(
radiusInDirection(visibleDirection, amplitude) + 0.1
);
moonAnchor.copy(moonPosition).add(new THREE.Vector3(0, 0.58, 0));
// Update callout anchors and dynamic label placement.
nearCallout.anchor.copy(nearTip);
farCallout.anchor.copy(farTip);
sideCallout.anchor.copy(sideTip);
oceanCallout.anchor.copy(oceanAnchor);
moonCallout.anchor.copy(moonAnchor);
const nearScreen = projectToScreen(nearTip);
const farScreen = projectToScreen(farTip);
const sideScreen = projectToScreen(sideTip);
const moonScreen = projectToScreen(moonAnchor);
const oceanScreen = projectToScreen(oceanAnchor);
const centerX = window.innerWidth * 0.5;
placeCallout(
nearCallout,
nearScreen.x >= centerX ? 105 : -105,
-44
);
placeCallout(
farCallout,
farScreen.x >= centerX ? -105 : 105,
-44
);
placeCallout(
sideCallout,
sideScreen.x >= centerX ? 112 : -112,
34
);
placeCallout(
oceanCallout,
oceanScreen.x >= centerX ? -125 : 125,
-28
);
placeCallout(
moonCallout,
moonScreen.x >= centerX ? 55 : -55,
-44
);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```