Prompt specification
Build or customize the Three.js 3D scene "Alternating-ion lattice section" 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: Alternating-ion lattice section
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/alternating-ion-lattice-section-x1953
Scene Description Prompt:
Create a standalone cutaway scene in one HTML file using Three.js from cdnjs. Build a finite cubic ionic-lattice fragment with alternating charge labels and a removable layer; clarify that the cluster boundary is artificial and colors are symbolic.
Working Three.js Starter (alternating-ion-lattice-section-x1953):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ionic Lattice — Cutaway Study</title>
<style>
:root {
color-scheme: dark;
--bg: #070b12;
--panel: rgba(12, 20, 33, 0.76);
--panel-strong: rgba(15, 25, 41, 0.92);
--border: rgba(180, 205, 235, 0.16);
--text: #eaf2ff;
--muted: #91a4bb;
--positive: #ff6577;
--negative: #43d7f5;
--cut: #f3bd69;
--removable: #b69aff;
--radius: 16px;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 57% 35%, rgba(31, 54, 82, 0.9), transparent 43%),
radial-gradient(circle at 18% 92%, rgba(33, 59, 77, 0.44), transparent 35%),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#stage canvas {
display: block;
cursor: grab;
outline: none;
touch-action: none;
}
#stage canvas:focus-visible {
outline: 2px solid rgba(182, 154, 255, 0.8);
outline-offset: -4px;
}
.overlay {
position: fixed;
z-index: 10;
pointer-events: none;
}
.panel {
pointer-events: auto;
border: 1px solid var(--border);
border-radius: var(--radius);
background: var(--panel);
box-shadow:
0 20px 55px rgba(0, 0, 0, 0.32),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px) saturate(120%);
-webkit-backdrop-filter: blur(18px) saturate(120%);
}
.intro {
top: 24px;
left: 24px;
width: min(440px, calc(100vw - 48px));
padding: 20px 22px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: #aebfd2;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 1px;
background: var(--removable);
content: "";
box-shadow: 0 0 10px rgba(182, 154, 255, 0.7);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 38px);
font-weight: 650;
letter-spacing: -0.045em;
line-height: 1.02;
}
.intro p {
max-width: 390px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.65;
}
.meta {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 16px;
}
.chip {
padding: 5px 8px;
border: 1px solid rgba(180, 205, 235, 0.12);
border-radius: 999px;
color: #b6c6d8;
background: rgba(255, 255, 255, 0.035);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend {
top: 24px;
right: 24px;
width: 270px;
padding: 17px 18px;
}
.panel-title {
margin: 0 0 12px;
color: #dce8f7;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.charge-list {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.charge {
display: flex;
align-items: center;
gap: 9px;
color: #c6d3e2;
font-size: 11px;
}
.charge-symbol {
display: grid;
width: 29px;
height: 29px;
place-items: center;
border-radius: 50%;
font-size: 17px;
font-weight: 850;
line-height: 1;
}
.charge.positive .charge-symbol {
color: #fff;
background: var(--positive);
box-shadow: 0 0 22px rgba(255, 101, 119, 0.24);
}
.charge.negative .charge-symbol {
color: #06131b;
background: var(--negative);
box-shadow: 0 0 22px rgba(67, 215, 245, 0.2);
}
.symbolic-note {
margin: 13px 0 0;
padding-top: 12px;
border-top: 1px solid rgba(180, 205, 235, 0.11);
color: var(--muted);
font-size: 10.5px;
line-height: 1.55;
}
.symbolic-note strong {
color: #d7e4f4;
font-weight: 700;
}
.interpretation {
right: 24px;
bottom: 25px;
width: 310px;
padding: 17px 18px;
}
.guide-row {
display: grid;
grid-template-columns: 35px 1fr;
gap: 10px;
align-items: center;
margin-top: 10px;
color: #aebed0;
font-size: 10.5px;
line-height: 1.35;
}
.guide-mark {
height: 1px;
background: #d3dfed;
}
.guide-mark.dashed {
height: 0;
border-top: 1px dashed #d3dfed;
background: none;
}
.guide-mark.cut {
background: var(--cut);
box-shadow: 0 0 8px rgba(243, 189, 105, 0.45);
}
.guide-mark.layer {
background: var(--removable);
box-shadow: 0 0 8px rgba(182, 154, 255, 0.45);
}
.model-note {
margin: 14px 0 0;
padding-top: 13px;
border-top: 1px solid rgba(180, 205, 235, 0.11);
color: var(--muted);
font-size: 10.5px;
line-height: 1.55;
}
.model-note strong {
color: #dbe7f5;
}
.controls {
left: 24px;
bottom: 25px;
width: 390px;
max-width: calc(100vw - 48px);
padding: 13px;
}
.control-main {
display: flex;
gap: 8px;
}
button {
border: 1px solid rgba(180, 205, 235, 0.16);
border-radius: 11px;
color: var(--text);
background: rgba(255, 255, 255, 0.055);
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(182, 154, 255, 0.48);
background: rgba(255, 255, 255, 0.09);
}
button:active {
transform: translateY(1px);
}
button:focus-visible,
input:focus-visible {
outline: 2px solid rgba(182, 154, 255, 0.85);
outline-offset: 2px;
}
.primary {
display: flex;
flex: 1;
align-items: center;
justify-content: center;
gap: 9px;
min-height: 42px;
padding: 0 14px;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.025em;
}
.primary .dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--removable);
box-shadow: 0 0 12px rgba(182, 154, 255, 0.85);
}
.primary.is-removed {
border-color: rgba(182, 154, 255, 0.52);
background: rgba(182, 154, 255, 0.12);
}
.primary.is-removed .dot {
background: transparent;
border: 1px solid var(--removable);
box-shadow: none;
}
.icon-button {
width: 42px;
min-height: 42px;
font-size: 20px;
}
.toggles {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 10px;
}
.toggle {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 8px;
border-radius: 8px;
color: #aebed0;
background: rgba(255, 255, 255, 0.028);
font-size: 10px;
cursor: pointer;
user-select: none;
}
.toggle:hover {
color: #e3edf9;
background: rgba(255, 255, 255, 0.06);
}
.toggle input {
width: 12px;
height: 12px;
margin: 0;
accent-color: var(--removable);
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid rgba(180, 205, 235, 0.09);
color: #8296ad;
font-size: 9.5px;
letter-spacing: 0.025em;
}
#layerState {
color: #c6b7f5;
font-weight: 700;
}
.interaction-hint {
left: 50%;
bottom: 28px;
transform: translateX(-50%);
color: rgba(194, 209, 226, 0.58);
font-size: 9.5px;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 980px) {
.interaction-hint {
display: none;
}
.interpretation {
width: 285px;
}
}
@media (max-width: 760px) {
.intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px 16px;
}
.intro p {
margin-top: 9px;
font-size: 11px;
}
.meta {
margin-top: 10px;
}
.legend {
top: auto;
right: 12px;
bottom: 151px;
width: 230px;
padding: 12px 13px;
}
.symbolic-note {
margin-top: 8px;
padding-top: 8px;
}
.interpretation {
display: none;
}
.controls {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
max-width: none;
}
.status-row {
font-size: 9px;
}
}
@media (max-height: 650px) {
.intro p,
.meta,
.interpretation {
display: none;
}
.legend {
padding: 12px 14px;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive three-dimensional ionic lattice cutaway"></main>
<header class="intro overlay panel">
<div class="eyebrow">Structure study 01</div>
<h1>Ionic lattice<br>cutaway</h1>
<p>
A finite <strong>7 × 7 × 7</strong> checkerboard fragment with one corner
octant removed. The central <strong>x = 0</strong> layer can be removed
independently.
</p>
<div class="meta">
<span class="chip">343 finite sites</span>
<span class="chip">Schematic geometry</span>
<span class="chip">Interactive layer</span>
</div>
</header>
<aside class="legend overlay panel">
<h2 class="panel-title">Charge key</h2>
<div class="charge-list">
<div class="charge positive">
<span class="charge-symbol">+</span>
<span>Positive site</span>
</div>
<div class="charge negative">
<span class="charge-symbol">−</span>
<span>Negative site</span>
</div>
</div>
<p class="symbolic-note">
<strong>Colors are symbolic only.</strong> They distinguish charge labels;
they do not identify elements, oxidation states, or a physical color scale.
</p>
</aside>
<aside class="interpretation overlay panel">
<h2 class="panel-title">Reading the model</h2>
<div class="guide-row">
<span class="guide-mark dashed"></span>
<span>Artificial boundary of the finite fragment</span>
</div>
<div class="guide-row">
<span class="guide-mark cut"></span>
<span>Exposed cutaway face</span>
</div>
<div class="guide-row">
<span class="guide-mark layer"></span>
<span>Removable central x = 0 layer</span>
</div>
<p class="model-note">
<strong>The dashed cube is an artificial crop, not a physical surface.</strong>
This alternating simple-cubic checkerboard is a schematic lattice model,
not a specific mineral structure or atomistic scale model.
</p>
</aside>
<section class="controls overlay panel" aria-label="Scene controls">
<div class="control-main">
<button id="layerButton" class="primary" type="button" aria-pressed="false">
<span class="dot" aria-hidden="true"></span>
<span id="layerAction">Remove x = 0 layer</span>
</button>
<button id="resetButton" class="icon-button" type="button"
title="Reset camera" aria-label="Reset camera">↺</button>
</div>
<div class="toggles">
<label class="toggle">
<input id="cutawayToggle" type="checkbox" checked>
Cutaway
</label>
<label class="toggle">
<input id="labelsToggle" type="checkbox" checked>
Labels
</label>
<label class="toggle">
<input id="rotateToggle" type="checkbox">
Auto rotate
</label>
</div>
<div class="status-row">
<span><strong id="siteCount">316</strong> visible sites</span>
<span id="layerState">Layer x = 0 intact</span>
</div>
</section>
<div class="interaction-hint overlay">
Drag orbit · wheel zoom · shift-drag pan · L remove layer · R reset
</div>
<div id="announcer" class="sr-only" aria-live="polite"></div>
<noscript>This scene requires JavaScript and WebGL.</noscript>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const layerButton = document.querySelector("#layerButton");
const layerAction = document.querySelector("#layerAction");
const layerState = document.querySelector("#layerState");
const siteCount = document.querySelector("#siteCount");
const resetButton = document.querySelector("#resetButton");
const cutawayToggle = document.querySelector("#cutawayToggle");
const labelsToggle = document.querySelector("#labelsToggle");
const rotateToggle = document.querySelector("#rotateToggle");
const announcer = document.querySelector("#announcer");
const N = 7;
const SPACING = 1.05;
const HALF = SPACING * (N - 1) * 0.5;
const REMOVABLE_IX = 3; // The central x = 0 layer.
const CUT_BOUNDARY = SPACING * 0.5;
let cutawayEnabled = true;
let layerRemoved = false;
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D ionic lattice. Drag to orbit, use the mouse wheel to zoom, " +
"and shift-drag to pan."
);
stage.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xc7e4ff, 0x10131c, 1.8);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5.5, 8.5, 6.5);
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x72cfff, 1.35);
fillLight.position.set(-6, 1.5, -4);
scene.add(fillLight);
const warmLight = new THREE.PointLight(0xff667c, 22, 15, 2);
warmLight.position.set(4, -1, 3);
scene.add(warmLight);
const coolLight = new THREE.PointLight(0x43d7f5, 18, 14, 2);
coolLight.position.set(-4, 2, -3);
scene.add(coolLight);
const grid = new THREE.GridHelper(
20,
40,
0x3b5774,
0x1b3045
);
grid.position.y = -HALF - 1.05;
grid.material.transparent = true;
grid.material.opacity = 0.2;
grid.material.depthWrite = false;
scene.add(grid);
// -------------------------------------------------------------------------
// Materials and textures
// -------------------------------------------------------------------------
const positiveMaterial = new THREE.MeshStandardMaterial({
color: 0xff6577,
emissive: 0x4b0c18,
emissiveIntensity: 0.36,
roughness: 0.32,
metalness: 0.12
});
const negativeMaterial = new THREE.MeshStandardMaterial({
color: 0x43d7f5,
emissive: 0x07364a,
emissiveIntensity: 0.34,
roughness: 0.3,
metalness: 0.15
});
function makeChargeTexture(symbol) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 128, 128);
context.font = "800 88px Arial, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.lineJoin = "round";
context.lineWidth = 10;
context.strokeStyle = "rgba(4, 10, 18, 0.92)";
context.strokeText(symbol, 64, 65);
context.fillStyle = "#ffffff";
context.fillText(symbol, 64, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
return texture;
}
const positiveLabelMaterial = new THREE.SpriteMaterial({
map: makeChargeTexture("+"),
transparent: true,
depthWrite: false,
depthTest: true,
toneMapped: false
});
const negativeLabelMaterial = new THREE.SpriteMaterial({
map: makeChargeTexture("−"),
transparent: true,
depthWrite: false,
depthTest: true,
toneMapped: false
});
function roundedRect(context, x, y, width, height, radius) {
context.beginPath();
context.moveTo(x + radius, y);
context.lineTo(x + width - radius, y);
context.quadraticCurveTo(x + width, y, x + width, y + radius);
context.lineTo(x + width, y + height - radius);
context.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
context.lineTo(x + radius, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - radius);
context.lineTo(x, y + radius);
context.quadraticCurveTo(x, y, x + radius, y);
context.closePath();
}
function makeAnnotation(text, accent = "#b69aff", width = 3.2) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 112;
const context = canvas.getContext("2d");
roundedRect(context, 4, 4, 632, 104, 22);
context.fillStyle = "rgba(7, 13, 23, 0.88)";
context.fill();
context.lineWidth = 2;
context.strokeStyle = accent;
context.stroke();
context.font = "700 28px Arial, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = "#eef6ff";
context.fillText(text, 320, 57);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * 0.175, 1);
sprite.renderOrder = 20;
return sprite;
}
// -------------------------------------------------------------------------
// Finite checkerboard lattice
// -------------------------------------------------------------------------
const sites = [];
const positiveSites = [];
const negativeSites = [];
for (let ix = 0; ix < N; ix++) {
for (let iy = 0; iy < N; iy++) {
for (let iz = 0; iz < N; iz++) {
const charge = (ix + iy + iz) % 2 === 0 ? 1 : -1;
const site = {
ix,
iy,
iz,
charge,
position: new THREE.Vector3(
(ix - (N - 1) / 2) * SPACING,
(iy - (N - 1) / 2) * SPACING,
(iz - (N - 1) / 2) * SPACING
),
baseVisible: true,
visible: true,
mesh: null,
instanceIndex: -1,
sprite: null
};
sites.push(site);
(charge > 0 ? positiveSites : negativeSites).push(site);
}
}
}
const sphereGeometry = new THREE.SphereGeometry(0.235, 20, 14);
const positiveMesh = new THREE.InstancedMesh(
sphereGeometry,
positiveMaterial,
positiveSites.length
);
const negativeMesh = new THREE.InstancedMesh(
sphereGeometry,
negativeMaterial,
negativeSites.length
);
positiveMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
negativeMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
positiveMesh.frustumCulled = false;
negativeMesh.frustumCulled = false;
scene.add(positiveMesh, negativeMesh);
const labelsRoot = new THREE.Group();
scene.add(labelsRoot);
for (const site of sites) {
const mesh = site.charge > 0 ? positiveMesh : negativeMesh;
const collection = site.charge > 0 ? positiveSites : negativeSites;
site.mesh = mesh;
site.instanceIndex = collection.indexOf(site);
const sprite = new THREE.Sprite(
site.charge > 0 ? positiveLabelMaterial : negativeLabelMaterial
);
sprite.scale.set(0.43, 0.43, 1);
sprite.renderOrder = 4;
site.sprite = sprite;
labelsRoot.add(sprite);
}
// -------------------------------------------------------------------------
// Neighbor bonds
// -------------------------------------------------------------------------
const maximumBondVertices = N * N * N * 18;
const bondPositions = new Float32Array(maximumBondVertices);
const bondAttribute = new THREE.BufferAttribute(bondPositions, 3);
bondAttribute.setUsage(THREE.DynamicDrawUsage);
const bondGeometry = new THREE.BufferGeometry();
bondGeometry.setAttribute("position", bondAttribute);
bondGeometry.setDrawRange(0, 0);
const bondMaterial = new THREE.LineBasicMaterial({
color: 0x8ba3bd,
transparent: true,
opacity: 0.23,
depthWrite: false
});
const bondLines = new THREE.LineSegments(bondGeometry, bondMaterial);
bondLines.frustumCulled = false;
scene.add(bondLines);
function siteIndex(ix, iy, iz) {
return (ix * N + iy) * N + iz;
}
function refreshBonds() {
let cursor = 0;
const directions = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
];
for (const site of sites) {
if (!site.visible) continue;
for (const [dx, dy, dz] of directions) {
const nx = site.ix + dx;
const ny = site.iy + dy;
const nz = site.iz + dz;
if (nx >= N || ny >= N || nz >= N) continue;
const neighbor = sites[siteIndex(nx, ny, nz)];
if (!neighbor.visible) continue;
bondPositions[cursor++] = site.position.x;
bondPositions[cursor++] = site.position.y;
bondPositions[cursor++] = site.position.z;
bondPositions[cursor++] = neighbor.position.x;
bondPositions[cursor++] = neighbor.position.y;
bondPositions[cursor++] = neighbor.position.z;
}
}
bondGeometry.setDrawRange(0, cursor / 3);
bondAttribute.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Artificial boundary, cut faces, and removable-layer guide
// -------------------------------------------------------------------------
const boxSize = SPACING * (N - 1) + 0.44;
const boundaryGeometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(boxSize, boxSize, boxSize)
);
const boundaryMaterial = new THREE.LineDashedMaterial({
color: 0xc8d5e5,
transparent: true,
opacity: 0.38,
dashSize: 0.14,
gapSize: 0.1,
depthWrite: false
});
const boundary = new THREE.LineSegments(boundaryGeometry, boundaryMaterial);
boundary.computeLineDistances();
scene.add(boundary);
const boundaryAnnotation = makeAnnotation(
"ARTIFICIAL FRAGMENT BOUNDARY",
"#c8d5e5",
3.55
);
boundaryAnnotation.position.set(0, HALF + 0.72, 0);
scene.add(boundaryAnnotation);
const cutGuides = new THREE.Group();
scene.add(cutGuides);
const cutLineMaterial = new THREE.LineBasicMaterial({
color: 0xf3bd69,
transparent: true,
opacity: 0.9,
depthWrite: false
});
const cutPlaneMaterial = new THREE.MeshBasicMaterial({
color: 0xf3bd69,
transparent: true,
opacity: 0.026,
side: THREE.DoubleSide,
depthWrite: false
});
function makeRectangle(points, material, dashed = false) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.LineLoop(geometry, material);
if (dashed) line.computeLineDistances();
return line;
}
const b = CUT_BOUNDARY;
const h = HALF;
cutGuides.add(
makeRectangle(
[
new THREE.Vector3(b, b, b),
new THREE.Vector3(b, h, b),
new THREE.Vector3(b, h, h),
new THREE.Vector3(b, b, h)
],
cutLineMaterial
)
);
cutGuides.add(
makeRectangle(
[
new THREE.Vector3(b, b, b),
new THREE.Vector3(h, b, b),
new THREE.Vector3(h, b, h),
new THREE.Vector3(b, b, h)
],
cutLineMaterial
)
);
cutGuides.add(
makeRectangle(
[
new THREE.Vector3(b, b, b),
new THREE.Vector3(h, b, b),
new THREE.Vector3(h, h, b),
new THREE.Vector3(b, h, b)
],
cutLineMaterial
)
);
const cutPlaneSize = h - b;
const cutPlaneCenter = (h + b) * 0.5;
const cutPlaneX = new THREE.Mesh(
new THREE.PlaneGeometry(cutPlaneSize, cutPlaneSize),
cutPlaneMaterial
);
cutPlaneX.rotation.y = Math.PI / 2;
cutPlaneX.position.set(b, cutPlaneCenter, cutPlaneCenter);
cutGuides.add(cutPlaneX);
const cutPlaneY = new THREE.Mesh(
new THREE.PlaneGeometry(cutPlaneSize, cutPlaneSize),
cutPlaneMaterial
);
cutPlaneY.rotation.x = Math.PI / 2;
cutPlaneY.position.set(cutPlaneCenter, b, cutPlaneCenter);
cutGuides.add(cutPlaneY);
const cutPlaneZ = new THREE.Mesh(
new THREE.PlaneGeometry(cutPlaneSize, cutPlaneSize),
cutPlaneMaterial
);
cutPlaneZ.position.set(cutPlaneCenter, cutPlaneCenter, b);
cutGuides.add(cutPlaneZ);
const cutAnnotation = makeAnnotation("CUTAWAY FACE", "#f3bd69", 2.05);
cutAnnotation.position.set(b + 0.08, h + 0.35, b + 0.08);
cutGuides.add(cutAnnotation);
const layerGuide = new THREE.Group();
scene.add(layerGuide);
const layerPlaneMaterial = new THREE.MeshBasicMaterial({
color: 0xb69aff,
transparent: true,
opacity: 0.018,
side: THREE.DoubleSide,
depthWrite: false
});
const layerPlane = new THREE.Mesh(
new THREE.PlaneGeometry(SPACING * (N - 1), SPACING * (N - 1)),
layerPlaneMaterial
);
layerPlane.rotation.y = Math.PI / 2;
layerGuide.add(layerPlane);
const layerFrameMaterial = new THREE.LineDashedMaterial({
color: 0xb69aff,
transparent: true,
opacity: 0.82,
dashSize: 0.11,
gapSize: 0.075,
depthWrite: false
});
const layerFrame = makeRectangle(
[
new THREE.Vector3(0, -h, -h),
new THREE.Vector3(0, h, -h),
new THREE.Vector3(0, h, h),
new THREE.Vector3(0, -h, h)
],
layerFrameMaterial,
true
);
layerGuide.add(layerFrame);
const layerAnnotation = makeAnnotation(
"REMOVABLE x = 0 LAYER",
"#b69aff",
2.95
);
layerAnnotation.position.set(0, -h - 0.58, h + 0.45);
layerGuide.add(layerAnnotation);
// -------------------------------------------------------------------------
// Visibility and layer removal
// -------------------------------------------------------------------------
const instanceDummy = new THREE.Object3D();
function updateIonMatrices() {
for (const site of sites) {
instanceDummy.position.copy(site.position);
instanceDummy.rotation.set(0, 0, 0);
instanceDummy.scale.setScalar(site.visible ? 1 : 0.00001);
instanceDummy.updateMatrix();
site.mesh.setMatrixAt(site.instanceIndex, instanceDummy.matrix);
}
positiveMesh.instanceMatrix.needsUpdate = true;
negativeMesh.instanceMatrix.needsUpdate = true;
}
function applyVisibility() {
for (const site of sites) {
const inRemovedCorner =
cutawayEnabled &&
site.ix >= 4 &&
site.iy >= 4 &&
site.iz >= 4;
site.baseVisible = !inRemovedCorner;
site.visible =
site.baseVisible &&
!(layerRemoved && site.ix === REMOVABLE_IX);
site.sprite.visible = site.visible;
}
labelsRoot.visible = labelsToggle.checked;
cutGuides.visible = cutawayEnabled;
layerPlaneMaterial.opacity = layerRemoved ? 0.007 : 0.018;
layerFrameMaterial.opacity = layerRemoved ? 0.38 : 0.82;
updateIonMatrices();
refreshBonds();
const visible = sites.reduce((count, site) => count + Number(site.visible), 0);
siteCount.textContent = String(visible);
layerState.textContent = layerRemoved
? "Layer x = 0 removed"
: "Layer x = 0 intact";
}
function toggleLayer() {
layerRemoved = !layerRemoved;
layerAction.textContent = layerRemoved
? "Restore x = 0 layer"
: "Remove x = 0 layer";
layerButton.classList.toggle("is-removed", layerRemoved);
layerButton.setAttribute("aria-pressed", String(layerRemoved));
applyVisibility();
announcer.textContent = layerRemoved
? "The central x equals zero layer has been removed."
: "The central x equals zero layer has been restored.";
}
layerButton.addEventListener("click", toggleLayer);
cutawayToggle.addEventListener("change", () => {
cutawayEnabled = cutawayToggle.checked;
applyVisibility();
announcer.textContent = cutawayEnabled
? "Corner cutaway enabled."
: "The full finite cube is shown.";
});
labelsToggle.addEventListener("change", () => {
labelsRoot.visible = labelsToggle.checked;
});
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, and zoom
// -------------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, 0, 0),
desiredTarget: new THREE.Vector3(0, 0, 0),
theta: 0.73,
desiredTheta: 0.73,
phi: 1.03,
desiredPhi: 1.03,
radius: 12.4,
desiredRadius: 12.4
};
const initialCamera = {
theta: controls.theta,
phi: controls.phi,
radius: controls.radius
};
let dragging = false;
let dragMode = "orbit";
let lastPointerX = 0;
let lastPointerY = 0;
let pointerId = null;
const rightVector = new THREE.Vector3();
const upVector = new THREE.Vector3();
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
dragging = true;
dragMode = event.button === 2 || event.shiftKey ? "pan" : "orbit";
pointerId = event.pointerId;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== pointerId) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (dragMode === "orbit") {
controls.desiredTheta -= dx * 0.006;
controls.desiredPhi = THREE.MathUtils.clamp(
controls.desiredPhi - dy * 0.006,
0.18,
Math.PI - 0.18
);
} else {
camera.updateMatrixWorld();
rightVector.setFromMatrixColumn(camera.matrixWorld, 0);
upVector.setFromMatrixColumn(camera.matrixWorld, 1);
const panScale = controls.radius * 0.00125;
controls.desiredTarget
.addScaledVector(rightVector, -dx * panScale)
.addScaledVector(upVector, dy * panScale);
}
});
function endDrag(event) {
if (!dragging || event.pointerId !== pointerId) return;
dragging = false;
pointerId = null;
renderer.domElement.style.cursor = "grab";
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.desiredRadius = THREE.MathUtils.clamp(
controls.desiredRadius * Math.exp(event.deltaY * 0.001),
6.4,
23
);
},
{ passive: false }
);
function resetCamera() {
controls.desiredTheta = initialCamera.theta;
controls.desiredPhi = initialCamera.phi;
controls.desiredRadius = initialCamera.radius;
controls.desiredTarget.set(0, 0, 0);
}
resetButton.addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target !== renderer.domElement) return;
switch (event.code) {
case "ArrowLeft":
controls.desiredTheta += 0.12;
break;
case "ArrowRight":
controls.desiredTheta -= 0.12;
break;
case "ArrowUp":
controls.desiredPhi = Math.max(
0.18,
controls.desiredPhi - 0.1
);
break;
case "ArrowDown":
controls.desiredPhi = Math.min(
Math.PI - 0.18,
controls.desiredPhi + 0.1
);
break;
case "Equal":
case "NumpadAdd":
controls.desiredRadius = Math.max(
6.4,
controls.desiredRadius * 0.9
);
break;
case "Minus":
case "NumpadSubtract":
controls.desiredRadius = Math.min(
23,
controls.desiredRadius * 1.1
);
break;
case "KeyL":
toggleLayer();
break;
case "KeyR":
resetCamera();
break;
default:
return;
}
event.preventDefault();
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const labelCameraVector = new THREE.Vector3();
function updateLabelPositions() {
for (const site of sites) {
if (!site.visible) continue;
labelCameraVector
.subVectors(camera.position, site.position)
.normalize();
site.sprite.position
.copy(site.position)
.addScaledVector(labelCameraVector, 0.255);
}
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const damping = 1 - Math.pow(0.001, delta);
if (rotateToggle.checked) {
controls.desiredTheta += delta * 0.16;
}
controls.theta +=
(controls.desiredTheta - controls.theta) * damping;
controls.phi +=
(controls.desiredPhi - controls.phi) * damping;
controls.radius +=
(controls.desiredRadius - controls.radius) * damping;
controls.target.lerp(controls.desiredTarget, damping);
camera.position.setFromSphericalCoords(
controls.radius,
controls.phi,
controls.theta
);
camera.position.add(controls.target);
camera.lookAt(controls.target);
camera.updateMatrixWorld();
updateLabelPositions();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio || 1, 1.8)
);
renderer.setSize(window.innerWidth, window.innerHeight);
});
applyVisibility();
animate();
</script>
</body>
</html>
```