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
- 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
- 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 Output as a pleasing single-file html three.js visualization
Assistant
Of course. Here is a complete, single-file HTML document that creates the 3D pergola scene using Three.js as specified.
The code includes:
- Standard scene, camera, and renderer setup.
- Grassy ground plane and appropriate lighting.
OrbitControlsfor interactive viewing.- Functions to procedurally create each component of the pergola.
- Custom
ExtrudeGeometryfor the beams and rafters to create the tapered decorative ends. - A wood texture embedded as a Base64 Data URL to ensure the project remains a single file.
How to Use
Simply copy the code below, save it as an
.htmlfile (e.g.,pergola.html), and open it in a modern web browser.<!DOCTYPE html> <html lang="en"> <head> <title>Three.js Pergola Scene</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> <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 --- let scene, camera, renderer, controls; // --- CONSTANTS & DIMENSIONS (in feet) --- const INCH = 1 / 12; // Pergola footprint const POST_SPAN_X = 9.0; const POST_SPAN_Z = 6.0; const OVERHANG = 1.5; // Component dimensions const POST_DIM = { w: 3.5 * INCH, h: 8.0, d: 3.5 * INCH }; const BEAM_DIM = { totalLength: POST_SPAN_X + 2 * OVERHANG, h: 7.25 * INCH, thickness: 1.5 * INCH }; const RAFTER_DIM = { totalLength: POST_SPAN_Z + 2 * OVERHANG, h: 3.5 * INCH, thickness: 1.5 * INCH }; const PURLIN_DIM = { totalLength: BEAM_DIM.totalLength, w: 1.5 * INCH, h: 1.5 * INCH }; // Component counts const RAFTER_COUNT = 7; const PURLIN_COUNT = 7; // Base64 encoded wood texture to keep it a single file const woodTextureData = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBAUEBAYFBQUGBgYHCQ4JCQgICRINDQoOFRIWFhUSFBQXGI_HCAoGRIDHCAgSDg8bHR8hJzUoKCcpLS0pYnc0LDX/2wBDAQoFBQUGBgonDhYhDR4hNDhNDg4dGSEdGR4hHx4hISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEh/8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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA//Z"; function init() { // Scene scene = new THREE.Scene(); scene.background = new THREE.Color(0x87ceeb); // Sky blue scene.fog = new THREE.Fog(0x87ceeb, 50, 150); // Camera camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.set(12, 8, 18); // Renderer renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap; document.body.appendChild(renderer.domElement); // Controls controls = new THREE.OrbitControls(camera, renderer.domElement); controls.target.set(0, 4, 0); // Target center of pergola base controls.update(); // Lighting const ambientLight = new THREE.AmbientLight(0xffffff, 0.6); scene.add(ambientLight); const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); directionalLight.position.set(-20, 30, 20); directionalLight.castShadow = true; directionalLight.shadow.mapSize.width = 2048; directionalLight.shadow.mapSize.height = 2048; directionalLight.shadow.camera.left = -30; directionalLight.shadow.camera.right = 30; directionalLight.shadow.camera.top = 30; directionalLight.shadow.camera.bottom = -30; scene.add(directionalLight); // Materials const textureLoader = new THREE.TextureLoader(); const woodTexture = textureLoader.load(woodTextureData); woodTexture.wrapS = THREE.RepeatWrapping; woodTexture.wrapT = THREE.RepeatWrapping; const woodMaterial = new THREE.MeshPhongMaterial({ map: woodTexture, color: 0x8B5A2B // A brown wood color }); // Create Scene Contents createGround(); createPergola(woodMaterial); // Event Listeners window.addEventListener('resize', onWindowResize, false); } function createGround() { const groundGeometry = new THREE.PlaneGeometry(200, 200); const groundMaterial = new THREE.MeshPhongMaterial({ color: 0x556B2F, side: THREE.DoubleSide }); // Olive Drab Green const ground = new THREE.Mesh(groundGeometry, groundMaterial); ground.rotation.x = -Math.PI / 2; ground.receiveShadow = true; scene.add(ground); } function createPergola(material) { const pergola = new THREE.Group(); // --- 1. Posts --- const postPositions = [ new THREE.Vector3(-POST_SPAN_X / 2, POST_DIM.h / 2, -POST_SPAN_Z / 2), new THREE.Vector3( POST_SPAN_X / 2, POST_DIM.h / 2, -POST_SPAN_Z / 2), new THREE.Vector3(-POST_SPAN_X / 2, POST_DIM.h / 2, POST_SPAN_Z / 2), new THREE.Vector3( POST_SPAN_X / 2, POST_DIM.h / 2, POST_SPAN_Z / 2), ]; const postGeometry = new THREE.BoxGeometry(POST_DIM.w, POST_DIM.h, POST_DIM.d); postPositions.forEach(pos => { const post = new THREE.Mesh(postGeometry, material); post.position.copy(pos); post.castShadow = true; post.receiveShadow = true; pergola.add(post); }); // --- 2. Main Beams --- const beamY = POST_DIM.h + BEAM_DIM.h / 2; const beamGeometry = createTaperedBeamGeometry(BEAM_DIM.totalLength, BEAM_DIM.h, OVERHANG, 0.35); const beam1 = new THREE.Mesh(beamGeometry, material); beam1.position.set(0, beamY, -POST_SPAN_Z / 2); beam1.rotation.y = -Math.PI / 2; // Orient extrusion along X-axis beam1.castShadow = true; pergola.add(beam1); const beam2 = beam1.clone(); beam2.position.set(0, beamY, POST_SPAN_Z / 2); pergola.add(beam2); // --- 3. Rafters --- const rafterY = POST_DIM.h + BEAM_DIM.h + RAFTER_DIM.h / 2; const rafterGeometry = createTaperedBeamGeometry(RAFTER_DIM.totalLength, RAFTER_DIM.h, OVERHANG, 0.35); const rafterSpacing = BEAM_DIM.totalLength / (RAFTER_COUNT - 1); for (let i = 0; i < RAFTER_COUNT; i++) { const rafter = new THREE.Mesh(rafterGeometry, material); const xPos = -BEAM_DIM.totalLength / 2 + i * rafterSpacing; rafter.position.set(xPos, rafterY, 0); rafter.castShadow = true; pergola.add(rafter); } // --- 4. Purlins --- const purlinY = POST_DIM.h + BEAM_DIM.h + RAFTER_DIM.h + PURLIN_DIM.h / 2; const purlinGeometry = new THREE.BoxGeometry(PURLIN_DIM.totalLength, PURLIN_DIM.h, PURLIN_DIM.w); const purlinSpacing = RAFTER_DIM.totalLength / (PURLIN_COUNT - 1); for (let i = 0; i < PURLIN_COUNT; i++) { const purlin = new THREE.Mesh(purlinGeometry, material); const zPos = -RAFTER_DIM.totalLength / 2 + i * purlinSpacing; purlin.position.set(0, purlinY, zPos); purlin.castShadow = true; pergola.add(purlin); } scene.add(pergola); } /** * Creates a custom geometry for a beam/rafter with tapered ends. * The shape is created in the XY plane and extruded along Z. * It will need to be rotated accordingly when used. */ function createTaperedBeamGeometry(totalLength, height, overhang, endHeightRatio) { const shape = new THREE.Shape(); const halfLength = totalLength / 2; const halfHeight = height / 2; const mainBodyLength = totalLength - 2 * overhang; const halfMainBodyLength = mainBodyLength / 2; const taperHeight = height * (1 - endHeightRatio); // Start from bottom-left corner shape.moveTo(-halfLength, -halfHeight + taperHeight); // Tapered bottom-left section shape.lineTo(-halfMainBodyLength, -halfHeight); // Flat bottom section shape.lineTo(halfMainBodyLength, -halfHeight); // Tapered bottom-right section shape.lineTo(halfLength, -halfHeight + taperHeight); // Flat top-right section shape.lineTo(halfLength, halfHeight); // Flat top section shape.lineTo(-halfLength, halfHeight); // Close path back to start shape.lineTo(-halfLength, -halfHeight + taperHeight); const extrudeSettings = { steps: 1, depth: (BEAM_DIM.thickness < RAFTER_DIM.thickness) ? BEAM_DIM.thickness : RAFTER_DIM.thickness, // Use smaller thickness for generic creation bevelEnabled: false }; const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings); // Center the geometry origin geometry.center(); return geometry; } function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); } function animate() { requestAnimationFrame(animate); controls.update(); renderer.render(scene, camera); } // --- RUN --- init(); 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.