<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>3D Pergola Visualization</title>
<style>
body { margin: 0; overflow: hidden; }
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 background
// Camera setup
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(15, 10, 15);
camera.lookAt(0, 5, 0);
// Renderer setup
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// OrbitControls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 20, 10);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 1024;
directionalLight.shadow.mapSize.height = 1024;
scene.add(directionalLight);
// Ground (grassy plain)
const groundGeometry = new THREE.PlaneGeometry(50, 50);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x3a5f0b,
roughness: 0.8,
metalness: 0.2
});
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
// Wood material for pergola
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0x8B4513,
roughness: 0.7,
metalness: 0.1
});
// Helper function to create tapered beams
function createTaperedBeam(width, height, depth, length, taperStart, taperEnd) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = 0;
shape.moveTo(x, y);
shape.lineTo(x + width, y);
shape.lineTo(x + width, height);
shape.lineTo(x, height);
shape.lineTo(x, y);
const extrudeSettings = {
steps: 100,
depth: length,
bevelEnabled: false,
extrudePath: new THREE.LineCurve3(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, length)
)
};
// Create the geometry
const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
// Apply taper to the bottom face
const position = geometry.attributes.position;
for (let i = 0; i < position.count; i++) {
const z = position.getZ(i);
if (z < taperStart || z > length - taperEnd) {
// In the tapered section
const taperProgress = z < taperStart ?
(taperStart - z) / taperStart :
(z - (length - taperEnd)) / taperEnd;
const taperFactor = 1 - (0.65 * taperProgress); // 35% of original height at end
if (position.getY(i) === 0) {
position.setY(i, height * (1 - taperFactor));
}
}
}
return new THREE.Mesh(geometry, woodMaterial);
}
// Create posts (4 count)
const postWidth = 3.5 / 12;
const postDepth = 3.5 / 12;
const postHeight = 8.0;
// Post positions (9ft x 6ft rectangle)
const postPositions = [
{ x: -4.5, z: -3.0 },
{ x: 4.5, z: -3.0 },
{ x: -4.5, z: 3.0 },
{ x: 4.5, z: 3.0 }
];
const posts = [];
postPositions.forEach(pos => {
const postGeometry = new THREE.BoxGeometry(postWidth, postHeight, postDepth);
const post = new THREE.Mesh(postGeometry, woodMaterial);
post.position.set(pos.x, postHeight / 2, pos.z);
post.castShadow = true;
scene.add(post);
posts.push(post);
});
// Create main beams (2 count)
const mainBeamThickness = 1.5 / 12;
const mainBeamHeight = 7.25 / 12;
const mainBeamLength = 12.0;
const mainBeamOverhang = 1.5;
const mainBeam1 = createTaperedBeam(
mainBeamThickness, mainBeamHeight, mainBeamLength,
mainBeamLength, mainBeamOverhang, mainBeamOverhang
);
mainBeam1.rotation.y = Math.PI / 2;
mainBeam1.position.set(0, postHeight + mainBeamHeight / 2, -3.0 - mainBeamOverhang / 2);
mainBeam1.castShadow = true;
scene.add(mainBeam1);
const mainBeam2 = mainBeam1.clone();
mainBeam2.position.z = 3.0 + mainBeamOverhang / 2;
scene.add(mainBeam2);
// Create rafters (7 count)
const rafterThickness = 1.5 / 12;
const rafterHeight = 3.5 / 12;
const rafterLength = 9.0;
const rafterOverhang = 1.5;
const rafterCount = 7;
const rafterSpacing = mainBeamLength / (rafterCount + 1);
for (let i = 0; i < rafterCount; i++) {
const rafter = createTaperedBeam(
rafterThickness, rafterHeight, rafterLength,
rafterLength, rafterOverhang, rafterOverhang
);
rafter.position.set(
-mainBeamLength / 2 + rafterSpacing * (i + 1),
postHeight + mainBeamHeight + rafterHeight / 2,
0
);
rafter.castShadow = true;
scene.add(rafter);
}
// Create purlins (7 count)
const purlinWidth = 1.5 / 12;
const purlinHeight = 1.5 / 12;
const purlinLength = 12.0;
const purlinCount = 7;
const purlinSpacing = rafterLength / (purlinCount + 1);
for (let i = 0; i < purlinCount; i++) {
const purlinGeometry = new THREE.BoxGeometry(purlinWidth, purlinHeight, purlinLength);
const purlin = new THREE.Mesh(purlinGeometry, woodMaterial);
purlin.position.set(
0,
postHeight + mainBeamHeight + rafterHeight + purlinHeight / 2,
-rafterLength / 2 + purlinSpacing * (i + 1)
);
purlin.castShadow = true;
scene.add(purlin);
}
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Animation loop
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>