import * as THREE from 'three'; import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js'; import { brickMaps, woodMaps, brassMaps, rubberMaps } from './textures.js'; export function createEnvironment(scene) { const env = new THREE.Group(); env.name = 'environment'; scene.add(env); // ---------------- sky dome ---------------- const skyGeo = new THREE.SphereGeometry(160, 48, 32); const skyMat = new THREE.ShaderMaterial({ side: THREE.BackSide, depthWrite: false, uniforms: { topColor: { value: new THREE.Color(0x35526f) }, midColor: { value: new THREE.Color(0x9fb0bc) }, horizonColor: { value: new THREE.Color(0xf2c79a) }, sunColor: { value: new THREE.Color(0xffd9a2) }, sunDir: { value: new THREE.Vector3(0.45, 0.32, 0.82).normalize() }, }, vertexShader: ` varying vec3 vWorld; void main(){ vec4 wp = modelMatrix * vec4(position,1.0); vWorld = wp.xyz; gl_Position = projectionMatrix * viewMatrix * wp; }`, fragmentShader: ` varying vec3 vWorld; uniform vec3 topColor, midColor, horizonColor, sunColor, sunDir; void main(){ vec3 d = normalize(vWorld); float h = clamp(d.y, -0.2, 1.0); vec3 col = mix(horizonColor, midColor, smoothstep(0.0, 0.24, h)); col = mix(col, topColor, smoothstep(0.18, 0.75, h)); float sun = pow(max(dot(d, sunDir), 0.0), 180.0); col += sunColor * sun * 1.4; float glow = pow(max(dot(d, sunDir), 0.0), 8.0); col += sunColor * glow * 0.16; // subtle haze band col = mix(col, vec3(0.93,0.76,0.62), (1.0 - smoothstep(0.0,0.12,abs(h-0.02))) * 0.16); gl_FragColor = vec4(col, 1.0); }`, }); const sky = new THREE.Mesh(skyGeo, skyMat); env.add(sky); // ---------------- ground ---------------- const courtyardMaps = brickMaps(17, 17, 'warm'); const courtyard = new THREE.Mesh( new THREE.PlaneGeometry(44, 44), new THREE.MeshStandardMaterial({ ...courtyardMaps, roughness: 0.9, metalness: 0.02, bumpScale: 0.06, color: 0xbcaa99, }) ); courtyard.rotation.x = -Math.PI / 2; courtyard.position.y = 0.001; courtyard.receiveShadow = true; env.add(courtyard); // outer darker ground / street const outerMaps = brickMaps(60, 60, 'wall'); const outer = new THREE.Mesh( new THREE.PlaneGeometry(220, 220), new THREE.MeshStandardMaterial({ ...outerMaps, color: 0x6b5b4a, roughness: 1.0, bumpScale: 0.02 }) ); outer.rotation.x = -Math.PI / 2; outer.position.y = -0.012; outer.receiveShadow = true; env.add(outer); // stone border around courtyard const borderMat = new THREE.MeshStandardMaterial({ color: 0x6f6a62, roughness: 0.7, metalness: 0.05, }); for (const [x, z, w, d] of [[0, 22.2, 44.6, 0.5], [0, -22.2, 44.6, 0.5], [22.2, 0, 0.5, 44.6], [-22.2, 0, 0.5, 44.6]]) { const b = new THREE.Mesh(new RoundedBoxGeometry(w, 0.16, d, 2, 0.03), borderMat); b.position.set(x, 0.07, z); b.receiveShadow = true; env.add(b); } // ---------------- low brick walls ---------------- const wallMaps = brickMaps(12, 2, 'wall'); const wallMat = new THREE.MeshStandardMaterial({ ...wallMaps, roughness: 0.9, bumpScale: 0.05, color: 0xc9b6a6 }); const capMat = new THREE.MeshStandardMaterial({ color: 0x8b8278, roughness: 0.7 }); function wall(len, x, z, rotY) { const g = new THREE.Group(); const base = new THREE.Mesh(new THREE.BoxGeometry(len, 1.25, 0.55), wallMat); base.position.y = 0.625; base.castShadow = base.receiveShadow = true; g.add(base); // brick pillar caps for (let i = -1; i <= 1; i += 2) { const p = new THREE.Mesh(new THREE.BoxGeometry(0.8, 1.55, 0.8), wallMat); p.position.set(i * len * 0.5, 0.775, 0); p.castShadow = p.receiveShadow = true; g.add(p); const cap = new THREE.Mesh(new RoundedBoxGeometry(0.95, 0.13, 0.95, 2, 0.03), capMat); cap.position.set(i * len * 0.5, 1.62, 0); cap.castShadow = true; g.add(cap); } const top = new THREE.Mesh(new RoundedBoxGeometry(len, 0.12, 0.68, 2, 0.03), capMat); top.position.y = 1.31; top.castShadow = top.receiveShadow = true; g.add(top); g.position.set(x, 0, z); g.rotation.y = rotY; env.add(g); } wall(13, 0, -18.5, 0); wall(13, -18.5, -2, Math.PI / 2); wall(13, 18.5, -2, Math.PI / 2); // ---------------- distant buildings ---------------- const facadeMaps = brickMaps(9, 6, 'wall'); const facadeMat = new THREE.MeshStandardMaterial({ ...facadeMaps, roughness: 0.92, color: 0xb9a89a }); const roofMat = new THREE.MeshStandardMaterial({ color: 0x4a4440, roughness: 0.8 }); const windowMat = new THREE.MeshStandardMaterial({ color: 0x1e2a30, emissive: 0x2a1c10, emissiveIntensity: 0.35, roughness: 0.25, metalness: 0.4, }); const buildings = [ { x: -30, z: -34, w: 15, h: 7.5, d: 11, rot: 0.1 }, { x: 5, z: -42, w: 20, h: 9, d: 12, rot: -0.05 }, { x: 34, z: -28, w: 11, h: 6.5, d: 11, rot: -0.2 }, { x: -38, z: 10, w: 11, h: 8, d: 18, rot: 0.08 }, { x: 38, z: 16, w: 13, h: 6.2, d: 15, rot: 0.05 }, ]; for (const b of buildings) { const g = new THREE.Group(); const base = new THREE.Mesh(new THREE.BoxGeometry(b.w, b.h, b.d), facadeMat); base.position.y = b.h / 2; base.castShadow = base.receiveShadow = true; g.add(base); // pitched roof const roofGeo = new THREE.ConeGeometry(Math.max(b.w, b.d) * 0.78, 2.4, 4); roofGeo.rotateY(Math.PI / 4); const roof = new THREE.Mesh(roofGeo, roofMat); roof.scale.set(b.w / Math.max(b.w, b.d), 1, b.d / Math.max(b.w, b.d)); roof.position.y = b.h + 1.2; roof.rotation.y = 0.02; g.add(roof); // windows on all four facades const frameMat = new THREE.MeshStandardMaterial({ color: 0x8e8274, roughness: 0.7 }); const faces = [ { w: b.w, rot: 0, ox: 0, oz: b.d / 2 }, { w: b.w, rot: Math.PI, ox: 0, oz: -b.d / 2 }, { w: b.d, rot: Math.PI / 2, ox: b.w / 2, oz: 0 }, { w: b.d, rot: -Math.PI / 2, ox: -b.w / 2, oz: 0 }, ]; for (const f of faces) { const cols = Math.max(2, Math.floor(f.w / 2.7)); const rows = Math.max(2, Math.floor((b.h - 1.5) / 2.5)); for (let c = 0; c < cols; c++) { for (let r = 0; r < rows; r++) { const frame = new THREE.Mesh(new RoundedBoxGeometry(1.15, 1.55, 0.12, 2, 0.02), frameMat); frame.position.set(0, 0, 0); frame.rotation.y = f.rot; const cx = -f.w / 2 + f.w / cols * (c + 0.5); const win = new THREE.Mesh(new THREE.PlaneGeometry(0.9, 1.2), windowMat); win.position.z = 0.07; frame.add(win); const holder = new THREE.Group(); holder.add(frame); holder.position.set(f.ox, 1.8 + r * 2.35, f.oz); holder.position.x += Math.cos(f.rot) * cx; holder.position.z -= Math.sin(f.rot) * cx; // push frame out along facade normal holder.position.x += Math.sin(f.rot) * 0.03; holder.position.z += Math.cos(f.rot) * 0.03; g.add(holder); } } } g.position.set(b.x, 0, b.z); g.rotation.y = b.rot; env.add(g); } // ---------------- props (outside subject) ---------------- // fire hydrant { const g = new THREE.Group(); const red = new THREE.MeshStandardMaterial({ color: 0x9b1d16, roughness: 0.42, metalness: 0.35 }); const brass = new THREE.MeshStandardMaterial({ color: 0xb08b3c, roughness: 0.35, metalness: 0.9 }); const body = new THREE.Mesh(new THREE.CylinderGeometry(0.16, 0.2, 0.62, 14), red); body.position.y = 0.38; g.add(body); const dome = new THREE.Mesh(new THREE.SphereGeometry(0.17, 14, 8, 0, Math.PI * 2, 0, Math.PI / 2), red); dome.position.y = 0.69; g.add(dome); const cap = new THREE.Mesh(new THREE.CylinderGeometry(0.035, 0.05, 0.1, 8), brass); cap.position.y = 0.84; g.add(cap); for (const a of [0, Math.PI]) { const arm = new THREE.Mesh(new THREE.CylinderGeometry(0.055, 0.055, 0.16, 10), brass); arm.rotation.z = Math.PI / 2; arm.position.set(Math.cos(a) * 0.2, 0.54, Math.sin(a) * 0.2); g.add(arm); } const flange = new THREE.Mesh(new THREE.CylinderGeometry(0.26, 0.28, 0.08, 14), red); flange.position.y = 0.04; g.add(flange); g.traverse((o) => { if (o.isMesh) { o.castShadow = true; o.receiveShadow = true; } }); g.position.set(6.2, 0, 5.4); g.rotation.y = 0.5; env.add(g); } // wooden crate / bucket { const wood = new THREE.MeshStandardMaterial({ ...woodMaps(1, 1), roughness: 0.75 }); const bucket = new THREE.Group(); const body = new THREE.Mesh(new THREE.CylinderGeometry(0.28, 0.22, 0.38, 16, 1, true), wood); body.material.side = THREE.DoubleSide; bucket.add(body); const bottom = new THREE.Mesh(new THREE.CircleGeometry(0.22, 16), wood); bottom.rotation.x = -Math.PI / 2; bottom.position.y = -0.19; bucket.add(bottom); for (const y of [-0.12, 0.14]) { const hoop = new THREE.Mesh(new THREE.TorusGeometry(0.255, 0.012, 6, 24), new THREE.MeshStandardMaterial({ color: 0x3b3b3b, metalness: 0.8, roughness: 0.5 })); hoop.rotation.x = Math.PI / 2; hoop.position.y = y; bucket.add(hoop); } bucket.traverse((o) => { if (o.isMesh) { o.castShadow = true; o.receiveShadow = true; } }); bucket.position.set(-5.2, 0.19, 5.0); bucket.rotation.y = 0.7; env.add(bucket); } // cobbles & grass tufts along wall base (instanced) const cobbleGeo = new THREE.DodecahedronGeometry(0.12, 0); const cobbleMat = new THREE.MeshStandardMaterial({ color: 0x7a746b, roughness: 0.9 }); const cobbles = new THREE.InstancedMesh(cobbleGeo, cobbleMat, 160); const cd = new THREE.Object3D(); for (let i = 0; i < 160; i++) { const edge = i % 4; let x, z; if (edge === 0) { x = (Math.random() - 0.5) * 36; z = -17.6 - Math.random() * 0.6; } else if (edge === 1) { x = -17.6 - Math.random() * 0.6; z = (Math.random() - 0.5) * 34; } else if (edge === 2) { x = 17.6 + Math.random() * 0.6; z = (Math.random() - 0.5) * 34; } else { x = (Math.random() - 0.5) * 36; z = 20.8 + Math.random() * 0.4; } cd.position.set(x, 0.03 + Math.random() * 0.02, z); cd.rotation.set(Math.random() * 3, Math.random() * 3, Math.random() * 3); cd.scale.set(1, 0.5 + Math.random() * 0.4, 1); cd.updateMatrix(); cobbles.setMatrixAt(i, cd.matrix); } cobbles.receiveShadow = true; env.add(cobbles); const grassGeo = new THREE.ConeGeometry(0.055, 0.34, 4); grassGeo.translate(0, 0.17, 0); const grassMat = new THREE.MeshStandardMaterial({ color: 0x5d6b3a, roughness: 0.9 }); const grass = new THREE.InstancedMesh(grassGeo, grassMat, 110); for (let i = 0; i < 110; i++) { let x, z; const edge = i % 4; if (edge === 0) { x = (Math.random() - 0.5) * 34; z = -17.8 + Math.random() * 0.5; } else if (edge === 1) { x = -17.8 + Math.random() * 0.5; z = (Math.random() - 0.5) * 32; } else if (edge === 2) { x = 17.8 - Math.random() * 0.5; z = (Math.random() - 0.5) * 32; } else { x = (Math.random() - 0.5) * 34; z = 20.9; } cd.position.set(x, 0, z); cd.rotation.set(0, Math.random() * 6.28, (Math.random() - 0.5) * 0.3); cd.scale.setScalar(0.6 + Math.random() * 0.9); cd.updateMatrix(); grass.setMatrixAt(i, cd.matrix); } grass.castShadow = true; env.add(grass); // hedge line just beyond the courtyard wall const hedgeMat = new THREE.MeshStandardMaterial({ color: 0x36452a, roughness: 1.0 }); const hedgeGeo = new THREE.IcosahedronGeometry(1, 1); const hedge = new THREE.InstancedMesh(hedgeGeo, hedgeMat, 110); const hd = new THREE.Object3D(); for (let i = 0; i < 110; i++) { const a = (i / 110) * Math.PI * 2; const rad = 24.5 + Math.random() * 3.5; hd.position.set(Math.cos(a) * rad, 0.75 + Math.random() * 0.3, Math.sin(a) * rad); hd.scale.set(1.4 + Math.random(), 0.95 + Math.random() * 0.45, 1.4 + Math.random()); hd.rotation.set(Math.random(), Math.random() * 3, Math.random()); hd.updateMatrix(); hedge.setMatrixAt(i, hd.matrix); } hedge.castShadow = true; hedge.receiveShadow = true; env.add(hedge); // distant tree line / hedge masking the horizon const treeMat = new THREE.MeshStandardMaterial({ color: 0x2f3a22, roughness: 1.0 }); const trunkMat = new THREE.MeshStandardMaterial({ color: 0x3a2a1c, roughness: 1.0 }); const treeGeo = new THREE.IcosahedronGeometry(1, 1); const trunkGeo = new THREE.CylinderGeometry(0.16, 0.22, 2.0, 6); const trees = new THREE.Group(); for (let i = 0; i < 78; i++) { const a = (i / 46) * Math.PI * 2 + Math.random() * 0.1; const rad = 48 + Math.random() * 24; const x = Math.cos(a) * rad, z = Math.sin(a) * rad; const h = 3.0 + Math.random() * 2.2; const trunk = new THREE.Mesh(trunkGeo, trunkMat); trunk.position.set(x, 1, z); trunk.scale.setScalar(0.8 + Math.random() * 0.5); trees.add(trunk); const crown = new THREE.Mesh(treeGeo, treeMat); crown.position.set(x, 2.3 + h * 0.55, z); crown.scale.set(2.0 + Math.random() * 1.6, h * 0.7, 2.0 + Math.random() * 1.6); trees.add(crown); } trees.traverse(o => { if (o.isMesh) { o.castShadow = false; o.receiveShadow = false; } }); env.add(trees); // ---------------- lights ---------------- const hemi = new THREE.HemisphereLight(0xaccbe4, 0x6a4c38, 0.55); scene.add(hemi); const sun = new THREE.DirectionalLight(0xffd2a1, 2.55); sun.position.set(13, 17, 9); sun.castShadow = true; sun.shadow.mapSize.set(2048, 2048); sun.shadow.camera.left = -11; sun.shadow.camera.right = 11; sun.shadow.camera.top = 11; sun.shadow.camera.bottom = -11; sun.shadow.camera.near = 1; sun.shadow.camera.far = 55; sun.shadow.bias = -0.0006; sun.shadow.normalBias = 0.02; sun.shadow.radius = 3; scene.add(sun); scene.add(sun.target); const fill = new THREE.DirectionalLight(0x8fb4d8, 0.85); fill.position.set(-12, 8, -10); scene.add(fill); const rim = new THREE.DirectionalLight(0xffb072, 0.7); rim.position.set(-6, 5, 11); scene.add(rim); // warm fill near the truck front const bounce = new THREE.PointLight(0xffc88f, 8, 12, 2); bounce.position.set(4, 2.2, 4.5); scene.add(bounce); scene.fog = new THREE.FogExp2(0xc9a988, 0.009); scene.background = new THREE.Color(0xd9b995); return env; }