<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>3D Pergola Scene</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
<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>
</head>
<body>
<script>
// === Scene Setup ===
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xa0d0a0);
const camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set(15, 10, 15);
const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(4.5, 6, 3);
controls.update();
// === Lighting ===
scene.add(new THREE.AmbientLight(0x888888));
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
dirLight.position.set(10, 20, 10);
scene.add(dirLight);
// === Ground ===
const groundGeo = new THREE.PlaneGeometry(100, 100);
const groundMat = new THREE.MeshLambertMaterial({color: 0x55aa55});
const ground = new THREE.Mesh(groundGeo, groundMat);
ground.rotation.x = -Math.PI / 2;
ground.position.y = 0;
scene.add(ground);
// === Materials ===
const woodMaterial = new THREE.MeshLambertMaterial({color: 0x8B4513});
// === Utility: Create tapered beam/rafter with undercut relief ===
function createTaperedBeam(length, height, thickness, overhang, reliefPercent) {
const left = -length / 2;
const right = length / 2;
const h = height;
const lowH = h * reliefPercent;
const geom = new THREE.BufferGeometry();
const positions = [];
const faces = [];
const buildBox = (x0, x1, z0, z1, yTopFn, yBottomFn) => {
const offset = positions.length / 3;
// 8 vertices
const v = [
[x0, yBottomFn(x0), z0], [x1, yBottomFn(x1), z0],
[x1, yBottomFn(x1), z1], [x0, yBottomFn(x0), z1],
[x0, yTopFn(x0), z0], [x1, yTopFn(x1), z0],
[x1, yTopFn(x1), z1], [x0, yTopFn(x0), z1]
];
v.forEach(p => positions.push(...p));
const f = [
[0,1,2], [0,2,3],
[4,6,5], [4,7,6],
[4,5,1], [4,1,0],
[5,6,2], [5,2,1],
[6,7,3], [6,3,2],
[7,4,0], [7,0,3]
];
f.forEach(p => faces.push(...p.map(i => i + offset)));
};
const thicknessHalf = thickness / 2;
const yTopFn = x => h;
const yBottomFn = x => {
if (x <= left + overhang) {
let t = (x - left) / overhang;
return lowH + (h - lowH) * t;
} else if (x >= right - overhang) {
let t = (right - x) / overhang;
return lowH + (h - lowH) * t;
}
return h;
};
buildBox(left, right, -thicknessHalf, thicknessHalf, yTopFn, yBottomFn);
geom.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
geom.setIndex(faces);
geom.computeVertexNormals();
return new THREE.Mesh(geom, woodMaterial);
}
// === Dimensions (in feet) ===
const postWidth = 3.5/12;
const postHeight = 8.0;
const beamHeight = 7.25/12;
const beamThickness = 1.5/12;
const beamOverhang = 1.5;
const rafterHeight = 3.5/12;
const rafterThickness = 1.5/12;
const rafterOverhang = 1.5;
const purlinSize = 1.5 / 12;
// === Posts: 4 ===
const postGeom = new THREE.BoxGeometry(postWidth, postHeight, postWidth);
const postOffsetX = 9.0 / 2;
const postOffsetZ = 6.0 / 2;
const postY = postHeight / 2;
for (let dx of [-postOffsetX, postOffsetX]) {
for (let dz of [-postOffsetZ, postOffsetZ]) {
const post = new THREE.Mesh(postGeom, woodMaterial);
post.position.set(dx, postY, dz);
scene.add(post);
}
}
// === Main Beams: 2 ===
const beamLength = 9.0 + 2 * beamOverhang; // 12.0
for (let dz of [-postOffsetZ - postWidth/2 + beamThickness/2, postOffsetZ + postWidth/2 - beamThickness/2]) {
const beam = createTaperedBeam(beamLength, beamHeight, beamThickness, beamOverhang, 0.35);
beam.rotation.y = 0;
beam.position.set(0, postHeight, dz);
scene.add(beam);
}
// === Rafters: 7 ===
const rafterSpacing = (beamLength) / 6; // divide into 6 spacings between 7 rafters
const rafterLength = 6.0 + 2 * rafterOverhang; // 9.0
const rafterY = postHeight + beamHeight;
for (let i = 0; i < 7; i++) {
const x = -beamLength/2 + i * rafterSpacing;
const rafter = createTaperedBeam(rafterLength, rafterHeight, rafterThickness, rafterOverhang, 0.35);
rafter.rotation.y = Math.PI / 2;
rafter.position.set(x, rafterY, 0);
scene.add(rafter);
}
// === Purlins: 7 ===
const purlinSpacing = 9.0 / 6; // divide rafter span into 6 spacings between 7
const purlinY = postHeight + beamHeight + rafterHeight;
for (let i = 0; i < 7; i++) {
const z = -4.5 + i * purlinSpacing;
const purlinGeom = new THREE.BoxGeometry(beamLength, purlinSize, purlinSize);
const purlin = new THREE.Mesh(purlinGeom, woodMaterial);
purlin.position.set(0, purlinY, z);
scene.add(purlin);
}
// === Handle Window Resizing ===
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// === Render Loop ===
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>