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:
<!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; overflow: hidden; font-family: sans-serif; }
canvas { display: block; }
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background-color: rgba(0,0,0,0.5);
padding: 5px 10px;
border-radius: 5px;
z-index: 100;
}
</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>
<div id="info">
<h1>3D Pergola Model</h1>
<p>Use mouse to orbit, zoom, and pan.</p>
</div>
<script>
// --- Scene Setup ---
const scene = new THREE.Scene();
// A light sky blue background
scene.background = new THREE.Color(0xcce0ff);
// --- Constants for Pergola Dimensions and Positions (in feet) ---
const postHeight = 8.0;
const postSpanX = 9.0; // Width of the pergola along X axis (between posts)
const postSpanZ = 6.0; // Depth of the pergola along Z axis (between posts)
const overhang = 1.5; // Overhang for beams and rafters at each end
// Nominal lumber dimensions converted to feet
// (Actual dimensions are slightly smaller than nominal)
const postDim = 3.5 / 12.0; // Nominal 4x4: 3.5 inches / 12 inches/foot
const beamHeight = 7.25 / 12.0; // Nominal 2x8: 7.25 inches / 12
const beamThickness = 1.5 / 12.0; // Nominal 2x8: 1.5 inches / 12 (thickness along Z for beams along X)
const rafterHeight = 3.5 / 12.0; // Nominal 2x4: 3.5 inches / 12
const rafterThickness = 1.5 / 12.0; // Nominal 2x4: 1.5 inches / 12 (thickness along X for rafters along Z)
const purlinDim = 1.5 / 12.0; // Nominal 2x2: 1.5 inches / 12 (width/depth for purlins)
// Calculate derived dimensions
const beamTotalLength = postSpanX + 2 * overhang; // Total length of main beams along X
const rafterTotalLength = postSpanZ + 2 * overhang; // Total length of rafters along Z
// Material for all wood components
const woodMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513 }); // SaddleBrown
// --- Helper Function to Create Tapered Beam/Rafter Geometry ---
// This function creates a prism with a linearly varying cross-section along a primary axis.
// It's used for the main beams and rafters which have a decorative taper on the overhangs.
function createTaperedBeamGeometry(length, centerSpan, overhang, yTop, yBotCenter, yBotEnd, thickness, primaryAxis, offset) {
const geometry = new THREE.BufferGeometry();
const points = []; // Stores vertex coordinates {x,y,z}
const indices = [];
// Define points along the primary axis (where cross-sections are defined)
// These points mark the transitions: start, center-start, center-end, end
const axisPoints = [-length / 2, -centerSpan / 2, centerSpan / 2, length / 2];
// Define corresponding bottom Y coordinates for these cross-sections.
// The bottom edge is flat in the center span and tapers upwards (Y increases) at the overhang ends.
// The height is reduced to 35% of original at the overhang ends, meaning the bottom Y is higher.
// yBotEnd = yTop - (0.35 * beamHeight) => yBotEnd = (yBotCenter + beamHeight) - 0.35*beamHeight => yBotEnd = yBotCenter + 0.65*beamHeight
const yBottoms = [yBotEnd, yBotCenter, yBotCenter, yBotEnd];
const verticesPerSection = 4; // Each cross-section has 4 vertices: top-front, bottom-front, top-back, bottom-back
// Generate vertices for each of the 4 cross-sections
for (let i = 0; i < axisPoints.length; i++) {
const axisVal = axisPoints[i]; // The coordinate along the primary axis (X or Z)
const yBot = yBottoms[i]; // The Y coordinate of the bottom edge at this section
let v1, v2, v3, v4; // Define the 4 vertices of the cross-section
if (primaryAxis === 'x') {
// Primary axis is X, thickness is along Z. The 'offset' is the constant Z coordinate.
// Vertices are defined in the YZ plane at a specific X (axisVal).
v1 = { x: axisVal, y: yTop, z: thickness / 2 + offset }; // Top-front
v2 = { x: axisVal, y: yBot, z: thickness / 2 + offset }; // Bottom-front
v3 = { x: axisVal, y: yTop, z: -thickness / 2 + offset };// Top-back
v4 = { x: axisVal, y: yBot, z: -thickness / 2 + offset };// Bottom-back
} else { // primaryAxis === 'z'
// Primary axis is Z, thickness is along X. The 'offset' is the constant X coordinate.
// Vertices are defined in the XY plane at a specific Z (axisVal).
v1 = { x: offset, y: yTop, z: axisVal }; // Top-front (relative to X-axis perspective)
v2 = { x: offset, y: yBot, z: axisVal }; // Bottom-front
v3 = { x: offset - thickness, y: yTop, z: axisVal }; // Top-back (thickness subtracted along X)
v4 = { x: offset - thickness, y: yBot, z: axisVal }; // Bottom-back (thickness subtracted along X)
}
points.push(v1, v2, v3, v4);
}
// Generate indices for triangles connecting adjacent cross-sections.
// We connect the 4 vertices of section 'i' to the 4 vertices of section 'i+1'.
// Each quad face is split into two triangles.
for (let i = 0; i < axisPoints.length - 1; i++) {
const currentBase = i * verticesPerSection; // Index of the first vertex of the current cross-section
const nextBase = (i + 1) * verticesPerSection; // Index of the first vertex of the next cross-section
// Connect vertices to form faces:
// Top face (quad formed by v0, v2, next_v2, next_v0)
indices.push(currentBase + 0, currentBase + 2, nextBase + 2); // Triangle 1
indices.push(currentBase + 0, nextBase + 2, nextBase + 0); // Triangle 2
// Bottom face (quad formed by v1, v3, next_v3, next_v1)
indices.push(currentBase + 1, nextBase + 1, nextBase + 3); // Triangle 1
indices.push(currentBase + 1, nextBase + 3, currentBase + 3); // Triangle 2
// Front face (quad formed by v0, v1, next_v1, next_v0) - side along the primary axis
indices.push(currentBase + 0, currentBase + 1, nextBase + 1); // Triangle 1
indices.push(currentBase + 0, nextBase + 1, nextBase + 0); // Triangle 2
// Back face (quad formed by v2, v3, next_v3, next_v2) - opposite side along the primary axis
indices.push(currentBase + 2, nextBase + 2, nextBase + 3); // Triangle 1
indices.push(currentBase + 2, nextBase + 3, currentBase + 3); // Triangle 2
}
// Convert the array of {x,y,z} objects into a Float32Array for the BufferAttribute
const positionArray = new Float32Array(points.length * 3);
for (let i = 0; i < points.length; i++) {
positionArray[i * 3] = points[i].x;
positionArray[i * 3 + 1] = points[i].y;
positionArray[i * 3 + 2] = points[i].z;
}
geometry.setAttribute('position', new THREE.BufferAttribute(positionArray, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals(); // Important for correct lighting and shading
return geometry;
}
// --- Initialize Scene, Camera, Renderer, Controls ---
function init() {
// Camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(15, 15, 15); // Position for a good overview of the pergola
camera.lookAt(0, 0, 0); // Look towards the center of the scene
// Renderer
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true; // Enable shadows for a more realistic look
document.body.appendChild(renderer.domElement);
// Controls for camera manipulation (orbit, zoom, pan)
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Adds inertia to camera movement for smoother control
// Lighting
// Ambient light provides general illumination
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
// Directional light simulates sunlight
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(5, 10, 7); // Position the light source
directionalLight.castShadow = true; // Enable shadows for this light
// Configure shadow properties for better quality (optional but recommended)
directionalLight.shadow.mapSize.width = 1024;
directionalLight.shadow.mapSize.height = 1024;
directionalLight.shadow.camera.near = 0.5;
directionalLight.shadow.camera.far = 50;
directionalLight.shadow.focus = 1; // Focus for soft shadows
scene.add(directionalLight);
// --- Add Scene Elements ---
// Ground Plane
const groundSize = 25; // The ground plane extends 12.5 feet in each direction from center
const groundGeo = new THREE.PlaneGeometry(groundSize, groundSize);
const groundMat = new THREE.MeshStandardMaterial({ color: 0x228B22 }); // ForestGreen for grass
const groundMesh = new THREE.Mesh(groundGeo, groundMat);
groundMesh.rotation.x = -Math.PI / 2; // Rotate the plane to be horizontal (Y-axis is up)
groundMesh.position.y = 0; // Place it at the base of the structure
groundMesh.receiveShadow = true; // The ground should receive shadows cast by the pergola
scene.add(groundMesh);
// --- Pergola Construction ---
// 1. Posts (4 count)
const postGeo = new THREE.BoxGeometry(postDim, postHeight, postDim); // Width, Height, Depth
// Define the positions for the four posts to form a 9ft x 6ft rectangle
const postPositions = [
{ x: -postSpanX / 2, z: -postSpanZ / 2 }, // Corner 1 (Front-Left)
{ x: postSpanX / 2, z: -postSpanZ / 2 }, // Corner 2 (Front-Right)
{ x: -postSpanX / 2, z: postSpanZ / 2 }, // Corner 3 (Back-Left)
{ x: postSpanX / 2, z: postSpanZ / 2 } // Corner 4 (Back-Right)
];
for (const pos of postPositions) {
const postMesh = new THREE.Mesh(postGeo, woodMaterial);
// Position the center of the post geometry. Since BoxGeometry is centered,
// its center is at height/2, so we set Y to postHeight/2 to place the base at Y=0.
postMesh.position.set(pos.x, postHeight / 2, pos.z);
postMesh.castShadow = true; // Posts should cast shadows
scene.add(postMesh);
}
// 2. Main Beams (2 count) - Run along the X-axis
// The beams sit on top of the posts.
// Y_top of beam = postHeight + beamHeight (top of post + beam height)
// Y_bot_center = postHeight (bottom of beam sits on top of post)
// Y_bot_end = postHeight + (1.0 - 0.35) * beamHeight (bottom edge tapers up at overhangs)
const Y_top_beam = postHeight + beamHeight;
const Y_bot_beam_center = postHeight;
const Y_bot_beam_end = postHeight + (1.0 - 0.35) * beamHeight;
// Create the geometry for one main beam
const mainBeamGeometry = createTaperedBeamGeometry(
beamTotalLength, // total length along primary axis (X)
postSpanX, // span between posts along primary axis (X)
overhang, // overhang length
Y_top_beam, // Y coordinate of the top surface
Y_bot_beam_center, // Y coordinate of the bottom surface in the center span
Y_bot_beam_end, // Y coordinate of the bottom surface at the overhang ends
beamThickness, // thickness of the beam (along Z axis)
'x', // primary axis of extrusion is X
0 // offset value for the thickness axis (Z is centered at 0)
);
// Create two main beams, one on each side of the Z-span
const beam1 = new THREE.Mesh(mainBeamGeometry, woodMaterial);
// Position beam1: centered at X=0, and offset along Z to one side of the posts.
beam1.position.set(0, 0, -postSpanZ / 2);
beam1.castShadow = true;
scene.add(beam1);
const beam2 = new THREE.Mesh(mainBeamGeometry, woodMaterial);
// Position beam2: centered at X=0, and offset along Z to the other side of the posts.
beam2.position.set(0, 0, postSpanZ / 2);
beam2.castShadow = true;
scene.add(beam2);
// 3. Rafters (7 count) - Run along the Z-axis
// Rafters are spaced along the X-axis, resting on the main beams.
// Their total length is 9ft (6ft span + 2x1.5ft overhang).
// Calculate the X positions for the 7 rafters.
// They are spaced evenly along the total length of the main beams (12ft from -6ft to 6ft).
const rafter_xPositions = [];
for (let i = 0; i < 7; i++) {
// The spacing is calculated based on the total span divided by the number of gaps (7 rafters = 6 gaps)
const xPos = -beamTotalLength / 2 + i * (beamTotalLength / 6);
rafter_xPositions.push(xPos);
}
// Define the Y coordinates for the rafters' top and bottom surfaces.
// Rafters sit on top of the main beams.
// Y_top_rafter = Y_top_beam + rafterHeight (top of beam + rafter height)
// Y_bot_rafter_center = Y_top_beam (bottom of rafter sits on top of beam)
// Y_bot_rafter_end = Y_top_beam + (1.0 - 0.35) * rafterHeight (tapered relief for overhangs)
const Y_top_rafter = Y_top_beam + rafterHeight;
const Y_bot_rafter_center = Y_top_beam;
const Y_bot_rafter_end = Y_top_beam + (1.0 - 0.35) * rafterHeight;
// Create each of the 7 rafters
for (const xPos of rafter_xPositions) {
const rafterGeo = createTaperedBeamGeometry(
rafterTotalLength, // total length along primary axis (Z)
postSpanZ, // span between beams along primary axis (Z)
overhang, // overhang length
Y_top_rafter, // Y coordinate of the top surface
Y_bot_rafter_center, // Y coordinate of the bottom surface in the center span
Y_bot_rafter_end, // Y coordinate of the bottom surface at the overhang ends
rafterThickness, // thickness of the rafter (along X axis)
'z', // primary axis of extrusion is Z
xPos // offset value for the thickness axis (X is constant for this rafter)
);
const rafterMesh = new THREE.Mesh(rafterGeo, woodMaterial);
rafterMesh.castShadow = true;
scene.add(rafterMesh);
}
// 4. Purlins (7 count) - Run along the X-axis
// Purlins are spaced along the Z-axis, resting on top of the rafters.
// Their total length is 12ft (matching the main beams).
// Calculate the Z positions for the 7 purlins.
// They are spaced evenly along the total length of the rafters (9ft from -4.5ft to 4.5ft).
const purlin_zPositions = [];
for (let i = 0; i < 7; i++) {
// The spacing is calculated based on the rafter span (9ft) divided by number of gaps (6)
const zPos = -postSpanZ / 2 + i * (postSpanZ / 6);
purlin_zPositions.push(zPos);
}
// Purlins have a simple rectangular cross-section (nominal 2x2 = 1.5/12 in all directions).
const purlinHeight = purlinDim; // Dimension along Y
const purlinLength = beamTotalLength; // Length along X (12.0 ft)
const purlinThickness = purlinDim; // Dimension along Z
// Create a standard box geometry for the purlins
const purlinGeo = new THREE.BoxGeometry(purlinLength, purlinHeight, purlinThickness);
// Create each of the 7 purlins
for (const zPos of purlin_zPositions) {
const purlinMesh = new THREE.Mesh(purlinGeo, woodMaterial);
// Position the purlins on top of the rafters.
// The center Y coordinate is the top of the rafters plus half the purlin height.
const purlinCenterY = Y_top_rafter + purlinHeight / 2;
// Position the purlin: centered at X=0, and positioned along Z according to zPos.
purlinMesh.position.set(0, purlinCenterY, zPos);
purlinMesh.castShadow = true;
scene.add(purlinMesh);
}
// --- Animation Loop ---
function animate() {
requestAnimationFrame(animate); // Request the next frame
controls.update(); // Update controls if damping is enabled
renderer.render(scene, camera); // Render the scene
}
// --- Window Resize Handler ---
function onWindowResize() {
// Update camera's aspect ratio to match the new window size
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix(); // Apply the changes to the camera projection
// Update renderer's size to match the new window size
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Add event listener for window resizing
window.addEventListener('resize', onWindowResize, false);
// Start the animation loop
animate();
}
// Call the initialization function when the script loads
init();
</script>
</body>
</html>