<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pergola Visualization</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<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();
scene.background = new THREE.Color(0x87CEEB); // Sky blue
// Camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(15, 10, 15); // Position for a good overview
camera.lookAt(0, 4, 0);
// Renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 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);
scene.add(directionalLight);
// Ground
const groundGeometry = new THREE.PlaneGeometry(50, 50);
const groundMaterial = new THREE.MeshLambertMaterial({ color: 0x32CD32 }); // Grassy green
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
scene.add(ground);
// Wood Material
const woodMaterial = new THREE.MeshLambertMaterial({ color: 0x964B00 }); // Wood brown
// Pergola Dimensions (in feet)
const postWidth = 3.5 / 12;
const postHeight = 8.0;
const mainBeamThickness = 1.5 / 12;
const mainBeamHeight = 7.25 / 12;
const mainBeamOverhang = 1.5;
const rafterThickness = 1.5 / 12;
const rafterHeight = 3.5 / 12;
const rafterOverhang = 1.5;
const purlinSize = 1.5 / 12;
// Function to create a tapered beam
function createTaperedBeam(length, height, thickness, overhang, taperHeight) {
const geometry = new THREE.BufferGeometry();
const vertices = new Float32Array([
-length / 2, height / 2, thickness / 2, // 0
length / 2, height / 2, thickness / 2, // 1
length / 2, height / 2, -thickness / 2, // 2
-length / 2, height / 2, -thickness / 2, // 3
-length / 2, taperHeight / 2, thickness / 2, // 4
length / 2, taperHeight / 2, thickness / 2, // 5
length / 2, taperHeight / 2, -thickness / 2, // 6
-length / 2, taperHeight / 2, -thickness / 2 // 7
]);
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
const indices = new Uint16Array([
0, 1, 2, 0, 2, 3, // Top face
4, 5, 6, 4, 6, 7, // Bottom face
0, 4, 5, 0, 5, 1, // Front face
2, 6, 7, 2, 7, 3, // Back face
1, 5, 6, 1, 6, 2, // Right face
0, 4, 7, 0, 7, 3 // Left face
]);
geometry.setIndex(new THREE.BufferAttribute(indices, 1));
// Adjust positions for overhang taper
const positionAttribute = geometry.getAttribute('position');
for (let i = 0; i < 8; i++) {
const x = positionAttribute.getX(i);
if (x < -length / 2 + overhang) {
const taperFactor = (x + length / 2) / overhang;
positionAttribute.setY(i * 3 + 1, THREE.MathUtils.lerp(height / 2, taperHeight / 2, taperFactor));
} else if (x > length / 2 - overhang) {
const taperFactor = (length / 2 - x) / overhang;
positionAttribute.setY(i * 3 + 1, THREE.MathUtils.lerp(height / 2, taperHeight / 2, taperFactor));
}
}
positionAttribute.needsUpdate = true;
return new THREE.Mesh(geometry, woodMaterial);
}
// Posts
const postPositions = [
[-4.5, 0, -3], // Bottom left
[4.5, 0, -3], // Bottom right
[-4.5, 0, 3], // Top left
[4.5, 0, 3] // Top right
];
postPositions.forEach(([x, _, z]) => {
const postGeometry = new THREE.BoxGeometry(postWidth, postHeight, postWidth);
const post = new THREE.Mesh(postGeometry, woodMaterial);
post.position.set(x, postHeight / 2, z);
scene.add(post);
});
// Main Beams
const mainBeamLength = 9.0 + 2 * mainBeamOverhang;
const mainBeamTaperHeight = mainBeamHeight * 0.35;
const mainBeams = [
createTaperedBeam(mainBeamLength, mainBeamHeight, mainBeamThickness, mainBeamOverhang, mainBeamTaperHeight),
createTaperedBeam(mainBeamLength, mainBeamHeight, mainBeamThickness, mainBeamOverhang, mainBeamTaperHeight)
];
mainBeams[0].position.set(0, postHeight + mainBeamHeight / 2, -3);
mainBeams[1].position.set(0, postHeight + mainBeamHeight / 2, 3);
scene.add(...mainBeams);
// Rafters
const rafterLength = 6.0 + 2 * rafterOverhang;
const rafterTaperHeight = rafterHeight * 0.35;
const rafterSpacing = mainBeamLength / 8; // 7 rafters, 8 gaps
for (let i = 0; i < 7; i++) {
const rafter = createTaperedBeam(rafterLength, rafterHeight, rafterThickness, rafterOverhang, rafterTaperHeight);
rafter.position.set(-mainBeamLength / 2 + rafterSpacing * (i + 1), postHeight + mainBeamHeight + rafterHeight / 2, 0);
rafter.rotation.y = Math.PI / 2;
scene.add(rafter);
}
// Purlins
const purlinSpacing = rafterLength / 8; // 7 purlins, 8 gaps
for (let i = 0; i < 7; i++) {
const purlinGeometry = new THREE.BoxGeometry(mainBeamLength, purlinSize, purlinSize);
const purlin = new THREE.Mesh(purlinGeometry, woodMaterial);
purlin.position.set(0, postHeight + mainBeamHeight + rafterHeight + purlinSize / 2, -rafterLength / 2 + purlinSpacing * (i + 1));
scene.add(purlin);
}
// Orbit Controls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(0, 4, 0);
controls.update();
// Animation Loop
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
// Handle Window Resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>