Create a static 3D pergola scene using three.js. Use these imports: <script…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Create a static 3D pergola scene using three.js.
Use these imports:
All dimensions are in feet. Y-axis is up.
Scene Setup:
Pergola Dimensions & Components (Actual lumber dimensions in feet):
Construction:
Posts (4 count):
Main Beams (2 count):
Rafters (7 count):
Purlins (7 count):
Add OrbitControls for ease of viewing. It is optional to make the scene more pleasing by simple additions but avoid anything distracting from the main pergola design. Output as a single-file html three.js visualization of the pergola you have designed...
Here's the single-file HTML for the static 3D pergola scene using three.js, adhering to all your specified requirements and dimensions:
<!DOCTYPE html>
<html>
<head>
<title>3D Pergola Scene</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<!-- Three.js Libraries -->
<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>
// Global variables for scene, camera, renderer, and controls
let scene, camera, renderer, controls;
// --- Constants for Pergola Dimensions (in feet, converted from nominal lumber sizes) ---
const FOOT = 1; // 1 Three.js unit = 1 foot for simpler scaling
// Post dimensions (nominal 4x4)
const POST_WIDTH = 3.5 / 12 * FOOT; // Width/Depth
const POST_HEIGHT = 8.0 * FOOT;
// Main Beam dimensions (nominal 2x8)
const MAIN_BEAM_THICKNESS = 1.5 / 12 * FOOT; // Corresponds to Z-axis for beam along X
const MAIN_BEAM_HEIGHT = 7.25 / 12 * FOOT; // Corresponds to Y-axis
const MAIN_BEAM_OVERHANG = 1.5 * FOOT;
const MAIN_BEAM_SPAN_X_POSTS = 9.0 * FOOT; // Distance between post centers along X
const MAIN_BEAM_TOTAL_LENGTH = MAIN_BEAM_SPAN_X_POSTS + 2 * MAIN_BEAM_OVERHANG; // Total length: 9.0 + 2*1.5 = 12.0 ft
// Rafter dimensions (nominal 2x4)
const RAFTER_THICKNESS = 1.5 / 12 * FOOT; // Corresponds to X-axis for rafter along Z
const RAFTER_HEIGHT = 3.5 / 12 * FOOT; // Corresponds to Y-axis
const RAFTER_OVERHANG = 1.5 * FOOT;
const RAFTER_SPAN_Z_BEAMS = 6.0 * FOOT; // Distance between main beam centers along Z (based on post Z-centers)
const RAFTER_TOTAL_LENGTH = RAFTER_SPAN_Z_BEAMS + 2 * RAFTER_OVERHANG; // Total length: 6.0 + 2*1.5 = 9.0 ft
// Purlin dimensions (nominal 2x2)
const PURLIN_WIDTH = 1.5 / 12 * FOOT; // Corresponds to X-axis
const PURLIN_HEIGHT = 1.5 / 12 * FOOT; // Corresponds to Y-axis
const PURLIN_DEPTH = 1.5 / 12 * FOOT; // Corresponds to Z-axis
const PURLIN_LENGTH = MAIN_BEAM_TOTAL_LENGTH; // Purlins span the full length of the main beams (12.0 ft)
// Taper percentage for beams/rafters overhang relief
const TAPER_END_HEIGHT_PERCENTAGE = 0.35; // 35% of original height
// Pergola Post Layout (Center of 9ft x 6ft post rectangle assumed at X=0, Z=0)
const PERGOLA_X_SPAN = 9.0 * FOOT; // Distance between post centers along X
const PERGOLA_Z_SPAN = 6.0 * FOOT; // Distance between post centers along Z
// Y-levels (heights from ground, Y=0)
const GROUND_Y = 0;
const POST_TOP_Y = POST_HEIGHT;
const MAIN_BEAM_TOP_Y = POST_TOP_Y + MAIN_BEAM_HEIGHT;
const RAFTER_TOP_Y = MAIN_BEAM_TOP_Y + RAFTER_HEIGHT;
const PURLIN_TOP_Y = RAFTER_TOP_Y + PURLIN_HEIGHT;
// Wood material
let woodMaterial;
/**
* Creates a basic BoxGeometry mesh.
* @param {number} width - Width of the box.
* @param {number} height - Height of the box.
* @param {number} depth - Depth of the box.
* @param {THREE.Material} material - The material for the box.
* @returns {THREE.Mesh} The created box mesh.
*/
function createBox(width, height, depth, material) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const mesh = new THREE.Mesh(geometry, material);
return mesh;
}
/**
* Creates an ExtrudeGeometry for a beam with tapered ends.
* The profile is defined in the XY plane, and extruded along the Z axis.
* @param {number} length - Total length of the beam (along X in profile).
* @param {number} height - Height of the beam (along Y in profile).
* @param {number} thickness - Thickness of the beam (extrusion depth along Z).
* @param {number} overhang - Length of the overhang at each end where the taper occurs.
* @param {number} taperHeightPercentage - The height at the very end of the taper as a percentage of original height.
* @returns {THREE.ExtrudeGeometry} The created geometry.
*/
function createTaperedBeamGeometry(length, height, thickness, overhang, taperHeightPercentage) {
const taperedHeight = height * taperHeightPercentage;
const shape = new THREE.Shape();
// Define the outer profile of the beam in the XY plane (length along X, height along Y)
// Path: Bottom-left -> Bottom-taper-start-left -> Bottom-taper-start-right -> Bottom-right -> Top-right -> Top-left -> Close
shape.moveTo(0, taperedHeight); // 1. Bottom-left tapered end
shape.lineTo(overhang, height); // 2. Start of taper (bottom face becomes flat here)
shape.lineTo(length - overhang, height); // 3. End of flat bottom section (start of right taper)
shape.lineTo(length, taperedHeight); // 4. Bottom-right tapered end
shape.lineTo(length, height); // 5. Top-right corner (top face is flat)
shape.lineTo(0, height); // 6. Top-left corner (top face is flat)
shape.lineTo(0, taperedHeight); // Close path back to 1.
const extrudeSettings = {
steps: 1,
depth: thickness,
bevelEnabled: false // No bevel for lumber
};
const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
// Center the geometry's origin for easier placement:
// Translate so its geometric center is at (0,0,0).
// This is crucial because the profile was defined from (0,0) upwards/rightwards.
geometry.translate(-length / 2, -height / 2, -thickness / 2);
return geometry;
}
/**
* Initializes the 3D scene, camera, renderer, and controls.
*/
function init() {
// Scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB); // Sky blue background
// Camera
// Positioned for a good overview, looking down slightly at the pergola
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(15 * FOOT, 15 * FOOT, 15 * FOOT);
camera.lookAt(0, 5 * FOOT, 0); // Look towards the approximate center of the pergola
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true; // Enable shadow maps for realistic lighting
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // Softer shadows
document.body.appendChild(renderer.domElement);
// OrbitControls for interactive viewing
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(0, 5 * FOOT, 0); // Orbit around the center of the pergola
controls.update();
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6); // Soft ambient light
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); // Stronger directional light
directionalLight.position.set(10 * FOOT, 20 * FOOT, 10 * FOOT); // Positioned above and to the side
directionalLight.castShadow = true; // Enable shadow casting
// Configure shadow properties for better quality and coverage
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
directionalLight.shadow.camera.near = 0.5 * FOOT;
directionalLight.shadow.camera.far = 50 * FOOT;
directionalLight.shadow.camera.left = -20 * FOOT;
directionalLight.shadow.camera.right = 20 * FOOT;
directionalLight.shadow.camera.top = 20 * FOOT;
directionalLight.shadow.camera.bottom = -20 * FOOT;
scene.add(directionalLight);
// Ground (grassy plain)
const groundGeometry = new THREE.PlaneGeometry(50 * FOOT, 50 * FOOT);
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x8BC34A }); // Grassy green
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2; // Rotate to lie flat on XZ plane
ground.position.y = GROUND_Y; // At Y=0
ground.receiveShadow = true; // Enable shadow receiving on the ground
scene.add(ground);
// Wood Material (SaddleBrown color)
woodMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513 });
// Build the pergola structure
createPergola();
// Handle window resizing
window.addEventListener('resize', onWindowResize, false);
}
/**
* Creates all components of the pergola and adds them to the scene.
*/
function createPergola() {
const pergolaGroup = new THREE.Group(); // Use a group to organize all pergola parts
// 1. Posts (4 count)
// Post centers define a 9ft (X) by 6ft (Z) rectangle
const postXPositions = [PERGOLA_X_SPAN / 2, -PERGOLA_X_SPAN / 2];
const postZPositions = [PERGOLA_Z_SPAN / 2, -PERGOLA_Z_SPAN / 2];
postXPositions.forEach(px => {
postZPositions.forEach(pz => {
const post = createBox(POST_WIDTH, POST_HEIGHT, POST_WIDTH, woodMaterial);
post.position.set(px, POST_HEIGHT / 2, pz); // Position Y at half height
post.castShadow = true;
post.receiveShadow = true;
pergolaGroup.add(post);
});
});
// 2. Main Beams (2 count)
// Beams along X axis. Dimensions: Length=MAIN_BEAM_TOTAL_LENGTH, Height=MAIN_BEAM_HEIGHT, Thickness=MAIN_BEAM_THICKNESS
const mainBeamGeometry = createTaperedBeamGeometry(
MAIN_BEAM_TOTAL_LENGTH, MAIN_BEAM_HEIGHT, MAIN_BEAM_THICKNESS,
MAIN_BEAM_OVERHANG, TAPER_END_HEIGHT_PERCENTAGE
);
// Calculate Z-offset for main beams to ride on top of posts
// Posts are centered at +/- PERGOLA_Z_SPAN / 2
// Main beams align with the outer edges of the posts
const beamZOffset = PERGOLA_Z_SPAN / 2 + (POST_WIDTH / 2) + (MAIN_BEAM_THICKNESS / 2);
// Main Beam 1 (front)
const mainBeam1 = new THREE.Mesh(mainBeamGeometry, woodMaterial);
// Position Y at the nominal center of the beam's height, ensuring its top is at MAIN_BEAM_TOP_Y
mainBeam1.position.set(0, MAIN_BEAM_TOP_Y - MAIN_BEAM_HEIGHT / 2, beamZOffset);
mainBeam1.castShadow = true;
mainBeam1.receiveShadow = true;
pergolaGroup.add(mainBeam1);
// Main Beam 2 (back)
const mainBeam2 = new THREE.Mesh(mainBeamGeometry, woodMaterial);
mainBeam2.position.set(0, MAIN_BEAM_TOP_Y - MAIN_BEAM_HEIGHT / 2, -beamZOffset);
mainBeam2.castShadow = true;
mainBeam2.receiveShadow = true;
pergolaGroup.add(mainBeam2);
// 3. Rafters (7 count)
// Rafters along Z axis. Dimensions: Length=RAFTER_TOTAL_LENGTH, Height=RAFTER_HEIGHT, Thickness=RAFTER_THICKNESS
const rafterGeometry = createTaperedBeamGeometry(
RAFTER_TOTAL_LENGTH, RAFTER_HEIGHT, RAFTER_THICKNESS,
RAFTER_OVERHANG, TAPER_END_HEIGHT_PERCENTAGE
);
// The `createTaperedBeamGeometry` creates the beam with its length along X and thickness along Z.
// For rafters, the length should be along Z and thickness along X. So, rotate around Y by 90 degrees.
rafterGeometry.rotateY(Math.PI / 2);
const numRafters = 7;
// "end rafters aligning with the ends of the main beam ends"
// Main beams span from -MAIN_BEAM_TOTAL_LENGTH/2 to +MAIN_BEAM_TOTAL_LENGTH/2 (i.e., -6.0 to 6.0 on X)
// First rafter's outermost face is at -6.0, last rafter's outermost face is at 6.0
const rafterStartX = -MAIN_BEAM_TOTAL_LENGTH / 2 + RAFTER_THICKNESS / 2;
const rafterSpanForCenters = MAIN_BEAM_TOTAL_LENGTH - RAFTER_THICKNESS; // Span covered by rafter centers
const rafterSpacing = rafterSpanForCenters / (numRafters - 1); // Distance between centers
for (let i = 0; i < numRafters; i++) {
const rafter = new THREE.Mesh(rafterGeometry, woodMaterial);
const xPos = rafterStartX + i * rafterSpacing;
rafter.position.set(xPos, RAFTER_TOP_Y - RAFTER_HEIGHT / 2, 0); // Centered along Z
rafter.castShadow = true;
rafter.receiveShadow = true;
pergolaGroup.add(rafter);
}
// 4. Purlins (7 count)
// Purlins along X axis. Dimensions: Length=PURLIN_LENGTH, Height=PURLIN_HEIGHT, Depth=PURLIN_DEPTH
const purlinGeometry = createBox(PURLIN_LENGTH, PURLIN_HEIGHT, PURLIN_DEPTH, woodMaterial);
const numPurlins = 7;
// "end purlins aligning with the ends of the rafter ends"
// Rafters span from -RAFTER_TOTAL_LENGTH/2 to +RAFTER_TOTAL_LENGTH/2 (i.e., -4.5 to 4.5 on Z)
// First purlin's outermost face is at -4.5, last purlin's outermost face is at 4.5
const purlinStartZ = -RAFTER_TOTAL_LENGTH / 2 + PURLIN_DEPTH / 2;
const purlinSpanForCenters = RAFTER_TOTAL_LENGTH - PURLIN_DEPTH; // Span covered by purlin centers
const purlinSpacing = purlinSpanForCenters / (numPurlins - 1); // Distance between centers
for (let i = 0; i < numPurlins; i++) {
const purlin = new THREE.Mesh(purlinGeometry, woodMaterial);
const zPos = purlinStartZ + i * purlinSpacing;
purlin.position.set(0, PURLIN_TOP_Y - PURLIN_HEIGHT / 2, zPos); // Centered along X
purlin.castShadow = true;
purlin.receiveShadow = true;
pergolaGroup.add(purlin);
}
scene.add(pergolaGroup);
}
/**
* Handles window resizing to adjust camera aspect ratio and renderer size.
*/
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
/**
* Animation loop for rendering the scene and updating controls.
*/
function animate() {
requestAnimationFrame(animate);
controls.update(); // Required if damping or auto-rotate are enabled
renderer.render(scene, camera);
}
// Initialize and animate the scene when the script loads
init();
animate();
</script>
</body>
</html>