<!DOCTYPE html>
<html>
<head>
<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>
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);
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 groundGeometry = new THREE.PlaneGeometry(20, 20);
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x228B22, side: THREE.DoubleSide });
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
scene.add(ground);
function createPost(x, z) {
const postGeometry = new THREE.BoxGeometry(3.5 / 12, 8, 3.5 / 12);
const post = new THREE.Mesh(postGeometry, woodMaterial);
post.position.set(x, 4, z);
scene.add(post);
}
function createMainBeam(x, z) {
const beamGeometry = new THREE.BoxGeometry(12, 7.25 / 12, 1.5 / 12);
const beam = new THREE.Mesh(beamGeometry, woodMaterial);
beam.position.set(x, 8 + 7.25 / 24, z);
scene.add(beam);
}
function createRafter(x, z) {
const rafterGeometry = new THREE.BoxGeometry(1.5 / 12, 9, 3.5 / 12);
const rafter = new THREE.Mesh(rafterGeometry, woodMaterial);
rafter.position.set(x, 8 + 7.25 / 24 + 3.5 / 24, z);
scene.add(rafter);
}
function createPurlin(x, z) {
const purlinGeometry = new THREE.BoxGeometry(12, 1.5 / 12, 1.5 / 12);
const purlin = new THREE.Mesh(purlinGeometry, woodMaterial);
purlin.position.set(x, 8 + 7.25 / 24 + 3.5 / 24 + 1.5 / 24, z);
scene.add(purlin);
}
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>