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:
Below is a complete HTML file that creates a static 3D pergola scene using three.js. This code includes all the details specified in the problem statement.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pergola Visualization</title>
<style>
body { margin: 0; }
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();
scene.background = new THREE.Color(0x87CEEB); // Sky blue
// Camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(15, 10, 15); // Position for a good overview
camera.lookAt(0, 4, 0);
// Renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.4);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 20, 10);
scene.add(directionalLight);
// Material
const woodMaterial = new THREE.MeshLambertMaterial({ color: 0x964B00 }); // Wood color
// Function to create a tapered box (for main beams and rafters)
function createTaperedBox(length, height, thickness, overhang, taperHeight) {
const geometry = new THREE.BoxGeometry(length, height, thickness);
// Calculate taper
const taperLength = overhang;
const vertices = geometry.attributes.position.array;
for (let i = 0; i < vertices.length; i += 3) {
// Bottom face vertices have y = -height/2
if (Math.abs(vertices[i + 1] + height / 2) < 0.01) {
if (vertices[i] < -length / 2 + taperLength) {
// Left taper
const factor = (vertices[i] + length / 2) / taperLength;
vertices[i + 1] = -height / 2 * (taperHeight + (1 - taperHeight) * factor);
} else if (vertices[i] > length / 2 - taperLength) {
// Right taper
const factor = (length / 2 - vertices[i]) / taperLength;
vertices[i + 1] = -height / 2 * (taperHeight + (1 - taperHeight) * factor);
}
}
}
geometry.attributes.position.needsUpdate = true;
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, woodMaterial);
return mesh;
}
// Pergola Dimensions (in feet)
const postWidth = 3.5 / 12;
const postHeight = 8.0;
const mainBeamThickness = 1.5 / 12;
const mainBeamHeight = 7.25 / 12;
const mainBeamOverhang = 1.5;
const mainBeamLength = 9.0 + 2 * mainBeamOverhang;
const rafterThickness = 1.5 / 12;
const rafterHeight = 3.5 / 12;
const rafterOverhang = 1.5;
const rafterLength = 6.0 + 2 * rafterOverhang;
const purlinDimension = 1.5 / 12;
// Posts
const postPositions = [
[-9 / 2, 0, -6 / 2],
[9 / 2, 0, -6 / 2],
[9 / 2, 0, 6 / 2],
[-9 / 2, 0, 6 / 2]
];
postPositions.forEach(([x, y, z]) => {
const post = new THREE.Mesh(new THREE.BoxGeometry(postWidth, postHeight, postWidth), woodMaterial);
post.position.set(x, postHeight / 2, z);
scene.add(post);
});
// Main Beams
const mainBeamTaperHeight = 0.35; // 35% of original height
const mainBeams = [
createTaperedBox(mainBeamLength, mainBeamHeight, mainBeamThickness, mainBeamOverhang, mainBeamTaperHeight),
createTaperedBox(mainBeamLength, mainBeamHeight, mainBeamThickness, mainBeamOverhang, mainBeamTaperHeight)
];
mainBeams[0].position.set(0, postHeight + mainBeamHeight / 2, -6 / 2);
mainBeams[1].position.set(0, postHeight + mainBeamHeight / 2, 6 / 2);
mainBeams.forEach(beam => scene.add(beam));
// Rafters
const rafterTaperHeight = 0.35; // 35% of original height
const rafterSpacing = (mainBeamLength - 2 * mainBeamOverhang) / 6; // 7 rafters, 6 gaps
for (let i = 0; i < 7; i++) {
const x = -mainBeamLength / 2 + mainBeamOverhang + i * rafterSpacing;
const rafter = createTaperedBox(rafterLength, rafterHeight, rafterThickness, rafterOverhang, rafterTaperHeight);
rafter.rotation.z = Math.PI / 2; // Rotate to align with Z-axis
rafter.position.set(x, postHeight + mainBeamHeight + rafterHeight / 2, 0);
scene.add(rafter);
}
// Purlins
const purlinSpacing = (rafterLength - 2 * rafterOverhang) / 6; // 7 purlins, 6 gaps
for (let i = 0; i < 7; i++) {
const z = -rafterLength / 2 + rafterOverhang + i * purlinSpacing;
const purlin = new THREE.Mesh(new THREE.BoxGeometry(mainBeamLength, purlinDimension, purlinDimension), woodMaterial);
purlin.position.set(0, postHeight + mainBeamHeight + rafterHeight + purlinDimension / 2, z);
scene.add(purlin);
}
// Ground
const groundGeometry = new THREE.PlaneGeometry(50, 50);
const groundMaterial = new THREE.MeshLambertMaterial({ color: 0x32CD32 }); // Grassy green
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.1; // Slightly below the posts
scene.add(ground);
// OrbitControls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(0, 4, 0); // Focus on the pergola
controls.update();
// Animation Loop
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
This HTML file creates a 3D pergola scene with the specified dimensions and design. You can rotate, zoom, and pan the view using the mouse or trackpad thanks to the OrbitControls integration.
Key Features:
Open this file in a web browser to view the pergola visualization. Ensure you have an active internet connection to load the three.js libraries. Enjoy exploring your 3D pergola design!