<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>3D Pergola Scene</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>
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
camera.position.set(10, 10, 10);
controls.update();
const ambientLight = new THREE.AmbientLight(0x404040); // soft white light
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 10, 7.5);
scene.add(directionalLight);
const woodMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513, metalness: 0.1, roughness: 0.5 });
const postWidth = 3.5 / 12;
const postHeight = 8;
const postDepth = postWidth;
const mainBeamThickness = 1.5 / 12;
const mainBeamHeight = 7.25 / 12;
const mainBeamLength = 12;
const rafterThickness = 1.5 / 12;
const rafterHeight = 3.5 / 12;
const rafterLength = 9;
const purlinWidth = 1.5 / 12;
const purlinHeight = purlinWidth;
const purlinLength = 12;
function createPost(x, z) {
const geometry = new THREE.BoxGeometry(postWidth, postHeight, postDepth);
const mesh = new THREE.Mesh(geometry, woodMaterial);
mesh.position.set(x, postHeight / 2, z);
scene.add(mesh);
}
function createMainBeam(x, z) {
const geometry = new THREE.BoxGeometry(mainBeamLength, mainBeamHeight, mainBeamThickness);
const mesh = new THREE.Mesh(geometry, woodMaterial);
mesh.position.set(x, postHeight + mainBeamHeight / 2, z);
scene.add(mesh);
const taperGeometry = new THREE.PlaneGeometry(mainBeamLength, mainBeamHeight);
const taperMesh = new THREE.Mesh(taperGeometry, woodMaterial);
taperMesh.rotation.x = -Math.PI / 2;
taperMesh.position.set(x, postHeight, z);
taperMesh.scale.y = 0.35;
scene.add(taperMesh);
}
function createRafter(x, z) {
const geometry = new THREE.BoxGeometry(rafterLength, rafterHeight, rafterThickness);
const mesh = new THREE.Mesh(geometry, woodMaterial);
mesh.position.set(x, postHeight + mainBeamHeight + rafterHeight / 2, z);
scene.add(mesh);
const taperGeometry = new THREE.PlaneGeometry(rafterLength, rafterHeight);
const taperMesh = new THREE.Mesh(taperGeometry, woodMaterial);
taperMesh.rotation.x = -Math.PI / 2;
taperMesh.position.set(x, postHeight + mainBeamHeight, z);
taperMesh.scale.y = 0.35;
scene.add(taperMesh);
}
function createPurlin(x, z) {
const geometry = new THREE.BoxGeometry(purlinLength, purlinHeight, purlinWidth);
const mesh = new THREE.Mesh(geometry, woodMaterial);
mesh.position.set(x, postHeight + mainBeamHeight + rafterHeight + purlinHeight / 2, z);
scene.add(mesh);
}
const postPositions = [
{ x: -4.5, z: -3 },
{ x: 4.5, z: -3 },
{ x: -4.5, z: 3 },
{ x: 4.5, z: 3 }
];
postPositions.forEach(pos => createPost(pos.x, pos.z));
createMainBeam(0, -3);
createMainBeam(0, 3);
const rafterPositions = [
{ x: -6, z: 0 },
{ x: -3, z: 0 },
{ x: 0, z: 0 },
{ x: 3, z: 0 },
{ x: 6, z: 0 }
];
rafterPositions.forEach(pos => {
createRafter(pos.x, -3);
createRafter(pos.x, 3);
});
const purlinPositions = [
{ x: -6, z: -4.5 },
{ x: -6, z: 4.5 },
{ x: -3, z: -4.5 },
{ x: -3, z: 4.5 },
{ x: 0, z: -4.5 },
{ x: 0, z: 4.5 },
{ x: 3, z: -4.5 },
{ x: 3, z: 4.5 },
{ x: 6, z: -4.5 },
{ x: 6, z: 4.5 }
];
purlinPositions.forEach(pos => createPurlin(pos.x, pos.z));
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>