Prompt specification
Build or customize the Three.js 3D scene "Scaffold load-path teaching model" 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: Scaffold load-path teaching model
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/scaffold-load-path-teaching-model-x1435
Scene Description Prompt:
A voxel scaffold model with movable loads, support reactions, and explicitly idealized force arrows. Three.js from cdnjs, one HTML file.
Working Three.js Starter (scaffold-load-path-teaching-model-x1435):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Scaffold Force Model</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(10, 21, 32, 0.88);
--panel-border: rgba(139, 191, 221, 0.18);
--text: #e8f2fa;
--muted: #8ca4b8;
--cyan: #54d9ff;
--green: #39e6b5;
--orange: #ffb44d;
--pink: #ff6684;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
overflow: hidden;
}
.force-label {
position: absolute;
left: 0;
top: 0;
padding: 5px 8px;
border: 1px solid currentColor;
border-radius: 5px;
background: rgba(5, 13, 20, 0.9);
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.25);
color: var(--cyan);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
white-space: nowrap;
transform: translate(-50%, -50%);
will-change: transform;
}
.force-label.reaction {
color: var(--green);
}
.force-label span {
opacity: 0.58;
margin-right: 4px;
font-weight: 500;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--panel-border);
border-radius: 13px;
background: var(--panel);
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.32),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(14px);
}
#header {
top: 18px;
left: 18px;
width: 330px;
padding: 17px 18px;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 6px;
font-size: 21px;
line-height: 1.15;
letter-spacing: -0.025em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 14px;
}
.stat {
padding: 9px 10px;
border: 1px solid rgba(140, 180, 210, 0.12);
border-radius: 8px;
background: rgba(255, 255, 255, 0.025);
}
.stat strong {
display: block;
font: 700 17px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.stat small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.08em;
}
#controls {
top: 18px;
right: 18px;
width: 300px;
padding: 17px;
max-height: calc(100vh - 36px);
overflow: auto;
}
.section {
padding: 13px 0;
border-top: 1px solid rgba(140, 180, 210, 0.12);
}
.section:first-of-type {
border-top: 0;
padding-top: 7px;
}
.section-title {
margin-bottom: 10px;
color: #b8cad8;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.load-tabs {
display: flex;
gap: 7px;
margin-bottom: 11px;
}
.load-tab {
flex: 1;
padding: 7px 5px;
border: 1px solid rgba(140, 180, 210, 0.16);
border-radius: 7px;
background: rgba(255, 255, 255, 0.025);
color: var(--muted);
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition: 160ms ease;
}
.load-tab:hover,
.load-tab.active {
border-color: var(--tab-color);
color: var(--text);
background: color-mix(in srgb, var(--tab-color) 13%, transparent);
}
.load-tab i {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 5px;
border-radius: 2px;
background: var(--tab-color);
}
.weight-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 7px;
font-size: 12px;
color: var(--muted);
}
#weightValue {
color: var(--text);
font: 700 13px ui-monospace, SFMono-Regular, Menlo, monospace;
}
input[type="range"] {
width: 100%;
height: 5px;
accent-color: var(--cyan);
cursor: pointer;
}
.reaction-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.reaction-cell {
padding: 8px;
border: 1px solid rgba(57, 230, 181, 0.16);
border-radius: 7px;
background: rgba(57, 230, 181, 0.035);
}
.reaction-cell b {
display: block;
color: var(--green);
font: 700 13px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.reaction-cell span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.toggle {
display: flex;
align-items: center;
justify-content: space-between;
padding: 6px 0;
color: #c2d1dc;
font-size: 12px;
cursor: pointer;
}
.toggle input {
width: 15px;
height: 15px;
accent-color: var(--cyan);
cursor: pointer;
}
.button-row {
display: flex;
gap: 8px;
}
.button {
flex: 1;
padding: 9px 10px;
border: 1px solid rgba(140, 180, 210, 0.2);
border-radius: 8px;
background: rgba(255, 255, 255, 0.035);
color: #d7e3eb;
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition: 160ms ease;
}
.button:hover {
border-color: var(--cyan);
color: white;
background: rgba(84, 217, 255, 0.08);
}
#hint {
position: fixed;
z-index: 8;
left: 50%;
bottom: 19px;
transform: translateX(-50%);
padding: 9px 13px;
border: 1px solid rgba(140, 180, 210, 0.15);
border-radius: 999px;
background: rgba(7, 16, 25, 0.76);
color: #9eb2c2;
font-size: 11px;
white-space: nowrap;
pointer-events: none;
backdrop-filter: blur(10px);
}
#hint b {
color: var(--text);
font-weight: 700;
}
#legend {
position: fixed;
z-index: 8;
left: 18px;
bottom: 18px;
display: flex;
gap: 13px;
padding: 8px 11px;
border: 1px solid rgba(140, 180, 210, 0.13);
border-radius: 8px;
background: rgba(7, 16, 25, 0.72);
color: var(--muted);
font-size: 10px;
pointer-events: none;
backdrop-filter: blur(10px);
}
.legend-item {
display: flex;
align-items: center;
gap: 5px;
}
.legend-dot {
width: 7px;
height: 7px;
border-radius: 50%;
}
@media (max-width: 760px) {
#header {
top: 10px;
left: 10px;
width: calc(100% - 20px);
padding: 13px 15px;
}
#header .subtitle,
#header .stats {
display: none;
}
#controls {
top: auto;
right: 10px;
bottom: 54px;
width: 280px;
max-height: 48vh;
padding: 13px;
}
#hint {
bottom: 10px;
max-width: calc(100% - 20px);
overflow: hidden;
text-overflow: ellipsis;
}
#legend {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel scaffold model"></canvas>
<div id="labels"></div>
<header id="header" class="panel">
<div class="eyebrow">Structural Systems Lab</div>
<h1>Voxel Scaffold / Live Load Model</h1>
<p class="subtitle">
Rigid deck on four point supports. Drag the loads to update the
idealized reactions and equilibrium checks.
</p>
<div class="stats">
<div class="stat">
<strong id="totalLoad">26.0 kN</strong>
<small>Total applied load</small>
</div>
<div class="stat">
<strong id="totalReaction">26.0 kN</strong>
<small>Sum of reactions</small>
</div>
</div>
</header>
<aside id="controls" class="panel">
<div class="section">
<div class="section-title">Movable Load</div>
<div class="load-tabs">
<button class="load-tab active" data-load="0" style="--tab-color:#ffb44d">
<i></i>P₁
</button>
<button class="load-tab" data-load="1" style="--tab-color:#ff6684">
<i></i>P₂
</button>
<button class="load-tab" data-load="2" style="--tab-color:#70e1ff">
<i></i>P₃
</button>
</div>
<div class="weight-row">
<span>Applied force</span>
<span id="weightValue">8.0 kN</span>
</div>
<input
id="weight"
type="range"
min="2"
max="20"
step="0.5"
value="8"
aria-label="Selected load magnitude"
/>
</div>
<div class="section">
<div class="section-title">Support Reactions</div>
<div class="reaction-grid">
<div class="reaction-cell">
<b id="r0">0.0 kN</b>
<span>R₁ · x− / z−</span>
</div>
<div class="reaction-cell">
<b id="r1">0.0 kN</b>
<span>R₂ · x+ / z−</span>
</div>
<div class="reaction-cell">
<b id="r2">0.0 kN</b>
<span>R₃ · x− / z+</span>
</div>
<div class="reaction-cell">
<b id="r3">0.0 kN</b>
<span>R₄ · x+ / z+</span>
</div>
</div>
</div>
<div class="section">
<div class="section-title">Display</div>
<label class="toggle">
Idealized force arrows
<input id="showArrows" type="checkbox" checked />
</label>
<label class="toggle">
Force value labels
<input id="showLabels" type="checkbox" checked />
</label>
<label class="toggle">
Reference grid
<input id="showGrid" type="checkbox" checked />
</label>
</div>
<div class="section">
<div class="section-title">Model</div>
<div class="button-row">
<button id="resetLoads" class="button">Reset loads</button>
<button id="resetCamera" class="button">Reset camera</button>
</div>
</div>
</aside>
<div id="legend">
<div class="legend-item">
<span class="legend-dot" style="background:#ffb44d"></span>
Applied load
</div>
<div class="legend-item">
<span class="legend-dot" style="background:#39e6b5"></span>
Support reaction
</div>
<div class="legend-item">
<span class="legend-dot" style="background:#54d9ff"></span>
Scaffold / deck
</div>
</div>
<div id="hint">
<b>Drag</b> a load to move it · <b>Left drag</b> orbit ·
<b>Right drag</b> pan · <b>Scroll</b> zoom
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const labelLayer = document.querySelector("#labels");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 27, 55);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemi = new THREE.HemisphereLight(0xc5eaff, 0x10202b, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffffff, 3.0);
sun.position.set(8, 15, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.00035;
scene.add(sun);
const rim = new THREE.PointLight(0x37cfff, 22, 30, 2);
rim.position.set(-10, 6, -7);
scene.add(rim);
const warm = new THREE.PointLight(0xff8b44, 14, 24, 2);
warm.position.set(9, 4, 7);
scene.add(warm);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const materials = {
steel: new THREE.MeshStandardMaterial({
color: 0x38647d,
roughness: 0.58,
metalness: 0.58
}),
steelLight: new THREE.MeshStandardMaterial({
color: 0x548ba3,
roughness: 0.5,
metalness: 0.48
}),
deckA: new THREE.MeshStandardMaterial({
color: 0x286078,
roughness: 0.68,
metalness: 0.36
}),
deckB: new THREE.MeshStandardMaterial({
color: 0x347990,
roughness: 0.62,
metalness: 0.4
}),
joint: new THREE.MeshStandardMaterial({
color: 0xe89a32,
roughness: 0.46,
metalness: 0.42
}),
brace: new THREE.MeshStandardMaterial({
color: 0xd27e2e,
roughness: 0.55,
metalness: 0.45
}),
ground: new THREE.MeshStandardMaterial({
color: 0x0b1720,
roughness: 0.94,
metalness: 0.05
})
};
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
materials.ground
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.015;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(44, 44, 0x416477, 0x19313e);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.55;
scene.add(grid);
// -------------------------------------------------------------------------
// Scaffold geometry
// -------------------------------------------------------------------------
const DECK_Y = 5.44;
const SUPPORT_X = 5.5;
const SUPPORT_Z = 3.5;
function addBox(size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function addBarBetween(start, end, thickness, material) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const geometry = new THREE.BoxGeometry(thickness, length, thickness);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
const supportLocations = [
{ sx: -1, sz: -1, x: -SUPPORT_X, z: -SUPPORT_Z },
{ sx: 1, sz: -1, x: SUPPORT_X, z: -SUPPORT_Z },
{ sx: -1, sz: 1, x: -SUPPORT_X, z: SUPPORT_Z },
{ sx: 1, sz: 1, x: SUPPORT_X, z: SUPPORT_Z }
];
// Four voxel towers.
for (const support of supportLocations) {
for (let level = 0; level < 6; level++) {
const material =
level % 2 === 0 ? materials.steel : materials.steelLight;
addBox(
new THREE.Vector3(0.78, 0.78, 0.78),
new THREE.Vector3(
support.x,
0.44 + level * 0.84,
support.z
),
material
);
addBox(
new THREE.Vector3(0.83, 0.09, 0.83),
new THREE.Vector3(
support.x,
0.04 + level * 0.84,
support.z
),
materials.joint,
false
);
}
addBox(
new THREE.Vector3(1.18, 0.13, 1.18),
new THREE.Vector3(support.x, 0.065, support.z),
materials.joint
);
}
// Longitudinal deck beams.
for (let x = -5.5; x <= 5.5; x += 1) {
for (const z of [-SUPPORT_Z, SUPPORT_Z]) {
addBox(
new THREE.Vector3(0.94, 0.38, 0.66),
new THREE.Vector3(x, 5.0, z),
materials.steel
);
}
}
// Transverse deck beams.
for (let z = -3.5; z <= 3.5; z += 1) {
for (const x of [-SUPPORT_X, SUPPORT_X]) {
addBox(
new THREE.Vector3(0.66, 0.38, 0.94),
new THREE.Vector3(x, 5.0, z),
materials.steel
);
}
}
// Secondary joists.
for (const x of [-4, -2, 0, 2, 4]) {
addBox(
new THREE.Vector3(0.38, 0.28, 7.35),
new THREE.Vector3(x, 4.78, 0),
materials.steelLight
);
}
// Voxel deck tiles.
for (let ix = 0; ix < 12; ix++) {
for (let iz = 0; iz < 8; iz++) {
const material =
(ix + iz) % 2 === 0 ? materials.deckA : materials.deckB;
addBox(
new THREE.Vector3(0.93, 0.2, 0.93),
new THREE.Vector3(
-5.5 + ix,
5.34,
-3.5 + iz
),
material
);
}
}
// Rear X bracing.
addBarBetween(
new THREE.Vector3(-5.5, 0.75, -3.56),
new THREE.Vector3(5.5, 4.7, -3.56),
0.13,
materials.brace
);
addBarBetween(
new THREE.Vector3(5.5, 0.75, -3.56),
new THREE.Vector3(-5.5, 4.7, -3.56),
0.13,
materials.brace
);
// Left-side X bracing.
addBarBetween(
new THREE.Vector3(-5.56, 0.75, -3.5),
new THREE.Vector3(-5.56, 4.7, 3.5),
0.13,
materials.brace
);
addBarBetween(
new THREE.Vector3(-5.56, 4.7, -3.5),
new THREE.Vector3(-5.56, 0.75, 3.5),
0.13,
materials.brace
);
// -------------------------------------------------------------------------
// Idealized force arrows
// -------------------------------------------------------------------------
const Y_AXIS = new THREE.Vector3(0, 1, 0);
function createArrow(color) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 1.25,
roughness: 0.35,
metalness: 0.08,
transparent: true,
opacity: 0.96,
depthTest: false,
depthWrite: false
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 1, 10),
material
);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.17, 0.34, 12),
material
);
shaft.renderOrder = 20;
head.renderOrder = 21;
group.add(shaft, head);
scene.add(group);
return { group, shaft, head, material };
}
function updateArrow(arrow, origin, tip) {
const direction = new THREE.Vector3().subVectors(tip, origin);
const length = Math.max(direction.length(), 0.001);
const normalized = direction.clone().normalize();
arrow.shaft.position.copy(origin).add(tip).multiplyScalar(0.5);
arrow.shaft.scale.set(1, length, 1);
arrow.shaft.quaternion.setFromUnitVectors(Y_AXIS, normalized);
arrow.head.position.copy(tip).addScaledVector(normalized, -0.17);
arrow.head.quaternion.setFromUnitVectors(Y_AXIS, normalized);
}
function createLabel(className) {
const element = document.createElement("div");
element.className = `force-label ${className}`;
labelLayer.appendChild(element);
return element;
}
// -------------------------------------------------------------------------
// Loads
// -------------------------------------------------------------------------
const loadDefinitions = [
{
name: "P₁",
color: 0xffb44d,
css: "#ffb44d",
weight: 8,
x: -2.8,
z: 1.45
},
{
name: "P₂",
color: 0xff6684,
css: "#ff6684",
weight: 12,
x: 1.15,
z: -1.55
},
{
name: "P₃",
color: 0x70e1ff,
css: "#70e1ff",
weight: 6,
x: 3.65,
z: 2.25
}
];
const loads = [];
const loadMeshes = [];
function createLoad(definition, index) {
const group = new THREE.Group();
group.position.set(definition.x, DECK_Y, definition.z);
scene.add(group);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: definition.color,
roughness: 0.52,
metalness: 0.28,
emissive: definition.color,
emissiveIntensity: 0.08
});
const bandMaterial = new THREE.MeshStandardMaterial({
color: 0x17242c,
roughness: 0.48,
metalness: 0.62
});
const body = new THREE.Mesh(
new THREE.BoxGeometry(1.18, 1.0, 1.18),
bodyMaterial
);
body.position.y = 0.5;
body.castShadow = true;
body.receiveShadow = true;
body.userData.loadId = index;
group.add(body);
loadMeshes.push(body);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(body.geometry),
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.34
})
);
edges.position.copy(body.position);
group.add(edges);
const top = new THREE.Mesh(
new THREE.BoxGeometry(1.24, 0.1, 1.24),
bandMaterial
);
top.position.y = 1.015;
top.castShadow = true;
top.userData.loadId = index;
group.add(top);
loadMeshes.push(top);
for (const x of [-0.49, 0.49]) {
for (const z of [-0.49, 0.49]) {
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 1.06, 0.13),
bandMaterial
);
post.position.set(x, 0.52, z);
post.castShadow = true;
post.userData.loadId = index;
group.add(post);
loadMeshes.push(post);
}
}
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.73, 0.82, 48),
new THREE.MeshBasicMaterial({
color: definition.color,
transparent: true,
opacity: 0.85,
side: THREE.DoubleSide,
depthWrite: false
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.y = 0.018;
ring.visible = index === 0;
group.add(ring);
const arrow = createArrow(definition.color);
const label = createLabel("");
label.style.color = definition.css;
const load = {
...definition,
index,
group,
ring,
arrow,
label,
arrowOrigin: new THREE.Vector3(),
labelAnchor: new THREE.Vector3()
};
loads.push(load);
return load;
}
loadDefinitions.forEach(createLoad);
// Reaction arrows and labels.
const reactionColor = 0x39e6b5;
const reactionCss = "#39e6b5";
for (const support of supportLocations) {
support.arrow = createArrow(reactionColor);
support.label = createLabel("reaction");
support.label.style.color = reactionCss;
support.value = 0;
support.arrowOrigin = new THREE.Vector3();
support.labelAnchor = new THREE.Vector3();
}
// -------------------------------------------------------------------------
// Structural calculations
//
// Rigid deck supported at four corners. Each load contributes:
//
// R(sx,sz) = P/4 * (1 + sx*x/a) * (1 + sz*z/b)
//
// This preserves vertical force equilibrium and the two planar moments.
// -------------------------------------------------------------------------
let selectedLoadIndex = 0;
function updateStructuralModel() {
for (const support of supportLocations) {
support.value = 0;
}
let totalLoad = 0;
for (const load of loads) {
totalLoad += load.weight;
for (const support of supportLocations) {
const xFactor = 1 + support.sx * load.group.position.x / SUPPORT_X;
const zFactor = 1 + support.sz * load.group.position.z / SUPPORT_Z;
support.value +=
(load.weight / 4) * xFactor * zFactor;
}
}
const totalReaction = supportLocations.reduce(
(sum, support) => sum + support.value,
0
);
document.querySelector("#totalLoad").textContent =
`${totalLoad.toFixed(1)} kN`;
document.querySelector("#totalReaction").textContent =
`${totalReaction.toFixed(1)} kN`;
supportLocations.forEach((support, index) => {
document.querySelector(`#r${index}`).textContent =
`${support.value.toFixed(1)} kN`;
});
for (const load of loads) {
const arrowLength = 0.52 + load.weight * 0.047;
const topY = DECK_Y + 1.09;
load.arrowOrigin.set(
load.group.position.x,
topY + arrowLength,
load.group.position.z
);
const arrowTip = new THREE.Vector3(
load.group.position.x,
topY,
load.group.position.z
);
updateArrow(load.arrow, load.arrowOrigin, arrowTip);
load.labelAnchor.copy(load.arrowOrigin);
load.labelAnchor.y += 0.26;
load.label.innerHTML =
`<span>${load.name}</span>${load.weight.toFixed(1)} kN ↓`;
}
for (const support of supportLocations) {
const arrowLength = 0.18 + support.value * 0.145;
support.arrowOrigin.set(
support.x,
0.08,
support.z
);
const arrowTip = new THREE.Vector3(
support.x,
arrowLength,
support.z
);
updateArrow(support.arrow, support.arrowOrigin, arrowTip);
support.labelAnchor.set(
support.x,
arrowLength + 0.25,
support.z
);
support.label.innerHTML =
`<span>R</span>${support.value.toFixed(1)} kN ↑`;
}
updateVisibility();
}
function updateVisibility() {
const arrowsVisible = document.querySelector("#showArrows").checked;
const labelsVisible = document.querySelector("#showLabels").checked;
for (const load of loads) {
load.arrow.group.visible = arrowsVisible;
load.label.style.display = labelsVisible ? "block" : "none";
}
for (const support of supportLocations) {
support.arrow.group.visible =
arrowsVisible && support.value > 0.001;
support.label.style.display = labelsVisible ? "block" : "none";
}
}
// -------------------------------------------------------------------------
// Selection and UI
// -------------------------------------------------------------------------
const weightInput = document.querySelector("#weight");
const weightValue = document.querySelector("#weightValue");
const loadTabs = [...document.querySelectorAll(".load-tab")];
function selectLoad(index) {
selectedLoadIndex = index;
loads.forEach((load, loadIndex) => {
load.ring.visible = loadIndex === index;
});
loadTabs.forEach((tab, tabIndex) => {
tab.classList.toggle("active", tabIndex === index);
});
const load = loads[index];
weightInput.value = load.weight;
weightValue.textContent = `${load.weight.toFixed(1)} kN`;
}
loadTabs.forEach((tab) => {
tab.addEventListener("click", () => {
selectLoad(Number(tab.dataset.load));
});
});
weightInput.addEventListener("input", () => {
const load = loads[selectedLoadIndex];
load.weight = Number(weightInput.value);
weightValue.textContent = `${load.weight.toFixed(1)} kN`;
updateStructuralModel();
});
document.querySelector("#showArrows").addEventListener("change", updateVisibility);
document.querySelector("#showLabels").addEventListener("change", updateVisibility);
document.querySelector("#showGrid").addEventListener("change", () => {
grid.visible = document.querySelector("#showGrid").checked;
});
document.querySelector("#resetLoads").addEventListener("click", () => {
loadDefinitions.forEach((definition, index) => {
loads[index].group.position.set(
definition.x,
DECK_Y,
definition.z
);
loads[index].weight = definition.weight;
});
selectLoad(selectedLoadIndex);
updateStructuralModel();
});
// -------------------------------------------------------------------------
// Custom orbit / pan / zoom controls
// -------------------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(0, 2.65, 0),
radius: 20.5,
theta: 0.72,
phi: 1.04
};
const defaultOrbit = {
target: new THREE.Vector3(0, 2.65, 0),
radius: 20.5,
theta: 0.72,
phi: 1.04
};
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);
camera.updateMatrixWorld();
}
document.querySelector("#resetCamera").addEventListener("click", () => {
orbit.target.copy(defaultOrbit.target);
orbit.radius = defaultOrbit.radius;
orbit.theta = defaultOrbit.theta;
orbit.phi = defaultOrbit.phi;
updateCamera();
});
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -DECK_Y);
const dragPoint = new THREE.Vector3();
const dragOffset = new THREE.Vector3();
let controlMode = null;
let draggingLoad = null;
let lastPointerX = 0;
let lastPointerY = 0;
function setPointerFromEvent(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 pickLoad(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(loadMeshes, false);
return hits.length ? hits[0] : null;
}
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (event.button === 0 && !event.shiftKey) {
const hit = pickLoad(event);
if (hit) {
const loadIndex = hit.object.userData.loadId;
selectLoad(loadIndex);
draggingLoad = loads[loadIndex];
raycaster.ray.intersectPlane(dragPlane, dragPoint);
dragOffset.copy(draggingLoad.group.position).sub(dragPoint);
controlMode = "drag";
canvas.style.cursor = "grabbing";
return;
}
}
controlMode = event.button === 2 || event.shiftKey ? "pan" : "orbit";
canvas.style.cursor = controlMode === "pan" ? "move" : "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (controlMode === "drag" && draggingLoad) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
dragPoint.add(dragOffset);
draggingLoad.group.position.x = THREE.MathUtils.clamp(
dragPoint.x,
-5.45,
5.45
);
draggingLoad.group.position.z = THREE.MathUtils.clamp(
dragPoint.z,
-3.45,
3.45
);
updateStructuralModel();
}
return;
}
if (controlMode === "orbit") {
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.005,
0.35,
1.46
);
updateCamera();
return;
}
if (controlMode === "pan") {
const right = new THREE.Vector3();
const up = new THREE.Vector3();
camera.matrixWorld.extractBasis(right, up, new THREE.Vector3());
const scale =
(2 * orbit.radius * Math.tan(THREE.MathUtils.degToRad(camera.fov / 2))) /
canvas.clientHeight;
orbit.target.addScaledVector(right, -dx * scale);
orbit.target.addScaledVector(up, dy * scale);
orbit.target.x = THREE.MathUtils.clamp(orbit.target.x, -9, 9);
orbit.target.y = THREE.MathUtils.clamp(orbit.target.y, 0, 8);
orbit.target.z = THREE.MathUtils.clamp(orbit.target.z, -9, 9);
updateCamera();
return;
}
// Hover feedback when not actively manipulating the scene.
if (!controlMode) {
const hit = pickLoad(event);
canvas.style.cursor = hit ? "grab" : "default";
}
});
function endPointerInteraction(event) {
controlMode = null;
draggingLoad = null;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
canvas.style.cursor = "default";
}
canvas.addEventListener("pointerup", endPointerInteraction);
canvas.addEventListener("pointercancel", endPointerInteraction);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
8,
36
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Projected DOM force labels
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
element.style.visibility = visible ? "visible" : "hidden";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
function updateLabels() {
for (const load of loads) {
positionLabel(load.label, load.labelAnchor);
}
for (const support of supportLocations) {
positionLabel(support.label, support.labelAnchor);
}
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
selectLoad(0);
updateStructuralModel();
updateCamera();
function animate(time) {
requestAnimationFrame(animate);
// Gentle force-arrow pulse.
const pulse = 1.12 + Math.sin(time * 0.0035) * 0.18;
for (const load of loads) {
load.arrow.material.emissiveIntensity = pulse;
}
for (const support of supportLocations) {
support.arrow.material.emissiveIntensity = pulse * 0.92;
}
updateLabels();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```