Prompt specification
Build or customize the Three.js 3D scene "Electromagnet sorting teaching station" 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: Electromagnet sorting teaching station
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/electromagnet-sorting-teaching-station-x1671
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a scene with a virtual coil-core gripper above labeled ferromagnetic and nonmagnetic sample blocks; illustrate selective attraction qualitatively without treating every metal as magnetically attracted. Voxel style.
Working Three.js Starter (electromagnet-sorting-teaching-station-x1671):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Magnetic Pick Lab</title>
<style>
:root {
color-scheme: dark;
--cyan: #65ddff;
--amber: #ffc45c;
--green: #78f0ad;
--muted: #91a4b7;
--panel: rgba(13, 21, 34, 0.82);
--border: rgba(157, 204, 235, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 28% 18%, rgba(38, 101, 132, 0.22), transparent 34%),
radial-gradient(circle at 78% 72%, rgba(113, 54, 129, 0.18), transparent 35%),
linear-gradient(145deg, #08111e 0%, #0c1524 48%, #090f19 100%);
color: #e9f4fb;
font-family: "Courier New", Courier, monospace;
}
body {
user-select: none;
}
#app,
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: absolute;
inset: 0;
pointer-events: none;
background:
linear-gradient(to bottom, rgba(2, 7, 13, 0.28), transparent 25%),
radial-gradient(ellipse at center, transparent 48%, rgba(2, 7, 13, 0.54) 100%);
}
.topbar {
position: absolute;
top: 24px;
left: 26px;
z-index: 5;
pointer-events: none;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 42px);
line-height: 0.95;
letter-spacing: -0.06em;
text-transform: uppercase;
}
.subtitle {
margin-top: 10px;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.06em;
}
.legend {
position: absolute;
top: 24px;
right: 26px;
z-index: 5;
width: min(315px, calc(100vw - 52px));
padding: 16px;
border: 1px solid var(--border);
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(12px);
pointer-events: none;
}
.legend h2 {
margin: 0 0 10px;
color: #ffffff;
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend p {
margin: 0;
color: #aebdca;
font-family: Arial, sans-serif;
font-size: 12px;
line-height: 1.55;
}
.legend strong {
color: var(--cyan);
}
.legend-row {
display: flex;
flex-wrap: wrap;
gap: 10px 16px;
margin-top: 14px;
padding-top: 12px;
border-top: 1px solid var(--border);
color: #b8c7d4;
font-size: 10px;
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 9px;
height: 9px;
box-shadow: 0 0 10px currentColor;
background: currentColor;
}
.swatch.field {
color: var(--amber);
}
.swatch.response {
color: var(--green);
}
.swatch.no-response {
color: #718399;
}
.sample-label {
position: absolute;
z-index: 4;
width: 220px;
padding: 11px 13px;
border: 1px solid var(--border);
border-left: 3px solid var(--cyan);
background: rgba(9, 17, 28, 0.88);
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.28);
transform: translate(-50%, -100%);
pointer-events: none;
backdrop-filter: blur(8px);
}
.sample-label.nonmagnetic {
border-left-color: #8192a5;
}
.sample-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 19px;
background: linear-gradient(var(--cyan), transparent);
}
.sample-label.nonmagnetic::after {
background: linear-gradient(#8192a5, transparent);
}
.label-kicker {
display: block;
margin-bottom: 5px;
color: var(--cyan);
font-size: 9px;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.nonmagnetic .label-kicker {
color: #95a7b9;
}
.sample-label strong {
display: block;
color: #f3f9fc;
font-size: 13px;
letter-spacing: 0.04em;
}
.sample-label .status {
display: block;
margin-top: 6px;
color: #9eb0bf;
font-family: Arial, sans-serif;
font-size: 11px;
}
#magnetLabel {
width: 170px;
border-left-color: var(--amber);
}
#magnetLabel .label-kicker {
color: var(--amber);
}
#magnetLabel::after {
background: linear-gradient(var(--amber), transparent);
}
.controls {
position: absolute;
left: 50%;
bottom: 24px;
z-index: 6;
display: flex;
align-items: center;
gap: 9px;
padding: 10px;
border: 1px solid var(--border);
background: rgba(9, 17, 29, 0.9);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
transform: translateX(-50%);
backdrop-filter: blur(12px);
}
button {
min-height: 38px;
padding: 0 15px;
border: 1px solid #395064;
border-radius: 0;
background: #142235;
color: #d8e8f2;
font: 700 11px/1 "Courier New", Courier, monospace;
letter-spacing: 0.06em;
text-transform: uppercase;
cursor: pointer;
transition:
background 140ms ease,
border-color 140ms ease,
color 140ms ease,
transform 140ms ease;
}
button:hover {
border-color: var(--cyan);
background: #1a3045;
color: #ffffff;
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--amber);
outline-offset: 2px;
}
button:disabled {
cursor: wait;
opacity: 0.55;
transform: none;
}
#powerBtn.active {
border-color: rgba(120, 240, 173, 0.8);
background: rgba(42, 111, 83, 0.45);
color: var(--green);
}
#demoBtn {
border-color: rgba(255, 196, 92, 0.75);
background: rgba(111, 75, 24, 0.46);
color: #ffd98e;
}
.control-note {
padding: 0 8px;
color: #7f93a6;
font-family: Arial, sans-serif;
font-size: 10px;
white-space: nowrap;
}
.interaction-hint {
position: absolute;
left: 26px;
bottom: 25px;
z-index: 5;
color: #708397;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
.disclaimer {
position: absolute;
right: 26px;
bottom: 25px;
z-index: 5;
max-width: 270px;
color: #708397;
font-family: Arial, sans-serif;
font-size: 10px;
line-height: 1.4;
text-align: right;
pointer-events: none;
}
@media (max-width: 900px) {
.legend,
.disclaimer {
display: none;
}
.topbar {
top: 18px;
left: 18px;
}
.interaction-hint {
left: 18px;
bottom: 91px;
}
.controls {
bottom: 16px;
width: calc(100vw - 24px);
justify-content: center;
flex-wrap: wrap;
}
.control-note {
display: none;
}
.sample-label {
width: 175px;
}
}
@media (max-width: 560px) {
h1 {
font-size: 25px;
}
.subtitle {
font-size: 10px;
}
.sample-label {
width: 150px;
padding: 8px 9px;
}
.sample-label strong {
font-size: 10px;
}
.sample-label .status {
font-size: 9px;
}
button {
padding: 0 10px;
font-size: 9px;
}
}
</style>
</head>
<body>
<main id="app">
<div id="scene" aria-label="Interactive 3D voxel scene of a magnetic gripper above two metal specimens"></div>
<div class="vignette"></div>
<header class="topbar">
<div class="eyebrow">Voxel Lab / Selectivity Test 01</div>
<h1>Magnetic Pick</h1>
<div class="subtitle">Coil-core gripper · two metal specimens · qualitative response</div>
</header>
<aside class="legend">
<h2>Not all metal is magnetic</h2>
<p>
The magnetic field reaches <strong>both specimens</strong>, but only the
<strong>ferromagnetic sample</strong> is pulled upward. The nonmagnetic
metal alloy remains in place.
</p>
<div class="legend-row">
<span class="legend-item"><i class="swatch field"></i>Field</span>
<span class="legend-item"><i class="swatch response"></i>Response</span>
<span class="legend-item"><i class="swatch no-response"></i>No pull</span>
</div>
</aside>
<div id="ferroLabel" class="sample-label">
<span class="label-kicker">Specimen A / Ferrous</span>
<strong>FERROMAGNETIC</strong>
<span id="ferroStatus" class="status" aria-live="polite">Responds to the magnetic field</span>
</div>
<div id="nonmagLabel" class="sample-label nonmagnetic">
<span class="label-kicker">Specimen B / Metal</span>
<strong>NONMAGNETIC ALLOY</strong>
<span id="nonmagStatus" class="status" aria-live="polite">Field present · no pull</span>
</div>
<div id="magnetLabel" class="sample-label">
<span class="label-kicker">Electromagnet</span>
<strong>COIL CORE</strong>
<span class="status">Energized / field on</span>
</div>
<section class="controls" aria-label="Scene controls">
<button id="powerBtn" class="active" type="button" aria-pressed="true">Power: On</button>
<button id="demoBtn" type="button">Demo Selective Pick</button>
<button id="resetBtn" type="button">Reset</button>
<span class="control-note">Space: power · D: demo · R: reset</span>
</section>
<div class="interaction-hint">Drag to orbit · Scroll to zoom</div>
<div class="disclaimer">
Illustrative keyframed animation.<br />
No electromagnetic force calculation is performed.
</div>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const host = document.getElementById("scene");
const ferroLabel = document.getElementById("ferroLabel");
const nonmagLabel = document.getElementById("nonmagLabel");
const magnetLabel = document.getElementById("magnetLabel");
const ferroStatus = document.getElementById("ferroStatus");
const nonmagStatus = document.getElementById("nonmagStatus");
const powerBtn = document.getElementById("powerBtn");
const demoBtn = document.getElementById("demoBtn");
const resetBtn = document.getElementById("resetBtn");
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x0b1422, 17, 31);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("tabindex", "0");
host.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
36,
window.innerWidth / window.innerHeight,
0.1,
80
);
// Custom camera controls: no external controls/addons are used.
const cameraTarget = new THREE.Vector3(0, 2.35, 0.1);
const orbit = {
radius: 14.2,
theta: 0.61,
phi: 1.02,
dragging: false,
lastX: 0,
lastY: 0
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
renderer.domElement.addEventListener("pointerdown", (event) => {
orbit.dragging = true;
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!orbit.dragging) return;
const dx = event.clientX - orbit.lastX;
const dy = event.clientY - orbit.lastY;
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.005, 0.42, 1.43);
updateCamera();
});
function endDrag(event) {
orbit.dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.008,
8.5,
21
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
orbit.radius = 14.2;
orbit.theta = 0.61;
orbit.phi = 1.02;
updateCamera();
});
// Lighting
scene.add(new THREE.HemisphereLight(0xa9dcff, 0x1a1220, 2.1));
const keyLight = new THREE.DirectionalLight(0xfff2df, 3.1);
keyLight.position.set(-5.5, 10, 7.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x33c9ff, 16, 11, 2);
cyanLight.position.set(-5, 3.6, 3);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0xb65cff, 13, 10, 2);
violetLight.position.set(5, 4.2, -1);
scene.add(violetLight);
const materials = {
platform: new THREE.MeshStandardMaterial({
color: 0x101c2a,
roughness: 0.86,
metalness: 0.2
}),
tile: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.88,
metalness: 0.08
}),
rail: new THREE.MeshStandardMaterial({
color: 0x34495c,
roughness: 0.48,
metalness: 0.72
}),
core: new THREE.MeshStandardMaterial({
color: 0x263b50,
roughness: 0.38,
metalness: 0.8
}),
pole: new THREE.MeshStandardMaterial({
color: 0x111b28,
roughness: 0.27,
metalness: 0.88
}),
copper: new THREE.MeshStandardMaterial({
color: 0xb65a26,
emissive: 0x5a1800,
emissiveIntensity: 0.65,
roughness: 0.31,
metalness: 0.76
}),
cyan: new THREE.MeshStandardMaterial({
color: 0x4bd7ff,
emissive: 0x117a9b,
emissiveIntensity: 0.8,
roughness: 0.3,
metalness: 0.35
}),
slate: new THREE.MeshStandardMaterial({
color: 0x74869a,
emissive: 0x182532,
emissiveIntensity: 0.5,
roughness: 0.6,
metalness: 0.35
}),
amber: new THREE.MeshStandardMaterial({
color: 0xffbd46,
emissive: 0x8b4300,
emissiveIntensity: 0.55,
roughness: 0.42,
metalness: 0.25
}),
green: new THREE.MeshStandardMaterial({
color: 0x73f2aa,
emissive: 0x28c76d,
emissiveIntensity: 1.1,
roughness: 0.35,
metalness: 0.1
}),
gray: new THREE.MeshStandardMaterial({
color: 0x718296,
emissive: 0x111b25,
emissiveIntensity: 0.4,
roughness: 0.75,
metalness: 0.25
})
};
function addBox(width, height, depth, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Voxel tabletop
addBox(12.4, 0.42, 7.7, materials.platform, 0, -0.24, 0.15);
const tileGeometry = new THREE.BoxGeometry(0.68, 0.08, 0.68);
const tileCountX = 15;
const tileCountZ = 9;
const tiles = new THREE.InstancedMesh(
tileGeometry,
materials.tile,
tileCountX * tileCountZ
);
const dummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let ix = 0; ix < tileCountX; ix++) {
for (let iz = 0; iz < tileCountZ; iz++) {
const x = (ix - (tileCountX - 1) / 2) * 0.76;
const z = (iz - (tileCountZ - 1) / 2) * 0.76 + 0.15;
dummy.position.set(x, 0.015, z);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const variation = ((ix * 7 + iz * 11) % 5) * 0.012;
tileColor.setHSL(0.58, 0.25, 0.12 + variation);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
// Front edge accent blocks
for (let i = 0; i < 12; i++) {
addBox(
0.68,
0.12,
0.12,
i % 3 === 0 ? materials.cyan : materials.rail,
(i - 5.5) * 0.78,
-0.03,
4.03
);
}
const ferroX = -2.35;
const nonmagX = 2.25;
const sampleZ = 0.35;
const sampleBaseY = 0.1;
function makePad(x, accentMaterial) {
const group = new THREE.Group();
group.position.set(x, 0.1, sampleZ);
addBox(3.0, 0.05, 0.07, accentMaterial, 0, 0, -1.18, group);
addBox(3.0, 0.05, 0.07, accentMaterial, 0, 0, 1.18, group);
addBox(0.07, 0.05, 2.42, accentMaterial, -1.46, 0, 0, group);
addBox(0.07, 0.05, 2.42, accentMaterial, 1.46, 0, 0, group);
scene.add(group);
return group;
}
makePad(ferroX, materials.cyan);
makePad(nonmagX, materials.slate);
// Deterministic pseudo-random colors for voxel variation.
function seededRandom(seed) {
const value = Math.sin(seed * 999.91) * 43758.5453;
return value - Math.floor(value);
}
function makeVoxelBlock(x, z, kind) {
const group = new THREE.Group();
group.position.set(x, sampleBaseY, z);
const nx = 5;
const ny = 4;
const nz = 4;
const cell = 0.38;
const count = nx * ny * nz;
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: kind === "ferro" ? 0.34 : 0.52,
metalness: kind === "ferro" ? 0.82 : 0.65
});
const mesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(cell * 0.91, cell * 0.91, cell * 0.91),
material,
count
);
mesh.castShadow = true;
mesh.receiveShadow = true;
const color = new THREE.Color();
let index = 0;
for (let ix = 0; ix < nx; ix++) {
for (let iy = 0; iy < ny; iy++) {
for (let iz = 0; iz < nz; iz++) {
dummy.position.set(
(ix - (nx - 1) / 2) * cell,
(iy + 0.5) * cell,
(iz - (nz - 1) / 2) * cell
);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
const noise = seededRandom(index + (kind === "ferro" ? 0 : 100));
if (kind === "ferro") {
color.setHSL(0.56, 0.18, 0.29 + noise * 0.07);
} else {
color.setHSL(0.55, 0.08, 0.60 + noise * 0.09);
}
mesh.setColorAt(index, color);
index++;
}
}
}
mesh.instanceMatrix.needsUpdate = true;
mesh.instanceColor.needsUpdate = true;
group.add(mesh);
// Subtle voxel-grid outline
const edgeGeometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(nx * cell - 0.03, ny * cell - 0.03, nz * cell - 0.03)
);
const edgeMaterial = new THREE.LineBasicMaterial({
color: kind === "ferro" ? 0x76c4dc : 0xc1d0da,
transparent: true,
opacity: 0.3
});
const edges = new THREE.LineSegments(edgeGeometry, edgeMaterial);
edges.position.y = (ny * cell) / 2;
group.add(edges);
// Small top markers reinforce each specimen.
const markerMaterial = kind === "ferro" ? materials.amber : materials.gray;
for (let i = 0; i < 3; i++) {
addBox(
0.24,
0.12,
0.24,
markerMaterial,
(i - 1) * 0.43,
ny * cell + 0.03,
-0.25,
group
);
}
scene.add(group);
return group;
}
const ferroBlock = makeVoxelBlock(ferroX, sampleZ, "ferro");
const nonmagBlock = makeVoxelBlock(nonmagX, sampleZ, "nonmag");
// Overhead gantry
addBox(11.1, 0.35, 0.68, materials.rail, 0, 7.22, sampleZ);
addBox(0.38, 6.75, 0.7, materials.rail, -5.35, 3.48, sampleZ);
addBox(0.38, 6.75, 0.7, materials.rail, 5.35, 3.48, sampleZ);
addBox(11.2, 0.18, 1.05, materials.core, 0, 6.91, sampleZ);
for (const x of [-5.35, 5.35]) {
addBox(1.0, 0.24, 1.4, materials.core, x, 0.16, sampleZ);
addBox(0.7, 0.08, 0.08, materials.cyan, x, 0.34, sampleZ + 0.72);
}
// Magnet core assembly. The root's Y position is the bottom of the pole.
const magnetAssembly = new THREE.Group();
scene.add(magnetAssembly);
addBox(1.0, 0.30, 1.0, materials.pole, 0, 0.15, 0, magnetAssembly);
addBox(0.56, 1.5, 0.56, materials.core, 0, 1.02, 0, magnetAssembly);
addBox(2.6, 0.42, 0.68, materials.core, 0, 1.96, 0, magnetAssembly);
addBox(0.52, 0.48, 0.52, materials.rail, 0, 2.35, 0, magnetAssembly);
// Square copper windings made from voxel bars.
for (let ring = 0; ring < 6; ring++) {
const y = 0.38 + ring * 0.22;
const half = 0.68;
const thickness = 0.13;
addBox(
half * 2,
thickness,
thickness,
materials.copper,
0,
y,
-half,
magnetAssembly
);
addBox(
half * 2,
thickness,
thickness,
materials.copper,
0,
y,
half,
magnetAssembly
);
addBox(
thickness,
thickness,
half * 2 - thickness,
materials.copper,
-half,
y,
0,
magnetAssembly
);
addBox(
thickness,
thickness,
half * 2 - thickness,
materials.copper,
half,
y,
0,
magnetAssembly
);
}
const magnetLampMaterial = new THREE.MeshStandardMaterial({
color: 0x7dffb2,
emissive: 0x35ff87,
emissiveIntensity: 2.2,
roughness: 0.25
});
addBox(
0.18,
0.18,
0.18,
magnetLampMaterial,
1.02,
1.97,
0.25,
magnetAssembly
);
// Separate carriage and telescoping stem.
const carriage = new THREE.Group();
scene.add(carriage);
addBox(1.15, 0.46, 1.05, materials.rail, 0, 0, 0, carriage);
addBox(0.58, 0.18, 1.22, materials.core, 0, 0.28, 0, carriage);
const stem = addBox(0.30, 1.0, 0.30, materials.rail, 0, 0, 0);
const magnetGlow = new THREE.PointLight(0xff8d3c, 6, 5, 2);
scene.add(magnetGlow);
// Voxel response arrow above the ferromagnetic sample.
const responseArrow = new THREE.Group();
addBox(0.13, 0.72, 0.13, materials.green, 0, 0.36, 0, responseArrow);
addBox(0.42, 0.13, 0.13, materials.green, 0, 0.74, 0, responseArrow);
addBox(0.26, 0.13, 0.13, materials.green, 0, 0.88, 0, responseArrow);
addBox(0.12, 0.13, 0.13, materials.green, 0, 1.02, 0, responseArrow);
responseArrow.position.set(ferroX, 2.0, 1.42);
responseArrow.visible = false;
scene.add(responseArrow);
// A dim X over the nonmagnetic sample means "no response".
const noPullMarker = new THREE.Group();
for (const rotation of [Math.PI / 4, -Math.PI / 4]) {
const bar = addBox(
0.72,
0.10,
0.10,
materials.gray,
0,
0,
0,
noPullMarker
);
bar.rotation.z = rotation;
}
noPullMarker.position.set(nonmagX, 2.05, 1.42);
scene.add(noPullMarker);
// Dotted voxel field paths. One path reaches each specimen:
// field presence does not imply that both specimens will respond.
const FIELD_POINTS_PER_PATH = 24;
const fieldMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.075, 0.075, 0.075),
new THREE.MeshBasicMaterial({
color: 0xffc65c,
transparent: true,
opacity: 0.78,
depthWrite: false,
toneMapped: false
}),
FIELD_POINTS_PER_PATH * 2
);
fieldMesh.frustumCulled = false;
scene.add(fieldMesh);
const fieldColor = new THREE.Color();
for (let i = 0; i < FIELD_POINTS_PER_PATH * 2; i++) {
fieldColor.set(i < FIELD_POINTS_PER_PATH ? 0xffc65c : 0xffb13d);
fieldMesh.setColorAt(i, fieldColor);
}
fieldMesh.instanceColor.needsUpdate = true;
// Animation state
const state = {
powered: true,
playing: false,
demoTime: 0,
magnetX: 0,
magnetY: 4.05,
ferroLift: 0
};
const clamp01 = (value) => Math.min(1, Math.max(0, value));
const smooth = (value) => {
const t = clamp01(value);
return t * t * (3 - 2 * t);
};
const lerp = THREE.MathUtils.lerp;
function segment(time, start, end, from, to) {
return lerp(from, to, smooth((time - start) / (end - start)));
}
function resetScene() {
state.playing = false;
state.demoTime = 0;
state.magnetX = 0;
state.magnetY = 4.05;
state.ferroLift = 0;
state.powered = true;
powerBtn.classList.add("active");
powerBtn.setAttribute("aria-pressed", "true");
powerBtn.textContent = "Power: On";
demoBtn.disabled = false;
ferroStatus.textContent = "Responds to the magnetic field";
nonmagStatus.textContent = "Field present · no pull";
magnetLabel.querySelector(".status").textContent = "Energized / field on";
}
function setPower(value) {
state.powered = value;
powerBtn.classList.toggle("active", value);
powerBtn.setAttribute("aria-pressed", String(value));
powerBtn.textContent = value ? "Power: On" : "Power: Off";
if (!value) {
state.playing = false;
demoBtn.disabled = false;
ferroStatus.textContent = "Power off · no magnetic field";
nonmagStatus.textContent = "Power off · no pull";
magnetLabel.querySelector(".status").textContent = "De-energized";
} else {
ferroStatus.textContent = "Responds to the magnetic field";
nonmagStatus.textContent = "Field present · no pull";
magnetLabel.querySelector(".status").textContent = "Energized / field on";
}
}
function startDemo() {
state.powered = true;
state.playing = true;
state.demoTime = 0;
state.magnetX = 0;
state.magnetY = 4.05;
state.ferroLift = 0;
powerBtn.classList.add("active");
powerBtn.setAttribute("aria-pressed", "true");
powerBtn.textContent = "Power: On";
demoBtn.disabled = true;
}
powerBtn.addEventListener("click", () => {
setPower(!state.powered);
});
demoBtn.addEventListener("click", startDemo);
resetBtn.addEventListener("click", resetScene);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
if (event.code === "Space") {
event.preventDefault();
setPower(!state.powered);
} else if (event.key.toLowerCase() === "d") {
startDemo();
} else if (event.key.toLowerCase() === "r") {
resetScene();
}
});
function updateDemo(delta) {
if (!state.playing) return;
state.demoTime += delta;
const t = state.demoTime;
// Move the coil over the ferromagnetic sample.
state.magnetX = segment(t, 0.0, 1.15, 0, ferroX);
// Lower the pole toward the ferromagnetic specimen.
state.magnetY = segment(t, 1.15, 2.05, 4.05, 2.05);
// Only the ferromagnetic block rises.
state.ferroLift = segment(t, 1.45, 2.35, 0, 0.41);
// Return the assembly and reset the specimen.
if (t >= 3.35) {
state.magnetY = segment(t, 3.35, 4.25, 2.05, 4.05);
state.ferroLift = segment(t, 3.45, 4.1, 0.41, 0);
}
if (t >= 4.25) {
state.magnetX = segment(t, 4.25, 5.05, ferroX, 0);
}
if (t >= 1.45 && t < 3.35) {
ferroStatus.textContent = "Pulling · field response";
} else {
ferroStatus.textContent = "Responds to the magnetic field";
}
nonmagStatus.textContent = "Field present · no pull";
if (t >= 5.05) {
state.playing = false;
state.magnetX = 0;
state.magnetY = 4.05;
state.ferroLift = 0;
demoBtn.disabled = false;
}
}
const fieldP0 = new THREE.Vector3();
const fieldP1 = new THREE.Vector3();
const fieldP2 = new THREE.Vector3();
const fieldPoint = new THREE.Vector3();
function updateField() {
fieldMesh.visible = state.powered;
if (!state.powered) return;
const targets = [
new THREE.Vector3(
ferroX,
sampleBaseY + 1.56 + state.ferroLift,
sampleZ
),
new THREE.Vector3(
nonmagX,
sampleBaseY + 1.56,
sampleZ
)
];
let instance = 0;
for (const target of targets) {
fieldP0.set(state.magnetX, state.magnetY - 0.08, sampleZ);
fieldP1.set(
lerp(state.magnetX, target.x, 0.42),
Math.max(1.2, state.magnetY * 0.42),
sampleZ + 0.75
);
fieldP2.copy(target);
for (let i = 0; i < FIELD_POINTS_PER_PATH; i++) {
const u = i / (FIELD_POINTS_PER_PATH - 1);
const inv = 1 - u;
fieldPoint.set(
inv * inv * fieldP0.x +
2 * inv * u * fieldP1.x +
u * u * fieldP2.x,
inv * inv * fieldP0.y +
2 * inv * u * fieldP1.y +
u * u * fieldP2.y,
inv * inv * fieldP0.z +
2 * inv * u * fieldP1.z +
u * u * fieldP2.z
);
dummy.position.copy(fieldPoint);
dummy.rotation.set(0, (i % 3) * 0.15, 0);
const scale = 0.78 + Math.sin(i * 0.7) * 0.12;
dummy.scale.setScalar(scale);
dummy.updateMatrix();
fieldMesh.setMatrixAt(instance, dummy.matrix);
instance++;
}
}
fieldMesh.instanceMatrix.needsUpdate = true;
}
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.25 &&
projected.x < 1.25 &&
projected.y > -1.25 &&
projected.y < 1.25;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
const labelPosition = new THREE.Vector3();
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
updateDemo(delta);
magnetAssembly.position.set(
state.magnetX,
state.magnetY,
sampleZ
);
carriage.position.set(state.magnetX, 6.99, sampleZ);
const magnetTop = state.magnetY + 2.59;
const railBottom = 6.94;
const stemLength = Math.max(0.15, railBottom - magnetTop);
stem.position.set(
state.magnetX,
magnetTop + stemLength / 2,
sampleZ
);
stem.scale.y = stemLength;
ferroBlock.position.y = sampleBaseY + state.ferroLift;
nonmagBlock.position.y = sampleBaseY;
responseArrow.visible = state.ferroLift > 0.025;
responseArrow.position.y = 1.86 + state.ferroLift;
responseArrow.scale.y = 0.92 + Math.sin(performance.now() * 0.009) * 0.08;
materials.copper.emissiveIntensity = state.powered
? 0.65 + Math.sin(performance.now() * 0.005) * 0.12
: 0.03;
magnetLampMaterial.color.setHex(state.powered ? 0x7dffb2 : 0xff6574);
magnetLampMaterial.emissive.setHex(state.powered ? 0x35ff87 : 0xff183e);
magnetGlow.position.set(state.magnetX, state.magnetY + 0.55, sampleZ);
magnetGlow.intensity = state.powered ? 6 : 0.3;
updateField();
// Project HTML labels onto the 3D scene.
labelPosition.set(ferroX, sampleBaseY + 2.22 + state.ferroLift, sampleZ);
positionLabel(ferroLabel, labelPosition);
labelPosition.set(nonmagX, sampleBaseY + 2.22, sampleZ);
positionLabel(nonmagLabel, labelPosition);
labelPosition.set(state.magnetX, state.magnetY + 2.95, sampleZ);
positionLabel(magnetLabel, labelPosition);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```