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>
<head>
<title>Static 3D Pergola Scene</title>
<style>
body { margin: 0; overflow: hidden; }
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>
// --- Scene Setup ---
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true; // Optional: Enable shadows for realism
document.body.appendChild(renderer.domElement);
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040, 0.8); // Soft white light
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 20, 15); // From above and slightly to the side
directionalLight.castShadow = true; // Optional: Enable shadows
// Adjust shadow properties for better results (optional)
directionalLight.shadow.mapSize.width = 1024;
directionalLight.shadow.mapSize.height = 1024;
directionalLight.shadow.camera.near = 0.5;
directionalLight.shadow.camera.far = 50;
directionalLight.shadow.camera.left = -20;
directionalLight.shadow.camera.right = 20;
directionalLight.shadow.camera.top = 20;
directionalLight.shadow.camera.bottom = -20;
scene.add(directionalLight);
// Ground
const groundGeometry = new THREE.PlaneGeometry(50, 50); // Large plane for the ground
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x8FBC8F }); // Greenish color for grass
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2; // Rotate to be horizontal
ground.receiveShadow = true; // Optional: Ground receives shadows
scene.add(ground);
// Wood Material
const woodMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513 }); // Saddle Brown color
// OrbitControls
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Smooth camera movement
controls.dampingFactor = 0.25;
controls.screenSpacePanning = false;
controls.maxPolarAngle = Math.PI / 2; // Don't go below the ground
// --- Pergola Dimensions (in feet) ---
// Actual lumber dimensions
const POST_WIDTH = 3.5 / 12;
const POST_HEIGHT = 8.0;
const POST_DEPTH = 3.5 / 12;
const BEAM_THICKNESS = 1.5 / 12; // Z-axis thickness for beam along X
const BEAM_HEIGHT = 7.25 / 12; // Y-axis height
const BEAM_OVERHANG = 1.5; // X-axis overhang
const BEAM_POST_SPAN_X = 9.0; // X distance between post centers
const BEAM_TOTAL_LENGTH = BEAM_POST_SPAN_X + 2 * BEAM_OVERHANG; // X-axis length
const RAFTER_THICKNESS = 1.5 / 12; // X-axis thickness for rafter along Z
const RAFTER_HEIGHT = 3.5 / 12; // Y-axis height
const RAFTER_OVERHANG = 1.5; // Z-axis overhang
const RAFTER_BEAM_SPAN_Z = 6.0; // Z distance between beam centers (same as post span Z)
const RAFTER_TOTAL_LENGTH = RAFTER_BEAM_SPAN_Z + 2 * RAFTER_OVERHANG; // Z-axis length
const PURLIN_DIM = 1.5 / 12; // Square dimensions
const PURLIN_RAFTER_SPAN_X = BEAM_TOTAL_LENGTH; // Purlins span the full beam length
const PURLIN_TOTAL_LENGTH = PURLIN_RAFTER_SPAN_X; // X-axis length
const TAPER_END_HEIGHT_RATIO = 0.35; // Height at the end of the overhang (35% of original)
// --- Construction ---
// Helper function to create the tapered geometry for beams and rafters
function createTaperedGeometry(length, height, thickness, overhang, taperRatio, axis) {
// length: total length (X for beam, Z for rafter)
// height: Y dimension
// thickness: Z for beam, X for rafter (extrusion depth)
// overhang: length of the tapered part
// taperRatio: height at end / original height (e.g., 0.35)
// axis: 'x' for beam (profile in XY, extrude Z), 'z' for rafter (profile in XY, extrude X, then rotate)
const shape = new THREE.Shape();
const halfLength = length / 2;
const halfHeight = height / 2;
const taperStart = halfLength - overhang;
const taperedHeight = height * taperRatio;
if (axis === 'x') {
// Profile in XY plane for extrusion along Z (Beam)
shape.moveTo(-halfLength, halfHeight); // Top-left
shape.lineTo(halfLength, halfHeight); // Top-right
shape.lineTo(halfLength, -halfHeight + (height - taperedHeight)); // Bottom-right tapered point
shape.lineTo(taperStart, -halfHeight); // Bottom-right taper start
shape.lineTo(-taperStart, -halfHeight); // Bottom-left taper start
shape.lineTo(-halfLength, -halfHeight + (height - taperedHeight)); // Bottom-left tapered point
} else if (axis === 'z') {
// Profile in XY plane for extrusion along X (Rafter - will need rotation)
// X in shape maps to Z in rafter's final orientation
// Y in shape maps to Y in rafter's final orientation
shape.moveTo(-halfLength, halfHeight); // Back-top
shape.lineTo(halfLength, halfHeight); // Front-top
shape.lineTo(halfLength, -halfHeight + (height - taperedHeight)); // Front-bottom tapered point
shape.lineTo(taperStart, -halfHeight); // Front-bottom taper start
shape.lineTo(-taperStart, -halfHeight); // Back-bottom taper start
shape.lineTo(-halfLength, -halfHeight + (height - taperedHeight)); // Back-bottom tapered point
}
const extrudeSettings = {
steps: 1,
depth: thickness,
bevelEnabled: false,
};
const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
geometry.center(); // Center the geometry pivot
if (axis === 'z') {
// Rafter geometry is created with length along X, thickness along Z.
// Need to rotate it so length is along Z, thickness along X.
geometry.rotateY(-Math.PI / 2); // Rotate around Y by -90 degrees
}
return geometry;
}
// 1. Posts
const postGeometry = new THREE.BoxGeometry(POST_WIDTH, POST_HEIGHT, POST_DEPTH);
const posts = [];
const post_x_span = BEAM_POST_SPAN_X;
const post_z_span = RAFTER_BEAM_SPAN_Z;
const post_positions = [
new THREE.Vector3(-post_x_span / 2, POST_HEIGHT / 2, -post_z_span / 2),
new THREE.Vector3(-post_x_span / 2, POST_HEIGHT / 2, post_z_span / 2),
new THREE.Vector3(post_x_span / 2, POST_HEIGHT / 2, -post_z_span / 2),
new THREE.Vector3(post_x_span / 2, POST_HEIGHT / 2, post_z_span / 2),
];
post_positions.forEach(pos => {
const post = new THREE.Mesh(postGeometry, woodMaterial);
post.position.copy(pos);
post.castShadow = true;
post.receiveShadow = true;
scene.add(post);
posts.push(post);
});
// Calculate the top of the posts for positioning beams
const postTopY = POST_HEIGHT;
// 2. Main Beams
const beamGeometry = createTaperedGeometry(BEAM_TOTAL_LENGTH, BEAM_HEIGHT, BEAM_THICKNESS, BEAM_OVERHANG, TAPER_END_HEIGHT_RATIO, 'x');
const beams = [];
const beam_z_positions = [-RAAFTER_BEAM_SPAN_Z / 2, RAFTER_BEAM_SPAN_Z / 2]; // Aligns with post Z centers
beam_z_positions.forEach(z_pos => {
const beam = new THREE.Mesh(beamGeometry, woodMaterial);
// Position on top of posts, centered in X, and at the specific Z position
beam.position.set(0, postTopY + BEAM_HEIGHT / 2, z_pos);
beam.castShadow = true;
beam.receiveShadow = true;
scene.add(beam);
beams.push(beam);
});
// Calculate the top of the beams for positioning rafters
const beamTopY = postTopY + BEAM_HEIGHT;
// 3. Rafters
const numRafters = 7;
const rafterGeometry = createTaperedGeometry(RAFTER_TOTAL_LENGTH, RAFTER_HEIGHT, RAFTER_THICKNESS, RAFTER_OVERHANG, TAPER_END_HEIGHT_RATIO, 'z');
const rafters = [];
// Rafters are distributed along the beam's X length (BEAM_TOTAL_LENGTH)
const rafterSpanX = BEAM_TOTAL_LENGTH; // The space the rafter centers are distributed over
const rafterSpacingX = rafterSpanX / (numRafters - 1);
for (let i = 0; i < numRafters; i++) {
const rafter = new THREE.Mesh(rafterGeometry, woodMaterial);
// Calculate X position: start from the far end of the beam's total length
const x_pos = -rafterSpanX / 2 + i * rafterSpacingX;
const z_pos = 0; // Centered over the Z span (RAFTER_BEAM_SPAN_Z)
const y_pos = beamTopY + RAFTER_HEIGHT / 2; // On top of beams
rafter.position.set(x_pos, y_pos, z_pos);
rafter.castShadow = true;
rafter.receiveShadow = true;
scene.add(rafter);
rafters.push(rafter);
}
// Calculate the top of the rafters for positioning purlins
const rafterTopY = beamTopY + RAFTER_HEIGHT;
// 4. Purlins
const numPurlins = 7;
const purlinGeometry = new THREE.BoxGeometry(PURLIN_TOTAL_LENGTH, PURLIN_DIM, PURLIN_DIM);
const purlins = [];
// Purlins are distributed along the rafter's Z length (RAFTER_TOTAL_LENGTH)
const purlinSpanZ = RAFTER_TOTAL_LENGTH; // The space the purlin centers are distributed over
const purlinSpacingZ = purlinSpanZ / (numPurlins - 1);
for (let i = 0; i < numPurlins; i++) {
const purlin = new THREE.Mesh(purlinGeometry, woodMaterial);
// Calculate Z position: start from the far end of the rafter's total length
const z_pos = -purlinSpanZ / 2 + i * purlinSpacingZ;
const x_pos = 0; // Centered over the X span (PURLIN_TOTAL_LENGTH)
const y_pos = rafterTopY + PURLIN_DIM / 2; // On top of rafters
purlin.position.set(x_pos, y_pos, z_pos);
purlin.castShadow = true;
purlin.receiveShadow = true;
scene.add(purlin);
purlins.push(purlin);
}
// --- Camera Positioning ---
// Position the camera to see the whole structure
camera.position.set(15, 15, 15); // Example position
camera.lookAt(0, POST_HEIGHT + BEAM_HEIGHT, 0); // Look towards the top of the structure
// Update controls target to the center of the structure
controls.target.set(0, POST_HEIGHT + BEAM_HEIGHT / 2, 0);
controls.update(); // Must call .update() after setting target
// --- Animation Loop ---
function animate() {
requestAnimationFrame(animate);
controls.update(); // required if damping enabled
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(); // Start the animation loop
</script>
</body>
</html>