Prompt specification
Build or customize the Three.js 3D scene "Bookshelf sag teaching vignette" 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: Bookshelf sag teaching vignette
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/bookshelf-sag-teaching-vignette-x1375
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows elastic bending of beams. Compose a shelf with movable books and a nearby exaggerated deflection inset; label amplification and model assumptions so visible sag is not mistaken for a real prediction. Voxel style.
Working Three.js Starter (bookshelf-sag-teaching-vignette-x1375):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Elastic Beam Lab</title>
<style>
:root {
--ink: #142c35;
--muted: #587078;
--paper: #f3f0e7;
--glass: rgba(246, 244, 236, 0.91);
--line: #173d45;
--teal: #2e8585;
--orange: #e87943;
--red: #df604f;
--shadow: rgba(20, 44, 53, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 30%, #f7f4e9 0, #e7eeee 48%, #ccdcdf 100%);
color: var(--ink);
font-family: "Courier New", ui-monospace, SFMono-Regular, Menlo, monospace;
}
button,
input {
font: inherit;
}
#app {
position: relative;
width: 100%;
height: 100%;
isolation: isolate;
}
#scene {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
z-index: 0;
}
.panel {
background: var(--glass);
border: 1px solid var(--line);
box-shadow:
5px 5px 0 var(--shadow),
inset 0 0 0 1px rgba(255, 255, 255, 0.55);
}
.brand {
position: absolute;
top: 22px;
left: 22px;
z-index: 4;
width: 280px;
padding: 17px 18px 16px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
color: var(--teal);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.pixel-dot {
width: 8px;
height: 8px;
background: var(--orange);
box-shadow: 3px 3px 0 rgba(232, 121, 67, 0.25);
}
h1 {
margin: 11px 0 7px;
font-size: 31px;
line-height: 0.93;
letter-spacing: -0.06em;
text-transform: uppercase;
}
h1 span {
color: var(--teal);
}
.brand p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.model-chip {
display: inline-block;
margin-top: 12px;
padding: 6px 8px;
border: 1px solid rgba(20, 60, 69, 0.28);
background: #e8eee9;
color: #315b61;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
}
.inspector {
position: absolute;
top: 22px;
right: 22px;
z-index: 5;
width: 292px;
padding: 15px;
pointer-events: auto;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
padding-bottom: 10px;
border-bottom: 1px solid rgba(20, 60, 69, 0.18);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status {
display: flex;
align-items: center;
gap: 5px;
color: var(--teal);
font-size: 9px;
}
.status::before {
content: "";
width: 7px;
height: 7px;
background: #4ca877;
box-shadow: 0 0 0 3px rgba(76, 168, 119, 0.15);
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin: 11px 0;
}
.metric {
min-height: 67px;
padding: 9px;
border: 1px solid rgba(20, 60, 69, 0.15);
background: rgba(255, 255, 255, 0.48);
}
.metric span {
display: block;
margin-bottom: 8px;
color: var(--muted);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric strong {
display: block;
font-size: 18px;
letter-spacing: -0.06em;
}
.metric small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 8px;
letter-spacing: 0;
}
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
margin: 14px 0 7px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#stiffness {
width: 100%;
height: 16px;
margin: 0;
accent-color: var(--teal);
cursor: ew-resize;
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: 3px;
color: #789096;
font-size: 8px;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 13px;
}
button {
min-height: 34px;
border: 1px solid var(--line);
border-radius: 0;
background: var(--ink);
color: #f7f3e9;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
cursor: pointer;
box-shadow: 3px 3px 0 rgba(20, 44, 53, 0.16);
}
button:hover {
background: var(--teal);
}
button:active {
transform: translate(2px, 2px);
box-shadow: 1px 1px 0 rgba(20, 44, 53, 0.16);
}
.button-row .secondary {
background: transparent;
color: var(--ink);
}
.button-row .secondary:hover {
background: #dce8e5;
color: var(--ink);
}
.legend {
position: absolute;
bottom: 22px;
left: 22px;
z-index: 4;
width: 280px;
padding: 12px 14px;
pointer-events: none;
}
.legend-title {
margin-bottom: 8px;
color: var(--teal);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 7px;
color: var(--muted);
font-size: 9px;
line-height: 1.35;
}
.swatch {
width: 15px;
height: 8px;
flex: 0 0 auto;
background: var(--teal);
}
.swatch.amplified {
background: var(--red);
}
.swatch.baseline {
height: 2px;
background: repeating-linear-gradient(
90deg,
var(--teal) 0 5px,
transparent 5px 8px
);
}
.inset-panel {
position: absolute;
right: 22px;
bottom: 22px;
z-index: 3;
width: min(430px, calc(100vw - 44px));
border: 1px solid var(--line);
box-shadow:
6px 6px 0 var(--shadow),
inset 0 0 0 1px rgba(255, 255, 255, 0.45);
pointer-events: none;
background: transparent;
}
.inset-heading {
display: flex;
align-items: center;
justify-content: space-between;
min-height: 49px;
padding: 9px 12px;
background: rgba(246, 244, 236, 0.96);
border-bottom: 1px solid var(--line);
}
.inset-heading strong {
display: block;
font-size: 11px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.inset-heading small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 8px;
}
.amplification {
display: grid;
place-items: center;
width: 57px;
height: 34px;
background: var(--red);
color: white;
font-size: 20px;
font-weight: 700;
letter-spacing: -0.08em;
box-shadow: 3px 3px 0 rgba(109, 45, 39, 0.22);
}
#insetViewport {
position: relative;
width: 100%;
height: 230px;
background: transparent;
}
.inset-caption {
padding: 10px 12px 11px;
background: rgba(246, 244, 236, 0.97);
border-top: 1px solid var(--line);
}
.amplification-note {
margin-bottom: 7px;
color: #a4483d;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.assumptions {
margin: 0;
color: var(--muted);
font-size: 8px;
line-height: 1.45;
}
.assumptions strong {
color: var(--ink);
}
.footer-note {
margin-top: 6px;
color: #7d8d91;
font-size: 7px;
line-height: 1.35;
}
.hint {
position: absolute;
left: 50%;
bottom: 22px;
z-index: 4;
transform: translateX(-50%);
padding: 9px 12px;
border: 1px solid rgba(20, 60, 69, 0.25);
background: rgba(247, 244, 235, 0.78);
color: #526b72;
font-size: 9px;
white-space: nowrap;
pointer-events: none;
}
.hint b {
color: var(--ink);
}
@media (max-width: 900px) {
.brand {
width: 235px;
}
h1 {
font-size: 25px;
}
.inspector {
width: 260px;
}
.legend {
width: 235px;
}
.hint {
display: none;
}
}
@media (max-width: 680px) {
.brand {
top: 12px;
left: 12px;
width: 205px;
padding: 12px;
}
.brand p,
.model-chip {
display: none;
}
h1 {
font-size: 22px;
}
.inspector {
top: 96px;
left: 12px;
right: auto;
width: 225px;
padding: 11px;
}
.metrics {
margin: 8px 0;
}
.metric {
min-height: 56px;
padding: 7px;
}
.metric strong {
font-size: 15px;
}
.control-label {
margin-top: 9px;
}
.button-row {
margin-top: 9px;
}
.legend {
display: none;
}
.inset-panel {
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
}
#insetViewport {
height: 165px;
}
}
</style>
</head>
<body>
<main id="app">
<canvas id="scene" aria-label="Interactive voxel shelf showing elastic beam bending"></canvas>
<header class="brand panel">
<div class="eyebrow">
<span class="pixel-dot"></span>
Structural playground / 01
</div>
<h1>Elastic<br><span>Beam Lab</span></h1>
<p>
Move the books to redistribute load. The shelf deforms according to a
normalized linear-elastic beam model.
</p>
<div class="model-chip">Normalized model · not a real prediction</div>
</header>
<aside class="inspector panel" aria-label="Beam controls and readouts">
<div class="panel-title">
<span>Beam inspector</span>
<span class="status">Live model</span>
</div>
<div class="metrics">
<div class="metric">
<span>Maximum sag</span>
<strong id="maxSag">0.00 u</strong>
<small id="peakLocation">peak x = 0.00</small>
</div>
<div class="metric">
<span>Selected book</span>
<strong id="selectedBook">B03</strong>
<small id="selectedLoad">1.35 load units</small>
</div>
</div>
<label class="control-label" for="stiffness">
Relative stiffness, EI
<output id="stiffnessValue">1.00×</output>
</label>
<input id="stiffness" type="range" min="0.75" max="1.80" step="0.05" value="1">
<div class="range-labels">
<span>More flexible</span>
<span>More rigid</span>
</div>
<div class="button-row">
<button id="clusterButton" type="button">Cluster center</button>
<button id="resetButton" type="button" class="secondary">Reset spread</button>
</div>
<div class="button-row">
<button id="cameraButton" type="button" class="secondary">Reset camera</button>
<button id="centerButton" type="button" class="secondary">Center view</button>
</div>
</aside>
<section class="legend panel" aria-label="Scene legend">
<div class="legend-title">Reading the model</div>
<div class="legend-row">
<span class="swatch"></span>
<span>Nominal deflection view, ×1</span>
</div>
<div class="legend-row">
<span class="swatch amplified"></span>
<span>Nearby inset, same response at ×8</span>
</div>
<div class="legend-row">
<span class="swatch baseline"></span>
<span>Dashed line = undeformed beam axis</span>
</div>
</section>
<section class="inset-panel" aria-label="Exaggerated deflection inset">
<div class="inset-heading">
<div>
<strong>Exaggerated deflection</strong>
<small>Same book positions and relative stiffness</small>
</div>
<div class="amplification" title="Deflection amplification factor">×8</div>
</div>
<div id="insetViewport"></div>
<div class="inset-caption">
<div class="amplification-note">
Amplification ×8 — shape shown for legibility, not load scale
</div>
<p class="assumptions">
<strong>Model assumptions:</strong> normalized Euler–Bernoulli beam;
constant relative EI; ideal pin/roller ends; each book is a point load;
linear small-deflection theory. Self-weight, shear, contact, friction,
yielding, buckling, and connection deformation are omitted.
</p>
<div class="footer-note">
Display units are relative and uncalibrated. No material dimensions,
loads, or safety factors are supplied. This is an educational diagram,
not a structural analysis.
</div>
</div>
</section>
<div class="hint">
<b>Drag books</b> to move loads · <b>drag background</b> to orbit ·
<b>scroll</b> to zoom · <b>← →</b> nudges the selected book
</div>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const insetViewport = document.querySelector("#insetViewport");
const maxSagElement = document.querySelector("#maxSag");
const peakLocationElement = document.querySelector("#peakLocation");
const selectedBookElement = document.querySelector("#selectedBook");
const selectedLoadElement = document.querySelector("#selectedLoad");
const stiffnessInput = document.querySelector("#stiffness");
const stiffnessValue = document.querySelector("#stiffnessValue");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x000000, 0);
renderer.autoClear = true;
// ------------------------------------------------------------------
// Shared scene constants
// ------------------------------------------------------------------
const SPAN = 6.4;
const HALF_SPAN = SPAN / 2;
const BASE_Y = 1.45;
const BEAM_THICKNESS = 0.18;
const SEGMENT_COUNT = 40;
const SEGMENT_LENGTH = SPAN / SEGMENT_COUNT;
const EI_BASE = 120;
const INSET_AMPLIFICATION = 8;
const COLORS = {
ink: 0x173a43,
teal: 0x3f9996,
tealDark: 0x2f7377,
tealLight: 0x75b7ad,
cream: 0xe9dfc9,
wood: 0xbe8760,
woodDark: 0x936347,
orange: 0xe87943,
red: 0xdf604f,
yellow: 0xe9b84f,
blue: 0x5878c5,
violet: 0x8a70bf,
mint: 0x78b58c,
gray: 0xaab8b8
};
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.76,
metalness: 0.02,
flatShading: true,
...options
});
}
function addBox(parent, size, position, mat, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
mat
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeDashedLine(scene, y, z, color, opacity = 0.72) {
const points = [];
for (let i = 0; i <= 80; i++) {
const x = -HALF_SPAN + (SPAN * i) / 80;
points.push(new THREE.Vector3(x, y, z));
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const lineMaterial = new THREE.LineDashedMaterial({
color,
dashSize: 0.12,
gapSize: 0.085,
transparent: true,
opacity,
depthTest: false
});
const line = new THREE.Line(geometry, lineMaterial);
line.computeLineDistances();
line.renderOrder = 20;
scene.add(line);
return line;
}
// ------------------------------------------------------------------
// Main diorama
// ------------------------------------------------------------------
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xdce6e7, 12, 25);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
60
);
const orbit = {
theta: 0.16,
phi: 1.02,
radius: 10.5,
target: new THREE.Vector3(0, 1.78, 0)
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.theta = 0.16;
orbit.phi = 1.02;
orbit.radius = 10.5;
orbit.target.set(0, 1.78, 0);
updateCamera();
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xf8f5e8, 0x78949a, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0d6, 3.4);
sun.position.set(-4.5, 9, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -5;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 24;
sun.shadow.bias = -0.0008;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x77b9bd, 1.15);
rimLight.position.set(6, 4, -5);
scene.add(rimLight);
const world = new THREE.Group();
scene.add(world);
// Ground and voxel platform.
const ground = addBox(
world,
new THREE.Vector3(22, 0.18, 16),
new THREE.Vector3(0, -0.34, 0),
material(0xd2dcd9),
false
);
ground.receiveShadow = true;
const platform = addBox(
world,
new THREE.Vector3(8.8, 0.22, 3.7),
new THREE.Vector3(0, -0.12, 0),
material(0xb8c9c5)
);
platform.castShadow = true;
const grid = new THREE.GridHelper(20, 40, 0x78989a, 0x9fb4b3);
grid.position.y = -0.235;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
// Shelf frame made from stacked blocks.
function createVoxelColumn(x) {
const group = new THREE.Group();
const baseY = 0.02;
const blockHeight = 0.31;
const count = 11;
for (let i = 0; i < count; i++) {
const mat = material(
i % 3 === 0 ? COLORS.woodDark : COLORS.wood,
{ roughness: 0.88 }
);
addBox(
group,
new THREE.Vector3(0.34, 0.285, 1.28),
new THREE.Vector3(x, baseY + i * blockHeight + 0.145, 0),
mat
);
}
world.add(group);
}
createVoxelColumn(-3.48);
createVoxelColumn(3.48);
function createSegmentedPlank(y, colorA, colorB) {
const count = 20;
const length = 7.18;
const segment = length / count;
for (let i = 0; i < count; i++) {
const x = -length / 2 + (i + 0.5) * segment;
addBox(
world,
new THREE.Vector3(segment * 0.92, 0.18, 1.42),
new THREE.Vector3(x, y, 0),
material(i % 2 ? colorA : colorB)
);
}
}
createSegmentedPlank(3.27, COLORS.wood, COLORS.woodDark);
createSegmentedPlank(0.54, COLORS.wood, COLORS.woodDark);
// Small storage blocks on the upper and lower shelves.
function createArchiveBox(x, y, color) {
const group = new THREE.Group();
addBox(
group,
new THREE.Vector3(0.72, 0.48, 0.82),
new THREE.Vector3(0, 0.24, 0),
material(color)
);
addBox(
group,
new THREE.Vector3(0.8, 0.1, 0.88),
new THREE.Vector3(0, 0.52, 0),
material(COLORS.cream)
);
addBox(
group,
new THREE.Vector3(0.24, 0.1, 0.035),
new THREE.Vector3(0, 0.28, 0.43),
material(COLORS.ink)
);
group.position.set(x, y, 0);
world.add(group);
}
createArchiveBox(-1.25, 3.36, COLORS.teal);
createArchiveBox(-0.35, 3.36, COLORS.yellow);
createArchiveBox(1.32, 3.36, COLORS.blue);
createArchiveBox(-2.25, 0.63, COLORS.orange);
createArchiveBox(1.95, 0.63, COLORS.tealLight);
// Idealized simple supports under the deforming beam.
for (const side of [-1, 1]) {
const x = side * HALF_SPAN;
addBox(
world,
new THREE.Vector3(0.5, 0.22, 1.36),
new THREE.Vector3(x, BASE_Y - 0.25, 0),
material(side < 0 ? COLORS.tealDark : COLORS.teal)
);
const roller = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 1.05, 8),
material(side < 0 ? COLORS.teal : COLORS.orange)
);
roller.rotation.x = Math.PI / 2;
roller.position.set(x, BASE_Y - 0.12, 0);
roller.castShadow = true;
world.add(roller);
addBox(
world,
new THREE.Vector3(0.38, 0.42, 1.28),
new THREE.Vector3(side * 3.45, BASE_Y - 0.42, 0),
material(COLORS.cream)
);
}
// The deforming shelf beam, assembled from discrete voxel segments.
const mainBeamSegments = [];
for (let i = 0; i < SEGMENT_COUNT; i++) {
const mesh = addBox(
world,
new THREE.Vector3(SEGMENT_LENGTH * 0.91, BEAM_THICKNESS, 1.18),
new THREE.Vector3(0, BASE_Y, 0),
material(i % 3 === 0 ? COLORS.tealLight : COLORS.teal)
);
mainBeamSegments.push(mesh);
}
makeDashedLine(scene, BASE_Y, 0.61, COLORS.tealDark, 0.58);
// Movable books.
const bookDefinitions = [
{ label: "B01", x: -2.70, mass: 1.05, w: 0.43, h: 1.16, color: COLORS.red },
{ label: "B02", x: -1.95, mass: 0.85, w: 0.38, h: 0.98, color: COLORS.yellow },
{ label: "B03", x: -1.10, mass: 1.35, w: 0.46, h: 1.28, color: COLORS.teal },
{ label: "B04", x: -0.25, mass: 0.95, w: 0.40, h: 1.08, color: COLORS.blue },
{ label: "B05", x: 0.63, mass: 1.15, w: 0.44, h: 1.21, color: COLORS.violet },
{ label: "B06", x: 1.48, mass: 0.75, w: 0.36, h: 0.92, color: COLORS.orange },
{ label: "B07", x: 2.45, mass: 1.05, w: 0.42, h: 1.12, color: COLORS.mint }
];
const books = [];
function createMainBook(definition) {
const group = new THREE.Group();
const bodyMaterial = material(definition.color, {
roughness: 0.72,
emissive: 0x000000,
emissiveIntensity: 0
});
const body = addBox(
group,
new THREE.Vector3(definition.w, definition.h, 0.62),
new THREE.Vector3(0, definition.h / 2, 0),
bodyMaterial
);
const spineMaterial = material(new THREE.Color(definition.color).multiplyScalar(0.72));
addBox(
group,
new THREE.Vector3(definition.w * 0.79, definition.h * 0.84, 0.045),
new THREE.Vector3(0, definition.h * 0.53, 0.328),
spineMaterial
);
const labelMaterial = material(COLORS.cream);
addBox(
group,
new THREE.Vector3(definition.w * 0.53, 0.16, 0.05),
new THREE.Vector3(0, definition.h * 0.70, 0.36),
labelMaterial
);
addBox(
group,
new THREE.Vector3(definition.w * 0.64, 0.045, 0.055),
new THREE.Vector3(0, definition.h * 0.29, 0.36),
labelMaterial
);
addBox(
group,
new THREE.Vector3(definition.w * 0.64, 0.045, 0.055),
new THREE.Vector3(0, definition.h * 0.19, 0.36),
labelMaterial
);
// A small grab indicator makes the interaction discoverable.
addBox(
group,
new THREE.Vector3(0.07, 0.07, 0.07),
new THREE.Vector3(definition.w * 0.25, 0.08, 0.37),
material(COLORS.orange)
);
group.position.set(definition.x, BASE_Y, 0);
world.add(group);
const book = {
...definition,
initialX: definition.x,
group,
body,
bodyMaterial
};
group.userData.book = book;
books.push(book);
return book;
}
bookDefinitions.forEach(createMainBook);
const selectedArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(),
0.7,
COLORS.orange,
0.17,
0.12
);
scene.add(selectedArrow);
let selectedBook = books[2];
function setSelectedBook(book) {
if (!book) return;
if (selectedBook && selectedBook !== book) {
selectedBook.bodyMaterial.emissive.setHex(0x000000);
selectedBook.bodyMaterial.emissiveIntensity = 0;
}
selectedBook = book;
selectedBook.bodyMaterial.emissive.setHex(0x4b3215);
selectedBook.bodyMaterial.emissiveIntensity = 0.18;
selectedBookElement.textContent = selectedBook.label;
selectedLoadElement.textContent =
`${selectedBook.mass.toFixed(2)} load units`;
}
setSelectedBook(selectedBook);
// ------------------------------------------------------------------
// Exaggerated inset scene
// ------------------------------------------------------------------
const insetScene = new THREE.Scene();
insetScene.background = new THREE.Color(0xf2ede3);
const insetCamera = new THREE.OrthographicCamera(-4, 4, 3, -3, 0.1, 30);
insetCamera.position.set(0, 0.8, 8);
const insetHemi = new THREE.HemisphereLight(0xfff9eb, 0x9aafb0, 2.5);
insetScene.add(insetHemi);
const insetSun = new THREE.DirectionalLight(0xffead0, 2.5);
insetSun.position.set(-3, 5, 6);
insetScene.add(insetSun);
const insetBackdrop = addBox(
insetScene,
new THREE.Vector3(12, 7, 0.12),
new THREE.Vector3(0, -0.7, -1.55),
material(0xe9e1d3),
false
);
insetBackdrop.receiveShadow = false;
const insetBeamSegments = [];
for (let i = 0; i < SEGMENT_COUNT; i++) {
const mesh = addBox(
insetScene,
new THREE.Vector3(SEGMENT_LENGTH * 0.91, BEAM_THICKNESS, 1.05),
new THREE.Vector3(0, 0, 0),
material(i % 3 === 0 ? COLORS.red : 0xc94f46)
);
mesh.castShadow = false;
insetBeamSegments.push(mesh);
}
for (const side of [-1, 1]) {
const x = side * HALF_SPAN;
addBox(
insetScene,
new THREE.Vector3(0.42, 0.25, 1.12),
new THREE.Vector3(x, -0.23, 0),
material(side < 0 ? COLORS.tealDark : COLORS.teal),
false
);
addBox(
insetScene,
new THREE.Vector3(0.34, 1.05, 1.02),
new THREE.Vector3(x, -0.83, 0),
material(COLORS.cream),
false
);
}
makeDashedLine(insetScene, 0, 0.66, COLORS.tealDark, 0.85);
const insetBooks = [];
function createInsetBook(definition) {
const group = new THREE.Group();
const h = 0.64;
addBox(
group,
new THREE.Vector3(definition.w * 0.92, h, 0.54),
new THREE.Vector3(0, h / 2, 0),
material(definition.color),
false
);
addBox(
group,
new THREE.Vector3(definition.w * 0.66, 0.09, 0.04),
new THREE.Vector3(0, h * 0.70, 0.29),
material(COLORS.cream),
false
);
group.position.z = 0.03;
insetScene.add(group);
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(),
0.7,
COLORS.orange,
0.16,
0.12
);
insetScene.add(arrow);
const insetBook = {
...definition,
group,
arrow,
insetHeight: h
};
insetBooks.push(insetBook);
return insetBook;
}
bookDefinitions.forEach(createInsetBook);
// ------------------------------------------------------------------
// Linear-elastic simply supported beam model
//
// Deflection is the superposition of point-load solutions for a
// simply supported Euler–Bernoulli beam with constant EI.
// Units are deliberately normalized and uncalibrated.
// ------------------------------------------------------------------
let relativeEI = 1;
let currentMaximum = 0;
let currentPeakX = 0;
function pointLoadDeflection(localX, loadX, load, EI) {
const L = SPAN;
const a = loadX;
const b = L - a;
if (localX <= a) {
return (
load * b * localX *
(L * L - b * b - localX * localX) /
(6 * L * EI)
);
}
const rightDistance = L - localX;
return (
load * a * rightDistance *
(L * L - a * a - rightDistance * rightDistance) /
(6 * L * EI)
);
}
function deflectionAtWorld(worldX) {
const localX = THREE.MathUtils.clamp(
worldX + HALF_SPAN,
0,
SPAN
);
const EI = EI_BASE * relativeEI;
let deflection = 0;
for (const book of books) {
const loadX = THREE.MathUtils.clamp(
book.x + HALF_SPAN,
0,
SPAN
);
deflection += pointLoadDeflection(
localX,
loadX,
book.mass,
EI
);
}
return Math.max(0, deflection);
}
function slopeAtWorld(worldX, amplification = 1) {
const epsilon = 0.008;
const left = deflectionAtWorld(worldX - epsilon);
const right = deflectionAtWorld(worldX + epsilon);
const derivative = (right - left) / (2 * epsilon);
// The physical vertical coordinate is y = -deflection.
return -derivative * amplification;
}
function updateInsetCamera(aspect = 1.6) {
const safeAspect = Math.max(0.8, aspect);
const amplifiedSag = currentMaximum * INSET_AMPLIFICATION;
const halfHeight = Math.max(
2.1,
amplifiedSag * 0.62 + 1.15,
3.35 / safeAspect
);
const targetY = -amplifiedSag * 0.42;
insetCamera.left = -halfHeight * safeAspect;
insetCamera.right = halfHeight * safeAspect;
insetCamera.top = halfHeight;
insetCamera.bottom = -halfHeight;
insetCamera.position.set(0, targetY + 1.45, 7);
insetCamera.lookAt(0, targetY, 0);
insetCamera.updateProjectionMatrix();
}
function updateModel() {
currentMaximum = 0;
currentPeakX = 0;
for (let i = 0; i <= 160; i++) {
const x = -HALF_SPAN + (SPAN * i) / 160;
const value = deflectionAtWorld(x);
if (value > currentMaximum) {
currentMaximum = value;
currentPeakX = x;
}
}
// Main, nominal ×1 beam.
for (let i = 0; i < mainBeamSegments.length; i++) {
const x = -HALF_SPAN + (i + 0.5) * SEGMENT_LENGTH;
const deflection = deflectionAtWorld(x);
const slope = slopeAtWorld(x);
const mesh = mainBeamSegments[i];
mesh.position.set(x, BASE_Y - deflection, 0);
mesh.rotation.z = Math.atan(slope);
}
// Movable books remain attached to the local beam tangent.
for (const book of books) {
const deflection = deflectionAtWorld(book.x);
const slope = slopeAtWorld(book.x);
book.group.position.set(
book.x,
BASE_Y - deflection + BEAM_THICKNESS / 2,
0
);
book.group.rotation.z = Math.atan(slope);
}
// Inset, exact ×8 rendering of the same response.
for (let i = 0; i < insetBeamSegments.length; i++) {
const x = -HALF_SPAN + (i + 0.5) * SEGMENT_LENGTH;
const deflection = deflectionAtWorld(x);
const slope = slopeAtWorld(x, INSET_AMPLIFICATION);
const mesh = insetBeamSegments[i];
mesh.position.set(x, -deflection * INSET_AMPLIFICATION, 0);
mesh.rotation.z = Math.atan(slope);
}
for (const book of insetBooks) {
const deflection = deflectionAtWorld(book.x);
const slope = slopeAtWorld(book.x, INSET_AMPLIFICATION);
const baseY = -deflection * INSET_AMPLIFICATION + BEAM_THICKNESS / 2;
book.group.position.set(book.x, baseY, 0.03);
book.group.rotation.z = Math.atan(slope);
const arrowLength = 0.58 + book.mass * 0.13;
book.arrow.position.set(
book.x,
baseY + book.insetHeight + arrowLength * 0.82,
0.72
);
book.arrow.setLength(arrowLength, 0.16, 0.12);
}
if (selectedBook) {
selectedArrow.position.set(
selectedBook.x,
selectedBook.group.position.y + selectedBook.h + 0.64,
0.83
);
}
maxSagElement.textContent = `${currentMaximum.toFixed(2)} u`;
peakLocationElement.textContent =
`peak x = ${currentPeakX >= 0 ? "+" : ""}${currentPeakX.toFixed(2)}`;
const insetRect = insetViewport.getBoundingClientRect();
updateInsetCamera(insetRect.width / Math.max(1, insetRect.height));
}
updateModel();
// ------------------------------------------------------------------
// Direct camera controls and book dragging
// ------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const planeHit = new THREE.Vector3();
let interactionMode = null;
let activePointerId = null;
let lastPointerX = 0;
let lastPointerY = 0;
let dragOffset = 0;
function updatePointer(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 pickBook(event) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(
books.map((book) => book.group),
true
);
if (!intersections.length) return null;
let object = intersections[0].object;
while (object && !object.userData.book) {
object = object.parent;
}
return object ? object.userData.book : null;
}
function moveBookTo(book, requestedX) {
const ordered = [...books].sort((a, b) => a.x - b.x);
const index = ordered.indexOf(book);
const previous = ordered[index - 1];
const next = ordered[index + 1];
const minimum = previous
? previous.x + (previous.w + book.w) / 2 + 0.055
: -HALF_SPAN + book.w / 2 + 0.025;
const maximum = next
? next.x - (next.w + book.w) / 2 - 0.055
: HALF_SPAN - book.w / 2 - 0.025;
book.x = THREE.MathUtils.clamp(requestedX, minimum, maximum);
updateModel();
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
canvas.focus();
activePointerId = event.pointerId;
canvas.setPointerCapture(event.pointerId);
const pickedBook = pickBook(event);
if (pickedBook) {
interactionMode = "book";
setSelectedBook(pickedBook);
dragPlane.set(
new THREE.Vector3(0, 1, 0),
-pickedBook.group.position.y
);
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
dragOffset = pickedBook.x - planeHit.x;
} else {
dragOffset = 0;
}
canvas.style.cursor = "grabbing";
} else {
interactionMode = "orbit";
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.style.cursor = "grabbing";
}
event.preventDefault();
});
canvas.addEventListener("pointermove", (event) => {
if (interactionMode === "book" && event.pointerId === activePointerId) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
moveBookTo(selectedBook, planeHit.x + dragOffset);
}
event.preventDefault();
return;
}
if (interactionMode === "orbit" && event.pointerId === activePointerId) {
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
orbit.theta = THREE.MathUtils.clamp(
orbit.theta - deltaX * 0.006,
-1.2,
1.2
);
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - deltaY * 0.005,
0.62,
1.25
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
event.preventDefault();
return;
}
if (!interactionMode) {
const hoveredBook = pickBook(event);
canvas.style.cursor = hoveredBook ? "pointer" : "grab";
}
});
function endPointer(event) {
if (event.pointerId !== activePointerId) return;
interactionMode = null;
activePointerId = null;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("pointerleave", () => {
if (!interactionMode) canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
7.2,
15
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if ((event.key === "ArrowLeft" || event.key === "ArrowRight") && selectedBook) {
event.preventDefault();
const direction = event.key === "ArrowLeft" ? -1 : 1;
moveBookTo(selectedBook, selectedBook.x + direction * 0.12);
}
if (event.key.toLowerCase() === "c") {
clusterBooks();
}
if (event.key.toLowerCase() === "r") {
resetBooks();
}
});
function clusterBooks() {
const spacing = 0.69;
books.forEach((book, index) => {
book.x = (index - (books.length - 1) / 2) * spacing;
});
updateModel();
}
function resetBooks() {
books.forEach((book) => {
book.x = book.initialX;
});
updateModel();
}
document.querySelector("#clusterButton").addEventListener("click", clusterBooks);
document.querySelector("#resetButton").addEventListener("click", resetBooks);
document.querySelector("#cameraButton").addEventListener("click", resetCamera);
document.querySelector("#centerButton").addEventListener("click", () => {
orbit.theta = 0;
orbit.phi = 1.02;
orbit.radius = 9.7;
orbit.target.set(0, 1.78, 0);
updateCamera();
});
stiffnessInput.addEventListener("input", () => {
relativeEI = Number(stiffnessInput.value);
stiffnessValue.textContent = `${relativeEI.toFixed(2)}×`;
updateModel();
});
// ------------------------------------------------------------------
// Rendering: one canvas, with the inset rendered into a scissor region
// ------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
const insetRect = insetViewport.getBoundingClientRect();
updateInsetCamera(insetRect.width / Math.max(1, insetRect.height));
}
window.addEventListener("resize", resize);
resize();
function renderInset() {
const canvasRect = canvas.getBoundingClientRect();
const insetRect = insetViewport.getBoundingClientRect();
const width = insetRect.width;
const height = insetRect.height;
if (width < 2 || height < 2) return;
const x = insetRect.left - canvasRect.left;
const y = canvasRect.bottom - insetRect.bottom;
renderer.setScissorTest(true);
renderer.setScissor(x, y, width, height);
renderer.setViewport(x, y, width, height);
renderer.render(insetScene, insetCamera);
renderer.setScissorTest(false);
}
function animate() {
requestAnimationFrame(animate);
renderer.setScissorTest(false);
renderer.setViewport(0, 0, window.innerWidth, window.innerHeight);
renderer.render(scene, camera);
renderInset();
}
animate();
</script>
</body>
</html>
```