Prompt specification
Build or customize the Three.js 3D scene "Near-back versus outer loading" 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: Near-back versus outer loading
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/near-back-versus-outer-loading-x0562
Scene Description Prompt:
Move equal heavy loads nearer to or farther from the back in low-poly backpacks; track illustrative center-of-mass shifts. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (near-back-versus-outer-loading-x0562):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Backpack Load Center-of-Mass Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07131f;
--panel: rgba(10, 27, 41, 0.86);
--border: rgba(157, 224, 235, 0.17);
--text: #e8f6f7;
--muted: #94adb5;
--cyan: #65e4e6;
--gold: #ffc857;
--coral: #ff6b6b;
--green: #8ee6a8;
}
* {
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;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.brand {
position: fixed;
top: 24px;
left: 26px;
z-index: 5;
max-width: 430px;
pointer-events: none;
text-shadow: 0 2px 18px #000;
}
.eyebrow {
margin: 0 0 7px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 1.03;
letter-spacing: -0.035em;
}
.brand p:last-child {
margin: 10px 0 0;
color: #abc2c9;
font-size: 13px;
line-height: 1.5;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.36),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.controls {
left: 24px;
bottom: 24px;
width: min(390px, calc(100vw - 48px));
padding: 18px;
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.control-heading strong {
font-size: 14px;
}
.control-heading span {
color: var(--muted);
font-size: 11px;
}
.control {
margin: 0 0 17px;
}
.control-label {
display: flex;
justify-content: space-between;
gap: 14px;
margin-bottom: 9px;
color: #c8dcdf;
font-size: 12px;
font-weight: 650;
}
.control-label output {
color: var(--cyan);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 4px 0 0;
appearance: none;
border-radius: 999px;
outline: none;
background: linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--fill, 50%),
rgba(255, 255, 255, 0.12) var(--fill, 50%),
rgba(255, 255, 255, 0.12) 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
appearance: none;
border: 3px solid #dffcff;
border-radius: 50%;
background: #36c8cb;
box-shadow: 0 0 0 5px rgba(101, 228, 230, 0.13);
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #dffcff;
border-radius: 50%;
background: #36c8cb;
box-shadow: 0 0 0 5px rgba(101, 228, 230, 0.13);
cursor: pointer;
}
.range-ends {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: var(--muted);
font-size: 10px;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 3px;
}
button {
min-height: 39px;
border: 1px solid rgba(101, 228, 230, 0.22);
border-radius: 10px;
color: var(--text);
background: rgba(101, 228, 230, 0.09);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(101, 228, 230, 0.5);
background: rgba(101, 228, 230, 0.16);
}
button:active {
transform: translateY(0);
}
.button-row button:last-child {
grid-column: 1 / -1;
color: var(--muted);
background: rgba(255, 255, 255, 0.035);
border-color: rgba(255, 255, 255, 0.1);
}
.chart-wrap {
margin-top: 17px;
padding-top: 14px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.chart-title {
display: flex;
justify-content: space-between;
gap: 10px;
margin-bottom: 5px;
color: #c8dcdf;
font-size: 11px;
font-weight: 700;
}
.chart-title span:last-child {
color: var(--coral);
}
#chart {
display: block;
width: 100%;
height: 105px;
overflow: visible;
}
.disclaimer {
margin: 8px 0 0;
color: #78929b;
font-size: 10px;
line-height: 1.45;
}
.metrics {
top: 24px;
right: 24px;
width: min(315px, calc(100vw - 48px));
padding: 17px;
}
.status {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 14px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
content: "";
}
.metric {
display: grid;
grid-template-columns: 1fr auto;
gap: 12px;
align-items: baseline;
padding: 10px 0;
border-top: 1px solid rgba(255, 255, 255, 0.075);
}
.metric:first-of-type {
border-top: 0;
}
.metric-label {
color: var(--muted);
font-size: 11px;
}
.metric-value {
color: var(--text);
font-size: 18px;
font-weight: 780;
font-variant-numeric: tabular-nums;
}
.metric-value.accent {
color: var(--coral);
}
.metric-value.gold {
color: var(--gold);
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 10px 15px;
margin-top: 11px;
color: var(--muted);
font-size: 10px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
}
.dot.gold {
background: var(--gold);
}
.dot.coral {
background: var(--coral);
}
.dot.cyan {
background: var(--cyan);
}
.help {
position: fixed;
right: 25px;
bottom: 24px;
z-index: 4;
color: rgba(197, 218, 222, 0.7);
font-size: 10px;
text-align: right;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
color: var(--cyan);
background: var(--bg);
font-size: 13px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
@media (max-width: 760px) {
.brand {
top: 16px;
left: 17px;
right: 17px;
}
.brand p:last-child {
display: none;
}
.metrics {
top: 103px;
right: 14px;
width: 210px;
padding: 12px;
}
.metric {
padding: 7px 0;
}
.metric-value {
font-size: 14px;
}
.legend,
.help {
display: none;
}
.controls {
left: 12px;
right: 12px;
bottom: 12px;
width: auto;
max-height: 50vh;
overflow-y: auto;
padding: 14px;
}
.chart-wrap {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="loading">Loading low-poly backpack…</div>
<header class="brand">
<p class="eyebrow">Interactive mass distribution</p>
<h1>Backpack Load<br>Center-of-Mass Lab</h1>
<p>
Move two equal heavy loads toward or away from the back panel and
follow the illustrative combined center of mass.
</p>
</header>
<aside class="panel metrics" aria-label="Center-of-mass measurements">
<div class="status" id="status">Balanced depth</div>
<div class="metric">
<span class="metric-label">Combined COM depth</span>
<span class="metric-value" id="comValue">0 mm</span>
</div>
<div class="metric">
<span class="metric-label">Shift from near-back position</span>
<span class="metric-value accent" id="shiftValue">0 mm forward</span>
</div>
<div class="metric">
<span class="metric-label">Loads’ share of total mass</span>
<span class="metric-value gold" id="loadShare">80%</span>
</div>
<div class="legend">
<span><i class="dot gold"></i> Equal loads</span>
<span><i class="dot coral"></i> Combined COM</span>
<span><i class="dot cyan"></i> Back plane</span>
</div>
</aside>
<section class="panel controls" aria-label="Simulation controls">
<div class="control-heading">
<strong>Load configuration</strong>
<span>Symmetric cutaway view</span>
</div>
<div class="control">
<label class="control-label" for="distance">
<span>Distance from back panel</span>
<output id="distanceValue" for="distance">28 cm</output>
</label>
<input
id="distance"
type="range"
min="4"
max="52"
step="1"
value="28"
>
<div class="range-ends">
<span>Near the back</span>
<span>Farther forward</span>
</div>
</div>
<div class="control">
<label class="control-label" for="mass">
<span>Mass of each equal load</span>
<output id="massValue" for="mass">2.5 kg</output>
</label>
<input
id="mass"
type="range"
min="1"
max="5"
step="0.5"
value="2.5"
>
<div class="range-ends">
<span>1 kg</span>
<span>5 kg</span>
</div>
</div>
<div class="button-row">
<button id="nearButton" type="button">Move near back</button>
<button id="farButton" type="button">Move farther forward</button>
<button id="resetCamera" type="button">Reset camera</button>
</div>
<div class="chart-wrap">
<div class="chart-title">
<span>COM shift from near-back position</span>
<span>Forward ↓</span>
</div>
<svg id="chart" viewBox="0 0 260 105" role="img"
aria-label="Graph of center-of-mass shift as load distance changes">
<defs>
<linearGradient id="chartGradient" x1="0" x2="0" y1="0" y2="1">
<stop offset="0%" stop-color="#65e4e6" stop-opacity="0.08"></stop>
<stop offset="100%" stop-color="#65e4e6" stop-opacity="0.55"></stop>
</linearGradient>
</defs>
<line x1="16" y1="82" x2="244" y2="82"
stroke="rgba(255,255,255,.18)" stroke-width="1"></line>
<line x1="16" y1="16" x2="16" y2="82"
stroke="rgba(255,255,255,.18)" stroke-width="1"></line>
<path id="chartArea" fill="url(#chartGradient)"></path>
<path id="chartPath" fill="none" stroke="#65e4e6"
stroke-width="2.2" stroke-linecap="round"></path>
<circle id="chartDot" cx="130" cy="40" r="4.5"
fill="#ff6b6b" stroke="#fff" stroke-width="1.5"></circle>
<text x="16" y="99" fill="#78929b" font-size="9">near</text>
<text x="244" y="99" fill="#78929b" font-size="9"
text-anchor="end">far</text>
<text id="chartMax" x="6" y="17" fill="#78929b" font-size="8"></text>
</svg>
</div>
<p class="disclaimer">
Illustrative rigid-body model only. Actual packing, body posture,
load shape, and backpack mass distribution affect comfort and balance.
</p>
</section>
<div class="help">
Drag to orbit · Scroll or pinch-zoom to inspect · Double-click to reset
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const ui = {
stage: $("stage"),
loading: $("loading"),
distance: $("distance"),
mass: $("mass"),
distanceValue: $("distanceValue"),
massValue: $("massValue"),
status: $("status"),
comValue: $("comValue"),
shiftValue: $("shiftValue"),
loadShare: $("loadShare"),
nearButton: $("nearButton"),
farButton: $("farButton"),
resetCamera: $("resetCamera"),
chartPath: $("chartPath"),
chartArea: $("chartArea"),
chartDot: $("chartDot"),
chartMax: $("chartMax")
};
// ------------------------------------------------------------
// Scene
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131f);
scene.fog = new THREE.Fog(0x07131f, 8, 18);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.05,
50
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
ui.stage.appendChild(renderer.domElement);
const hemiLight = new THREE.HemisphereLight(0xc8f7ff, 0x182331, 2.15);
scene.add(hemiLight);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(4.5, 7, -4);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -4;
keyLight.shadow.camera.right = 4;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x60e8ff, 2.4);
rimLight.position.set(-4, 3.5, 3.5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa85c, 11, 9, 2);
warmLight.position.set(2.7, 2.4, -2.8);
scene.add(warmLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(24, 24),
new THREE.MeshStandardMaterial({
color: 0x0a1823,
roughness: 0.95,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(2.25, 2.45, 0.12, 32),
new THREE.MeshStandardMaterial({
color: 0x102736,
roughness: 0.8,
metalness: 0.12
})
);
platform.position.y = 0.02;
platform.receiveShadow = true;
scene.add(platform);
const platformRing = new THREE.Mesh(
new THREE.RingGeometry(2.19, 2.23, 64),
new THREE.MeshBasicMaterial({
color: 0x65e4e6,
transparent: true,
opacity: 0.38,
side: THREE.DoubleSide
})
);
platformRing.rotation.x = -Math.PI / 2;
platformRing.position.y = 0.085;
scene.add(platformRing);
const grid = new THREE.GridHelper(10, 20, 0x254353, 0x142b37);
grid.position.y = 0.087;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
// ------------------------------------------------------------
// Model constants. Positive z points toward the wearer's back.
// The backpack center is z = 0.
// ------------------------------------------------------------
const PACK_MASS = 1.25;
const PACK_COM_Y = 1.28;
const PACK_COM_Z = 0.0;
const BACK_PLANE_Z = 0.30;
const LOAD_Y = 1.13;
const MIN_DISTANCE = 0.04;
const MAX_DISTANCE = 0.52;
const backpack = new THREE.Group();
scene.add(backpack);
// ------------------------------------------------------------
// Helpers
// ------------------------------------------------------------
function roundedRectShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function makePanelGeometry(width, height, depth) {
const geometry = new THREE.ExtrudeGeometry(
roundedRectShape(width, height, 0.18),
{
depth,
steps: 1,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: 0.055,
bevelThickness: 0.055,
curveSegments: 2
}
);
geometry.translate(0, 0, -depth / 2);
geometry.computeVertexNormals();
return geometry;
}
function addEdges(mesh, color = 0x8ef3f3, opacity = 0.7) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 25),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(edges);
return edges;
}
function createTextSprite(initialText, width = 1, height = 0.25) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 30;
function setText(text) {
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(5, 18, 29, 0.86)";
context.strokeStyle = "rgba(155, 232, 235, 0.45)";
context.lineWidth = 3;
context.fillRect(4, 15, 504, 98);
context.strokeRect(4, 15, 504, 98);
context.fillStyle = "#ecfcfc";
context.font = "700 38px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 65);
texture.needsUpdate = true;
}
setText(initialText);
return { sprite, setText };
}
function addStrap(points, radius = 0.062, color = 0x142b3a) {
const curve = new THREE.CatmullRomCurve3(points);
const geometry = new THREE.TubeGeometry(
curve,
14,
radius,
4,
false
);
const mesh = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color,
roughness: 0.85,
flatShading: true
})
);
mesh.castShadow = true;
backpack.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Low-poly backpack shell
// ------------------------------------------------------------
const shellMaterial = new THREE.MeshStandardMaterial({
color: 0x258d92,
roughness: 0.58,
metalness: 0.05,
flatShading: true,
transparent: true,
opacity: 0.34,
depthWrite: false,
side: THREE.DoubleSide
});
const shell = new THREE.Mesh(
makePanelGeometry(1.52, 1.90, 0.60),
shellMaterial
);
shell.position.y = 1.32;
shell.renderOrder = 3;
backpack.add(shell);
addEdges(shell, 0x8ef3f3, 0.82);
const solidFabric = new THREE.MeshStandardMaterial({
color: 0x176b72,
roughness: 0.78,
metalness: 0.02,
flatShading: true
});
const topFlap = new THREE.Mesh(
new THREE.BoxGeometry(1.58, 0.46, 0.70),
solidFabric
);
topFlap.position.set(0, 2.26, -0.025);
topFlap.rotation.x = -0.09;
topFlap.castShadow = true;
backpack.add(topFlap);
addEdges(topFlap, 0x7de5e5, 0.48);
const frontPocket = new THREE.Mesh(
makePanelGeometry(1.08, 0.62, 0.20),
new THREE.MeshStandardMaterial({
color: 0x1e8589,
roughness: 0.72,
flatShading: true,
transparent: true,
opacity: 0.82
})
);
frontPocket.position.set(0, 0.76, -0.405);
frontPocket.castShadow = true;
backpack.add(frontPocket);
addEdges(frontPocket, 0x8ef3f3, 0.45);
for (const side of [-1, 1]) {
const sidePocket = new THREE.Mesh(
new THREE.BoxGeometry(0.30, 0.62, 0.53),
solidFabric
);
sidePocket.position.set(side * 0.82, 0.79, 0.01);
sidePocket.rotation.z = side * -0.035;
sidePocket.castShadow = true;
backpack.add(sidePocket);
addEdges(sidePocket, 0x7de5e5, 0.36);
}
// Shoulder straps on the back of the pack.
for (const x of [-0.37, 0.37]) {
addStrap([
new THREE.Vector3(x, 2.22, 0.32),
new THREE.Vector3(x, 2.03, 0.73),
new THREE.Vector3(x, 1.45, 0.88),
new THREE.Vector3(x, 0.86, 0.62)
]);
const buckle = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.12, 0.08),
new THREE.MeshStandardMaterial({
color: 0xffc857,
roughness: 0.48,
metalness: 0.08,
flatShading: true
})
);
buckle.position.set(x, 1.22, 0.91);
backpack.add(buckle);
}
// Low-poly top handle.
addStrap([
new THREE.Vector3(-0.18, 2.47, 0),
new THREE.Vector3(-0.12, 2.69, 0),
new THREE.Vector3(0.12, 2.69, 0),
new THREE.Vector3(0.18, 2.47, 0)
], 0.042, 0x153747);
// Back-plane reference outline.
const backPlanePoints = [
new THREE.Vector3(-0.84, 0.40, BACK_PLANE_Z),
new THREE.Vector3(0.84, 0.40, BACK_PLANE_Z),
new THREE.Vector3(0.84, 2.42, BACK_PLANE_Z),
new THREE.Vector3(-0.84, 2.42, BACK_PLANE_Z),
new THREE.Vector3(-0.84, 0.40, BACK_PLANE_Z)
];
const backPlaneLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(backPlanePoints),
new THREE.LineDashedMaterial({
color: 0x65e4e6,
dashSize: 0.07,
gapSize: 0.055,
transparent: true,
opacity: 0.5
})
);
backPlaneLine.computeLineDistances();
backpack.add(backPlaneLine);
const backLabel = createTextSprite("BACK PLANE", 0.95, 0.24);
backLabel.sprite.position.set(-0.93, 2.58, BACK_PLANE_Z);
backpack.add(backLabel.sprite);
// ------------------------------------------------------------
// Equal movable loads
// ------------------------------------------------------------
const loadGroup = new THREE.Group();
backpack.add(loadGroup);
const loadMaterial = new THREE.MeshStandardMaterial({
color: 0xffbf3f,
roughness: 0.38,
metalness: 0.22,
flatShading: true
});
const loadGeometry = new THREE.BoxGeometry(0.50, 0.48, 0.28);
const loadLabels = [];
for (const x of [-0.34, 0.34]) {
const load = new THREE.Mesh(loadGeometry, loadMaterial);
load.position.set(x, 0, 0);
load.castShadow = true;
load.receiveShadow = true;
loadGroup.add(load);
addEdges(load, 0xffed9a, 0.78);
const label = createTextSprite("2.5 kg", 0.66, 0.20);
label.sprite.position.set(x, 0.39, 0);
loadGroup.add(label.sprite);
loadLabels.push(label);
}
// ------------------------------------------------------------
// Center-of-mass marker and trace
// ------------------------------------------------------------
const comGroup = new THREE.Group();
backpack.add(comGroup);
const comSphere = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.09, 1),
new THREE.MeshStandardMaterial({
color: 0xff6262,
emissive: 0x7a1111,
emissiveIntensity: 0.7,
roughness: 0.34,
flatShading: true
})
);
comSphere.castShadow = true;
comGroup.add(comSphere);
const comRing = new THREE.Mesh(
new THREE.TorusGeometry(0.145, 0.017, 6, 28),
new THREE.MeshBasicMaterial({
color: 0xff8b76,
transparent: true,
opacity: 0.95
})
);
comGroup.add(comRing);
const comLabel = createTextSprite("COMBINED COM", 1.15, 0.25);
backpack.add(comLabel.sprite);
const projectionGeometry = new THREE.BufferGeometry();
projectionGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const projectionLine = new THREE.Line(
projectionGeometry,
new THREE.LineDashedMaterial({
color: 0xff6b6b,
dashSize: 0.055,
gapSize: 0.045,
transparent: true,
opacity: 0.58
})
);
backpack.add(projectionLine);
const groundProjection = new THREE.Mesh(
new THREE.RingGeometry(0.075, 0.12, 24),
new THREE.MeshBasicMaterial({
color: 0xff6b6b,
transparent: true,
opacity: 0.72,
side: THREE.DoubleSide
})
);
groundProjection.rotation.x = -Math.PI / 2;
groundProjection.position.y = 0.09;
backpack.add(groundProjection);
const trailGroup = new THREE.Group();
backpack.add(trailGroup);
let trailLine = null;
let trailDots = null;
const trailMaterial = new THREE.LineBasicMaterial({
color: 0xff8b76,
transparent: true,
opacity: 0.4
});
const trailDotMaterial = new THREE.PointsMaterial({
color: 0xffb099,
size: 0.035,
transparent: true,
opacity: 0.56,
sizeAttenuation: true
});
// ------------------------------------------------------------
// Dimension line
// ------------------------------------------------------------
const dimensionGeometry = new THREE.BufferGeometry();
dimensionGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(18), 3)
);
const dimensionLine = new THREE.LineSegments(
dimensionGeometry,
new THREE.LineBasicMaterial({
color: 0xffc857,
transparent: true,
opacity: 0.9
})
);
backpack.add(dimensionLine);
const dimensionLabel = createTextSprite("28 cm", 0.82, 0.23);
backpack.add(dimensionLabel.sprite);
// ------------------------------------------------------------
// Physics / mass model
// ------------------------------------------------------------
function calculateCOM(distanceMeters, loadMass) {
const loadZ = BACK_PLANE_Z - distanceMeters;
const totalMass = PACK_MASS + 2 * loadMass;
const z =
(PACK_MASS * PACK_COM_Z + 2 * loadMass * loadZ) / totalMass;
const y =
(PACK_MASS * PACK_COM_Y + 2 * loadMass * LOAD_Y) / totalMass;
return {
x: 0,
y,
z,
loadZ,
totalMass
};
}
function forwardShift(distanceMeters, loadMass) {
const near = calculateCOM(MIN_DISTANCE, loadMass);
const current = calculateCOM(distanceMeters, loadMass);
return (near.z - current.z) * 1000;
}
function updateDimension(startZ, endZ, centimeters) {
const x = 1.12;
const y = 0.43;
const values = dimensionGeometry.attributes.position.array;
values.set([
x, y, startZ,
x, y, endZ,
x - 0.08, y, startZ,
x + 0.08, y, startZ,
x - 0.08, y, endZ,
x + 0.08, y, endZ
]);
dimensionGeometry.attributes.position.needsUpdate = true;
dimensionLabel.setText(`${centimeters.toFixed(0)} cm`);
dimensionLabel.sprite.position.set(
x + 0.13,
y + 0.03,
(startZ + endZ) / 2
);
}
function rebuildTrail(loadMass) {
if (trailLine) {
trailGroup.remove(trailLine);
trailLine.geometry.dispose();
trailLine.material.dispose();
trailLine = null;
}
if (trailDots) {
trailGroup.remove(trailDots);
trailDots.geometry.dispose();
trailDots.material.dispose();
trailDots = null;
}
const tracePoints = [];
const dotPoints = [];
const samples = 31;
for (let i = 0; i < samples; i++) {
const t = i / (samples - 1);
const distance =
MIN_DISTANCE + (MAX_DISTANCE - MIN_DISTANCE) * t;
const com = calculateCOM(distance, loadMass);
tracePoints.push(new THREE.Vector3(com.x, com.y, com.z));
if (i % 3 === 0) {
dotPoints.push(new THREE.Vector3(com.x, com.y, com.z));
}
}
trailLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(tracePoints),
trailMaterial
);
trailGroup.add(trailLine);
trailDots = new THREE.Points(
new THREE.BufferGeometry().setFromPoints(dotPoints),
trailDotMaterial
);
trailGroup.add(trailDots);
}
function rebuildChart(loadMass) {
const maxShift = forwardShift(MAX_DISTANCE, loadMass);
const samples = 31;
const path = [];
const area = [];
for (let i = 0; i < samples; i++) {
const t = i / (samples - 1);
const distance =
MIN_DISTANCE + (MAX_DISTANCE - MIN_DISTANCE) * t;
const x = 16 + t * 228;
const shift = forwardShift(distance, loadMass);
const y = 18 + (shift / Math.max(maxShift, 0.001)) * 63;
path.push(`${i === 0 ? "M" : "L"}${x.toFixed(2)},${y.toFixed(2)}`);
area.push(`${x.toFixed(2)},${y.toFixed(2)}`);
}
ui.chartPath.setAttribute("d", path.join(" "));
ui.chartArea.setAttribute(
"d",
`M16,18 L${area.join(" L")} L244,18 Z`
);
ui.chartMax.textContent = `${maxShift.toFixed(0)} mm`;
}
function updateModel(rebuild = false) {
const distanceCentimeters = Number(ui.distance.value);
const distanceMeters = distanceCentimeters / 100;
const loadMass = Number(ui.mass.value);
const com = calculateCOM(distanceMeters, loadMass);
const nearCOM = calculateCOM(MIN_DISTANCE, loadMass);
const shiftMillimeters = (nearCOM.z - com.z) * 1000;
const loadMassShare = (2 * loadMass / com.totalMass) * 100;
loadGroup.position.z = com.loadZ;
comGroup.position.set(com.x, com.y, com.z);
comLabel.sprite.position.set(com.x, com.y + 0.24, com.z);
const projection = projectionGeometry.attributes.position.array;
projection.set([
com.x, 0.09, com.z,
com.x, com.y, com.z
]);
projectionGeometry.attributes.position.needsUpdate = true;
projectionLine.computeLineDistances();
groundProjection.position.x = com.x;
groundProjection.position.z = com.z;
updateDimension(
BACK_PLANE_Z,
com.loadZ,
distanceCentimeters
);
ui.distanceValue.textContent = `${distanceCentimeters.toFixed(0)} cm`;
ui.massValue.textContent = `${loadMass.toFixed(1)} kg each`;
ui.comValue.textContent = `${(com.z * 1000).toFixed(0)} mm`;
ui.shiftValue.textContent =
`${Math.max(0, shiftMillimeters).toFixed(0)} mm forward`;
ui.loadShare.textContent = `${loadMassShare.toFixed(0)}%`;
if (distanceCentimeters < 15) {
ui.status.textContent = "Close to the back";
ui.status.style.color = "#8ee6a8";
} else if (distanceCentimeters > 40) {
ui.status.textContent = "Shifted forward";
ui.status.style.color = "#ffc857";
} else {
ui.status.textContent = "Balanced depth";
ui.status.style.color = "#8ee6a8";
}
const chartX =
16 +
((distanceMeters - MIN_DISTANCE) /
(MAX_DISTANCE - MIN_DISTANCE)) * 228;
const maxShift = forwardShift(MAX_DISTANCE, loadMass);
const chartY =
18 + (shiftMillimeters / Math.max(maxShift, 0.001)) * 63;
ui.chartDot.setAttribute("cx", chartX.toFixed(2));
ui.chartDot.setAttribute("cy", chartY.toFixed(2));
for (const label of loadLabels) {
label.setText(`${loadMass.toFixed(1)} kg`);
}
const percentage =
((distanceCentimeters - 4) / (52 - 4)) * 100;
ui.distance.style.setProperty("--fill", `${percentage}%`);
ui.mass.style.setProperty(
"--fill",
`${((loadMass - 1) / (5 - 1)) * 100}%`
);
if (rebuild) {
rebuildTrail(loadMass);
rebuildChart(loadMass);
}
}
// ------------------------------------------------------------
// Direct camera controls: no examples/addons used.
// ------------------------------------------------------------
const cameraState = {
yaw: 2.49,
pitch: 0.26,
radius: 6.4,
target: new THREE.Vector3(0, 1.32, 0)
};
function updateCamera() {
const horizontalRadius =
cameraState.radius * Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x +
cameraState.radius * Math.sin(cameraState.yaw) *
Math.cos(cameraState.pitch),
cameraState.target.y + cameraState.radius * Math.sin(cameraState.pitch),
cameraState.target.z +
horizontalRadius * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.yaw = 2.49;
cameraState.pitch = 0.26;
cameraState.radius = 6.4;
cameraState.target.set(0, 1.32, 0);
updateCamera();
}
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.yaw -= deltaX * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + deltaY * 0.005,
-0.08,
1.12
);
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
3.3,
10
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ------------------------------------------------------------
// UI behavior
// ------------------------------------------------------------
let cameraTween = null;
function moveDistanceTo(target) {
cameraTween = {
from: Number(ui.distance.value),
to: target,
start: performance.now(),
duration: 1100
};
}
ui.distance.addEventListener("input", () => {
cameraTween = null;
updateModel(false);
});
ui.mass.addEventListener("input", () => {
cameraTween = null;
updateModel(true);
});
ui.nearButton.addEventListener("click", () => {
moveDistanceTo(4);
});
ui.farButton.addEventListener("click", () => {
moveDistanceTo(52);
});
ui.resetCamera.addEventListener("click", resetCamera);
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
// ------------------------------------------------------------
// Initialization and animation
// ------------------------------------------------------------
updateCamera();
updateModel(true);
function animate(time) {
requestAnimationFrame(animate);
if (cameraTween) {
const rawProgress = THREE.MathUtils.clamp(
(time - cameraTween.start) / cameraTween.duration,
0,
1
);
const eased =
rawProgress < 0.5
? 4 * rawProgress * rawProgress * rawProgress
: 1 -
Math.pow(-2 * rawProgress + 2, 3) / 2;
const value = THREE.MathUtils.lerp(
cameraTween.from,
cameraTween.to,
eased
);
ui.distance.value = value.toFixed(2);
updateModel(false);
if (rawProgress >= 1) {
cameraTween = null;
}
}
updateCamera();
comRing.quaternion.copy(camera.quaternion);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
ui.loading.remove();
</script>
</body>
</html>
```