<!DOCTYPE html>
<html>
<head>
<title>3D Pergola Scene</title>
<style>
body {
margin: 0;
}
</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);
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.0;
const postDepth = postWidth;
const mainBeamThickness = 1.5 / 12;
const mainBeamHeight = 7.25 / 12;
const mainBeamLength = 12.0;
const rafterThickness = 1.5 / 12;
const rafterHeight = 3.5 / 12;
const rafterLength = 9.0;
const purlinWidth = 1.5 / 12;
const purlinHeight = purlinWidth;
const purlinLength = 12.0;
function createTaperedGeometry(width, height, depth, segments) {
const geometry = new THREE.BufferGeometry();
const vertices = [];
const indices = [];
const normals = [];
const uvs = [];
const x = width / 2;
const y = height / 2;
const z = depth / 2;
for (let i = 0; i <= segments; i++) {
const t = i / segments;
const h = height * (1 - 0.35 * t);
vertices.push(
-x, -y, -z,
x, -y, -z,
x, h, -z,
-x, h, -z,
-x, -y, z,
x, -y, z,
x, h, z,
-x, h, z
);
if (i < segments) {
const offset = i * 8;
indices.push(
offset, offset + 1, offset + 2,
offset + 2, offset + 3, offset,
offset + 4, offset + 5, offset + 6,
offset + 6, offset + 7, offset + 4,
offset, offset + 4, offset + 5,
offset + 1, offset + 5, offset + 1,
offset + 2, offset + 6, offset + 2,
offset + 3, offset + 7, offset + 3,
offset + 4, offset + 6, offset + 6,
offset + 7, offset + 3, offset + 7
);
}
const normal = new THREE.Vector3(0, 1, 0);
for (let j = 0; j < 8; j++) {
normals.push(normal.x, normal.y, normal.z);
}
for (let j = 0; j < 4; j++) {
uvs.push(t, 0, t, 1, t, 1, t, 0);
}
}
geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
geometry.setIndex(indices);
geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));
geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2));
return geometry;
}
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 = createTaperedGeometry(mainBeamThickness, mainBeamHeight, mainBeamLength, 30);
const mesh = new THREE.Mesh(geometry, woodMaterial);
mesh.position.set(x, postHeight + mainBeamHeight / 2, z);
mesh.rotation.x = -Math.PI / 2;
scene.add(mesh);
}
function createRafter(x, z) {
const geometry = createTaperedGeometry(rafterThickness, rafterHeight, rafterLength, 30);
const mesh = new THREE.Mesh(geometry, woodMaterial);
mesh.position.set(x, postHeight + mainBeamHeight + rafterHeight / 2, z);
mesh.rotation.x = -Math.PI / 2;
scene.add(mesh);
}
function createPurlin(x, z) {
const geometry = new THREE.BoxGeometry(purlinWidth, purlinHeight, purlinLength);
const mesh = new THREE.Mesh(geometry, woodMaterial);
mesh.position.set(x, postHeight + mainBeamHeight + rafterHeight + purlinHeight / 2, z);
mesh.rotation.z = -Math.PI / 2;
scene.add(mesh);
}
function createPergola() {
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));
const mainBeamPositions = [
{ x: -4.5, z: 0 },
{ x: 4.5, z: 0 }
];
mainBeamPositions.forEach(pos => createMainBeam(pos.x, pos.z));
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, pos.z));
const purlinPositions = [
{ x: -6, z: -4.5 },
{ x: -6, z: -1.5 },
{ x: -6, z: 1.5 },
{ x: -6, z: 4.5 },
{ x: 6, z: -4.5 },
{ x: 6, z: -1.5 },
{ x: 6, z: 1.5 },
{ x: 6, z: 4.5 }
];
purlinPositions.forEach(pos => createPurlin(pos.x, pos.z));
}
createPergola();
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>