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;
font-family: Arial, sans-serif;
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="info">3D Pergola - Use mouse to orbit</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);
camera.lookAt(0, 4, 0);
// 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);
// Orbit controls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.target.set(0, 4, 0);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 20, 5);
directionalLight.castShadow = true;
directionalLight.shadow.camera.left = -20;
directionalLight.shadow.camera.right = 20;
directionalLight.shadow.camera.top = 20;
directionalLight.shadow.camera.bottom = -20;
directionalLight.shadow.camera.near = 0.1;
directionalLight.shadow.camera.far = 50;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
scene.add(directionalLight);
// Materials
const woodMaterial = new THREE.MeshPhongMaterial({
color: 0x8B4513,
shininess: 30
});
const grassMaterial = new THREE.MeshPhongMaterial({
color: 0x228B22,
shininess: 10
});
// 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 dimensions
const postWidth = 3.5 / 12;
const postHeight = 8.0;
const mainBeamThickness = 1.5 / 12;
const mainBeamHeight = 7.25 / 12;
const mainBeamOverhang = 1.5;
const rafterThickness = 1.5 / 12;
const rafterHeight = 3.5 / 12;
const rafterOverhang = 1.5;
const purlinSize = 1.5 / 12;
// Post positions for 9ft x 6ft rectangle
const postSpacingX = 9.0;
const postSpacingZ = 6.0;
// Create tapered geometry for beams and rafters
function createTaperedBeam(length, height, thickness, overhang, taperRatio) {
const shape = new THREE.Shape();
const halfLength = length / 2;
const halfThickness = thickness / 2;
// Start from bottom left
shape.moveTo(-halfLength, 0);
// Left taper
shape.lineTo(-halfLength + overhang, height * (1 - taperRatio));
shape.lineTo(-halfLength + overhang, height);
// Top edge
shape.lineTo(halfLength - overhang, height);
// Right taper
shape.lineTo(halfLength - overhang, height);
shape.lineTo(halfLength, height * (1 - taperRatio));
shape.lineTo(halfLength, 0);
// Bottom edge
shape.closePath();
const extrudeSettings = {
steps: 1,
depth: thickness,
bevelEnabled: false
};
const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
geometry.translate(0, 0, -halfThickness);
return geometry;
}
// Create posts
const posts = [];
const postPositions = [
[-postSpacingX/2, 0, -postSpacingZ/2],
[postSpacingX/2, 0, -postSpacingZ/2],
[-postSpacingX/2, 0, postSpacingZ/2],
[postSpacingX/2, 0, postSpacingZ/2]
];
postPositions.forEach(pos => {
const postGeometry = new THREE.BoxGeometry(postWidth, postHeight, postWidth);
const post = new THREE.Mesh(postGeometry, woodMaterial);
post.position.set(pos[0], postHeight/2, pos[2]);
post.castShadow = true;
post.receiveShadow = true;
scene.add(post);
posts.push(post);
});
// Create main beams
const mainBeamLength = postSpacingX + 2 * mainBeamOverhang;
const mainBeamGeometry = createTaperedBeam(mainBeamLength, mainBeamHeight, mainBeamThickness, mainBeamOverhang, 0.65);
const mainBeamPositions = [
[0, postHeight + mainBeamHeight/2, -postSpacingZ/2],
[0, postHeight + mainBeamHeight/2, postSpacingZ/2]
];
mainBeamPositions.forEach(pos => {
const beam = new THREE.Mesh(mainBeamGeometry, woodMaterial);
beam.position.set(pos[0], pos[1], pos[2]);
beam.castShadow = true;
beam.receiveShadow = true;
scene.add(beam);
});
// Create rafters
const rafterLength = postSpacingZ + 2 * rafterOverhang;
const rafterGeometry = createTaperedBeam(rafterLength, rafterHeight, rafterThickness, rafterOverhang, 0.65);
// Rotate rafter geometry 90 degrees for proper orientation
rafterGeometry.rotateY(Math.PI / 2);
const rafterCount = 7;
const rafterSpacing = mainBeamLength / (rafterCount - 1);
for (let i = 0; i < rafterCount; i++) {
const rafter = new THREE.Mesh(rafterGeometry, woodMaterial);
const xPos = -mainBeamLength/2 + i * rafterSpacing;
rafter.position.set(xPos, postHeight + mainBeamHeight + rafterHeight/2, 0);
rafter.castShadow = true;
rafter.receiveShadow = true;
scene.add(rafter);
}
// Create purlins
const purlinLength = mainBeamLength;
const purlinGeometry = new THREE.BoxGeometry(purlinLength, purlinSize, purlinSize);
const purlinCount = 7;
const purlinSpacing = rafterLength / (purlinCount - 1);
for (let i = 0; i < purlinCount; i++) {
const purlin = new THREE.Mesh(purlinGeometry, woodMaterial);
const zPos = -rafterLength/2 + i * purlinSpacing;
purlin.position.set(0, postHeight + mainBeamHeight + rafterHeight + purlinSize/2, zPos);
purlin.castShadow = true;
purlin.receiveShadow = true;
scene.add(purlin);
}
// 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>