<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>3D Pergola - Three.js</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #87ceeb; /* sky blue */
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: #222;
font-family: sans-serif;
background: rgba(255 255 255 / 0.8);
padding: 6px 12px;
border-radius: 4px;
user-select: none;
font-size: 14px;
max-width: 300px;
}
</style>
</head>
<body>
<div id="info">3D Pergola - Use mouse to orbit, zoom, pan</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script>
// === Scene Setup ===
const scene = new THREE.Scene();
// Camera
const camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
// Position camera for good overview
camera.position.set(15, 12, 15);
camera.lookAt(0, 4, 0);
// Renderer
const renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x87ceeb); // sky blue
document.body.appendChild(renderer.domElement);
// Controls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(4.5, 4, 3);
controls.update();
// Resize handler
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// === Ground: grassy plain ===
const groundGeo = new THREE.PlaneGeometry(50, 50);
const groundMat = new THREE.MeshLambertMaterial({color: 0x3a6a1a});
const ground = new THREE.Mesh(groundGeo, groundMat);
ground.rotation.x = -Math.PI/2;
ground.position.y = 0;
scene.add(ground);
// === Lighting ===
const ambientLight = new THREE.AmbientLight(0xffffff, 0.4);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 20, 10);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
directionalLight.shadow.camera.left = -20;
directionalLight.shadow.camera.right = 20;
directionalLight.shadow.camera.top = 20;
directionalLight.shadow.camera.bottom = -20;
directionalLight.shadow.camera.near = 1;
directionalLight.shadow.camera.far = 50;
scene.add(directionalLight);
// === Wood Material ===
// Simple wood texture-like color with some roughness
const woodColor = 0x8b5a2b;
const woodMaterial = new THREE.MeshStandardMaterial({
color: woodColor,
roughness: 0.7,
metalness: 0.1,
});
// === Dimensions (feet) ===
// Posts (4x4 nominal): width/depth = 3.5/12, height = 8.0
const postWidth = 3.5 / 12; // ft
const postDepth = 3.5 / 12; // ft
const postHeight = 8.0; // ft
// Main Beam (2x8 nominal): thickness (Z) = 1.5/12, height (Y) = 7.25/12, length = 12.0 (9 + 2*1.5)
const mainBeamThickness = 1.5 / 12; // Z thickness
const mainBeamHeight = 7.25 / 12; // Y height
const mainBeamLength = 12.0; // ft (9 + 2*1.5)
// Rafter (2x4 nominal): thickness (X) = 1.5/12, height (Y) = 3.5/12, length = 9.0 (6 + 2*1.5)
const rafterThickness = 1.5 / 12; // X thickness
const rafterHeight = 3.5 / 12; // Y height
const rafterLength = 9.0; // ft (6 + 2*1.5)
// Purlin (2x2 nominal): width/height/depth = 1.5/12
const purlinWidth = 1.5 / 12; // X width
const purlinHeight = 1.5 / 12; // Y height
const purlinDepth = 1.5 / 12; // Z depth
const purlinLength = mainBeamLength; // 12.0 ft
// Pergola footprint: 9ft (X) by 6ft (Z)
const pergolaSpanX = 9.0;
const pergolaSpanZ = 6.0;
// === Helper function: create tapered relief geometry for beams and rafters ===
// Creates a box with a tapered relief on bottom face over the overhang length (1.5 ft)
// The taper reduces height to 35% of original at the end of overhang, top face flat.
// Parameters:
// length: total length of beam/rafter (including overhangs)
// height: full height of beam/rafter
// thickness: thickness (depth for beam, width for rafter)
// overhang: length of overhang on each end
// axis: 'x' or 'z' - direction along which length extends
// Returns: THREE.BufferGeometry
function createTaperedBeamGeometry(length, height, thickness, overhang, axis) {
// We'll build a custom geometry with 8 vertices:
// The top face is flat at full height.
// The bottom face is full height in center, linearly tapers down to 35% height at overhang ends.
const hFull = height;
const hTaper = height * 0.35;
const o = overhang;
// Vertices order:
// 0: front-left-top
// 1: front-right-top
// 2: back-right-top
// 3: back-left-top
// 4: front-left-bottom
// 5: front-right-bottom
// 6: back-right-bottom
// 7: back-left-bottom
// Depending on axis, length is along X or Z
// thickness is the other horizontal dimension
// We'll define the box centered at origin along length axis, thickness centered at 0,
// height from 0 to height along Y.
// For taper, bottom vertices at ends have height hTaper, bottom vertices in center have height hFull.
// Define positions array (8 vertices * 3 coords)
const positions = [];
// Helper to push vertex
function v(x,y,z) { positions.push(x,y,z); }
// Half dimensions
const halfLength = length / 2;
const halfThickness = thickness / 2;
// Top face (flat at full height)
// front-left-top
if(axis === 'x') {
v(-halfLength, hFull, -halfThickness); // 0
v(halfLength, hFull, -halfThickness); // 1
v(halfLength, hFull, halfThickness); // 2
v(-halfLength, hFull, halfThickness); // 3
} else { // axis === 'z'
v(-halfThickness, hFull, -halfLength); // 0
v(halfThickness, hFull, -halfLength); // 1
v(halfThickness, hFull, halfLength); // 2
v(-halfThickness, hFull, halfLength); // 3
}
// Bottom face (tapered)
// At ends (overhang zones): height = hTaper
// At center (between -halfLength+o and halfLength-o): height = hFull
// We'll create bottom vertices with Y = hFull in center, linearly interpolated in overhang zones
// For bottom vertices, front-left-bottom (4), front-right-bottom (5), back-right-bottom (6), back-left-bottom (7)
// We'll define a function to get bottom height at a given length position
function bottomHeight(pos) {
// pos from -halfLength to halfLength
if(pos < -halfLength + o) {
// left overhang zone
const t = (pos + halfLength) / o; // 0 at -halfLength, 1 at -halfLength+o
return hTaper + t * (hFull - hTaper);
} else if(pos > halfLength - o) {
// right overhang zone
const t = (halfLength - pos) / o; // 0 at halfLength, 1 at halfLength-o
return hTaper + t * (hFull - hTaper);
} else {
return hFull;
}
}
if(axis === 'x') {
// bottom vertices order same as top
// front-left-bottom (x=-halfLength, z=-halfThickness)
v(-halfLength, bottomHeight(-halfLength), -halfThickness); //4
v(halfLength, bottomHeight(halfLength), -halfThickness); //5
v(halfLength, bottomHeight(halfLength), halfThickness); //6
v(-halfLength, bottomHeight(-halfLength), halfThickness); //7
} else {
// axis z
// front-left-bottom (x=-halfThickness, z=-halfLength)
v(-halfThickness, bottomHeight(-halfLength), -halfLength); //4
v(halfThickness, bottomHeight(-halfLength), -halfLength); //5
v(halfThickness, bottomHeight(halfLength), halfLength); //6
v(-halfThickness, bottomHeight(halfLength), halfLength); //7
}
// Indices for box faces (two triangles per face)
const indices = [
// top
0,1,2, 0,2,3,
// bottom
4,6,5, 4,7,6,
// front
0,4,5, 0,5,1,
// back
3,2,6, 3,6,7,
// left
0,3,7, 0,7,4,
// right
1,5,6, 1,6,2
];
const geometry = new THREE.BufferGeometry();
geometry.setIndex(indices);
geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
geometry.computeVertexNormals();
return geometry;
}
// === Create Posts ===
// 4 posts at corners of 9ft x 6ft rectangle
// Posts centered on their footprint, so position is center of post base
// Posts sit on ground (y=0), height = 8ft
// Post positions (X,Z):
// Posts form rectangle 9ft x 6ft
// We'll place posts at (0,0), (9,0), (0,6), (9,6)
// But since Y is up, and ground is at y=0, posts base at y=0
// To center pergola near origin, shift all positions by -4.5 in X and -3 in Z
// So pergola center at (0,0) in XZ plane
const postPositions = [
{x: 0, z: 0},
{x: pergolaSpanX, z: 0},
{x: 0, z: pergolaSpanZ},
{x: pergolaSpanX, z: pergolaSpanZ},
].map(p => ({x: p.x - pergolaSpanX/2, z: p.z - pergolaSpanZ/2}));
const postGeometry = new THREE.BoxGeometry(postWidth, postHeight, postDepth);
postPositions.forEach(pos => {
const post = new THREE.Mesh(postGeometry, woodMaterial);
post.position.set(pos.x, postHeight/2, pos.z);
post.castShadow = true;
post.receiveShadow = true;
scene.add(post);
});
// === Create Main Beams (2 count) ===
// Length: 12.0 ft (9 + 2*1.5)
// Height: 7.25/12 ft
// Thickness: 1.5/12 ft (Z-axis)
// Positioned on top of posts at height = postHeight
// Two beams run along X axis, at Z = +/- pergolaSpanZ/2 (top of posts)
// Beam center Y = postHeight + mainBeamHeight/2 (since beam height is along Y)
// Beam center X = 0 (centered on pergola X)
// Create geometry with tapered relief on bottom face
const mainBeamGeo = createTaperedBeamGeometry(mainBeamLength, mainBeamHeight, mainBeamThickness, 1.5, 'x');
// Positions of main beams along Z:
// They sit on top of posts, so at Z = -pergolaSpanZ/2 and +pergolaSpanZ/2
// But posts have depth, so beams should be centered on posts top face
// Posts depth = 3.5/12 = 0.2917 ft
// So beam center Z = post Z position (±3) (since pergolaSpanZ=6) (posts centered at ±3)
// We'll place beams exactly at post Z positions
const mainBeamZPositions = [-pergolaSpanZ/2, pergolaSpanZ/2];
mainBeamZPositions.forEach(zPos => {
const beam = new THREE.Mesh(mainBeamGeo, woodMaterial);
beam.position.set(0, postHeight + mainBeamHeight/2, zPos);
beam.castShadow = true;
beam.receiveShadow = true;
scene.add(beam);
});
// === Create Rafters (7 count) ===
// Length: 9.0 ft (6 + 2*1.5)
// Height: 3.5/12 ft
// Thickness: 1.5/12 ft (X-axis)
// Rafters run along Z axis, spaced evenly along X axis over 12.0 ft (main beam length)
// Rafters sit on top of main beams at height = postHeight + mainBeamHeight
// Rafters center Y = postHeight + mainBeamHeight + rafterHeight/2
// Rafters center Z = 0 (centered between main beams)
// Rafters spaced evenly along X from -6 to +6 (12 ft total length)
// Create geometry with tapered relief on bottom face
const rafterGeo = createTaperedBeamGeometry(rafterLength, rafterHeight, rafterThickness, 1.5, 'z');
// Rafters spacing along X:
// 7 rafters evenly spaced along 12 ft length (main beam length)
// Positions from -6 to +6 (centered at 0)
// Even spacing: distance between rafters = 12 / (7-1) = 2 ft
const rafterCount = 7;
const rafterSpacing = mainBeamLength / (rafterCount - 1);
for(let i=0; i<rafterCount; i++) {
const xPos = -mainBeamLength/2 + i * rafterSpacing;
const rafter = new THREE.Mesh(rafterGeo, woodMaterial);
rafter.position.set(xPos, postHeight + mainBeamHeight + rafterHeight/2, 0);
rafter.castShadow = true;
rafter.receiveShadow = true;
scene.add(rafter);
}
// === Create Purlins (7 count) ===
// Length: 12.0 ft (same as main beam length)
// Height: 1.5/12 ft
// Depth: 1.5/12 ft
// Purlins run along X axis, placed on top of rafters
// Purlins spaced evenly along Z axis over 9.0 ft (rafter length)
// Purlins center Y = postHeight + mainBeamHeight + rafterHeight + purlinHeight/2
// Purlins center X = 0 (centered on pergola X)
// Purlins spaced evenly along Z from -4.5 to +4.5 (9 ft total length)
const purlinCount = 7;
const purlinSpacing = rafterLength / (purlinCount - 1);
const purlinGeo = new THREE.BoxGeometry(purlinLength, purlinHeight, purlinDepth);
for(let i=0; i<purlinCount; i++) {
const zPos = -rafterLength/2 + i * purlinSpacing;
const purlin = new THREE.Mesh(purlinGeo, woodMaterial);
purlin.position.set(0, postHeight + mainBeamHeight + rafterHeight + purlinHeight/2, zPos);
purlin.castShadow = true;
purlin.receiveShadow = true;
scene.add(purlin);
}
// === Shadows and helpers ===
renderer.shadowMap.enabled = true;
ground.receiveShadow = true;
// === Animation loop ===
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>