Create a static 3D pergola scene using three.js. Use these imports: <script…
- User
Create a static 3D pergola scene using three.js.
Use these imports:
All dimensions are in feet. Y-axis is up.
Scene Setup:
- Ground: grassy plain
- Lighting: ambient light and directional light
- Material: wood for all pergola parts
- Camera: Position for a good overview
Pergola Dimensions & Components (Actual lumber dimensions in feet):
- Post dimensions (nominal 4x4): width/depth = 3.5/12, height = 8.0.
- Main Beam dimensions (nominal 2x8): thickness (Z-axis for beam along X) = 1.5/12, height (Y-axis) = 7.25/12. Overhang = 1.5.
- Rafter dimensions (nominal 2x4): thickness (X-axis for rafter along Z) = 1.5/12, height (Y-axis) = 3.5/12. Overhang = 1.5.
- Purlin dimensions (nominal 2x2): width/height/depth = 1.5/12.
Construction:
-
Posts (4 count):
- Calculate position so as to create a 9ft (X) by 6ft (Z) rectangle.
-
Main Beams (2 count):
- Calculate position so as to ride on top of posts
- Total Length: 9.0 (post span) + 2 * 1.5 (overhangs) = 12.0.
- Height: 7.25/12. Thickness: 1.5/12. Overhang: 1.5.
- Cut a tapered relief on the bottom face of each 1.5 overhang, reducing the lumber height to 35% of original by the end of each overhang to create an upward sweep profile, keeping the top face flat.
-
Rafters (7 count):
- Calculate positions so as to ride on top of main beams, with end rafters aligning with the ends of the main beam ends.
- Spacing: Distribute 7 rafters evenly along the 12.0 ft X-length of main beams
- Total Length: 6.0 (main beam span based on post Z-centers) + 2 * 1.5 (overhangs) = 9.0.
- Height: 3.5/12. Thickness: 1.5/12. Overhang: 1.5.
- Cut a tapered relief on the bottom face of each 1.5 overhang, reducing the lumber height to 35% of original by the end of each overhang to create an upward sweep profile, keeping the top face flat.
-
Purlins (7 count):
- Calculate positions so as to ride on top of rafters, with end purlins aligning with the ends of the rafter ends.
- Spacing: Distribute 7 purlins evenly along the 9.0 ft Z-length of rafters.
- Dimensions: Length = 12.0. Height = 1.5/12. Depth = 1.5/12.
Add OrbitControls for ease of viewing. You may add visual embellishments but will be penalized if you add anything distracting from the main pergola design. Output the pergola you designed as a three.js visualization in a single html file.
Assistant
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>3D Pergola</title> <style> body { margin: 0; overflow: hidden; background-color: #f0f0f0; } 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); document.body.appendChild(renderer.domElement); const controls = new OrbitControls(camera, renderer.domElement); // --- Lighting --- const ambientLight = new THREE.AmbientLight(0xffffff, 0.7); // Soft white light scene.add(ambientLight); const directionalLight = new THREE.DirectionalLight(0xffffff, 0.9); directionalLight.position.set(5, 10, 7.5); // Position the light directionalLight.castShadow = true; // Optional: enables shadow casting scene.add(directionalLight); // --- Materials --- // Using a simple brown color for wood const woodMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513 }); // SaddleBrown // --- Constants and Conversions --- const IN_TO_FT = 1 / 12.0; // Pergola Dimensions (in feet) const PERGOLA_LENGTH_FT = 9.0; // Along X-axis const PERGOLA_WIDTH_FT = 6.0; // Along Z-axis // Post (nominal 4x4) const POST_WIDTH_IN = 3.5; const POST_HEIGHT_FT = 8.0; const POST_THICKNESS_IN = 3.5; // Same as width for square post const postWidth = POST_WIDTH_IN * IN_TO_FT; const postHeight = POST_HEIGHT_FT; const postThickness = POST_THICKNESS_IN * IN_TO_FT; // Main Beam (nominal 2x8) const BEAM_HEIGHT_IN = 7.25; const BEAM_THICKNESS_IN = 1.5; // Thickness along Z-axis for beams along X const BEAM_OVERHANG_FT = 1.5; const beamHeight = BEAM_HEIGHT_IN * IN_TO_FT; const beamThickness = BEAM_THICKNESS_IN * IN_TO_FT; const beamOverhang = BEAM_OVERHANG_FT; const beamSpan = PERGOLA_LENGTH_FT; // Span between posts (X) const beamTotalLength = beamSpan + 2 * beamOverhang; // Total length including overhangs (X) // Rafter (nominal 2x4) const RAFTER_HEIGHT_IN = 3.5; const RAFTER_THICKNESS_IN = 1.5; // Thickness along X-axis for rafters along Z const RAFTER_OVERHANG_FT = 1.5; const rafterHeight = RAFTER_HEIGHT_IN * IN_TO_FT; const rafterThickness = RAFTER_THICKNESS_IN * IN_TO_FT; const rafterOverhang = RAFTER_OVERHANG_FT; const rafterSpan = PERGOLA_WIDTH_FT; // Span between beams (Z) const rafterTotalLength = rafterSpan + 2 * rafterOverhang; // Total length including overhangs (Z) // Purlin (nominal 2x2) const PURLIN_DIM_IN = 1.5; const purlinDim = PURLIN_DIM_IN * IN_TO_FT; // Height and Depth const taperRatio = 0.35; // Height reduces to 35% at overhang ends // --- Geometry Creation Helper --- // Creates a prism with tapered ends. Can be used for beams (axis='x') or rafters (axis='z'). function createTaperedPrism(axis, baseY, length, thickness, height, overhang, taperRatio, segments) { const geometry = new THREE.BufferGeometry(); const vertices = []; const indices = []; const uvs = []; const segLength = length / (2 * segments); // Length of each tapered segment const coordsAlongLength = []; // Stores positions along the main axis (X for beams, Z for rafters) for (let i = 0; i < 2 * segments + 1; i++) { coordsAlongLength.push(-length / 2.0 + i * segLength); } const yBottomCoords = []; // Stores the Y coordinate of the bottom face for each slice const yTopCoords = []; // Stores the Y coordinate of the top face for each slice for (let i = 0; i < coordsAlongLength.length; i++) { const currentAlongLengthPos = coordsAlongLength[i]; let yBottom, yTop; // Determine if this slice is in the overhang section if (Math.abs(currentAlongLengthPos) >= length / 2.0 - segLength) { // Overhang section: bottom tapers yBottom = baseY + taperRatio * height; } else { // Main section: bottom is flat at baseY yBottom = baseY; } // Top face is always flat at baseY + height yTop = baseY + height; yBottomCoords.push(yBottom); yTopCoords.push(yTop); } // Define coordinate mappings based on the specified axis let mainAxisCoords, minorAxisCoords, thicknessCoords; if (axis === 'x') { // Beam along X mainAxisCoords = coordsAlongLength; // X coordinates minorAxisCoords = yBottomCoords; // Y coordinates thicknessCoords = [-thickness / 2.0, thickness / 2.0]; // Z coordinates for thickness } else if (axis === 'z') { // Rafter along Z mainAxisCoords = coordsAlongLength; // Z coordinates minorAxisCoords = yBottomCoords; // Y coordinates thicknessCoords = [-thickness / 2.0, thickness / 2.0]; // X coordinates for thickness } else { throw new Error("Axis must be 'x' or 'z'"); } // Helper function to get 3D coordinates based on axis const getVertexPosition = (posAlongMainAxis, posAlongMinorAxis, thicknessVal) => { const p = [0, 0, 0]; if (axis === 'x') { p[0] = posAlongMainAxis; p[1] = posAlongMinorAxis; p[2] = thicknessVal; } else { // axis === 'z' p[0] = thicknessVal; p[1] = posAlongMinorAxis; p[2] = posAlongMainAxis; } return p; }; let vertexIndex = 0; // Generate vertices and UVs for each slice along the main axis for (let i = 0; i < mainAxisCoords.length; i++) { const currentMainPos = mainAxisCoords[i]; const currentYBottom = minorAxisCoords[i]; const currentYTop = yTopCoords[i]; // This is constant for the top face // U coordinate maps along the length of the prism, V maps along height (0=bottom, 1=top) const u_start = i / (mainAxisCoords.length - 1); const u_end = (i + 1) / (mainAxisCoords.length - 1); // Vertex definition for slice i (ordered for creating faces) // v[i][0] Top-left (min thickness coord) // v[i][1] Top-right (max thickness coord) // v[i][2] Bottom-right (max thickness coord) // v[i][3] Bottom-left (min thickness coord) // Top-left vertex (v[i][0]) let p = getVertexPosition(currentMainPos, currentYTop, thicknessCoords[0]); vertices.push(...p); uvs.push(u_start, 0.0); // UV for top face // Top-right vertex (v[i][1]) p = getVertexPosition(currentMainPos, currentYTop, thicknessCoords[1]); vertices.push(...p); uvs.push(u_end, 0.0); // UV for top face // Bottom-right vertex (v[i][2]) p = getVertexPosition(currentMainPos, currentYBottom, thicknessCoords[1]); vertices.push(...p); uvs.push(u_end, 1.0); // UV for bottom face // Bottom-left vertex (v[i][3]) p = getVertexPosition(currentMainPos, currentYBottom, thicknessCoords[0]); vertices.push(...p); uvs.push(u_start, 1.0); // UV for bottom face // Generate faces connecting slice i to slice i+1 if (i < mainAxisCoords.length - 1) { const nextIndex = (i + 1) * 4; const currentIndex = i * 4; // Faces (quads, represented by two triangles each): // Face 1: Top surface (UV V=0) indices.push(currentIndex + 0, currentIndex + 1, nextIndex + 1); // TRI1 indices.push(currentIndex + 0, nextIndex + 1, nextIndex + 0); // TRI2 // Face 2: Bottom surface (UV V=1) indices.push(currentIndex + 3, currentIndex + 2, nextIndex + 2); // TRI1 indices.push(currentIndex + 3, nextIndex + 2, nextIndex + 3); // TRI2 // Face 3: Left side (min thickness, UV V maps height) indices.push(currentIndex + 0, currentIndex + 3, nextIndex + 3); // TRI1 indices.push(currentIndex + 0, nextIndex + 3, nextIndex + 0); // TRI2 // Face 4: Right side (max thickness, UV V maps height) indices.push(currentIndex + 1, nextIndex + 1, nextIndex + 2); // TRI1 indices.push(currentIndex + 1, nextIndex + 2, currentIndex + 2); // TRI2 } } geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)); geometry.setIndex(indices); geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2)); geometry.computeVertexNormals(); // Calculate normals for lighting return new THREE.Mesh(geometry, woodMaterial); } // --- Create Pergola Components --- // 1. Posts const postCenterOffsetY = postHeight / 2.0; const postXPositions = [-PERGOLA_LENGTH_FT / 2.0, PERGOLA_LENGTH_FT / 2.0]; // +/- 4.5 const postZPositions = [-PERGOLA_WIDTH_FT / 2.0, PERGOLA_WIDTH_FT / 2.0]; // +/- 3.0 function createPost(x, z) { const geometry = new THREE.BoxGeometry(postWidth, postHeight, postThickness); const mesh = new THREE.Mesh(geometry, woodMaterial); mesh.position.set(x, postCenterOffsetY, z); scene.add(mesh); } createPost(postXPositions[0], postZPositions[0]); createPost(postXPositions[1], postZPositions[0]); createPost(postXPositions[0], postZPositions[1]); createPost(postXPositions[1], postZPositions[1]); // 2. Main Beams (along X) const beamZOffset = (postWidth / 2.0) + (beamThickness / 2.0); // Offset from post center line const beamCenterY = postHeight + beamHeight / 2.0; // Beam bottom sits on post top (Y=postHeight) const numBeamSegments = 2; // 2 segments for the two overhangs // Beam 1 (front) const beam1 = createTaperedPrism('x', postHeight, beamTotalLength, beamThickness, beamHeight, beamOverhang, taperRatio, numBeamSegments); beam1.position.set(0, beamCenterY, postZPositions[0] - beamZOffset); // Positioned along X, offset in Z scene.add(beam1); // Beam 2 (back) const beam2 = createTaperedPrism('x', postHeight, beamTotalLength, beamThickness, beamHeight, beamOverhang, taperRatio, numBeamSegments); beam2.position.set(0, beamCenterY, postZPositions[1] + beamZOffset); // Positioned along X, offset in Z scene.add(beam2); // 3. Rafters (along Z) const rafterXSpacing = beamTotalLength / 6.0; // 7 rafters = 6 gaps over the 12ft beam length const rafterXPositions = Array.from({ length: 7 }, (_, i) => -beamTotalLength / 2.0 + i * rafterXSpacing); // X positions for rafters const rafterBaseY = postHeight + beamHeight; // Top surface Y of the main beams const rafterCenterY = rafterBaseY + rafterHeight / 2.0; // Rafter center Y const numRafterSegments = 2; // 2 segments for the two overhangs for (let i = 0; i < 7; i++) { const rafter = createTaperedPrism('z', rafterBaseY, rafterTotalLength, rafterThickness, rafterHeight, rafterOverhang, taperRatio, numRafterSegments); // Rafters run along Z, their X position determines spacing rafter.position.set(rafterXPositions[i], rafterCenterY, 0); scene.add(rafter); } // 4. Purlins (along X) const purlinZSpacing = rafterTotalLength / 6.0; // 7 purlins = 6 gaps over the 9ft rafter span const purlinZPositions = Array.from({ length: 7 }, (_, i) => -rafterTotalLength / 2.0 + i * purlinZSpacing); // Z positions for purlins const purlinBaseY = rafterBaseY + rafterHeight; // Top surface Y of the rafters const purlinCenterY = purlinBaseY + purlinDim / 2.0; // Purlin center Y function createPurlin(x, y, z) { const geometry = new THREE.BoxGeometry(PERGOLA_LENGTH_FT, purlinDim, purlinDim); // Purlin length is PERGOLA_LENGTH_FT, not beamTotalLength! Check prompt. // Prompt states "Purlin dimensions: Length = 12.0." This corresponds to beamTotalLength. const adjustedGeometry = new THREE.BoxGeometry(beamTotalLength, purlinDim, purlinDim); // Use beamTotalLength for purlin length along X. const mesh = new THREE.Mesh(adjustedGeometry, woodMaterial); mesh.position.set(x, y, z); scene.add(mesh); } // Create 7 purlins, spaced along Z, centered along X for (let i = 0; i < 7; i++) { createPurlin(0, purlinCenterY, purlinZPositions[i]); } // --- Ground --- const groundGeometry = new THREE.PlaneGeometry(100, 100); const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x556B2F }); // DarkOliveGreen const ground = new THREE.Mesh(groundGeometry, groundMaterial); ground.rotation.x = -Math.PI / 2; // Rotate to be horizontal ground.position.y = -0.01; // Slightly below pergola base to ensure it's visible scene.add(ground); // --- Camera Positioning --- camera.position.set(20, 18, 20); controls.target.set(0, 4, 0); // Target the approximate center of the pergola structure controls.update(); // --- Animation Loop --- function animate() { requestAnimationFrame(animate); controls.update(); // Only required if controls.enableDamping or controls.autoRotate are set to true renderer.render(scene, camera); } // Handle window resizing window.addEventListener('resize', () => { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); }); animate(); </script> </body> </html>
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.