Prompt specification
Build or customize the Three.js 3D scene "Polarizer angle intensity explorer" 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: Polarizer angle intensity explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/polarizer-angle-intensity-explorer-x1568
Scene Description Prompt:
Create an HTML voxel polarization exhibit with Three.js from cdnjs: rotate an analyzer after an ideal polarized source and plot cosine-squared intensity.
Working Three.js Starter (polarizer-angle-intensity-explorer-x1568):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Polarization Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #050912;
--panel: rgba(12, 20, 35, 0.82);
--panel-border: rgba(116, 160, 220, 0.18);
--text: #e9f2ff;
--muted: #8fa5c5;
--cyan: #52e5ff;
--blue: #4f7dff;
--gold: #ffc766;
--green: #72f2b8;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
background:
radial-gradient(circle at 15% 10%, rgba(45, 93, 170, 0.20), transparent 32rem),
radial-gradient(circle at 85% 80%, rgba(27, 150, 161, 0.13), transparent 30rem),
var(--bg);
}
button,
input {
font: inherit;
}
.app {
width: min(1480px, 100%);
margin: 0 auto;
padding: 22px;
}
header {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
margin-bottom: 18px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--cyan);
font-size: 0.73rem;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.65rem, 3vw, 2.65rem);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 590px;
margin: 10px 0 0;
color: var(--muted);
font-size: 0.95rem;
line-height: 1.55;
}
.source-chip {
flex: 0 0 auto;
padding: 10px 13px;
border: 1px solid rgba(82, 229, 255, 0.28);
border-radius: 10px;
color: #bff6ff;
background: rgba(30, 106, 130, 0.12);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.76rem;
}
.source-chip::before {
content: "";
display: inline-block;
width: 7px;
height: 7px;
margin-right: 8px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
vertical-align: 1px;
}
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 365px;
gap: 18px;
align-items: start;
}
.stage {
position: relative;
min-height: 650px;
overflow: hidden;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: #060c18;
box-shadow: 0 25px 70px rgba(0, 0, 0, 0.35);
}
.stage > canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
.stage > canvas.grabbing {
cursor: grabbing;
}
.stage-hud {
position: absolute;
z-index: 2;
inset: 0;
pointer-events: none;
}
.stage-top {
position: absolute;
top: 16px;
left: 16px;
right: 16px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.stage-tag {
padding: 8px 10px;
border: 1px solid rgba(143, 165, 197, 0.18);
border-radius: 8px;
color: #adc1df;
background: rgba(5, 10, 20, 0.7);
backdrop-filter: blur(10px);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.68rem;
letter-spacing: 0.08em;
}
.stage-tag strong {
color: var(--cyan);
font-weight: 700;
}
.reset-view {
pointer-events: auto;
padding: 8px 10px;
border: 1px solid rgba(143, 165, 197, 0.22);
border-radius: 8px;
color: #d9e9ff;
background: rgba(7, 14, 27, 0.8);
cursor: pointer;
}
.reset-view:hover {
border-color: rgba(82, 229, 255, 0.55);
color: white;
}
.stage-bottom {
position: absolute;
right: 16px;
bottom: 15px;
left: 16px;
display: flex;
justify-content: space-between;
gap: 15px;
color: #8298b8;
font-size: 0.7rem;
letter-spacing: 0.03em;
}
.legend {
display: flex;
align-items: center;
gap: 7px;
}
.legend-dot {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 10px rgba(82, 229, 255, 0.75);
}
.panel {
overflow: hidden;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 20px 55px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(15px);
}
.panel-section {
padding: 19px;
border-bottom: 1px solid rgba(116, 160, 220, 0.12);
}
.panel-section:last-child {
border-bottom: 0;
}
.section-label {
margin: 0 0 13px;
color: var(--muted);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.angle-readout {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 14px;
}
.angle-value {
color: var(--gold);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 2.3rem;
font-weight: 800;
letter-spacing: -0.06em;
}
.angle-context {
color: var(--muted);
font-size: 0.75rem;
text-align: right;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 8px 0 11px;
appearance: none;
border-radius: 10px;
outline: none;
background:
linear-gradient(
90deg,
var(--gold) 0%,
var(--gold) var(--pct, 19%),
rgba(255, 255, 255, 0.1) var(--pct, 19%),
rgba(255, 255, 255, 0.1) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
appearance: none;
border: 3px solid #172238;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 0 1px var(--gold), 0 0 18px rgba(255, 199, 102, 0.45);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #172238;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 0 1px var(--gold), 0 0 18px rgba(255, 199, 102, 0.45);
}
.range-labels {
display: flex;
justify-content: space-between;
color: #667b9b;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.66rem;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 17px;
}
.control-button {
padding: 10px 12px;
border: 1px solid rgba(143, 165, 197, 0.2);
border-radius: 9px;
color: #c9d9f1;
background: rgba(255, 255, 255, 0.035);
cursor: pointer;
transition: 140ms ease;
}
.control-button:hover {
border-color: rgba(82, 229, 255, 0.45);
color: white;
background: rgba(82, 229, 255, 0.07);
}
.control-button.active {
border-color: rgba(114, 242, 184, 0.55);
color: #d9ffed;
background: rgba(114, 242, 184, 0.1);
}
.formula {
padding: 12px;
border: 1px solid rgba(82, 229, 255, 0.14);
border-radius: 10px;
color: #c6f8ff;
background: rgba(82, 229, 255, 0.045);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 1.04rem;
text-align: center;
}
.formula small {
display: block;
margin-top: 5px;
color: var(--muted);
font-family: Inter, ui-sans-serif, system-ui, sans-serif;
font-size: 0.69rem;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 12px;
}
.stat {
padding: 11px;
border: 1px solid rgba(143, 165, 197, 0.12);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.stat-label {
color: #7489a9;
font-size: 0.64rem;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.stat-value {
margin-top: 5px;
color: white;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 1.05rem;
font-weight: 700;
}
.intensity-bar {
height: 7px;
margin-top: 13px;
overflow: hidden;
border-radius: 8px;
background: rgba(255, 255, 255, 0.07);
}
.intensity-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--blue), var(--cyan), var(--green));
box-shadow: 0 0 15px rgba(82, 229, 255, 0.5);
transition: width 80ms linear;
}
.relation {
margin-top: 9px;
color: var(--muted);
font-size: 0.69rem;
}
.plot-wrap {
position: relative;
height: 185px;
border: 1px solid rgba(143, 165, 197, 0.12);
border-radius: 11px;
overflow: hidden;
background:
linear-gradient(rgba(143, 165, 197, 0.025) 1px, transparent 1px),
linear-gradient(90deg, rgba(143, 165, 197, 0.025) 1px, transparent 1px),
rgba(3, 8, 17, 0.42);
background-size: 24px 24px;
}
#plot {
display: block;
width: 100%;
height: 100%;
}
.plot-caption {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: #6e83a3;
font-size: 0.66rem;
}
.note {
margin: 0;
color: #8298b8;
font-size: 0.76rem;
line-height: 1.55;
}
.note strong {
color: #c8d9f0;
}
@media (max-width: 1000px) {
.layout {
grid-template-columns: 1fr;
}
.stage {
min-height: 560px;
}
.panel {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.panel-section {
border-right: 1px solid rgba(116, 160, 220, 0.12);
}
.panel-section:nth-child(even) {
border-right: 0;
}
}
@media (max-width: 650px) {
.app {
padding: 13px;
}
header {
align-items: flex-start;
flex-direction: column;
gap: 12px;
}
.stage {
min-height: 480px;
}
.panel {
display: block;
}
.panel-section {
border-right: 0;
}
.stage-bottom {
flex-direction: column;
gap: 5px;
}
}
</style>
</head>
<body>
<main class="app">
<header>
<div>
<p class="eyebrow">Malus's Law // Voxel Lab</p>
<h1>Polarization Analyzer</h1>
<p class="subtitle">
Rotate the analyzer downstream of an ideal, vertically polarized source.
The transmitted intensity follows the cosine-squared law.
</p>
</div>
<div class="source-chip">SOURCE POLARIZATION · 0°</div>
</header>
<div class="layout">
<section class="stage" id="stage" aria-label="Interactive Three.js polarization exhibit">
<div class="stage-hud">
<div class="stage-top">
<div class="stage-tag">
LIVE OPTICS · <strong id="hudState">PARTIAL TRANSMISSION</strong>
</div>
<button class="reset-view" id="resetView" type="button">Reset camera</button>
</div>
<div class="stage-bottom">
<span>Drag to orbit · Wheel to zoom · Double-click to reset</span>
<span class="legend"><i class="legend-dot"></i> Voxel photon beam</span>
</div>
</div>
</section>
<aside class="panel">
<section class="panel-section">
<p class="section-label">Analyzer orientation</p>
<div class="angle-readout">
<div class="angle-value" id="angleValue">35°</div>
<div class="angle-context">
angle from source axis<br>
α = 0° → 180°
</div>
</div>
<input
id="angleSlider"
type="range"
min="0"
max="180"
step="0.1"
value="35"
aria-label="Analyzer angle"
>
<div class="range-labels">
<span>0° · ALIGNED</span>
<span>90° · CROSSED</span>
<span>180°</span>
</div>
<div class="button-row">
<button class="control-button" id="autoButton" type="button">▶ Auto-rotate</button>
<button class="control-button" id="snapButton" type="button">Snap 90°</button>
</div>
</section>
<section class="panel-section">
<p class="section-label">Malus's law</p>
<div class="formula">
I(α) = I₀ cos²(α)
<small>Normalized to incident intensity I₀</small>
</div>
<div class="stats">
<div class="stat">
<div class="stat-label">Transmitted</div>
<div class="stat-value" id="intensityValue">82.1%</div>
</div>
<div class="stat">
<div class="stat-label">Extinction</div>
<div class="stat-value" id="extinctionValue">17.9%</div>
</div>
</div>
<div class="intensity-bar">
<div class="intensity-fill" id="intensityFill"></div>
</div>
<div class="relation" id="relationText">Partial transmission</div>
</section>
<section class="panel-section">
<p class="section-label">Cosine-squared response</p>
<div class="plot-wrap">
<canvas id="plot" aria-label="Plot of transmitted intensity versus analyzer angle"></canvas>
</div>
<div class="plot-caption">
<span>Analyzer angle α</span>
<span>I / I₀</span>
</div>
</section>
<section class="panel-section">
<p class="section-label">Experiment note</p>
<p class="note">
<strong>Maximum transmission</strong> occurs when the analyzer axis is parallel
to the source polarization. At <strong>90°</strong>, the analyzer rejects the
horizontal component, producing extinction.
</p>
</section>
</aside>
</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 stage = document.getElementById("stage");
const angleSlider = document.getElementById("angleSlider");
const angleValue = document.getElementById("angleValue");
const intensityValue = document.getElementById("intensityValue");
const extinctionValue = document.getElementById("extinctionValue");
const intensityFill = document.getElementById("intensityFill");
const relationText = document.getElementById("relationText");
const hudState = document.getElementById("hudState");
const autoButton = document.getElementById("autoButton");
const snapButton = document.getElementById("snapButton");
const resetView = document.getElementById("resetView");
const plotCanvas = document.getElementById("plot");
let analyzerAngle = Number(angleSlider.value);
let autoRotate = false;
const autoSpeed = 34; // degrees per second
// ---------- Three.js scene ----------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x050a14);
scene.fog = new THREE.Fog(0x050a14, 11, 24);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Three-dimensional voxel polarization apparatus");
stage.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0x8bc8ff, 0x172035, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xd8ecff, 3.1);
keyLight.position.set(-3, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x4fe4ff, 16, 11, 2);
rimLight.position.set(0.5, 1.5, -4);
scene.add(rimLight);
const goldLight = new THREE.PointLight(0xffc766, 9, 7, 2);
goldLight.position.set(0.2, 0.4, 3);
scene.add(goldLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(20, 14),
new THREE.MeshStandardMaterial({
color: 0x07101d,
roughness: 0.92,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.18;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(20, 40, 0x254566, 0x14263b);
grid.position.y = -2.165;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), material);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createLabel(text, color = "#9dc5e8", position = [0, 0, 0]) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 112;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(5, 12, 25, 0.82)";
ctx.strokeStyle = "rgba(125, 173, 224, 0.32)";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.roundRect(5, 5, 630, 102, 18);
ctx.fill();
ctx.stroke();
ctx.fillStyle = color;
ctx.font = "700 38px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 320, 57);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.position.set(...position);
sprite.scale.set(3.35, 0.59, 1);
sprite.renderOrder = 10;
scene.add(sprite);
return sprite;
}
const darkVoxelMaterial = new THREE.MeshStandardMaterial({
color: 0x172438,
roughness: 0.58,
metalness: 0.46
});
const sourceVoxelMaterial = new THREE.MeshStandardMaterial({
color: 0x263b55,
roughness: 0.42,
metalness: 0.55
});
const goldMaterial = new THREE.MeshStandardMaterial({
color: 0xd9a84f,
roughness: 0.3,
metalness: 0.72
});
const cyanMaterial = new THREE.MeshStandardMaterial({
color: 0x52e5ff,
emissive: 0x117e99,
emissiveIntensity: 1.15,
roughness: 0.28,
metalness: 0.25
});
// ---------- Ideal polarized source ----------
const source = new THREE.Group();
source.position.x = -4.05;
scene.add(source);
const sourceGeometry = new THREE.BoxGeometry(0.4, 0.4, 0.4);
for (let ix = 0; ix < 4; ix++) {
for (let iy = 0; iy < 6; iy++) {
for (let iz = 0; iz < 5; iz++) {
const voxel = new THREE.Mesh(sourceGeometry, sourceVoxelMaterial);
voxel.position.set(
(ix - 1.5) * 0.46,
(iy - 2.5) * 0.46,
(iz - 2) * 0.46
);
voxel.castShadow = true;
voxel.receiveShadow = true;
source.add(voxel);
}
}
}
addBox(source, [0.16, 1.45, 1.45], [0.97, 0, 0], cyanMaterial, false);
addBox(source, [0.15, 0.15, 0.15], [1.08, 0.95, 0.95], goldMaterial, false);
// Source support
addBox(scene, [1.75, 0.22, 1.65], [-4.05, -1.95, 0], darkVoxelMaterial);
addBox(scene, [0.45, 0.58, 0.45], [-4.05, -1.63, 0], darkVoxelMaterial);
// Vertical source polarization arrow
const sourceArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(-3.05, -1.05, -1.55),
2.1,
0x52e5ff,
0.34,
0.2
);
scene.add(sourceArrow);
createLabel("IDEAL SOURCE", "#8cecff", [-4.02, -1.94, 1.4]);
// ---------- Analyzer ----------
const analyzerAnchor = new THREE.Group();
analyzerAnchor.position.x = 0.2;
scene.add(analyzerAnchor);
const analyzer = new THREE.Group();
analyzerAnchor.add(analyzer);
const analyzerVoxelGeometry = new THREE.BoxGeometry(0.43, 0.43, 0.43);
// Side rails of the rotating analyzer.
for (const z of [-1.35, 1.35]) {
for (let i = 0; i < 5; i++) {
const voxel = new THREE.Mesh(analyzerVoxelGeometry, goldMaterial);
voxel.position.set(0, (i - 2) * 0.54, z);
voxel.castShadow = true;
voxel.receiveShadow = true;
analyzer.add(voxel);
}
}
// Top and bottom rails.
for (const y of [-1.35, 1.35]) {
for (let i = 0; i < 5; i++) {
const voxel = new THREE.Mesh(analyzerVoxelGeometry, goldMaterial);
voxel.position.set(0, y, (i - 2) * 0.54);
voxel.castShadow = true;
voxel.receiveShadow = true;
analyzer.add(voxel);
}
}
const analyzerPanel = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 2.45, 2.45),
new THREE.MeshStandardMaterial({
color: 0x42bcd5,
emissive: 0x0c667c,
emissiveIntensity: 0.5,
transparent: true,
opacity: 0.18,
roughness: 0.22,
metalness: 0.12,
side: THREE.DoubleSide,
depthWrite: false
})
);
analyzer.add(analyzerPanel);
// Transmission axis.
addBox(analyzer, [0.12, 2.28, 0.075], [0.17, 0, 0.48], cyanMaterial, false);
const analyzerArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(0.19, -1.08, 0.48),
2.16,
0x8cecff,
0.32,
0.18
);
analyzer.add(analyzerArrow);
// Static protractor ring around the beam axis.
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.72, 0.025, 8, 96),
new THREE.MeshStandardMaterial({
color: 0x7d9abf,
emissive: 0x182d45,
emissiveIntensity: 0.7,
roughness: 0.35,
metalness: 0.6
})
);
ring.rotation.y = Math.PI / 2;
analyzerAnchor.add(ring);
const tickMaterial = new THREE.MeshStandardMaterial({
color: 0x6f89aa,
roughness: 0.42,
metalness: 0.55
});
for (let i = 0; i < 12; i++) {
const a = (i / 12) * Math.PI * 2;
const tick = new THREE.Mesh(
new THREE.BoxGeometry(0.10, i % 3 === 0 ? 0.28 : 0.18, 0.065),
tickMaterial
);
tick.position.set(0, Math.cos(a) * 1.72, Math.sin(a) * 1.72);
tick.rotation.x = a;
analyzerAnchor.add(tick);
}
addBox(scene, [0.52, 0.62, 0.52], [0.2, -1.78, 0], darkVoxelMaterial);
addBox(scene, [1.35, 0.2, 1.25], [0.2, -2.04, 0], darkVoxelMaterial);
createLabel("ROTATING ANALYZER", "#ffd38c", [0.2, -1.92, 1.45]);
// ---------- Detector ----------
const detector = new THREE.Group();
detector.position.x = 3.8;
scene.add(detector);
const detectorBody = addBox(
detector,
[1.05, 2.65, 2.3],
[0, 0, 0],
darkVoxelMaterial
);
const detectorEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(detectorBody.geometry),
new THREE.LineBasicMaterial({ color: 0x577493, transparent: true, opacity: 0.65 })
);
detector.add(detectorEdges);
const detectorScreenMaterial = new THREE.MeshStandardMaterial({
color: 0x173b52,
emissive: 0x1bc6ee,
emissiveIntensity: 0.3,
roughness: 0.28,
metalness: 0.2
});
addBox(detector, [0.13, 1.72, 1.72], [-0.59, 0, 0], detectorScreenMaterial, false);
addBox(detector, [0.18, 0.18, 0.18], [-0.69, 1.02, 0.73], cyanMaterial, false);
addBox(scene, [0.52, 0.62, 0.52], [3.8, -1.78, 0], darkVoxelMaterial);
addBox(scene, [1.35, 0.2, 1.35], [3.8, -2.04, 0], darkVoxelMaterial);
createLabel("INTENSITY DETECTOR", "#a8d7ff", [3.78, -1.92, 1.45]);
// ---------- Voxel beam ----------
const beamGeometry = new THREE.BoxGeometry(0.18, 0.18, 0.18);
const incomingMaterials = [];
for (let i = 0; i < 11; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0x64eaff,
transparent: true,
opacity: 0.9
});
const segment = new THREE.Mesh(beamGeometry, material);
segment.position.set(-2.9 + i * 0.255, 0, 0);
scene.add(segment);
incomingMaterials.push(material);
}
const outgoingMaterials = [];
for (let i = 0; i < 11; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0x52e5ff,
transparent: true,
opacity: 0.7
});
const segment = new THREE.Mesh(beamGeometry, material);
segment.position.set(0.48 + i * 0.255, 0, 0);
scene.add(segment);
outgoingMaterials.push(material);
}
// ---------- Camera controls implemented directly ----------
const cameraState = {
target: new THREE.Vector3(-0.15, -0.15, 0),
radius: 11.8,
theta: 0.86,
phi: 1.08
};
const defaultCamera = { ...cameraState };
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.cos(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.sin(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.radius = defaultCamera.radius;
cameraState.theta = defaultCamera.theta;
cameraState.phi = defaultCamera.phi;
cameraState.target.copy(defaultCamera.target);
updateCamera();
}
let dragging = false;
let dragX = 0;
let dragY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("grabbing");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - dragX;
const dy = event.clientY - dragY;
dragX = event.clientX;
dragY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.28,
Math.PI - 0.28
);
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("grabbing");
if (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();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.008,
7.2,
17.5
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
resetView.addEventListener("click", resetCamera);
// ---------- Readouts and plot ----------
const plotContext = plotCanvas.getContext("2d");
function drawPlot() {
const rect = plotCanvas.getBoundingClientRect();
if (rect.width < 1 || rect.height < 1) return;
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const width = rect.width;
const height = rect.height;
const pixelWidth = Math.round(width * dpr);
const pixelHeight = Math.round(height * dpr);
if (plotCanvas.width !== pixelWidth || plotCanvas.height !== pixelHeight) {
plotCanvas.width = pixelWidth;
plotCanvas.height = pixelHeight;
}
const ctx = plotContext;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, width, height);
const padding = {
left: 39,
right: 13,
top: 15,
bottom: 28
};
const graphWidth = width - padding.left - padding.right;
const graphHeight = height - padding.top - padding.bottom;
const xMap = (degrees) =>
padding.left + (degrees / 180) * graphWidth;
const yMap = (intensity) =>
padding.top + (1 - intensity) * graphHeight;
// Grid and axes.
ctx.lineWidth = 1;
ctx.font = "10px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textBaseline = "middle";
for (let degrees = 0; degrees <= 180; degrees += 30) {
const x = xMap(degrees);
ctx.strokeStyle = "rgba(143,165,197,0.13)";
ctx.beginPath();
ctx.moveTo(x, padding.top);
ctx.lineTo(x, padding.top + graphHeight);
ctx.stroke();
ctx.fillStyle = "#7186a6";
ctx.textAlign = "center";
ctx.fillText(`${degrees}°`, x, height - 12);
}
for (let level = 0; level <= 1.0001; level += 0.25) {
const y = yMap(level);
ctx.strokeStyle = "rgba(143,165,197,0.13)";
ctx.beginPath();
ctx.moveTo(padding.left, y);
ctx.lineTo(padding.left + graphWidth, y);
ctx.stroke();
ctx.fillStyle = "#7186a6";
ctx.textAlign = "right";
ctx.fillText(level.toFixed(2), padding.left - 7, y);
}
// Cosine-squared curve.
const gradient = ctx.createLinearGradient(0, padding.top, 0, padding.top + graphHeight);
gradient.addColorStop(0, "rgba(82,229,255,0.30)");
gradient.addColorStop(1, "rgba(79,125,255,0.025)");
ctx.beginPath();
ctx.moveTo(xMap(0), yMap(1));
for (let degree = 0; degree <= 180; degree++) {
const radians = degree * Math.PI / 180;
const intensity = Math.cos(radians) ** 2;
ctx.lineTo(xMap(degree), yMap(intensity));
}
ctx.lineTo(xMap(180), yMap(0));
ctx.closePath();
ctx.fillStyle = gradient;
ctx.fill();
ctx.beginPath();
for (let degree = 0; degree <= 180; degree++) {
const radians = degree * Math.PI / 180;
const intensity = Math.cos(radians) ** 2;
const x = xMap(degree);
const y = yMap(intensity);
if (degree === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.strokeStyle = "#62e8ff";
ctx.lineWidth = 2.25;
ctx.stroke();
// Current analyzer position.
const currentRadians = analyzerAngle * Math.PI / 180;
const currentIntensity = Math.cos(currentRadians) ** 2;
const currentX = xMap(analyzerAngle);
const currentY = yMap(currentIntensity);
ctx.strokeStyle = "rgba(255,199,102,0.78)";
ctx.lineWidth = 1.5;
ctx.setLineDash([4, 4]);
ctx.beginPath();
ctx.moveTo(currentX, padding.top);
ctx.lineTo(currentX, padding.top + graphHeight);
ctx.stroke();
ctx.setLineDash([]);
ctx.fillStyle = "#ffc766";
ctx.beginPath();
ctx.arc(currentX, currentY, 5.2, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = "#fff1cb";
ctx.lineWidth = 1.5;
ctx.stroke();
}
function updateReadouts() {
const radians = analyzerAngle * Math.PI / 180;
const transmitted = Math.cos(radians) ** 2;
const extinction = 1 - transmitted;
analyzer.rotation.x = radians;
angleValue.textContent = `${analyzerAngle.toFixed(1)}°`;
intensityValue.textContent = `${(transmitted * 100).toFixed(1)}%`;
extinctionValue.textContent = `${(extinction * 100).toFixed(1)}%`;
intensityFill.style.width = `${transmitted * 100}%`;
angleSlider.style.setProperty("--pct", `${(analyzerAngle / 180) * 100}%`);
outgoingMaterials.forEach((material, index) => {
const pulse = 0.94 + 0.06 * Math.sin(index * 0.75);
material.opacity = (0.015 + transmitted * 0.84) * pulse;
material.color.setHSL(
0.535,
0.93,
0.35 + transmitted * 0.19
);
});
detectorScreenMaterial.emissiveIntensity = 0.025 + transmitted * 2.45;
detectorScreenMaterial.emissive.setHSL(
0.53,
0.92,
0.25 + transmitted * 0.22
);
detectorScreenMaterial.color.setHSL(
0.54,
0.55,
0.13 + transmitted * 0.16
);
if (analyzerAngle % 180 === 0) {
relationText.textContent = "Aligned · maximum transmission";
hudState.textContent = "ALIGNED · MAXIMUM";
} else if (analyzerAngle === 90) {
relationText.textContent = "Crossed · extinction";
hudState.textContent = "CROSSED · EXTINCTION";
} else if ((transmitted * 100).toFixed(1) === "50.0") {
relationText.textContent = "Partial transmission · 50%";
hudState.textContent = "PARTIAL TRANSMISSION";
} else if (transmitted > 0.5) {
relationText.textContent = "Partial transmission · more than 50%";
hudState.textContent = "PARTIAL TRANSMISSION";
} else {
relationText.textContent = "Partial transmission · less than 50%";
hudState.textContent = "LOW TRANSMISSION";
}
drawPlot();
}
angleSlider.addEventListener("input", () => {
analyzerAngle = Number(angleSlider.value);
updateReadouts();
});
autoButton.addEventListener("click", () => {
autoRotate = !autoRotate;
autoButton.textContent = autoRotate ? "❚❚ Pause rotation" : "▶ Auto-rotate";
autoButton.classList.toggle("active", autoRotate);
});
snapButton.addEventListener("click", () => {
analyzerAngle = 90;
angleSlider.value = "90";
updateReadouts();
});
const stageObserver = new ResizeObserver(() => {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
drawPlot();
});
stageObserver.observe(stage);
const plotObserver = new ResizeObserver(drawPlot);
plotObserver.observe(plotCanvas);
updateReadouts();
updateCamera();
// ---------- Animation loop ----------
let previousTime = performance.now();
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (autoRotate) {
analyzerAngle = (analyzerAngle + autoSpeed * delta) % 180;
angleSlider.value = analyzerAngle.toFixed(1);
updateReadouts();
}
const subtlePulse = 0.85 + 0.15 * Math.sin(now * 0.003);
incomingMaterials.forEach((material) => {
material.opacity = 0.78 * subtlePulse;
});
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```