Prompt specification
Build or customize the Three.js 3D scene "Light mixing and filter stacking comparison" 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: Light mixing and filter stacking comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/light-mixing-and-filter-stacking-comparison-x1526
Scene Description Prompt:
Light mixing and filter stacking comparison. Place an additive light panel beside subtractive colored filters under white light; explain that the operations differ and use clearly specified illustrative transmission values. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (light-mixing-and-filter-stacking-comparison-x1526):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Additive Light vs. Subtractive Filters</title>
<style>
:root {
--ink: #17212b;
--muted: #5e6872;
--paper: #f4f1e8;
--panel: rgba(255, 255, 255, 0.9);
--border: rgba(23, 33, 43, 0.13);
--red: #ef3e46;
--green: #20b95a;
--blue: #2775ea;
--cyan: #00cbd7;
--magenta: #e83a9d;
--yellow: #f3bd16;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
background: var(--paper);
}
body {
overflow: hidden;
}
button {
font: inherit;
}
#layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
width: 100vw;
height: 100vh;
}
#stage {
position: relative;
min-width: 0;
overflow: hidden;
background: var(--paper);
}
#canvas-wrap {
position: absolute;
inset: 0;
}
#canvas-wrap canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.scene-header {
position: absolute;
top: 22px;
left: 24px;
z-index: 2;
max-width: 440px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 7px;
color: #53606b;
font-size: 0.73rem;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.scene-header h1 {
margin: 0;
font-size: clamp(1.55rem, 3vw, 2.5rem);
line-height: 1.05;
letter-spacing: -0.045em;
}
.scene-header p:last-child {
margin: 9px 0 0;
max-width: 390px;
color: var(--muted);
font-size: 0.92rem;
line-height: 1.45;
}
.scene-tools {
position: absolute;
right: 18px;
top: 20px;
z-index: 3;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: 330px;
}
.tool-button {
border: 1px solid rgba(23, 33, 43, 0.16);
border-radius: 999px;
padding: 9px 13px;
color: var(--ink);
background: rgba(255, 255, 255, 0.86);
box-shadow: 0 7px 22px rgba(30, 38, 45, 0.08);
cursor: pointer;
backdrop-filter: blur(12px);
transition:
transform 150ms ease,
background 150ms ease;
}
.tool-button:hover {
transform: translateY(-1px);
background: white;
}
.tool-button:focus-visible {
outline: 3px solid rgba(39, 117, 234, 0.3);
outline-offset: 2px;
}
.hint {
position: absolute;
left: 24px;
bottom: 18px;
z-index: 2;
padding: 8px 11px;
border: 1px solid rgba(23, 33, 43, 0.1);
border-radius: 8px;
color: #5b6570;
background: rgba(255, 255, 255, 0.7);
font-size: 0.76rem;
pointer-events: none;
backdrop-filter: blur(8px);
}
aside {
position: relative;
z-index: 4;
height: 100vh;
overflow-y: auto;
padding: 30px 26px 34px;
border-left: 1px solid var(--border);
background:
linear-gradient(180deg, rgba(255,255,255,0.98), rgba(249,248,244,0.96));
box-shadow: -18px 0 48px rgba(36, 45, 54, 0.06);
}
aside h2 {
margin: 5px 0 10px;
font-size: 1.7rem;
line-height: 1.1;
letter-spacing: -0.035em;
}
aside > p {
color: var(--muted);
font-size: 0.93rem;
line-height: 1.55;
}
.model-card {
margin-top: 20px;
padding: 18px;
border: 1px solid var(--border);
border-radius: 16px;
background: rgba(255, 255, 255, 0.78);
}
.model-title {
display: flex;
align-items: center;
gap: 10px;
margin: 0 0 8px;
font-size: 1rem;
font-weight: 800;
}
.model-title .operation {
display: inline-grid;
place-items: center;
width: 34px;
height: 34px;
border-radius: 9px;
color: white;
background: var(--ink);
font-size: 1.15rem;
font-weight: 900;
}
.model-card p {
margin: 8px 0;
color: #58636d;
font-size: 0.88rem;
line-height: 1.5;
}
.equation {
margin: 14px 0;
padding: 12px 13px;
overflow-x: auto;
border-radius: 10px;
color: #eef7ff;
background: #1a2630;
font-family:
"SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 0.8rem;
line-height: 1.6;
white-space: nowrap;
}
.source-list,
.filter-list {
display: grid;
gap: 7px;
margin-top: 12px;
}
.source-row,
.filter-row {
display: grid;
grid-template-columns: 86px 1fr;
align-items: center;
gap: 10px;
font-size: 0.78rem;
}
.source-row strong,
.filter-row strong {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
display: inline-block;
width: 12px;
height: 12px;
flex: 0 0 auto;
border: 1px solid rgba(0, 0, 0, 0.15);
border-radius: 4px;
}
.swatch.red { background: var(--red); }
.swatch.green { background: var(--green); }
.swatch.blue { background: var(--blue); }
.swatch.cyan { background: var(--cyan); }
.swatch.magenta { background: var(--magenta); }
.swatch.yellow { background: var(--yellow); }
code {
padding: 0.1em 0.3em;
border-radius: 4px;
color: #253541;
background: #eef0ed;
font-family:
"SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 0.9em;
}
.step-list {
display: grid;
gap: 8px;
margin-top: 12px;
}
.step {
display: grid;
grid-template-columns: 22px 1fr;
gap: 8px;
align-items: start;
font-size: 0.8rem;
line-height: 1.45;
}
.step-number {
display: grid;
place-items: center;
width: 22px;
height: 22px;
border-radius: 50%;
color: white;
background: #34424d;
font-size: 0.69rem;
font-weight: 800;
}
.step span:last-child {
color: #4f5a64;
}
.note {
margin-top: 20px;
padding: 14px 15px;
border-left: 4px solid #e3a91d;
border-radius: 0 10px 10px 0;
color: #5e5538;
background: #fff8df;
font-size: 0.8rem;
line-height: 1.5;
}
.summary {
margin-top: 18px;
padding-top: 16px;
border-top: 1px solid var(--border);
color: #384550;
font-size: 0.86rem;
line-height: 1.55;
}
.summary strong {
color: var(--ink);
}
@media (max-width: 900px) {
body {
overflow: auto;
}
#layout {
grid-template-columns: 1fr;
grid-template-rows: minmax(500px, 66vh) auto;
width: 100%;
height: auto;
min-height: 100vh;
}
aside {
height: auto;
overflow: visible;
border-top: 1px solid var(--border);
border-left: 0;
}
.scene-header {
top: 16px;
left: 16px;
max-width: calc(100% - 32px);
}
.scene-header p:last-child {
max-width: 310px;
}
.scene-tools {
top: auto;
right: 14px;
bottom: 14px;
max-width: calc(100% - 28px);
}
.hint {
display: none;
}
}
@media (max-width: 520px) {
.scene-header h1 {
max-width: 270px;
}
.scene-header p:last-child {
display: none;
}
.scene-tools {
justify-content: flex-start;
}
.tool-button {
padding: 8px 11px;
font-size: 0.76rem;
}
aside {
padding: 26px 18px;
}
}
</style>
</head>
<body>
<main id="layout">
<section id="stage" aria-label="Interactive Three.js color comparison">
<div id="canvas-wrap"></div>
<header class="scene-header">
<p class="eyebrow">Light mixing / filter stacking</p>
<h1>Two ways to combine color</h1>
<p>
Left: overlapping emitters add light. Right: colored filters
subtract selected channels from white light.
</p>
</header>
<div class="scene-tools" aria-label="Scene controls">
<button class="tool-button" id="stackButton" type="button">
Compress stack
</button>
<button class="tool-button" id="beamButton" type="button" aria-pressed="true">
Hide beams
</button>
<button class="tool-button" id="resetButton" type="button">
Reset camera
</button>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</section>
<aside aria-label="Color model explanation">
<p class="eyebrow">Side-by-side comparison</p>
<h2>Addition vs. multiplication</h2>
<p>
These operations look superficially similar, but they act on
different quantities. Additive mixing adds <strong>emitted light
intensity</strong>. Subtractive filtering multiplies the
<strong>fraction of light transmitted</strong> in each channel.
</p>
<section class="model-card">
<h3 class="model-title">
<span class="operation">+</span>
Additive light panel
</h3>
<p>
Red, green, and blue sources overlap. Each overlap adds the
corresponding RGB channel, with values clipped to the range 0–1.
</p>
<div class="source-list">
<div class="source-row">
<strong><span class="swatch red"></span>Red</strong>
<code>R = (1, 0, 0)</code>
</div>
<div class="source-row">
<strong><span class="swatch green"></span>Green</strong>
<code>G = (0, 1, 0)</code>
</div>
<div class="source-row">
<strong><span class="swatch blue"></span>Blue</strong>
<code>B = (0, 0, 1)</code>
</div>
</div>
<div class="equation">
R + G + B<br>
= (1, 0, 0) + (0, 1, 0) + (0, 0, 1)<br>
= (1, 1, 1) → white
</div>
<p>
Pairwise overlaps also appear naturally:
<code>R+G → yellow</code>,
<code>G+B → cyan</code>, and
<code>R+B → magenta</code>.
</p>
</section>
<section class="model-card">
<h3 class="model-title">
<span class="operation">×</span>
Subtractive filter stack
</h3>
<p>
White light enters the stack with
<code>W = (1, 1, 1)</code>. The illustrative values below are
ideal channel transmittances: each tuple lists the fractions
of red, green, and blue light that pass.
</p>
<div class="filter-list">
<div class="filter-row">
<strong><span class="swatch cyan"></span>Cyan</strong>
<code>T<sub>C</sub> = (0%, 100%, 100%)</code>
</div>
<div class="filter-row">
<strong><span class="swatch magenta"></span>Magenta</strong>
<code>T<sub>M</sub> = (100%, 0%, 100%)</code>
</div>
<div class="filter-row">
<strong><span class="swatch yellow"></span>Yellow</strong>
<code>T<sub>Y</sub> = (100%, 100%, 0%)</code>
</div>
</div>
<div class="equation">
T<sub>out</sub> = W × T<sub>C</sub> × T<sub>M</sub> × T<sub>Y</sub><br>
= (1,1,1) × (0,1,1) × (1,0,1) × (1,1,0)<br>
= (0,0,0) → black
</div>
<div class="step-list">
<div class="step">
<span class="step-number">1</span>
<span>After cyan: <code>(1,1,1) × (0,1,1) = (0,1,1)</code></span>
</div>
<div class="step">
<span class="step-number">2</span>
<span>After magenta: <code>(0,1,1) × (1,0,1) = (0,0,1)</code></span>
</div>
<div class="step">
<span class="step-number">3</span>
<span>After yellow: <code>(0,0,1) × (1,1,0) = (0,0,0)</code></span>
</div>
</div>
</section>
<div class="note">
<strong>Illustrative idealization:</strong> real colored filters leak
some unwanted wavelengths, so a CMY stack usually produces a very
dark gray or brown rather than perfect black.
</div>
<div class="summary">
<strong>Key distinction:</strong> the additive panel creates color by
adding emitted photons. The filter stack does not add light; each
filter passes a fraction of the light reaching it.
</div>
</aside>
</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 mount = document.getElementById("canvas-wrap");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf4f1e8);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
camera.position.set(0.8, 5.4, 11.8);
const renderer = new THREE.WebGLRenderer({
antialias: true,
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;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional comparison of additive light and subtractive filters"
);
renderer.domElement.tabIndex = 0;
mount.appendChild(renderer.domElement);
const ambient = new THREE.HemisphereLight(
0xffffff,
0x83909a,
2.25
);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xfff7e8, 3.1);
keyLight.position.set(-5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xbfd9ff, 1.25);
rimLight.position.set(7, 5, -5);
scene.add(rimLight);
const materials = {
floor: new THREE.MeshStandardMaterial({
color: 0xe9e5da,
roughness: 0.94,
metalness: 0
}),
wall: new THREE.MeshStandardMaterial({
color: 0xf2efe6,
roughness: 1,
metalness: 0
}),
frame: new THREE.MeshStandardMaterial({
color: 0x1c2832,
roughness: 0.5,
metalness: 0.08
}),
stand: new THREE.MeshStandardMaterial({
color: 0x394753,
roughness: 0.58,
metalness: 0.12
}),
screen: new THREE.MeshStandardMaterial({
color: 0x050a11,
roughness: 0.88,
metalness: 0
}),
base: new THREE.MeshStandardMaterial({
color: 0x2e3b46,
roughness: 0.6,
metalness: 0.12
}),
metal: new THREE.MeshStandardMaterial({
color: 0x9aa6ae,
roughness: 0.35,
metalness: 0.7
})
};
function mesh(geometry, material, position, parent = scene) {
const object = new THREE.Mesh(geometry, material);
if (position) object.position.set(...position);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
// Room and floor.
mesh(
new THREE.PlaneGeometry(30, 30),
materials.floor,
[0, -0.01, 0]
).rotation.x = -Math.PI / 2;
mesh(
new THREE.BoxGeometry(22, 9, 0.22),
materials.wall,
[0, 4.1, -2.65]
);
/*
* Procedural labels are generated with a canvas and used as sprites.
* No external fonts, image files, or Three.js addons are required.
*/
function roundedRect(ctx, x, y, w, h, radius) {
const r = Math.min(radius, w / 2, h / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeLabel(
text,
{
foreground = "#ffffff",
background = "rgba(17,28,37,0.86)",
width = 2.2,
fontSize = 48,
border = null
} = {}
) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 160;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (background) {
roundedRect(ctx, 8, 8, 752, 144, 28);
ctx.fillStyle = background;
ctx.fill();
if (border) {
ctx.lineWidth = 4;
ctx.strokeStyle = border;
ctx.stroke();
}
}
ctx.fillStyle = foreground;
ctx.font = `800 ${fontSize}px system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 384, 83, 710);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
sprite.scale.set(width, width * (160 / 768), 1);
sprite.renderOrder = 20;
return sprite;
}
// ---------------------------------------------------------------------
// LEFT: additive light panel
// ---------------------------------------------------------------------
const additiveGroup = new THREE.Group();
additiveGroup.position.set(-3.15, 2.72, 0);
scene.add(additiveGroup);
mesh(
new THREE.BoxGeometry(4.35, 3.58, 0.32),
materials.frame,
[0, 0, 0],
additiveGroup
);
mesh(
new THREE.BoxGeometry(4.02, 3.27, 0.16),
materials.screen,
[0, 0, 0.22],
additiveGroup
);
mesh(
new THREE.BoxGeometry(0.48, 1.08, 0.38),
materials.stand,
[0, -2.02, -0.02],
additiveGroup
);
mesh(
new THREE.BoxGeometry(2.25, 0.14, 0.95),
materials.frame,
[0, -2.48, 0],
additiveGroup
);
const additiveSources = [
{ x: -0.9, color: 0xff0000, label: "R" },
{ x: 0.0, color: 0x00ff00, label: "G" },
{ x: 0.9, color: 0x0000ff, label: "B" }
];
additiveSources.forEach((source, index) => {
const material = new THREE.MeshBasicMaterial({
color: source.color,
transparent: true,
opacity: 1,
blending: THREE.AdditiveBlending,
depthWrite: false,
toneMapped: false,
side: THREE.DoubleSide
});
const circle = mesh(
new THREE.CircleGeometry(1.02, 96),
material,
[source.x, 0.02, 0.36],
additiveGroup
);
circle.castShadow = false;
circle.receiveShadow = false;
circle.renderOrder = 3 + index;
const label = makeLabel(source.label, {
foreground: source.label === "R" ? "#430000" :
source.label === "G" ? "#003a12" : "#ffffff",
background: `#${source.color.toString(16).padStart(6, "0")}`,
width: 0.42,
fontSize: 62
});
label.position.set(source.x, -1.34, 0.46);
additiveGroup.add(label);
});
const additiveHeading = makeLabel("ADDITIVE • EMISSION", {
foreground: "#ffffff",
background: "rgba(18,28,37,0.90)",
border: "rgba(255,255,255,0.16)",
width: 3.45,
fontSize: 49
});
additiveHeading.position.set(0, 2.18, 0.35);
additiveGroup.add(additiveHeading);
const additiveSubheading = makeLabel("OVERLAP = ADD", {
foreground: "#b9c8d2",
background: "rgba(18,28,37,0.72)",
width: 2.25,
fontSize: 38
});
additiveSubheading.position.set(0, 1.73, 0.35);
additiveGroup.add(additiveSubheading);
// ---------------------------------------------------------------------
// RIGHT: subtractive filters under white light
// ---------------------------------------------------------------------
const subtractiveGroup = new THREE.Group();
subtractiveGroup.position.set(3.05, 0, 0);
scene.add(subtractiveGroup);
mesh(
new THREE.CylinderGeometry(2.18, 2.32, 0.26, 64),
materials.base,
[0, 0.13, 0],
subtractiveGroup
);
mesh(
new THREE.CylinderGeometry(0.06, 0.06, 3.92, 24),
materials.metal,
[0, 2.65, -1.28],
subtractiveGroup
);
const lampHousing = mesh(
new THREE.CylinderGeometry(0.42, 0.58, 0.48, 40),
materials.frame,
[0, 4.52, 0],
subtractiveGroup
);
const bulbMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: 0xffffff,
emissiveIntensity: 3.2,
roughness: 0.25,
metalness: 0
});
mesh(
new THREE.SphereGeometry(0.27, 32, 24),
bulbMaterial,
[0, 4.15, 0],
subtractiveGroup
);
const lampLight = new THREE.PointLight(0xffffff, 10, 7, 2);
lampLight.position.set(0, 4.08, 0);
subtractiveGroup.add(lampLight);
const filterDefinitions = [
{
name: "CYAN",
color: 0x00ffff,
output: "(0,1,1)"
},
{
name: "MAGENTA",
color: 0xff00aa,
output: "(0,0,1)"
},
{
name: "YELLOW",
color: 0xffdf00,
output: "(0,0,0)"
}
];
const filters = filterDefinitions.map((definition) => {
const material = new THREE.MeshStandardMaterial({
color: definition.color,
transparent: true,
opacity: 0.48,
roughness: 0.3,
metalness: 0.02,
depthWrite: false,
side: THREE.DoubleSide
});
const pane = mesh(
new THREE.BoxGeometry(2.88, 0.12, 1.9),
material,
[0, 3.38, 0],
subtractiveGroup
);
pane.castShadow = false;
pane.renderOrder = 6;
const edgeMaterial = new THREE.LineBasicMaterial({
color: definition.color,
transparent: true,
opacity: 0.75
});
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(pane.geometry),
edgeMaterial
);
edge.position.copy(pane.position);
edge.renderOrder = 7;
subtractiveGroup.add(edge);
const nameLabel = makeLabel(definition.name, {
foreground: "#ffffff",
background: "rgba(21,31,39,0.88)",
border: `#${definition.color.toString(16).padStart(6, "0")}`,
width: 1.55,
fontSize: 42
});
nameLabel.position.set(-1.92, 0, 0.1);
subtractiveGroup.add(nameLabel);
const outputLabel = makeLabel(`→ ${definition.output}`, {
foreground:
definition.output === "(0,0,0)" ? "#d4d9dd" :
definition.output === "(0,0,1)" ? "#b9d5ff" :
"#b9fffb",
background: "rgba(21,31,39,0.82)",
width: 1.68,
fontSize: 40
});
outputLabel.position.set(1.95, 0, 0.1);
subtractiveGroup.add(outputLabel);
const arm = mesh(
new THREE.BoxGeometry(0.08, 0.08, 1.12),
materials.metal,
[0, 3.38, -0.72],
subtractiveGroup
);
return {
...definition,
pane,
edge,
nameLabel,
outputLabel,
arm
};
});
const subtractiveHeading = makeLabel("SUBTRACTIVE • TRANSMISSION", {
foreground: "#ffffff",
background: "rgba(18,28,37,0.90)",
border: "rgba(255,255,255,0.16)",
width: 3.7,
fontSize: 47
});
subtractiveHeading.position.set(0, 5.13, 0);
subtractiveGroup.add(subtractiveHeading);
const whiteInputLabel = makeLabel("WHITE IN (1,1,1)", {
foreground: "#ffffff",
background: "rgba(18,28,37,0.82)",
width: 1.92,
fontSize: 38
});
whiteInputLabel.position.set(1.9, 4.18, 0.1);
subtractiveGroup.add(whiteInputLabel);
const outputLabel = makeLabel("OUTPUT (0,0,0)", {
foreground: "#e4e8eb",
background: "rgba(18,28,37,0.9)",
border: "rgba(255,255,255,0.18)",
width: 2.0,
fontSize: 40
});
outputLabel.position.set(1.95, 0.48, 0.1);
subtractiveGroup.add(outputLabel);
// Beam segments are separate so their displayed colors match the
// channel-by-channel mathematical result.
function makeUnitBeam(color, opacity, openEnded = true) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false,
side: THREE.DoubleSide,
toneMapped: false
});
const object = new THREE.Mesh(
new THREE.CylinderGeometry(1, 1, 1, 48, 1, openEnded),
material
);
object.renderOrder = 2;
subtractiveGroup.add(object);
return object;
}
const whiteBeamMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.25,
depthWrite: false,
side: THREE.DoubleSide,
toneMapped: false
});
const whiteBeam = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.6, 1, 48, 1, true),
whiteBeamMaterial
);
whiteBeam.renderOrder = 2;
subtractiveGroup.add(whiteBeam);
const cyanBeam = makeUnitBeam(0x00ffff, 0.34);
const blueBeam = makeUnitBeam(0x0000ff, 0.48);
const blackBeam = makeUnitBeam(0x030507, 0.9, false);
const beams = [whiteBeam, cyanBeam, blueBeam, blackBeam];
let spacing = 0.78;
let targetSpacing = 0.78;
let stackCompressed = false;
function setVerticalSegment(object, top, bottom, radius = 0.6) {
const length = Math.max(0.01, top - bottom);
object.position.set(0, (top + bottom) / 2, 0);
object.scale.set(radius, length, radius);
}
function updateFilterStack() {
const step = 0.45 + spacing;
const cyanY = 3.38;
const magentaY = cyanY - step;
const yellowY = magentaY - step;
const positions = [cyanY, magentaY, yellowY];
filters.forEach((filter, index) => {
const y = positions[index];
filter.pane.position.y = y;
filter.edge.position.y = y;
filter.arm.position.y = y;
filter.nameLabel.position.y = y + 0.05;
filter.outputLabel.position.y = y - step * 0.5;
});
// White incident beam.
whiteBeam.position.set(0, (4.03 + cyanY) / 2, 0);
whiteBeam.scale.set(1, 4.03 - cyanY, 1);
// Cyan, then blue, then ideal black.
setVerticalSegment(cyanBeam, cyanY, magentaY, 0.6);
setVerticalSegment(blueBeam, magentaY, yellowY, 0.6);
setVerticalSegment(blackBeam, yellowY, 0.42, 0.6);
outputLabel.position.y = Math.max(0.48, yellowY * 0.45);
}
updateFilterStack();
// Decorative small colored markers on the base.
const baseMarkers = [
{ color: 0x00ffff, x: -1.4 },
{ color: 0xff00aa, x: 0 },
{ color: 0xffdf00, x: 1.4 }
];
baseMarkers.forEach((marker) => {
const material = new THREE.MeshStandardMaterial({
color: marker.color,
emissive: marker.color,
emissiveIntensity: 0.12,
roughness: 0.45
});
mesh(
new THREE.BoxGeometry(0.72, 0.06, 0.38),
material,
[marker.x, 0.29, 1.55],
subtractiveGroup
);
});
// ---------------------------------------------------------------------
// Direct camera controls: orbit, zoom, keyboard, and reset
// ---------------------------------------------------------------------
const cameraState = {
theta: 0.055,
phi: 1.31,
distance: 12.2,
target: new THREE.Vector3(0, 2.35, 0)
};
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function resetCamera() {
cameraState.theta = 0.055;
cameraState.phi = 1.31;
cameraState.distance = 12.2;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.theta -= dx * 0.0055;
cameraState.phi -= dy * 0.0045;
cameraState.theta = THREE.MathUtils.clamp(
cameraState.theta,
-1.05,
1.05
);
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi,
0.78,
1.48
);
});
function endDrag(event) {
dragging = false;
renderer.domElement.style.cursor = "grab";
if (
event.pointerType !== "mouse" &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.style.cursor = "grab";
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.009,
8.0,
18.0
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
document.addEventListener("keydown", (event) => {
if (["INPUT", "TEXTAREA", "SELECT"].includes(document.activeElement.tagName)) {
return;
}
if (event.key === "ArrowLeft") cameraState.theta += 0.08;
if (event.key === "ArrowRight") cameraState.theta -= 0.08;
if (event.key === "ArrowUp") cameraState.phi -= 0.06;
if (event.key === "ArrowDown") cameraState.phi += 0.06;
if (event.key === "+" || event.key === "=") {
cameraState.distance = Math.max(8, cameraState.distance - 0.6);
}
if (event.key === "-" || event.key === "_") {
cameraState.distance = Math.min(18, cameraState.distance + 0.6);
}
if (event.key === "Home") resetCamera();
});
const stackButton = document.getElementById("stackButton");
const beamButton = document.getElementById("beamButton");
const resetButton = document.getElementById("resetButton");
stackButton.addEventListener("click", () => {
stackCompressed = !stackCompressed;
targetSpacing = stackCompressed ? 0.18 : 0.78;
stackButton.textContent = stackCompressed
? "Explode stack"
: "Compress stack";
});
let beamsVisible = true;
beamButton.addEventListener("click", () => {
beamsVisible = !beamsVisible;
beams.forEach((beam) => {
beam.visible = beamsVisible;
});
beamButton.textContent = beamsVisible ? "Hide beams" : "Show beams";
beamButton.setAttribute("aria-pressed", String(beamsVisible));
});
resetButton.addEventListener("click", resetCamera);
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
new ResizeObserver(resize).observe(stage);
resize();
function animate() {
requestAnimationFrame(animate);
spacing += (targetSpacing - spacing) * 0.09;
updateFilterStack();
// Widen the camera on narrow displays so both models remain visible.
const fitScale = Math.max(1, 1.12 / camera.aspect);
const distance = cameraState.distance * fitScale;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + distance * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + distance * Math.cos(cameraState.phi),
cameraState.target.z + distance * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```