<!DOCTYPE html>
<html lang="en">
<head>
<title>Three.js Pergola Scene</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body { margin: 0; overflow: hidden; background-color: #a0a0a0; }
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>
let scene, camera, renderer, controls;
// --- DIMENSION CONSTANTS (in feet) ---
// Post dimensions (nominal 4x4)
const POST_WIDTH = 3.5 / 12;
const POST_HEIGHT = 8.0;
const POST_DEPTH = 3.5 / 12;
// Main Beam dimensions (nominal 2x8)
const BEAM_THICKNESS = 1.5 / 12;
const BEAM_HEIGHT = 7.25 / 12;
const BEAM_TOTAL_LENGTH = 12.0;
const BEAM_OVERHANG = 1.5;
// Rafter dimensions (nominal 2x4)
const RAFTER_THICKNESS = 1.5 / 12;
const RAFTER_HEIGHT = 3.5 / 12;
const RAFTER_TOTAL_LENGTH = 9.0;
const RAFTER_OVERHANG = 1.5;
// Purlin dimensions (nominal 2x2)
const PURLIN_SIZE = 1.5 / 12;
const PURLIN_LENGTH = 12.0;
// Layout dimensions
const POST_SPAN_X = 9.0;
const POST_SPAN_Z = 6.0;
const RAFTER_COUNT = 7;
const PURLIN_COUNT = 7;
init();
animate();
function init() {
// --- SCENE SETUP ---
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87ceeb); // Sky blue
scene.fog = new THREE.Fog(0x87ceeb, 10, 100);
// --- CAMERA ---
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(12, 10, 15);
camera.lookAt(0, 4, 0);
// --- RENDERER ---
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// --- CONTROLS ---
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(0, 4, 0);
controls.update();
// --- LIGHTING ---
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(-15, 20, 10);
directionalLight.castShadow = true;
directionalLight.shadow.camera.top = 20;
directionalLight.shadow.camera.bottom = -20;
directionalLight.shadow.camera.left = -20;
directionalLight.shadow.camera.right = 20;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
scene.add(directionalLight);
// --- MATERIALS & TEXTURES ---
const textureLoader = new THREE.TextureLoader();
// Ground
const grassTexture = textureLoader.load('https://cdn.jsdelivr.net/gh/mrdoob/three.js/examples/textures/terrain/grasslight-big.jpg');
grassTexture.wrapS = THREE.RepeatWrapping;
grassTexture.wrapT = THREE.RepeatWrapping;
grassTexture.repeat.set(20, 20);
const groundMaterial = new THREE.MeshStandardMaterial({ map: grassTexture });
const groundGeometry = new THREE.PlaneGeometry(200, 200);
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
// Wood
const woodTexture = textureLoader.load('https://www.textures.com/system/gallery/photos/Wood/Planks/Clean/34317/WoodPlanksClean0004_1_S.jpg');
woodTexture.wrapS = THREE.RepeatWrapping;
woodTexture.wrapT = THREE.RepeatWrapping;
const woodMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0x966F33, // Brownish color
roughness: 0.8,
metalness: 0.1,
});
// --- CREATE PERGOLA ---
const pergola = new THREE.Group();
createPosts(pergola, woodMaterial);
createBeams(pergola, woodMaterial);
createRafters(pergola, woodMaterial);
createPurlins(pergola, woodMaterial);
scene.add(pergola);
// --- EVENT LISTENERS ---
window.addEventListener('resize', onWindowResize, false);
}
// --- PERGOLA COMPONENT FUNCTIONS ---
function createPosts(parentGroup, material) {
const postGeometry = new THREE.BoxGeometry(POST_WIDTH, POST_HEIGHT, POST_DEPTH);
const halfSpanX = POST_SPAN_X / 2;
const halfSpanZ = POST_SPAN_Z / 2;
const positions = [
new THREE.Vector3(halfSpanX, POST_HEIGHT / 2, halfSpanZ),
new THREE.Vector3(halfSpanX, POST_HEIGHT / 2, -halfSpanZ),
new THREE.Vector3(-halfSpanX, POST_HEIGHT / 2, halfSpanZ),
new THREE.Vector3(-halfSpanX, POST_HEIGHT / 2, -halfSpanZ),
];
positions.forEach(pos => {
const post = new THREE.Mesh(postGeometry, material);
post.position.copy(pos);
post.castShadow = true;
post.receiveShadow = true;
parentGroup.add(post);
});
}
function createBeamShape() {
const shape = new THREE.Shape();
const halfLen = BEAM_TOTAL_LENGTH / 2;
const halfHeight = BEAM_HEIGHT / 2;
const overhangStart = (POST_SPAN_X / 2);
const taperEndHeight = BEAM_HEIGHT * 0.35;
const bottomY = -halfHeight;
const taperedY = bottomY + (BEAM_HEIGHT - taperEndHeight);
shape.moveTo(-halfLen, taperedY); // 1. bottom-left
shape.lineTo(-overhangStart, bottomY); // 2. start of flat bottom
shape.lineTo(overhangStart, bottomY); // 3. end of flat bottom
shape.lineTo(halfLen, taperedY); // 4. bottom-right
shape.lineTo(halfLen, halfHeight); // 5. top-right
shape.lineTo(-halfLen, halfHeight); // 6. top-left
shape.closePath(); // 7. back to start
return shape;
}
function createBeams(parentGroup, material) {
const beamShape = createBeamShape();
const extrudeSettings = { depth: BEAM_THICKNESS, bevelEnabled: false };
const beamGeometry = new THREE.ExtrudeGeometry(beamShape, extrudeSettings);
beamGeometry.center(); // Center the geometry for easier positioning
const beamY = POST_HEIGHT + BEAM_HEIGHT / 2;
const beamZ = POST_SPAN_Z / 2 + BEAM_THICKNESS / 2;
const positions = [
new THREE.Vector3(0, beamY, beamZ),
new THREE.Vector3(0, beamY, -beamZ)
];
positions.forEach(pos => {
const beam = new THREE.Mesh(beamGeometry, material);
beam.position.copy(pos);
// Rotate to align extrusion depth with Z-axis
beam.rotation.y = Math.PI / 2;
beam.castShadow = true;
beam.receiveShadow = true;
parentGroup.add(beam);
});
}
function createRafterShape() {
const shape = new THREE.Shape();
const halfLen = RAFTER_TOTAL_LENGTH / 2;
const halfHeight = RAFTER_HEIGHT / 2;
const overhangStart = (POST_SPAN_Z / 2);
const taperEndHeight = RAFTER_HEIGHT * 0.35;
const bottomY = -halfHeight;
const taperedY = bottomY + (RAFTER_HEIGHT - taperEndHeight);
shape.moveTo(-halfLen, taperedY);
shape.lineTo(-overhangStart, bottomY);
shape.lineTo(overhangStart, bottomY);
shape.lineTo(halfLen, taperedY);
shape.lineTo(halfLen, halfHeight);
shape.lineTo(-halfLen, halfHeight);
shape.closePath();
return shape;
}
function createRafters(parentGroup, material) {
const rafterShape = createRafterShape();
const extrudeSettings = { depth: RAFTER_THICKNESS, bevelEnabled: false };
const rafterGeometry = new THREE.ExtrudeGeometry(rafterShape, extrudeSettings);
rafterGeometry.center();
const rafterY = POST_HEIGHT + BEAM_HEIGHT + (RAFTER_HEIGHT / 2);
const spacingX = BEAM_TOTAL_LENGTH / (RAFTER_COUNT - 1);
for (let i = 0; i < RAFTER_COUNT; i++) {
const rafter = new THREE.Mesh(rafterGeometry, material);
const xPos = -BEAM_TOTAL_LENGTH / 2 + i * spacingX;
rafter.position.set(xPos, rafterY, 0);
rafter.castShadow = true;
rafter.receiveShadow = true;
parentGroup.add(rafter);
}
}
function createPurlins(parentGroup, material) {
const purlinGeometry = new THREE.BoxGeometry(PURLIN_LENGTH, PURLIN_SIZE, PURLIN_SIZE);
const purlinY = POST_HEIGHT + BEAM_HEIGHT + RAFTER_HEIGHT + (PURLIN_SIZE / 2);
const spacingZ = RAFTER_TOTAL_LENGTH / (PURLIN_COUNT - 1);
for (let i = 0; i < PURLIN_COUNT; i++) {
const purlin = new THREE.Mesh(purlinGeometry, material);
const zPos = -RAFTER_TOTAL_LENGTH / 2 + i * spacingZ;
purlin.position.set(0, purlinY, zPos);
purlin.castShadow = true;
purlin.receiveShadow = true;
parentGroup.add(purlin);
}
}
// --- UTILITY FUNCTIONS ---
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
</script>
</body>
</html>