Prompt specification
Build or customize the Three.js 3D scene "Aligned and random fiber comparison" 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: Aligned and random fiber comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/aligned-and-random-fiber-comparison-x1938
Scene Description Prompt:
Place two composite coupons with different fiber orientations under a labeled toy load model; show directional response while avoiding invented real strength values. Use Three.js from cdnjs in a single HTML file, with a 3D cutaway.
Working Three.js Starter (aligned-and-random-fiber-comparison-x1938):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Composite Coupon Orientation — Toy Load Cutaway</title>
<style>
:root {
--ink: #152633;
--muted: #5e7080;
--panel: rgba(255, 255, 255, 0.91);
--line: rgba(35, 62, 80, 0.14);
--teal: #0c9f98;
--violet: #675ee8;
--orange: #f08a36;
--danger: #e65454;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
background:
radial-gradient(circle at 72% 18%, rgba(255, 255, 255, 0.95), transparent 34%),
linear-gradient(145deg, #edf3f6 0%, #dfe9ee 54%, #d5e1e7 100%);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.topbar {
position: absolute;
top: 22px;
left: 24px;
max-width: min(560px, calc(100vw - 48px));
padding: 16px 18px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 16px;
background: rgba(255, 255, 255, 0.72);
box-shadow: 0 12px 34px rgba(36, 62, 78, 0.10);
backdrop-filter: blur(14px);
}
.eyebrow {
margin-bottom: 5px;
color: #557080;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 34px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.controls {
position: absolute;
top: 22px;
right: 24px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
pointer-events: auto;
}
button {
min-height: 38px;
padding: 9px 13px;
border: 1px solid rgba(30, 57, 73, 0.16);
border-radius: 11px;
background: rgba(255, 255, 255, 0.91);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
box-shadow: 0 6px 18px rgba(35, 61, 77, 0.08);
transition:
transform 120ms ease,
background 120ms ease;
}
button:hover {
transform: translateY(-1px);
background: white;
}
button:active {
transform: translateY(0);
}
button.primary {
border-color: transparent;
background: #17384a;
color: white;
}
.info-panel {
position: absolute;
bottom: 22px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 16px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 17px;
background: var(--panel);
box-shadow: 0 16px 42px rgba(35, 60, 75, 0.13);
backdrop-filter: blur(16px);
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.status {
display: inline-flex;
align-items: center;
gap: 7px;
color: #405969;
font-size: 12px;
font-weight: 750;
}
.status::before {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 5px rgba(240, 138, 54, 0.12);
content: "";
}
.coupon-card {
display: grid;
grid-template-columns: 39px 1fr;
gap: 11px;
padding: 11px 0;
border-top: 1px solid var(--line);
}
.coupon-card:last-of-type {
padding-bottom: 8px;
}
.angle {
display: grid;
width: 39px;
height: 39px;
place-items: center;
border-radius: 11px;
color: white;
font-size: 13px;
font-weight: 850;
}
.angle.a {
background: var(--teal);
}
.angle.b {
background: var(--violet);
}
.coupon-card strong {
display: block;
margin: 1px 0 3px;
font-size: 13px;
}
.coupon-card p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.42;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin: 5px 0 11px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
border: 1px solid var(--line);
border-radius: 999px;
color: #516674;
font-size: 10px;
font-weight: 750;
}
.swatch {
width: 8px;
height: 8px;
border-radius: 50%;
}
.disclaimer {
margin: 0;
padding-top: 11px;
border-top: 1px solid var(--line);
color: #627481;
font-size: 10.5px;
line-height: 1.45;
}
.disclaimer strong {
color: #344c5b;
}
#labels {
position: absolute;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.scene-label {
position: absolute;
z-index: 3;
min-width: 126px;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 10px;
background: rgba(255, 255, 255, 0.88);
box-shadow: 0 7px 20px rgba(30, 53, 67, 0.13);
color: #203846;
font-size: 11px;
line-height: 1.25;
text-align: center;
transform: translate(-50%, -100%);
backdrop-filter: blur(8px);
}
.scene-label::after {
position: absolute;
bottom: -6px;
left: 50%;
width: 10px;
height: 10px;
border-right: 1px solid rgba(255, 255, 255, 0.8);
border-bottom: 1px solid rgba(255, 255, 255, 0.8);
background: rgba(255, 255, 255, 0.88);
content: "";
transform: translateX(-50%) rotate(45deg);
}
.scene-label b {
display: block;
font-size: 12px;
}
.scene-label.a {
border-top: 3px solid var(--teal);
}
.scene-label.b {
border-top: 3px solid var(--violet);
}
.scene-label.load {
min-width: 145px;
border-top: 3px solid var(--danger);
}
.scene-label.load::after {
border-top-color: var(--danger);
}
.hint {
position: absolute;
right: 24px;
bottom: 22px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 11px;
background: rgba(255, 255, 255, 0.72);
color: #637684;
font-size: 10px;
font-weight: 650;
backdrop-filter: blur(10px);
}
.fallback {
position: fixed;
inset: 0;
z-index: 20;
display: none;
place-items: center;
padding: 30px;
background: #e5edf1;
text-align: center;
}
@media (max-width: 780px) {
.topbar {
top: 12px;
left: 12px;
right: 12px;
max-width: none;
padding: 13px 14px;
}
.controls {
top: 116px;
right: 12px;
justify-content: flex-start;
}
.info-panel {
bottom: 12px;
left: 12px;
width: calc(100vw - 24px);
max-height: 42vh;
overflow: auto;
padding: 13px;
}
.hint {
display: none;
}
.scene-label {
min-width: 105px;
padding: 6px 8px;
font-size: 9px;
}
.scene-label b {
font-size: 10px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D cutaway of two composite coupons"></canvas>
<div class="overlay">
<header class="topbar">
<div class="eyebrow">Qualitative mechanics toy</div>
<h1>Fiber orientation changes the displayed response</h1>
<p class="subtitle">
Two cutaway composite coupons sit under identical labeled toy loads.
The bending animation is intentionally exaggerated to reveal directionality.
</p>
</header>
<nav class="controls" aria-label="3D controls">
<button id="cycleButton" class="primary" type="button">Pause toy cycle</button>
<button id="resetButton" type="button">Reset view</button>
<button id="zoomInButton" type="button" aria-label="Zoom in">+</button>
<button id="zoomOutButton" type="button" aria-label="Zoom out">−</button>
</nav>
<div id="labels" aria-hidden="true"></div>
<aside class="info-panel">
<div class="status-row">
<div class="eyebrow" style="margin: 0">Comparison</div>
<div id="responseStatus" class="status" aria-live="polite">Toy load applied</div>
</div>
<div class="legend">
<span><i class="swatch" style="background: var(--teal)"></i>Coupon A fibers</span>
<span><i class="swatch" style="background: var(--violet)"></i>Coupon B fibers</span>
<span><i class="swatch" style="background: var(--danger)"></i>Toy load</span>
</div>
<div class="coupon-card">
<div class="angle a">0°</div>
<div>
<strong>Coupon A — fibers along the loading span</strong>
<p>Longitudinal fibers are exposed by the cutaway. This orientation is shown with a smaller displayed bend.</p>
</div>
</div>
<div class="coupon-card">
<div class="angle b">90°</div>
<div>
<strong>Coupon B — fibers across the loading span</strong>
<p>Transverse fibers are exposed by the cutaway. This orientation is shown with a larger displayed bend.</p>
</div>
</div>
<p class="disclaimer">
<strong>Illustration only.</strong> This is a labeled toy-load model, not a structural simulation.
No stiffness, stress, capacity, failure load, or real-world strength values are represented or implied.
</p>
</aside>
<div class="hint">Drag to orbit · Shift/right-drag to pan · Wheel to zoom · R to reset</div>
</div>
<div id="fallback" class="fallback">
<div>
<h2>3D view unavailable</h2>
<p>This demonstration requires WebGL and access to the Three.js CDN module.</p>
</div>
</div>
<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 labelsRoot = document.querySelector("#labels");
const cycleButton = document.querySelector("#cycleButton");
const resetButton = document.querySelector("#resetButton");
const zoomInButton = document.querySelector("#zoomInButton");
const zoomOutButton = document.querySelector("#zoomOutButton");
const responseStatus = document.querySelector("#responseStatus");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
} catch (error) {
document.querySelector("#fallback").style.display = "grid";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xe4edf1, 12, 23);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 0.18, 0);
const spherical = new THREE.Spherical(10.8, 1.04, 0.68);
const cameraOffset = new THREE.Vector3();
function updateCamera() {
spherical.makeSafe();
cameraOffset.setFromSpherical(spherical).add(cameraTarget);
camera.position.copy(cameraOffset);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xf7fbff, 0x78909c, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.5);
keyLight.position.set(5.5, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.00015;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x93c9ff, 1.35);
rimLight.position.set(-7, 4, -6);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffc27a, 18, 12, 2);
warmLight.position.set(4, 3.5, 3);
scene.add(warmLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 24),
new THREE.MeshStandardMaterial({
color: 0xdbe6eb,
roughness: 0.96,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.96;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 48, 0xb9cbd3, 0xcbd9df);
grid.position.y = -0.952;
grid.material.transparent = true;
grid.material.opacity = 0.48;
scene.add(grid);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(9.1, 0.34, 6.1),
new THREE.MeshStandardMaterial({
color: 0x526a78,
roughness: 0.66,
metalness: 0.12
})
);
platform.position.set(0, -0.76, 0);
platform.castShadow = true;
platform.receiveShadow = true;
scene.add(platform);
const platformTop = new THREE.Mesh(
new THREE.BoxGeometry(8.72, 0.08, 5.72),
new THREE.MeshStandardMaterial({
color: 0x718793,
roughness: 0.72,
metalness: 0.08
})
);
platformTop.position.set(0, -0.55, 0);
platformTop.receiveShadow = true;
scene.add(platformTop);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const fiberGeometry = new THREE.CylinderGeometry(1, 1, 1, 8, 1);
const darkMetal = new THREE.MeshStandardMaterial({
color: 0x263d4a,
roughness: 0.38,
metalness: 0.55
});
const blueMetal = new THREE.MeshStandardMaterial({
color: 0x547b8d,
roughness: 0.42,
metalness: 0.42
});
const orangeMaterial = new THREE.MeshStandardMaterial({
color: 0xf18b35,
roughness: 0.36,
metalness: 0.12
});
const matrixMaterial = new THREE.MeshPhysicalMaterial({
color: 0xd5a46b,
roughness: 0.28,
metalness: 0,
transparent: true,
opacity: 0.29,
depthWrite: false,
clearcoat: 0.45,
clearcoatRoughness: 0.28,
side: THREE.DoubleSide
});
const upperMatrixMaterial = matrixMaterial.clone();
upperMatrixMaterial.opacity = 0.23;
upperMatrixMaterial.color.set(0xe3b982);
const cutEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0xe89b50,
roughness: 0.42,
metalness: 0.06
});
const tealFiberMaterial = new THREE.MeshStandardMaterial({
color: 0x10aaa1,
roughness: 0.28,
metalness: 0.12,
emissive: 0x073e3b,
emissiveIntensity: 0.18
});
const violetFiberMaterial = new THREE.MeshStandardMaterial({
color: 0x6a61e9,
roughness: 0.28,
metalness: 0.12,
emissive: 0x211b67,
emissiveIntensity: 0.18
});
function addBox(size, position, material, parent = scene) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size.x, size.y, size.z);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createLane(z, accent) {
const lane = new THREE.Mesh(
new THREE.BoxGeometry(7.4, 0.075, 1.95),
new THREE.MeshStandardMaterial({
color: 0xa7bbc4,
roughness: 0.8,
metalness: 0.03
})
);
lane.position.set(0, -0.49, z);
lane.receiveShadow = true;
scene.add(lane);
const accentStrip = new THREE.Mesh(
new THREE.BoxGeometry(7.25, 0.025, 0.045),
new THREE.MeshStandardMaterial({
color: accent,
roughness: 0.5,
emissive: accent,
emissiveIntensity: 0.08
})
);
accentStrip.position.set(0, -0.44, z + 0.86);
scene.add(accentStrip);
}
createLane(1.42, 0x0c9f98);
createLane(-1.42, 0x675ee8);
class HelixCurve extends THREE.Curve {
constructor(radius, height, turns) {
super();
this.radius = radius;
this.height = height;
this.turns = turns;
}
getPoint(t, target = new THREE.Vector3()) {
const angle = t * Math.PI * 2 * this.turns;
return target.set(
Math.cos(angle) * this.radius,
-t * this.height,
Math.sin(angle) * this.radius
);
}
}
function createLoadRig(z) {
const group = new THREE.Group();
const post = addBox(
new THREE.Vector3(0.2, 2.15, 0.2),
new THREE.Vector3(3.12, 0.47, z - 0.84),
blueMetal,
group
);
addBox(
new THREE.Vector3(0.55, 0.13, 0.5),
new THREE.Vector3(3.12, -0.48, z - 0.84),
darkMetal,
group
);
addBox(
new THREE.Vector3(0.82, 0.15, 0.18),
new THREE.Vector3(2.78, 1.58, z - 0.84),
blueMetal,
group
);
addBox(
new THREE.Vector3(0.16, 0.15, 1.02),
new THREE.Vector3(2.52, 1.58, z - 0.38),
blueMetal,
group
);
const springGeometry = new THREE.TubeGeometry(
new HelixCurve(0.095, 0.22, 4.5),
64,
0.018,
7,
false
);
const spring = new THREE.Mesh(
springGeometry,
new THREE.MeshStandardMaterial({
color: 0xd7e0e5,
roughness: 0.25,
metalness: 0.78
})
);
spring.position.set(2.52, 1.51, z);
spring.castShadow = true;
group.add(spring);
const weight = new THREE.Group();
const block = addBox(
new THREE.Vector3(0.43, 0.31, 0.43),
new THREE.Vector3(0, 0, 0),
orangeMaterial,
weight
);
block.castShadow = true;
addBox(
new THREE.Vector3(0.46, 0.055, 0.46),
new THREE.Vector3(0, 0.06, 0),
new THREE.MeshStandardMaterial({
color: 0xffd29e,
roughness: 0.48
}),
weight
);
weight.position.set(2.52, 1.24, z);
group.add(weight);
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(2.52, 0.94, z),
0.56,
0xe65454,
0.17,
0.12
);
arrow.line.material.transparent = true;
arrow.line.material.opacity = 0.9;
arrow.cone.material.transparent = true;
arrow.cone.material.opacity = 0.94;
group.add(arrow);
scene.add(group);
return { group, weight, arrow };
}
function createClamp(z) {
addBox(
new THREE.Vector3(0.22, 1.16, 1.55),
new THREE.Vector3(-2.92, -0.04, z),
darkMetal
);
addBox(
new THREE.Vector3(0.48, 0.15, 1.53),
new THREE.Vector3(-2.68, 0.27, z),
darkMetal
);
addBox(
new THREE.Vector3(0.48, 0.15, 1.53),
new THREE.Vector3(-2.68, -0.27, z),
darkMetal
);
addBox(
new THREE.Vector3(0.25, 0.43, 0.32),
new THREE.Vector3(-2.81, -0.34, z - 0.59),
blueMetal
);
}
class CompositeCoupon {
constructor({ z, orientation, fiberMaterial, bendMagnitude }) {
this.z = z;
this.orientation = orientation;
this.fiberMaterial = fiberMaterial;
this.bendMagnitude = bendMagnitude;
this.startX = -2.55;
this.endX = 2.55;
this.length = this.endX - this.startX;
this.segmentCount = 34;
this.segmentLength = this.length / this.segmentCount;
this.lowerSegments = [];
this.upperSegments = [];
this.edgeSegments = [];
this.railSegments = [];
this.fiberSegments = [];
this.group = new THREE.Group();
scene.add(this.group);
this.createMatrix();
this.createFibers();
const axisArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-2.52, -0.38, z + 0.68),
1.18,
orientation === 0 ? 0x0c9f98 : 0x675ee8,
0.18,
0.12
);
this.group.add(axisArrow);
}
createMatrix() {
for (let i = 0; i < this.segmentCount; i++) {
const lower = new THREE.Mesh(unitBox, matrixMaterial);
lower.castShadow = true;
lower.receiveShadow = true;
this.group.add(lower);
this.lowerSegments.push(lower);
const upper = new THREE.Mesh(unitBox, upperMatrixMaterial);
upper.castShadow = true;
upper.receiveShadow = true;
this.group.add(upper);
this.upperSegments.push(upper);
const edge = new THREE.Mesh(unitBox, cutEdgeMaterial);
edge.castShadow = true;
this.group.add(edge);
this.edgeSegments.push(edge);
const rail = new THREE.Mesh(unitBox, upperMatrixMaterial);
rail.castShadow = true;
this.group.add(rail);
this.railSegments.push(rail);
}
}
createFibers() {
if (this.orientation === 0) {
const zPositions = [-0.48, -0.25, -0.02, 0.22, 0.46];
const yPositions = [-0.085, 0.085];
const fiberSubdivisions = 13;
for (const z of zPositions) {
for (const y of yPositions) {
for (let i = 0; i < fiberSubdivisions; i++) {
const fiber = new THREE.Mesh(fiberGeometry, this.fiberMaterial);
fiber.castShadow = true;
this.group.add(fiber);
this.fiberSegments.push({
mesh: fiber,
mode: "longitudinal",
z,
y,
index: i,
count: fiberSubdivisions
});
}
}
}
} else {
const xPositions = [-2.2, -1.65, -1.1, -0.55, 0, 0.55, 1.1, 1.65, 2.2];
const yPositions = [-0.085, 0.085];
for (const x of xPositions) {
for (const y of yPositions) {
const fiber = new THREE.Mesh(fiberGeometry, this.fiberMaterial);
fiber.castShadow = true;
this.group.add(fiber);
this.fiberSegments.push({
mesh: fiber,
mode: "transverse",
x,
y
});
}
}
}
}
bendAt(x, response) {
const u = THREE.MathUtils.clamp(
(x - this.startX) / this.length,
0,
1
);
return (
-this.bendMagnitude *
response *
(0.08 * u + 0.92 * u * u)
);
}
slopeAt(x, response) {
const u = THREE.MathUtils.clamp(
(x - this.startX) / this.length,
0,
1
);
return (
-this.bendMagnitude *
response *
(0.08 + 1.84 * u) /
this.length
);
}
updateSegment(mesh, index, response, baseY, baseZ, height, width) {
const u0 = index / this.segmentCount;
const u1 = (index + 1) / this.segmentCount;
const x0 = this.startX + u0 * this.length;
const x1 = this.startX + u1 * this.length;
const x = (x0 + x1) * 0.5;
const y = this.bendAt(x, response);
const slope = this.slopeAt(x, response);
const angle = Math.atan(slope);
const curvedLength = this.segmentLength * Math.sqrt(1 + slope * slope);
mesh.position.set(x, y + baseY, baseZ);
mesh.rotation.set(0, 0, angle);
mesh.scale.set(curvedLength, height, width);
}
update(response) {
for (let i = 0; i < this.segmentCount; i++) {
// Full lower laminate.
this.updateSegment(
this.lowerSegments[i],
i,
response,
-0.105,
this.z,
0.22,
1.27
);
// Upper laminate is cut back, leaving the front half open.
this.updateSegment(
this.upperSegments[i],
i,
response,
0.115,
this.z - 0.325,
0.20,
0.56
);
// Bright cut face along the exposed edge.
this.updateSegment(
this.edgeSegments[i],
i,
response,
0.115,
this.z - 0.035,
0.205,
0.035
);
// Thin upper side rail preserves the 3D cutaway volume.
this.updateSegment(
this.railSegments[i],
i,
response,
0.115,
this.z + 0.575,
0.20,
0.10
);
}
const up = new THREE.Vector3(0, 1, 0);
for (const strand of this.fiberSegments) {
const mesh = strand.mesh;
if (strand.mode === "longitudinal") {
const u0 = strand.index / strand.count;
const u1 = (strand.index + 1) / strand.count;
const x0 = this.startX + u0 * this.length;
const x1 = this.startX + u1 * this.length;
const y0 = this.bendAt(x0, response) + strand.y;
const y1 = this.bendAt(x1, response) + strand.y;
const start = new THREE.Vector3(x0, y0, strand.z);
const end = new THREE.Vector3(x1, y1, strand.z);
const direction = end.clone().sub(start);
const length = direction.length();
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
mesh.scale.set(0.017, length, 0.017);
} else {
const x = strand.x;
const y = this.bendAt(x, response) + strand.y;
const start = new THREE.Vector3(x, y, this.z - 0.61);
const end = new THREE.Vector3(x, y, this.z + 0.61);
const direction = end.clone().sub(start);
const length = direction.length();
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
mesh.scale.set(0.017, length, 0.017);
}
}
}
}
createClamp(1.42);
createClamp(-1.42);
const couponA = new CompositeCoupon({
z: 1.42,
orientation: 0,
fiberMaterial: tealFiberMaterial,
bendMagnitude: 0.10
});
const couponB = new CompositeCoupon({
z: -1.42,
orientation: 90,
fiberMaterial: violetFiberMaterial,
bendMagnitude: 0.31
});
const rigA = createLoadRig(1.42);
const rigB = createLoadRig(-1.42);
function makeLabel(className, html) {
const element = document.createElement("div");
element.className = `scene-label ${className}`;
element.innerHTML = html;
labelsRoot.appendChild(element);
return element;
}
const labelA = makeLabel(
"a",
"<b>Coupon A · 0°</b>Fibers along the loading span"
);
const labelB = makeLabel(
"b",
"<b>Coupon B · 90°</b>Fibers across the loading span"
);
const labelLoadA = makeLabel(
"load",
"<b>TOY LOAD</b>Same illustrative setting"
);
const labelLoadB = makeLabel(
"load",
"<b>TOY LOAD</b>Same illustrative setting"
);
const projected = new THREE.Vector3();
function placeLabel(element, worldPosition, offsetX = 0, offsetY = 0) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.3 &&
Math.abs(projected.y) < 1.3;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x + offsetX}px`;
element.style.top = `${y + offsetY}px`;
}
let running = true;
let cycleTime = 3.2;
const cycleDuration = 7.2;
let lastTime = performance.now();
function loadEnvelope(phase) {
if (phase < 0.18) return 0;
if (phase < 0.42) {
return THREE.MathUtils.smoothstep(phase, 0.18, 0.42);
}
if (phase < 0.68) return 1;
if (phase < 0.9) {
return 1 - THREE.MathUtils.smoothstep(phase, 0.68, 0.9);
}
return 0;
}
function updateStatus(response, phase) {
let text;
if (response < 0.02) {
text = phase < 0.5 ? "Toy load at rest" : "Toy load released";
} else if (response < 0.98) {
text = phase < 0.5 ? "Toy load applying" : "Toy load releasing";
} else {
text = "Toy load applied";
}
if (responseStatus.textContent !== text) {
responseStatus.textContent = text;
}
}
function animate(now) {
const dt = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
if (running) {
cycleTime += dt;
}
const phase = (cycleTime % cycleDuration) / cycleDuration;
const response = loadEnvelope(phase);
couponA.update(response);
couponB.update(response);
rigA.weight.position.y = 1.24 - response * 0.075;
rigB.weight.position.y = 1.24 - response * 0.075;
rigA.arrow.setLength(0.56 - response * 0.035, 0.17, 0.12);
rigB.arrow.setLength(0.56 - response * 0.035, 0.17, 0.12);
const labelAAnchor = new THREE.Vector3(
-1.25,
couponA.bendAt(-1.25, response) + 0.46,
couponA.z + 0.12
);
const labelBAnchor = new THREE.Vector3(
-1.25,
couponB.bendAt(-1.25, response) + 0.46,
couponB.z + 0.12
);
placeLabel(labelA, labelAAnchor, -10, -8);
placeLabel(labelB, labelBAnchor, -10, -8);
placeLabel(
labelLoadA,
new THREE.Vector3(2.52, rigA.weight.position.y + 0.31, rigA.weight.position.z),
0,
-9
);
placeLabel(
labelLoadB,
new THREE.Vector3(2.52, rigB.weight.position.y + 0.31, rigB.weight.position.z),
0,
-9
);
updateStatus(response, phase);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
// Camera controls implemented directly, without addons.
let dragging = false;
let panning = false;
let previousX = 0;
let previousY = 0;
const forward = new THREE.Vector3();
const right = new THREE.Vector3();
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
panning = event.button === 2 || event.shiftKey;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (panning) {
camera.getWorldDirection(forward);
right.crossVectors(forward, camera.up).normalize();
const scale = spherical.radius * 0.0013;
cameraTarget.addScaledVector(right, -dx * scale);
cameraTarget.y += dy * scale;
} else {
spherical.theta -= dx * 0.006;
spherical.phi -= dy * 0.006;
spherical.phi = THREE.MathUtils.clamp(spherical.phi, 0.22, Math.PI - 0.25);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
panning = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius * Math.exp(event.deltaY * 0.001),
6.2,
18
);
updateCamera();
},
{ passive: false }
);
function resetView() {
cameraTarget.set(0, 0.18, 0);
spherical.set(10.8, 1.04, 0.68);
updateCamera();
}
cycleButton.addEventListener("click", () => {
running = !running;
cycleButton.textContent = running ? "Pause toy cycle" : "Resume toy cycle";
});
resetButton.addEventListener("click", resetView);
zoomInButton.addEventListener("click", () => {
spherical.radius = Math.max(6.2, spherical.radius * 0.88);
updateCamera();
});
zoomOutButton.addEventListener("click", () => {
spherical.radius = Math.min(18, spherical.radius * 1.12);
updateCamera();
});
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "r") {
resetView();
} else if (key === " ") {
event.preventDefault();
running = !running;
cycleButton.textContent = running ? "Pause toy cycle" : "Resume toy cycle";
} else if (key === "arrowleft") {
spherical.theta += 0.08;
updateCamera();
} else if (key === "arrowright") {
spherical.theta -= 0.08;
updateCamera();
} else if (key === "arrowup") {
spherical.phi = Math.max(0.22, spherical.phi - 0.06);
updateCamera();
} else if (key === "arrowdown") {
spherical.phi = Math.min(Math.PI - 0.25, spherical.phi + 0.06);
updateCamera();
} else if (key === "+" || key === "=") {
spherical.radius = Math.max(6.2, spherical.radius * 0.9);
updateCamera();
} else if (key === "-") {
spherical.radius = Math.min(18, spherical.radius * 1.1);
updateCamera();
}
});
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
requestAnimationFrame(animate);
</script>
</body>
</html>
```