Prompt specification
Build or customize the Three.js 3D scene "Compare roof geometry with the same footprint" 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: Compare roof geometry with the same footprint
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/compare-roof-geometry-with-the-same-footprint-x2236
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a voxel hall whose roof can change between flat, pitched, and vaulted forms. Keep the rectangular floor plan, wall positions, and reference height markers fixed. Switching roof type replaces only the roof geometry and its stated schematic support points. Show the resulting interior clearance with a few vertical measuring rods and display the modeled roof surface area. Use a cutaway near wall so the underside is visible. Keep the three designs comparable in scale and separate the geometric comparison from a real structural analysis. Warm masonry blocks, dark supports, and a simple ground plane should make the roof shapes the main focus.
Working Three.js Starter (compare-roof-geometry-with-the-same-footprint-x2236):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Hall — Geometric Roof Comparison</title>
<style>
:root {
--ink: #26231f;
--muted: #6d655b;
--paper: rgba(248, 244, 235, 0.94);
--line: rgba(63, 54, 43, 0.16);
--accent: #c5633e;
--teal: #087f79;
--gold: #d99325;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 35% 25%, #f3eee3 0, #ddd6c8 48%, #bdb4a6 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas.dragging { cursor: grabbing; }
.heading {
position: fixed;
top: 18px;
left: 20px;
z-index: 5;
pointer-events: none;
text-shadow: 0 1px 0 rgba(255,255,255,.55);
}
.heading h1 {
margin: 0;
font-size: clamp(19px, 2.2vw, 30px);
font-weight: 750;
letter-spacing: -.035em;
}
.heading p {
margin: 4px 0 0;
color: #5e554b;
font-size: 12px;
letter-spacing: .08em;
text-transform: uppercase;
}
.panel {
position: fixed;
z-index: 10;
top: 18px;
right: 18px;
width: min(340px, calc(100vw - 28px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 17px;
border: 1px solid rgba(75, 61, 45, .18);
border-radius: 17px;
background: var(--paper);
box-shadow:
0 18px 50px rgba(47, 36, 23, .17),
inset 0 1px 0 rgba(255,255,255,.8);
backdrop-filter: blur(13px);
}
.eyebrow {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: .12em;
text-transform: uppercase;
}
.panel h2 {
margin: 4px 0 2px;
font-size: 20px;
letter-spacing: -.025em;
}
.fixed-note {
margin: 8px 0 13px;
color: #6c6257;
font-size: 11.5px;
line-height: 1.45;
}
.roof-buttons {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin: 12px 0 15px;
}
button {
appearance: none;
border: 1px solid rgba(74, 58, 42, .18);
border-radius: 10px;
background: rgba(255,255,255,.58);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 700;
padding: 10px 5px;
cursor: pointer;
transition: transform .15s ease, background .15s ease, color .15s ease;
}
button:hover { transform: translateY(-1px); }
button:focus-visible { outline: 3px solid rgba(8,127,121,.25); outline-offset: 2px; }
.roof-buttons button[aria-pressed="true"] {
color: white;
background: var(--accent);
border-color: var(--accent);
box-shadow: 0 5px 12px rgba(197,99,62,.22);
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 13px;
}
.metric {
min-height: 77px;
padding: 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255,255,255,.42);
}
.metric .name {
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: .09em;
text-transform: uppercase;
}
.metric .value {
margin-top: 5px;
font-size: 21px;
font-weight: 780;
letter-spacing: -.035em;
}
.metric .sub {
margin-top: 1px;
color: var(--muted);
font-size: 10px;
}
.section-title {
margin: 14px 0 7px;
color: var(--muted);
font-size: 10px;
font-weight: 780;
letter-spacing: .1em;
text-transform: uppercase;
}
.points {
margin: 0;
padding: 10px 11px;
border-radius: 10px;
background: rgba(217,147,37,.09);
color: #705020;
font-size: 11px;
line-height: 1.5;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 10.5px;
}
th {
padding: 6px 4px;
border-bottom: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
letter-spacing: .05em;
text-align: right;
text-transform: uppercase;
}
th:first-child, td:first-child { text-align: left; }
td {
padding: 7px 4px;
border-bottom: 1px solid rgba(63,54,43,.09);
text-align: right;
font-variant-numeric: tabular-nums;
}
tr.current td {
color: #a64e31;
font-weight: 780;
background: rgba(197,99,62,.055);
}
.disclaimer {
margin: 13px 0 0;
padding: 10px 11px;
border-left: 3px solid var(--gold);
border-radius: 3px 8px 8px 3px;
background: rgba(217,147,37,.09);
color: #66543a;
font-size: 10.5px;
line-height: 1.45;
}
.camera-help {
display: flex;
gap: 7px;
margin-top: 11px;
}
.camera-help button {
flex: 1;
padding: 8px 6px;
font-size: 10.5px;
}
.legend {
position: fixed;
z-index: 6;
left: 20px;
bottom: 18px;
display: flex;
flex-wrap: wrap;
gap: 13px;
max-width: min(560px, calc(100vw - 40px));
padding: 9px 12px;
border: 1px solid rgba(75,61,45,.13);
border-radius: 10px;
background: rgba(248,244,235,.80);
color: #5d5449;
font-size: 10.5px;
pointer-events: none;
backdrop-filter: blur(8px);
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
white-space: nowrap;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--teal);
}
.swatch.gold { background: var(--gold); }
.swatch.ref {
width: 18px;
height: 0;
border-top: 2px dashed #887969;
border-radius: 0;
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
overflow: hidden;
pointer-events: none;
}
.scene-label {
position: absolute;
transform: translate(-50%, -100%);
padding: 3px 6px;
border: 1px solid rgba(72,59,44,.13);
border-radius: 5px;
background: rgba(250,247,239,.88);
color: #4c453c;
font-size: 9.5px;
font-weight: 700;
white-space: nowrap;
box-shadow: 0 2px 8px rgba(40,30,20,.08);
}
.scene-label.clearance {
border-color: rgba(8,127,121,.24);
background: rgba(231,255,251,.91);
color: #076e69;
}
.scene-label.control {
min-width: 18px;
padding: 2px 4px;
border-color: rgba(217,147,37,.28);
background: rgba(255,246,223,.94);
color: #956214;
font-size: 8.5px;
text-align: center;
}
.scene-label.reference {
border-style: dashed;
background: rgba(248,244,235,.76);
color: #74695b;
font-size: 8.5px;
}
.scene-label.cutaway {
border-color: rgba(94,80,64,.16);
background: rgba(248,244,235,.68);
color: #766b5f;
font-size: 8.5px;
letter-spacing: .05em;
text-transform: uppercase;
}
@media (max-width: 760px) {
.panel {
top: auto;
bottom: 10px;
right: 10px;
width: calc(100vw - 20px);
max-height: 46vh;
}
.heading {
top: 12px;
left: 13px;
}
.heading p { display: none; }
.legend { display: none; }
}
</style>
</head>
<body>
<div class="heading">
<h1>Voxel Hall / Roof Geometry</h1>
<p>Fixed footprint · switchable roof · interior clearance</p>
</div>
<aside class="panel" aria-label="Roof comparison controls">
<div class="eyebrow">Geometric model</div>
<h2 id="current-title">Flat roof</h2>
<div class="fixed-note">
The 18 × 12 m floor, masonry walls, and 3 m / 6 m reference markers
remain fixed in every view.
</div>
<div class="roof-buttons" role="group" aria-label="Roof type">
<button type="button" data-roof="flat" aria-pressed="true">Flat</button>
<button type="button" data-roof="pitched" aria-pressed="false">Pitched</button>
<button type="button" data-roof="vaulted" aria-pressed="false">Vaulted</button>
</div>
<div class="metric-grid">
<div class="metric">
<div class="name">Modeled cover area</div>
<div class="value" id="area-value">216.0 m²</div>
<div class="sub">Upper roof skin</div>
</div>
<div class="metric">
<div class="name">Maximum clearance</div>
<div class="value" id="clearance-value">6.3 m</div>
<div class="sub">Floor to underside</div>
</div>
</div>
<div class="section-title">Schematic support / control points</div>
<div class="points" id="points-text"></div>
<div class="section-title">Scale comparison</div>
<table>
<thead>
<tr>
<th>Roof</th>
<th>Area</th>
<th>Max clear.</th>
<th>Points</th>
</tr>
</thead>
<tbody id="comparison-body"></tbody>
</table>
<p class="disclaimer">
<strong>Geometry only.</strong> This comparison does not evaluate loads,
deflections, connections, material capacity, buckling, code compliance,
or safety. Ridge and crown markers are geometric reference nodes, not
structural bearing assumptions.
</p>
<div class="camera-help">
<button type="button" id="reset-camera">Reset camera</button>
<button type="button" id="cutaway-view">Cutaway view</button>
</div>
</aside>
<div id="labels" aria-hidden="true"></div>
<div class="legend">
<span><i class="swatch"></i> vertical clearance rod</span>
<span><i class="swatch gold"></i> schematic point</span>
<span><i class="swatch ref"></i> fixed height reference</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Fixed model dimensions. Roof switches never alter these values.
// ------------------------------------------------------------
const HALL_WIDTH = 18;
const HALL_DEPTH = 12;
const ROOF_LENGTH = HALL_WIDTH;
const ROOF_HALF_DEPTH = HALL_DEPTH / 2;
const WALL_THICKNESS = 0.60;
const WALL_TOP = 6.30;
const ROOF_THICKNESS = 0.45;
const FLAT_TOP = WALL_TOP + ROOF_THICKNESS;
const PITCH_RISE = 3.60;
const PITCH_RIDGE_TOP = FLAT_TOP + PITCH_RISE;
const PITCH_RIDGE_UNDER = PITCH_RIDGE_TOP - ROOF_THICKNESS;
const VAULT_OUTER_RISE = 4.00;
const VAULT_INNER_RISE = VAULT_OUTER_RISE - ROOF_THICKNESS;
const VAULT_TOP = FLAT_TOP + VAULT_OUTER_RISE;
const VAULT_UNDER_APEX = FLAT_TOP + VAULT_INNER_RISE;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9d2c5);
scene.fog = new THREE.Fog(0xd9d2c5, 34, 68);
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
} catch (error) {
document.body.innerHTML =
'<p style="padding:2rem;font:16px system-ui">This scene requires WebGL.</p>';
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42, innerWidth / innerHeight, 0.1, 150
);
// Custom camera controls: no addons or examples are used.
const cameraState = {
yaw: 0.70,
pitch: 0.17,
distance: 29,
target: new THREE.Vector3(0, 4.0, 0)
};
const defaultCamera = {
yaw: 0.70,
pitch: 0.17,
distance: 29,
target: new THREE.Vector3(0, 4.0, 0)
};
function updateCamera() {
const { yaw, pitch, distance, target } = cameraState;
camera.position.set(
target.x + distance * Math.cos(pitch) * Math.sin(yaw),
target.y + distance * Math.sin(pitch),
target.z + distance * Math.cos(pitch) * Math.cos(yaw)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraState.yaw = defaultCamera.yaw;
cameraState.pitch = defaultCamera.pitch;
cameraState.distance = defaultCamera.distance;
cameraState.target.copy(defaultCamera.target);
updateCamera();
}
function setCutawayCamera() {
cameraState.yaw = 0.38;
cameraState.pitch = 0.13;
cameraState.distance = 25;
cameraState.target.set(0, 3.7, 0);
updateCamera();
}
// ------------------------------------------------------------
// Lighting and ground.
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xfff4dc, 0x6e6b68, 1.55));
const sun = new THREE.DirectionalLight(0xffe6c4, 3.1);
sun.position.set(11, 20, 13);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -23;
sun.shadow.camera.right = 23;
sun.shadow.camera.top = 23;
sun.shadow.camera.bottom = -23;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 60;
sun.shadow.bias = -0.00025;
scene.add(sun);
const interiorLight = new THREE.PointLight(0xffd7a0, 34, 24, 2);
interiorLight.position.set(0, 4.4, 2.0);
scene.add(interiorLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(100, 80),
new THREE.MeshStandardMaterial({
color: 0xb8ad9c,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.39;
ground.receiveShadow = true;
scene.add(ground);
const groundGrid = new THREE.GridHelper(80, 80, 0x9f9587, 0xb1a89c);
groundGrid.position.y = -0.382;
groundGrid.material.transparent = true;
groundGrid.material.opacity = 0.20;
scene.add(groundGrid);
// ------------------------------------------------------------
// Shared materials.
// ------------------------------------------------------------
const roofTopMaterial = new THREE.MeshStandardMaterial({
color: 0x455b5e,
roughness: 0.86,
metalness: 0.04,
side: THREE.DoubleSide
});
const roofUnderMaterial = new THREE.MeshStandardMaterial({
color: 0x352925,
roughness: 0.94,
side: THREE.DoubleSide
});
const fasciaMaterial = new THREE.MeshStandardMaterial({
color: 0x241f1c,
roughness: 0.78,
side: THREE.DoubleSide
});
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x29221e,
roughness: 0.8
});
const mortarMaterial = new THREE.MeshStandardMaterial({
color: 0x6d5144,
roughness: 1
});
const controlMaterial = new THREE.MeshBasicMaterial({
color: 0xf0ad36
});
const rodMaterial = new THREE.MeshStandardMaterial({
color: 0x42d7c8,
emissive: 0x075b56,
emissiveIntensity: 0.65,
roughness: 0.48
});
// ------------------------------------------------------------
// Floor and voxel masonry.
// Outer wall faces remain at x = ±9 and z = ±6.
// ------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.BoxGeometry(HALL_WIDTH, 0.32, HALL_DEPTH),
new THREE.MeshStandardMaterial({
color: 0x756c61,
roughness: 0.95
})
);
floor.position.y = -0.16;
floor.receiveShadow = true;
scene.add(floor);
const paverTransforms = [];
const paverColors = [];
for (let x = 0; x < HALL_WIDTH; x++) {
for (let z = 0; z < HALL_DEPTH; z++) {
paverTransforms.push({
x: -8.5 + x,
z: -5.5 + z,
sx: 0.96,
sz: 0.96,
sy: 0.045 + Math.random() * 0.012
});
const color = new THREE.Color().setHSL(
0.085,
0.10 + Math.random() * 0.05,
0.40 + Math.random() * 0.075
);
paverColors.push(color);
}
}
const paverMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.98 }),
paverTransforms.length
);
const dummy = new THREE.Object3D();
paverTransforms.forEach((p, i) => {
dummy.position.set(p.x, 0.022, p.z);
dummy.scale.set(p.sx, p.sy, p.sz);
dummy.updateMatrix();
paverMesh.setMatrixAt(i, dummy.matrix);
paverMesh.setColorAt(i, paverColors[i]);
});
paverMesh.receiveShadow = true;
scene.add(paverMesh);
const brickTransforms = [];
const brickColors = [];
function addBrickRun(axis, fixed, start, end, courseCount) {
const nominalLength = 1.20;
const courseHeight = 0.50;
const brickHeight = 0.46;
const brickDepth = 0.58;
for (let course = 0; course < courseCount; course++) {
const offset = course % 2 ? nominalLength * 0.5 : 0;
let cursor = start - offset;
while (cursor < end) {
const a = Math.max(cursor, start);
const b = Math.min(cursor + nominalLength, end);
if (b - a > 0.08) {
const along = (a + b) * 0.5;
const y = course * courseHeight + 0.25;
brickTransforms.push({
axis,
along,
fixed,
y,
length: b - a - 0.035,
height: brickHeight,
depth: brickDepth
});
brickColors.push(
new THREE.Color().setHSL(
0.055 + Math.random() * 0.030,
0.34 + Math.random() * 0.13,
0.40 + Math.random() * 0.105
)
);
}
cursor += nominalLength;
}
}
}
// Full rear wall and cutaway near wall.
addBrickRun("x", -5.70, -9.0, 9.0, 12);
addBrickRun("x", 5.70, -9.0, 9.0, 2);
// Side walls stop at the front/rear wall junctions.
addBrickRun("z", -8.70, -5.40, 5.40, 12);
addBrickRun("z", 8.70, -5.40, 5.40, 12);
const brickMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94
}),
brickTransforms.length
);
brickTransforms.forEach((b, i) => {
dummy.position.set(
b.axis === "x" ? b.along : b.fixed,
b.y,
b.axis === "x" ? b.fixed : b.along
);
dummy.scale.set(
b.axis === "x" ? b.length : b.depth,
b.height,
b.axis === "x" ? b.depth : b.length
);
dummy.updateMatrix();
brickMesh.setMatrixAt(i, dummy.matrix);
brickMesh.setColorAt(i, brickColors[i]);
});
brickMesh.castShadow = true;
brickMesh.receiveShadow = true;
scene.add(brickMesh);
function addBox(size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Mortar cores show through the small gaps between blocks.
addBox(
new THREE.Vector3(18, 5.92, 0.34),
new THREE.Vector3(0, 2.96, -5.70),
mortarMaterial
);
addBox(
new THREE.Vector3(18, 0.92, 0.34),
new THREE.Vector3(0, 0.46, 5.70),
mortarMaterial
);
addBox(
new THREE.Vector3(0.34, 5.92, 10.8),
new THREE.Vector3(-8.70, 2.96, 0),
mortarMaterial
);
addBox(
new THREE.Vector3(0.34, 5.92, 10.8),
new THREE.Vector3(8.70, 2.96, 0),
mortarMaterial
);
// Dark wall caps.
addBox(
new THREE.Vector3(18.1, 0.20, 0.72),
new THREE.Vector3(0, 6.17, -5.70),
supportMaterial
);
addBox(
new THREE.Vector3(18.1, 0.20, 0.72),
new THREE.Vector3(0, 1.07, 5.70),
supportMaterial
);
addBox(
new THREE.Vector3(0.72, 0.20, 10.85),
new THREE.Vector3(-8.70, 6.17, 0),
supportMaterial
);
addBox(
new THREE.Vector3(0.72, 0.20, 10.85),
new THREE.Vector3(8.70, 6.17, 0),
supportMaterial
);
// Fixed dark interior supports.
for (const x of [-8.32, 8.32]) {
for (const z of [-5.30, 5.30]) {
addBox(
new THREE.Vector3(0.34, 6.27, 0.34),
new THREE.Vector3(x, 3.135, z),
supportMaterial
);
}
}
// ------------------------------------------------------------
// Fixed reference-height ruler and dashed rear-wall references.
// ------------------------------------------------------------
const referenceMaterial = new THREE.LineDashedMaterial({
color: 0x8d7d68,
dashSize: 0.22,
gapSize: 0.13,
transparent: true,
opacity: 0.78
});
for (const y of [3, 6]) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-8.15, y, -5.34),
new THREE.Vector3(8.15, y, -5.34)
]);
const line = new THREE.Line(geometry, referenceMaterial);
line.computeLineDistances();
scene.add(line);
}
const rulerPoints = [
new THREE.Vector3(-8.48, 0, 5.13),
new THREE.Vector3(-8.48, 6.30, 5.13)
];
for (const y of [0, 3, 6]) {
rulerPoints.push(
new THREE.Vector3(-8.70, y, 5.13),
new THREE.Vector3(-8.25, y, 5.13)
);
}
const rulerGeometry = new THREE.BufferGeometry().setFromPoints(rulerPoints);
const ruler = new THREE.LineSegments(
rulerGeometry,
new THREE.LineBasicMaterial({ color: 0x5c5147 })
);
scene.add(ruler);
// Dashed ghost outline identifies the removed upper near wall.
const cutawayPoints = [
new THREE.Vector3(-9, 1.1, 5.70),
new THREE.Vector3(-9, 6.3, 5.70),
new THREE.Vector3(9, 6.3, 5.70),
new THREE.Vector3(9, 1.1, 5.70)
];
const cutawayLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(cutawayPoints),
new THREE.LineDashedMaterial({
color: 0x887968,
dashSize: 0.20,
gapSize: 0.14,
transparent: true,
opacity: 0.58
})
);
cutawayLine.computeLineDistances();
scene.add(cutawayLine);
// ------------------------------------------------------------
// HTML labels projected from 3D positions.
// ------------------------------------------------------------
const labelLayer = document.getElementById("labels");
const projectedLabels = [];
function addSceneLabel(text, position, className = "") {
const element = document.createElement("div");
element.className = `scene-label ${className}`.trim();
element.textContent = text;
labelLayer.appendChild(element);
const label = {
element,
position: position.clone()
};
projectedLabels.push(label);
return label;
}
addSceneLabel(
"3.0 m REF",
new THREE.Vector3(-7.65, 3.16, -5.30),
"reference"
);
addSceneLabel(
"6.0 m REF",
new THREE.Vector3(-7.65, 6.16, -5.30),
"reference"
);
addSceneLabel(
"0",
new THREE.Vector3(-8.18, 0.12, 5.13),
"reference"
);
addSceneLabel(
"3 m",
new THREE.Vector3(-8.05, 3.12, 5.13),
"reference"
);
addSceneLabel(
"6 m",
new THREE.Vector3(-8.05, 6.12, 5.13),
"reference"
);
addSceneLabel(
"Near wall cut away",
new THREE.Vector3(7.8, 4.15, 5.70),
"cutaway"
);
// ------------------------------------------------------------
// Roof calculations.
// ------------------------------------------------------------
function ellipseHalfArcLength(a, b, steps = 12000) {
// Integral of sqrt(a² sin²(t) + b² cos²(t)) from 0 to pi.
const n = steps % 2 === 0 ? steps : steps + 1;
const h = Math.PI / n;
let sum = 0;
for (let i = 0; i <= n; i++) {
const t = i * h;
const value = Math.sqrt(
a * a * Math.sin(t) * Math.sin(t) +
b * b * Math.cos(t) * Math.cos(t)
);
sum += value * (i === 0 || i === n ? 1 : (i % 2 ? 4 : 2));
}
return sum * h / 3;
}
const roofData = {
flat: {
title: "Flat roof",
area: ROOF_LENGTH * HALL_DEPTH,
clearance: WALL_TOP,
pointText: "E1–E4: four eave corners at 6.30 m."
},
pitched: {
title: "Pitched roof",
area: 2 * ROOF_LENGTH *
Math.sqrt(ROOF_HALF_DEPTH ** 2 + PITCH_RISE ** 2),
clearance: PITCH_RIDGE_UNDER,
pointText:
"E1–E4: eave corners at 6.30 m; R1–R2: ridge endpoints at 9.90 m. " +
"Ridge nodes are reference geometry, not bearing assumptions."
},
vaulted: {
title: "Vaulted roof",
area: ROOF_LENGTH * ellipseHalfArcLength(
ROOF_HALF_DEPTH, VAULT_OUTER_RISE
),
clearance: VAULT_UNDER_APEX,
pointText:
"S1–S4: spring-line corners at 6.30 m; C1–C2: crown endpoints at 10.30 m. " +
"Crown nodes are reference geometry, not bearing assumptions."
}
};
function roofTopY(type, z) {
const az = Math.abs(z);
if (type === "flat") return FLAT_TOP;
if (type === "pitched") {
return PITCH_RIDGE_TOP - (az / ROOF_HALF_DEPTH) * PITCH_RISE;
}
return FLAT_TOP + VAULT_OUTER_RISE *
Math.sqrt(Math.max(0, 1 - (az / ROOF_HALF_DEPTH) ** 2));
}
function roofUnderY(type, z) {
const az = Math.abs(z);
if (type === "flat") return WALL_TOP;
if (type === "pitched") {
return PITCH_RIDGE_UNDER - (az / ROOF_HALF_DEPTH) * PITCH_RISE;
}
return WALL_TOP + VAULT_INNER_RISE *
Math.sqrt(Math.max(0, 1 - (az / ROOF_HALF_DEPTH) ** 2));
}
function supportPoints(type) {
const corners = [
["E1", new THREE.Vector3(-9, WALL_TOP, -6)],
["E2", new THREE.Vector3( 9, WALL_TOP, -6)],
["E3", new THREE.Vector3(-9, WALL_TOP, 6)],
["E4", new THREE.Vector3( 9, WALL_TOP, 6)]
];
if (type === "pitched") {
corners.push(
["R1", new THREE.Vector3(-9, PITCH_RIDGE_UNDER, 0)],
["R2", new THREE.Vector3( 9, PITCH_RIDGE_UNDER, 0)]
);
}
if (type === "vaulted") {
corners.push(
["C1", new THREE.Vector3(-9, VAULT_UNDER_APEX, 0)],
["C2", new THREE.Vector3( 9, VAULT_UNDER_APEX, 0)]
);
}
return corners;
}
// ------------------------------------------------------------
// Roof geometry creation.
// ------------------------------------------------------------
const roofRoot = new THREE.Group();
const rodRoot = new THREE.Group();
scene.add(roofRoot, rodRoot);
let currentRoof = "flat";
let dynamicPointLabels = [];
let clearanceLabels = [];
function clearGroup(group) {
group.traverse(object => {
if (object.geometry) object.geometry.dispose();
});
group.clear();
}
function createRoofSurface(type, top = true) {
const nx = 1;
const nz = type === "vaulted" ? 72 : 24;
const positions = [];
const indices = [];
for (let ix = 0; ix <= nx; ix++) {
const x = -ROOF_LENGTH / 2 + ROOF_LENGTH * ix / nx;
for (let iz = 0; iz <= nz; iz++) {
const z = -ROOF_HALF_DEPTH + HALL_DEPTH * iz / nz;
const y = roofTopY(type, z) + (top ? 0 : -ROOF_THICKNESS);
positions.push(x, y, z);
}
}
const stride = nz + 1;
for (let ix = 0; ix < nx; ix++) {
for (let iz = 0; iz < nz; iz++) {
const a = ix * stride + iz;
const b = (ix + 1) * stride + iz;
const c = a + 1;
const d = b + 1;
if (top) {
indices.push(a, c, b, b, c, d);
} else {
indices.push(a, b, c, b, d, c);
}
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createRoofEndCap(type, x) {
const segments = type === "vaulted" ? 72 : 24;
const positions = [];
const indices = [];
for (let i = 0; i <= segments; i++) {
const z = -ROOF_HALF_DEPTH + HALL_DEPTH * i / segments;
const y = roofTopY(type, z);
positions.push(x, y, z, x, y - ROOF_THICKNESS, z);
}
for (let i = 0; i < segments; i++) {
const a = i * 2;
indices.push(a, a + 1, a + 2, a + 1, a + 3, a + 2);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createRoofEaveCap(type, z) {
const y = roofTopY(type, z);
const positions = [
-ROOF_LENGTH / 2, y, z,
-ROOF_LENGTH / 2, y - ROOF_THICKNESS, z,
ROOF_LENGTH / 2, y, z,
ROOF_LENGTH / 2, y - ROOF_THICKNESS, z
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex([0, 1, 2, 2, 1, 3]);
geometry.computeVertexNormals();
return geometry;
}
function createRoofSeams(type) {
const positions = [];
const yOffset = 0.018;
// Lines running along the hall.
for (let x = -6; x <= 6; x += 3) {
const steps = type === "vaulted" ? 64 : 20;
for (let i = 0; i < steps; i++) {
const z0 = -ROOF_HALF_DEPTH + HALL_DEPTH * i / steps;
const z1 = -ROOF_HALF_DEPTH + HALL_DEPTH * (i + 1) / steps;
positions.push(
x, roofTopY(type, z0) + yOffset, z0,
x, roofTopY(type, z1) + yOffset, z1
);
}
}
// Lines running across the hall.
for (let z = -5; z <= 5; z += 1.25) {
positions.push(
-ROOF_LENGTH / 2, roofTopY(type, z) + yOffset, z,
ROOF_LENGTH / 2, roofTopY(type, z) + yOffset, z
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
return new THREE.LineSegments(
geometry,
new THREE.LineBasicMaterial({
color: 0xaab5b4,
transparent: true,
opacity: 0.40
})
);
}
function addControlPoint(name, position) {
const marker = new THREE.Mesh(
new THREE.SphereGeometry(0.14, 16, 12),
controlMaterial
);
marker.position.copy(position);
marker.renderOrder = 5;
roofRoot.add(marker);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.23, 0.025, 8, 28),
controlMaterial
);
ring.position.copy(position);
ring.rotation.x = Math.PI / 2;
roofRoot.add(ring);
const label = addSceneLabel(
name,
position.clone().add(new THREE.Vector3(0, 0.34, 0)),
"control"
);
dynamicPointLabels.push(label);
}
function createClearanceRod(x, z, type) {
const height = roofUnderY(type, z);
const rod = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.035, height, 10),
rodMaterial
);
rod.position.set(x, height / 2, z);
rod.castShadow = true;
rodRoot.add(rod);
const base = new THREE.Mesh(
new THREE.TorusGeometry(0.17, 0.025, 8, 24),
rodMaterial
);
base.position.set(x, 0.055, z);
base.rotation.x = Math.PI / 2;
rodRoot.add(base);
const top = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 12, 8),
rodMaterial
);
top.position.set(x, height, z);
rodRoot.add(top);
const label = addSceneLabel(
`${height.toFixed(1)} m`,
new THREE.Vector3(x, height + 0.32, z),
"clearance"
);
clearanceLabels.push(label);
}
const rodPositions = [
new THREE.Vector2(-5.25, -2.10),
new THREE.Vector2( 0.20, 1.80),
new THREE.Vector2( 5.20, -3.20)
];
function switchRoof(type) {
currentRoof = type;
clearGroup(roofRoot);
clearGroup(rodRoot);
for (const label of [...dynamicPointLabels, ...clearanceLabels]) {
label.element.remove();
const index = projectedLabels.indexOf(label);
if (index !== -1) projectedLabels.splice(index, 1);
}
dynamicPointLabels = [];
clearanceLabels = [];
// Only the roof geometry and its schematic point set are replaced.
const topMesh = new THREE.Mesh(
createRoofSurface(type, true),
roofTopMaterial
);
topMesh.castShadow = true;
topMesh.receiveShadow = true;
roofRoot.add(topMesh);
const underMesh = new THREE.Mesh(
createRoofSurface(type, false),
roofUnderMaterial
);
underMesh.castShadow = true;
underMesh.receiveShadow = true;
roofRoot.add(underMesh);
for (const x of [-ROOF_LENGTH / 2, ROOF_LENGTH / 2]) {
const cap = new THREE.Mesh(
createRoofEndCap(type, x),
fasciaMaterial
);
cap.castShadow = true;
roofRoot.add(cap);
}
for (const z of [-ROOF_HALF_DEPTH, ROOF_HALF_DEPTH]) {
const cap = new THREE.Mesh(
createRoofEaveCap(type, z),
fasciaMaterial
);
cap.castShadow = true;
roofRoot.add(cap);
}
roofRoot.add(createRoofSeams(type));
if (type === "pitched") {
const ridgeCap = new THREE.Mesh(
new THREE.BoxGeometry(ROOF_LENGTH + 0.08, 0.13, 0.16),
fasciaMaterial
);
ridgeCap.position.set(0, PITCH_RIDGE_TOP + 0.025, 0);
ridgeCap.castShadow = true;
roofRoot.add(ridgeCap);
}
for (const [name, position] of supportPoints(type)) {
addControlPoint(name, position);
}
for (const position of rodPositions) {
createClearanceRod(position.x, position.y, type);
}
// Interface.
document.querySelectorAll("[data-roof]").forEach(button => {
button.setAttribute(
"aria-pressed",
String(button.dataset.roof === type)
);
});
document.getElementById("current-title").textContent = roofData[type].title;
document.getElementById("area-value").textContent =
`${roofData[type].area.toFixed(1)} m²`;
document.getElementById("clearance-value").textContent =
`${roofData[type].clearance.toFixed(1)} m`;
document.getElementById("points-text").textContent =
roofData[type].pointText;
document.querySelectorAll("#comparison-body tr").forEach(row => {
row.classList.toggle("current", row.dataset.roof === type);
});
}
// ------------------------------------------------------------
// Static comparison table.
// ------------------------------------------------------------
const comparisonBody = document.getElementById("comparison-body");
for (const [type, data] of Object.entries(roofData)) {
const row = document.createElement("tr");
row.dataset.roof = type;
row.innerHTML = `
<td>${data.title.replace(" roof", "")}</td>
<td>${data.area.toFixed(1)}</td>
<td>${data.clearance.toFixed(1)}</td>
<td>${supportPoints(type).length}</td>
`;
comparisonBody.appendChild(row);
}
document.querySelectorAll("[data-roof]").forEach(button => {
button.addEventListener("click", () => switchRoof(button.dataset.roof));
});
document.getElementById("reset-camera")
.addEventListener("click", resetCamera);
document.getElementById("cutaway-view")
.addEventListener("click", setCutawayCamera);
window.addEventListener("keydown", event => {
if (event.key === "1") switchRoof("flat");
if (event.key === "2") switchRoof("pitched");
if (event.key === "3") switchRoof("vaulted");
});
// ------------------------------------------------------------
// Custom pointer camera controls.
// ------------------------------------------------------------
let pointerDown = false;
let pointerId = null;
let lastX = 0;
let lastY = 0;
let panning = false;
renderer.domElement.addEventListener("pointerdown", event => {
pointerDown = true;
pointerId = event.pointerId;
lastX = event.clientX;
lastY = event.clientY;
panning = event.button === 2 || event.shiftKey;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointerDown || event.pointerId !== pointerId) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (panning) {
const yaw = cameraState.yaw;
const scale = cameraState.distance * 0.0017;
cameraState.target.x +=
(-dx * Math.cos(yaw) + dy * Math.sin(yaw)) * scale;
cameraState.target.z +=
( dx * Math.sin(yaw) + dy * Math.cos(yaw)) * scale;
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x, -8, 8
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z, -7, 7
);
} else {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
-0.05,
0.95
);
}
updateCamera();
});
function endPointer(event) {
if (event.pointerId !== pointerId) return;
pointerDown = false;
panning = false;
renderer.domElement.classList.remove("dragging");
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance * Math.exp(event.deltaY * 0.001),
14,
48
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
// ------------------------------------------------------------
// Resize and render loop.
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
function updateProjectedLabels() {
const width = renderer.domElement.clientWidth;
const height = renderer.domElement.clientHeight;
for (const label of projectedLabels) {
const point = label.position.clone().project(camera);
const visible = point.z > -1 && point.z < 1;
label.element.style.display = visible ? "block" : "none";
if (visible) {
label.element.style.left = `${(point.x * 0.5 + 0.5) * width}px`;
label.element.style.top = `${(-point.y * 0.5 + 0.5) * height}px`;
}
}
}
switchRoof("flat");
resetCamera();
renderer.setAnimationLoop(() => {
updateProjectedLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```