Prompt specification
Build or customize the Three.js 3D scene "Color operation exploration board" 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: Color operation exploration board
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/color-operation-exploration-board-x1528
Scene Description Prompt:
Choose light addition or filter multiplication on a voxel color board; show intermediate values in a simplified RGB model. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (color-operation-exploration-board-x1528):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Chroma Lab — Voxel RGB</title>
<style>
:root {
color-scheme: dark;
--bg: #090e14;
--panel: #101720;
--panel-2: #151e29;
--line: #26313e;
--text: #edf3f7;
--muted: #8f9baa;
--accent: #c8f66a;
--accent-soft: rgba(200, 246, 106, 0.12);
--cyan: #62e6dc;
--amber: #ffbd67;
--radius: 12px;
}
body[data-mode="filter"] {
--accent: #62e6dc;
--accent-soft: rgba(98, 230, 220, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
display: grid;
grid-template-columns: 360px minmax(0, 1fr);
grid-template-rows: 68px minmax(0, 1fr);
width: 100%;
height: 100dvh;
background: var(--bg);
}
.topbar {
grid-column: 1 / -1;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 24px;
border-bottom: 1px solid var(--line);
background: rgba(10, 15, 21, 0.94);
backdrop-filter: blur(18px);
z-index: 10;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
}
.brand-mark {
position: relative;
width: 30px;
height: 30px;
transform: rotate(45deg);
}
.brand-mark span {
position: absolute;
width: 13px;
height: 13px;
border-radius: 3px;
background: var(--accent);
box-shadow: 0 0 18px var(--accent-soft);
}
.brand-mark span:nth-child(1) {
top: 0;
left: 0;
}
.brand-mark span:nth-child(2) {
right: 0;
bottom: 0;
opacity: 0.45;
}
.brand-copy strong {
display: block;
font-size: 13px;
line-height: 1;
letter-spacing: 0.16em;
}
.brand-copy small {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.top-status {
display: flex;
align-items: center;
gap: 10px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 10px var(--accent);
}
.panel {
grid-column: 1;
grid-row: 2;
min-height: 0;
overflow-y: auto;
border-right: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(255, 255, 255, 0.018), transparent 26%),
var(--panel);
scrollbar-width: thin;
scrollbar-color: #344150 transparent;
}
.intro,
.control-section {
padding: 22px 24px;
border-bottom: 1px solid var(--line);
}
.intro {
padding-top: 26px;
}
.eyebrow {
display: block;
margin-bottom: 9px;
color: var(--accent);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 26px;
line-height: 1.12;
letter-spacing: -0.035em;
}
.intro p {
margin: 12px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.65;
}
.section-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 16px;
}
.section-head h2 {
margin: 0;
font-size: 13px;
letter-spacing: -0.01em;
}
.section-head small {
color: var(--muted);
font-size: 10px;
}
.mode-switch {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 4px;
padding: 4px;
border: 1px solid var(--line);
border-radius: 10px;
background: #0b1118;
}
.mode-btn {
min-height: 48px;
padding: 8px 10px;
border: 0;
border-radius: 7px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 12px;
font-weight: 650;
transition:
background 160ms ease,
color 160ms ease,
transform 160ms ease;
}
.mode-btn:hover {
color: var(--text);
}
.mode-btn.active {
background: var(--accent);
color: #11170d;
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
}
.mode-note {
margin: 12px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.color-line {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 18px;
}
.color-preview {
position: relative;
flex: 0 0 auto;
width: 46px;
height: 46px;
padding: 0;
overflow: hidden;
border: 1px solid #3a4654;
border-radius: 10px;
background: #fff;
cursor: pointer;
}
.color-preview input {
position: absolute;
inset: -8px;
width: 62px;
height: 62px;
padding: 0;
border: 0;
cursor: pointer;
opacity: 0;
}
.color-info strong {
display: block;
font-size: 12px;
}
.color-info small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
}
.hex-value {
margin-left: auto;
color: var(--muted);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
}
.slider-row {
display: grid;
grid-template-columns: 18px minmax(0, 1fr) 36px;
align-items: center;
gap: 10px;
min-height: 30px;
}
.slider-row > span {
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.slider-row output {
color: #d8e0e8;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
text-align: right;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 999px;
outline: none;
background: #2a3542;
cursor: pointer;
accent-color: var(--accent);
appearance: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 13px;
height: 13px;
border: 2px solid #101720;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent);
appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 2px solid #101720;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent);
}
.strength-wrap {
margin-top: 14px;
padding-top: 14px;
border-top: 1px dashed var(--line);
}
.strength-label {
display: flex;
justify-content: space-between;
margin-bottom: 9px;
color: var(--muted);
font-size: 10px;
}
.strength-label output {
color: var(--accent);
font-family: "SFMono-Regular", Consolas, monospace;
}
.math-stack {
display: grid;
gap: 8px;
}
.math-item {
display: grid;
grid-template-columns: 34px minmax(0, 1fr);
align-items: center;
gap: 10px;
min-height: 48px;
padding: 7px 9px;
border: 1px solid #222d39;
border-radius: 9px;
background: #0c131b;
}
.math-swatch {
width: 34px;
height: 34px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 7px;
background: #27313d;
}
.math-copy {
min-width: 0;
}
.math-copy span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.math-copy strong {
display: block;
overflow: hidden;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
font-weight: 500;
line-height: 1.45;
text-overflow: ellipsis;
white-space: nowrap;
}
.math-item.final {
border-color: rgba(200, 246, 106, 0.28);
background: var(--accent-soft);
}
.equation {
margin-top: 12px;
padding: 12px;
overflow-x: auto;
border: 1px solid var(--line);
border-radius: 9px;
background: #090f15;
color: var(--accent);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
line-height: 1.65;
white-space: nowrap;
}
.model-note {
margin: 10px 0 0;
color: #687687;
font-size: 9px;
line-height: 1.5;
}
.stage {
position: relative;
grid-column: 2;
grid-row: 2;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 52% 42%, rgba(48, 70, 88, 0.35), transparent 37%),
radial-gradient(circle at 12% 88%, rgba(68, 93, 72, 0.12), transparent 32%),
linear-gradient(145deg, #0c1219, #090e14 72%);
}
#scene {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
}
.scene-heading {
position: absolute;
top: 28px;
left: 30px;
z-index: 2;
pointer-events: none;
}
.scene-heading .eyebrow {
color: var(--muted);
}
.scene-heading h2 {
margin: 0;
font-size: clamp(22px, 2.3vw, 34px);
font-weight: 580;
letter-spacing: -0.045em;
}
.scene-heading p {
margin: 8px 0 0;
color: var(--muted);
font-size: 11px;
}
.scene-tools {
position: absolute;
top: 25px;
right: 26px;
z-index: 3;
display: flex;
gap: 8px;
}
.tool-btn {
min-height: 36px;
padding: 0 13px;
border: 1px solid #2c3947;
border-radius: 9px;
background: rgba(15, 23, 32, 0.78);
color: #b7c2ce;
cursor: pointer;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.03em;
backdrop-filter: blur(14px);
transition:
border-color 160ms ease,
color 160ms ease,
background 160ms ease;
}
.tool-btn:hover {
border-color: #506172;
background: rgba(23, 34, 45, 0.92);
color: white;
}
.output-card {
position: absolute;
right: 26px;
bottom: 25px;
z-index: 3;
width: 245px;
padding: 15px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 13px;
background: rgba(12, 19, 27, 0.82);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(18px);
}
.output-top {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
}
.output-top span {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.mode-badge {
padding: 4px 7px;
border-radius: 999px;
background: var(--accent-soft);
color: var(--accent);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.output-main {
display: grid;
grid-template-columns: 50px minmax(0, 1fr);
align-items: center;
gap: 12px;
}
.stage-swatch {
width: 50px;
height: 50px;
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 10px;
background: #4b72a2;
box-shadow: inset 0 1px rgba(255, 255, 255, 0.15);
}
.output-values strong {
display: block;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 12px;
font-weight: 550;
}
.output-values small {
display: block;
margin-top: 5px;
color: var(--muted);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
}
.copy-btn {
width: 100%;
margin-top: 12px;
padding: 8px;
border: 1px solid #344250;
border-radius: 7px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.copy-btn:hover {
border-color: var(--accent);
color: var(--accent);
}
.gesture-hint {
position: absolute;
left: 28px;
bottom: 28px;
z-index: 2;
display: flex;
align-items: center;
gap: 9px;
color: #718091;
font-size: 9px;
letter-spacing: 0.04em;
pointer-events: none;
}
.gesture-hint::before {
content: "";
width: 7px;
height: 7px;
border: 1px solid var(--accent);
border-radius: 50%;
box-shadow: 0 0 8px var(--accent-soft);
}
@media (max-width: 900px) {
html,
body {
overflow: auto;
}
.app {
grid-template-columns: 1fr;
grid-template-rows: 62px 58dvh auto;
height: auto;
min-height: 100dvh;
}
.topbar {
grid-column: 1;
grid-row: 1;
padding: 0 16px;
}
.panel {
grid-column: 1;
grid-row: 3;
overflow: visible;
border-right: 0;
}
.stage {
grid-column: 1;
grid-row: 2;
min-height: 420px;
}
.scene-heading {
top: 20px;
left: 18px;
}
.scene-heading p {
display: none;
}
.scene-tools {
top: 18px;
right: 16px;
}
.tool-btn {
padding: 0 10px;
}
.output-card {
right: 16px;
bottom: 16px;
width: 205px;
}
.gesture-hint {
left: 18px;
bottom: 20px;
}
}
@media (max-width: 560px) {
.top-status span:last-child {
display: none;
}
.scene-heading h2 {
font-size: 21px;
}
.scene-tools {
top: 67px;
}
.output-card {
width: 184px;
}
.gesture-hint {
display: none;
}
.intro,
.control-section {
padding-right: 18px;
padding-left: 18px;
}
}
</style>
</head>
<body data-mode="addition">
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<span></span>
<span></span>
</div>
<div class="brand-copy">
<strong>CHROMA LAB</strong>
<small>Voxel color systems</small>
</div>
</div>
<div class="top-status">
<span class="status-dot"></span>
<span>8-bit RGB / channel model</span>
</div>
</header>
<aside class="panel">
<section class="intro">
<span class="eyebrow" id="modeEyebrow">Model 01 / additive</span>
<h1 id="modeTitle">Light addition</h1>
<p id="modeCopy">
Add an independent light contribution to every base-color channel,
then clip the result to the display range.
</p>
</section>
<section class="control-section">
<div class="section-head">
<h2>Color operation</h2>
<small>Choose one</small>
</div>
<div class="mode-switch" role="group" aria-label="Color operation">
<button
class="mode-btn active"
id="modeAdd"
type="button"
aria-pressed="true"
>
Light addition
</button>
<button
class="mode-btn"
id="modeFilter"
type="button"
aria-pressed="false"
>
Filter multiply
</button>
</div>
<p class="mode-note" id="modeNote">
I = clip(C + L × energy)
</p>
</section>
<section class="control-section">
<div class="section-head">
<h2 id="lightTitle">Light contribution</h2>
<small>RGB source</small>
</div>
<div class="color-line">
<label class="color-preview" id="lightPreview" title="Choose light color">
<input id="lightPicker" type="color" value="#48362a" />
</label>
<div class="color-info">
<strong id="lightHint">Added to the selected voxel</strong>
<small>0–255 per channel</small>
</div>
<span class="hex-value" id="lightHex">#48362A</span>
</div>
<label class="slider-row">
<span>R</span>
<input id="lightR" type="range" min="0" max="255" value="72" />
<output id="lightRVal">72</output>
</label>
<label class="slider-row">
<span>G</span>
<input id="lightG" type="range" min="0" max="255" value="54" />
<output id="lightGVal">54</output>
</label>
<label class="slider-row">
<span>B</span>
<input id="lightB" type="range" min="0" max="255" value="42" />
<output id="lightBVal">42</output>
</label>
<div class="strength-wrap">
<div class="strength-label">
<span id="strengthLabel">Energy</span>
<output id="strengthValue">0.80</output>
</div>
<input
id="strength"
type="range"
min="0"
max="2"
step="0.01"
value="0.8"
/>
</div>
</section>
<section class="control-section">
<div class="section-head">
<h2>Selected voxel</h2>
<small id="baseCoord">R3 / C5</small>
</div>
<div class="color-line">
<label class="color-preview" id="basePreview" title="Choose base color">
<input id="basePicker" type="color" value="#356ca8" />
</label>
<div class="color-info">
<strong id="baseName">Voxel V21</strong>
<small>Base reflectance color</small>
</div>
<span class="hex-value" id="baseHex">#356CA8</span>
</div>
<label class="slider-row">
<span>R</span>
<input id="baseR" type="range" min="0" max="255" value="53" />
<output id="baseRVal">53</output>
</label>
<label class="slider-row">
<span>G</span>
<input id="baseG" type="range" min="0" max="255" value="108" />
<output id="baseGVal">108</output>
</label>
<label class="slider-row">
<span>B</span>
<input id="baseB" type="range" min="0" max="255" value="168" />
<output id="baseBVal">168</output>
</label>
</section>
<section class="control-section">
<div class="section-head">
<h2>Intermediate values</h2>
<small>Per selected voxel</small>
</div>
<div class="math-stack">
<div class="math-item">
<div class="math-swatch" id="baseSwatch"></div>
<div class="math-copy">
<span>Base C</span>
<strong id="baseRGB">53 / 108 / 168</strong>
</div>
</div>
<div class="math-item">
<div class="math-swatch" id="lightSwatch"></div>
<div class="math-copy">
<span id="lightMathLabel">Light L</span>
<strong id="lightRGB">72 / 54 / 42</strong>
</div>
</div>
<div class="math-item">
<div class="math-swatch" id="factorSwatch"></div>
<div class="math-copy">
<span id="factorLabel">L × energy</span>
<strong id="factorRGB">57.6 / 43.2 / 33.6</strong>
</div>
</div>
<div class="math-item">
<div class="math-swatch" id="rawSwatch"></div>
<div class="math-copy">
<span id="rawLabel">Unclipped sum</span>
<strong id="rawRGB">110.6 / 151.2 / 201.6</strong>
</div>
</div>
<div class="math-item final">
<div class="math-swatch" id="finalSwatch"></div>
<div class="math-copy">
<span>Final / clipped</span>
<strong id="finalRGB">111 / 151 / 202</strong>
</div>
</div>
</div>
<div class="equation" id="equation">[53, 108, 168] + [57.6, 43.2, 33.6]</div>
<p class="model-note">
Simplified per-channel model. Each final channel is clipped to 0–255.
Scene illumination is neutral; the swatches show the exact model result.
</p>
</section>
</aside>
<main class="stage" id="stage">
<canvas
id="scene"
tabindex="0"
aria-label="Interactive 3D voxel color board. Drag to orbit, scroll to zoom, and click a voxel to select it."
></canvas>
<div class="scene-heading">
<span class="eyebrow">Board 08 × 06 / 48 voxels</span>
<h2>Voxel spectrum</h2>
<p>Click a block to inspect its channel math.</p>
</div>
<div class="scene-tools">
<button class="tool-btn" id="resetView" type="button">Reset view</button>
<button class="tool-btn" id="shuffleBtn" type="button">Shuffle colors</button>
</div>
<div class="output-card">
<div class="output-top">
<span>Selected output</span>
<span class="mode-badge" id="stageMode">Addition</span>
</div>
<div class="output-main">
<div class="stage-swatch" id="stageSwatch"></div>
<div class="output-values">
<strong id="stageRGB">RGB 111 / 151 / 202</strong>
<small id="stageHex">#6F97CA</small>
</div>
</div>
<button class="copy-btn" id="copyBtn" type="button">Copy calculation</button>
</div>
<div class="gesture-hint">Drag to orbit · Scroll to zoom · Click to inspect</div>
</main>
</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 state
// -------------------------------------------------------------------------
const $ = (id) => document.getElementById(id);
const stage = $("stage");
const canvas = $("scene");
const ui = {
modeAdd: $("modeAdd"),
modeFilter: $("modeFilter"),
modeEyebrow: $("modeEyebrow"),
modeTitle: $("modeTitle"),
modeCopy: $("modeCopy"),
modeNote: $("modeNote"),
lightTitle: $("lightTitle"),
lightHint: $("lightHint"),
lightPicker: $("lightPicker"),
lightPreview: $("lightPreview"),
lightHex: $("lightHex"),
lightR: $("lightR"),
lightG: $("lightG"),
lightB: $("lightB"),
lightRVal: $("lightRVal"),
lightGVal: $("lightGVal"),
lightBVal: $("lightBVal"),
strength: $("strength"),
strengthLabel: $("strengthLabel"),
strengthValue: $("strengthValue"),
basePicker: $("basePicker"),
basePreview: $("basePreview"),
baseHex: $("baseHex"),
baseCoord: $("baseCoord"),
baseName: $("baseName"),
baseR: $("baseR"),
baseG: $("baseG"),
baseB: $("baseB"),
baseRVal: $("baseRVal"),
baseGVal: $("baseGVal"),
baseBVal: $("baseBVal"),
baseSwatch: $("baseSwatch"),
lightSwatch: $("lightSwatch"),
factorSwatch: $("factorSwatch"),
rawSwatch: $("rawSwatch"),
finalSwatch: $("finalSwatch"),
baseRGB: $("baseRGB"),
lightRGB: $("lightRGB"),
lightMathLabel: $("lightMathLabel"),
factorLabel: $("factorLabel"),
factorRGB: $("factorRGB"),
rawLabel: $("rawLabel"),
rawRGB: $("rawRGB"),
finalRGB: $("finalRGB"),
equation: $("equation"),
stageMode: $("stageMode"),
stageSwatch: $("stageSwatch"),
stageRGB: $("stageRGB"),
stageHex: $("stageHex"),
copyBtn: $("copyBtn"),
resetView: $("resetView"),
shuffleBtn: $("shuffleBtn")
};
const columns = 8;
const rows = 6;
const spacing = 0.98;
let mode = "addition";
let light = [72, 54, 42];
let selectedIndex = 20;
let hoveredIndex = -1;
let cameraTouched = false;
const clamp = (value, min = 0, max = 255) =>
Math.min(max, Math.max(min, value));
const format = (value, decimals = 1) => {
const rounded = Number(value.toFixed(decimals));
return Number.isInteger(rounded) ? String(rounded) : rounded.toFixed(decimals);
};
function rgbToHex(rgb) {
return (
"#" +
rgb
.map((channel) => clamp(Math.round(channel)).toString(16).padStart(2, "0"))
.join("")
.toUpperCase()
);
}
function hexToRgb(hex) {
const value = hex.replace("#", "");
return [
parseInt(value.slice(0, 2), 16),
parseInt(value.slice(2, 4), 16),
parseInt(value.slice(4, 6), 16)
];
}
function rgbCss(rgb) {
return `rgb(${rgb.map((value) => clamp(Math.round(value))).join(", ")})`;
}
function hslToRgb(h, s, l) {
s /= 100;
l /= 100;
const c = (1 - Math.abs(2 * l - 1)) * s;
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
const m = l - c / 2;
let rgb;
if (h < 60) rgb = [c, x, 0];
else if (h < 120) rgb = [x, c, 0];
else if (h < 180) rgb = [0, c, x];
else if (h < 240) rgb = [0, x, c];
else if (h < 300) rgb = [x, 0, c];
else rgb = [c, 0, x];
return rgb.map((value) => Math.round((value + m) * 255));
}
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x0b1118, 15, 29);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const cameraState = {
azimuth: 0.62,
elevation: 0.67,
distance: 11.5,
targetAzimuth: 0.62,
targetElevation: 0.67,
targetDistance: 11.5
};
const cameraTarget = new THREE.Vector3(0, 0.12, 0);
function updateCamera() {
const { azimuth, elevation, distance } = cameraState;
camera.position.set(
Math.sin(azimuth) * Math.cos(elevation) * distance,
Math.sin(elevation) * distance,
Math.cos(azimuth) * Math.cos(elevation) * distance
);
camera.lookAt(cameraTarget);
}
updateCamera();
scene.add(new THREE.AmbientLight(0xffffff, 0.72));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.35);
keyLight.position.set(4.5, 8, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 22;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xffffff, 0.72);
fillLight.position.set(-5, 4, -4);
scene.add(fillLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x0c1219,
roughness: 1,
metalness: 0,
transparent: true,
opacity: 0.78
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.205;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 48, 0x263642, 0x18242e);
grid.position.y = -0.198;
grid.material.transparent = true;
grid.material.opacity = 0.34;
scene.add(grid);
const boardWidth = columns * spacing + 0.36;
const boardDepth = rows * spacing + 0.36;
const platform = new THREE.Mesh(
new THREE.BoxGeometry(boardWidth, 0.18, boardDepth),
new THREE.MeshStandardMaterial({
color: 0x151e28,
roughness: 0.72,
metalness: 0.18
})
);
platform.position.y = -0.09;
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const platformEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(platform.geometry),
new THREE.LineBasicMaterial({
color: 0x3a4856,
transparent: true,
opacity: 0.75
})
);
platformEdges.position.copy(platform.position);
scene.add(platformEdges);
const tileGeometry = new THREE.BoxGeometry(0.82, 0.62, 0.82);
const tiles = [];
const palette = [
[224, 90, 71],
[243, 168, 59],
[232, 212, 91],
[104, 185, 132],
[58, 166, 160],
[78, 120, 199],
[140, 97, 199],
[214, 93, 145],
[240, 141, 104],
[212, 195, 106],
[123, 196, 122],
[77, 165, 183],
[94, 114, 208],
[156, 107, 193],
[201, 97, 145],
[49, 78, 122]
];
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
const index = row * columns + column;
const colorIndex = (column * 3 + row * 5) % palette.length;
const base = [...palette[colorIndex]];
const variation = 0.92 + ((index * 17) % 13) / 100;
const adjusted = base.map((channel) =>
clamp(Math.round(channel * variation))
);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.31,
metalness: 0.035,
emissive: 0x000000
});
const mesh = new THREE.Mesh(tileGeometry, material);
mesh.position.set(
(column - (columns - 1) / 2) * spacing,
0.31,
(row - (rows - 1) / 2) * spacing
);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.index = index;
scene.add(mesh);
tiles.push({
index,
row,
column,
base: adjusted,
mesh
});
}
}
const selectionFrame = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(0.88, 0.68, 0.88)),
new THREE.LineBasicMaterial({
color: 0xc8f66a,
transparent: true,
opacity: 0.95
})
);
scene.add(selectionFrame);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
// -------------------------------------------------------------------------
// Simplified RGB calculations
// -------------------------------------------------------------------------
function calculate(base, source, strength, operation) {
if (operation === "addition") {
const gain = source.map((channel) => channel * strength);
const raw = base.map((channel, index) => channel + gain[index]);
const final = raw.map((channel) => clamp(Math.round(channel)));
return {
gain,
factor: null,
raw,
final
};
}
const factor = source.map(
(channel) => 1 + strength * (channel / 255 - 1)
);
const raw = base.map((channel, index) => channel * factor[index]);
const final = raw.map((channel) => clamp(Math.round(channel)));
return {
gain: null,
factor,
raw,
final
};
}
function updateMaterials() {
const strength = Number(ui.strength.value);
for (const tile of tiles) {
const result = calculate(tile.base, light, strength, mode);
tile.result = result;
tile.mesh.material.color.setRGB(
result.final[0] / 255,
result.final[1] / 255,
result.final[2] / 255,
THREE.SRGBColorSpace
);
const isSelected = tile.index === selectedIndex;
const isHovered = tile.index === hoveredIndex;
const emissiveStrength = isSelected ? 0.1 : isHovered ? 0.045 : 0;
tile.mesh.material.emissive.setRGB(
(result.final[0] / 255) * emissiveStrength,
(result.final[1] / 255) * emissiveStrength,
(result.final[2] / 255) * emissiveStrength,
THREE.SRGBColorSpace
);
tile.mesh.position.y = isSelected ? 0.35 : 0.31;
}
const selected = tiles[selectedIndex];
selectionFrame.position.copy(selected.mesh.position);
selectionFrame.material.color.set(mode === "addition" ? 0xc8f66a : 0x62e6dc);
}
function syncBaseControls() {
const tile = tiles[selectedIndex];
const [r, g, b] = tile.base;
ui.baseR.value = r;
ui.baseG.value = g;
ui.baseB.value = b;
ui.baseRVal.value = r;
ui.baseGVal.value = g;
ui.baseBVal.value = b;
ui.basePicker.value = rgbToHex(tile.base);
ui.basePreview.style.background = rgbCss(tile.base);
ui.baseHex.textContent = rgbToHex(tile.base);
ui.baseCoord.textContent =
`R${tile.row + 1} / C${tile.column + 1}`;
ui.baseName.textContent =
`Voxel V${String(tile.index + 1).padStart(2, "0")}`;
}
function syncLightControls() {
const [r, g, b] = light;
ui.lightR.value = r;
ui.lightG.value = g;
ui.lightB.value = b;
ui.lightRVal.value = r;
ui.lightGVal.value = g;
ui.lightBVal.value = b;
ui.lightPicker.value = rgbToHex(light);
ui.lightPreview.style.background = rgbCss(light);
ui.lightHex.textContent = rgbToHex(light);
}
function updatePanel() {
const tile = tiles[selectedIndex];
const strength = Number(ui.strength.value);
const result = calculate(tile.base, light, strength, mode);
ui.baseSwatch.style.background = rgbCss(tile.base);
ui.lightSwatch.style.background = rgbCss(light);
ui.baseRGB.textContent = tile.base.join(" / ");
ui.lightRGB.textContent = light.join(" / ");
if (mode === "addition") {
ui.lightMathLabel.textContent = "Light L";
ui.factorLabel.textContent = "L × energy";
ui.factorRGB.textContent = result.gain.map((v) => format(v, 1)).join(" / ");
ui.rawLabel.textContent = "Unclipped sum";
ui.equation.textContent =
`[${tile.base.join(", ")}] + ` +
`[${result.gain.map((v) => format(v, 1)).join(", ")}] = ` +
`[${result.raw.map((v) => format(v, 1)).join(", ")}]`;
} else {
ui.lightMathLabel.textContent = "Filter L";
ui.factorLabel.textContent = "Filter factor";
ui.factorRGB.textContent = result.factor.map((v) => format(v, 3)).join(" / ");
ui.rawLabel.textContent = "Unclipped product";
ui.equation.textContent =
`[${tile.base.join(", ")}] × ` +
`[${result.factor.map((v) => format(v, 3)).join(", ")}] = ` +
`[${result.raw.map((v) => format(v, 1)).join(", ")}]`;
}
ui.rawRGB.textContent = result.raw.map((v) => format(v, 1)).join(" / ");
ui.finalRGB.textContent = result.final.join(" / ");
const rawPreview = result.raw.map((value) => clamp(value));
const factorPreview =
mode === "addition"
? result.gain
: result.factor.map((value) => value * 255);
ui.factorSwatch.style.background = rgbCss(factorPreview);
ui.rawSwatch.style.background = rgbCss(rawPreview);
ui.finalSwatch.style.background = rgbCss(result.final);
ui.stageSwatch.style.background = rgbCss(result.final);
ui.stageRGB.textContent = `RGB ${result.final.join(" / ")}`;
ui.stageHex.textContent = rgbToHex(result.final);
ui.stageMode.textContent = mode === "addition" ? "Addition" : "Multiply";
}
function updateEverything() {
updateMaterials();
updatePanel();
}
// -------------------------------------------------------------------------
// UI events
// -------------------------------------------------------------------------
function setMode(nextMode) {
mode = nextMode;
document.body.dataset.mode = mode;
const isAddition = mode === "addition";
ui.modeAdd.classList.toggle("active", isAddition);
ui.modeFilter.classList.toggle("active", !isAddition);
ui.modeAdd.setAttribute("aria-pressed", String(isAddition));
ui.modeFilter.setAttribute("aria-pressed", String(!isAddition));
ui.modeEyebrow.textContent = isAddition
? "Model 01 / additive"
: "Model 02 / multiplicative";
ui.modeTitle.textContent = isAddition
? "Light addition"
: "Filter multiplication";
ui.modeCopy.textContent = isAddition
? "Add an independent light contribution to every base-color channel, then clip the result to the display range."
: "Scale every base-color channel by a tinted filter factor, then clip the result to the display range.";
ui.modeNote.textContent = isAddition
? "I = clip(C + L × energy)"
: "I = clip(C × [1 + density × (L/255 − 1)])";
ui.lightTitle.textContent = isAddition
? "Light contribution"
: "Filter tint";
ui.lightHint.textContent = isAddition
? "Added to the selected voxel"
: "Scales the selected voxel";
ui.lightMathLabel.textContent = isAddition ? "Light L" : "Filter L";
ui.strengthLabel.textContent = isAddition ? "Energy" : "Density";
ui.strength.max = isAddition ? "2" : "1";
updateEverything();
}
ui.modeAdd.addEventListener("click", () => setMode("addition"));
ui.modeFilter.addEventListener("click", () => setMode("filter"));
ui.strength.addEventListener("input", () => {
ui.strengthValue.value = Number(ui.strength.value).toFixed(2);
updateEverything();
});
[
[ui.lightR, ui.lightRVal, 0],
[ui.lightG, ui.lightGVal, 1],
[ui.lightB, ui.lightBVal, 2]
].forEach(([input, output, index]) => {
input.addEventListener("input", () => {
light[index] = Number(input.value);
output.value = input.value;
ui.lightPicker.value = rgbToHex(light);
ui.lightPreview.style.background = rgbCss(light);
ui.lightHex.textContent = rgbToHex(light);
updateEverything();
});
});
ui.lightPicker.addEventListener("input", () => {
light = hexToRgb(ui.lightPicker.value);
syncLightControls();
updateEverything();
});
[
[ui.baseR, ui.baseRVal, 0],
[ui.baseG, ui.baseGVal, 1],
[ui.baseB, ui.baseBVal, 2]
].forEach(([input, output, index]) => {
input.addEventListener("input", () => {
tiles[selectedIndex].base[index] = Number(input.value);
output.value = input.value;
ui.basePicker.value = rgbToHex(tiles[selectedIndex].base);
ui.basePreview.style.background = rgbCss(tiles[selectedIndex].base);
ui.baseHex.textContent = rgbToHex(tiles[selectedIndex].base);
updateEverything();
});
});
ui.basePicker.addEventListener("input", () => {
tiles[selectedIndex].base = hexToRgb(ui.basePicker.value);
syncBaseControls();
updateEverything();
});
function selectTile(index) {
selectedIndex = index;
syncBaseControls();
updateEverything();
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, keyboard, and selection
// -------------------------------------------------------------------------
let pointerDown = false;
let pointerId = null;
let pointerStartX = 0;
let pointerStartY = 0;
let previousX = 0;
let previousY = 0;
let pointerTravel = 0;
function pointerToNDC(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function tileAt(event) {
pointerToNDC(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(
tiles.map((tile) => tile.mesh),
false
);
return intersections.length ? intersections[0].object.userData.index : -1;
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerDown = true;
pointerId = event.pointerId;
pointerStartX = event.clientX;
pointerStartY = event.clientY;
previousX = event.clientX;
previousY = event.clientY;
pointerTravel = 0;
canvas.setPointerCapture(event.pointerId);
canvas.focus({ preventScroll: true });
});
canvas.addEventListener("pointermove", (event) => {
if (pointerDown && event.pointerId === pointerId) {
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
pointerTravel = Math.max(
pointerTravel,
Math.hypot(
event.clientX - pointerStartX,
event.clientY - pointerStartY
)
);
cameraState.targetAzimuth -= dx * 0.006;
cameraState.targetElevation = clamp(
cameraState.targetElevation + dy * 0.005,
0.25,
1.38
);
cameraTouched = true;
return;
}
const nextHovered = tileAt(event);
if (nextHovered !== hoveredIndex) {
hoveredIndex = nextHovered;
canvas.style.cursor = hoveredIndex >= 0 ? "pointer" : "grab";
updateMaterials();
}
});
canvas.addEventListener("pointerup", (event) => {
if (!pointerDown || event.pointerId !== pointerId) return;
pointerDown = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (pointerTravel < 5) {
const index = tileAt(event);
if (index >= 0) selectTile(index);
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
});
canvas.addEventListener("pointerleave", () => {
if (!pointerDown && hoveredIndex !== -1) {
hoveredIndex = -1;
updateMaterials();
}
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetDistance = clamp(
cameraState.targetDistance * Math.exp(event.deltaY * 0.001),
6.5,
23
);
cameraTouched = true;
},
{ passive: false }
);
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
cameraState.targetAzimuth -= 0.1;
break;
case "ArrowRight":
cameraState.targetAzimuth += 0.1;
break;
case "ArrowUp":
cameraState.targetElevation = clamp(
cameraState.targetElevation + 0.08,
0.25,
1.38
);
break;
case "ArrowDown":
cameraState.targetElevation = clamp(
cameraState.targetElevation - 0.08,
0.25,
1.38
);
break;
case "+":
case "=":
cameraState.targetDistance = clamp(
cameraState.targetDistance - 0.5,
6.5,
23
);
break;
case "-":
cameraState.targetDistance = clamp(
cameraState.targetDistance + 0.5,
6.5,
23
);
break;
case "r":
case "R":
resetCamera();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
cameraTouched = true;
}
});
function fittedDistance() {
const verticalHalfFov = THREE.MathUtils.degToRad(camera.fov / 2);
const horizontalHalfFov = Math.atan(
Math.tan(verticalHalfFov) * camera.aspect
);
return Math.max(11.5, 4.7 / Math.max(0.15, Math.tan(horizontalHalfFov)));
}
function resetCamera() {
cameraState.targetAzimuth = 0.62;
cameraState.targetElevation = 0.67;
cameraState.targetDistance = fittedDistance();
cameraTouched = false;
}
ui.resetView.addEventListener("click", resetCamera);
ui.shuffleBtn.addEventListener("click", () => {
tiles.forEach((tile) => {
tile.base = hslToRgb(
Math.random() * 360,
54 + Math.random() * 24,
42 + Math.random() * 18
);
});
syncBaseControls();
updateEverything();
ui.shuffleBtn.textContent = "Mixed";
setTimeout(() => {
ui.shuffleBtn.textContent = "Shuffle colors";
}, 900);
});
ui.copyBtn.addEventListener("click", async () => {
const tile = tiles[selectedIndex];
const strength = Number(ui.strength.value);
const result = calculate(tile.base, light, strength, mode);
const text =
`${ui.modeTitle}\n` +
`Base: ${tile.base.join(" / ")}\n` +
`${mode === "addition" ? "Light" : "Filter"}: ${light.join(" / ")}\n` +
`${ui.factorLabel}: ${
mode === "addition" ? result.gain : result.factor
}\n` +
`Raw: ${result.raw.map((v) => format(v, 2)).join(" / ")}\n` +
`Final: ${result.final.join(" / ")}\n` +
`HEX: ${rgbToHex(result.final)}`;
try {
await navigator.clipboard.writeText(text);
} catch {
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.style.position = "fixed";
textarea.style.opacity = "0";
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
textarea.remove();
}
ui.copyBtn.textContent = "Copied";
setTimeout(() => {
ui.copyBtn.textContent = "Copy calculation";
}, 1000);
});
// -------------------------------------------------------------------------
// 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();
if (!cameraTouched) {
cameraState.targetDistance = fittedDistance();
}
}
if ("ResizeObserver" in window) {
new ResizeObserver(resize).observe(stage);
} else {
window.addEventListener("resize", resize);
}
let previousTime = performance.now();
function animate(time) {
const delta = Math.min(0.05, (time - previousTime) / 1000);
previousTime = time;
const damping = 1 - Math.exp(-delta * 9);
cameraState.azimuth +=
(cameraState.targetAzimuth - cameraState.azimuth) * damping;
cameraState.elevation +=
(cameraState.targetElevation - cameraState.elevation) * damping;
cameraState.distance +=
(cameraState.targetDistance - cameraState.distance) * damping;
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
resize();
syncBaseControls();
syncLightControls();
setMode("addition");
requestAnimationFrame(animate);
</script>
</body>
</html>
```