Here's a complete HTML file with a 3D pergola scene using three.js:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Pergola Scene</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background: #87CEEB;
font-family: Arial, sans-serif;
}
canvas {
display: block;
}
.info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 5px;
font-size: 14px;
}
</style>
</head>
<body>
<div class="info">
3D Pergola Scene<br>
Use mouse to orbit, zoom, and 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();
scene.background = new THREE.Color(0x87CEEB); // Sky blue
// Camera setup
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(15, 10, 15);
// Renderer setup
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
// Controls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040, 0.4);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 20, 5);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
directionalLight.shadow.camera.near = 0.5;
directionalLight.shadow.camera.far = 50;
directionalLight.shadow.camera.left = -20;
directionalLight.shadow.camera.right = 20;
directionalLight.shadow.camera.top = 20;
directionalLight.shadow.camera.bottom = -20;
scene.add(directionalLight);
// Materials
const woodMaterial = new THREE.MeshLambertMaterial({
color: 0x8B4513,
transparent: false
});
const grassMaterial = new THREE.MeshLambertMaterial({ color: 0x228B22 });
// Ground
const groundGeometry = new THREE.PlaneGeometry(50, 50);
const ground = new THREE.Mesh(groundGeometry, grassMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
// Pergola construction
const pergola = new THREE.Group();
// Post dimensions (nominal 4x4)
const postWidth = 3.5 / 12;
const postDepth = 3.5 / 12;
const postHeight = 8.0;
// Main beam dimensions (nominal 2x8)
const beamThickness = 1.5 / 12;
const beamHeight = 7.25 / 12;
const beamOverhang = 1.5;
// Rafter dimensions (nominal 2x4)
const rafterThickness = 1.5 / 12;
const rafterHeight = 3.5 / 12;
const rafterOverhang = 1.5;
// Purlin dimensions (nominal 2x2)
const purlinSize = 1.5 / 12;
// 1. Posts (4 count) - 9ft x 6ft rectangle
const postPositions = [
[-4.5, 0, -3],
[4.5, 0, -3],
[-4.5, 0, 3],
[4.5, 0, 3]
];
const postGeometry = new THREE.BoxGeometry(postWidth, postHeight, postDepth);
postPositions.forEach(pos => {
const post = new THREE.Mesh(postGeometry, woodMaterial);
post.position.set(pos[0], postHeight / 2, pos[2]);
post.castShadow = true;
pergola.add(post);
});
// 2. Main Beams (2 count) with tapered overhangs
function createTaperedBeam(length, height, thickness, overhang, isMainBeam = true) {
const segments = 20;
const geometry = new THREE.BufferGeometry();
const vertices = [];
const indices = [];
for (let i = 0; i <= segments; i++) {
const x = (i / segments - 0.5) * length;
let currentHeight = height;
// Apply taper to overhangs
if (Math.abs(x) > length / 2 - overhang) {
const distanceIntoOverhang = Math.abs(x) - (length / 2 - overhang);
const tapeFactor = 1 - (distanceIntoOverhang / overhang) * 0.65; // Reduce to 35%
currentHeight = height * tapeFactor;
}
// Bottom vertices (tapered)
vertices.push(x, -currentHeight / 2, -thickness / 2);
vertices.push(x, -currentHeight / 2, thickness / 2);
// Top vertices (flat)
vertices.push(x, height / 2, -thickness / 2);
vertices.push(x, height / 2, thickness / 2);
}
// Create faces
for (let i = 0; i < segments; i++) {
const base = i * 4;
const next = (i + 1) * 4;
// Bottom face
indices.push(base, base + 1, next + 1);
indices.push(base, next + 1, next);
// Top face
indices.push(base + 2, next + 3, base + 3);
indices.push(base + 2, next + 2, next + 3);
// Side faces
indices.push(base, next, next + 2);
indices.push(base, next + 2, base + 2);
indices.push(base + 1, base + 3, next + 3);
indices.push(base + 1, next + 3, next + 1);
}
geometry.setIndex(indices);
geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
geometry.computeVertexNormals();
return geometry;
}
const beamLength = 12.0;
const beamGeometry = createTaperedBeam(beamLength, beamHeight, beamThickness, beamOverhang);
// Two main beams along X-axis
const beam1 = new THREE.Mesh(beamGeometry, woodMaterial);
beam1.position.set(0, postHeight + beamHeight / 2, -3);
beam1.castShadow = true;
pergola.add(beam1);
const beam2 = new THREE.Mesh(beamGeometry, woodMaterial);
beam2.position.set(0, postHeight + beamHeight / 2, 3);
beam2.castShadow = true;
pergola.add(beam2);
// 3. Rafters (7 count) with tapered overhangs
const rafterLength = 9.0;
const rafterGeometry = createTaperedBeam(rafterLength, rafterHeight, rafterThickness, rafterOverhang);
for (let i = 0; i < 7; i++) {
const x = -6 + (i * 2); // Distribute evenly along 12ft span
const rafter = new THREE.Mesh(rafterGeometry, woodMaterial);
rafter.position.set(x, postHeight + beamHeight + rafterHeight / 2, 0);
rafter.rotation.y = Math.PI / 2; // Rotate to run along Z-axis
rafter.castShadow = true;
pergola.add(rafter);
}
// 4. Purlins (7 count)
const purlinGeometry = new THREE.BoxGeometry(beamLength, purlinSize, purlinSize);
for (let i = 0; i < 7; i++) {
const z = -4.5 + (i * 1.5); // Distribute evenly along 9ft span
const purlin = new THREE.Mesh(purlinGeometry, woodMaterial);
purlin.position.set(0, postHeight + beamHeight + rafterHeight + purlinSize / 2, z);
purlin.castShadow = true;
pergola.add(purlin);
}
scene.add(pergola);
// Animation loop
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
</script>
</body>
</html>