Prompt specification
Build or customize the Three.js 3D scene "Rotating bead symmetry window" 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: Rotating bead symmetry window
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/rotating-bead-symmetry-window-x0346
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: rotating bead symmetry window. Create a stylized kaleidoscope view where rotating a small bead arrangement updates repeated reflected sectors; display the source arrangement beside the symmetric result for comparison. Return one self-contained HTML file.
Working Three.js Starter (rotating-bead-symmetry-window-x0346):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#080b16" />
<title>Voxel Bead — Symmetry Window</title>
<style>
:root {
color-scheme: dark;
--bg: #080b16;
--panel: rgba(17, 23, 40, 0.76);
--panel-border: rgba(157, 176, 219, 0.16);
--text: #f2f5ff;
--muted: #8f9bb8;
--cyan: #58e2e7;
--violet: #a887ff;
--coral: #ff7185;
--amber: #ffc857;
--mint: #6be3b2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
}
body {
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
background:
radial-gradient(circle at 21% 25%, rgba(55, 94, 151, 0.2), transparent 34%),
radial-gradient(circle at 78% 36%, rgba(120, 72, 154, 0.18), transparent 33%),
linear-gradient(180deg, #090c18 0%, #070a13 100%);
}
body::before {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
opacity: 0.16;
background-image:
linear-gradient(rgba(255, 255, 255, 0.018) 1px, transparent 1px),
linear-gradient(90deg, rgba(255, 255, 255, 0.018) 1px, transparent 1px);
background-size: 42px 42px;
mask-image: radial-gradient(circle at center, black, transparent 82%);
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.topbar {
position: fixed;
z-index: 20;
top: 0;
left: 0;
right: 0;
height: 78px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 28px;
border-bottom: 1px solid rgba(151, 168, 207, 0.1);
background: rgba(8, 11, 22, 0.64);
backdrop-filter: blur(18px);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 36px;
height: 36px;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 3px;
padding: 5px;
border: 1px solid rgba(88, 226, 231, 0.25);
border-radius: 9px;
background: rgba(88, 226, 231, 0.06);
box-shadow: 0 0 26px rgba(88, 226, 231, 0.08);
}
.brand-mark i {
display: block;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 9px rgba(88, 226, 231, 0.35);
}
.brand-mark i:nth-child(2),
.brand-mark i:nth-child(6) {
opacity: 0.55;
}
.brand-mark i:nth-child(3),
.brand-mark i:nth-child(7) {
background: var(--violet);
box-shadow: 0 0 9px rgba(168, 135, 255, 0.35);
}
.brand-mark i:nth-child(5),
.brand-mark i:nth-child(8) {
opacity: 0.26;
}
.brand-kicker {
margin: 0 0 3px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
font-size: clamp(15px, 2vw, 19px);
line-height: 1;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.brand h1 span {
color: var(--muted);
font-weight: 500;
}
.header-note {
display: flex;
align-items: center;
gap: 18px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.live-status {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 11px;
border: 1px solid rgba(107, 227, 178, 0.18);
border-radius: 999px;
color: #bdf7df;
background: rgba(107, 227, 178, 0.06);
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 11px rgba(107, 227, 178, 0.85);
animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse {
50% {
opacity: 0.45;
transform: scale(0.8);
}
}
#stage {
position: fixed;
z-index: 1;
top: 78px;
right: 0;
bottom: 158px;
left: 0;
overflow: hidden;
isolation: isolate;
}
#stage canvas {
position: absolute;
z-index: 0;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
}
#stage canvas.dragging {
cursor: grabbing;
}
.scene-label {
position: absolute;
z-index: 3;
top: 0;
left: 0;
min-width: 138px;
padding: 8px 10px;
pointer-events: none;
border: 1px solid var(--label-border);
border-radius: 9px;
background: rgba(10, 14, 27, 0.72);
box-shadow: 0 10px 35px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(10px);
transform: translate3d(-100px, -100px, 0);
will-change: transform;
}
.scene-label::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 18px;
background: linear-gradient(var(--label-color), transparent);
opacity: 0.7;
}
#source-label {
--label-color: var(--cyan);
--label-border: rgba(88, 226, 231, 0.2);
}
#result-label {
--label-color: var(--violet);
--label-border: rgba(168, 135, 255, 0.22);
}
.label-kicker {
display: flex;
align-items: center;
gap: 7px;
margin-bottom: 4px;
color: var(--label-color);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.label-kicker::before {
content: "";
width: 5px;
height: 5px;
border-radius: 1px;
background: currentColor;
box-shadow: 0 0 8px currentColor;
}
.label-title {
color: #e7ebf8;
font-size: 12px;
font-weight: 650;
letter-spacing: 0.025em;
}
.label-sub {
margin-top: 3px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#fallback {
position: absolute;
z-index: 8;
top: 50%;
left: 50%;
width: min(420px, calc(100% - 40px));
padding: 24px;
transform: translate(-50%, -50%);
border: 1px solid rgba(255, 113, 133, 0.25);
border-radius: 14px;
color: var(--muted);
text-align: center;
background: rgba(17, 23, 40, 0.9);
}
#fallback[hidden] {
display: none;
}
.controls {
position: fixed;
z-index: 20;
right: 0;
bottom: 18px;
left: 0;
width: min(980px, calc(100vw - 32px));
margin: auto;
padding: 15px 17px 11px;
border: 1px solid var(--panel-border);
border-radius: 15px;
background:
linear-gradient(135deg, rgba(255, 255, 255, 0.025), transparent 42%),
var(--panel);
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.35),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(22px);
}
.control-main {
display: grid;
grid-template-columns: auto minmax(220px, 1fr) auto auto auto;
align-items: center;
gap: 14px;
}
.button {
height: 42px;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 9px;
padding: 0 15px;
border: 1px solid rgba(153, 171, 214, 0.18);
border-radius: 9px;
cursor: pointer;
color: #dce2f3;
background: rgba(255, 255, 255, 0.035);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.055em;
text-transform: uppercase;
transition:
transform 150ms ease,
border-color 150ms ease,
background 150ms ease;
}
.button:hover {
transform: translateY(-1px);
border-color: rgba(153, 171, 214, 0.34);
background: rgba(255, 255, 255, 0.065);
}
.button:active {
transform: translateY(0);
}
.button.primary {
min-width: 128px;
border-color: rgba(88, 226, 231, 0.3);
color: #071318;
background: var(--cyan);
box-shadow: 0 7px 24px rgba(88, 226, 231, 0.15);
}
.button.primary:hover {
background: #7cecef;
}
.play-symbol {
width: 16px;
font-size: 13px;
line-height: 1;
text-align: center;
}
.angle-block {
min-width: 0;
}
.angle-top {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
}
.angle-top label,
.control-caption {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#angle-value {
min-width: 42px;
color: var(--amber);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 12px;
font-weight: 700;
text-align: right;
}
input[type="range"] {
--fill: 5%;
width: 100%;
height: 5px;
display: block;
margin: 0;
appearance: none;
border-radius: 999px;
outline: none;
cursor: ew-resize;
background: linear-gradient(
to right,
var(--amber) 0%,
var(--amber) var(--fill),
rgba(139, 155, 192, 0.17) var(--fill),
rgba(139, 155, 192, 0.17) 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
appearance: none;
border: 3px solid #101522;
border-radius: 4px;
background: var(--amber);
box-shadow: 0 0 0 1px rgba(255, 200, 87, 0.3), 0 0 14px rgba(255, 200, 87, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #101522;
border-radius: 4px;
background: var(--amber);
box-shadow: 0 0 0 1px rgba(255, 200, 87, 0.3), 0 0 14px rgba(255, 200, 87, 0.25);
}
.sector-block {
display: flex;
align-items: center;
gap: 9px;
}
.segmented {
display: flex;
padding: 3px;
border: 1px solid rgba(153, 171, 214, 0.14);
border-radius: 9px;
background: rgba(0, 0, 0, 0.16);
}
.segmented button {
width: 30px;
height: 30px;
border: 0;
border-radius: 6px;
cursor: pointer;
color: var(--muted);
background: transparent;
font-size: 10px;
font-weight: 800;
transition: 150ms ease;
}
.segmented button:hover {
color: white;
}
.segmented button.active {
color: #0c1420;
background: var(--violet);
box-shadow: 0 3px 14px rgba(168, 135, 255, 0.22);
}
.icon-button {
width: 42px;
padding: 0;
font-size: 19px;
}
.control-tip {
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
margin-top: 10px;
color: rgba(143, 155, 184, 0.65);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.control-tip i {
width: 3px;
height: 3px;
border-radius: 50%;
background: rgba(143, 155, 184, 0.45);
}
@media (max-width: 820px) {
.topbar {
height: 68px;
padding: 0 15px;
}
.brand-kicker,
.header-note > span:not(.live-status) {
display: none;
}
.live-status {
padding: 7px 9px;
font-size: 9px;
}
#stage {
top: 68px;
bottom: 192px;
}
.controls {
bottom: 10px;
width: calc(100vw - 20px);
padding: 12px;
}
.control-main {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.angle-block {
width: 100%;
order: -1;
}
.button.primary {
min-width: 0;
flex: 1;
}
.sector-block {
flex: 1;
justify-content: space-between;
}
.control-tip {
display: none;
}
.label-sub {
display: none;
}
.scene-label {
min-width: 112px;
padding: 6px 8px;
}
.label-title {
font-size: 10px;
}
}
@media (max-width: 460px) {
.brand h1 {
font-size: 13px;
}
.brand-mark {
width: 32px;
height: 32px;
}
.live-status {
max-width: 112px;
overflow: hidden;
white-space: nowrap;
}
.sector-block .control-caption {
display: none;
}
.button {
padding: 0 11px;
font-size: 10px;
}
}
@media (prefers-reduced-motion: reduce) {
.status-dot {
animation: none;
}
.button {
transition: none;
}
}
</style>
</head>
<body>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i>
<i></i><i></i><i></i>
<i></i><i></i><i></i>
</div>
<div>
<p class="brand-kicker">Interactive voxel study</p>
<h1>Voxel Bead <span>/ Symmetry Window</span></h1>
</div>
</div>
<div class="header-note">
<span>Rotate one motif. Watch every mirror respond.</span>
<div class="live-status">
<span class="status-dot"></span>
<span id="mirror-mode">6-sector mirror</span>
</div>
</div>
</header>
<main id="stage" aria-label="Interactive 3D bead symmetry comparison">
<div id="fallback" hidden>
This scene needs WebGL. Try enabling hardware acceleration or opening it in a modern browser.
</div>
<div class="scene-label" id="source-label">
<div class="label-kicker">Source</div>
<div class="label-title">3 × 3 bead matrix</div>
<div class="label-sub">The rotating window</div>
</div>
<div class="scene-label" id="result-label">
<div class="label-kicker">Symmetric result</div>
<div class="label-title" id="result-title">6 reflected sectors</div>
<div class="label-sub">Live dihedral mirror</div>
</div>
</main>
<section class="controls" aria-label="Symmetry controls">
<div class="control-main">
<button class="button primary" id="auto" type="button" aria-pressed="true">
<span class="play-symbol" id="auto-symbol" aria-hidden="true">Ⅱ</span>
<span id="auto-label">Pause</span>
</button>
<div class="angle-block">
<div class="angle-top">
<label for="angle">Bead rotation</label>
<output id="angle-value" for="angle">018°</output>
</div>
<input
id="angle"
type="range"
min="0"
max="359.9"
step="0.1"
value="18"
aria-label="Source bead rotation angle"
/>
</div>
<div class="sector-block">
<span class="control-caption">Sectors</span>
<div class="segmented" role="group" aria-label="Number of reflected sectors">
<button type="button" data-sectors="4" aria-pressed="false">4</button>
<button type="button" data-sectors="6" class="active" aria-pressed="true">6</button>
<button type="button" data-sectors="8" aria-pressed="false">8</button>
</div>
</div>
<button class="button" id="shuffle" type="button" title="Shuffle bead colors">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M3 7h3c4.5 0 5.5 10 10 10h5M17 13l4 4-4 4M3 17h3c1.8 0 2.9-1.5 4-3.3M14.5 8.3C15.5 7.4 16.2 7 17 7h4M17 3l4 4-4 4"
stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
Shuffle
</button>
<button class="button icon-button" id="reset-view" type="button" title="Reset camera view" aria-label="Reset camera view">
↺
</button>
</div>
<div class="control-tip">
<span>Drag to orbit</span><i></i>
<span>Scroll to zoom</span><i></i>
<span>← → to nudge</span><i></i>
<span>Space to pause</span>
</div>
</section>
<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 fallback = document.getElementById("fallback");
const angleInput = document.getElementById("angle");
const angleValue = document.getElementById("angle-value");
const autoButton = document.getElementById("auto");
const autoLabel = document.getElementById("auto-label");
const autoSymbol = document.getElementById("auto-symbol");
const shuffleButton = document.getElementById("shuffle");
const resetViewButton = document.getElementById("reset-view");
const sectorButtons = [...document.querySelectorAll("[data-sectors]")];
const mirrorMode = document.getElementById("mirror-mode");
const resultTitle = document.getElementById("result-title");
const sourceLabel = document.getElementById("source-label");
const resultLabel = document.getElementById("result-label");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
} catch (error) {
fallback.hidden = false;
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setClearColor(0x000000, 0);
renderer.domElement.setAttribute("aria-label", "Three-dimensional voxel kaleidoscope");
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x080b16, 0.014);
const camera = new THREE.PerspectiveCamera(36, 1, 0.1, 120);
const cameraTarget = new THREE.Vector3(0, 0.15, 0);
const orbit = {
azimuth: 0.035,
elevation: 0.075,
zoom: 1,
baseRadius: 14
};
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let angle = 18;
let sectorCount = 6;
let autoRotate = !reducedMotion;
let baseRadius = 14;
let isDragging = false;
let pointerX = 0;
let pointerY = 0;
const palette = [
0xffc857, // amber
0x4dd9e8, // cyan
0xff6b7a, // coral
0x9d7cff, // violet
0x6be3b2 // mint
];
let pattern = [
0, 1, 2,
3, 4, 0,
2, 3, 1
];
const paletteMaterials = palette.map((hex) => {
const color = new THREE.Color(hex);
return new THREE.MeshStandardMaterial({
color,
roughness: 0.3,
metalness: 0.12,
emissive: color.clone().multiplyScalar(0.12),
emissiveIntensity: 0.7
});
});
const beadEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x050914,
transparent: true,
opacity: 0.78
});
const highlightMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.48,
depthWrite: false
});
const sourceBeadGeometry = new THREE.BoxGeometry(0.4, 0.4, 0.28);
const resultBeadGeometry = new THREE.BoxGeometry(0.3, 0.3, 0.23);
const sourceEdgeGeometry = new THREE.EdgesGeometry(sourceBeadGeometry);
const resultEdgeGeometry = new THREE.EdgesGeometry(resultBeadGeometry);
const sourceHighlightGeometry = new THREE.PlaneGeometry(0.085, 0.035);
const resultHighlightGeometry = new THREE.PlaneGeometry(0.065, 0.027);
function makeBead(colorIndex, geometry, edgeGeometry, highlightGeometry) {
const group = new THREE.Group();
const cube = new THREE.Mesh(geometry, paletteMaterials[colorIndex]);
const edges = new THREE.LineSegments(edgeGeometry, beadEdgeMaterial);
const highlight = new THREE.Mesh(highlightGeometry, highlightMaterial);
const size = geometry.parameters.width;
highlight.position.set(
-size * 0.18,
size * 0.23,
geometry.parameters.depth * 0.5 + 0.002
);
cube.add(edges);
group.add(cube, highlight);
return { group, cube };
}
function setBeadColor(bead, colorIndex) {
bead.cube.material = paletteMaterials[colorIndex];
}
// Lighting
scene.add(new THREE.AmbientLight(0x91a7dc, 1.2));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.4);
keyLight.position.set(-3, 7, 11);
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x4dd9e8, 22, 18, 2);
cyanLight.position.set(-6, 2, 6);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0x9d7cff, 24, 18, 2);
violetLight.position.set(6, -1, 7);
scene.add(violetLight);
// Subtle background grid
const gridPositions = [];
for (let x = -12; x <= 12; x += 1) {
gridPositions.push(x, -6, -4.2, x, 6, -4.2);
}
for (let y = -6; y <= 6; y += 1) {
gridPositions.push(-12, y, -4.2, 12, y, -4.2);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x33415d,
transparent: true,
opacity: 0.19
})
);
scene.add(grid);
// Background voxel dust
const dustGeometry = new THREE.BoxGeometry(0.045, 0.045, 0.045);
const dustMaterial = new THREE.MeshBasicMaterial({
color: 0x7587b3,
transparent: true,
opacity: 0.28
});
const dust = new THREE.InstancedMesh(dustGeometry, dustMaterial, 85);
const dustDummy = new THREE.Object3D();
let randomSeed = 9271;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
for (let i = 0; i < 85; i++) {
dustDummy.position.set(
(random() - 0.5) * 20,
(random() - 0.5) * 11,
-2.8 + random() * 1.6
);
const scale = 0.45 + random() * 1.35;
dustDummy.scale.setScalar(scale);
dustDummy.rotation.set(random() * 0.7, random() * 0.7, random() * Math.PI);
dustDummy.updateMatrix();
dust.setMatrixAt(i, dustDummy.matrix);
}
dust.instanceMatrix.needsUpdate = true;
scene.add(dust);
// Divider between the two studies
const dividerGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.2, -2.8, -0.4),
new THREE.Vector3(-0.2, 2.8, -0.4)
]);
const divider = new THREE.Line(
dividerGeometry,
new THREE.LineDashedMaterial({
color: 0x53617e,
transparent: true,
opacity: 0.35,
dashSize: 0.12,
gapSize: 0.16
})
);
divider.computeLineDistances();
scene.add(divider);
// Source window
const sourceRoot = new THREE.Group();
sourceRoot.position.set(-4.15, -0.08, 0);
scene.add(sourceRoot);
const panelMaterial = new THREE.MeshStandardMaterial({
color: 0x11182a,
roughness: 0.68,
metalness: 0.18,
transparent: true,
opacity: 0.94
});
const sourcePlate = new THREE.Mesh(
new THREE.BoxGeometry(3.8, 3.8, 0.13),
panelMaterial
);
sourcePlate.position.z = -0.26;
sourceRoot.add(sourcePlate);
const sourcePlateEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(sourcePlate.geometry),
new THREE.LineBasicMaterial({
color: 0x4b5d80,
transparent: true,
opacity: 0.38
})
);
sourcePlateEdges.position.copy(sourcePlate.position);
sourceRoot.add(sourcePlateEdges);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x3d4d6d,
roughness: 0.4,
metalness: 0.35,
emissive: 0x111a2c,
emissiveIntensity: 0.7
});
function addFrameBar(width, height, x, y) {
const bar = new THREE.Mesh(
new THREE.BoxGeometry(width, height, 0.16),
frameMaterial
);
bar.position.set(x, y, 0.02);
sourceRoot.add(bar);
}
addFrameBar(3.96, 0.075, 0, 1.92);
addFrameBar(3.96, 0.075, 0, -1.92);
addFrameBar(0.075, 3.84, -1.92, 0);
addFrameBar(0.075, 3.84, 1.92, 0);
const cornerMaterial = new THREE.MeshStandardMaterial({
color: 0x4dd9e8,
emissive: 0x17474c,
emissiveIntensity: 1.4
});
for (const x of [-1.9, 1.9]) {
for (const y of [-1.9, 1.9]) {
const corner = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.16, 0.2),
cornerMaterial
);
corner.position.set(x, y, 0.12);
sourceRoot.add(corner);
}
}
const sourceGridPositions = [];
for (const coordinate of [-0.68, 0, 0.68]) {
sourceGridPositions.push(
coordinate, -1.45, 0.02,
coordinate, 1.45, 0.02,
-1.45, coordinate, 0.02,
1.45, coordinate, 0.02
);
}
const sourceGridGeometry = new THREE.BufferGeometry();
sourceGridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(sourceGridPositions, 3)
);
sourceRoot.add(
new THREE.LineSegments(
sourceGridGeometry,
new THREE.LineBasicMaterial({
color: 0x52627f,
transparent: true,
opacity: 0.22
})
)
);
const sourceMotif = new THREE.Group();
sourceMotif.position.z = 0.24;
sourceRoot.add(sourceMotif);
const sourceBeads = [];
for (let row = 0; row < 3; row++) {
for (let column = 0; column < 3; column++) {
const index = row * 3 + column;
const bead = makeBead(
pattern[index],
sourceBeadGeometry,
sourceEdgeGeometry,
sourceHighlightGeometry
);
bead.group.position.set(
(column - 1) * 0.68,
(1 - row) * 0.68,
0
);
sourceMotif.add(bead.group);
sourceBeads.push(bead);
}
}
// Kaleidoscope result
const resultRoot = new THREE.Group();
resultRoot.position.set(3.55, 0, 0);
scene.add(resultRoot);
const resultDisk = new THREE.Mesh(
new THREE.CircleGeometry(3.28, 96),
new THREE.MeshBasicMaterial({
color: 0x18243a,
transparent: true,
opacity: 0.56,
depthWrite: false
})
);
resultDisk.position.z = -0.45;
resultRoot.add(resultDisk);
const outerRing = new THREE.Mesh(
new THREE.TorusGeometry(3.28, 0.026, 8, 144),
new THREE.MeshBasicMaterial({
color: 0x65779c,
transparent: true,
opacity: 0.58
})
);
outerRing.position.z = -0.24;
resultRoot.add(outerRing);
const innerRing = new THREE.Mesh(
new THREE.TorusGeometry(0.72, 0.025, 8, 96),
new THREE.MeshBasicMaterial({
color: 0xa887ff,
transparent: true,
opacity: 0.55
})
);
innerRing.position.z = -0.2;
resultRoot.add(innerRing);
const dashedRingPoints = [];
for (let i = 0; i <= 180; i++) {
const a = (i / 180) * Math.PI * 2;
dashedRingPoints.push(
new THREE.Vector3(Math.cos(a) * 3.06, Math.sin(a) * 3.06, -0.2)
);
}
const dashedRing = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(dashedRingPoints),
new THREE.LineDashedMaterial({
color: 0x4c5b7c,
transparent: true,
opacity: 0.55,
dashSize: 0.11,
gapSize: 0.1
})
);
dashedRing.computeLineDistances();
resultRoot.add(dashedRing);
const guideRoot = new THREE.Group();
resultRoot.add(guideRoot);
const markerGeometry = new THREE.BoxGeometry(0.065, 0.19, 0.075);
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xffc857,
emissive: 0x5b4014,
emissiveIntensity: 1.1,
roughness: 0.35
});
let markerMesh = null;
const resultBeadRoot = new THREE.Group();
resultRoot.add(resultBeadRoot);
let resultBeads = [];
function buildResultBeads() {
resultBeadRoot.clear();
resultBeads = [];
for (let sector = 0; sector < sectorCount; sector++) {
const sectorGroup = new THREE.Group();
resultBeadRoot.add(sectorGroup);
for (let row = 0; row < 3; row++) {
for (let column = 0; column < 3; column++) {
const patternIndex = row * 3 + column;
const bead = makeBead(
pattern[patternIndex],
resultBeadGeometry,
resultEdgeGeometry,
resultHighlightGeometry
);
sectorGroup.add(bead.group);
resultBeads.push({
bead,
row,
column,
sector,
patternIndex
});
}
}
}
}
function rebuildGuides() {
guideRoot.clear();
if (markerMesh) {
guideRoot.remove(markerMesh);
markerMesh.dispose();
markerMesh = null;
}
const alpha = (Math.PI * 2) / sectorCount;
const positions = [];
for (let sector = 0; sector < sectorCount; sector++) {
const boundary = sector * alpha;
const cosine = Math.cos(boundary);
const sine = Math.sin(boundary);
positions.push(
cosine * 0.58, sine * 0.58, -0.18,
cosine * 3.18, sine * 3.18, -0.18
);
}
const guideGeometry = new THREE.BufferGeometry();
guideGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
guideRoot.add(
new THREE.LineSegments(
guideGeometry,
new THREE.LineBasicMaterial({
color: 0x6b7896,
transparent: true,
opacity: 0.34
})
)
);
markerMesh = new THREE.InstancedMesh(
markerGeometry,
markerMaterial,
sectorCount
);
const markerDummy = new THREE.Object3D();
for (let sector = 0; sector < sectorCount; sector++) {
const boundary = sector * alpha;
markerDummy.position.set(
Math.cos(boundary) * 3.18,
Math.sin(boundary) * 3.18,
-0.05
);
markerDummy.rotation.set(0, 0, boundary - Math.PI / 2);
markerDummy.updateMatrix();
markerMesh.setMatrixAt(sector, markerDummy.matrix);
}
markerMesh.instanceMatrix.needsUpdate = true;
guideRoot.add(markerMesh);
}
// Central jewel
const core = new THREE.Group();
core.position.z = 0.3;
resultRoot.add(core);
const coreCube = new THREE.Mesh(
new THREE.BoxGeometry(0.43, 0.43, 0.34),
paletteMaterials[0]
);
core.add(coreCube);
const coreEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(coreCube.geometry),
new THREE.LineBasicMaterial({
color: 0xfff0bb,
transparent: true,
opacity: 0.8
})
);
coreEdges.scale.setScalar(1.26);
core.add(coreEdges);
const coreHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.55, 0.018, 8, 64),
new THREE.MeshBasicMaterial({
color: 0xffc857,
transparent: true,
opacity: 0.55
})
);
coreHalo.position.z = -0.08;
core.add(coreHalo);
function updateSymmetry() {
const radians = THREE.MathUtils.degToRad(angle);
const cosine = Math.cos(radians);
const sine = Math.sin(radians);
const alpha = (Math.PI * 2) / sectorCount;
sourceMotif.rotation.z = radians;
for (const record of resultBeads) {
const localX = (record.column - 1) * 0.58;
const localY = (1 - record.row) * 0.58;
let rotatedX = localX * cosine - localY * sine;
const rotatedY = localX * sine + localY * cosine;
// Alternate sectors use the mirrored orientation.
if (record.sector % 2 === 1) {
rotatedX = -rotatedX;
}
const sectorAngle = record.sector * alpha + alpha * 0.5;
const radialX = Math.cos(sectorAngle);
const radialY = Math.sin(sectorAngle);
// Local X runs clockwise around the circle; local Y points outward.
const tangentX = Math.sin(sectorAngle);
const tangentY = -Math.cos(sectorAngle);
const centerRadius = 1.72;
record.bead.group.position.set(
radialX * centerRadius + tangentX * rotatedX + radialX * rotatedY,
radialY * centerRadius + tangentY * rotatedX + radialY * rotatedY,
0.22 + ((record.row + record.column) % 2) * 0.025
);
record.bead.group.rotation.z =
record.sector % 2 === 1 ? -radians : radians;
}
}
function refreshColors() {
sourceBeads.forEach((bead, index) => {
setBeadColor(bead, pattern[index]);
});
resultBeads.forEach((record) => {
setBeadColor(record.bead, pattern[record.patternIndex]);
});
}
function setSectorCount(count) {
sectorCount = count;
sectorButtons.forEach((button) => {
const active = Number(button.dataset.sectors) === sectorCount;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
mirrorMode.textContent = `${sectorCount}-sector mirror`;
resultTitle.textContent = `${sectorCount} reflected sectors`;
buildResultBeads();
rebuildGuides();
updateSymmetry();
}
function setAutoRotate(enabled) {
autoRotate = enabled;
autoButton.setAttribute("aria-pressed", String(enabled));
autoLabel.textContent = enabled ? "Pause" : "Resume";
autoSymbol.textContent = enabled ? "Ⅱ" : "▶";
}
function updateAngleUI() {
const rounded = Math.round(angle) % 360;
angleValue.textContent = `${String(rounded).padStart(3, "0")}°`;
angleInput.value = angle.toFixed(1);
angleInput.style.setProperty("--fill", `${(angle / 360) * 100}%`);
}
function updateCamera() {
const radius = baseRadius * orbit.zoom;
const horizontal = radius * Math.cos(orbit.elevation);
const targetX = cameraTarget.x + Math.sin(orbit.azimuth) * horizontal;
const targetY = cameraTarget.y + Math.sin(orbit.elevation) * radius;
const targetZ = cameraTarget.z + Math.cos(orbit.azimuth) * horizontal;
camera.position.set(targetX, targetY, targetZ);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resize() {
const width = Math.max(stage.clientWidth, 1);
const height = Math.max(stage.clientHeight, 1);
const aspect = width / height;
camera.aspect = aspect;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
const halfFov = THREE.MathUtils.degToRad(camera.fov * 0.5);
const distanceForWidth = 14.4 / (2 * Math.tan(halfFov) * aspect);
const distanceForHeight = 8.7 / (2 * Math.tan(halfFov));
baseRadius = Math.max(12.9, distanceForWidth, distanceForHeight);
updateCamera();
}
const projected = new THREE.Vector3();
function placeLabel(element, root, localY) {
projected.set(0, localY, 0.25);
root.localToWorld(projected);
projected.project(camera);
const width = stage.clientWidth;
const height = stage.clientHeight;
const x = THREE.MathUtils.clamp(
(projected.x * 0.5 + 0.5) * width,
64,
width - 64
);
const y = THREE.MathUtils.clamp(
(-projected.y * 0.5 + 0.5) * height,
18,
height - 8
);
element.style.transform =
`translate3d(${x}px, ${y}px, 0) translate(-50%, -100%)`;
}
// Direct camera controls: no addons are used.
renderer.domElement.addEventListener("pointerdown", (event) => {
isDragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!isDragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.azimuth = THREE.MathUtils.clamp(
orbit.azimuth - deltaX * 0.0045,
-0.55,
0.55
);
orbit.elevation = THREE.MathUtils.clamp(
orbit.elevation + deltaY * 0.0035,
-0.28,
0.42
);
updateCamera();
});
function endDrag(event) {
isDragging = false;
renderer.domElement.classList.remove("dragging");
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.zoom = THREE.MathUtils.clamp(
orbit.zoom * Math.exp(event.deltaY * 0.001),
0.72,
1.28
);
updateCamera();
},
{ passive: false }
);
angleInput.addEventListener("input", () => {
angle = Number(angleInput.value);
setAutoRotate(false);
updateAngleUI();
updateSymmetry();
});
autoButton.addEventListener("click", () => {
setAutoRotate(!autoRotate);
});
shuffleButton.addEventListener("click", () => {
pattern = pattern.map(() => Math.floor(Math.random() * palette.length));
// Guarantee at least one bead of every color.
const colors = [...palette.keys()];
for (let i = colors.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[colors[i], colors[j]] = [colors[j], colors[i]];
}
colors.forEach((color, index) => {
pattern[index] = color;
});
refreshColors();
});
resetViewButton.addEventListener("click", () => {
orbit.azimuth = 0.035;
orbit.elevation = 0.075;
orbit.zoom = 1;
updateCamera();
});
sectorButtons.forEach((button) => {
button.addEventListener("click", () => {
setSectorCount(Number(button.dataset.sectors));
});
});
window.addEventListener("keydown", (event) => {
const activeTag = document.activeElement?.tagName;
const isFormControl = activeTag === "INPUT" || activeTag === "BUTTON";
if (event.code === "Space" && !isFormControl) {
event.preventDefault();
setAutoRotate(!autoRotate);
}
if ((event.code === "ArrowLeft" || event.code === "ArrowRight") && !isFormControl) {
event.preventDefault();
const direction = event.code === "ArrowRight" ? 1 : -1;
angle = (angle + direction * 5 + 360) % 360;
setAutoRotate(false);
updateAngleUI();
updateSymmetry();
}
if (event.code === "KeyR" && !isFormControl) {
orbit.azimuth = 0.035;
orbit.elevation = 0.075;
orbit.zoom = 1;
updateCamera();
}
});
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
window.addEventListener("resize", resize);
setAutoRotate(autoRotate);
setSectorCount(sectorCount);
refreshColors();
updateAngleUI();
resize();
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
if (autoRotate) {
angle = (angle + delta * 11) % 360;
updateAngleUI();
updateSymmetry();
}
dust.rotation.z = elapsed * 0.006;
dashedRing.rotation.z = elapsed * -0.025;
core.rotation.z = elapsed * 0.18;
core.rotation.y = Math.sin(elapsed * 0.7) * 0.12;
const corePulse = 1 + Math.sin(elapsed * 1.7) * 0.035;
coreHalo.scale.setScalar(corePulse);
placeLabel(sourceLabel, sourceRoot, 2.35);
placeLabel(resultLabel, resultRoot, 3.62);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```