Prompt specification
Build or customize the Three.js 3D scene "Ramp force decomposition exhibit" 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: Ramp force decomposition exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/ramp-force-decomposition-exhibit-x1755
Scene Description Prompt:
I'd like to see this as a voxel Three.js scene: ramp force decomposition exhibit. Compose a block on a slope with gravity decomposed into parallel and normal components; use vector projections and keep the basis aligned with the ramp. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (ramp-force-decomposition-exhibit-x1755):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Force Lab — Ramp Decomposition</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(8, 19, 34, 0.88);
--line: rgba(151, 203, 231, 0.18);
--text: #eaf7ff;
--muted: #8da8bc;
--cyan: #39d9ff;
--pink: #ff5ebc;
--amber: #ffc857;
--green: #72e6a6;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
.brand {
position: fixed;
z-index: 30;
top: 20px;
left: 20px;
display: flex;
align-items: center;
gap: 12px;
pointer-events: none;
}
.brand-mark {
width: 42px;
height: 42px;
display: grid;
place-items: center;
border: 1px solid rgba(57, 217, 255, 0.55);
background: rgba(57, 217, 255, 0.1);
box-shadow: 0 0 28px rgba(57, 217, 255, 0.14);
color: var(--cyan);
font-size: 22px;
font-weight: 900;
}
.brand strong {
display: block;
font-size: 14px;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.brand small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.panel {
position: fixed;
z-index: 25;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.controls {
top: 82px;
left: 20px;
width: 318px;
max-height: calc(100vh - 108px);
overflow: auto;
padding: 18px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 24px;
line-height: 1.08;
letter-spacing: -0.03em;
}
.intro {
margin: 10px 0 18px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.control { margin-top: 16px; }
.control-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
}
.control label {
color: #c5d7e4;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.07em;
text-transform: uppercase;
}
output {
color: var(--text);
font-variant-numeric: tabular-nums;
font-size: 13px;
font-weight: 800;
}
input[type="range"] {
--pct: 0%;
width: 100%;
height: 4px;
margin: 7px 0;
appearance: none;
border-radius: 0;
background:
linear-gradient(90deg, var(--cyan) var(--pct), rgba(130, 174, 198, 0.18) var(--pct));
outline: none;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 3px solid #07111f;
outline: 1px solid var(--cyan);
background: var(--cyan);
cursor: grab;
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #07111f;
outline: 1px solid var(--cyan);
border-radius: 0;
background: var(--cyan);
cursor: grab;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 18px;
}
.metric {
min-width: 0;
padding: 10px 8px;
border: 1px solid rgba(139, 190, 218, 0.13);
background: rgba(255, 255, 255, 0.025);
}
.metric span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 6px;
overflow: hidden;
font-size: 15px;
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
}
.metric.weight strong { color: var(--amber); }
.metric.parallel strong { color: var(--cyan); }
.metric.normal strong { color: var(--pink); }
.toggles {
display: grid;
gap: 9px;
margin: 17px 0;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.toggle {
display: flex;
align-items: center;
justify-content: space-between;
color: #c5d7e4;
font-size: 11px;
cursor: pointer;
}
.toggle input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch {
position: relative;
width: 32px;
height: 17px;
border: 1px solid rgba(130, 174, 198, 0.35);
background: rgba(130, 174, 198, 0.1);
transition: 160ms ease;
}
.switch::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 9px;
height: 9px;
background: #7892a4;
transition: 160ms ease;
}
.toggle input:checked + .switch {
border-color: rgba(57, 217, 255, 0.7);
background: rgba(57, 217, 255, 0.14);
}
.toggle input:checked + .switch::after {
left: 18px;
background: var(--cyan);
box-shadow: 0 0 9px rgba(57, 217, 255, 0.7);
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 36px;
border: 1px solid rgba(57, 217, 255, 0.28);
background: rgba(57, 217, 255, 0.07);
color: #dff8ff;
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition: 150ms ease;
}
button:hover {
border-color: rgba(57, 217, 255, 0.7);
background: rgba(57, 217, 255, 0.14);
}
.math-panel {
top: 20px;
right: 20px;
width: 294px;
padding: 17px;
}
.math-panel h2 {
margin: 0 0 13px;
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.equation {
margin: 10px 0;
padding: 11px 12px;
border-left: 2px solid var(--cyan);
background: rgba(57, 217, 255, 0.055);
color: #e7faff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
line-height: 1.6;
}
.basis-row {
display: grid;
grid-template-columns: 22px 1fr;
gap: 9px;
margin-top: 11px;
color: #c7d7e2;
font-size: 11px;
line-height: 1.5;
}
.basis-chip {
display: grid;
place-items: center;
width: 22px;
height: 22px;
color: var(--green);
border: 1px solid rgba(114, 230, 166, 0.35);
background: rgba(114, 230, 166, 0.08);
font-weight: 900;
}
.basis-chip.normal {
color: var(--pink);
border-color: rgba(255, 94, 188, 0.35);
background: rgba(255, 94, 188, 0.08);
}
.scale-note {
margin-top: 14px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.legend {
position: fixed;
z-index: 24;
right: 20px;
bottom: 20px;
display: flex;
flex-wrap: wrap;
gap: 8px 15px;
max-width: 360px;
padding: 11px 14px;
border: 1px solid var(--line);
background: rgba(8, 19, 34, 0.8);
backdrop-filter: blur(12px);
color: #bdd0dc;
font-size: 10px;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
white-space: nowrap;
}
.swatch {
width: 17px;
height: 3px;
background: currentColor;
box-shadow: 0 0 8px currentColor;
}
.swatch.cyan { color: var(--cyan); }
.swatch.pink { color: var(--pink); }
.swatch.amber { color: var(--amber); }
.help {
position: fixed;
z-index: 22;
left: 20px;
bottom: 20px;
padding: 9px 12px;
border: 1px solid var(--line);
background: rgba(8, 19, 34, 0.72);
color: var(--muted);
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
}
#labels {
position: fixed;
z-index: 12;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.vector-label,
.axis-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
padding: 5px 8px;
border: 1px solid currentColor;
background: rgba(5, 13, 24, 0.9);
box-shadow: 0 0 15px rgba(0, 0, 0, 0.28);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.04em;
white-space: nowrap;
}
.vector-label.weight { color: var(--amber); }
.vector-label.parallel { color: var(--cyan); }
.vector-label.normal { color: var(--pink); }
.axis-label {
padding: 3px 6px;
border-color: rgba(190, 213, 226, 0.25);
color: #bdd0dc;
background: rgba(5, 13, 24, 0.72);
font-size: 9px;
font-weight: 700;
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #07111f;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 920px) {
.math-panel { display: none; }
.controls { width: 292px; }
}
@media (max-width: 620px) {
.brand { top: 12px; left: 12px; }
.brand-mark { width: 34px; height: 34px; }
.controls {
top: 58px;
left: 10px;
width: min(270px, calc(100vw - 20px));
max-height: calc(100vh - 112px);
padding: 14px;
}
h1 { font-size: 20px; }
.intro { display: none; }
.metrics { gap: 4px; }
.metric { padding: 8px 5px; }
.metric strong { font-size: 13px; }
.help { display: none; }
.legend {
right: 10px;
bottom: 10px;
left: 10px;
max-width: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel ramp force decomposition scene"></canvas>
<div id="labels"></div>
<header class="brand">
<div class="brand-mark">F</div>
<div>
<strong>Force Lab</strong>
<small>Voxel mechanics / study 01</small>
</div>
</header>
<section class="panel controls">
<div class="eyebrow">Control deck</div>
<h1>Ramp force<br>decomposition</h1>
<p class="intro">
Rotate the incline and change the block mass. The yellow gravity vector is
projected onto a ramp-aligned basis to produce down-ramp and into-ramp components.
</p>
<div class="control">
<div class="control-head">
<label for="angle">Ramp angle</label>
<output id="angleOutput" for="angle">26°</output>
</div>
<input id="angle" type="range" min="5" max="55" step="1" value="26">
</div>
<div class="control">
<div class="control-head">
<label for="mass">Block mass</label>
<output id="massOutput" for="mass">40 kg</output>
</div>
<input id="mass" type="range" min="10" max="100" step="1" value="40">
</div>
<div class="metrics">
<div class="metric weight">
<span>Weight</span>
<strong id="weightValue">392 N</strong>
</div>
<div class="metric parallel">
<span>Parallel</span>
<strong id="parallelValue">172 N</strong>
</div>
<div class="metric normal">
<span>Normal</span>
<strong id="normalValue">353 N</strong>
</div>
</div>
<div class="toggles">
<label class="toggle">
Projection parallelogram
<input id="projectionToggle" type="checkbox" checked>
<span class="switch"></span>
</label>
<label class="toggle">
Ramp-aligned basis
<input id="basisToggle" type="checkbox" checked>
<span class="switch"></span>
</label>
<label class="toggle">
Vector labels
<input id="labelsToggle" type="checkbox" checked>
<span class="switch"></span>
</label>
</div>
<div class="buttons">
<button id="resetButton" type="button">Reset exhibit</button>
<button id="topButton" type="button">Top view</button>
</div>
</section>
<aside class="panel math-panel">
<div class="eyebrow">Vector model</div>
<h2>Projection onto the ramp basis</h2>
<div class="equation">
g = (g · û) û + (g · n̂) n̂<br>
= (−mg sin θ) û + (−mg cos θ) n̂
</div>
<div class="basis-row">
<span class="basis-chip">û</span>
<span><strong>Up-ramp basis</strong><br>(cos θ, sin θ, 0)</span>
</div>
<div class="basis-row">
<span class="basis-chip normal">n̂</span>
<span><strong>Outward normal basis</strong><br>(−sin θ, cos θ, 0)</span>
</div>
<div class="scale-note">
All force arrows share one scale. The normal component points into the ramp;
the ramp's separate reaction force is not drawn.
</div>
</aside>
<div class="legend">
<span class="legend-item"><i class="swatch amber"></i>Weight g</span>
<span class="legend-item"><i class="swatch cyan"></i>Parallel / down-ramp</span>
<span class="legend-item"><i class="swatch pink"></i>Normal / into-ramp</span>
</div>
<div class="help">Drag to orbit · wheel to zoom · double-click to reset</div>
<div id="loading">Initializing voxel lab</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 labelsLayer = document.querySelector("#labels");
const angleInput = document.querySelector("#angle");
const massInput = document.querySelector("#mass");
const angleOutput = document.querySelector("#angleOutput");
const massOutput = document.querySelector("#massOutput");
const weightValue = document.querySelector("#weightValue");
const parallelValue = document.querySelector("#parallelValue");
const normalValue = document.querySelector("#normalValue");
const projectionToggle = document.querySelector("#projectionToggle");
const basisToggle = document.querySelector("#basisToggle");
const labelsToggle = document.querySelector("#labelsToggle");
const resetButton = document.querySelector("#resetButton");
const topButton = document.querySelector("#topButton");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x07111f, 1);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 18, 42);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 2.05, 0);
const defaultCamera = { radius: 18, phi: 1.12, theta: 0.34 };
const cameraState = { ...defaultCamera };
function updateCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xb9eaff, 0x101b2a, 1.55));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-6, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 14;
keyLight.shadow.camera.bottom = -14;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x28caff, 16, 13, 2);
cyanLight.position.set(-5, 3, 4);
scene.add(cyanLight);
const pinkLight = new THREE.PointLight(0xff4fae, 12, 11, 2);
pinkLight.position.set(6, 4, -2);
scene.add(pinkLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x091726,
roughness: 0.94,
metalness: 0.08
});
const ground = new THREE.Mesh(
new THREE.BoxGeometry(42, 0.3, 30),
groundMaterial
);
ground.position.y = -0.16;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(40, 40, 0x24536b, 0x153347);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.42;
grid.material.depthWrite = false;
scene.add(grid);
const boundaryMaterial = new THREE.MeshStandardMaterial({
color: 0x174158,
roughness: 0.75,
metalness: 0.18
});
for (const z of [-10.8, 10.8]) {
const strip = new THREE.Mesh(
new THREE.BoxGeometry(28, 0.035, 0.08),
boundaryMaterial
);
strip.position.set(0, 0.015, z);
scene.add(strip);
}
const RAMP_LENGTH = 11.5;
const RAMP_WIDTH = 4.2;
const RAMP_THICKNESS = 0.44;
const TOP_TILE_HEIGHT = 0.12;
const BLOCK_X = RAMP_LENGTH * 0.59;
const VOXEL = 0.46;
const BLOCK_X_COUNT = 4;
const BLOCK_Y_COUNT = 4;
const BLOCK_Z_COUNT = 3;
const BLOCK_HEIGHT = BLOCK_Y_COUNT * VOXEL;
const BLOCK_DEPTH = BLOCK_Z_COUNT * VOXEL;
const BLOCK_Y = TOP_TILE_HEIGHT + BLOCK_HEIGHT / 2;
const FORCE_SCALE = 0.0055;
const GRAVITY = 9.81;
const ramp = new THREE.Group();
scene.add(ramp);
const rampMaterials = {
slab: new THREE.MeshStandardMaterial({
color: 0x22384d,
roughness: 0.78,
metalness: 0.2,
flatShading: true
}),
tileA: new THREE.MeshStandardMaterial({
color: 0x36566d,
roughness: 0.74,
metalness: 0.16,
flatShading: true
}),
tileB: new THREE.MeshStandardMaterial({
color: 0x304d62,
roughness: 0.78,
metalness: 0.15,
flatShading: true
}),
edge: new THREE.MeshStandardMaterial({
color: 0xd69b35,
roughness: 0.58,
metalness: 0.28,
flatShading: true
})
};
const slab = new THREE.Mesh(
new THREE.BoxGeometry(RAMP_LENGTH, RAMP_THICKNESS, RAMP_WIDTH),
rampMaterials.slab
);
slab.position.set(RAMP_LENGTH / 2, -RAMP_THICKNESS / 2, 0);
slab.castShadow = true;
slab.receiveShadow = true;
ramp.add(slab);
const tileCount = 16;
const tileLength = RAMP_LENGTH / tileCount;
const tileWidth = RAMP_WIDTH / 2;
for (let i = 0; i < tileCount; i++) {
for (let lane = 0; lane < 2; lane++) {
const tile = new THREE.Mesh(
new THREE.BoxGeometry(
tileLength * 0.965,
TOP_TILE_HEIGHT,
tileWidth * 0.965
),
i % 2 === lane % 2 ? rampMaterials.tileA : rampMaterials.tileB
);
tile.position.set(
(i + 0.5) * tileLength,
TOP_TILE_HEIGHT / 2,
(lane - 0.5) * tileWidth
);
tile.castShadow = true;
tile.receiveShadow = true;
ramp.add(tile);
}
const edge = new THREE.Mesh(
new THREE.BoxGeometry(tileLength * 0.91, 0.17, 0.16),
rampMaterials.edge
);
edge.position.set(
(i + 0.5) * tileLength,
0.12,
RAMP_WIDTH / 2 - 0.06
);
edge.castShadow = true;
ramp.add(edge);
}
const block = new THREE.Group();
block.position.set(BLOCK_X, BLOCK_Y, 0);
ramp.add(block);
const blockMaterials = [
new THREE.MeshStandardMaterial({
color: 0x1eb7bd,
roughness: 0.62,
metalness: 0.08,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x28c9c9,
roughness: 0.6,
metalness: 0.08,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x169ca9,
roughness: 0.66,
metalness: 0.08,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x5be0d6,
roughness: 0.58,
metalness: 0.07,
flatShading: true
})
];
const voxelGeometry = new THREE.BoxGeometry(VOXEL * 0.94, VOXEL * 0.94, VOXEL * 0.94);
for (let ix = 0; ix < BLOCK_X_COUNT; ix++) {
for (let iy = 0; iy < BLOCK_Y_COUNT; iy++) {
for (let iz = 0; iz < BLOCK_Z_COUNT; iz++) {
let materialIndex = (ix + iy + iz) % 3;
if (iy === BLOCK_Y_COUNT - 1) materialIndex = 3;
const voxel = new THREE.Mesh(voxelGeometry, blockMaterials[materialIndex]);
voxel.position.set(
(ix - (BLOCK_X_COUNT - 1) / 2) * VOXEL,
(iy - (BLOCK_Y_COUNT - 1) / 2) * VOXEL,
(iz - (BLOCK_Z_COUNT - 1) / 2) * VOXEL
);
voxel.castShadow = true;
voxel.receiveShadow = true;
block.add(voxel);
}
}
}
const supportGroup = new THREE.Group();
scene.add(supportGroup);
const supportGeometry = new THREE.BoxGeometry(1, 1, 1);
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x193247,
roughness: 0.8,
metalness: 0.2,
flatShading: true
});
function rebuildSupports() {
while (supportGroup.children.length) {
supportGroup.remove(supportGroup.children[0]);
}
for (const localX of [RAMP_LENGTH * 0.28, RAMP_LENGTH * 0.69]) {
const underside = ramp.localToWorld(
new THREE.Vector3(localX, -RAMP_THICKNESS, 0)
);
const count = Math.max(1, Math.floor((underside.y - 0.08) / 0.42));
for (const z of [-1.35, 1.35]) {
for (let i = 0; i < count; i++) {
const support = new THREE.Mesh(supportGeometry, supportMaterial);
support.scale.set(0.39, 0.39, 0.39);
support.position.set(
underside.x,
0.205 + i * 0.41,
z
);
support.castShadow = true;
support.receiveShadow = true;
supportGroup.add(support);
}
}
}
}
function makeArrow(color, headScale = 1) {
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(),
1,
color,
0.35 * headScale,
0.23 * headScale
);
for (const part of [arrow.line, arrow.cone]) {
part.material.depthTest = false;
part.material.transparent = true;
part.material.opacity = 0.98;
part.renderOrder = 12;
}
arrow.renderOrder = 12;
return arrow;
}
const weightArrow = makeArrow(0xffc857);
const parallelArrow = makeArrow(0x39d9ff);
const normalArrow = makeArrow(0xff5ebc);
const basisUArrow = makeArrow(0x72e6a6, 0.72);
const basisNArrow = makeArrow(0xd38ac7, 0.72);
scene.add(
weightArrow,
parallelArrow,
normalArrow,
basisUArrow,
basisNArrow
);
const projectionGeometry = new THREE.BufferGeometry();
const projectionMaterial = new THREE.LineDashedMaterial({
color: 0xd9f2ff,
dashSize: 0.18,
gapSize: 0.12,
transparent: true,
opacity: 0.48,
depthTest: false
});
const projectionLine = new THREE.Line(projectionGeometry, projectionMaterial);
projectionLine.renderOrder = 11;
scene.add(projectionLine);
const pointGeometry = new THREE.BoxGeometry(0.11, 0.11, 0.11);
const pointMaterials = {
origin: new THREE.MeshBasicMaterial({ color: 0xffffff, depthTest: false }),
parallel: new THREE.MeshBasicMaterial({ color: 0x39d9ff, depthTest: false }),
weight: new THREE.MeshBasicMaterial({ color: 0xffc857, depthTest: false }),
normal: new THREE.MeshBasicMaterial({ color: 0xff5ebc, depthTest: false })
};
const pointMeshes = {
origin: new THREE.Mesh(pointGeometry, pointMaterials.origin),
parallel: new THREE.Mesh(pointGeometry, pointMaterials.parallel),
weight: new THREE.Mesh(pointGeometry, pointMaterials.weight),
normal: new THREE.Mesh(pointGeometry, pointMaterials.normal)
};
for (const point of Object.values(pointMeshes)) {
point.renderOrder = 13;
scene.add(point);
}
const angleArcGeometry = new THREE.BufferGeometry();
const angleLineMaterial = new THREE.LineBasicMaterial({
color: 0xffc857,
transparent: true,
opacity: 0.85
});
const angleArc = new THREE.Line(angleArcGeometry, angleLineMaterial);
scene.add(angleArc);
const angleBaselineGeometry = new THREE.BufferGeometry();
const angleBaselineMaterial = new THREE.LineDashedMaterial({
color: 0xffc857,
dashSize: 0.12,
gapSize: 0.1,
transparent: true,
opacity: 0.55
});
const angleBaseline = new THREE.Line(
angleBaselineGeometry,
angleBaselineMaterial
);
scene.add(angleBaseline);
function createLabel(className, text) {
const element = document.createElement("div");
element.className = className;
element.textContent = text;
labelsLayer.appendChild(element);
return element;
}
const labels = {
weight: createLabel("vector-label weight", "W"),
parallel: createLabel("vector-label parallel", "F∥"),
normal: createLabel("vector-label normal", "F⊥"),
basisU: createLabel("axis-label", "û · up-ramp"),
basisN: createLabel("axis-label", "n̂ · outward normal"),
angle: createLabel("axis-label", "θ")
};
const labelPositions = {
weight: new THREE.Vector3(),
parallel: new THREE.Vector3(),
normal: new THREE.Vector3(),
basisU: new THREE.Vector3(),
basisN: new THREE.Vector3(),
angle: new THREE.Vector3()
};
function placeLabel(element, worldPosition, offsetX = 0, offsetY = 0) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
element.style.display = visible && labelsToggle.checked ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth + offsetX;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight + offsetY;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
let currentValues = {
angle: 26,
mass: 40,
weight: 0,
parallel: 0,
normal: 0
};
function setArrow(arrow, origin, direction, length) {
arrow.position.copy(origin);
arrow.setDirection(direction.clone().normalize());
const headLength = Math.min(0.43, Math.max(0.12, length * 0.23));
const headWidth = Math.min(0.28, Math.max(0.09, length * 0.15));
arrow.setLength(length, headLength, headWidth);
}
function updateScene() {
const angle = Number(angleInput.value);
const mass = Number(massInput.value);
const radians = THREE.MathUtils.degToRad(angle);
currentValues.angle = angle;
currentValues.mass = mass;
const weight = mass * GRAVITY;
const parallel = weight * Math.sin(radians);
const normal = weight * Math.cos(radians);
currentValues.weight = weight;
currentValues.parallel = parallel;
currentValues.normal = normal;
ramp.rotation.z = radians;
ramp.position.set(
-(RAMP_LENGTH / 2) * Math.cos(radians),
0,
0
);
ramp.updateMatrixWorld(true);
rebuildSupports();
const u = new THREE.Vector3(Math.cos(radians), Math.sin(radians), 0);
const n = new THREE.Vector3(-Math.sin(radians), Math.cos(radians), 0);
const origin = ramp.localToWorld(
new THREE.Vector3(
BLOCK_X,
BLOCK_Y,
BLOCK_DEPTH / 2 + 0.34
)
);
const weightDirection = new THREE.Vector3(0, -1, 0);
const parallelDirection = u.clone().negate();
const normalDirection = n.clone().negate();
const weightEnd = origin.clone().addScaledVector(
weightDirection,
weight * FORCE_SCALE
);
const parallelEnd = origin.clone().addScaledVector(
parallelDirection,
parallel * FORCE_SCALE
);
const normalEnd = origin.clone().addScaledVector(
normalDirection,
normal * FORCE_SCALE
);
setArrow(weightArrow, origin, weightDirection, weight * FORCE_SCALE);
setArrow(parallelArrow, origin, parallelDirection, parallel * FORCE_SCALE);
setArrow(normalArrow, origin, normalDirection, normal * FORCE_SCALE);
pointMeshes.origin.position.copy(origin);
pointMeshes.weight.position.copy(weightEnd);
pointMeshes.parallel.position.copy(parallelEnd);
pointMeshes.normal.position.copy(normalEnd);
projectionGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
parallelEnd.x, parallelEnd.y, parallelEnd.z,
weightEnd.x, weightEnd.y, weightEnd.z,
normalEnd.x, normalEnd.y, normalEnd.z
],
3
)
);
projectionGeometry.attributes.position.needsUpdate = true;
projectionLine.computeLineDistances();
const basisOrigin = ramp.localToWorld(
new THREE.Vector3(RAMP_LENGTH * 0.78, 0.12, 3.0)
);
const basisUEnd = basisOrigin.clone().addScaledVector(u, 1.65);
const basisNEnd = basisOrigin.clone().addScaledVector(n, 1.38);
setArrow(basisUArrow, basisOrigin, u, 1.65);
setArrow(basisNArrow, basisOrigin, n, 1.38);
const lowerPoint = ramp.localToWorld(
new THREE.Vector3(0, 0, RAMP_WIDTH / 2 + 0.55)
);
const arcRadius = 1.7;
const arcPoints = [];
for (let i = 0; i <= 30; i++) {
const a = radians * (i / 30);
arcPoints.push(
lowerPoint.x + Math.cos(a) * arcRadius,
lowerPoint.y + Math.sin(a) * arcRadius,
lowerPoint.z
);
}
angleArcGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(arcPoints, 3)
);
angleArcGeometry.attributes.position.needsUpdate = true;
angleBaselineGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
lowerPoint.x,
lowerPoint.y,
lowerPoint.z,
lowerPoint.x + 2.05,
lowerPoint.y,
lowerPoint.z
],
3
)
);
angleBaselineGeometry.attributes.position.needsUpdate = true;
angleBaseline.computeLineDistances();
labelPositions.weight.copy(weightEnd).add(new THREE.Vector3(0, 0.18, 0));
labelPositions.parallel
.copy(parallelEnd)
.add(new THREE.Vector3(-0.12, 0.17, 0));
labelPositions.normal
.copy(normalEnd)
.add(new THREE.Vector3(0.16, 0.18, 0));
labelPositions.basisU
.copy(basisUEnd)
.add(new THREE.Vector3(0.04, 0.15, 0));
labelPositions.basisN
.copy(basisNEnd)
.add(new THREE.Vector3(-0.12, 0.15, 0));
labelPositions.angle.set(
lowerPoint.x + Math.cos(radians / 2) * arcRadius * 0.63,
lowerPoint.y + Math.sin(radians / 2) * arcRadius * 0.63,
lowerPoint.z
);
labels.weight.textContent = `W = ${Math.round(weight)} N`;
labels.parallel.textContent = `F∥ = ${Math.round(parallel)} N`;
labels.normal.textContent = `F⊥ = ${Math.round(normal)} N`;
labels.angle.textContent = `θ = ${angle}°`;
projectionLine.visible = projectionToggle.checked;
for (const point of Object.values(pointMeshes)) {
point.visible = projectionToggle.checked;
}
basisUArrow.visible = basisToggle.checked;
basisNArrow.visible = basisToggle.checked;
updateUI();
}
function updateUI() {
const angle = Number(angleInput.value);
const mass = Number(massInput.value);
const weight = mass * GRAVITY;
const parallel = weight * Math.sin(THREE.MathUtils.degToRad(angle));
const normal = weight * Math.cos(THREE.MathUtils.degToRad(angle));
angleOutput.textContent = `${angle}°`;
massOutput.textContent = `${mass} kg`;
weightValue.textContent = `${Math.round(weight)} N`;
parallelValue.textContent = `${Math.round(parallel)} N`;
normalValue.textContent = `${Math.round(normal)} N`;
const anglePercent =
((angle - Number(angleInput.min)) /
(Number(angleInput.max) - Number(angleInput.min))) *
100;
const massPercent =
((mass - Number(massInput.min)) /
(Number(massInput.max) - Number(massInput.min))) *
100;
angleInput.style.setProperty("--pct", `${anglePercent}%`);
massInput.style.setProperty("--pct", `${massPercent}%`);
}
angleInput.addEventListener("input", updateScene);
massInput.addEventListener("input", updateScene);
projectionToggle.addEventListener("change", updateScene);
basisToggle.addEventListener("change", updateScene);
labelsToggle.addEventListener("change", () => {
if (!labelsToggle.checked) {
for (const label of Object.values(labels)) label.style.display = "none";
}
});
resetButton.addEventListener("click", () => {
angleInput.value = 26;
massInput.value = 40;
cameraState.radius = defaultCamera.radius;
cameraState.phi = defaultCamera.phi;
cameraState.theta = defaultCamera.theta;
updateCamera();
updateScene();
});
topButton.addEventListener("click", () => {
cameraState.radius = 18;
cameraState.phi = 0.22;
cameraState.theta = 0;
updateCamera();
});
let dragging = false;
let lastX = 0;
let lastY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.pointerType === "mouse" && event.button !== 0) return;
dragging = true;
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;
cameraState.theta -= dx * 0.005;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.18,
1.48
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
10,
29
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
cameraState.radius = defaultCamera.radius;
cameraState.phi = defaultCamera.phi;
cameraState.theta = defaultCamera.theta;
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateScene();
function animate() {
requestAnimationFrame(animate);
placeLabel(labels.weight, labelPositions.weight, 0, -9);
placeLabel(labels.parallel, labelPositions.parallel, -18, 0);
placeLabel(labels.normal, labelPositions.normal, 20, 0);
placeLabel(labels.basisU, labelPositions.basisU, 10, 0);
placeLabel(labels.basisN, labelPositions.basisN, -12, 0);
placeLabel(labels.angle, labelPositions.angle, 0, -10);
renderer.render(scene, camera);
}
animate();
document.querySelector("#loading").classList.add("hidden");
</script>
</body>
</html>
```