Prompt specification
Build or customize the Three.js 3D scene "Folding stall frame joints" 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: Folding stall frame joints
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/folding-stall-frame-joints-x0509
Scene Description Prompt:
I'd like to see this as a stylized Three.js scene: folding stall frame joints. Display a compact market frame with telescoping legs, hinge stops, and separate canopy; show packed and partly open states side by side for structural comparison. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (folding-stall-frame-joints-x0509):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Folding Market Stall — Structural Study</title>
<style>
:root {
color-scheme: dark;
--ink: #e8f0fa;
--muted: #91a4b8;
--cyan: #6fe7dc;
--amber: #ffb65c;
--coral: #ff765f;
--panel: rgba(10, 18, 31, 0.76);
--line: rgba(163, 193, 220, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #09111e;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body::before {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 16%, rgba(54, 103, 134, 0.12), transparent 42%),
linear-gradient(180deg, #0c1726 0%, #080f1a 100%);
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#stage canvas {
touch-action: none;
outline: none;
}
.hud {
position: fixed;
z-index: 5;
pointer-events: none;
}
#heading {
top: 24px;
left: 28px;
max-width: 390px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 22px;
height: 1px;
background: currentColor;
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 38px);
font-weight: 720;
line-height: 1.03;
letter-spacing: -0.045em;
}
#heading p {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
#controls {
top: 24px;
right: 28px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
border: 1px solid var(--line);
border-radius: 8px;
padding: 9px 12px;
background: rgba(15, 26, 43, 0.82);
color: #c9d7e6;
font: inherit;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(12px);
transition: border-color 160ms ease, color 160ms ease, transform 160ms ease;
}
button:hover {
color: white;
border-color: rgba(111, 231, 220, 0.55);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
#legend {
right: 28px;
bottom: 24px;
width: 220px;
padding: 14px 15px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
backdrop-filter: blur(14px);
}
.legend-title {
margin-bottom: 11px;
color: #74889d;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 8px;
color: #bdcbd9;
font-size: 11px;
}
.swatch {
width: 19px;
height: 4px;
border-radius: 99px;
background: #657b91;
}
.swatch.gold {
background: #e8b65e;
}
.swatch.coral {
background: var(--coral);
}
.swatch.teal {
background: var(--cyan);
}
#hint {
left: 50%;
bottom: 24px;
transform: translateX(-50%);
color: #6f8297;
font-size: 10px;
letter-spacing: 0.08em;
white-space: nowrap;
}
.scene-label {
position: fixed;
z-index: 4;
top: 0;
left: 0;
display: flex;
align-items: center;
gap: 10px;
min-width: 190px;
padding: 10px 13px;
border: 1px solid rgba(151, 181, 208, 0.19);
border-radius: 10px;
background: rgba(9, 17, 29, 0.82);
box-shadow: 0 10px 35px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(10px);
pointer-events: none;
will-change: transform;
}
.state-number {
display: grid;
width: 28px;
height: 28px;
place-items: center;
border: 1px solid rgba(111, 231, 220, 0.5);
border-radius: 50%;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
}
.state-label.open .state-number {
color: var(--amber);
border-color: rgba(255, 182, 92, 0.58);
}
.state-copy strong {
display: block;
font-size: 11px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.state-copy small {
display: block;
margin-top: 3px;
color: #8497aa;
font-size: 9px;
letter-spacing: 0.035em;
}
.pin {
position: fixed;
z-index: 4;
top: 0;
left: 0;
padding: 6px 9px;
border: 1px solid rgba(255, 182, 92, 0.32);
border-radius: 5px;
background: rgba(12, 20, 32, 0.86);
color: #e7edf5;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.11em;
white-space: nowrap;
pointer-events: none;
will-change: transform;
}
.pin::after {
content: "";
position: absolute;
left: 12px;
top: 100%;
width: 1px;
height: 21px;
background: linear-gradient(var(--amber), transparent);
opacity: 0.7;
}
#collarPin {
border-color: rgba(111, 231, 220, 0.32);
}
#collarPin::after {
background: linear-gradient(var(--cyan), transparent);
}
@media (max-width: 760px) {
#heading {
top: 16px;
left: 16px;
}
#heading p,
#legend,
.state-copy small {
display: none;
}
#controls {
top: 16px;
right: 16px;
}
button {
padding: 8px 9px;
font-size: 9px;
}
.scene-label {
min-width: auto;
padding: 7px 9px;
}
.state-number {
width: 23px;
height: 23px;
}
.pin {
display: none;
}
#hint {
bottom: 14px;
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<section id="heading" class="hud">
<div class="eyebrow">Structural study / 02</div>
<h1>Folding market frame</h1>
<p>
Packed and partly deployed configurations with sliding legs,
articulated rails, hinge stops, and detached canopy assemblies.
</p>
</section>
<nav id="controls" class="hud" aria-label="Camera controls">
<button id="wideView" type="button">Wide view</button>
<button id="jointView" type="button">Joint detail</button>
</nav>
<div id="packedLabel" class="scene-label">
<div class="state-number">01</div>
<div class="state-copy">
<strong>Packed</strong>
<small>rails stowed · canopy detached</small>
</div>
</div>
<div id="openLabel" class="scene-label open">
<div class="state-number">02</div>
<div class="state-copy">
<strong>Partly open</strong>
<small>hinges stopped · canopy detached</small>
</div>
</div>
<div id="hingePin" class="pin">HINGE + STOP</div>
<div id="collarPin" class="pin">SLIDING COLLAR</div>
<aside id="legend" class="hud">
<div class="legend-title">Component key</div>
<div class="legend-row"><i class="swatch"></i>Frame tube</div>
<div class="legend-row"><i class="swatch gold"></i>Telescoping section</div>
<div class="legend-row"><i class="swatch coral"></i>Pivot / latch</div>
<div class="legend-row"><i class="swatch teal"></i>Canopy / alignment</div>
</aside>
<div id="hint" class="hud">Drag to orbit · Scroll to zoom · 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";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.setClearColor(0x09111e, 1);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x09111e);
scene.fog = new THREE.Fog(0x09111e, 15, 31);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.05,
80
);
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const tempVector = new THREE.Vector3();
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const materials = {
frame: new THREE.MeshStandardMaterial({
color: 0x4b6177,
metalness: 0.72,
roughness: 0.3
}),
frameDark: new THREE.MeshStandardMaterial({
color: 0x26394d,
metalness: 0.68,
roughness: 0.36
}),
frameLight: new THREE.MeshStandardMaterial({
color: 0x8296a8,
metalness: 0.68,
roughness: 0.28
}),
slider: new THREE.MeshStandardMaterial({
color: 0xe0aa54,
metalness: 0.72,
roughness: 0.28
}),
pivot: new THREE.MeshStandardMaterial({
color: 0xff735b,
metalness: 0.42,
roughness: 0.32
}),
stop: new THREE.MeshStandardMaterial({
color: 0xf4c65b,
metalness: 0.35,
roughness: 0.42
}),
rubber: new THREE.MeshStandardMaterial({
color: 0x111820,
metalness: 0.05,
roughness: 0.86
}),
bolt: new THREE.MeshStandardMaterial({
color: 0x101722,
metalness: 0.82,
roughness: 0.24
}),
platform: new THREE.MeshStandardMaterial({
color: 0x101c2b,
metalness: 0.12,
roughness: 0.72
}),
platformOpen: new THREE.MeshStandardMaterial({
color: 0x151d29,
metalness: 0.14,
roughness: 0.68
}),
canopyTeal: new THREE.MeshStandardMaterial({
color: 0x3bc6ba,
metalness: 0.02,
roughness: 0.8,
side: THREE.DoubleSide
}),
canopyCream: new THREE.MeshStandardMaterial({
color: 0xeadfc4,
metalness: 0.01,
roughness: 0.87,
side: THREE.DoubleSide
}),
canopyDark: new THREE.MeshStandardMaterial({
color: 0x155f64,
metalness: 0.02,
roughness: 0.82,
side: THREE.DoubleSide
})
};
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb8d8ef, 0x17202d, 1.75));
const keyLight = new THREE.DirectionalLight(0xfff0d7, 3.1);
keyLight.position.set(5.5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const tealRim = new THREE.PointLight(0x49d9cf, 34, 14, 2);
tealRim.position.set(-6, 4.5, 2);
scene.add(tealRim);
const amberRim = new THREE.PointLight(0xffa85c, 30, 13, 2);
amberRim.position.set(6, 3.8, -4);
scene.add(amberRim);
const frontFill = new THREE.DirectionalLight(0x91b7d9, 1.15);
frontFill.position.set(-4, 4, 8);
scene.add(frontFill);
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function addMesh(parent, geometry, material, position = [0, 0, 0]) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addBox(
parent,
size,
position,
material,
rotation = [0, 0, 0]
) {
const mesh = addMesh(
parent,
new THREE.BoxGeometry(...size),
material,
position
);
mesh.rotation.set(...rotation);
return mesh;
}
function addTube(
parent,
start,
end,
radius,
material,
radialSegments = 12
) {
const a = new THREE.Vector3(...start);
const b = new THREE.Vector3(...end);
const direction = b.clone().sub(a);
const length = direction.length();
if (length < 0.0001) return null;
const mesh = addMesh(
parent,
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments,
1,
false
),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
Y_AXIS,
direction.normalize()
);
return mesh;
}
function addDisc(parent, position, axis, radius, depth, material) {
const disc = addMesh(
parent,
new THREE.CylinderGeometry(radius, radius, depth, 24),
material,
position
);
if (axis === "z") disc.rotation.x = Math.PI / 2;
if (axis === "x") disc.rotation.z = -Math.PI / 2;
if (axis === "y") disc.rotation.y = 0;
return disc;
}
function addEdges(parent, mesh, color, opacity = 0.32) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(edges);
return edges;
}
function addGuide(parent, start, end, color = 0x79ded7) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(...start),
new THREE.Vector3(...end)
]);
const line = new THREE.Line(
geometry,
new THREE.LineDashedMaterial({
color,
dashSize: 0.075,
gapSize: 0.055,
transparent: true,
opacity: 0.42
})
);
line.computeLineDistances();
parent.add(line);
return line;
}
function addPlatform(parent, open = false) {
const material = open
? materials.platformOpen
: materials.platform;
const platform = addBox(
parent,
[4.35, 0.085, 2.85],
[0, -0.035, 0],
material
);
addEdges(
parent,
platform,
open ? 0xe6a653 : 0x4acbc4,
0.38
);
const ring = addMesh(
parent,
new THREE.RingGeometry(2.06, 2.075, 96),
new THREE.MeshBasicMaterial({
color: open ? 0xe6a653 : 0x4acbc4,
transparent: true,
opacity: 0.24,
side: THREE.DoubleSide
}),
[0, 0.012, 0]
);
ring.rotation.x = -Math.PI / 2;
ring.castShadow = false;
ring.receiveShadow = false;
}
// -------------------------------------------------------------------------
// Structural components
// -------------------------------------------------------------------------
function addHinge(parent, position, axis = "z", scale = 1) {
const [x, y, z] = position;
addDisc(
parent,
[x, y, z],
axis,
0.105 * scale,
0.075 * scale,
materials.pivot
);
addDisc(
parent,
[x, y, z],
axis,
0.043 * scale,
0.105 * scale,
materials.bolt
);
addBox(
parent,
[0.19 * scale, 0.11 * scale, 0.075 * scale],
[x, y + 0.095 * scale, z],
materials.frameDark
);
addBox(
parent,
[0.13 * scale, 0.047 * scale, 0.08 * scale],
[x + 0.12 * scale, y + 0.045 * scale, z],
materials.stop,
[0, 0, -0.17]
);
addTube(
parent,
[x + 0.07 * scale, y - 0.02 * scale, z + 0.06 * scale],
[x + 0.235 * scale, y - 0.13 * scale, z + 0.11 * scale],
0.014 * scale,
materials.pivot,
8
);
addDisc(
parent,
[x + 0.235 * scale, y - 0.13 * scale, z + 0.11 * scale],
"x",
0.033 * scale,
0.05 * scale,
materials.stop
);
}
function addRail(
parent,
start,
end,
axis = "z",
radius = 0.043,
sleeve = true
) {
const a = new THREE.Vector3(...start);
const b = new THREE.Vector3(...end);
addTube(parent, start, end, radius, materials.frame, 14);
if (sleeve) {
const s1 = a.clone().lerp(b, 0.08);
const s2 = a.clone().lerp(b, 0.18);
const e1 = a.clone().lerp(b, 0.82);
const e2 = a.clone().lerp(b, 0.92);
addTube(
parent,
s1.toArray(),
s2.toArray(),
radius * 1.36,
materials.frameDark,
14
);
addTube(
parent,
e1.toArray(),
e2.toArray(),
radius * 1.36,
materials.frameDark,
14
);
}
addBox(
parent,
[0.095, 0.085, 0.095],
end,
materials.pivot
);
addBox(
parent,
[0.038, 0.026, 0.105],
[end[0], end[1] + 0.012, end[2]],
materials.bolt
);
}
function addLeg(parent, foot, top) {
const footVector = new THREE.Vector3(...foot);
const topVector = new THREE.Vector3(...top);
const direction = topVector.clone().sub(footVector);
const length = direction.length();
const leg = new THREE.Group();
leg.position.copy(footVector);
leg.quaternion.setFromUnitVectors(
Y_AXIS,
direction.clone().normalize()
);
parent.add(leg);
const outerStart = 0.12;
const outerEnd = length * 0.52;
const innerStart = length * 0.46;
const innerEnd = length - 0.035;
addTube(
leg,
[0, outerStart, 0],
[0, outerEnd, 0],
0.063,
materials.frameDark,
16
);
addTube(
leg,
[0, innerStart, 0],
[0, innerEnd, 0],
0.044,
materials.slider,
16
);
addDisc(
leg,
[0, outerEnd, 0],
"y",
0.081,
0.145,
materials.frameDark
);
addDisc(
leg,
[0, outerEnd + 0.04, 0],
"y",
0.082,
0.035,
materials.pivot
);
addBox(
leg,
[0.15, 0.045, 0.055],
[0.08, outerEnd - 0.025, 0.045],
materials.stop,
[0.12, 0, -0.22]
);
addTube(
leg,
[0.045, outerEnd - 0.01, 0.06],
[0.16, outerEnd - 0.09, 0.11],
0.013,
materials.pivot,
8
);
const holeYs = [
length * 0.62,
length * 0.73,
length * 0.84
];
for (const holeY of holeYs) {
addDisc(
leg,
[0, holeY, 0.046],
"z",
0.013,
0.012,
materials.bolt
);
}
addDisc(
leg,
[0, 0.065, 0],
"y",
0.11,
0.11,
materials.rubber
);
addDisc(
leg,
[0, 0.017, 0],
"y",
0.145,
0.035,
materials.frameDark
);
addDisc(
leg,
[0, innerEnd, 0],
"y",
0.061,
0.07,
materials.frameDark
);
return topVector;
}
function addCrossBrace(parent, a, b, c, d) {
addTube(parent, a, b, 0.025, materials.frameDark, 10);
addTube(parent, c, d, 0.025, materials.frameDark, 10);
const center = new THREE.Vector3(...a)
.add(new THREE.Vector3(...b))
.multiplyScalar(0.5);
addDisc(
parent,
center.toArray(),
"x",
0.073,
0.07,
materials.pivot
);
addDisc(
parent,
center.toArray(),
"x",
0.028,
0.09,
materials.bolt
);
}
// -------------------------------------------------------------------------
// Canopies
// -------------------------------------------------------------------------
function canopyHeight(x, width, z, depth, stripIndex) {
const ridge = 0.19 * (1 - Math.pow(x / (width * 0.5), 2));
const cornerDrop =
0.055 *
Math.pow(Math.abs(x) / (width * 0.5), 4) *
Math.pow(Math.abs(z) / (depth * 0.5), 4);
const pleat = 0.018 * Math.sin(stripIndex * Math.PI);
return ridge - cornerDrop + pleat;
}
function addCanopy(parent, packed = false) {
const canopy = new THREE.Group();
if (packed) {
canopy.position.set(0, 2.82, -0.04);
const panelCount = 11;
const panelWidth = 0.16;
const panelDepth = 0.34;
for (let i = 0; i < panelCount; i++) {
const x =
(i - (panelCount - 1) / 2) * panelWidth;
const material =
i % 3 === 0
? materials.canopyCream
: i % 3 === 1
? materials.canopyTeal
: materials.canopyDark;
addBox(
canopy,
[panelWidth * 0.92, 0.035, panelDepth],
[x, i * 0.027, 0],
material,
[0, 0, (i % 2 ? 1 : -1) * 0.018]
);
}
addBox(
canopy,
[0.047, 0.37, 0.38],
[-0.5, 0.13, 0],
materials.frameDark
);
addBox(
canopy,
[0.047, 0.37, 0.38],
[0.5, 0.13, 0],
materials.frameDark
);
addDisc(
canopy,
[0, 0.12, 0.194],
"z",
0.058,
0.03,
materials.pivot
);
parent.add(canopy);
return canopy;
}
canopy.position.set(0.08, 3.18, -0.04);
const width = 2.82;
const depth = 1.83;
const strips = 12;
const step = width / strips;
for (let i = 0; i < strips; i++) {
const x0 = -width / 2 + i * step;
const x1 = x0 + step;
const y00 = canopyHeight(x0, width, -depth / 2, depth, i);
const y10 = canopyHeight(x1, width, -depth / 2, depth, i + 1);
const y11 = canopyHeight(x1, width, depth / 2, depth, i + 1);
const y01 = canopyHeight(x0, width, depth / 2, depth, i);
const positions = new Float32Array([
x0, y00, -depth / 2,
x1, y10, -depth / 2,
x1, y11, depth / 2,
x0, y00, -depth / 2,
x1, y11, depth / 2,
x0, y01, depth / 2
]);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
const material =
i % 4 === 0
? materials.canopyCream
: i % 4 === 1
? materials.canopyTeal
: i % 4 === 2
? materials.canopyDark
: materials.canopyTeal;
const strip = addMesh(canopy, geometry, material);
strip.castShadow = true;
// Front valance.
const valancePositions = new Float32Array([
x0, y01, depth / 2,
x1, y11, depth / 2,
x1, y11 - 0.17, depth / 2,
x0, y01, depth / 2,
x1, y11 - 0.17, depth / 2,
x0, y01 - 0.17, depth / 2
]);
const valanceGeometry = new THREE.BufferGeometry();
valanceGeometry.setAttribute(
"position",
new THREE.BufferAttribute(valancePositions, 3)
);
valanceGeometry.computeVertexNormals();
addMesh(canopy, valanceGeometry, material);
// Lightweight underside rib.
if (i % 3 === 0) {
const ribX = (x0 + x1) * 0.5;
const ribY =
canopyHeight(ribX, width, 0, depth, i) - 0.027;
addTube(
canopy,
[ribX, ribY, -depth / 2 + 0.04],
[ribX, ribY, depth / 2 - 0.04],
0.012,
materials.frameLight,
8
);
}
}
// Longitudinal seams.
for (let i = 2; i < strips; i += 2) {
const x = -width / 2 + i * step;
const backY = canopyHeight(x, width, -depth / 2, depth, i) + 0.008;
const frontY = canopyHeight(x, width, depth / 2, depth, i) + 0.008;
addTube(
canopy,
[x, backY, -depth / 2],
[x, frontY, depth / 2],
0.007,
materials.canopyCream,
6
);
}
// Corner eyelets.
for (const x of [-width / 2 + 0.08, width / 2 - 0.08]) {
for (const z of [-depth / 2 + 0.08, depth / 2 - 0.08]) {
const y = canopyHeight(x, width, z, depth, 0) - 0.015;
addDisc(
canopy,
[x, y, z],
"y",
0.027,
0.025,
materials.frameLight
);
}
}
parent.add(canopy);
return canopy;
}
// -------------------------------------------------------------------------
// Packed state
// -------------------------------------------------------------------------
const packed = new THREE.Group();
packed.position.x = -3.02;
scene.add(packed);
addPlatform(packed, false);
const packedFeet = [
[-0.66, 0.085, 0.53],
[0.66, 0.085, 0.53],
[-0.66, 0.085, -0.53],
[0.66, 0.085, -0.53]
];
const packedTops = [
[-0.78, 2.32, 0.58],
[0.78, 2.32, 0.58],
[-0.78, 2.32, -0.58],
[0.78, 2.32, -0.58]
];
packedFeet.forEach((foot, index) => {
addLeg(packed, foot, packedTops[index]);
});
// Four upper rails folded down and locked alongside the legs.
addRail(
packed,
packedTops[0],
[-0.91, 0.62, 0.67],
"z",
0.039
);
addRail(
packed,
packedTops[1],
[0.91, 0.62, 0.67],
"z",
0.039
);
addRail(
packed,
packedTops[2],
[-0.91, 0.62, -0.67],
"z",
0.039
);
addRail(
packed,
packedTops[3],
[0.91, 0.62, -0.67],
"z",
0.039
);
packedTops.forEach((top) => {
addHinge(packed, top, "z", 0.87);
});
// Compressed diagonal members.
addTube(
packed,
[-0.6, 0.65, -0.51],
[0.6, 1.65, -0.55],
0.024,
materials.frameDark,
10
);
addTube(
packed,
[0.6, 0.65, -0.51],
[-0.6, 1.65, -0.55],
0.024,
materials.frameDark,
10
);
addDisc(
packed,
[0, 1.15, -0.53],
"x",
0.07,
0.075,
materials.pivot
);
const packedCanopy = addCanopy(packed, true);
addGuide(
packed,
[-0.78, 2.38, 0.58],
[-0.76, 2.87, -0.04],
0x6fe7dc
);
addGuide(
packed,
[0.78, 2.38, 0.58],
[0.76, 2.87, -0.04],
0x6fe7dc
);
// -------------------------------------------------------------------------
// Partly open state
// -------------------------------------------------------------------------
const open = new THREE.Group();
open.position.x = 3.02;
scene.add(open);
addPlatform(open, true);
const openFeet = [
[-1.03, 0.085, 0.72],
[1.03, 0.085, 0.72],
[-1.03, 0.085, -0.72],
[1.03, 0.085, -0.72]
];
const openTops = [
[-1.24, 2.38, 0.88],
[1.24, 2.38, 0.88],
[-1.24, 2.38, -0.88],
[1.24, 2.38, -0.88]
];
openFeet.forEach((foot, index) => {
addLeg(open, foot, openTops[index]);
});
// Split front rail, stopped just before the center coupling.
addRail(
open,
openTops[0],
[-0.10, 2.34, 0.82],
"z",
0.045
);
addRail(
open,
openTops[1],
[0.10, 2.34, 0.82],
"z",
0.045
);
// Split back rail.
addRail(
open,
openTops[2],
[-0.10, 2.34, -0.82],
"z",
0.045
);
addRail(
open,
openTops[3],
[0.10, 2.34, -0.82],
"z",
0.045
);
// Side rails are only partly unfolded.
addRail(
open,
openTops[0],
[-1.18, 2.34, 0.10],
"x",
0.042
);
addRail(
open,
openTops[2],
[-1.18, 2.34, -0.10],
"x",
0.042
);
addRail(
open,
openTops[1],
[1.18, 2.34, 0.10],
"x",
0.042
);
addRail(
open,
openTops[3],
[1.18, 2.34, -0.10],
"x",
0.042
);
// Visible pivot hardware at every principal corner.
addHinge(open, openTops[0], "z", 1);
addHinge(open, openTops[1], "z", 1);
addHinge(open, openTops[2], "z", 1);
addHinge(open, openTops[3], "z", 1);
// Side-axis pivots at the right-hand corners.
addHinge(open, openTops[1], "x", 0.78);
addHinge(open, openTops[3], "x", 0.78);
// Lower stabilizing perimeter.
addTube(
open,
[-1.02, 1.00, 0.73],
[1.02, 1.00, 0.73],
0.028,
materials.frameDark,
10
);
addTube(
open,
[-1.02, 1.00, -0.73],
[1.02, 1.00, -0.73],
0.028,
materials.frameDark,
10
);
// Side X-bracing and its center locking pivot.
addCrossBrace(
open,
[1.02, 0.27, 0.72],
[1.20, 2.12, -0.87],
[1.02, 0.27, -0.72],
[1.20, 2.12, 0.87]
);
const openCanopy = addCanopy(open, false);
// Dashed alignment guides emphasize that the canopy is a separate part.
addGuide(
open,
[-1.24, 2.46, 0.88],
[-1.32, 3.12, 0.82],
0xffb65c
);
addGuide(
open,
[1.24, 2.46, 0.88],
[1.40, 3.12, 0.82],
0xffb65c
);
addGuide(
open,
[-1.24, 2.46, -0.88],
[-1.32, 3.12, -0.82],
0xffb65c
);
addGuide(
open,
[1.24, 2.46, -0.88],
[1.40, 3.12, -0.82],
0xffb65c
);
// -------------------------------------------------------------------------
// Ground
// -------------------------------------------------------------------------
const ground = addMesh(
scene,
new THREE.PlaneGeometry(38, 38),
new THREE.MeshStandardMaterial({
color: 0x0b1422,
metalness: 0.08,
roughness: 0.9
}),
[0, -0.085, 0]
);
ground.rotation.x = -Math.PI / 2;
ground.castShadow = false;
const grid = new THREE.GridHelper(
34,
68,
0x355065,
0x1d3041
);
grid.position.y = -0.077;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
// -------------------------------------------------------------------------
// Custom camera controls, implemented without external controls modules
// -------------------------------------------------------------------------
const orbit = {
theta: 0.54,
phi: 1.10,
radius: 13.2,
target: new THREE.Vector3(0, 1.55, 0)
};
let cameraTween = null;
let dragging = false;
let previousPointer = { x: 0, y: 0 };
let pointerId = null;
function syncCameraFromOrbit() {
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);
}
function updateOrbitFromCamera() {
const offset = camera.position.clone().sub(orbit.target);
orbit.radius = offset.length();
orbit.theta = Math.atan2(offset.x, offset.z);
orbit.phi = Math.acos(
THREE.MathUtils.clamp(offset.y / orbit.radius, -1, 1)
);
}
function flyTo(position, target) {
cameraTween = {
start: performance.now(),
duration: 750,
fromPosition: camera.position.clone(),
fromTarget: orbit.target.clone(),
toPosition: position.clone(),
toTarget: target.clone()
};
}
function wideView() {
const aspect = window.innerWidth / window.innerHeight;
const radius = aspect < 1 ? 20 : 13.2;
flyTo(
new THREE.Vector3(
radius * Math.sin(1.10) * Math.sin(0.54),
1.55 + radius * Math.cos(1.10),
radius * Math.sin(1.10) * Math.cos(0.54)
),
new THREE.Vector3(0, 1.55, 0)
);
}
function jointView() {
const target = new THREE.Vector3(3.02 + 1.15, 2.28, 0.83);
flyTo(
target.clone().add(new THREE.Vector3(3.0, 1.55, 3.4)),
target
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
cameraTween = null;
dragging = true;
pointerId = event.pointerId;
previousPointer.x = event.clientX;
previousPointer.y = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== pointerId) return;
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
previousPointer.x = event.clientX;
previousPointer.y = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.005,
0.42,
1.43
);
syncCameraFromOrbit();
});
function endDrag(event) {
if (!dragging || event.pointerId !== pointerId) return;
dragging = false;
renderer.domElement.style.cursor = "grab";
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraTween = null;
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
5.2,
23
);
syncCameraFromOrbit();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", wideView);
renderer.domElement.style.cursor = "grab";
document
.getElementById("wideView")
.addEventListener("click", wideView);
document
.getElementById("jointView")
.addEventListener("click", jointView);
// -------------------------------------------------------------------------
// Projected DOM labels
// -------------------------------------------------------------------------
const packedLabel = document.getElementById("packedLabel");
const openLabel = document.getElementById("openLabel");
const hingePin = document.getElementById("hingePin");
const collarPin = document.getElementById("collarPin");
function projectPoint(worldPoint) {
const projected = worldPoint.clone().project(camera);
return {
x: (projected.x * 0.5 + 0.5) * window.innerWidth,
y: (-projected.y * 0.5 + 0.5) * window.innerHeight,
visible: projected.z > -1 && projected.z < 1
};
}
function updateLabel(element, worldPoint, offsetX = 0, offsetY = 0) {
const point = projectPoint(worldPoint);
element.style.left = "0";
element.style.top = "0";
element.style.transform =
`translate(${point.x + offsetX}px, ${point.y + offsetY}px) ` +
"translate(-50%, -100%)";
element.style.opacity = point.visible ? "1" : "0";
}
function updatePinnedLabel(element, worldPoint, offsetX, offsetY) {
const point = projectPoint(worldPoint);
element.style.left = "0";
element.style.top = "0";
element.style.transform =
`translate(${point.x + offsetX}px, ${point.y + offsetY}px) ` +
"translate(-50%, -100%)";
element.style.opacity = point.visible ? "1" : "0";
}
function updateProjectedLabels() {
scene.updateMatrixWorld(true);
const packedAnchor = packed.localToWorld(
new THREE.Vector3(-0.05, 3.42, 0.05)
);
const openAnchor = open.localToWorld(
new THREE.Vector3(0.08, 3.73, -0.03)
);
const hingeAnchor = open.localToWorld(
new THREE.Vector3(1.24, 2.43, 0.94)
);
const collarAnchor = open.localToWorld(
new THREE.Vector3(1.12, 1.27, 0.80)
);
updateLabel(packedLabel, packedAnchor, 0, -6);
updateLabel(openLabel, openAnchor, 0, -8);
updatePinnedLabel(hingePin, hingeAnchor, 44, -30);
updatePinnedLabel(collarPin, collarAnchor, 52, -16);
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", onResize);
if (window.innerWidth / window.innerHeight < 1) {
orbit.radius = 20;
}
syncCameraFromOrbit();
function animate(now) {
requestAnimationFrame(animate);
if (cameraTween) {
const raw = Math.min(
1,
(now - cameraTween.start) / cameraTween.duration
);
const eased =
raw < 0.5
? 4 * raw * raw * raw
: 1 - Math.pow(-2 * raw + 2, 3) / 2;
camera.position.lerpVectors(
cameraTween.fromPosition,
cameraTween.toPosition,
eased
);
orbit.target.lerpVectors(
cameraTween.fromTarget,
cameraTween.toTarget,
eased
);
camera.lookAt(orbit.target);
if (raw >= 1) {
updateOrbitFromCamera();
cameraTween = null;
}
}
// A very slight suspended-canopy drift.
const time = now * 0.001;
packedCanopy.position.y = 2.82 + Math.sin(time * 0.7) * 0.012;
openCanopy.position.y = 3.18 + Math.sin(time * 0.7 + 1.3) * 0.018;
updateProjectedLabels();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```