Prompt specification
Build or customize the Three.js 3D scene "Lattice cleavage and layer alignment 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: Lattice cleavage and layer alignment display
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/lattice-cleavage-and-layer-alignment-display-x1954
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows an enlarged technical cutaway of a schematic ionic lattice. Arrange alternating positive and negative ion markers in stacked layers, leaving the front and upper layers open enough to inspect their alignment. Let one layer slide sideways by a controlled amount while the others remain fixed. At selected offsets, highlight neighboring like-charge alignments with short connecting marks, contrasting them with the original alternating arrangement. Keep the ions as clearly symbolic spheres or blocks rather than suggesting discrete salt molecules. The mechanical explanation should be identified as qualitative; do not animate a supposedly exact fracture calculation.
Working Three.js Starter (lattice-cleavage-and-layer-alignment-display-x1954):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Schematic Ionic Lattice — Qualitative Cutaway</title>
<style>
:root {
--ink: #14242c;
--muted: #64747b;
--line: #d6dfe2;
--paper: #f5f7f7;
--panel: #fbfcfc;
--positive: #f06b5b;
--negative: #3f73e8;
--highlight: #efb33d;
--teal: #257e7a;
--shadow: 0 18px 55px rgba(25, 44, 52, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
}
body {
overflow: hidden;
color: var(--ink);
background: var(--paper);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
display: flex;
flex-direction: column;
width: 100%;
height: 100dvh;
min-height: 640px;
}
.topbar {
position: relative;
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
min-height: 72px;
padding: 0 26px;
border-bottom: 1px solid var(--line);
background: rgba(251, 252, 252, 0.94);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
display: grid;
place-items: center;
width: 38px;
height: 38px;
flex: 0 0 auto;
color: white;
border-radius: 10px;
background: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 22px;
font-weight: 700;
}
.brand h1 {
margin: 0;
font-size: 15px;
line-height: 1.2;
letter-spacing: 0.055em;
text-transform: uppercase;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.025em;
}
.top-note {
display: flex;
align-items: center;
gap: 9px;
padding: 8px 11px;
color: #52636a;
border: 1px solid var(--line);
border-radius: 999px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
letter-spacing: 0.075em;
text-transform: uppercase;
white-space: nowrap;
}
.top-note::before {
width: 7px;
height: 7px;
content: "";
border-radius: 50%;
background: var(--highlight);
box-shadow: 0 0 0 4px rgba(239, 179, 61, 0.14);
}
main {
display: grid;
grid-template-columns: minmax(0, 1fr) 350px;
min-height: 0;
flex: 1;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #e9eef0;
isolation: isolate;
}
.stage canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.stage-top {
position: absolute;
z-index: 3;
top: 20px;
left: 20px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: calc(100% - 40px);
pointer-events: none;
}
.chip {
padding: 7px 10px;
color: #4b5f67;
border: 1px solid rgba(108, 129, 136, 0.22);
border-radius: 7px;
background: rgba(249, 251, 251, 0.78);
backdrop-filter: blur(8px);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
letter-spacing: 0.085em;
text-transform: uppercase;
}
.stage-help {
position: absolute;
z-index: 3;
top: 20px;
right: 20px;
padding: 7px 10px;
color: #687980;
border: 1px solid rgba(108, 129, 136, 0.18);
border-radius: 7px;
background: rgba(249, 251, 251, 0.72);
backdrop-filter: blur(8px);
font-size: 10px;
letter-spacing: 0.03em;
pointer-events: none;
}
.legend {
position: absolute;
z-index: 3;
left: 20px;
bottom: 20px;
display: grid;
gap: 9px;
padding: 12px 14px;
border: 1px solid rgba(108, 129, 136, 0.2);
border-radius: 10px;
background: rgba(249, 251, 251, 0.82);
backdrop-filter: blur(10px);
box-shadow: 0 10px 28px rgba(32, 52, 60, 0.07);
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: #506169;
font-size: 10px;
letter-spacing: 0.025em;
}
.legend-swatch {
position: relative;
width: 13px;
height: 13px;
flex: 0 0 auto;
border-radius: 50%;
}
.legend-swatch.positive {
background: var(--positive);
box-shadow: inset -2px -2px 4px rgba(80, 20, 15, 0.18);
}
.legend-swatch.negative {
background: var(--negative);
box-shadow: inset -2px -2px 4px rgba(10, 30, 90, 0.2);
}
.legend-swatch.tick {
width: 4px;
height: 17px;
border-radius: 3px;
background: var(--highlight);
box-shadow: 0 0 8px rgba(239, 179, 61, 0.45);
}
.legend-swatch.plate {
width: 15px;
height: 8px;
border: 1px solid #87999f;
border-radius: 2px;
background: rgba(119, 139, 146, 0.08);
}
.stage-caption {
position: absolute;
z-index: 3;
right: 20px;
bottom: 20px;
max-width: 360px;
padding: 9px 12px;
color: #52656d;
border-left: 2px solid var(--teal);
background: rgba(249, 251, 251, 0.76);
backdrop-filter: blur(8px);
font-size: 10px;
line-height: 1.5;
letter-spacing: 0.025em;
pointer-events: none;
}
.tooltip {
position: absolute;
z-index: 8;
display: none;
min-width: 172px;
padding: 10px 11px;
color: #ecf2f3;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 8px;
background: rgba(20, 36, 44, 0.94);
box-shadow: var(--shadow);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
line-height: 1.55;
pointer-events: none;
}
.tooltip strong {
display: block;
margin-bottom: 3px;
color: white;
font-size: 11px;
}
.loading {
position: absolute;
z-index: 12;
inset: 0;
display: grid;
place-items: center;
color: #5f727a;
background: #e9eef0;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.loading.hidden {
display: none;
}
.panel {
position: relative;
z-index: 5;
min-height: 0;
overflow-y: auto;
border-left: 1px solid var(--line);
background: var(--panel);
box-shadow: -12px 0 35px rgba(33, 52, 60, 0.045);
}
.panel-section {
padding: 22px 22px 21px;
border-bottom: 1px solid var(--line);
}
.eyebrow {
margin-bottom: 10px;
color: #718188;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.panel h2 {
margin: 0;
font-size: 20px;
letter-spacing: -0.025em;
}
.panel h3 {
margin: 0 0 10px;
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel p {
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.offset-readout {
display: flex;
align-items: baseline;
justify-content: space-between;
margin: 22px 0 8px;
}
.offset-readout span {
color: #64767d;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.offset-readout output {
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 24px;
font-weight: 700;
letter-spacing: -0.04em;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 8px 0 0;
border-radius: 999px;
outline: none;
background: linear-gradient(
90deg,
#d8e0e2 0%,
#d8e0e2 50%,
var(--teal) 50%,
var(--teal) 50%,
#d8e0e2 50%
);
appearance: none;
-webkit-appearance: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
border: 3px solid white;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 2px 8px rgba(28, 76, 74, 0.32);
cursor: ew-resize;
appearance: none;
-webkit-appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid white;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 2px 8px rgba(28, 76, 74, 0.32);
cursor: ew-resize;
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: #829097;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
}
.presets {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
margin-top: 16px;
}
.preset {
min-height: 34px;
padding: 7px 4px;
color: #53666d;
border: 1px solid var(--line);
border-radius: 7px;
background: white;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
cursor: pointer;
transition:
border-color 150ms ease,
color 150ms ease,
background 150ms ease;
}
.preset:hover,
.preset.active {
color: #185f5c;
border-color: rgba(37, 126, 122, 0.48);
background: #edf8f6;
}
.registry-card {
padding: 14px;
border: 1px solid var(--line);
border-radius: 10px;
background: #f5f7f7;
transition:
border-color 180ms ease,
background 180ms ease;
}
.registry-card[data-state="base"] {
border-color: rgba(63, 115, 232, 0.32);
background: #f1f5fd;
}
.registry-card[data-state="like"] {
border-color: rgba(239, 179, 61, 0.65);
background: #fff8e9;
}
.registry-title {
display: flex;
align-items: center;
gap: 8px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.055em;
text-transform: uppercase;
}
.registry-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #8a9aa0;
box-shadow: 0 0 0 4px rgba(138, 154, 160, 0.12);
}
.registry-card[data-state="base"] .registry-dot {
background: var(--negative);
box-shadow: 0 0 0 4px rgba(63, 115, 232, 0.12);
}
.registry-card[data-state="like"] .registry-dot {
background: var(--highlight);
box-shadow: 0 0 0 4px rgba(239, 179, 61, 0.14);
}
.registry-card p {
margin-top: 8px;
font-size: 11px;
line-height: 1.5;
}
.tick-count {
display: flex;
justify-content: space-between;
margin-top: 13px;
padding-top: 11px;
border-top: 1px solid rgba(111, 129, 136, 0.18);
color: #687980;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
letter-spacing: 0.035em;
}
.comparison {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.diagram {
padding: 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: white;
transition:
border-color 160ms ease,
box-shadow 160ms ease;
}
.diagram.active {
border-color: var(--teal);
box-shadow: 0 0 0 2px rgba(37, 126, 122, 0.09);
}
.diagram-title {
margin-bottom: 9px;
color: #718188;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.diagram-row {
display: grid;
grid-template-columns: 29px repeat(4, 1fr);
align-items: center;
gap: 3px;
margin: 4px 0;
}
.diagram-row span:first-child {
color: #89979c;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 7px;
}
.charge {
display: grid;
place-items: center;
width: 19px;
height: 19px;
border-radius: 50%;
color: white;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
font-weight: 800;
}
.charge.pos {
background: var(--positive);
}
.charge.neg {
background: var(--negative);
}
.qualitative-note {
border-left: 3px solid var(--teal);
background: #f2f8f7;
}
.qualitative-note p {
color: #536b70;
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.action {
min-height: 38px;
border: 1px solid var(--line);
border-radius: 8px;
background: white;
color: #4e626a;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.04em;
text-transform: uppercase;
cursor: pointer;
}
.action:hover {
border-color: #a9b7bc;
background: #f5f8f8;
}
.model-note {
margin-top: 14px !important;
font-size: 10px !important;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100dvh;
}
.topbar {
min-height: 66px;
padding: 0 16px;
}
.top-note {
display: none;
}
main {
display: block;
}
.stage {
height: 62dvh;
min-height: 470px;
}
.panel {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
.stage-help {
display: none;
}
.stage-caption {
right: 14px;
bottom: 14px;
max-width: 250px;
}
.legend {
left: 14px;
bottom: 14px;
}
}
@media (max-width: 520px) {
.brand p {
display: none;
}
.brand h1 {
font-size: 12px;
}
.stage {
min-height: 440px;
}
.stage-top {
top: 13px;
left: 13px;
}
.chip:nth-child(2) {
display: none;
}
.legend {
padding: 9px 10px;
}
.legend-row {
font-size: 8px;
}
.stage-caption {
display: none;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark">±</div>
<div>
<h1>Ionic lattice / cutaway study</h1>
<p>Geometric registry of alternating ion sites</p>
</div>
</div>
<div class="top-note">Qualitative model · no fracture solver</div>
</header>
<main>
<section class="stage" id="stage" aria-label="Interactive three-dimensional ionic lattice cutaway">
<div class="loading" id="loading">Constructing lattice…</div>
<div class="stage-top">
<div class="chip">04 stacked planes</div>
<div class="chip">open front / stepped upper cutaway</div>
<div class="chip">ion sites, not salt molecules</div>
</div>
<div class="stage-help">Drag to orbit · scroll to zoom</div>
<div class="legend">
<div class="legend-row">
<span class="legend-swatch positive"></span>
<span>Positive ion site</span>
</div>
<div class="legend-row">
<span class="legend-swatch negative"></span>
<span>Negative ion site</span>
</div>
<div class="legend-row">
<span class="legend-swatch tick"></span>
<span>Selected like-charge registry</span>
</div>
<div class="legend-row">
<span class="legend-swatch plate"></span>
<span>Layer footprint</span>
</div>
</div>
<div class="stage-caption">
Spheres represent separate ionic sites. Short amber marks indicate selected
same-sign vertical alignments; they are registration guides, not bonds.
</div>
<div class="tooltip" id="tooltip"></div>
</section>
<aside class="panel">
<section class="panel-section">
<div class="eyebrow">01 / Controlled displacement</div>
<h2>Slide one layer</h2>
<p>
Layer L1 translates along the horizontal axis. All other layers remain fixed.
The offset is expressed in lattice units, <strong>a</strong>.
</p>
<div class="offset-readout">
<span>Horizontal offset</span>
<output id="offsetValue" for="offset">+0.00 a</output>
</div>
<input
id="offset"
type="range"
min="-1.5"
max="1.5"
step="0.25"
value="0"
aria-label="Horizontal displacement of layer L1 in lattice units"
>
<div class="range-labels">
<span>−1.5 a</span>
<span>0</span>
<span>+1.5 a</span>
</div>
<div class="presets" aria-label="Offset presets">
<button class="preset" type="button" data-offset="-1">−1 a</button>
<button class="preset active" type="button" data-offset="0">Original</button>
<button class="preset" type="button" data-offset="0.5">½ a</button>
<button class="preset" type="button" data-offset="1">+1 a</button>
</div>
</section>
<section class="panel-section">
<div class="eyebrow">02 / Registry readout</div>
<div class="registry-card" id="registryCard" data-state="base" aria-live="polite">
<div class="registry-title">
<span class="registry-dot"></span>
<span id="registryTitle">Alternating registry</span>
</div>
<p id="registryDescription">
Opposite signs remain vertically registered in the original arrangement.
</p>
<div class="tick-count">
<span>Illustrative marks</span>
<span id="tickCount">none</span>
</div>
</div>
</section>
<section class="panel-section">
<div class="eyebrow">03 / Geometric comparison</div>
<h3>Original vs. one-site shift</h3>
<div class="comparison">
<div class="diagram active" id="originalDiagram">
<div class="diagram-title">Original</div>
<div class="diagram-row">
<span>UP</span>
<span class="charge neg">−</span>
<span class="charge pos">+</span>
<span class="charge neg">−</span>
<span class="charge pos">+</span>
</div>
<div class="diagram-row">
<span>LOW</span>
<span class="charge pos">+</span>
<span class="charge neg">−</span>
<span class="charge pos">+</span>
<span class="charge neg">−</span>
</div>
</div>
<div class="diagram" id="shiftedDiagram">
<div class="diagram-title">Shift +1 a</div>
<div class="diagram-row">
<span>UP</span>
<span class="charge pos">+</span>
<span class="charge neg">−</span>
<span class="charge pos">+</span>
<span class="charge neg">−</span>
</div>
<div class="diagram-row">
<span>LOW</span>
<span class="charge pos">+</span>
<span class="charge neg">−</span>
<span class="charge pos">+</span>
<span class="charge neg">−</span>
</div>
</div>
</div>
<p>
The open front and progressively shortened upper planes expose these
vertical relationships without depicting discrete salt molecules.
</p>
</section>
<section class="panel-section qualitative-note">
<div class="eyebrow">04 / Interpretation</div>
<h3>Qualitative mechanical explanation</h3>
<p>
The slider applies a prescribed geometric translation only. It does not
calculate stress, strain energy, charge repulsion, instability, bond
rupture, or a fracture path.
</p>
<p class="model-note">
Amber ticks identify selected same-sign alignments for visual comparison.
Their appearance is not an energetic failure criterion.
</p>
</section>
<section class="panel-section">
<div class="actions">
<button class="action" id="resetView" type="button">Reset view</button>
<button class="action" id="resetOffset" type="button">Reset offset</button>
</div>
</section>
</aside>
</main>
</div>
<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.getElementById("stage");
const loading = document.getElementById("loading");
const tooltip = document.getElementById("tooltip");
const slider = document.getElementById("offset");
const offsetValue = document.getElementById("offsetValue");
const registryCard = document.getElementById("registryCard");
const registryTitle = document.getElementById("registryTitle");
const registryDescription = document.getElementById("registryDescription");
const tickCount = document.getElementById("tickCount");
const originalDiagram = document.getElementById("originalDiagram");
const shiftedDiagram = document.getElementById("shiftedDiagram");
const presetButtons = [...document.querySelectorAll(".preset")];
const COLORS = {
positive: 0xf06b5b,
negative: 0x3f73e8,
highlight: 0xefb33d,
plateFixed: 0x84969c,
plateMoving: 0x257e7a,
background: 0xe9eef0
};
const X_SPACING = 1.08;
const Z_SPACING = 1.05;
const LAYER_HEIGHT = 1.32;
const ION_RADIUS = 0.235;
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.background);
scene.fog = new THREE.Fog(COLORS.background, 15, 25);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 60);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.prepend(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Orbitable cutaway of four stacked layers of alternating positive and negative ion sites"
);
renderer.domElement.tabIndex = 0;
const hemiLight = new THREE.HemisphereLight(0xffffff, 0xaebdc2, 2.0);
scene.add(hemiLight);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.8);
keyLight.position.set(5, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc8e0ef, 1.0);
fillLight.position.set(-6, 4, -5);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0xe7edef,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.52;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(24, 48, 0xaab8bc, 0xc6d1d4);
grid.position.y = -0.505;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
const ionGeometry = new THREE.SphereGeometry(ION_RADIUS, 22, 15);
const symbolTextures = {
"+": makeSymbolTexture("+"),
"−": makeSymbolTexture("−")
};
const layers = [
{
id: "L0",
y: 0,
depths: [-2, -1, 0],
moving: false,
label: "L0 / FIXED"
},
{
id: "L1",
y: LAYER_HEIGHT,
depths: [-2, -1, 0],
moving: true,
label: "L1 / SLIDING"
},
{
id: "L2",
y: LAYER_HEIGHT * 2,
depths: [-2, -1],
moving: false,
label: "L2 / FIXED"
},
{
id: "L3",
y: LAYER_HEIGHT * 3,
depths: [-2],
moving: false,
label: "L3 / FIXED"
}
];
const ionMeshes = [];
const symbolSprites = [];
const siteMap = new Map();
for (let layerIndex = 0; layerIndex < layers.length; layerIndex++) {
const layer = layers[layerIndex];
layer.group = new THREE.Group();
layer.group.position.y = layer.y;
scene.add(layer.group);
const plateColor = layer.moving ? COLORS.plateMoving : COLORS.plateFixed;
const depthPositions = layer.depths.map(d => d * Z_SPACING);
const zMin = Math.min(...depthPositions) - 0.53;
const zMax = Math.max(...depthPositions) + 0.53;
const plateDepth = zMax - zMin;
const plateZ = (zMin + zMax) / 2;
const plate = new THREE.Mesh(
new THREE.PlaneGeometry(7.9, plateDepth),
new THREE.MeshBasicMaterial({
color: plateColor,
transparent: true,
opacity: layer.moving ? 0.075 : 0.035,
depthWrite: false,
side: THREE.DoubleSide
})
);
plate.rotation.x = -Math.PI / 2;
plate.position.set(0, -0.37, plateZ);
plate.renderOrder = 0;
layer.group.add(plate);
const outlinePoints = [
new THREE.Vector3(-3.95, -0.345, zMin),
new THREE.Vector3(3.95, -0.345, zMin),
new THREE.Vector3(3.95, -0.345, zMax),
new THREE.Vector3(-3.95, -0.345, zMax),
new THREE.Vector3(-3.95, -0.345, zMin)
];
const outline = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(outlinePoints),
new THREE.LineBasicMaterial({
color: plateColor,
transparent: true,
opacity: layer.moving ? 0.48 : 0.25
})
);
layer.group.add(outline);
const labelSprite = makeLabelSprite(layer.label);
labelSprite.position.set(-4.25, 0.05, zMin - 0.04);
layer.group.add(labelSprite);
for (let xIndex = 0; xIndex < 7; xIndex++) {
for (const depthIndex of layer.depths) {
const sign = ((xIndex + depthIndex + layerIndex) % 2 === 0) ? "+" : "−";
const x = (xIndex - 3) * X_SPACING;
const z = depthIndex * Z_SPACING;
const color = sign === "+" ? COLORS.positive : COLORS.negative;
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.055,
roughness: 0.42,
metalness: 0.04
});
const mesh = new THREE.Mesh(ionGeometry, material);
mesh.position.set(x, 0, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData = {
layerIndex,
layerId: layer.id,
xIndex,
depthIndex,
sign,
moving: layer.moving,
baseScale: 1,
aligned: false
};
layer.group.add(mesh);
ionMeshes.push(mesh);
const symbol = new THREE.Sprite(
new THREE.SpriteMaterial({
map: symbolTextures[sign],
transparent: true,
depthWrite: false,
depthTest: true,
toneMapped: false
})
);
symbol.scale.set(0.30, 0.30, 1);
symbol.renderOrder = 4;
layer.group.add(symbol);
symbolSprites.push({ mesh, symbol });
siteMap.set(`${layerIndex}:${xIndex}:${depthIndex}`, {
mesh,
sign,
xIndex,
depthIndex,
layerIndex,
layer
});
}
}
}
// Ghost outline preserves the original footprint of the sliding plane.
const ghostOutline = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-3.95, LAYER_HEIGHT - 0.345, -1.58),
new THREE.Vector3(3.95, LAYER_HEIGHT - 0.345, -1.58),
new THREE.Vector3(3.95, LAYER_HEIGHT - 0.345, 1.58),
new THREE.Vector3(-3.95, LAYER_HEIGHT - 0.345, 1.58),
new THREE.Vector3(-3.95, LAYER_HEIGHT - 0.345, -1.58)
]),
new THREE.LineDashedMaterial({
color: 0x6f878e,
dashSize: 0.14,
gapSize: 0.10,
transparent: true,
opacity: 0.35
})
);
ghostOutline.computeLineDistances();
scene.add(ghostOutline);
const displacementArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(0, LAYER_HEIGHT - 0.31, 1.72),
0.01,
COLORS.highlight,
0.24,
0.12
);
displacementArrow.visible = false;
scene.add(displacementArrow);
// Pool of short vertical marks used to call out selected like-charge pairs.
const connectorGeometry = new THREE.CylinderGeometry(0.026, 0.026, 0.42, 10);
const connectorMaterial = new THREE.MeshBasicMaterial({
color: COLORS.highlight,
toneMapped: false
});
const connectorPool = Array.from({ length: 80 }, () => {
const connector = new THREE.Mesh(connectorGeometry, connectorMaterial);
connector.visible = false;
connector.renderOrder = 3;
scene.add(connector);
return connector;
});
// Direct camera controls, implemented without an examples/addons import.
const cameraState = {
target: new THREE.Vector3(0, 1.72, 0),
radius: 10.8,
theta: 0.72,
phi: 1.00
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.target.set(0, 1.72, 0);
cameraState.radius = 10.8;
cameraState.theta = 0.72;
cameraState.phi = 1.00;
updateCamera();
}
let dragging = false;
let dragPointerId = null;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragging = true;
dragPointerId = event.pointerId;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
tooltip.style.display = "none";
});
renderer.domElement.addEventListener("pointermove", event => {
if (dragging && event.pointerId === dragPointerId) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.36,
1.42
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
return;
}
inspectIon(event);
});
function endDrag(event) {
if (event.pointerId !== dragPointerId) return;
dragging = false;
dragPointerId = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("pointerleave", () => {
tooltip.style.display = "none";
clearHover();
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7.0,
18.0
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let hoveredMesh = null;
function inspectIon(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;
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(ionMeshes, false)[0];
clearHover();
if (!hit) {
tooltip.style.display = "none";
return;
}
hoveredMesh = hit.object;
hoveredMesh.scale.setScalar(1.16);
hoveredMesh.material.emissiveIntensity = 0.42;
const data = hoveredMesh.userData;
const chargeName = data.sign === "+" ? "Positive ion site" : "Negative ion site";
tooltip.innerHTML = `
<strong>${chargeName}</strong>
Layer ${data.layerId} · ${data.moving ? "sliding" : "fixed"}<br>
Site x${data.xIndex} / z${data.depthIndex}<br>
Charge ${data.sign}
`;
tooltip.style.display = "block";
const stageRect = stage.getBoundingClientRect();
const left = event.clientX - stageRect.left + 14;
const top = event.clientY - stageRect.top + 14;
tooltip.style.left = `${Math.min(left, stageRect.width - 190)}px`;
tooltip.style.top = `${Math.min(top, stageRect.height - 90)}px`;
}
function clearHover() {
if (!hoveredMesh) return;
hoveredMesh.scale.setScalar(hoveredMesh.userData.baseScale);
hoveredMesh.material.emissiveIntensity = hoveredMesh.userData.aligned ? 0.28 : 0.055;
hoveredMesh = null;
}
let currentOffset = 0;
function applyOffset(value, updateSlider = true) {
currentOffset = Number(value);
if (updateSlider) slider.value = String(currentOffset);
layers[1].group.position.x = currentOffset * X_SPACING;
const signed = currentOffset >= 0 ? "+" : "−";
offsetValue.textContent =
`${signed}${Math.abs(currentOffset).toFixed(2)} a`;
if (Math.abs(currentOffset) > 0.001) {
displacementArrow.visible = true;
displacementArrow.setDirection(
new THREE.Vector3(currentOffset >= 0 ? 1 : -1, 0, 0)
);
displacementArrow.setLength(
Math.abs(currentOffset) * X_SPACING,
Math.min(0.24, 0.12 + Math.abs(currentOffset) * 0.08),
0.12
);
} else {
displacementArrow.visible = false;
}
const isOriginal = Math.abs(currentOffset) < 0.001;
const isLikeAligned = Math.abs(Math.abs(currentOffset) - 1) < 0.001;
presetButtons.forEach(button => {
button.classList.toggle(
"active",
Math.abs(Number(button.dataset.offset) - currentOffset) < 0.001
);
});
originalDiagram.classList.toggle("active", isOriginal);
shiftedDiagram.classList.toggle("active", isLikeAligned);
const count = updateAlignmentMarks(currentOffset);
if (isOriginal) {
registryCard.dataset.state = "base";
registryTitle.textContent = "Alternating registry";
registryDescription.textContent =
"Opposite signs remain vertically registered in the original arrangement.";
tickCount.textContent = "none";
} else if (isLikeAligned) {
registryCard.dataset.state = "like";
registryTitle.textContent = "Like-charge registry";
registryDescription.textContent =
"Selected vertical neighbors now carry the same sign after a one-site lateral shift.";
tickCount.textContent = `${count} vertical ticks`;
} else {
registryCard.dataset.state = "offset";
registryTitle.textContent = "Off-registry offset";
registryDescription.textContent =
"The selected planes no longer form an exact one-site vertical alignment.";
tickCount.textContent = "none at this offset";
}
}
function updateAlignmentMarks(offset) {
for (const ion of ionMeshes) {
ion.userData.aligned = false;
ion.material.emissiveIntensity = 0.055;
}
connectorPool.forEach(connector => {
connector.visible = false;
});
const isLikeAligned = Math.abs(Math.abs(offset) - 1) < 0.001;
if (!isLikeAligned) return 0;
const direction = offset >= 0 ? 1 : -1;
const movingLayer = layers[1];
let count = 0;
const movingIonIds = [...siteMap.keys()].filter(key => {
const [layerIndex, , depthIndex] = key.split(":").map(Number);
return (
layerIndex === 1 &&
movingLayer.depths.includes(depthIndex)
);
});
for (const key of movingIonIds) {
const movingSite = siteMap.get(key);
const targetXIndex = movingSite.xIndex + direction;
if (targetXIndex < 0 || targetXIndex > 6) continue;
for (const neighborLayerIndex of [0, 2]) {
const fixedKey = `${neighborLayerIndex}:${targetXIndex}:${movingSite.depthIndex}`;
const fixedSite = siteMap.get(fixedKey);
if (!fixedSite) continue;
if (fixedSite.sign !== movingSite.sign) continue;
const connector = connectorPool[count];
if (!connector) break;
connector.visible = true;
connector.position.set(
movingSite.x + movingLayer.group.position.x,
(movingLayer.y + layers[neighborLayerIndex].y) / 2,
movingSite.mesh.position.z
);
movingSite.mesh.userData.aligned = true;
fixedSite.mesh.userData.aligned = true;
movingSite.mesh.material.emissiveIntensity = 0.28;
fixedSite.mesh.material.emissiveIntensity = 0.28;
count++;
}
}
return count;
}
slider.addEventListener("input", event => {
applyOffset(event.target.value);
});
presetButtons.forEach(button => {
button.addEventListener("click", () => {
applyOffset(button.dataset.offset);
});
});
document.getElementById("resetOffset").addEventListener("click", () => {
applyOffset(0);
});
document.getElementById("resetView").addEventListener("click", resetCamera);
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.fov = camera.aspect < 1 ? 58 : 42;
camera.updateProjectionMatrix();
updateCamera();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
// Keep the charge glyphs on the camera-facing surface of each sphere.
const tempWorld = new THREE.Vector3();
const tempDirection = new THREE.Vector3();
function updateChargeGlyphs() {
scene.updateMatrixWorld();
for (const { mesh, symbol } of symbolSprites) {
mesh.getWorldPosition(tempWorld);
tempDirection.copy(camera.position).sub(tempWorld).normalize();
symbol.position.copy(mesh.position).addScaledVector(tempDirection, ION_RADIUS + 0.018);
}
}
function makeSymbolTexture(symbol) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 128, 128);
context.strokeStyle = "#ffffff";
context.fillStyle = "#ffffff";
context.lineWidth = 11;
context.lineCap = "round";
context.shadowColor = "rgba(10, 25, 35, 0.35)";
context.shadowBlur = 3;
if (symbol === "+") {
context.beginPath();
context.moveTo(64, 28);
context.lineTo(64, 100);
context.moveTo(28, 64);
context.lineTo(100, 64);
context.stroke();
} else {
context.beginPath();
context.moveTo(30, 64);
context.lineTo(98, 64);
context.stroke();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
return texture;
}
function makeLabelSprite(text) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 96;
const context = canvas.getContext("2d");
context.fillStyle = "rgba(246, 249, 249, 0.90)";
context.fillRect(0, 0, 512, 96);
context.strokeStyle = "rgba(92, 113, 121, 0.35)";
context.lineWidth = 3;
context.strokeRect(2, 2, 508, 92);
context.fillStyle = "#40565e";
context.font = "700 33px ui-monospace, SFMono-Regular, Menlo, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 50);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: false,
toneMapped: false
})
);
sprite.scale.set(1.62, 0.304, 1);
sprite.renderOrder = 10;
return sprite;
}
applyOffset(0);
resetCamera();
resize();
renderer.setAnimationLoop(() => {
updateChargeGlyphs();
renderer.render(scene, camera);
});
requestAnimationFrame(() => {
loading.classList.add("hidden");
});
</script>
</body>
</html>
```