Prompt specification
Build or customize the Three.js 3D scene "Repeated Measurements With Synthetic Error" 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: Repeated Measurements With Synthetic Error
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/repeated-measurements-with-synthetic-error-x2046
Scene Description Prompt:
Repeated measurements with synthetic error. Create a caliper-like schematic measuring one fixed block repeatedly under a stated synthetic noise model. Plot readings and distinguish random variation from a fixed bias. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (repeated-measurements-with-synthetic-error-x2046):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Caliper — Repeated Synthetic Measurements</title>
<style>
:root {
color-scheme: dark;
--bg: #06101b;
--panel: #0b1726;
--panel-2: #0e1d2e;
--line: #21364b;
--text: #e8f1f7;
--muted: #91a7b9;
--cyan: #42d9e8;
--cyan-soft: rgba(66, 217, 232, 0.15);
--orange: #ffad4a;
--orange-soft: rgba(255, 173, 74, 0.16);
--green: #6ee7a2;
--white: #f5f8fb;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-width: 320px;
min-height: 100vh;
overflow-x: hidden;
background:
radial-gradient(circle at 15% 0%, rgba(37, 104, 133, 0.16), transparent 34rem),
radial-gradient(circle at 95% 20%, rgba(255, 150, 55, 0.08), transparent 28rem),
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 {
border: 1px solid var(--line);
border-radius: 10px;
background: #14263a;
color: var(--text);
padding: 10px 14px;
cursor: pointer;
font-weight: 700;
transition: border-color 150ms ease, background 150ms ease, transform 150ms ease;
}
button:hover {
border-color: #3a607e;
background: #193149;
}
button:active {
transform: translateY(1px);
}
button:focus-visible,
input:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
button.primary {
border-color: rgba(66, 217, 232, 0.55);
background: linear-gradient(135deg, #147383, #105767);
color: #ecfdff;
}
button.primary:hover {
background: linear-gradient(135deg, #198b9e, #146678);
}
.page {
width: min(1540px, 100%);
margin: 0 auto;
padding: 20px;
}
.page-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
margin-bottom: 16px;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
position: relative;
width: 42px;
height: 42px;
flex: 0 0 auto;
}
.brand-mark span {
position: absolute;
width: 16px;
height: 16px;
border-radius: 3px;
background: var(--orange);
box-shadow: 0 0 18px rgba(255, 173, 74, 0.18);
}
.brand-mark span:nth-child(1) { left: 0; top: 0; }
.brand-mark span:nth-child(2) { right: 0; top: 0; }
.brand-mark span:nth-child(3) { left: 0; bottom: 0; }
.brand-mark span:nth-child(4) { right: 0; bottom: 0; }
h1 {
margin: 0;
font-size: clamp(20px, 2.3vw, 30px);
letter-spacing: -0.035em;
line-height: 1.05;
}
.subtitle {
margin: 5px 0 0;
color: var(--muted);
font-size: 13px;
}
.status {
display: inline-flex;
align-items: center;
gap: 8px;
min-width: 112px;
justify-content: center;
padding: 8px 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(13, 29, 46, 0.8);
color: var(--muted);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.1em;
}
.status::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
.status[data-state="running"] {
color: var(--green);
}
.status[data-state="complete"] {
color: var(--cyan);
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1.35fr) minmax(440px, 0.9fr);
gap: 16px;
align-items: start;
}
.card {
border: 1px solid var(--line);
border-radius: 16px;
background: linear-gradient(145deg, rgba(15, 31, 48, 0.98), rgba(8, 20, 33, 0.98));
box-shadow: 0 22px 55px rgba(0, 0, 0, 0.18);
overflow: hidden;
}
.scene-card {
position: sticky;
top: 14px;
height: calc(100vh - 112px);
min-height: 650px;
max-height: 850px;
}
#sceneHost {
position: absolute;
inset: 0;
background: #07121f;
}
#sceneHost canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.scene-top,
.scene-bottom {
position: absolute;
z-index: 3;
left: 16px;
right: 16px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.scene-top {
top: 16px;
}
.scene-bottom {
bottom: 16px;
align-items: flex-end;
}
.scene-tag,
.scene-chip {
border: 1px solid rgba(102, 137, 164, 0.24);
border-radius: 10px;
background: rgba(6, 16, 27, 0.78);
backdrop-filter: blur(10px);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.16);
}
.scene-tag {
padding: 10px 12px;
}
.scene-tag strong {
display: block;
font-size: 11px;
letter-spacing: 0.12em;
}
.scene-tag span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 11px;
}
.scene-chip {
padding: 8px 10px;
color: var(--muted);
font-size: 11px;
white-space: nowrap;
}
.scene-chip strong {
color: var(--orange);
}
.view-button {
pointer-events: auto;
background: rgba(10, 25, 40, 0.9);
backdrop-filter: blur(10px);
}
.scene-legend {
display: flex;
gap: 13px;
flex-wrap: wrap;
margin-top: 10px;
color: var(--muted);
font-size: 11px;
}
.scene-legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
display: inline-block;
}
.dot.fixed { background: var(--orange); }
.dot.random { background: var(--cyan); }
.dot.block { background: #f8b84e; }
.side {
display: grid;
gap: 12px;
}
.side .card {
padding: 16px;
}
.card-title-row {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-bottom: 12px;
}
.card-title {
margin: 0;
font-size: 14px;
letter-spacing: -0.01em;
}
.eyebrow {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.equation {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 6px;
padding: 13px 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: #081522;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 14px;
}
.equation .bias {
color: var(--orange);
}
.equation .noise {
color: var(--cyan);
}
.model-note {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.model-note strong {
color: var(--text);
}
.controls {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.speed-control {
grid-column: 1 / -1;
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 10px;
color: var(--muted);
font-size: 12px;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
}
#speedValue {
min-width: 52px;
color: var(--text);
text-align: right;
font-family: ui-monospace, monospace;
}
.seed-line {
margin-top: 11px;
color: var(--muted);
font-size: 11px;
display: flex;
justify-content: space-between;
gap: 12px;
}
.seed-line code {
color: var(--cyan);
}
.stats {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
}
.stat {
min-width: 0;
padding: 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(8, 20, 33, 0.65);
}
.stat-label {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
white-space: nowrap;
}
.stat-value {
display: block;
overflow: hidden;
text-overflow: ellipsis;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 15px;
font-weight: 800;
white-space: nowrap;
}
.stat-value.cyan { color: var(--cyan); }
.stat-value.orange { color: var(--orange); }
.chart-wrap {
position: relative;
width: 100%;
height: 342px;
}
#chart {
display: block;
width: 100%;
height: 100%;
}
.chart-legend {
display: flex;
align-items: center;
gap: 13px;
flex-wrap: wrap;
margin-top: 7px;
color: var(--muted);
font-size: 10px;
}
.chart-legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 14px;
height: 8px;
border-radius: 2px;
display: inline-block;
}
.swatch.bias { background: var(--orange-soft); border: 1px solid var(--orange); }
.swatch.random { background: var(--cyan-soft); border: 1px solid var(--cyan); }
.swatch.true {
height: 0;
border-top: 2px dashed var(--white);
border-radius: 0;
}
.method-note {
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.method-note strong {
color: var(--text);
}
@media (max-width: 1050px) {
.workspace {
grid-template-columns: 1fr;
}
.scene-card {
position: relative;
top: 0;
height: 620px;
min-height: 0;
}
}
@media (max-width: 600px) {
.page {
padding: 12px;
}
.page-header {
align-items: flex-start;
}
.subtitle {
max-width: 250px;
}
.status {
min-width: 94px;
padding: 7px 9px;
font-size: 10px;
}
.scene-card {
height: 520px;
}
.scene-top,
.scene-bottom {
left: 10px;
right: 10px;
}
.scene-chip {
display: none;
}
.stats {
grid-template-columns: repeat(2, 1fr);
}
.controls {
grid-template-columns: 1fr;
}
.speed-control {
grid-column: auto;
grid-template-columns: auto 1fr auto;
}
}
</style>
</head>
<body>
<div class="page">
<header class="page-header">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<span></span><span></span><span></span><span></span>
</div>
<div>
<h1>Voxel Caliper Lab</h1>
<p class="subtitle">Repeated measurements of one fixed block under controlled synthetic error</p>
</div>
</div>
<div id="status" class="status" data-state="running">RUNNING</div>
</header>
<main class="workspace">
<section class="card scene-card" aria-label="Interactive 3D caliper schematic">
<div id="sceneHost"></div>
<div class="scene-top">
<div class="scene-tag">
<strong>3D MEASUREMENT SCHEMATIC</strong>
<span>The orange voxel block remains fixed.</span>
<div class="scene-legend">
<span><i class="dot block"></i>fixed object</span>
<span><i class="dot fixed"></i>fixed bias</span>
<span><i class="dot random"></i>random variation</span>
</div>
</div>
<div class="scene-chip">3D jaw motion exaggerated <strong>×4</strong></div>
</div>
<div class="scene-bottom">
<div class="scene-chip">
Drag to orbit · Shift/right-drag to pan · Scroll to zoom
</div>
<button id="resetView" class="view-button" type="button">Reset view</button>
</div>
</section>
<aside class="side">
<section class="card">
<div class="card-title-row">
<h2 class="card-title">Synthetic error model</h2>
<span class="eyebrow">36 trials</span>
</div>
<div class="equation" aria-label="Measurement model">
x<sub>i</sub> = 32.000
<span class="bias">+ 0.120</span>
<span class="noise">+ ε<sub>i</sub></span>
</div>
<p class="model-note">
<strong>Fixed bias:</strong> every reading is shifted by
<strong style="color: var(--orange)">+0.120 mm</strong>.
<strong>Random variation:</strong>
ε<sub>i</sub> ~ Normal(0, 0.035² mm), generated with a seeded
Box–Muller transform.
</p>
</section>
<section class="card">
<div class="card-title-row">
<h2 class="card-title">Measurement sequence</h2>
<span class="eyebrow">Interactive</span>
</div>
<div class="controls">
<button id="playBtn" class="primary" type="button">Pause</button>
<button id="newBtn" type="button">New synthetic run</button>
<label class="speed-control" for="speed">
<span>Rate</span>
<input id="speed" type="range" min="1" max="10" step="0.5" value="4" />
<span id="speedValue">4.0/s</span>
</label>
</div>
<div class="seed-line">
<span>Reproducible seed</span>
<code id="seedValue">18473</code>
</div>
</section>
<section class="card">
<div class="card-title-row">
<h2 class="card-title">Observed results</h2>
<span id="trialCount" class="eyebrow">0 / 36 trials</span>
</div>
<div class="stats">
<div class="stat">
<span class="stat-label">Mean</span>
<span id="meanValue" class="stat-value cyan">—</span>
</div>
<div class="stat">
<span class="stat-label">Sample SD</span>
<span id="sdValue" class="stat-value">—</span>
</div>
<div class="stat">
<span class="stat-label">Observed bias</span>
<span id="biasValue" class="stat-value orange">—</span>
</div>
<div class="stat">
<span class="stat-label">True width</span>
<span class="stat-value">32.000</span>
</div>
</div>
</section>
<section class="card">
<div class="card-title-row">
<h2 class="card-title">Reading plot</h2>
<span class="eyebrow">millimetres</span>
</div>
<div class="chart-wrap">
<canvas id="chart" aria-label="Scatter plot of repeated caliper readings"></canvas>
</div>
<div class="chart-legend">
<span><i class="swatch bias"></i>fixed bias</span>
<span><i class="swatch random"></i>random ±1σ</span>
<span><i class="swatch true"></i>true width</span>
</div>
</section>
<section class="card method-note">
<strong>How to read it:</strong> each cyan segment joins a reading
to the biased center, 32.120 mm. The orange band is the constant
0.120 mm offset from truth. Cyan scatter around that center is the
synthetic random component. The numerical plot is not exaggerated.
</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";
// ------------------------------------------------------------
// Model constants
// ------------------------------------------------------------
const TRUE_WIDTH = 32.000;
const FIXED_BIAS = 0.120;
const RANDOM_SIGMA = 0.035;
const TRIAL_COUNT = 36;
const VISUAL_GAIN = 4;
const EXPECTED_CENTER = TRUE_WIDTH + FIXED_BIAS;
const dom = {
host: document.getElementById("sceneHost"),
status: document.getElementById("status"),
playBtn: document.getElementById("playBtn"),
newBtn: document.getElementById("newBtn"),
speed: document.getElementById("speed"),
speedValue: document.getElementById("speedValue"),
seedValue: document.getElementById("seedValue"),
trialCount: document.getElementById("trialCount"),
meanValue: document.getElementById("meanValue"),
sdValue: document.getElementById("sdValue"),
biasValue: document.getElementById("biasValue"),
resetView: document.getElementById("resetView"),
chart: document.getElementById("chart")
};
// ------------------------------------------------------------
// Seeded random number generation and synthetic measurements
// ------------------------------------------------------------
function mulberry32(seed) {
let a = seed >>> 0;
return function () {
a += 0x6d2b79f5;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function generateReadings(seed) {
const random = mulberry32(seed);
const values = [];
for (let i = 0; i < TRIAL_COUNT; i++) {
// Box–Muller transform: Z ~ N(0, 1).
const u1 = Math.max(random(), 1e-12);
const u2 = random();
const z = Math.sqrt(-2 * Math.log(u1)) *
Math.cos(2 * Math.PI * u2);
values.push(
TRUE_WIDTH + FIXED_BIAS + RANDOM_SIGMA * z
);
}
return values;
}
let seed = 18473;
let readings = generateReadings(seed);
let currentIndex = -1;
let running = true;
let lastAdvance = performance.now();
// ------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07121f);
scene.fog = new THREE.Fog(0x07121f, 17, 31);
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
} catch (error) {
dom.host.innerHTML =
'<div style="padding:2rem;color:#ffb4a6">WebGL is required to display this schematic.</div>';
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
dom.host.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
39,
1,
0.1,
100
);
const cameraState = {
theta: 0.38,
phi: 1.05,
radius: 15.2,
target: new THREE.Vector3(0, 0.25, 0)
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.theta = 0.38;
cameraState.phi = 1.05;
cameraState.radius = 15.2;
cameraState.target.set(0, 0.25, 0);
updateCamera();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
0xb8eaff,
0x142033,
1.8
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(6, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x35d9ef, 18, 18, 2);
cyanLight.position.set(-7, 3, 5);
scene.add(cyanLight);
const orangeLight = new THREE.PointLight(0xff9b3e, 14, 16, 2);
orangeLight.position.set(7, 2, 3);
scene.add(orangeLight);
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x0d1c2b,
roughness: 0.88,
metalness: 0.08
});
const platform = new THREE.Mesh(
new THREE.BoxGeometry(16.5, 0.28, 8.2),
platformMaterial
);
platform.position.set(0, -2.27, 0);
platform.receiveShadow = true;
scene.add(platform);
const grid = new THREE.GridHelper(
18,
36,
0x31536a,
0x193044
);
grid.position.y = -2.12;
grid.material.transparent = true;
grid.material.opacity = 0.56;
scene.add(grid);
const LEFT_EDGE = -3.95;
const RIGHT_EDGE = 3.95;
// Fixed voxel block: 8 × 4 × 4 visible cubes.
const voxelGeometry = new THREE.BoxGeometry(0.90, 0.90, 0.90);
const voxelMaterials = [
new THREE.MeshStandardMaterial({
color: 0xf59e0b,
roughness: 0.57,
metalness: 0.08
}),
new THREE.MeshStandardMaterial({
color: 0xffb52f,
roughness: 0.52,
metalness: 0.07
}),
new THREE.MeshStandardMaterial({
color: 0xe88908,
roughness: 0.62,
metalness: 0.08
}),
new THREE.MeshStandardMaterial({
color: 0xffc452,
roughness: 0.48,
metalness: 0.06
})
];
const blockGroup = new THREE.Group();
scene.add(blockGroup);
for (let ix = 0; ix < 8; ix++) {
for (let iy = 0; iy < 4; iy++) {
for (let iz = 0; iz < 4; iz++) {
const colorIndex =
(ix * 3 + iy * 2 + iz) % voxelMaterials.length;
const voxel = new THREE.Mesh(
voxelGeometry,
voxelMaterials[colorIndex]
);
voxel.position.set(
-3.5 + ix,
-0.75 + iy,
-1.5 + iz
);
voxel.castShadow = true;
voxel.receiveShadow = true;
blockGroup.add(voxel);
}
}
}
// Caliper rail and frame.
const railMaterial = new THREE.MeshStandardMaterial({
color: 0xc5d3dd,
roughness: 0.29,
metalness: 0.72
});
const darkMetal = new THREE.MeshStandardMaterial({
color: 0x263b4b,
roughness: 0.38,
metalness: 0.68
});
const tealMetal = new THREE.MeshStandardMaterial({
color: 0x168b9a,
roughness: 0.31,
metalness: 0.58
});
function addBox(parent, size, position, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const caliper = new THREE.Group();
scene.add(caliper);
addBox(
caliper,
[13.6, 0.28, 0.66],
[0, -1.75, 0],
railMaterial
);
// Fixed jaw touching the left edge of the voxel block.
addBox(
caliper,
[0.36, 4.15, 0.72],
[LEFT_EDGE - 0.30, 0.40, 0],
railMaterial
);
addBox(
caliper,
[0.12, 0.34, 0.96],
[LEFT_EDGE - 0.06, 0.34, 0],
darkMetal
);
// Small rail graduations.
const tickMaterial = new THREE.MeshBasicMaterial({
color: 0x38566b
});
for (let i = -13; i <= 13; i++) {
const x = i * 0.5;
const major = i % 2 === 0;
addBox(
caliper,
[major ? 0.065 : 0.035, major ? 0.17 : 0.10, 0.035],
[x, -1.58, 0.35],
tickMaterial
);
}
// Moving jaw assembly. Its origin is the right measuring contact.
const movingJaw = new THREE.Group();
scene.add(movingJaw);
addBox(
movingJaw,
[0.36, 4.15, 0.72],
[0.30, 0.40, 0],
tealMetal
);
addBox(
movingJaw,
[0.12, 0.34, 0.96],
[0.06, 0.34, 0],
darkMetal
);
addBox(
movingJaw,
[1.18, 0.78, 1.12],
[0.60, -1.69, 0],
tealMetal
);
addBox(
movingJaw,
[1.02, 0.50, 0.10],
[0.60, -1.47, 0.59],
darkMetal
);
// A faint datum at the biased center makes the fixed offset visible.
const biasDatum = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(
RIGHT_EDGE + FIXED_BIAS * VISUAL_GAIN,
-1.35,
2.01
),
new THREE.Vector3(
RIGHT_EDGE + FIXED_BIAS * VISUAL_GAIN,
2.62,
2.01
)
]),
new THREE.LineBasicMaterial({
color: 0x42d9e8,
transparent: true,
opacity: 0.45
})
);
scene.add(biasDatum);
// Caliper's digital display, generated with a CanvasTexture.
const screenCanvas = document.createElement("canvas");
screenCanvas.width = 512;
screenCanvas.height = 220;
const screenContext = screenCanvas.getContext("2d");
const screenTexture = new THREE.CanvasTexture(screenCanvas);
screenTexture.colorSpace = THREE.SRGBColorSpace;
const screen = new THREE.Mesh(
new THREE.PlaneGeometry(0.91, 0.39),
new THREE.MeshBasicMaterial({
map: screenTexture,
side: THREE.DoubleSide
})
);
screen.position.set(0.60, -1.46, 0.647);
movingJaw.add(screen);
function drawScreen(reading, index) {
const c = screenContext;
c.clearRect(0, 0, screenCanvas.width, screenCanvas.height);
c.fillStyle = "#061210";
c.fillRect(0, 0, screenCanvas.width, screenCanvas.height);
c.fillStyle = "#6ee7a2";
c.font = "700 76px ui-monospace, monospace";
c.textAlign = "center";
c.fillText(
reading === null ? "----" : reading.toFixed(3),
256,
103
);
c.fillStyle = "#77b7a1";
c.font = "600 27px ui-monospace, monospace";
c.fillText(
index >= 0
? `MM TRIAL ${String(index + 1).padStart(2, "0")}/${TRIAL_COUNT}`
: "MM READY",
256,
157
);
c.fillStyle = "#ffad4a";
c.font = "600 23px ui-monospace, monospace";
c.fillText(
`FIXED BIAS +${FIXED_BIAS.toFixed(3)}`,
256,
198
);
screenTexture.needsUpdate = true;
}
drawScreen(null, -1);
// Dimension line spanning the two jaw contacts.
const dimensionPositions = new Float32Array([
LEFT_EDGE, -1.30, 2.03,
RIGHT_EDGE + FIXED_BIAS * VISUAL_GAIN, -1.30, 2.03
]);
const dimensionGeometry = new THREE.BufferGeometry();
dimensionGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dimensionPositions, 3)
);
const dimensionLine = new THREE.Line(
dimensionGeometry,
new THREE.LineBasicMaterial({ color: 0x42d9e8 })
);
scene.add(dimensionLine);
const dimensionTickMaterial = new THREE.MeshBasicMaterial({
color: 0x42d9e8
});
const leftDimensionTick = new THREE.Mesh(
new THREE.BoxGeometry(0.055, 0.28, 0.055),
dimensionTickMaterial
);
leftDimensionTick.position.set(LEFT_EDGE, -1.30, 2.03);
scene.add(leftDimensionTick);
const rightDimensionTick = new THREE.Mesh(
new THREE.BoxGeometry(0.055, 0.28, 0.055),
dimensionTickMaterial
);
rightDimensionTick.position.set(
RIGHT_EDGE + FIXED_BIAS * VISUAL_GAIN,
-1.30,
2.03
);
scene.add(rightDimensionTick);
// ------------------------------------------------------------
// Canvas-based labels
// ------------------------------------------------------------
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeLabel(initialText, color = "#e8f1f7", width = 3.0) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 160;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * (160 / 768), 1);
function set(text, secondary = "") {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgba(5, 15, 25, 0.84)";
roundedRect(ctx, 8, 8, 752, 144, 22);
ctx.fill();
ctx.strokeStyle = "rgba(111, 148, 173, 0.34)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.textAlign = "center";
ctx.fillStyle = color;
ctx.font = "700 49px ui-monospace, monospace";
ctx.fillText(text, 384, secondary ? 73 : 95);
if (secondary) {
ctx.fillStyle = "#9db2c2";
ctx.font = "600 25px ui-monospace, monospace";
ctx.fillText(secondary, 384, 119);
}
texture.needsUpdate = true;
}
set(initialText);
return { sprite, set };
}
const blockLabel = makeLabel(
"FIXED VOXEL BLOCK",
"#ffd079",
3.65
);
blockLabel.sprite.position.set(-0.2, 3.12, 1.5);
scene.add(blockLabel.sprite);
const caliperLabel = makeLabel(
"MOVING CALIPER",
"#65e6f1",
2.75
);
caliperLabel.sprite.position.set(4.0, -2.68, 1.2);
scene.add(caliperLabel.sprite);
const readingLabel = makeLabel(
"READY",
"#65e6f1",
2.45
);
readingLabel.sprite.position.set(0, -2.83, 2.0);
scene.add(readingLabel.sprite);
// ------------------------------------------------------------
// Direct camera controls: orbit, pan and zoom
// ------------------------------------------------------------
let dragState = null;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
dragState = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY,
pan: event.button === 2 || event.shiftKey
};
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragState || event.pointerId !== dragState.pointerId) return;
const dx = event.clientX - dragState.x;
const dy = event.clientY - dragState.y;
dragState.x = event.clientX;
dragState.y = event.clientY;
if (dragState.pan) {
const scale = cameraState.radius * 0.0015;
const right = new THREE.Vector3(
Math.cos(cameraState.theta),
0,
-Math.sin(cameraState.theta)
);
cameraState.target.addScaledVector(right, -dx * scale);
cameraState.target.y += dy * scale;
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-6,
6
);
cameraState.target.y = THREE.MathUtils.clamp(
cameraState.target.y,
-3,
5
);
} else {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.006,
0.28,
1.55
);
}
updateCamera();
});
function endDrag(event) {
if (!dragState || event.pointerId !== dragState.pointerId) return;
dragState = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8.0,
27.0
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
dom.resetView.addEventListener("click", resetCamera);
// ------------------------------------------------------------
// Numerical plot
// ------------------------------------------------------------
const chart = dom.chart;
const chartContext = chart.getContext("2d");
function niceTicks(min, max, desired = 6) {
const rawStep = (max - min) / desired;
const magnitude = Math.pow(10, Math.floor(Math.log10(rawStep)));
const normalized = rawStep / magnitude;
let factor;
if (normalized < 1.5) factor = 1;
else if (normalized < 3.5) factor = 2;
else if (normalized < 7.5) factor = 5;
else factor = 10;
const step = factor * magnitude;
const start = Math.ceil(min / step) * step;
const ticks = [];
for (let value = start; value <= max + step * 0.1; value += step) {
ticks.push(value);
}
return ticks;
}
function drawChart() {
const width = chart.clientWidth;
const height = chart.clientHeight;
if (!width || !height) return;
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const pixelWidth = Math.round(width * dpr);
const pixelHeight = Math.round(height * dpr);
if (chart.width !== pixelWidth || chart.height !== pixelHeight) {
chart.width = pixelWidth;
chart.height = pixelHeight;
}
const ctx = chartContext;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, width, height);
const padding = {
left: 49,
right: 17,
top: 42,
bottom: 38
};
const plotWidth = width - padding.left - padding.right;
const plotHeight = height - padding.top - padding.bottom;
const allValues = readings.concat([TRUE_WIDTH, EXPECTED_CENTER]);
let domainMin = Math.min(...allValues, TRUE_WIDTH - 0.09);
let domainMax = Math.max(...allValues, EXPECTED_CENTER + 0.09);
domainMin = Math.floor((domainMin - 0.015) * 100) / 100;
domainMax = Math.ceil((domainMax + 0.015) * 100) / 100;
const x = (value) =>
padding.left +
((value - domainMin) / (domainMax - domainMin)) * plotWidth;
const y = (index) =>
padding.top +
8 +
(index / (TRIAL_COUNT - 1)) * (plotHeight - 16);
// Background.
ctx.fillStyle = "#081522";
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = "#0c1a29";
ctx.fillRect(
padding.left,
padding.top,
plotWidth,
plotHeight
);
// Fixed bias interval: truth to the biased center.
ctx.fillStyle = "rgba(255, 173, 74, 0.17)";
ctx.fillRect(
x(TRUE_WIDTH),
padding.top,
x(EXPECTED_CENTER) - x(TRUE_WIDTH),
plotHeight
);
// Random variation interval: ± one standard deviation.
ctx.fillStyle = "rgba(66, 217, 232, 0.13)";
ctx.fillRect(
x(EXPECTED_CENTER - RANDOM_SIGMA),
padding.top,
x(EXPECTED_CENTER + RANDOM_SIGMA) -
x(EXPECTED_CENTER - RANDOM_SIGMA),
plotHeight
);
// Horizontal trial grid.
ctx.strokeStyle = "rgba(125, 151, 171, 0.10)";
ctx.lineWidth = 1;
for (let i = 0; i < TRIAL_COUNT; i++) {
if (i % 5 !== 0 && i !== TRIAL_COUNT - 1) continue;
const yy = y(i);
ctx.beginPath();
ctx.moveTo(padding.left, yy);
ctx.lineTo(padding.left + plotWidth, yy);
ctx.stroke();
}
// Vertical measurement grid.
const ticks = niceTicks(domainMin, domainMax, 6);
ctx.fillStyle = "#8ba2b4";
ctx.font = "10px ui-monospace, monospace";
ctx.textAlign = "center";
for (const tick of ticks) {
const xx = x(tick);
ctx.strokeStyle = "rgba(125, 151, 171, 0.11)";
ctx.beginPath();
ctx.moveTo(xx, padding.top);
ctx.lineTo(xx, padding.top + plotHeight);
ctx.stroke();
ctx.fillText(tick.toFixed(2), xx, height - 16);
}
// True-width reference.
ctx.save();
ctx.setLineDash([5, 5]);
ctx.strokeStyle = "#f5f8fb";
ctx.lineWidth = 1.4;
ctx.beginPath();
ctx.moveTo(x(TRUE_WIDTH), padding.top);
ctx.lineTo(x(TRUE_WIDTH), padding.top + plotHeight);
ctx.stroke();
ctx.restore();
// Biased-center reference.
ctx.strokeStyle = "#ffad4a";
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.moveTo(x(EXPECTED_CENTER), padding.top);
ctx.lineTo(x(EXPECTED_CENTER), padding.top + plotHeight);
ctx.stroke();
// Observed mean.
if (currentIndex >= 0) {
const observedMean = meanOf(readings.slice(0, currentIndex + 1));
ctx.save();
ctx.setLineDash([2, 4]);
ctx.strokeStyle = "#6ee7a2";
ctx.lineWidth = 1.2;
ctx.beginPath();
ctx.moveTo(x(observedMean), padding.top);
ctx.lineTo(x(observedMean), padding.top + plotHeight);
ctx.stroke();
ctx.restore();
}
// Readings and their random components.
for (let i = 0; i <= currentIndex; i++) {
const reading = readings[i];
const xx = x(reading);
const yy = y(i);
const centerX = x(EXPECTED_CENTER);
ctx.strokeStyle = "rgba(66, 217, 232, 0.46)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(centerX, yy);
ctx.lineTo(xx, yy);
ctx.stroke();
ctx.beginPath();
ctx.arc(xx, yy, i === currentIndex ? 4.8 : 3.4, 0, Math.PI * 2);
ctx.fillStyle = i === currentIndex ? "#b8fbff" : "#42d9e8";
ctx.fill();
ctx.lineWidth = 1;
ctx.strokeStyle = "#07121f";
ctx.stroke();
if (i === currentIndex) {
ctx.beginPath();
ctx.arc(xx, yy, 8, 0, Math.PI * 2);
ctx.strokeStyle = "rgba(66, 217, 232, 0.7)";
ctx.lineWidth = 1.3;
ctx.stroke();
}
}
// Axis and top labels.
ctx.textAlign = "right";
ctx.fillStyle = "#8ba2b4";
ctx.font = "10px ui-monospace, monospace";
for (let i = 0; i < TRIAL_COUNT; i++) {
if (i % 5 !== 0 && i !== TRIAL_COUNT - 1) continue;
ctx.fillText(String(i + 1), padding.left - 10, y(i) + 3);
}
ctx.textAlign = "center";
ctx.fillStyle = "#c4d2dc";
ctx.font = "600 11px ui-sans-serif, system-ui";
ctx.fillText(
"MEASURED WIDTH (mm)",
padding.left + plotWidth / 2,
height - 1
);
ctx.save();
ctx.translate(12, padding.top + plotHeight / 2);
ctx.rotate(-Math.PI / 2);
ctx.fillText("TRIAL", 0, 0);
ctx.restore();
ctx.font = "700 9px ui-monospace, monospace";
ctx.textAlign = "left";
ctx.fillStyle = "#f5f8fb";
ctx.fillText("TRUE", x(TRUE_WIDTH) + 5, padding.top - 19);
ctx.fillStyle = "#ffad4a";
ctx.textAlign = "right";
ctx.fillText(
"BIAS CENTER",
x(EXPECTED_CENTER) - 5,
padding.top - 19
);
ctx.fillStyle = "#71899b";
ctx.textAlign = "left";
ctx.fillText(
"σ ±" + RANDOM_SIGMA.toFixed(3),
x(EXPECTED_CENTER + RANDOM_SIGMA) + 7,
padding.top + 12
);
}
// ------------------------------------------------------------
// Statistics and UI state
// ------------------------------------------------------------
function meanOf(values) {
if (!values.length) return NaN;
return values.reduce((sum, value) => sum + value, 0) /
values.length;
}
function sampleSD(values) {
if (values.length < 2) return NaN;
const mean = meanOf(values);
const squared = values.map(
value => Math.pow(value - mean, 2)
);
return Math.sqrt(
squared.reduce((sum, value) => sum + value, 0) /
(values.length - 1)
);
}
function updateStats() {
const completed = readings.slice(0, currentIndex + 1);
dom.trialCount.textContent =
`${completed.length} / ${TRIAL_COUNT} trials`;
if (!completed.length) {
dom.meanValue.textContent = "—";
dom.sdValue.textContent = "—";
dom.biasValue.textContent = "—";
return;
}
const mean = meanOf(completed);
const sd = sampleSD(completed);
dom.meanValue.textContent = mean.toFixed(3);
dom.sdValue.textContent = Number.isFinite(sd)
? sd.toFixed(3)
: "—";
dom.biasValue.textContent =
`${mean - TRUE_WIDTH >= 0 ? "+" : ""}${(mean - TRUE_WIDTH).toFixed(3)}`;
}
function updatePlayButton() {
if (running) {
dom.playBtn.textContent = "Pause";
dom.status.textContent = "RUNNING";
dom.status.dataset.state = "running";
} else if (currentIndex >= TRIAL_COUNT - 1) {
dom.playBtn.textContent = "Replay";
dom.status.textContent = "COMPLETE";
dom.status.dataset.state = "complete";
} else {
dom.playBtn.textContent = currentIndex < 0 ? "Start" : "Resume";
dom.status.textContent = "PAUSED";
dom.status.dataset.state = "paused";
}
}
function applyCurrentMeasurement() {
if (currentIndex < 0) {
drawScreen(null, -1);
readingLabel.set("READY", "WAITING FOR TRIAL");
updateStats();
drawChart();
return;
}
const reading = readings[currentIndex];
drawScreen(reading, currentIndex);
readingLabel.set(
`${reading.toFixed(3)} mm`,
`TRIAL ${String(currentIndex + 1).padStart(2, "0")} / ${TRIAL_COUNT}`
);
updateStats();
drawChart();
}
function startNewRun(newSeed = null) {
if (newSeed !== null) {
seed = newSeed >>> 0;
readings = generateReadings(seed);
}
currentIndex = -1;
running = true;
lastAdvance = performance.now();
dom.seedValue.textContent = String(seed);
applyCurrentMeasurement();
updatePlayButton();
}
dom.playBtn.addEventListener("click", () => {
if (currentIndex >= TRIAL_COUNT - 1) {
// Replay the same seeded sequence.
currentIndex = -1;
running = true;
lastAdvance = performance.now();
applyCurrentMeasurement();
} else {
running = !running;
lastAdvance = performance.now();
}
updatePlayButton();
});
dom.newBtn.addEventListener("click", () => {
const randomSeed = new Uint32Array(1);
crypto.getRandomValues(randomSeed);
startNewRun(randomSeed[0]);
});
dom.speed.addEventListener("input", () => {
const speed = Number(dom.speed.value);
dom.speedValue.textContent = `${speed.toFixed(1)}/s`;
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
let targetJawX = RIGHT_EDGE + FIXED_BIAS * VISUAL_GAIN;
let jawX = targetJawX;
movingJaw.position.x = jawX;
let previousFrame = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - previousFrame) / 1000, 0.05);
previousFrame = now;
if (running && currentIndex < TRIAL_COUNT - 1) {
const interval = 1000 / Number(dom.speed.value);
while (
running &&
currentIndex < TRIAL_COUNT - 1 &&
now - lastAdvance >= interval
) {
lastAdvance += interval;
currentIndex += 1;
applyCurrentMeasurement();
if (currentIndex >= TRIAL_COUNT - 1) {
running = false;
updatePlayButton();
break;
}
}
}
if (currentIndex >= 0) {
targetJawX =
RIGHT_EDGE +
(readings[currentIndex] - TRUE_WIDTH) * VISUAL_GAIN;
} else {
targetJawX = RIGHT_EDGE + FIXED_BIAS * VISUAL_GAIN;
}
const smoothing = 1 - Math.exp(-dt * 11);
jawX += (targetJawX - jawX) * smoothing;
movingJaw.position.x = jawX;
// Update the in-scene dimension line.
const positions = dimensionGeometry.attributes.position.array;
positions[0] = LEFT_EDGE;
positions[1] = -1.30;
positions[2] = 2.03;
positions[3] = jawX;
positions[4] = -1.30;
positions[5] = 2.03;
dimensionGeometry.attributes.position.needsUpdate = true;
dimensionGeometry.computeBoundingSphere();
rightDimensionTick.position.x = jawX;
renderer.render(scene, camera);
}
// ------------------------------------------------------------
// Resizing
// ------------------------------------------------------------
function resizeThree() {
const width = dom.host.clientWidth;
const height = dom.host.clientHeight;
if (!width || !height) return;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const sceneResizeObserver = new ResizeObserver(resizeThree);
sceneResizeObserver.observe(dom.host);
const chartResizeObserver = new ResizeObserver(drawChart);
chartResizeObserver.observe(
chart.parentElement
);
resizeThree();
drawChart();
applyCurrentMeasurement();
updatePlayButton();
requestAnimationFrame(animate);
</script>
</body>
</html>
```