import * as THREE from 'three'; import { groundTextures, barkTextures, cloudTexture } from './textures.js'; function rng(seed = 1) { let s = seed >>> 0; return () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; }; } const R = rng(2027); /* ------------- visible sky dome + sun ------------- */ export function makeSky(sunDir) { const geo = new THREE.SphereGeometry(380, 40, 24); const uniforms = { sunDir: { value: sunDir.clone().normalize() }, top: { value: new THREE.Color(0x4a7cb0) }, mid: { value: new THREE.Color(0xb9c4c6) }, horizon: { value: new THREE.Color(0xf3c795) }, below: { value: new THREE.Color(0x8a765c) }, }; const mat = new THREE.ShaderMaterial({ side: THREE.BackSide, depthWrite: false, fog: false, uniforms, vertexShader: `varying vec3 vDir; void main(){ vDir = normalize(position); gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }`, fragmentShader: ` varying vec3 vDir; uniform vec3 sunDir, top, mid, horizon, below; void main(){ float h = clamp(vDir.y, -1.0, 1.0); vec3 col = mix(horizon, mid, smoothstep(0.0, 0.18, h)); col = mix(col, top, smoothstep(0.12, 0.72, h)); col = mix(below, col, smoothstep(-0.18, 0.015, h)); float sd = max(dot(normalize(vDir), normalize(sunDir)), 0.0); col += vec3(1.0,0.72,0.42) * pow(sd, 900.0) * 5.0; col += vec3(1.0,0.62,0.34) * pow(sd, 12.0) * 0.22; col += vec3(0.9,0.55,0.3) * pow(sd, 3.0) * 0.06; gl_FragColor = vec4(col, 1.0); }` }); const sky = new THREE.Mesh(geo, mat); sky.name = 'sky'; return sky; } export function makeEnvScene(sunDir) { const envScene = new THREE.Scene(); envScene.add(makeSky(sunDir)); const sun = new THREE.Mesh(new THREE.SphereGeometry(4.5, 16, 8), new THREE.MeshBasicMaterial({ color: 0xffe0b0 })); sun.position.copy(sunDir).normalize().multiplyScalar(90); envScene.add(sun); return envScene; } /* ------------- terrain ------------- */ export function buildTerrain() { const gtex = groundTextures(); const size = 260, seg = 180; const geo = new THREE.PlaneGeometry(size, size, seg, seg); geo.rotateX(-Math.PI / 2); const pos = geo.attributes.position; for (let i = 0; i < pos.count; i++) { const x = pos.getX(i), z = pos.getZ(i); const d = Math.sqrt(x * x + z * z); const flat = THREE.MathUtils.smoothstep(d, 11.0, 24.0); let h = (Math.sin(x * 0.09) * Math.cos(z * 0.07) * 0.35 + Math.sin(x * 0.031 + 1.3) * Math.cos(z * 0.043 + 2.1) * 0.8 + Math.sin(x * 0.013 - z * 0.017) * 1.2) * flat; h += flat * 0.15; pos.setY(i, h - 0.02); } geo.computeVertexNormals(); const mat = new THREE.MeshStandardMaterial({ map: gtex.map, bumpMap: gtex.bump, bumpScale: 0.12, roughness: 0.98, metalness: 0, color: 0xe4dcc8, }); const mesh = new THREE.Mesh(geo, mat); mesh.receiveShadow = true; mesh.name = 'ground'; return mesh; } /* ------------- autumn grass tufts ------------- */ function grassGeometry() { const verts = [], normals = [], indices = []; const blades = 9; for (let b = 0; b < blades; b++) { const ang = (b / blades) * Math.PI * 2 + R() * 0.8; const lean = 0.08 + R() * 0.16; const h = 0.16 + R() * 0.22; const w = 0.026 + R() * 0.022; const ox = Math.cos(ang) * 0.03, oz = Math.sin(ang) * 0.03; const dx = Math.cos(ang), dz = Math.sin(ang); const segs = 3; let prevL = null, prevR = null; for (let s = 0; s <= segs; s++) { const t = s / segs; const hy = t * h; const out = lean * t * t; const width = w * (1 - t * 0.85); const px = ox + dx * out, pz = oz + dz * out; const nx = -dz, nz = dx; verts.push(px + nx * width, hy, pz + nz * width); verts.push(px - nx * width, hy, pz - nz * width); normals.push(0, 1, 0, 0, 1, 0); if (s > 0) { const i0 = (b * (segs + 1) + s) * 2; indices.push(i0 - 2, i0 - 1, i0, i0 - 1, i0 + 1, i0); } } } const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.Float32BufferAttribute(verts, 3)); g.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3)); g.setIndex(indices); return g; } export function buildGrass(count = 6800) { const geo = grassGeometry(); const mat = new THREE.MeshStandardMaterial({ color: 0xcfc6ae, roughness: 0.98, side: THREE.DoubleSide, vertexColors: false }); const mesh = new THREE.InstancedMesh(geo, mat, count); mesh.receiveShadow = true; const m4 = new THREE.Matrix4(), q = new THREE.Quaternion(), sc = new THREE.Vector3(), p = new THREE.Vector3(); const col = new THREE.Color(); const palette = [0x82822f, 0x9b8c36, 0x6c722c, 0xa89640, 0x7c7a30, 0x5f6a2b, 0xa08c3c, 0x8e7b32]; let n = 0, guard = 0; while (n < count && guard++ < count * 4) { const a = R() * Math.PI * 2, r = 4 + Math.pow(R(), 0.55) * 44; const x = Math.cos(a) * r * 1.25, z = Math.sin(a) * r; if (Math.abs(x) < 9.4 && Math.abs(z) < 8.0) continue; const groundY = terrainHeight(x, z); p.set(x, groundY - 0.02, z); q.setFromEuler(new THREE.Euler(0, R() * Math.PI * 2, 0)); const s = 0.85 + R() * 1.05; sc.set(s, s * (0.8 + R() * 0.7), s); m4.compose(p, q, sc); mesh.setMatrixAt(n, m4); col.set(palette[Math.floor(R() * palette.length)]); col.offsetHSL((R() - 0.5) * 0.04, (R() - 0.5) * 0.15, (R() - 0.5) * 0.15); mesh.setColorAt(n, col); n++; } mesh.count = n; mesh.name = 'grass'; return mesh; } /* cheap terrain height function mirroring the shader-ish sine field */ export function terrainHeight(x, z) { const d = Math.sqrt(x * x + z * z); const flat = THREE.MathUtils.smoothstep(d, 11.0, 24.0); let h = (Math.sin(x * 0.09) * Math.cos(z * 0.07) * 0.35 + Math.sin(x * 0.031 + 1.3) * Math.cos(z * 0.043 + 2.1) * 0.8 + Math.sin(x * 0.013 - z * 0.017) * 1.2) * flat; return h + flat * 0.15 - 0.02; } /* ------------- autumn leaves on the ground ------------- */ export function buildLeafLitter(count = 900) { const geo = new THREE.PlaneGeometry(0.16, 0.1); geo.rotateX(-Math.PI / 2); const mat = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.9, side: THREE.DoubleSide }); const mesh = new THREE.InstancedMesh(geo, mat, count); const m4 = new THREE.Matrix4(), q = new THREE.Quaternion(), sc = new THREE.Vector3(), p = new THREE.Vector3(); const col = new THREE.Color(); const colors = [0xa8491f, 0xc1701f, 0x8a3418, 0xd9a02b, 0x6f3a1d, 0xb05420]; for (let i = 0; i < count; i++) { const a = R() * Math.PI * 2, r = 2 + Math.pow(R(), 0.5) * 24; const x = Math.cos(a) * r, z = Math.sin(a) * r; p.set(x, terrainHeight(x, z) + 0.015 + R() * 0.01, z); q.setFromEuler(new THREE.Euler((R() - 0.5) * 0.25, R() * Math.PI * 2, (R() - 0.5) * 0.25)); sc.setScalar(0.7 + R() * 1.0); m4.compose(p, q, sc); mesh.setMatrixAt(i, m4); col.set(colors[Math.floor(R() * colors.length)]); col.offsetHSL(0, 0, (R() - 0.5) * 0.1); mesh.setColorAt(i, col); } mesh.receiveShadow = true; mesh.name = 'leaves'; return mesh; } /* ------------- split-rail fence ------------- */ export function buildFence(segments) { const grp = new THREE.Group(); grp.name = 'fence'; const bark = barkTextures(); const woodMat = new THREE.MeshStandardMaterial({ map: bark.map, bumpMap: bark.bump, bumpScale: 0.03, color: 0xcdbda4, roughness: 0.95 }); const postGeo = new THREE.CylinderGeometry(0.095, 0.13, 1.2, 7); const railGeo = new THREE.CylinderGeometry(0.055, 0.085, 1, 6); for (const seg of segments) { const pts = []; const [x0, z0, x1, z1] = seg; const len = Math.hypot(x1 - x0, z1 - z0); const n = Math.max(2, Math.round(len / 3.4)); for (let i = 0; i <= n; i++) { const t = i / n; const x = x0 + (x1 - x0) * t, z = z0 + (z1 - z0) * t; const y = terrainHeight(x, z); const post = new THREE.Mesh(postGeo, woodMat); post.position.set(x + (R() - 0.5) * 0.15, y + 0.58, z + (R() - 0.5) * 0.15); post.rotation.set((R() - 0.5) * 0.08, R() * 3, (R() - 0.5) * 0.08); post.scale.setScalar(0.92 + R() * 0.2); post.castShadow = post.receiveShadow = true; grp.add(post); pts.push(new THREE.Vector3(post.position.x, y, post.position.z)); } for (let i = 0; i < pts.length - 1; i++) { for (const lvl of [0.46, 0.92]) { const a = pts[i].clone(), b = pts[i + 1].clone(); a.y += lvl; b.y += lvl + (R() - 0.5) * 0.06; const dir = b.clone().sub(a); const L = dir.length(); const rail = new THREE.Mesh(railGeo, woodMat); rail.scale.set(1, L, 1); rail.position.copy(a).add(b).multiplyScalar(0.5); rail.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), dir.normalize()); rail.rotation.z += (R() - 0.5) * 0.05; rail.castShadow = rail.receiveShadow = true; grp.add(rail); } } } return grp; } /* ------------- autumn trees ------------- */ export function buildTrees() { const grp = new THREE.Group(); grp.name = 'trees'; const bark = barkTextures(); const trunkMat = new THREE.MeshStandardMaterial({ map: bark.map, bumpMap: bark.bump, bumpScale: 0.04, color: 0xb6a38c, roughness: 0.95 }); const palettes = [ [0xc25a1d, 0xd98324, 0xa84216, 0xe0a233, 0xb24a1a], [0xd0a52a, 0xb87a1f, 0xe4bb3c, 0x9e6b1c, 0xc79428], [0x8f5a25, 0xb06a28, 0x7d4a1d, 0xc98a34, 0x6f5a26], [0xb7641f, 0x8a7a2c, 0xd08c2a, 0x9b4a1c], ]; const canopyMats = palettes.map(p => p.map(c => new THREE.MeshStandardMaterial({ color: c, roughness: 0.95 }))); const blobGeo = new THREE.IcosahedronGeometry(1, 2); const spots = [ [-34, -22, 1.15], [-42, 9, 1.35], [-27, 33, 1.0], [31, -34, 1.2], [39, 19, 1.1], [25, 40, 0.9], [-49, 38, 1.3], [50, -13, 1.15], [-23, -45, 1.35], [13, -48, 1.0], [56, 37, 1.05], [-57, -15, 1.2], [-30, -8, 0.85], [33, 8, 0.8], ]; for (const [x, z, s] of spots) { const tree = new THREE.Group(); const y = terrainHeight(x, z); const th = (4.6 + R() * 2.4) * s; const trunk = new THREE.Mesh(new THREE.CylinderGeometry(0.1 * s, 0.32 * s, th, 8), trunkMat); trunk.position.y = th / 2; trunk.castShadow = trunk.receiveShadow = true; tree.add(trunk); // branches for (let b = 0; b < 5; b++) { const ang = R() * Math.PI * 2; const bl = 1.3 + R() * 1.4; const br = new THREE.Mesh(new THREE.CylinderGeometry(0.035 * s, 0.09 * s, bl, 6), trunkMat); br.position.set(0, th * (0.5 + b * 0.09), 0); br.rotation.set(Math.cos(ang) * 0.75, 0, Math.sin(ang) * 0.75); br.translateY(bl / 2); br.castShadow = true; tree.add(br); } const pal = canopyMats[Math.floor(R() * canopyMats.length)]; const blobs = 11 + Math.floor(R() * 5); for (let i = 0; i < blobs; i++) { const mat = pal[Math.floor(R() * pal.length)]; const bm = new THREE.Mesh(blobGeo, mat); const ang = R() * Math.PI * 2; const rad = Math.pow(R(), 0.55) * 2.55 * s; bm.position.set(Math.cos(ang) * rad, th * (0.82 + R() * 0.35) + (R() - 0.5) * 1.3 * s, Math.sin(ang) * rad); const bs = (0.85 + R() * 0.95) * s; bm.scale.set(bs * (1 + R() * 0.35), bs * (0.75 + R() * 0.4), bs * (1 + R() * 0.35)); bm.rotation.set(R() * 3, R() * 3, R() * 3); bm.castShadow = true; bm.receiveShadow = true; tree.add(bm); } tree.position.set(x, y, z); grp.add(tree); } return grp; } /* ------------- far treeline / hills ------------- */ export function buildTreeline() { const grp = new THREE.Group(); grp.name = 'treeline'; const geo = new THREE.IcosahedronGeometry(1, 1); const mat = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 1 }); const clusters = 380, per = 4; const mesh = new THREE.InstancedMesh(geo, mat, clusters * per); const m4 = new THREE.Matrix4(), q = new THREE.Quaternion(), sc = new THREE.Vector3(), p = new THREE.Vector3(); const col = new THREE.Color(); const cols = [0x6d5530, 0x7a5c31, 0x8b6534, 0x5e5531, 0x946f38, 0x665231, 0x776235, 0x8a512b, 0x6f6a36]; let i = 0; for (let c = 0; c < clusters; c++) { const a = R() * Math.PI * 2; const r = 56 + R() * 62; const cx = Math.cos(a) * r, cz = Math.sin(a) * r; const baseY = terrainHeight(cx, cz); const cc = new THREE.Color(cols[Math.floor(R() * cols.length)]); cc.offsetHSL(0, -0.08, (R() - 0.5) * 0.12); const h = 2.2 + R() * 3.2; for (let j = 0; j < per; j++) { const aa = R() * Math.PI * 2, rr = R() * 1.7; p.set(cx + Math.cos(aa) * rr, baseY + h * (0.35 + R() * 0.55), cz + Math.sin(aa) * rr); q.setFromEuler(new THREE.Euler(0, R() * 6, 0)); const s = (1.3 + R() * 1.5) * (j === 0 ? 1.25 : 1); sc.set(s, s * (1.1 + R() * 0.5), s); m4.compose(p, q, sc); mesh.setMatrixAt(i, m4); mesh.setColorAt(i, cc.clone().offsetHSL(0, 0, (R() - 0.5) * 0.06)); i++; } } mesh.count = i; grp.add(mesh); // distant rolling hills const hillMat = [ new THREE.MeshStandardMaterial({ color: 0x9c8a5c, roughness: 1 }), new THREE.MeshStandardMaterial({ color: 0xa88c58, roughness: 1 }), new THREE.MeshStandardMaterial({ color: 0x8a7a52, roughness: 1 }), ]; for (let i = 0; i < 7; i++) { const a = (i / 7) * Math.PI * 2 + 0.3; const r = 150 + R() * 60; const hill = new THREE.Mesh(new THREE.SphereGeometry(1, 32, 16), hillMat[i % 3]); hill.position.set(Math.cos(a) * r, -7 - R() * 5, Math.sin(a) * r); hill.scale.set(36 + R() * 30, 8 + R() * 6, 28 + R() * 20); grp.add(hill); } return grp; } /* ------------- clouds ------------- */ export function buildClouds() { const grp = new THREE.Group(); grp.name = 'clouds'; const tex = cloudTexture(); for (let i = 0; i < 9; i++) { const mat = new THREE.SpriteMaterial({ map: tex, transparent: true, opacity: 0.32 + R() * 0.22, depthWrite: false, fog: false, color: 0xfff1dd }); const sp = new THREE.Sprite(mat); const a = R() * Math.PI * 2, r = 150 + R() * 90; sp.position.set(Math.cos(a) * r, 48 + R() * 48, Math.sin(a) * r); const s = 50 + R() * 80; sp.scale.set(s, s * 0.42, 1); grp.add(sp); } return grp; } /* ------------- farm props (outside subject) ------------- */ export function buildProps() { const grp = new THREE.Group(); grp.name = 'props'; const wood = new THREE.MeshStandardMaterial({ color: 0x7a6046, roughness: 0.9 }); const woodDark = new THREE.MeshStandardMaterial({ color: 0x4b3a2b, roughness: 0.95 }); const hay = new THREE.MeshStandardMaterial({ color: 0xc9a25a, roughness: 1, flatShading: true }); const metal = new THREE.MeshStandardMaterial({ color: 0x37312a, roughness: 0.65, metalness: 0.7 }); const orange = new THREE.MeshStandardMaterial({ color: 0xc4651e, roughness: 0.65 }); const stem = new THREE.MeshStandardMaterial({ color: 0x5c5a2c, roughness: 0.9 }); // hay bales for (let i = 0; i < 3; i++) { const bale = new THREE.Group(); const body = new THREE.Mesh(new THREE.CylinderGeometry(0.62, 0.62, 1.5, 20), hay); body.rotation.z = Math.PI / 2; body.castShadow = body.receiveShadow = true; bale.add(body); for (const bx of [-0.5, 0, 0.5]) { const strap = new THREE.Mesh(new THREE.TorusGeometry(0.625, 0.022, 5, 24), metal); strap.rotation.y = Math.PI / 2; strap.position.x = bx; bale.add(strap); } bale.position.set(8.7 + i * 0.35, 0.62, 2.6 - i * 1.35); bale.rotation.y = 0.25 + i * 0.1; grp.add(bale); } // pumpkins function pumpkin(x, z, s) { const geo = new THREE.SphereGeometry(0.34, 22, 14); const p = geo.attributes.position; for (let i = 0; i < p.count; i++) { const vx = p.getX(i), vy = p.getY(i), vz = p.getZ(i); const th = Math.atan2(vz, vx); const k = 1 + 0.075 * Math.sin(th * 9); p.setXYZ(i, vx * k, vy * 0.78 * k, vz * k); } geo.computeVertexNormals(); const m = new THREE.Mesh(geo, orange); m.position.set(x, 0.25 * s + terrainHeight(x, z), z); m.scale.setScalar(s); m.castShadow = m.receiveShadow = true; grp.add(m); const st = new THREE.Mesh(new THREE.CylinderGeometry(0.035, 0.05, 0.22, 6), stem); st.position.set(x, 0.26 * s + 0.18 * s + terrainHeight(x, z), z); st.rotation.z = 0.2; st.castShadow = true; grp.add(st); } const pk = [[-4.3, 6.7], [-5.1, 7.4], [-3.6, 7.7], [-4.8, 8.15], [-5.6, 6.9], [-4.1, 7.2]]; for (const [x, z] of pk) pumpkin(x, z, 0.7 + R() * 0.55); // barrel by the wall const barrelProfile = []; for (let i = 0; i <= 10; i++) { const t = i / 10; const r = 0.34 + Math.sin(t * Math.PI) * 0.075; barrelProfile.push(new THREE.Vector2(r, t * 0.95)); } const barrel = new THREE.Mesh(new THREE.LatheGeometry(barrelProfile, 18), wood); barrel.position.set(-8.3, 0.01, 6.1); barrel.castShadow = barrel.receiveShadow = true; grp.add(barrel); for (const yy of [0.18, 0.76]) { const hoop = new THREE.Mesh(new THREE.TorusGeometry(0.385, 0.028, 6, 24), metal); hoop.rotation.x = Math.PI / 2; hoop.position.set(-8.3, yy, 6.1); grp.add(hoop); } // firewood stack const logGeo = new THREE.CylinderGeometry(0.1, 0.11, 0.9, 8); for (let row = 0; row < 3; row++) { for (let i = 0; i < 6 - row; i++) { const log = new THREE.Mesh(logGeo, R() > 0.4 ? wood : woodDark); log.rotation.z = Math.PI / 2; log.rotation.y = (R() - 0.5) * 0.1; log.position.set(-9.1 + i * 0.21 + row * 0.1, 0.12 + row * 0.19, 7.4 - 2.2); log.castShadow = log.receiveShadow = true; grp.add(log); } } // leaning wagon wheel const wheel = new THREE.Group(); const rim = new THREE.Mesh(new THREE.TorusGeometry(0.85, 0.055, 8, 32), woodDark); wheel.add(rim); const hub = new THREE.Mesh(new THREE.CylinderGeometry(0.1, 0.1, 0.22, 10), wood); hub.rotation.x = Math.PI / 2; wheel.add(hub); for (let i = 0; i < 10; i++) { const a = (i / 10) * Math.PI * 2; const sp = new THREE.Mesh(new THREE.BoxGeometry(0.045, 0.78, 0.045), wood); sp.position.set(Math.cos(a) * 0.42, Math.sin(a) * 0.42, 0); sp.rotation.z = a - Math.PI / 2; wheel.add(sp); } wheel.position.set(8.0, 0.98, 3.0); wheel.rotation.set(0.06, -0.35, 0.3); wheel.traverse(o => { if (o.isMesh) { o.castShadow = true; o.receiveShadow = true; } }); grp.add(wheel); // a couple of fence posts leaning by the barn grp.traverse(o => { if (o.isMesh) { o.castShadow = true; o.receiveShadow = true; } }); return grp; }