Prompt specification
Build or customize the Three.js 3D scene "Beam with two support reactions" 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: Beam with two support reactions
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/beam-with-two-support-reactions-x1433
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows static equilibrium of forces and torques. Build a beam carrying a labeled point load between two supports; calculate ideal support reactions and show both force and moment balance. Low-poly.
Working Three.js Starter (beam-with-two-support-reactions-x1433):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Beam Static Equilibrium</title>
<style>
:root {
color-scheme: dark;
--bg: #09121d;
--panel: rgba(13, 25, 39, 0.9);
--border: rgba(157, 196, 220, 0.2);
--text: #e8f2f8;
--muted: #91a9b9;
--load: #ffad42;
--reaction-a: #50c8ff;
--reaction-b: #67e08b;
--moment: #c69cff;
}
* {
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);
}
canvas {
display: block;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.title {
position: absolute;
top: 22px;
left: 24px;
max-width: 430px;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.55);
}
.eyebrow {
color: var(--reaction-a);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 4px;
font-size: clamp(24px, 3.5vw, 40px);
line-height: 1;
letter-spacing: -0.04em;
}
.subtitle {
margin: 0;
color: #b7cad7;
font-size: 13px;
}
.panel {
position: absolute;
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--border);
border-radius: 16px;
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(14px);
}
.solution {
top: 20px;
right: 20px;
width: min(370px, calc(100vw - 40px));
padding: 18px;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.status {
padding: 5px 8px;
color: #8ef0ae;
background: rgba(74, 207, 121, 0.1);
border: 1px solid rgba(74, 207, 121, 0.25);
border-radius: 999px;
letter-spacing: 0.08em;
}
.reaction-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-bottom: 13px;
}
.reaction {
padding: 11px 12px;
background: rgba(255, 255, 255, 0.035);
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 11px;
}
.reaction span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 11px;
}
.reaction strong {
font-size: 20px;
font-variant-numeric: tabular-nums;
}
.reaction.a strong {
color: var(--reaction-a);
}
.reaction.b strong {
color: var(--reaction-b);
}
.equation {
margin-top: 8px;
padding: 10px 11px;
background: rgba(0, 0, 0, 0.18);
border-left: 3px solid var(--reaction-a);
border-radius: 6px;
}
.equation.moment {
border-left-color: var(--moment);
}
.equation-title {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
color: #cbd9e2;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
}
.equation-title span:last-child {
color: var(--muted);
font-weight: 500;
}
code {
display: block;
color: #f4f8fb;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 11.5px;
line-height: 1.55;
white-space: normal;
}
.checks {
display: flex;
gap: 8px;
margin-top: 11px;
color: var(--muted);
font-size: 11px;
}
.check {
flex: 1;
padding: 8px;
text-align: center;
background: rgba(103, 224, 139, 0.07);
border: 1px solid rgba(103, 224, 139, 0.16);
border-radius: 8px;
}
.check b {
color: var(--reaction-b);
}
.controls {
left: 20px;
bottom: 20px;
width: min(350px, calc(100vw - 40px));
padding: 16px 18px;
}
.control + .control {
margin-top: 14px;
}
.control-label {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
color: #c8d8e2;
font-size: 12px;
}
output {
color: #fff;
font-family: "SFMono-Regular", Consolas, monospace;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 5px 0;
appearance: none;
background: linear-gradient(90deg, #3c708a, #4cc3ed);
border-radius: 999px;
outline: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
appearance: none;
background: #ecfaff;
border: 3px solid #48bfe8;
border-radius: 50%;
cursor: pointer;
box-shadow: 0 0 0 4px rgba(72, 191, 232, 0.12);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
background: #ecfaff;
border: 3px solid #48bfe8;
border-radius: 50%;
cursor: pointer;
}
.control-note {
margin: 5px 0 0;
color: var(--muted);
font-size: 10.5px;
}
.control-footer {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 15px;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: var(--muted);
font-size: 10px;
}
button {
padding: 7px 11px;
color: #dff7ff;
background: rgba(80, 200, 255, 0.09);
border: 1px solid rgba(80, 200, 255, 0.25);
border-radius: 7px;
cursor: pointer;
font: inherit;
font-size: 10px;
}
button:hover {
background: rgba(80, 200, 255, 0.17);
}
.legend {
position: absolute;
right: 22px;
bottom: 22px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 7px 14px;
max-width: 390px;
padding: 10px 13px;
color: #c4d4df;
background: rgba(9, 18, 29, 0.72);
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 10px;
font-size: 10px;
backdrop-filter: blur(9px);
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 17px;
height: 3px;
border-radius: 3px;
}
.swatch.load {
background: var(--load);
}
.swatch.a {
background: var(--reaction-a);
}
.swatch.b {
background: var(--reaction-b);
}
.swatch.moment {
background: var(--moment);
}
.world-label {
position: absolute;
z-index: 8;
pointer-events: none;
transform: translate(-50%, -50%);
padding: 5px 8px;
white-space: nowrap;
background: rgba(8, 17, 28, 0.88);
border: 1px solid currentColor;
border-radius: 6px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.25);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 11px;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
.world-label.load {
color: var(--load);
}
.world-label.a {
color: var(--reaction-a);
}
.world-label.b {
color: var(--reaction-b);
}
.world-label.moment {
color: var(--moment);
font-size: 10px;
}
.world-label.support {
color: #d6e3eb;
font-size: 10px;
}
.world-label.dimension {
color: #a9bdca;
border-color: rgba(169, 189, 202, 0.35);
font-size: 10px;
}
@media (max-width: 760px) {
.title {
top: 14px;
left: 14px;
}
.subtitle {
display: none;
}
.solution {
top: 72px;
right: 12px;
width: min(350px, calc(100vw - 24px));
padding: 13px;
}
.reaction strong {
font-size: 17px;
}
.controls {
left: 12px;
bottom: 12px;
width: min(340px, calc(100vw - 24px));
}
.legend {
display: none;
}
.world-label {
font-size: 9px;
padding: 4px 6px;
}
}
</style>
</head>
<body>
<div class="hud">
<header class="title">
<div class="eyebrow">Structural Mechanics / Free-Body Diagram</div>
<h1>Beam Equilibrium</h1>
<p class="subtitle">Simply supported beam · ideal pin at A · ideal roller at B</p>
</header>
<section class="panel solution">
<div class="panel-title">
<span>Reaction Solution</span>
<span class="status">ΣF = 0 · ΣM = 0</span>
</div>
<div class="reaction-grid">
<div class="reaction a">
<span>R_A · vertical reaction</span>
<strong id="raValue">0.00 kN</strong>
</div>
<div class="reaction b">
<span>R_B · vertical reaction</span>
<strong id="rbValue">0.00 kN</strong>
</div>
</div>
<div class="equation">
<div class="equation-title">
<span>Vertical force balance</span>
<span>ΣF_y = 0</span>
</div>
<code id="forceEquation"></code>
</div>
<div class="equation moment">
<div class="equation-title">
<span>Moment balance about A</span>
<span>ΣM_A = 0</span>
</div>
<code id="momentEquation"></code>
</div>
<div class="checks">
<div class="check">Force residual <b>0.00 kN</b></div>
<div class="check">Moment residual <b>0.00 kN·m</b></div>
</div>
</section>
<section class="panel controls">
<div class="control">
<label class="control-label" for="position">
<span>Load position from A</span>
<output id="positionOutput"></output>
</label>
<input id="position" type="range" min="10" max="90" step="1" value="40" />
<p class="control-note">The load remains between both supports.</p>
</div>
<div class="control">
<label class="control-label" for="load">
<span>Point-load magnitude</span>
<output id="loadOutput"></output>
</label>
<input id="load" type="range" min="5" max="30" step="0.5" value="12" />
</div>
<div class="control-footer">
<span>Drag: orbit · Wheel: zoom · Right-drag: pan</span>
<button id="resetView" type="button">Reset view</button>
</div>
</section>
<aside class="legend">
<div class="legend-item"><span class="swatch load"></span>Point load P</div>
<div class="legend-item"><span class="swatch a"></span>Reaction R_A</div>
<div class="legend-item"><span class="swatch b"></span>Reaction R_B</div>
<div class="legend-item"><span class="swatch moment"></span>Moment vectors</div>
</aside>
</div>
<div id="loadLabel" class="world-label load"></div>
<div id="raLabel" class="world-label a"></div>
<div id="rbLabel" class="world-label b"></div>
<div id="mbLabel" class="world-label moment"></div>
<div id="mpLabel" class="world-label moment"></div>
<div id="aLabel" class="world-label support">A · PIN</div>
<div id="bLabel" class="world-label support">B · ROLLER</div>
<div id="spanLabel" class="world-label dimension"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Scene
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x09121d);
scene.fog = new THREE.Fog(0x09121d, 13, 27);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb9ddff, 0x162231, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffffff, 3.1);
sun.position.set(4, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -5;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 30;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x4baeff, 1.25);
rim.position.set(-7, 4, -6);
scene.add(rim);
const fill = new THREE.PointLight(0xffa64d, 25, 12, 2);
fill.position.set(0, 5, 4);
scene.add(fill);
const world = new THREE.Group();
scene.add(world);
// ---------------------------------------------------------------------
// Constants and mechanics
// ---------------------------------------------------------------------
const SPAN = 6.0; // meters
const BEAM_Y = 2.20;
const BEAM_HEIGHT = 0.34;
const BEAM_TOP = BEAM_Y + BEAM_HEIGHT / 2;
const BEAM_BOTTOM = BEAM_Y - BEAM_HEIGHT / 2;
const X_A = -SPAN / 2;
const X_B = SPAN / 2;
const FORCE_SCALE = 0.10; // scene units per kN
const colors = {
beam: 0x78909c,
beamEdge: 0x455d69,
support: 0x294657,
supportLight: 0x3e667b,
load: 0xffad42,
reactionA: 0x50c8ff,
reactionB: 0x67e08b,
moment: 0xc69cff,
ground: 0x111f2b
};
const loadInput = document.querySelector("#position");
const loadMagnitudeInput = document.querySelector("#load");
const positionOutput = document.querySelector("#positionOutput");
const loadOutput = document.querySelector("#loadOutput");
let P = Number(loadMagnitudeInput.value);
let positionPercent = Number(loadInput.value);
let a = SPAN * positionPercent / 100;
let xLoad = X_A + a;
let RA = 0;
let RB = 0;
// ---------------------------------------------------------------------
// Low-poly environment
// ---------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.BoxGeometry(13, 0.20, 7),
new THREE.MeshStandardMaterial({
color: colors.ground,
roughness: 0.95,
metalness: 0.03,
flatShading: true
})
);
ground.position.y = -0.12;
ground.receiveShadow = true;
world.add(ground);
const grid = new THREE.GridHelper(12, 24, 0x496879, 0x263b49);
grid.position.y = 0.005;
grid.material.transparent = true;
grid.material.opacity = 0.38;
world.add(grid);
const beam = new THREE.Mesh(
new THREE.BoxGeometry(SPAN + 1.0, BEAM_HEIGHT, 0.78),
new THREE.MeshStandardMaterial({
color: colors.beam,
roughness: 0.72,
metalness: 0.12,
flatShading: true
})
);
beam.position.y = BEAM_Y;
beam.castShadow = true;
beam.receiveShadow = true;
world.add(beam);
const beamTopStrip = new THREE.Mesh(
new THREE.BoxGeometry(SPAN + 0.92, 0.045, 0.72),
new THREE.MeshStandardMaterial({
color: 0x9eb3bd,
roughness: 0.68,
metalness: 0.1,
flatShading: true
})
);
beamTopStrip.position.y = BEAM_TOP + 0.018;
beamTopStrip.castShadow = true;
world.add(beamTopStrip);
const seamMaterial = new THREE.MeshStandardMaterial({
color: colors.beamEdge,
roughness: 0.85,
flatShading: true
});
for (let x = -2.6; x <= 2.61; x += 1.3) {
const seam = new THREE.Mesh(
new THREE.BoxGeometry(0.035, BEAM_HEIGHT + 0.015, 0.80),
seamMaterial
);
seam.position.set(x, BEAM_Y, 0);
world.add(seam);
}
function makeSupportTriangle(width, height, depth, material) {
const shape = new THREE.Shape();
shape.moveTo(-width / 2, 0);
shape.lineTo(width / 2, 0);
shape.lineTo(0, height);
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
bevelEnabled: false,
steps: 1
});
geometry.translate(0, 0, -depth / 2);
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
const supportMaterial = new THREE.MeshStandardMaterial({
color: colors.support,
roughness: 0.78,
metalness: 0.18,
flatShading: true
});
const supportAccent = new THREE.MeshStandardMaterial({
color: colors.supportLight,
roughness: 0.65,
metalness: 0.25,
flatShading: true
});
// Pin support A
const pinSupport = new THREE.Group();
pinSupport.position.x = X_A;
const pinBaseY = 0.16;
const pinTriangle = makeSupportTriangle(
1.05,
BEAM_BOTTOM - pinBaseY,
0.52,
supportMaterial
);
pinTriangle.position.y = pinBaseY;
pinSupport.add(pinTriangle);
const pinBase = new THREE.Mesh(
new THREE.BoxGeometry(1.28, 0.16, 0.92),
supportAccent
);
pinBase.position.y = 0.08;
pinBase.castShadow = true;
pinBase.receiveShadow = true;
pinSupport.add(pinBase);
const hinge = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.66, 12),
new THREE.MeshStandardMaterial({
color: 0x7ca5bb,
roughness: 0.4,
metalness: 0.65,
flatShading: true
})
);
hinge.rotation.x = Math.PI / 2;
hinge.position.y = BEAM_BOTTOM;
hinge.castShadow = true;
pinSupport.add(hinge);
world.add(pinSupport);
// Roller support B
const rollerSupport = new THREE.Group();
rollerSupport.position.x = X_B;
const rollerBaseY = 0.53;
const rollerTriangle = makeSupportTriangle(
0.96,
BEAM_BOTTOM - rollerBaseY,
0.48,
supportMaterial
);
rollerTriangle.position.y = rollerBaseY;
rollerSupport.add(rollerTriangle);
const rollerBase = new THREE.Mesh(
new THREE.BoxGeometry(1.24, 0.12, 0.94),
supportAccent
);
rollerBase.position.y = 0.06;
rollerBase.castShadow = true;
rollerBase.receiveShadow = true;
rollerSupport.add(rollerBase);
const wheelMaterial = new THREE.MeshStandardMaterial({
color: 0x6d8794,
roughness: 0.48,
metalness: 0.52,
flatShading: true
});
for (const dx of [-0.24, 0.24]) {
const wheel = new THREE.Mesh(
new THREE.CylinderGeometry(0.17, 0.17, 0.66, 12),
wheelMaterial
);
wheel.rotation.x = Math.PI / 2;
wheel.position.set(dx, 0.25, 0);
wheel.castShadow = true;
rollerSupport.add(wheel);
}
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 0.72, 8),
new THREE.MeshStandardMaterial({
color: 0x9bb3bf,
metalness: 0.72,
roughness: 0.32
})
);
axle.rotation.z = Math.PI / 2;
axle.position.y = 0.25;
rollerSupport.add(axle);
world.add(rollerSupport);
// Load application marker
const loadMarker = new THREE.Mesh(
new THREE.BoxGeometry(0.24, 0.10, 0.30),
new THREE.MeshStandardMaterial({
color: colors.load,
roughness: 0.48,
metalness: 0.14,
flatShading: true
})
);
loadMarker.position.set(xLoad, BEAM_TOP + 0.04, 0);
loadMarker.castShadow = true;
world.add(loadMarker);
// Span dimension line
const dimensionMaterial = new THREE.LineBasicMaterial({
color: 0x7694a5,
transparent: true,
opacity: 0.8
});
const dimensionPoints = [
new THREE.Vector3(X_A, 0.94, 0.48),
new THREE.Vector3(X_B, 0.94, 0.48),
new THREE.Vector3(X_A, 0.79, 0.48),
new THREE.Vector3(X_A, 1.09, 0.48),
new THREE.Vector3(X_B, 0.79, 0.48),
new THREE.Vector3(X_B, 1.09, 0.48)
];
const dimensionGeometry = new THREE.BufferGeometry().setFromPoints(dimensionPoints);
const dimensionLines = new THREE.LineSegments(
dimensionGeometry,
dimensionMaterial
);
world.add(dimensionLines);
// ---------------------------------------------------------------------
// Dynamic force and moment graphics
// ---------------------------------------------------------------------
const forceGroup = new THREE.Group();
const momentGroup = new THREE.Group();
world.add(forceGroup, momentGroup);
function disposeGroup(group) {
while (group.children.length) {
const child = group.children.pop();
child.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
if (Array.isArray(object.material)) {
object.material.forEach((material) => material.dispose());
} else {
object.material.dispose();
}
}
});
}
}
function makeForceArrow(tail, direction, length, color) {
const group = new THREE.Group();
const dir = direction.clone().normalize();
const headLength = THREE.MathUtils.clamp(length * 0.23, 0.13, 0.27);
const shaftLength = Math.max(0.08, length - headLength);
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.36,
metalness: 0.22,
flatShading: true
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.048, 0.048, 1, 8),
material
);
shaft.scale.y = shaftLength;
shaft.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
dir
);
shaft.position.copy(tail).addScaledVector(dir, shaftLength / 2);
shaft.castShadow = true;
group.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.125, headLength, 8),
material
);
head.quaternion.copy(shaft.quaternion);
head.position
.copy(tail)
.addScaledVector(dir, shaftLength + headLength / 2);
head.castShadow = true;
group.add(head);
return group;
}
class ArcCurve extends THREE.Curve {
constructor(center, radius, startAngle, endAngle) {
super();
this.center = center;
this.radius = radius;
this.startAngle = startAngle;
this.endAngle = endAngle;
}
getPoint(t, optionalTarget = new THREE.Vector3()) {
const angle = THREE.MathUtils.lerp(
this.startAngle,
this.endAngle,
t
);
return optionalTarget.set(
this.center.x + Math.cos(angle) * this.radius,
this.center.y + Math.sin(angle) * this.radius,
this.center.z
);
}
}
function makeMomentArrow(center, radius, startAngle, endAngle, color) {
const group = new THREE.Group();
const curve = new ArcCurve(center, radius, startAngle, endAngle);
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.38,
metalness: 0.18,
flatShading: true,
transparent: true,
opacity: 0.92
});
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 36, 0.026, 5, false),
material
);
tube.renderOrder = 5;
group.add(tube);
const end = curve.getPoint(1);
const previous = curve.getPoint(0.985);
const tangent = end.clone().sub(previous).normalize();
const headLength = 0.21;
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.095, headLength, 6),
material
);
head.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
head.position.copy(end).addScaledVector(tangent, -headLength / 2);
head.renderOrder = 6;
group.add(head);
return group;
}
// ---------------------------------------------------------------------
// HTML world labels
// ---------------------------------------------------------------------
const labels = {
load: {
element: document.querySelector("#loadLabel"),
position: new THREE.Vector3()
},
ra: {
element: document.querySelector("#raLabel"),
position: new THREE.Vector3()
},
rb: {
element: document.querySelector("#rbLabel"),
position: new THREE.Vector3()
},
mb: {
element: document.querySelector("#mbLabel"),
position: new THREE.Vector3()
},
mp: {
element: document.querySelector("#mpLabel"),
position: new THREE.Vector3()
},
a: {
element: document.querySelector("#aLabel"),
position: new THREE.Vector3(X_A, 0.30, 0.48)
},
b: {
element: document.querySelector("#bLabel"),
position: new THREE.Vector3(X_B, 0.30, 0.48)
},
span: {
element: document.querySelector("#spanLabel"),
position: new THREE.Vector3(0, 1.16, 0.48)
}
};
function placeWorldLabel(label) {
const projected = label.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.element.style.display = visible ? "block" : "none";
if (!visible) return;
label.element.style.left = `${(projected.x * 0.5 + 0.5) * innerWidth}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
function cleanNumber(value, digits = 2) {
if (Math.abs(value) < 0.5 * Math.pow(10, -digits)) value = 0;
return value.toFixed(digits);
}
function updateModel() {
P = Number(loadMagnitudeInput.value);
positionPercent = Number(loadInput.value);
a = SPAN * positionPercent / 100;
xLoad = X_A + a;
// Ideal simply supported beam:
// RA + RB = P
// RB*L - P*a = 0
RA = P * (SPAN - a) / SPAN;
RB = P * a / SPAN;
loadMarker.position.x = xLoad;
positionOutput.textContent =
`${positionPercent}% · a = ${cleanNumber(a)} m`;
loadOutput.textContent = `${cleanNumber(P, 1)} kN`;
document.querySelector("#raValue").textContent =
`${cleanNumber(RA)} kN`;
document.querySelector("#rbValue").textContent =
`${cleanNumber(RB)} kN`;
const forceResidual = RA + RB - P;
const momentResidual = RB * SPAN - P * a;
document.querySelector("#forceEquation").innerHTML =
`R_A + R_B − P<br>` +
`${cleanNumber(RA)} + ${cleanNumber(RB)} − ${cleanNumber(P)} = ` +
`${cleanNumber(forceResidual)} kN`;
document.querySelector("#momentEquation").innerHTML =
`R_B·L − P·a<br>` +
`${cleanNumber(RB)} × ${cleanNumber(SPAN)} − ` +
`${cleanNumber(P)} × ${cleanNumber(a)} = ` +
`${cleanNumber(momentResidual)} kN·m`;
// Rebuild force vectors.
disposeGroup(forceGroup);
const loadLength = Math.max(0.42, FORCE_SCALE * P);
const raLength = Math.max(0.42, FORCE_SCALE * RA);
const rbLength = Math.max(0.42, FORCE_SCALE * RB);
const vectorZ = 0.62;
const loadArrow = makeForceArrow(
new THREE.Vector3(xLoad, BEAM_TOP + loadLength, vectorZ),
new THREE.Vector3(0, -1, 0),
loadLength,
colors.load
);
forceGroup.add(loadArrow);
const raArrow = makeForceArrow(
new THREE.Vector3(X_A, BEAM_TOP, vectorZ),
new THREE.Vector3(0, 1, 0),
raLength,
colors.reactionA
);
forceGroup.add(raArrow);
const rbArrow = makeForceArrow(
new THREE.Vector3(X_B, BEAM_TOP, vectorZ),
new THREE.Vector3(0, 1, 0),
rbLength,
colors.reactionB
);
forceGroup.add(rbArrow);
// Rebuild moment vectors about A.
disposeGroup(momentGroup);
const momentCenter = new THREE.Vector3(X_A, BEAM_Y, 0.79);
// R_B * L is counter-clockwise.
momentGroup.add(
makeMomentArrow(
momentCenter,
1.05,
0.18,
2.72,
colors.moment
)
);
// -P * a is clockwise.
momentGroup.add(
makeMomentArrow(
momentCenter,
1.48,
-0.18,
-2.72,
colors.moment
)
);
labels.load.position.set(
xLoad,
BEAM_TOP + loadLength + 0.30,
vectorZ
);
labels.ra.position.set(
X_A,
BEAM_TOP + raLength + 0.25,
vectorZ
);
labels.rb.position.set(
X_B,
BEAM_TOP + rbLength + 0.25,
vectorZ
);
labels.mb.position.set(
X_A - 0.38,
BEAM_Y + 1.08,
0.82
);
labels.mp.position.set(
X_A - 0.93,
BEAM_Y - 1.12,
0.82
);
labels.load.element.textContent =
`P = ${cleanNumber(P, 1)} kN ↓`;
labels.ra.element.textContent =
`R_A = ${cleanNumber(RA)} kN ↑`;
labels.rb.element.textContent =
`R_B = ${cleanNumber(RB)} kN ↑`;
labels.mb.element.textContent =
`+M_B = R_B·L`;
labels.mp.element.textContent =
`−M_P = P·a`;
labels.span.element.textContent =
`L = ${cleanNumber(SPAN)} m`;
}
// ---------------------------------------------------------------------
// Direct camera controls: orbit, zoom, pan
// ---------------------------------------------------------------------
const orbit = {
theta: 0.28,
phi: 1.08,
radius: 11.5,
target: new THREE.Vector3(0, 1.75, 0)
};
const defaultOrbit = {
theta: 0.28,
phi: 1.08,
radius: 11.5,
target: new THREE.Vector3(0, 1.75, 0)
};
function updateCamera() {
orbit.phi = THREE.MathUtils.clamp(orbit.phi, 0.28, 1.46);
orbit.radius = THREE.MathUtils.clamp(orbit.radius, 6.5, 20);
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();
}
function resetCamera() {
orbit.theta = defaultOrbit.theta;
orbit.phi = defaultOrbit.phi;
orbit.radius = defaultOrbit.radius;
orbit.target.copy(defaultOrbit.target);
updateCamera();
}
const canvas = renderer.domElement;
let dragging = false;
let dragMode = "orbit";
let lastX = 0;
let lastY = 0;
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
dragMode = event.button === 2 || event.shiftKey ? "pan" : "orbit";
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (dragMode === "orbit") {
orbit.theta -= dx * 0.006;
orbit.phi -= dy * 0.006;
} else {
camera.updateMatrixWorld();
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0);
const up = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 1);
const factor = orbit.radius * 0.0014;
orbit.target
.addScaledVector(right, -dx * factor)
.addScaledVector(up, dy * factor);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius *= Math.exp(event.deltaY * 0.001);
updateCamera();
},
{ passive: false }
);
document.querySelector("#resetView").addEventListener("click", resetCamera);
// ---------------------------------------------------------------------
// UI and render loop
// ---------------------------------------------------------------------
loadInput.addEventListener("input", updateModel);
loadMagnitudeInput.addEventListener("input", updateModel);
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
}
addEventListener("resize", resize);
updateCamera();
updateModel();
function render() {
renderer.render(scene, camera);
Object.values(labels).forEach(placeWorldLabel);
requestAnimationFrame(render);
}
render();
</script>
</body>
</html>
```