Prompt specification
Build or customize the Three.js 3D scene "Resolution and Bias Counterfactual" 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: Resolution and Bias Counterfactual
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/resolution-and-bias-counterfactual-x2048
Scene Description Prompt:
Use Three.js from cdnjs to compare two measurement devices on the same objects using explicit resolution and bias settings. Show how finer display precision does not automatically remove systematic error. Deliver one self-contained HTML file with low-poly forms.
Working Three.js Starter (resolution-and-bias-counterfactual-x2048):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Measurement Resolution vs. Systematic Bias</title>
<style>
:root {
color-scheme: dark;
--bg: #080e19;
--panel: #101927;
--panel-2: #162235;
--line: #26364d;
--text: #e7edf7;
--muted: #91a2b9;
--a: #45d8c4;
--b: #ff9a61;
--good: #62df9b;
--warn: #ffc56d;
--bad: #ff7d86;
}
* {
box-sizing: border-box;
}
html, body {
margin: 0;
min-height: 100%;
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 {
min-height: 100vh;
}
header {
min-height: 82px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 16px 24px;
border-bottom: 1px solid var(--line);
background:
radial-gradient(circle at 15% 0%, rgba(69, 216, 196, 0.09), transparent 32%),
#0b1320;
}
.title-wrap {
display: flex;
align-items: center;
gap: 14px;
}
.logo {
width: 42px;
height: 42px;
display: grid;
place-items: center;
flex: 0 0 auto;
border: 1px solid rgba(69, 216, 196, 0.35);
border-radius: 12px;
color: var(--a);
background: rgba(69, 216, 196, 0.08);
font-weight: 800;
letter-spacing: -0.06em;
}
h1 {
margin: 0;
font-size: clamp(18px, 2vw, 25px);
line-height: 1.15;
letter-spacing: -0.025em;
}
.subtitle {
margin-top: 5px;
color: var(--muted);
font-size: 13px;
}
.header-badge {
flex: 0 0 auto;
padding: 8px 12px;
border: 1px solid #394960;
border-radius: 999px;
color: #c4d0e0;
background: #111b2a;
font-size: 12px;
font-weight: 700;
letter-spacing: 0.03em;
}
main {
display: grid;
grid-template-columns: minmax(0, 1fr) 430px;
height: calc(100vh - 82px);
min-height: 650px;
}
.viewport {
position: relative;
min-width: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 35%, #182b42 0%, #0b1422 55%, #080e19 100%);
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.scene-toolbar {
position: absolute;
z-index: 3;
top: 16px;
left: 16px;
right: 16px;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 12px;
pointer-events: none;
}
.scene-info,
.scene-actions {
pointer-events: auto;
border: 1px solid rgba(133, 158, 190, 0.25);
background: rgba(10, 17, 29, 0.8);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(10px);
}
.scene-info {
padding: 10px 13px;
border-radius: 11px;
font-size: 12px;
color: var(--muted);
}
.scene-info strong {
color: var(--text);
}
.scene-actions {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
padding: 8px;
border-radius: 11px;
}
.scene-actions button,
.scene-actions label {
border-radius: 7px;
font-size: 12px;
}
.scene-actions button {
cursor: pointer;
padding: 7px 10px;
border: 1px solid #35465f;
background: #172438;
}
.scene-actions button:hover {
background: #21324a;
}
.scene-actions label {
display: flex;
align-items: center;
gap: 5px;
padding: 7px 8px;
color: #c3cede;
cursor: pointer;
}
.scene-actions input {
accent-color: var(--a);
}
.scene-help {
position: absolute;
z-index: 2;
bottom: 15px;
left: 16px;
padding: 8px 11px;
border: 1px solid rgba(133, 158, 190, 0.2);
border-radius: 9px;
background: rgba(8, 14, 25, 0.78);
color: var(--muted);
font-size: 11px;
pointer-events: none;
}
.scene-legend {
position: absolute;
z-index: 2;
right: 16px;
bottom: 15px;
display: flex;
gap: 12px;
padding: 8px 11px;
border: 1px solid rgba(133, 158, 190, 0.2);
border-radius: 9px;
background: rgba(8, 14, 25, 0.78);
color: var(--muted);
font-size: 11px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-dot {
width: 8px;
height: 8px;
border-radius: 50%;
}
.panel {
overflow-y: auto;
padding: 18px;
border-left: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(25, 40, 61, 0.35), transparent 220px),
var(--panel);
scrollbar-width: thin;
scrollbar-color: #354962 transparent;
}
.section {
margin-bottom: 16px;
}
.section-title {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin: 0 0 9px;
color: #dce5f3;
font-size: 13px;
font-weight: 750;
letter-spacing: 0.015em;
}
.section-title small {
color: var(--muted);
font-size: 10px;
font-weight: 500;
}
.card {
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(20, 31, 48, 0.9);
box-shadow: 0 9px 24px rgba(0, 0, 0, 0.12);
}
.equation {
padding: 13px;
}
.equation code {
display: block;
color: #f2f6fc;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 13px;
line-height: 1.55;
}
.equation p {
margin: 8px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.device-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.device-card {
padding: 12px;
border-top: 3px solid var(--device-color);
}
.device-name {
display: flex;
align-items: center;
gap: 7px;
margin-bottom: 11px;
font-size: 13px;
font-weight: 800;
}
.device-name i {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--device-color);
box-shadow: 0 0 12px color-mix(in srgb, var(--device-color) 50%, transparent);
}
.field {
margin-top: 10px;
}
.field label {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.025em;
text-transform: uppercase;
}
.input-wrap {
display: flex;
align-items: center;
border: 1px solid #31435c;
border-radius: 8px;
background: #0c1523;
transition: border-color 150ms, box-shadow 150ms;
}
.input-wrap:focus-within {
border-color: var(--device-color);
box-shadow: 0 0 0 2px color-mix(in srgb, var(--device-color) 13%, transparent);
}
.input-wrap input {
width: 100%;
min-width: 0;
padding: 8px 9px;
border: 0;
outline: 0;
color: var(--text);
background: transparent;
font-size: 13px;
font-weight: 700;
}
.input-wrap span {
padding-right: 9px;
color: var(--muted);
font-size: 10px;
}
.field-note {
margin-top: 5px;
color: #73859d;
font-size: 9px;
line-height: 1.35;
}
.presets {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.preset {
min-height: 55px;
padding: 8px;
cursor: pointer;
border: 1px solid #31435c;
border-radius: 9px;
background: #121e2f;
color: #cbd6e5;
font-size: 10px;
line-height: 1.35;
}
.preset:hover {
border-color: #607894;
background: #1a2940;
}
.preset strong {
display: block;
margin-bottom: 3px;
color: #f2f6fc;
font-size: 11px;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.metric {
padding: 10px;
}
.metric-label {
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.metric-value {
margin-top: 4px;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 18px;
font-weight: 800;
}
.metric-value.a {
color: var(--a);
}
.metric-value.b {
color: var(--b);
}
.metric-note {
margin-top: 3px;
color: #70829a;
font-size: 9px;
}
.table-wrap {
overflow-x: auto;
border: 1px solid var(--line);
border-radius: 12px;
background: #0d1624;
}
table {
width: 100%;
min-width: 390px;
border-collapse: collapse;
font-size: 10px;
}
th {
padding: 9px 7px;
border-bottom: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
text-align: right;
text-transform: uppercase;
letter-spacing: 0.035em;
}
th:first-child,
td:first-child {
text-align: left;
}
td {
padding: 10px 7px;
border-bottom: 1px solid #1e2b3e;
text-align: right;
white-space: nowrap;
}
tbody tr {
cursor: pointer;
transition: background 120ms;
}
tbody tr:hover,
tbody tr.active {
background: rgba(74, 104, 145, 0.15);
}
tbody tr.active td:first-child {
box-shadow: inset 3px 0 var(--a);
}
tbody tr:last-child td {
border-bottom: 0;
}
.reading-a {
color: var(--a);
font-weight: 750;
}
.reading-b {
color: var(--b);
font-weight: 750;
}
.error-pos {
color: var(--bad);
}
.error-neg {
color: #75baff;
}
.error-zero {
color: var(--good);
}
.chart-wrap {
padding: 8px 8px 4px;
}
#errorChart {
display: block;
width: 100%;
height: 185px;
}
.chart-legend {
display: flex;
justify-content: center;
gap: 15px;
padding: 0 0 8px;
color: var(--muted);
font-size: 10px;
}
.chart-legend span {
display: flex;
align-items: center;
gap: 5px;
}
.chart-legend i {
width: 15px;
height: 2px;
background: currentColor;
}
.verdict {
padding: 13px;
border-left: 3px solid var(--warn);
}
.verdict.good {
border-left-color: var(--good);
}
.verdict.neutral {
border-left-color: #71849d;
}
.verdict h3 {
margin: 0 0 6px;
color: var(--warn);
font-size: 13px;
}
.verdict.good h3 {
color: var(--good);
}
.verdict.neutral h3 {
color: #a5b4c9;
}
.verdict p {
margin: 0;
color: #aab8ca;
font-size: 11px;
line-height: 1.55;
}
.verdict strong {
color: #edf3fb;
}
.loading {
position: absolute;
z-index: 10;
inset: 0;
display: grid;
place-items: center;
color: var(--muted);
background: #0b1422;
font-size: 13px;
}
@media (max-width: 920px) {
main {
display: block;
height: auto;
min-height: 0;
}
.viewport {
height: 58vh;
min-height: 430px;
}
.panel {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
.header-badge {
display: none;
}
}
@media (max-width: 560px) {
header {
min-height: 76px;
padding: 13px 15px;
}
.subtitle {
max-width: 280px;
font-size: 11px;
}
.panel {
padding: 13px;
}
.device-grid {
grid-template-columns: 1fr;
}
.presets {
grid-template-columns: 1fr;
}
.preset {
min-height: 0;
}
.scene-toolbar {
left: 10px;
right: 10px;
top: 10px;
}
.scene-info {
display: none;
}
.scene-actions {
margin-left: auto;
}
.scene-legend {
display: none;
}
}
</style>
</head>
<body>
<div class="app">
<header>
<div class="title-wrap">
<div class="logo">Δ</div>
<div>
<h1>Resolution Is Not Accuracy</h1>
<div class="subtitle">Compare two devices measuring the same low-poly objects.</div>
</div>
</div>
<div class="header-badge">INTERACTIVE MEASUREMENT LAB</div>
</header>
<main>
<section class="viewport">
<div id="scene">
<div class="loading" id="loading">Loading 3D scene…</div>
</div>
<div class="scene-toolbar">
<div class="scene-info">
Selected object: <strong id="selectedName">Hex block</strong><br>
Both devices are measuring this same object.
</div>
<div class="scene-actions">
<button id="resetView" type="button">Reset view</button>
<label><input id="showTruth" type="checkbox" checked> Truth labels</label>
<label><input id="showRays" type="checkbox" checked> Measurement rays</label>
</div>
</div>
<div class="scene-help">Drag to orbit · Scroll to zoom · Click an object to select</div>
<div class="scene-legend">
<div class="legend-item">
<span class="legend-dot" style="background:var(--a)"></span>
Device A
</div>
<div class="legend-item">
<span class="legend-dot" style="background:var(--b)"></span>
Device B
</div>
</div>
</section>
<aside class="panel">
<section class="section">
<h2 class="section-title">Measurement model</h2>
<div class="card equation">
<code>displayed = round(true size + bias, resolution)</code>
<p>
<strong>Resolution</strong> controls the display increment.
<strong>Bias</strong> is a signed, repeatable calibration offset applied before rounding.
</p>
</div>
</section>
<section class="section">
<h2 class="section-title">
Device settings
<small>Editable values</small>
</h2>
<div class="device-grid">
<div class="card device-card" style="--device-color:var(--a)">
<div class="device-name"><i></i> Device A</div>
<div class="field">
<label for="aResolution">Resolution / display step</label>
<div class="input-wrap">
<input id="aResolution" type="number" min="0.001" max="10" step="0.001" value="1">
<span>mm</span>
</div>
<div class="field-note">Larger steps mean coarser displayed digits.</div>
</div>
<div class="field">
<label for="aBias">Systematic bias</label>
<div class="input-wrap">
<input id="aBias" type="number" min="-10" max="10" step="0.1" value="0">
<span>mm</span>
</div>
<div class="field-note">Positive values make readings too high.</div>
</div>
</div>
<div class="card device-card" style="--device-color:var(--b)">
<div class="device-name"><i></i> Device B</div>
<div class="field">
<label for="bResolution">Resolution / display step</label>
<div class="input-wrap">
<input id="bResolution" type="number" min="0.001" max="10" step="0.001" value="0.1">
<span>mm</span>
</div>
<div class="field-note">Smaller steps show finer display precision.</div>
</div>
<div class="field">
<label for="bBias">Systematic bias</label>
<div class="input-wrap">
<input id="bBias" type="number" min="-10" max="10" step="0.1" value="0.6">
<span>mm</span>
</div>
<div class="field-note">This offset persists on every object.</div>
</div>
</div>
</div>
</section>
<section class="section">
<h2 class="section-title">Illustrative presets</h2>
<div class="presets">
<button class="preset" type="button" data-preset="precision">
<strong>Precision trap</strong>
B is finer, but biased
</button>
<button class="preset" type="button" data-preset="calibrated">
<strong>Calibrated B</strong>
Same resolution, no bias
</button>
<button class="preset" type="button" data-preset="mixed">
<strong>Mixed errors</strong>
Opposite signed biases
</button>
</div>
</section>
<section class="section">
<h2 class="section-title">
Error summary
<small>Across all three objects</small>
</h2>
<div class="metrics">
<div class="card metric">
<div class="metric-label">Device A · MAE</div>
<div class="metric-value a" id="aMae">—</div>
<div class="metric-note">Mean absolute error</div>
</div>
<div class="card metric">
<div class="metric-label">Device B · MAE</div>
<div class="metric-value b" id="bMae">—</div>
<div class="metric-note">Mean absolute error</div>
</div>
<div class="card metric">
<div class="metric-label">Device A · Mean error</div>
<div class="metric-value a" id="aMean">—</div>
<div class="metric-note">Signed average offset</div>
</div>
<div class="card metric">
<div class="metric-label">Device B · Mean error</div>
<div class="metric-value b" id="bMean">—</div>
<div class="metric-note">Signed average offset</div>
</div>
</div>
</section>
<section class="section">
<h2 class="section-title">
Same-object readings
<small>Click a row to inspect it</small>
</h2>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>Object</th>
<th>True</th>
<th>A</th>
<th>A error</th>
<th>B</th>
<th>B error</th>
</tr>
</thead>
<tbody id="readingsBody"></tbody>
</table>
</div>
</section>
<section class="section">
<h2 class="section-title">
Signed error profile
<small>Positive = reads high</small>
</h2>
<div class="card chart-wrap">
<canvas id="errorChart"></canvas>
<div class="chart-legend">
<span style="color:var(--a)"><i></i> Device A</span>
<span style="color:var(--b)"><i></i> Device B</span>
</div>
</div>
</section>
<section class="section">
<div id="verdict" class="card verdict">
<h3 id="verdictTitle">Evaluating…</h3>
<p id="verdictText"></p>
</div>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const COLORS = {
a: 0x45d8c4,
b: 0xff9a61,
truth: 0xd7e4f7
};
const objects = [
{
name: "Hex block",
short: "Block",
truth: 41.6,
x: -3.15,
kind: "box",
width: 2.0,
height: 2.25,
depth: 1.7,
color: 0x7187a8
},
{
name: "Hex drum",
short: "Drum",
truth: 27.4,
x: 0,
kind: "cylinder",
radius: 1.0,
height: 1.8,
color: 0x9b78b2
},
{
name: "Faceted stone",
short: "Stone",
truth: 18.2,
x: 3.15,
kind: "ico",
radius: 1.02,
height: 2.04,
color: 0xbc8767
}
];
const settings = {
a: { resolution: 1, bias: 0 },
b: { resolution: 0.1, bias: 0.6 }
};
let activeIndex = 0;
const sceneHost = document.getElementById("scene");
const loading = document.getElementById("loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1422);
scene.fog = new THREE.Fog(0x0b1422, 19, 34);
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const hemiLight = new THREE.HemisphereLight(0xb9d9ff, 0x172033, 2.0);
scene.add(hemiLight);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const rimLight = new THREE.PointLight(COLORS.a, 22, 16, 2);
rimLight.position.set(-7, 4, 1);
scene.add(rimLight);
const warmLight = new THREE.PointLight(COLORS.b, 18, 14, 2);
warmLight.position.set(7, 3, -1);
scene.add(warmLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0x0d1725,
roughness: 0.94,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.055;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(30, 30, 0x2b405c, 0x17263a);
grid.position.y = -0.035;
grid.material.transparent = true;
grid.material.opacity = 0.38;
scene.add(grid);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(10.2, 0.32, 3.6),
new THREE.MeshStandardMaterial({
color: 0x1b2a3e,
roughness: 0.78,
metalness: 0.16,
flatShading: true
})
);
platform.position.y = 0.16;
platform.castShadow = true;
platform.receiveShadow = true;
scene.add(platform);
const platformEdge = new THREE.LineSegments(
new THREE.EdgesGeometry(platform.geometry),
new THREE.LineBasicMaterial({
color: 0x405773,
transparent: true,
opacity: 0.55
})
);
platform.add(platformEdge);
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function createSprite(width, height) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 280;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 10;
return { canvas, ctx, texture, sprite };
}
function drawObjectLabel(def) {
const label = createSprite(2.35, 0.57);
const { ctx, texture } = label;
ctx.clearRect(0, 0, 640, 280);
roundedRectPath(ctx, 10, 10, 620, 260, 24);
ctx.fillStyle = "rgba(8, 15, 26, 0.86)";
ctx.fill();
ctx.strokeStyle = "rgba(181, 204, 232, 0.35)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.textAlign = "center";
ctx.fillStyle = "#91a5be";
ctx.font = "700 35px system-ui";
ctx.fillText(def.name.toUpperCase(), 320, 78);
ctx.fillStyle = "#e8f0fa";
ctx.font = "800 76px system-ui";
ctx.fillText(`${def.truth.toFixed(1)} mm true`, 320, 178);
ctx.fillStyle = "#6f849e";
ctx.font = "600 28px system-ui";
ctx.fillText("REFERENCE VALUE", 320, 226);
texture.needsUpdate = true;
return label.sprite;
}
function createDeviceDisplay(side, title, color) {
const display = createSprite(2.85, 1.25);
display.sprite.position.set(0, 3.78, 0);
display.sprite.userData = { side, title, color };
return display;
}
function drawDeviceDisplay(display, deviceSettings, reading, truth) {
const { ctx, texture, sprite } = display;
const { title, color } = sprite.userData;
const hex = "#" + color.toString(16).padStart(6, "0");
ctx.clearRect(0, 0, 640, 280);
roundedRectPath(ctx, 9, 9, 622, 262, 27);
ctx.fillStyle = "rgba(7, 13, 24, 0.94)";
ctx.fill();
ctx.strokeStyle = hex;
ctx.lineWidth = 5;
ctx.stroke();
ctx.textAlign = "left";
ctx.fillStyle = hex;
ctx.font = "800 34px system-ui";
ctx.fillText(title, 34, 58);
ctx.textAlign = "right";
ctx.fillStyle = "#71869f";
ctx.font = "700 24px system-ui";
ctx.fillText("ACTIVE OBJECT", 606, 56);
ctx.textAlign = "center";
ctx.fillStyle = "#f4f8fd";
ctx.font = "800 88px system-ui";
ctx.fillText(`${reading} mm`, 320, 156);
ctx.fillStyle = "#91a4bc";
ctx.font = "600 27px system-ui";
ctx.fillText(
`step ${deviceSettings.resolution.toFixed(3)} | bias ${formatSigned(deviceSettings.bias, 3)} mm`,
320,
215
);
ctx.fillStyle = "#60758f";
ctx.font = "600 23px system-ui";
ctx.fillText(`true ${truth.toFixed(1)} mm`, 320, 250);
texture.needsUpdate = true;
}
const objectMeshes = [];
const objectMaterials = [];
const truthLabels = [];
objects.forEach((def, index) => {
const bottom = 0.43;
const pad = new THREE.Mesh(
new THREE.CylinderGeometry(
def.kind === "ico" ? 1.37 : 1.42,
def.kind === "ico" ? 1.46 : 1.5,
0.08,
8
),
new THREE.MeshStandardMaterial({
color: 0x24364d,
roughness: 0.8,
metalness: 0.15,
flatShading: true
})
);
pad.position.set(def.x, 0.39, 0);
pad.receiveShadow = true;
scene.add(pad);
let geometry;
if (def.kind === "box") {
geometry = new THREE.BoxGeometry(def.width, def.height, def.depth);
} else if (def.kind === "cylinder") {
geometry = new THREE.CylinderGeometry(
def.radius,
def.radius,
def.height,
6,
1,
false
);
} else {
geometry = new THREE.IcosahedronGeometry(def.radius, 0);
}
const material = new THREE.MeshStandardMaterial({
color: def.color,
roughness: 0.67,
metalness: 0.08,
flatShading: true,
emissive: 0x000000,
emissiveIntensity: 0
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(def.x, bottom + def.height / 2, 0);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.objectIndex = index;
scene.add(mesh);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 18),
new THREE.LineBasicMaterial({
color: 0xd5e2f2,
transparent: true,
opacity: 0.25
})
);
mesh.add(edges);
const label = drawObjectLabel(def);
label.position.set(def.x, bottom + def.height + 0.55, 0);
scene.add(label);
truthLabels.push(label);
objectMeshes.push(mesh);
objectMaterials.push(material);
});
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(1.4, 0.035, 6, 40),
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.85
})
);
selectionRing.rotation.x = Math.PI / 2;
scene.add(selectionRing);
function createDevice(side, title, color) {
const group = new THREE.Group();
group.position.set(side * 5.55, 0, 0);
scene.add(group);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0x26374e,
roughness: 0.65,
metalness: 0.28,
flatShading: true
});
const accentMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.48,
metalness: 0.22,
flatShading: true
});
const base = new THREE.Mesh(
new THREE.BoxGeometry(1.65, 0.3, 1.4),
bodyMaterial
);
base.position.y = 0.15;
base.castShadow = true;
group.add(base);
const stem = new THREE.Mesh(
new THREE.BoxGeometry(0.42, 2.18, 0.42),
bodyMaterial
);
stem.position.y = 1.35;
stem.castShadow = true;
group.add(stem);
const head = new THREE.Mesh(
new THREE.BoxGeometry(1.24, 0.74, 0.78),
bodyMaterial
);
head.position.set(-side * 0.45, 2.45, 0);
head.castShadow = true;
group.add(head);
const accent = new THREE.Mesh(
new THREE.BoxGeometry(0.72, 0.12, 0.82),
accentMaterial
);
accent.position.set(-side * 0.45, 2.88, 0);
group.add(accent);
const sensor = new THREE.Mesh(
new THREE.CylinderGeometry(0.15, 0.22, 0.34, 6),
accentMaterial
);
sensor.rotation.z = Math.PI / 2;
sensor.position.set(-side * 0.95, 2.34, 0);
sensor.castShadow = true;
group.add(sensor);
const screen = new THREE.Mesh(
new THREE.BoxGeometry(0.8, 0.46, 0.06),
new THREE.MeshStandardMaterial({
color: 0x07101d,
roughness: 0.35,
metalness: 0.1
})
);
screen.position.set(-side * 0.45, 2.44, 0.42);
group.add(screen);
const display = createDeviceDisplay(side, title, color);
group.add(display.sprite);
group.updateMatrixWorld(true);
const sensorPosition = group.localToWorld(
new THREE.Vector3(-side * 0.98, 2.34, 0)
);
const beam = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
sensorPosition,
sensorPosition.clone()
]),
new THREE.LineDashedMaterial({
color,
dashSize: 0.24,
gapSize: 0.12,
transparent: true,
opacity: 0.9
})
);
beam.computeLineDistances();
scene.add(beam);
const endpoint = new THREE.Mesh(
new THREE.OctahedronGeometry(0.12, 0),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95
})
);
scene.add(endpoint);
return {
side,
title,
color,
group,
display,
beam,
endpoint
};
}
const deviceA = createDevice(-1, "DEVICE A", COLORS.a);
const deviceB = createDevice(1, "DEVICE B", COLORS.b);
function decimalsForStep(step) {
const text = String(Number(step.toFixed(6)));
if (!text.includes(".")) return 0;
return Math.min(6, text.split(".")[1].length);
}
function roundToStep(value, step) {
const units = value / step;
const roundedUnits =
Math.sign(units) * Math.round(Math.abs(units));
const decimals = decimalsForStep(step);
return Number((roundedUnits * step).toFixed(decimals + 2));
}
function formatFixed(value, decimals = 2) {
if (!Number.isFinite(value)) return "—";
return value.toFixed(decimals);
}
function formatSigned(value, decimals = 3) {
const sign = value >= 0 ? "+" : "−";
return sign + Math.abs(value).toFixed(decimals);
}
function readingFor(truth, deviceSettings) {
return roundToStep(
truth + deviceSettings.bias,
deviceSettings.resolution
);
}
function getReadings() {
return objects.map((object) => {
const a = readingFor(object.truth, settings.a);
const b = readingFor(object.truth, settings.b);
return {
object,
a,
b,
aError: a - object.truth,
bError: b - object.truth
};
});
}
function updateSelectionVisuals() {
const def = objects[activeIndex];
objectMaterials.forEach((material, index) => {
material.emissive.setHex(index === activeIndex ? 0x173c46 : 0x000000);
material.emissiveIntensity = index === activeIndex ? 0.36 : 0;
});
const ringRadius = def.kind === "ico" ? 1.39 : 1.46;
selectionRing.position.set(def.x, 0.475, 0);
selectionRing.userData.baseScale = ringRadius / 1.4;
}
function updateBeams() {
const def = objects[activeIndex];
const bottom = 0.43;
const target = new THREE.Vector3(
def.x,
bottom + def.height,
0
);
[deviceA, deviceB].forEach((device) => {
device.group.updateMatrixWorld(true);
const start = device.group.localToWorld(
new THREE.Vector3(-device.side * 0.98, 2.34, 0)
);
device.beam.geometry.setFromPoints([start, target]);
device.beam.computeLineDistances();
device.endpoint.position.copy(target);
});
}
function updateDeviceDisplays(readings) {
const active = readings[activeIndex];
drawDeviceDisplay(
deviceA.display,
settings.a,
formatFixed(active.a, decimalsForStep(settings.a.resolution)),
active.object.truth
);
drawDeviceDisplay(
deviceB.display,
settings.b,
formatFixed(active.b, decimalsForStep(settings.b.resolution)),
active.object.truth
);
}
function errorClass(value) {
if (Math.abs(value) < 0.0005) return "error-zero";
return value > 0 ? "error-pos" : "error-neg";
}
function updateTable(readings) {
const body = document.getElementById("readingsBody");
body.innerHTML = readings
.map((row, index) => {
const aDecimals = decimalsForStep(settings.a.resolution);
const bDecimals = decimalsForStep(settings.b.resolution);
return `
<tr data-index="${index}" class="${index === activeIndex ? "active" : ""}">
<td>${row.object.name}</td>
<td>${row.object.truth.toFixed(1)}</td>
<td class="reading-a">${formatFixed(row.a, aDecimals)}</td>
<td class="${errorClass(row.aError)}">${formatSigned(row.aError, 2)}</td>
<td class="reading-b">${formatFixed(row.b, bDecimals)}</td>
<td class="${errorClass(row.bError)}">${formatSigned(row.bError, 2)}</td>
</tr>
`;
})
.join("");
}
function updateMetrics(readings) {
const aErrors = readings.map((row) => row.aError);
const bErrors = readings.map((row) => row.bError);
const mean = (values) =>
values.reduce((sum, value) => sum + value, 0) / values.length;
const mae = (values) =>
mean(values.map((value) => Math.abs(value)));
document.getElementById("aMae").textContent =
`${formatSigned(mae(aErrors), 3)} mm`;
document.getElementById("bMae").textContent =
`${formatSigned(mae(bErrors), 3)} mm`;
document.getElementById("aMean").textContent =
`${formatSigned(mean(aErrors), 3)} mm`;
document.getElementById("bMean").textContent =
`${formatSigned(mean(bErrors), 3)} mm`;
}
function updateVerdict(readings) {
const verdict = document.getElementById("verdict");
const title = document.getElementById("verdictTitle");
const text = document.getElementById("verdictText");
const bErrors = readings.map((row) => row.bError);
const meanB = bErrors.reduce((sum, value) => sum + value, 0) /
bErrors.length;
const bIsFiner = settings.b.resolution < settings.a.resolution;
const bHasLargerBias =
Math.abs(settings.b.bias) > Math.abs(settings.a.bias) + 0.000001;
verdict.className = "card verdict";
if (bIsFiner && bHasLargerBias) {
verdict.classList.add("warn");
title.textContent = "More digits, but the same systematic offset";
text.innerHTML = `
Device B uses a
<strong>${settings.b.resolution.toFixed(3)} mm</strong> display step
instead of Device A's
<strong>${settings.a.resolution.toFixed(3)} mm</strong>.
However, its fixed bias is
<strong>${formatSigned(settings.b.bias, 2)} mm</strong>,
producing a mean signed error of
<strong>${formatSigned(meanB, 2)} mm</strong>.
Extra display digits do not calibrate that offset.
`;
} else if (bIsFiner && !bHasLargerBias) {
verdict.classList.add("good");
title.textContent = "Finer display and no larger bias";
text.innerHTML = `
Device B shows smaller increments without introducing a larger
fixed bias. Its repeated measurements are therefore closer to the
reference values in this example.
`;
} else {
verdict.classList.add("neutral");
title.textContent = "Resolution and bias describe different errors";
text.innerHTML = `
A display step of
<strong>${settings.b.resolution.toFixed(3)} mm</strong> only determines
how the result is rounded for display. The fixed bias of
<strong>${formatSigned(settings.b.bias, 2)} mm</strong> remains a
systematic error regardless of how many decimal places appear.
`;
}
}
function drawChart(readings) {
const canvas = document.getElementById("errorChart");
const width = Math.max(280, canvas.clientWidth);
const height = 185;
const dpr = Math.min(window.devicePixelRatio, 2);
canvas.width = Math.round(width * dpr);
canvas.height = Math.round(height * dpr);
const ctx = canvas.getContext("2d");
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, width, height);
const margin = {
left: 43,
right: 14,
top: 19,
bottom: 35
};
const plotWidth = width - margin.left - margin.right;
const plotHeight = height - margin.top - margin.bottom;
const allErrors = readings.flatMap((row) => [row.aError, row.bError]);
const maxAbs = Math.max(0.25, ...allErrors.map(Math.abs)) * 1.22;
const yFor = (value) =>
margin.top +
((maxAbs - value) / (2 * maxAbs)) * plotHeight;
const xFor = (index) =>
margin.left +
((index + 0.5) / readings.length) * plotWidth;
ctx.font = "600 10px system-ui";
ctx.textAlign = "right";
ctx.textBaseline = "middle";
[-maxAbs, -maxAbs / 2, 0, maxAbs / 2, maxAbs].forEach((value) => {
const y = yFor(value);
ctx.strokeStyle = value === 0 ? "#4a5c76" : "#223147";
ctx.lineWidth = value === 0 ? 1.4 : 1;
ctx.beginPath();
ctx.moveTo(margin.left, y);
ctx.lineTo(width - margin.right, y);
ctx.stroke();
ctx.fillStyle = "#73869f";
ctx.fillText(value.toFixed(1), margin.left - 8, y);
});
ctx.textAlign = "center";
ctx.textBaseline = "top";
readings.forEach((row, index) => {
ctx.fillStyle = "#8497af";
ctx.fillText(row.object.short, xFor(index), height - 25);
});
const series = [
{
key: "a",
color: "#45d8c4",
values: readings.map((row) => row.aError)
},
{
key: "b",
color: "#ff9a61",
values: readings.map((row) => row.bError)
}
];
series.forEach((seriesItem) => {
ctx.strokeStyle = seriesItem.color;
ctx.fillStyle = seriesItem.color;
ctx.lineWidth = 2;
ctx.setLineDash([]);
ctx.beginPath();
seriesItem.values.forEach((value, index) => {
const x = xFor(index);
const y = yFor(value);
if (index === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
ctx.stroke();
seriesItem.values.forEach((value, index) => {
const x = xFor(index);
const y = yFor(value);
const zeroY = yFor(0);
ctx.globalAlpha = 0.35;
ctx.beginPath();
ctx.moveTo(x, zeroY);
ctx.lineTo(x, y);
ctx.stroke();
ctx.globalAlpha = 1;
ctx.beginPath();
ctx.arc(x, y, 4, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = "#0d1624";
ctx.lineWidth = 2;
ctx.stroke();
});
});
ctx.setLineDash([]);
}
function updateAll() {
const readings = getReadings();
updateSelectionVisuals();
updateBeams();
updateDeviceDisplays(readings);
updateTable(readings);
updateMetrics(readings);
updateVerdict(readings);
drawChart(readings);
}
function selectObject(index) {
activeIndex = THREE.MathUtils.clamp(index, 0, objects.length - 1);
document.getElementById("selectedName").textContent =
objects[activeIndex].name;
updateAll();
}
document.getElementById("readingsBody").addEventListener("click", (event) => {
const row = event.target.closest("tr[data-index]");
if (row) selectObject(Number(row.dataset.index));
});
function updateSettingsFromInputs() {
const aResolution = parseFloat(document.getElementById("aResolution").value);
const aBias = parseFloat(document.getElementById("aBias").value);
const bResolution = parseFloat(document.getElementById("bResolution").value);
const bBias = parseFloat(document.getElementById("bBias").value);
settings.a.resolution = THREE.MathUtils.clamp(
Number.isFinite(aResolution) ? aResolution : 1,
0.001,
10
);
settings.a.bias = THREE.MathUtils.clamp(
Number.isFinite(aBias) ? aBias : 0,
-10,
10
);
settings.b.resolution = THREE.MathUtils.clamp(
Number.isFinite(bResolution) ? bResolution : 0.1,
0.001,
10
);
settings.b.bias = THREE.MathUtils.clamp(
Number.isFinite(bBias) ? bBias : 0,
-10,
10
);
updateAll();
}
[
"aResolution",
"aBias",
"bResolution",
"bBias"
].forEach((id) => {
document.getElementById(id).addEventListener("input", updateSettingsFromInputs);
});
const presets = {
precision: {
aResolution: 1,
aBias: 0,
bResolution: 0.1,
bBias: 0.6
},
calibrated: {
aResolution: 1,
aBias: 0,
bResolution: 0.1,
bBias: 0
},
mixed: {
aResolution: 0.5,
aBias: -0.25,
bResolution: 0.05,
bBias: 0.4
}
};
document.querySelectorAll("[data-preset]").forEach((button) => {
button.addEventListener("click", () => {
const preset = presets[button.dataset.preset];
Object.entries(preset).forEach(([id, value]) => {
document.getElementById(id).value = value;
});
updateSettingsFromInputs();
});
});
document.getElementById("showTruth").addEventListener("change", (event) => {
truthLabels.forEach((label) => {
label.visible = event.target.checked;
});
});
document.getElementById("showRays").addEventListener("change", (event) => {
[deviceA, deviceB].forEach((device) => {
device.beam.visible = event.target.checked;
device.endpoint.visible = event.target.checked;
});
});
// Custom camera controls: no addons are imported.
const cameraTarget = new THREE.Vector3(0, 1.25, 0);
const desiredView = {
theta: 0.43,
phi: 1.03,
radius: 16.5
};
const currentView = {
theta: desiredView.theta,
phi: desiredView.phi,
radius: desiredView.radius
};
function updateCamera() {
const sinPhi = Math.sin(currentView.phi);
camera.position.set(
cameraTarget.x +
currentView.radius * sinPhi * Math.sin(currentView.theta),
cameraTarget.y + currentView.radius * Math.cos(currentView.phi),
cameraTarget.z +
currentView.radius * sinPhi * Math.cos(currentView.theta)
);
camera.lookAt(cameraTarget);
}
document.getElementById("resetView").addEventListener("click", () => {
desiredView.theta = 0.43;
desiredView.phi = 1.03;
desiredView.radius = 16.5;
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = {
x: event.clientX,
y: event.clientY
};
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (Math.abs(dx) + Math.abs(dy) > 2) {
desiredView.theta -= dx * 0.007;
desiredView.phi = THREE.MathUtils.clamp(
desiredView.phi - dy * 0.006,
0.48,
1.38
);
pointerDown.x = event.clientX;
pointerDown.y = event.clientY;
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointerDown) return;
const moved =
Math.abs(event.clientX - pointerDown.x) +
Math.abs(event.clientY - pointerDown.y);
pointerDown = null;
if (moved < 6) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(objectMeshes, false);
if (hits.length) {
selectObject(hits[0].object.userData.objectIndex);
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = null;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredView.radius = THREE.MathUtils.clamp(
desiredView.radius + event.deltaY * 0.012,
10.5,
24
);
},
{ passive: false }
);
function resizeScene() {
const width = sceneHost.clientWidth;
const height = sceneHost.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.fov = camera.aspect < 1 ? 55 : 45;
camera.updateProjectionMatrix();
drawChart(getReadings());
}
const sceneResizeObserver = new ResizeObserver(resizeScene);
sceneResizeObserver.observe(sceneHost);
const chartResizeObserver = new ResizeObserver(() => {
drawChart(getReadings());
});
chartResizeObserver.observe(
document.getElementById("errorChart").parentElement
);
updateAll();
resizeScene();
loading.remove();
function animate() {
const damping = 0.11;
currentView.theta +=
(desiredView.theta - currentView.theta) * damping;
currentView.phi +=
(desiredView.phi - currentView.phi) * damping;
currentView.radius +=
(desiredView.radius - currentView.radius) * damping;
updateCamera();
const pulse = 1 + Math.sin(performance.now() * 0.0032) * 0.035;
const baseScale = selectionRing.userData.baseScale || 1;
selectionRing.scale.set(baseScale * pulse, baseScale * pulse, 1);
[deviceA, deviceB].forEach((device, index) => {
const wave = 1 + Math.sin(performance.now() * 0.004 + index) * 0.12;
device.endpoint.scale.setScalar(wave);
});
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```