// Environment: sky, sunlight, dry earth, stacked-stone terrace and distant hills. import * as THREE from 'three'; import { makeGroundMaps, makeStoneMaps, makeLeafTexture, makeCloudTexture, mulberry32 } from './textures.js'; export const SUN_DIR = new THREE.Vector3(-0.52, 0.62, 0.60).normalize(); function smoothstep(a, b, x) { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); } // ---------- sky ---------- function sunGlowTexture() { const c = document.createElement('canvas'); c.width = c.height = 256; const g = c.getContext('2d'); const grad = g.createRadialGradient(128, 128, 0, 128, 128, 128); grad.addColorStop(0, 'rgba(255,250,235,1)'); grad.addColorStop(0.035, 'rgba(255,244,214,0.98)'); grad.addColorStop(0.10, 'rgba(255,226,168,0.55)'); grad.addColorStop(0.30, 'rgba(255,208,150,0.16)'); grad.addColorStop(1, 'rgba(255,200,140,0)'); g.fillStyle = grad; g.fillRect(0, 0, 256, 256); const t = new THREE.CanvasTexture(c); t.colorSpace = THREE.SRGBColorSpace; return t; } function makeSky() { const geo = new THREE.SphereGeometry(150, 64, 40); const pos = geo.attributes.position; const cols = []; const c = new THREE.Color(); const zen = new THREE.Color('#3d76ad'); const mid = new THREE.Color('#8fb4cc'); const hor = new THREE.Color('#f3dfba'); const low = new THREE.Color('#cbb695'); const sunWarm = new THREE.Color('#ffcf91'); const sunAz = new THREE.Vector3(SUN_DIR.x, 0, SUN_DIR.z).normalize(); for (let i = 0; i < pos.count; i++) { const v = new THREE.Vector3(pos.getX(i), pos.getY(i), pos.getZ(i)).normalize(); const h = v.y; if (h >= 0) { const t = Math.pow(Math.min(1, h), 0.52); c.copy(hor).lerp(mid, Math.min(1, t * 1.6)).lerp(zen, Math.max(0, (t - 0.42) / 0.58)); } else { c.copy(hor).lerp(low, Math.min(1, -h * 5)); } const az = new THREE.Vector3(v.x, 0, v.z).normalize().dot(sunAz); const warm = Math.pow(Math.max(0, az), 3.0) * (1 - Math.min(1, Math.abs(h) * 2.2)) * 0.55; c.lerp(sunWarm, warm); cols.push(c.r, c.g, c.b); } geo.setAttribute('color', new THREE.Float32BufferAttribute(cols, 3)); const mat = new THREE.MeshBasicMaterial({ vertexColors: true, side: THREE.BackSide, depthWrite: false, fog: false, toneMapped: false }); const m = new THREE.Mesh(geo, mat); m.name = 'sky'; m.renderOrder = -10; const glow = new THREE.Sprite(new THREE.SpriteMaterial({ map: sunGlowTexture(), blending: THREE.AdditiveBlending, transparent: true, depthWrite: false, depthTest: false, fog: false, toneMapped: false, opacity: 0.95, })); glow.position.copy(SUN_DIR).multiplyScalar(90); glow.scale.set(58, 58, 1); glow.renderOrder = -9; m.add(glow); return m; } function makeClouds(rng) { const tex = makeCloudTexture(); const group = new THREE.Group(); group.name = 'clouds'; for (let i = 0; i < 8; i++) { const mat = new THREE.SpriteMaterial({ map: tex, transparent: true, depthWrite: false, opacity: 0.20 + rng() * 0.16, color: new THREE.Color(0xfff3da), fog: false, toneMapped: false, }); const s = new THREE.Sprite(mat); const a = rng() * Math.PI * 2; const r = 85 + rng() * 40; s.position.set(Math.cos(a) * r, 12 + rng() * 22, Math.sin(a) * r); const w = 34 + rng() * 34; s.scale.set(w, w * (0.22 + rng() * 0.14), 1); group.add(s); } return group; } // ---------- terrain ---------- function terrainHeight(x, z) { const r = Math.hypot(x, z); const n = Math.sin(x * 0.35) * Math.cos(z * 0.31) * 0.16 + Math.sin(x * 0.13 + z * 0.21) * 0.32; let y; if (r < 3.15) y = 0.02 + n * 0.015; else { const drop = smoothstep(3.15, 3.75, r); y = 0.02 - drop * 0.92; y += -Math.max(0, r - 4.0) * 0.03; y += n * (0.08 + Math.min(1, (r - 3) / 12) * 0.35); // gentle rolling shoulders y += Math.sin(x * 0.06 + 1.2) * Math.cos(z * 0.055) * Math.min(1, r / 25) * 1.1; } return y; } function makeGround(rng) { const g = new THREE.PlaneGeometry(150, 150, 170, 170); g.rotateX(-Math.PI / 2); const pos = g.attributes.position; const colors = []; const c = new THREE.Color(); for (let i = 0; i < pos.count; i++) { const x = pos.getX(i), z = pos.getZ(i); const y = terrainHeight(x, z); pos.setY(i, y); const r = Math.hypot(x, z); const litter = 1 - smoothstep(1.0, 5.5, r); c.setRGB(0.74, 0.72, 0.62); // green-grey scrub patches in the distance, warm bare soil near the terrace const patch = Math.sin(x * 0.18 + 2.2) * Math.cos(z * 0.16) + Math.sin(x * 0.07) * 0.6; if (patch > 0.1) c.lerp(new THREE.Color(0.42, 0.47, 0.31), Math.min(0.8, (patch - 0.1) * 0.55)); if (litter > 0) c.lerp(new THREE.Color(0.62, 0.55, 0.4), litter * 0.5); colors.push(c.r, c.g, c.b); } g.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); g.computeVertexNormals(); const maps = makeGroundMaps(); const mat = new THREE.MeshStandardMaterial({ map: maps.map, bumpMap: maps.bumpMap, bumpScale: 0.32, roughnessMap: maps.roughnessMap, roughness: 1.0, metalness: 0, vertexColors: true, color: 0xa79a80, }); const mesh = new THREE.Mesh(g, mat); mesh.name = 'terrain'; mesh.receiveShadow = true; return mesh; } // beveled rounded slab / stone function stoneGeometry(w = 1, h = 1, d = 1) { const shape = new THREE.Shape(); const rr = 0.12 * Math.min(w, h); const x = -w / 2, y = -h / 2; shape.moveTo(x + rr, y); shape.lineTo(x + w - rr, y); shape.quadraticCurveTo(x + w, y, x + w, y + rr); shape.lineTo(x + w, y + h - rr); shape.quadraticCurveTo(x + w, y + h, x + w - rr, y + h); shape.lineTo(x + rr, y + h); shape.quadraticCurveTo(x, y + h, x, y + h - rr); shape.lineTo(x, y + rr); shape.quadraticCurveTo(x, y, x + rr, y); const geo = new THREE.ExtrudeGeometry(shape, { depth: d, bevelEnabled: true, bevelSegments: 2, steps: 1, bevelSize: Math.min(w, h) * 0.11, bevelThickness: Math.min(w, h, d) * 0.12, }); geo.translate(0, 0, -d / 2); geo.computeVertexNormals(); return geo; } function makeTerrace(rng, stone) { const group = new THREE.Group(); group.name = 'terrace'; const stoneMat = new THREE.MeshStandardMaterial({ map: stone.map, bumpMap: stone.bumpMap, bumpScale: 1.6, roughnessMap: stone.roughnessMap, roughness: 0.95, metalness: 0, color: 0x8a7d64, }); const unit = stoneGeometry(1, 1, 1); const R = 3.34; const courses = 6; const dummy = new THREE.Object3D(); const col = new THREE.Color(); // retaining ring of stacked dry stones for (let course = 0; course < courses; course++) { const y = -0.92 + course * 0.168 + 0.084; let a = course * 0.7; while (a < Math.PI * 2 + course * 0.7) { const arc = 0.30 + rng() * 0.24; const w = arc * R * 0.86; const h = 0.15 + rng() * 0.045; const depth = 0.42 + rng() * 0.1; const r = R + (rng() - 0.5) * 0.09; dummy.position.set(Math.cos(a) * r, y + (rng() - 0.5) * 0.02, Math.sin(a) * r); dummy.rotation.set((rng() - 0.5) * 0.08, -a + Math.PI / 2 + (rng() - 0.5) * 0.09, (rng() - 0.5) * 0.06); dummy.scale.set(w, h, depth); dummy.updateMatrix(); const mesh = new THREE.InstancedMesh(unit, stoneMat, 1); mesh.setMatrixAt(0, dummy.matrix); col.setHSL(0.11 + rng() * 0.03, 0.10 + rng() * 0.08, 0.52 + rng() * 0.14); mesh.setColorAt(0, col); mesh.castShadow = true; mesh.receiveShadow = true; mesh.instanceMatrix.needsUpdate = true; if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true; group.add(mesh); a += arc; } } // capstones let a = 0.2; while (a < Math.PI * 2 + 0.2) { const arc = 0.42 + rng() * 0.26; dummy.position.set(Math.cos(a) * R, 0.058, Math.sin(a) * R); dummy.rotation.set(0, -a + Math.PI / 2, (rng() - 0.5) * 0.04); dummy.scale.set(arc * R * 1.02, 0.09 + rng() * 0.03, 0.55 + rng() * 0.1); dummy.updateMatrix(); const mesh = new THREE.InstancedMesh(unit, stoneMat, 1); mesh.setMatrixAt(0, dummy.matrix); col.setHSL(0.11, 0.09 + rng() * 0.06, 0.58 + rng() * 0.12); mesh.setColorAt(0, col); mesh.castShadow = true; mesh.receiveShadow = true; mesh.instanceMatrix.needsUpdate = true; if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true; group.add(mesh); a += arc; } // ring paving of tightly fitted flagstones on the terrace top const rings = [ { ri: 0.48, ro: 1.38, n: 6 }, { ri: 1.34, ro: 2.18, n: 9 }, { ri: 2.14, ro: 2.98, n: 13 }, ]; for (const ring of rings) { for (let i = 0; i < ring.n; i++) { const a0 = (i / ring.n) * Math.PI * 2 + (rng() - 0.5) * 0.05; const a1 = ((i + 1) / ring.n) * Math.PI * 2 + (rng() - 0.5) * 0.05; const gap = 0.035; const pts = []; const segs = 5; for (let k = 0; k <= segs; k++) { const a = a0 + gap + (a1 - a0 - gap * 2) * (k / segs); const r = ring.ro - 0.018 - rng() * 0.03; pts.push(new THREE.Vector2(Math.cos(a) * r, Math.sin(a) * r)); } for (let k = segs; k >= 0; k--) { const a = a0 + gap + (a1 - a0 - gap * 2) * (k / segs); const r = ring.ri + 0.018 + rng() * 0.03; pts.push(new THREE.Vector2(Math.cos(a) * r, Math.sin(a) * r)); } const shape = new THREE.Shape(pts); const geo = new THREE.ExtrudeGeometry(shape, { depth: 0.06, bevelEnabled: true, bevelSegments: 2, steps: 1, bevelSize: 0.022, bevelThickness: 0.018, }); geo.rotateX(-Math.PI / 2); geo.translate(0, 0.055, 0); const slabMat = stoneMat.clone(); slabMat.color.setHSL(0.095 + rng() * 0.02, 0.06 + rng() * 0.05, 0.20 + rng() * 0.09); const mesh = new THREE.Mesh(geo, slabMat); mesh.receiveShadow = true; mesh.castShadow = true; group.add(mesh); } } // stone steps descending on the +Z side const stepW = 1.8; for (let i = 0; i < 3; i++) { const s = new THREE.Mesh(stoneGeometry(stepW - i * 0.15, 0.16, 0.55), stoneMat); s.position.set(0.15, -0.16 - i * 0.18, R + 0.35 + i * 0.44); s.rotation.y = 0.02 * i; s.castShadow = true; s.receiveShadow = true; group.add(s); } // a lower dry-stone wall threading through the grove behind the terrace { const N = 78; const wall = new THREE.InstancedMesh(unit, stoneMat, N); let wi = 0; for (let course = 0; course < 3; course++) { let a = -0.35; while (a < 0.95 && wi < N) { const arc = 0.05 + rng() * 0.035; const rr = 8.5 + Math.sin(a * 2.0) * 1.3; const x = Math.cos(a) * rr, z = Math.sin(a) * rr; dummy.position.set(x, terrainHeight(x, z) + course * 0.155 + 0.075, z); dummy.rotation.set((rng() - 0.5) * 0.05, -a + Math.PI / 2, (rng() - 0.5) * 0.04); dummy.scale.set(arc * rr * 0.92, 0.15 + rng() * 0.035, 0.4 + rng() * 0.08); dummy.updateMatrix(); wall.setMatrixAt(wi, dummy.matrix); col.setHSL(0.10, 0.07 + rng() * 0.05, 0.32 + rng() * 0.12); wall.setColorAt(wi, col); wi++; a += arc; } } wall.count = wi; wall.castShadow = true; wall.receiveShadow = true; wall.instanceMatrix.needsUpdate = true; if (wall.instanceColor) wall.instanceColor.needsUpdate = true; group.add(wall); } // rubble and gravel scattered over dirt and wall base const pebGeo = new THREE.DodecahedronGeometry(0.5, 0); const pebMat = new THREE.MeshStandardMaterial({ map: stone.map, roughness: 0.95, color: 0xa89c82 }); const peb = new THREE.InstancedMesh(pebGeo, pebMat, 140); for (let i = 0; i < 140; i++) { const rr = i < 70 ? 0.4 + rng() * 2.9 : 3.5 + rng() * 3.5; const aa = rng() * Math.PI * 2; const x = Math.cos(aa) * rr, z = Math.sin(aa) * rr; const y = terrainHeight(x, z); const sc = 0.035 + rng() * 0.10; dummy.position.set(x, y + sc * 0.35, z); dummy.rotation.set(rng() * 3, rng() * 3, rng() * 3); dummy.scale.set(sc, sc * 0.7, sc * (0.8 + rng() * 0.5)); dummy.updateMatrix(); peb.setMatrixAt(i, dummy.matrix); col.setHSL(0.11, 0.08, 0.5 + rng() * 0.2); peb.setColorAt(i, col); } peb.castShadow = true; peb.receiveShadow = true; peb.instanceMatrix.needsUpdate = true; if (peb.instanceColor) peb.instanceColor.needsUpdate = true; group.add(peb); // larger weathered boulders in the scrub const rockGeo = new THREE.DodecahedronGeometry(1, 1); { const p = rockGeo.attributes.position; for (let i = 0; i < p.count; i++) { const x = p.getX(i), y = p.getY(i), z = p.getZ(i); const n = 1 + 0.16 * Math.sin(x * 4 + y * 3) + 0.12 * Math.cos(z * 5 + y * 2); p.setXYZ(i, x * n, y * n * 0.72, z * n); } rockGeo.computeVertexNormals(); } const rocks = new THREE.InstancedMesh(rockGeo, pebMat, 26); for (let i = 0; i < 26; i++) { const aa = rng() * Math.PI * 2; const rr = 4.3 + rng() * 12; const x = Math.cos(aa) * rr, z = Math.sin(aa) * rr; dummy.position.set(x, terrainHeight(x, z) + 0.1, z); dummy.rotation.set(rng() * 3, rng() * 3, rng() * 3); const sc = 0.25 + rng() * 0.55; dummy.scale.set(sc, sc * 0.55, sc * (0.7 + rng() * 0.6)); dummy.updateMatrix(); rocks.setMatrixAt(i, dummy.matrix); col.setHSL(0.10, 0.06, 0.48 + rng() * 0.18); rocks.setColorAt(i, col); } rocks.castShadow = true; rocks.receiveShadow = true; rocks.instanceMatrix.needsUpdate = true; if (rocks.instanceColor) rocks.instanceColor.needsUpdate = true; group.add(rocks); return group; } // dry grass tufts function makeGrass(rng) { const blades = []; for (let i = 0; i < 9; i++) { const h = 0.22 + rng() * 0.35; const w = 0.016 + rng() * 0.012; const g = new THREE.PlaneGeometry(w, h, 1, 4); const p = g.attributes.position; for (let k = 0; k < p.count; k++) { const y = p.getY(k) + h / 2; const t = y / h; p.setZ(k, Math.sin(t * 1.2) * 0.13 + t * t * 0.10); p.setX(k, p.getX(k) * (1 - t * 0.8)); } g.computeVertexNormals(); const q = new THREE.Quaternion().setFromEuler(new THREE.Euler((rng() - 0.5) * 0.35, rng() * Math.PI * 2, (rng() - 0.5) * 0.3)); g.applyQuaternion(q); g.translate((rng() - 0.5) * 0.15, 0, (rng() - 0.5) * 0.15); blades.push(g); } const geo = mergeGeos(blades); const mat = new THREE.MeshStandardMaterial({ color: 0xa89a68, roughness: 0.9, metalness: 0, side: THREE.DoubleSide, vertexColors: false, }); const inst = new THREE.InstancedMesh(geo, mat, 650); const dummy = new THREE.Object3D(); const col = new THREE.Color(); for (let i = 0; i < 650; i++) { let x, z, y; if (i < 30) { const a = rng() * Math.PI * 2, r = 2.3 + rng() * 0.85; x = Math.cos(a) * r; z = Math.sin(a) * r; y = terrainHeight(x, z); } else { const a = rng() * Math.PI * 2, r = 3.7 + Math.pow(rng(), 0.85) * 18; x = Math.cos(a) * r; z = Math.sin(a) * r; y = terrainHeight(x, z); } dummy.position.set(x, y, z); dummy.rotation.y = rng() * Math.PI * 2; const s = 0.65 + rng() * 0.8; dummy.scale.set(s, s * (0.8 + rng() * 0.6), s); dummy.updateMatrix(); inst.setMatrixAt(i, dummy.matrix); col.setHSL(0.10 + rng() * 0.035, 0.24 + rng() * 0.16, 0.36 + rng() * 0.20); inst.setColorAt(i, col); } inst.castShadow = true; inst.receiveShadow = true; inst.instanceMatrix.needsUpdate = true; if (inst.instanceColor) inst.instanceColor.needsUpdate = true; return inst; } function mergeGeos(geos) { // minimal merge for identical attribute sets (position, normal, uv) let total = 0, idxTotal = 0; for (const g of geos) { total += g.attributes.position.count; idxTotal += g.index ? g.index.count : 0; } const pos = new Float32Array(total * 3), nor = new Float32Array(total * 3), uv = new Float32Array(total * 2); const idx = new Uint32Array(idxTotal); let vo = 0, io = 0; for (const g of geos) { pos.set(g.attributes.position.array, vo * 3); nor.set(g.attributes.normal.array, vo * 3); uv.set(g.attributes.uv.array, vo * 2); const gi = g.index.array; for (let i = 0; i < gi.length; i++) idx[io + i] = gi[i] + vo; vo += g.attributes.position.count; io += gi.length; } const out = new THREE.BufferGeometry(); out.setAttribute('position', new THREE.BufferAttribute(pos, 3)); out.setAttribute('normal', new THREE.BufferAttribute(nor, 3)); out.setAttribute('uv', new THREE.BufferAttribute(uv, 2)); out.setIndex(new THREE.BufferAttribute(idx, 1)); return out; } function makeScrub(rng) { const group = new THREE.Group(); group.name = 'scrub'; const geo = new THREE.IcosahedronGeometry(1, 2); const p = geo.attributes.position; for (let i = 0; i < p.count; i++) { const x = p.getX(i), y = p.getY(i), z = p.getZ(i); const n = 1 + 0.22 * Math.sin(x * 5.1 + z * 3.7) + 0.14 * Math.cos(y * 6.3); p.setXYZ(i, x * n, y * n * 0.6, z * n); } geo.computeVertexNormals(); const mat = new THREE.MeshStandardMaterial({ color: 0x5c6140, roughness: 0.92, flatShading: true }); const count = 44; const inst = new THREE.InstancedMesh(geo, mat, count); const dummy = new THREE.Object3D(); const col = new THREE.Color(); for (let i = 0; i < count; i++) { const a = rng() * Math.PI * 2; const r = 5.5 + Math.pow(rng(), 0.8) * 15; const x = Math.cos(a) * r, z = Math.sin(a) * r; dummy.position.set(x, terrainHeight(x, z) + 0.05, z); dummy.rotation.y = rng() * Math.PI * 2; const s = 0.3 + rng() * 0.6; dummy.scale.set(s, s * 0.65, s); dummy.updateMatrix(); inst.setMatrixAt(i, dummy.matrix); col.setHSL(0.19 + rng() * 0.04, 0.16 + rng() * 0.12, 0.24 + rng() * 0.1); inst.setColorAt(i, col); } inst.castShadow = true; inst.receiveShadow = true; inst.instanceMatrix.needsUpdate = true; group.add(inst); return group; } function makeCypress(rng) { const group = new THREE.Group(); group.name = 'distant-cypress'; const mat = new THREE.MeshStandardMaterial({ color: 0x46533a, roughness: 0.95 }); for (let i = 0; i < 9; i++) { const a = rng() * Math.PI * 2; const r = 26 + rng() * 22; const x = Math.cos(a) * r, z = Math.sin(a) * r; const h = 4.5 + rng() * 4; const geo = new THREE.LatheGeometry([ new THREE.Vector2(0.001, 0), new THREE.Vector2(0.28, h * 0.05), new THREE.Vector2(0.58, h * 0.22), new THREE.Vector2(0.78, h * 0.45), new THREE.Vector2(0.56, h * 0.72), new THREE.Vector2(0.35, h * 0.9), new THREE.Vector2(0.001, h), ], 20); // rough the surface const p = geo.attributes.position; for (let k = 0; k < p.count; k++) { const px = p.getX(k), py = p.getY(k), pz = p.getZ(k); const n = 1 + 0.10 * Math.sin(px * 7 + py * 3) + 0.08 * Math.cos(pz * 6 - py * 2); p.setXYZ(k, px * n, py, pz * n); } geo.computeVertexNormals(); const m = new THREE.Mesh(geo, mat); m.position.set(x, terrainHeight(x, z), z); m.rotation.y = rng() * Math.PI; m.castShadow = true; m.receiveShadow = true; group.add(m); } return group; } function makeHills(rng) { const group = new THREE.Group(); group.name = 'hills'; const rings = [ { r: 70, base: -9, amp: 3.4, color: 0x9aa69b, op: 1 }, { r: 105, base: -10, amp: 6.2, color: 0xacb6b1, op: 1 }, ]; for (const ring of rings) { const N = 160; const verts = [], idx = []; for (let i = 0; i <= N; i++) { const a = (i / N) * Math.PI * 2; const h = ring.amp * (0.55 + 0.45 * Math.sin(a * 3.1 + 1.2) * Math.cos(a * 7.3)) + 1.6 * Math.sin(a * 11.0); const top = ring.base + h + 6.5; verts.push(Math.cos(a) * ring.r, ring.base, Math.sin(a) * ring.r); verts.push(Math.cos(a) * ring.r, top, Math.sin(a) * ring.r); } for (let i = 0; i < N; i++) { const a = i * 2; idx.push(a, a + 1, a + 2, a + 1, a + 3, a + 2); } const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.Float32BufferAttribute(verts, 3)); g.setIndex(idx); g.computeVertexNormals(); const mat = new THREE.MeshBasicMaterial({ color: ring.color, side: THREE.DoubleSide, fog: true, toneMapped: true, }); const m = new THREE.Mesh(g, mat); group.add(m); } return group; } function makeFallenLeaves(rng) { const tex = makeLeafTexture(); const geo = new THREE.PlaneGeometry(0.08, 0.24); geo.rotateX(-Math.PI / 2); const mat = new THREE.MeshStandardMaterial({ map: tex, alphaTest: 0.4, side: THREE.DoubleSide, roughness: 0.9, color: 0x8a8464, transparent: false, }); const n = 320; const inst = new THREE.InstancedMesh(geo, mat, n); const dummy = new THREE.Object3D(); const col = new THREE.Color(); for (let i = 0; i < n; i++) { const a = rng() * Math.PI * 2; const r = 0.35 + Math.pow(rng(), 0.7) * 4.2; const x = Math.cos(a) * r, z = Math.sin(a) * r; dummy.position.set(x, terrainHeight(x, z) + 0.012 + rng() * 0.01, z); dummy.rotation.set((rng() - 0.5) * 0.2, rng() * Math.PI * 2, (rng() - 0.5) * 0.2); const s = 0.7 + rng() * 0.6; dummy.scale.set(s, s, s); dummy.updateMatrix(); inst.setMatrixAt(i, dummy.matrix); col.setHSL(0.12 + rng() * 0.04, 0.15 + rng() * 0.12, 0.34 + rng() * 0.16); inst.setColorAt(i, col); } inst.receiveShadow = true; inst.instanceMatrix.needsUpdate = true; return inst; } export function buildEnvironment(scene, renderer) { const rng = mulberry32(99173); const stone = makeStoneMaps(); scene.add(makeSky()); scene.add(makeClouds(rng)); scene.add(makeHills(rng)); scene.add(makeGround(rng)); scene.add(makeTerrace(rng, stone)); scene.add(makeGrass(rng)); scene.add(makeScrub(rng)); scene.add(makeFallenLeaves(rng)); scene.fog = new THREE.FogExp2(0xdccbab, 0.0105); // lights const hemi = new THREE.HemisphereLight(0x9cc0de, 0x7b5f40, 0.75); scene.add(hemi); const sun = new THREE.DirectionalLight(0xffe0ab, 3.0); sun.position.copy(SUN_DIR).multiplyScalar(16); sun.target.position.set(0, 1.8, 0); sun.castShadow = true; sun.shadow.mapSize.set(2048, 2048); sun.shadow.camera.left = -7.5; sun.shadow.camera.right = 7.5; sun.shadow.camera.top = 8.5; sun.shadow.camera.bottom = -2.5; sun.shadow.camera.near = 1; sun.shadow.camera.far = 40; sun.shadow.bias = -0.0004; sun.shadow.normalBias = 0.025; sun.shadow.radius = 3; scene.add(sun); scene.add(sun.target); const fill = new THREE.DirectionalLight(0x9dc2e8, 0.55); fill.position.set(9, 4, -7); scene.add(fill); const rim = new THREE.DirectionalLight(0xffd7a0, 0.85); rim.position.set(2, 6, -11); scene.add(rim); // sky-based environment reflections const pmrem = new THREE.PMREMGenerator(renderer); const envScene = new THREE.Scene(); const envSky = makeSky(); envSky.scale.setScalar(0.3); envScene.add(envSky); const sunBall = new THREE.Mesh( new THREE.SphereGeometry(4, 16, 12), new THREE.MeshBasicMaterial({ color: 0xfff2d0 }) ); sunBall.position.copy(SUN_DIR).multiplyScalar(34); envScene.add(sunBall); const envRT = pmrem.fromScene(envScene, 0.04); scene.environment = envRT.texture; scene.environmentIntensity = 0.42; pmrem.dispose(); return { sun, hemi }; }