Prompt specification
Build or customize the Three.js 3D scene "Run-length encoded pixel strip" 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: Run-length encoded pixel strip
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/run-length-encoded-pixel-strip-x1861
Scene Description Prompt:
Build a colored pixel row beside its run-length pairs; show exact decoding back to the original sequence and count symbols under a stated storage convention. Build this with Three.js from cdnjs as a low-poly scene in one self-contained HTML file.
Working Three.js Starter (run-length-encoded-pixel-strip-x1861):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Run / Return — Three.js RLE Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #09111e;
--panel: rgba(12, 22, 38, 0.86);
--panel-border: rgba(155, 190, 230, 0.17);
--text: #e9f2ff;
--muted: #91a5bf;
--accent: #62e6ff;
--green: #63e6a8;
--red: #ff7d8d;
--mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
--sans: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
}
body {
color: var(--text);
font-family: var(--sans);
background:
radial-gradient(circle at 50% 20%, #1a2b45 0%, #0c1727 42%, #070c15 100%);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
position: absolute;
pointer-events: auto;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.34),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px) saturate(125%);
-webkit-backdrop-filter: blur(16px) saturate(125%);
}
.control-panel {
top: 20px;
left: 20px;
width: min(370px, calc(100vw - 40px));
max-height: calc(100vh - 40px);
overflow: auto;
padding: 20px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--accent);
font: 700 11px/1.2 var(--mono);
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(27px, 3vw, 38px);
line-height: 1;
letter-spacing: -0.045em;
}
.intro {
margin: 11px 0 18px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
label {
display: block;
margin-bottom: 7px;
color: #c8d7e9;
font: 700 12px/1.2 var(--mono);
letter-spacing: 0.05em;
text-transform: uppercase;
}
.input-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
}
input {
min-width: 0;
height: 44px;
padding: 0 12px;
border: 1px solid rgba(130, 170, 210, 0.25);
border-radius: 10px;
outline: none;
color: var(--text);
background: rgba(2, 8, 18, 0.65);
font: 600 16px/1 var(--mono);
letter-spacing: 0.08em;
}
input:focus {
border-color: var(--accent);
box-shadow: 0 0 0 3px rgba(98, 230, 255, 0.12);
}
input[aria-invalid="true"] {
border-color: var(--red);
}
button {
min-height: 44px;
padding: 0 15px;
border: 0;
border-radius: 10px;
color: #06121d;
background: linear-gradient(135deg, #70edff, #68f0b2);
box-shadow: 0 8px 24px rgba(55, 221, 220, 0.16);
cursor: pointer;
font: 800 12px/1 var(--sans);
letter-spacing: 0.03em;
}
button:hover {
filter: brightness(1.08);
}
button:active {
transform: translateY(1px);
}
button.secondary {
width: 100%;
margin-top: 8px;
color: #bed0e5;
background: rgba(125, 160, 200, 0.09);
box-shadow: none;
border: 1px solid rgba(135, 170, 210, 0.13);
}
#validation {
min-height: 19px;
margin-top: 8px;
color: var(--red);
font-size: 12px;
line-height: 1.4;
}
details {
margin-top: 12px;
padding-top: 13px;
border-top: 1px solid rgba(150, 180, 215, 0.12);
}
summary {
color: #cbd9e9;
cursor: pointer;
font: 700 12px/1.3 var(--mono);
letter-spacing: 0.04em;
text-transform: uppercase;
}
details p {
margin: 11px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
details code,
.wire code,
.decoded code {
color: #bfefff;
font-family: var(--mono);
}
.metrics-panel {
top: 20px;
right: 20px;
width: min(370px, calc(100vw - 40px));
max-height: calc(100vh - 40px);
overflow: auto;
padding: 19px;
}
.metrics-panel h2 {
margin: 0 0 14px;
font-size: 17px;
letter-spacing: -0.02em;
}
.metrics-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.metric {
min-height: 75px;
padding: 11px;
border: 1px solid rgba(130, 170, 210, 0.12);
border-radius: 11px;
background: rgba(2, 8, 18, 0.38);
}
.metric .name {
color: var(--muted);
font: 700 10px/1.25 var(--mono);
letter-spacing: 0.06em;
text-transform: uppercase;
}
.metric .value {
margin-top: 7px;
color: #f1f7ff;
font: 800 23px/1 var(--mono);
}
.metric .detail {
margin-top: 5px;
color: #7f95ae;
font-size: 10px;
line-height: 1.35;
}
.wire,
.decoded {
margin-top: 11px;
padding: 12px;
border: 1px solid rgba(130, 170, 210, 0.12);
border-radius: 11px;
background: rgba(2, 8, 18, 0.42);
}
.wire label,
.decoded label {
margin-bottom: 8px;
color: #8fa6c0;
font-size: 10px;
}
.wire code,
.decoded code {
display: block;
overflow-wrap: anywhere;
white-space: pre-wrap;
font-size: 13px;
line-height: 1.5;
}
.match {
margin-top: 11px;
padding: 10px 12px;
border: 1px solid rgba(99, 230, 168, 0.25);
border-radius: 10px;
color: var(--green);
background: rgba(39, 177, 119, 0.09);
font: 800 12px/1.3 var(--mono);
text-align: center;
}
.match.bad {
color: var(--red);
border-color: rgba(255, 125, 141, 0.3);
background: rgba(255, 70, 95, 0.08);
}
.help {
position: absolute;
bottom: 17px;
left: 50%;
padding: 9px 13px;
border: 1px solid rgba(145, 180, 220, 0.13);
border-radius: 999px;
color: #91a5bf;
background: rgba(6, 13, 24, 0.68);
backdrop-filter: blur(10px);
transform: translateX(-50%);
font: 600 11px/1 var(--mono);
white-space: nowrap;
}
@media (max-width: 820px) {
.control-panel {
top: 10px;
left: 10px;
width: min(340px, calc(100vw - 20px));
max-height: 48vh;
padding: 15px;
}
.metrics-panel {
top: auto;
right: 10px;
bottom: 10px;
width: min(340px, calc(100vw - 20px));
max-height: 43vh;
padding: 14px;
}
.help {
display: none;
}
.intro {
margin-bottom: 12px;
}
}
@media (max-width: 480px) {
.control-panel,
.metrics-panel {
width: calc(100vw - 20px);
}
.metrics-grid {
grid-template-columns: repeat(4, 1fr);
}
.metric {
min-height: 62px;
padding: 8px;
}
.metric .value {
font-size: 17px;
}
.metric .detail {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="hud">
<section class="panel control-panel" aria-label="RLE controls">
<p class="eyebrow">Three.js / Compression Study</p>
<h1>Run / Return</h1>
<p class="intro">
Enter a sequence. Its colored pixels, run-length pairs, exact decoding,
and storage counts are rebuilt in the low-poly scene.
</p>
<form id="sequenceForm">
<label for="sequence">Original sequence</label>
<div class="input-row">
<input
id="sequence"
type="text"
value="AAABBCDDD"
maxlength="48"
autocomplete="off"
autocapitalize="off"
spellcheck="false"
aria-describedby="validation"
>
<button type="submit">Encode</button>
</div>
<div id="validation" aria-live="polite"></div>
</form>
<button id="resetButton" class="secondary" type="button">
Reset example
</button>
<details open>
<summary>Storage convention: ASCII RLE-v1</summary>
<p>
Each run is encoded as
<code><symbol><decimal-count>|</code>.
The symbol is one printable ASCII byte, <code>|</code> is excluded,
and there is no header or global terminator.
</p>
<p>
Example: <code>AAABBC</code> becomes
<code>A3|B2|C1|</code>. Logical RLE fields are counted as two per run:
one symbol field and one count field.
</p>
</details>
</section>
<section class="panel metrics-panel" aria-label="Encoding statistics">
<h2>Exact round trip</h2>
<div class="metrics-grid">
<div class="metric">
<div class="name">Original symbols</div>
<div class="value" id="metricOriginal">0</div>
<div class="detail">Raw ASCII bytes</div>
</div>
<div class="metric">
<div class="name">RLE runs</div>
<div class="value" id="metricRuns">0</div>
<div class="detail">Consecutive pairs</div>
</div>
<div class="metric">
<div class="name">Stored fields</div>
<div class="value" id="metricFields">0</div>
<div class="detail">Two fields per run</div>
</div>
<div class="metric">
<div class="name">Stored symbols</div>
<div class="value" id="metricBytes">0</div>
<div class="detail">ASCII bytes in wire form</div>
</div>
</div>
<div class="wire">
<label>ASCII RLE-v1 wire form</label>
<code id="wireText"></code>
</div>
<div class="decoded">
<label>Exact decoded sequence</label>
<code id="decodedText"></code>
</div>
<div class="match" id="matchStatus">✓ Exact symbol match</div>
</section>
<div class="help">
Drag to orbit · Wheel to zoom · Arrow keys to move · Double-click to reset
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and renderer
// -------------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const sequenceInput = document.querySelector("#sequence");
const sequenceForm = document.querySelector("#sequenceForm");
const resetButton = document.querySelector("#resetButton");
const validation = document.querySelector("#validation");
const metricOriginal = document.querySelector("#metricOriginal");
const metricRuns = document.querySelector("#metricRuns");
const metricFields = document.querySelector("#metricFields");
const metricBytes = document.querySelector("#metricBytes");
const wireText = document.querySelector("#wireText");
const decodedText = document.querySelector("#decodedText");
const matchStatus = document.querySelector("#matchStatus");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x09111e, 0);
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",
"Interactive 3D visualization of a run-length encoded sequence"
);
renderer.domElement.tabIndex = 0;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x09111e, 19, 43);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
theta: 0.43,
phi: 0.91,
radius: 18.5,
target: new THREE.Vector3(0, 0.15, -0.45)
};
const defaultCamera = { ...cameraState };
function updateCamera() {
const aspect = window.innerWidth / window.innerHeight;
const mobileCompensation = Math.max(1, 0.92 / aspect);
const radius = cameraState.radius * mobileCompensation;
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraState.target.x + radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + radius * cosPhi,
cameraState.target.z + radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
// -------------------------------------------------------------------------
// Lights and low-poly environment
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xaed4ff, 0x142035, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(5, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 13;
keyLight.shadow.camera.bottom = -13;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x44dfff, 30, 18, 2);
cyanLight.position.set(-7, 4, 3);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0x9d6cff, 25, 17, 2);
violetLight.position.set(7, 3, -2);
scene.add(violetLight);
const floorGeometry = new THREE.PlaneGeometry(40, 40, 12, 12).toNonIndexed();
const floorColors = [];
for (let i = 0; i < floorGeometry.attributes.position.count; i += 3) {
const shade = 0.075 + Math.random() * 0.045;
const color = new THREE.Color().setHSL(0.59, 0.33, shade);
for (let j = 0; j < 3; j++) {
floorColors.push(color.r, color.g, color.b);
}
}
floorGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(floorColors, 3)
);
const floor = new THREE.Mesh(
floorGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.94,
metalness: 0.02,
flatShading: true
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.48;
floor.receiveShadow = true;
scene.add(floor);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(16.4, 0.42, 9.6),
new THREE.MeshStandardMaterial({
color: 0x14243a,
roughness: 0.72,
metalness: 0.08,
flatShading: true
})
);
platform.position.set(0, -0.21, -0.35);
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const decorations = [];
const crystalGeometry = new THREE.ConeGeometry(0.48, 1.25, 5);
const crystalGeometry2 = new THREE.DodecahedronGeometry(0.46, 0);
const decorationData = [
[-9.0, 3.7, 0x3ac6d9, 0.8],
[-8.7, -3.9, 0x7657e8, 1.1],
[ 9.1, 3.2, 0xe05b91, 0.9],
[ 9.0, -3.8, 0x43c88b, 1.0],
[-6.9, 5.3, 0xefaa46, 0.7],
[ 7.3, 5.1, 0x527de8, 0.75]
];
for (const [x, z, color, scale] of decorationData) {
const mesh = new THREE.Mesh(
Math.random() > 0.5 ? crystalGeometry : crystalGeometry2,
new THREE.MeshStandardMaterial({
color,
roughness: 0.6,
metalness: 0.08,
flatShading: true,
emissive: color,
emissiveIntensity: 0.035
})
);
mesh.position.set(x, 0.12, z);
mesh.scale.setScalar(scale);
mesh.rotation.set(
Math.random() * 0.5,
Math.random() * Math.PI,
Math.random() * 0.25
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
decorations.push(mesh);
}
// -------------------------------------------------------------------------
// Text sprites rendered with local canvas textures
// -------------------------------------------------------------------------
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeLabel(
text,
{
color = "#eaf2ff",
background = "rgba(7, 15, 28, 0.82)",
height = 0.42,
maxWidth = Infinity,
fontSize = 40
} = {}
) {
const measureCanvas = document.createElement("canvas");
const measureContext = measureCanvas.getContext("2d");
const font = `700 ${fontSize}px ui-monospace, SFMono-Regular, Menlo, monospace`;
measureContext.font = font;
const measuredWidth = Math.ceil(measureContext.measureText(text).width);
const paddingX = 18;
const paddingY = 12;
const canvas = document.createElement("canvas");
canvas.width = Math.max(4, measuredWidth + paddingX * 2);
canvas.height = fontSize + paddingY * 2;
const context = canvas.getContext("2d");
if (background) {
roundedRect(
context,
1,
1,
canvas.width - 2,
canvas.height - 2,
12
);
context.fillStyle = background;
context.fill();
}
context.font = font;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = color;
context.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.needsUpdate = true;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
const naturalRatio = canvas.width / canvas.height;
let labelHeight = height;
let labelWidth = height * naturalRatio;
if (labelWidth > maxWidth) {
labelWidth = maxWidth;
labelHeight = maxWidth / naturalRatio;
}
sprite.scale.set(labelWidth, labelHeight, 1);
sprite.renderOrder = 20;
return sprite;
}
function addLabel(parent, text, x, y, z, options) {
const label = makeLabel(text, options);
label.position.set(x, y, z);
parent.add(label);
return label;
}
// -------------------------------------------------------------------------
// Encoding and decoding
// -------------------------------------------------------------------------
function analyzeSequence(sequence) {
const runs = [];
for (const symbol of sequence) {
const last = runs[runs.length - 1];
if (last && last.symbol === symbol) {
last.count++;
} else {
runs.push({ symbol, count: 1 });
}
}
// ASCII RLE-v1:
// one symbol byte + decimal count bytes + one "|" separator byte.
const wire = runs
.map((run) => `${run.symbol}${run.count}|`)
.join("");
// Decode from the wire representation independently of the run list.
const decoded = decodeWire(wire);
return {
sequence,
runs,
wire,
decoded,
originalSymbols: sequence.length,
runCount: runs.length,
storedFields: runs.length * 2,
storedSymbols: wire.length,
rawBytes: sequence.length
};
}
function decodeWire(wire) {
let result = "";
for (const record of wire.split("|")) {
if (record.length === 0) continue;
const symbol = record[0];
const countText = record.slice(1);
if (!/^\d+$/.test(countText)) {
throw new Error(`Invalid ASCII RLE-v1 record: ${record}`);
}
result += symbol.repeat(Number(countText));
}
return result;
}
function validateSequence(value) {
if (value.length === 0) {
return "Enter at least one symbol.";
}
for (const character of value) {
const code = character.codePointAt(0);
if (
code < 33 ||
code > 126 ||
character === "|"
) {
return "Use printable ASCII, excluding spaces and the | delimiter.";
}
}
return "";
}
// -------------------------------------------------------------------------
// Dynamic scene construction
// -------------------------------------------------------------------------
const palette = [
0xff647c,
0xffc857,
0x55e6a5,
0x4cc9f0,
0x9b7bff,
0xff9850,
0x9bd65a,
0xf06fb0,
0x59c3ff,
0xe4e76f,
0x7cebc3,
0xff8fa3
];
let sceneContent = null;
function disposeGroup(group) {
if (!group) return;
const geometries = new Set();
const materials = new Set();
const textures = new Set();
group.traverse((object) => {
if (object.geometry) geometries.add(object.geometry);
if (object.material) {
const objectMaterials = Array.isArray(object.material)
? object.material
: [object.material];
for (const material of objectMaterials) {
materials.add(material);
if (material.map) textures.add(material.map);
}
}
});
for (const geometry of geometries) geometry.dispose();
for (const texture of textures) texture.dispose();
for (const material of materials) material.dispose();
}
function buildScene(analysis) {
if (sceneContent) {
scene.remove(sceneContent);
disposeGroup(sceneContent);
}
sceneContent = new THREE.Group();
scene.add(sceneContent);
const symbolColors = new Map();
const uniqueSymbols = [...new Set(analysis.sequence)];
uniqueSymbols.forEach((symbol, index) => {
symbolColors.set(symbol, palette[index % palette.length]);
});
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const pixelGeometry = new THREE.BoxGeometry(1, 1, 1);
const edgeGeometry = new THREE.EdgesGeometry(pixelGeometry);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x06101d,
transparent: true,
opacity: 0.48
});
const symbolMaterials = new Map();
function getSymbolMaterial(symbol) {
if (!symbolMaterials.has(symbol)) {
symbolMaterials.set(
symbol,
new THREE.MeshStandardMaterial({
color: symbolColors.get(symbol),
roughness: 0.52,
metalness: 0.06,
flatShading: true,
emissive: symbolColors.get(symbol),
emissiveIntensity: 0.035
})
);
}
return symbolMaterials.get(symbol);
}
function addPad(x, z, width, depth, color) {
const pad = new THREE.Mesh(
unitBox,
new THREE.MeshStandardMaterial({
color,
roughness: 0.8,
metalness: 0.04,
flatShading: true
})
);
pad.position.set(x, 0.025, z);
pad.scale.set(width, 0.035, depth);
pad.receiveShadow = true;
sceneContent.add(pad);
}
function addPixel(x, z, size, symbol) {
const pixel = new THREE.Mesh(
pixelGeometry,
getSymbolMaterial(symbol)
);
pixel.position.set(x, size / 2 + 0.055, z);
pixel.scale.setScalar(size);
pixel.castShadow = true;
pixel.receiveShadow = true;
const outline = new THREE.LineSegments(edgeGeometry, edgeMaterial);
pixel.add(outline);
sceneContent.add(pixel);
}
// Track surfaces and a vertical divider.
addPad(-3.55, 2.35, 5.9, 1.0, 0x102239);
addPad(4.0, 0.85, 6.55, 4.0, 0x101e33);
addPad(0, -3.75, 13.3, 1.0, 0x10243a);
const divider = new THREE.Mesh(
unitBox,
new THREE.MeshStandardMaterial({
color: 0x2b4563,
emissive: 0x183149,
emissiveIntensity: 0.2,
roughness: 0.65,
flatShading: true
})
);
divider.position.set(0, 0.05, -0.55);
divider.scale.set(0.035, 0.025, 7.3);
sceneContent.add(divider);
// Section labels.
addLabel(
sceneContent,
"ORIGINAL PIXELS",
-3.55,
1.24,
2.42,
{ color: "#bfefff", height: 0.48 }
);
addLabel(
sceneContent,
"RUN-LENGTH PAIRS",
4.0,
1.24,
3.18,
{ color: "#d8ceff", height: 0.48 }
);
addLabel(
sceneContent,
"DECODED PIXELS · EXACT MATCH",
0,
1.22,
-3.42,
{ color: "#8dffd0", height: 0.46 }
);
// Original sequence: a colored pixel row on the left.
const originalCount = analysis.sequence.length;
const originalTrackWidth = 5.72;
const originalSpacing = originalTrackWidth / originalCount;
const originalTotalWidth = originalSpacing * originalCount;
const originalStart = -3.55 - originalTotalWidth / 2;
const originalPixelSize = Math.min(0.48, originalSpacing * 0.72);
for (let i = 0; i < originalCount; i++) {
const x = originalStart + originalSpacing * (i + 0.5);
addPixel(x, 2.35, originalPixelSize, analysis.sequence[i]);
}
// RLE pairs: a compact grid to the right of the original row.
const runCount = analysis.runs.length;
const columns = Math.min(8, runCount);
const rows = Math.ceil(runCount / columns);
const gridWidth = 6.2;
const columnWidth = gridWidth / columns;
const cardWidth = Math.min(1.45, columnWidth * 0.86);
const rowGap = 0.66;
for (let i = 0; i < runCount; i++) {
const run = analysis.runs[i];
const row = Math.floor(i / columns);
const column = i % columns;
const x = 0.85 + columnWidth * (column + 0.5);
const z = 2.35 - row * rowGap;
const card = new THREE.Mesh(
unitBox,
new THREE.MeshStandardMaterial({
color: row % 2 === 0 ? 0x1a2d46 : 0x17263c,
roughness: 0.66,
metalness: 0.08,
flatShading: true
})
);
card.position.set(x, 0.29, z);
card.scale.set(cardWidth, 0.49, 0.16);
card.castShadow = true;
card.receiveShadow = true;
sceneContent.add(card);
// Colored symbol block.
const symbolBlock = new THREE.Mesh(
pixelGeometry,
getSymbolMaterial(run.symbol)
);
symbolBlock.position.set(x - cardWidth * 0.26, 0.56, z + 0.02);
symbolBlock.scale.setScalar(0.28);
symbolBlock.castShadow = true;
sceneContent.add(symbolBlock);
// Exact symbol glyph.
addLabel(
sceneContent,
run.symbol,
x - cardWidth * 0.26,
0.56,
z + 0.19,
{
color: "#ffffff",
background: null,
height: 0.25,
maxWidth: cardWidth * 0.31,
fontSize: 36
}
);
// Run count.
addLabel(
sceneContent,
`×${run.count}`,
x + cardWidth * 0.13,
0.57,
z + 0.15,
{
color: "#dbe9fa",
background: "rgba(5, 12, 23, 0.72)",
height: 0.25,
maxWidth: cardWidth * 0.53,
fontSize: 34
}
);
}
// Exact decoded sequence: a second colored row spanning the platform.
const decodedCount = analysis.decoded.length;
const decodedTrackWidth = 13.05;
const decodedSpacing = decodedTrackWidth / decodedCount;
const decodedTotalWidth = decodedSpacing * decodedCount;
const decodedStart = -decodedTotalWidth / 2;
const decodedPixelSize = Math.min(0.48, decodedSpacing * 0.72);
for (let i = 0; i < decodedCount; i++) {
const x = decodedStart + decodedSpacing * (i + 0.5);
addPixel(x, -3.75, decodedPixelSize, analysis.decoded[i]);
}
}
function updateInterface(analysis) {
metricOriginal.textContent = analysis.originalSymbols;
metricRuns.textContent = analysis.runCount;
metricFields.textContent = analysis.storedFields;
metricBytes.textContent = analysis.storedSymbols;
wireText.textContent = analysis.wire;
decodedText.textContent = analysis.decoded;
const exact = analysis.decoded === analysis.sequence;
matchStatus.textContent = exact
? "✓ Exact symbol match"
: "✕ Decoding mismatch";
matchStatus.classList.toggle("bad", !exact);
}
function renderSequence(sequence) {
const error = validateSequence(sequence);
if (error) {
validation.textContent = `${error} The scene shows the last valid sequence.`;
sequenceInput.setAttribute("aria-invalid", "true");
return false;
}
const analysis = analyzeSequence(sequence);
buildScene(analysis);
updateInterface(analysis);
validation.textContent = "";
sequenceInput.setAttribute("aria-invalid", "false");
return true;
}
// -------------------------------------------------------------------------
// Input behavior
// -------------------------------------------------------------------------
sequenceForm.addEventListener("submit", (event) => {
event.preventDefault();
renderSequence(sequenceInput.value);
});
let inputTimer = null;
sequenceInput.addEventListener("input", () => {
clearTimeout(inputTimer);
validation.textContent = "";
sequenceInput.removeAttribute("aria-invalid");
inputTimer = window.setTimeout(() => {
renderSequence(sequenceInput.value);
}, 180);
});
resetButton.addEventListener("click", () => {
sequenceInput.value = "AAABBCDDD";
renderSequence(sequenceInput.value);
});
// -------------------------------------------------------------------------
// Direct camera controls: no Three.js addons
// -------------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= deltaX * 0.005;
cameraState.phi -= deltaY * 0.0045;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi,
0.32,
1.43
);
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.style.cursor = "grab";
if (event.pointerId !== undefined) {
try {
renderer.domElement.releasePointerCapture(event.pointerId);
} catch {
// The pointer may already have been released.
}
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.style.cursor = "grab";
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius *= Math.exp(event.deltaY * 0.001);
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius,
10,
29
);
updateCamera();
},
{ passive: false }
);
function resetCamera() {
cameraState.theta = defaultCamera.theta;
cameraState.phi = defaultCamera.phi;
cameraState.radius = defaultCamera.radius;
cameraState.target.copy(defaultCamera.target);
updateCamera();
}
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
const tag = document.activeElement?.tagName;
if (tag === "INPUT" || tag === "TEXTAREA") return;
const step = 0.08;
switch (event.key) {
case "ArrowLeft":
cameraState.theta += step;
break;
case "ArrowRight":
cameraState.theta -= step;
break;
case "ArrowUp":
cameraState.phi = Math.max(0.32, cameraState.phi - step);
break;
case "ArrowDown":
cameraState.phi = Math.min(1.43, cameraState.phi + step);
break;
case "+":
case "=":
cameraState.radius = Math.max(10, cameraState.radius - 0.7);
break;
case "-":
cameraState.radius = Math.min(29, cameraState.radius + 0.7);
break;
case "Home":
resetCamera();
break;
default:
return;
}
event.preventDefault();
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
});
// -------------------------------------------------------------------------
// Start
// -------------------------------------------------------------------------
renderSequence(sequenceInput.value);
updateCamera();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
decorations.forEach((mesh, index) => {
mesh.rotation.y += 0.002 + index * 0.00015;
mesh.rotation.z = Math.sin(elapsed * 0.35 + index) * 0.035;
});
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```