import * as THREE from 'three' import { farMountainHeight, riverHalfWidth, riverQuery, terraceHeight, terraceMask, terraceStepFrac, terrainHeight, TERRACE, villageFactor, } from './layout' import { std } from './materials' import { clamp, fbm2, lerp, smoothstep, valueNoise2 } from './rng' import { getTextures } from './textures' export const NEAR = { x0: -760, x1: 760, z0: -700, z1: 640 } function axis(a: number, b: number, fine: number, coarse: number, r0: number, r1: number) { const out: number[] = [] let v = a while (v < b) { out.push(v) const sp = lerp(fine, coarse, smoothstep(r0, r1, Math.abs(v))) v += sp } out.push(b) return out } /** Unified height used by both terrain meshes (with far mountains blended in). */ export function worldHeight(x: number, z: number) { const r = Math.hypot(x, z) let h = terrainHeight(x, z) const m = smoothstep(560, 1500, r) if (m > 0) h += m * farMountainHeight(x, z) return h } type RGB = [number, number, number] const C = { village: [0.2, 0.18, 0.15] as RGB, bankGrass: [0.11, 0.17, 0.06] as RGB, paddyGreen: [0.13, 0.24, 0.05] as RGB, paddyGold: [0.36, 0.3, 0.09] as RGB, harvested: [0.22, 0.17, 0.1] as RGB, forest: [0.045, 0.085, 0.035] as RGB, bamboo: [0.09, 0.16, 0.05] as RGB, rock: [0.17, 0.16, 0.14] as RGB, mud: [0.12, 0.1, 0.08] as RGB, } const mix3 = (a: RGB, b: RGB, f: number): RGB => [lerp(a[0], b[0], f), lerp(a[1], b[1], f), lerp(a[2], b[2], f)] function groundColor(x: number, z: number, h: number, slope: number): RGB { const q = riverQuery(x, z) const vf = villageFactor(q.s) const w = riverHalfWidth(q.s) // farmland parcels on the valley floor (rotated grid) const ca = Math.cos(0.32), sa = Math.sin(0.32) const u = (x * ca - z * sa) / 26, v = (x * sa + z * ca) / 17 const cell = valueNoise2(Math.floor(u) * 1.0, Math.floor(v) * 1.0, 77) const fu = u - Math.floor(u), fv = v - Math.floor(v) const bund = Math.min(fu, 1 - fu, fv, 1 - fv) < 0.04 ? 1 : 0 let field: RGB = cell < 0.45 ? C.paddyGreen : cell < 0.8 ? C.paddyGold : C.harvested field = mix3(field, C.bankGrass, bund * 0.6) const n = fbm2(x * 0.03, z * 0.03, 3, 4) // hills const hill = smoothstep(4, 30, h) const bamboo = smoothstep(0.45, 0.6, fbm2(x * 0.004, z * 0.004, 3, 50)) let hillC = mix3(C.forest, C.bamboo, bamboo) hillC = mix3(hillC, C.rock, smoothstep(0.55, 0.85, slope)) let c = mix3(field, hillC, hill) // near the village: packed earth and trampled grass const villageZone = vf * (1 - smoothstep(55, 85, q.d)) c = mix3(c, mix3(C.village, C.bankGrass, n * 0.6), villageZone) // river banks: wet mud then grass c = mix3(c, C.mud, 1 - smoothstep(w - 1, w + 4, q.d)) c = mix3(c, C.bankGrass, (1 - smoothstep(w + 2, w + 10, q.d)) * smoothstep(w - 1, w + 2, q.d) * (1 - vf)) const k = 0.82 + n * 0.36 return [c[0] * k, c[1] * k, c[2] * k] } export function createTerrain() { const T = getTextures() const xs = axis(NEAR.x0, NEAR.x1, 2.5, 9, 280, 700) const zs = axis(NEAR.z0, NEAR.z1, 2.5, 9, 200, 640) const nx = xs.length, nz = zs.length const pos = new Float32Array(nx * nz * 3) const col = new Float32Array(nx * nz * 3) const hs = new Float32Array(nx * nz) for (let j = 0; j < nz; j++) for (let i = 0; i < nx; i++) { const x = xs[i], z = zs[j] hs[j * nx + i] = worldHeight(x, z) } for (let j = 0; j < nz; j++) for (let i = 0; i < nx; i++) { const k = j * nx + i const x = xs[i], z = zs[j], h = hs[k] pos[k * 3] = x; pos[k * 3 + 1] = h; pos[k * 3 + 2] = z const il = Math.max(0, i - 1), ir = Math.min(nx - 1, i + 1), jd = Math.max(0, j - 1), ju = Math.min(nz - 1, j + 1) const dx = (hs[j * nx + ir] - hs[j * nx + il]) / (xs[ir] - xs[il]) const dz = (hs[ju * nx + i] - hs[jd * nx + i]) / (zs[ju] - zs[jd]) const slope = clamp(Math.hypot(dx, dz)) const c = groundColor(x, z, h, slope) col[k * 3] = c[0]; col[k * 3 + 1] = c[1]; col[k * 3 + 2] = c[2] } const idx: number[] = [] for (let j = 0; j < nz - 1; j++) for (let i = 0; i < nx - 1; i++) { const a = j * nx + i, b = a + 1, c = a + nx, d = c + 1 idx.push(a, c, b, b, c, d) } const geo = new THREE.BufferGeometry() geo.setAttribute('position', new THREE.BufferAttribute(pos, 3)) geo.setAttribute('color', new THREE.BufferAttribute(col, 3)) geo.setIndex(idx) geo.computeVertexNormals() // world-space UV for detail texture const uv = new Float32Array(nx * nz * 2) for (let k = 0; k < nx * nz; k++) { uv[k * 2] = pos[k * 3] / 9; uv[k * 2 + 1] = pos[k * 3 + 2] / 9 } geo.setAttribute('uv', new THREE.BufferAttribute(uv, 2)) const mat = std({ vertexColors: true, roughness: 0.95, metalness: 0, map: T.ground }, { key: 'terrain' }) const mesh = new THREE.Mesh(geo, mat) mesh.receiveShadow = true mesh.castShadow = false return mesh } /** High resolution terraced paddies; flats hold water that mirrors the sky. */ export function createTerraces() { const res = 1.25 const x0 = TERRACE.cx - TERRACE.rx, x1 = TERRACE.cx + TERRACE.rx const z0 = TERRACE.cz - TERRACE.rz, z1 = TERRACE.cz + TERRACE.rz const nx = Math.ceil((x1 - x0) / res) + 1, nz = Math.ceil((z1 - z0) / res) + 1 const pos = new Float32Array(nx * nz * 3) const col = new Float32Array(nx * nz * 3) const wet = new Float32Array(nx * nz) const mask = new Float32Array(nx * nz) for (let j = 0; j < nz; j++) for (let i = 0; i < nx; i++) { const k = j * nx + i const x = x0 + i * res, z = z0 + j * res const m = terraceMask(x, z) mask[k] = m const h = terraceHeight(x, z) pos[k * 3] = x; pos[k * 3 + 1] = h; pos[k * 3 + 2] = z const f = terraceStepFrac(x, z) const flat = (1 - smoothstep(0.72, 0.8, f)) * smoothstep(0.03, 0.08, f) // some paddies flooded (mirror), some with young green rice, some ripening const level = Math.floor(h / TERRACE.step) const cellNoise = valueNoise2(level * 0.7, Math.floor((x + z * 0.3) / 40), 3) const flooded = cellNoise < 0.55 ? 1 : 0 const w = flat * flooded * m wet[k] = w const n = fbm2(x * 0.08, z * 0.08, 3, 8) let c: RGB = cellNoise < 0.78 ? [0.15, 0.26, 0.06] : [0.38, 0.32, 0.1] c = mix3(c, [0.05, 0.06, 0.05], w * 0.9) // risers: earth & grass c = mix3(c, mix3([0.14, 0.12, 0.08], [0.1, 0.16, 0.05], n), 1 - flat) col[k * 3] = c[0] * (0.85 + n * 0.3); col[k * 3 + 1] = c[1] * (0.85 + n * 0.3); col[k * 3 + 2] = c[2] * (0.85 + n * 0.3) } const idx: number[] = [] for (let j = 0; j < nz - 1; j++) for (let i = 0; i < nx - 1; i++) { const a = j * nx + i, b = a + 1, c = a + nx, d = c + 1 if (mask[a] + mask[b] + mask[c] + mask[d] < 0.002) continue idx.push(a, c, b, b, c, d) } const geo = new THREE.BufferGeometry() geo.setAttribute('position', new THREE.BufferAttribute(pos, 3)) geo.setAttribute('color', new THREE.BufferAttribute(col, 3)) geo.setAttribute('aWet', new THREE.BufferAttribute(wet, 1)) geo.setIndex(idx) geo.computeVertexNormals() // flatten normals on flooded paddies so they mirror cleanly const nrm = geo.getAttribute('normal') as THREE.BufferAttribute for (let k = 0; k < nx * nz; k++) { const w = wet[k] if (w > 0) { const v = new THREE.Vector3(nrm.getX(k), nrm.getY(k), nrm.getZ(k)).lerp(new THREE.Vector3(0, 1, 0), w).normalize() nrm.setXYZ(k, v.x, v.y, v.z) } } const mat = std({ vertexColors: true, roughness: 0.92, metalness: 0, polygonOffset: true, polygonOffsetFactor: -2, polygonOffsetUnits: -2 }, { key: 'terrace', vertexPars: 'attribute float aWet; varying float vWet;', vertex: 'vWet = aWet;', fragmentPars: 'varying float vWet;', }) const prev = mat.onBeforeCompile mat.onBeforeCompile = (s, r) => { prev.call(mat, s, r) s.fragmentShader = s.fragmentShader.replace('#include ', 'float roughnessFactor = mix(roughness, 0.04, vWet);') } const mesh = new THREE.Mesh(geo, mat) mesh.receiveShadow = true return mesh } /** Far mountain ring (polar grid) — layered ranges that fade into the haze. */ export function createFarMountains() { const rings: number[] = [] let r = 640 while (r < 9000) { rings.push(r); r *= 1.035 } const seg = 720 const pos: number[] = [], col: number[] = [] for (let ri = 0; ri < rings.length; ri++) { for (let a = 0; a <= seg; a++) { const ang = (a / seg) * Math.PI * 2 const x = Math.cos(ang) * rings[ri], z = Math.sin(ang) * rings[ri] const inside = x > -760 && x < 760 && z > -700 && z < 640 let h = worldHeight(x, z) if (inside) h -= 6 pos.push(x, h, z) const n = fbm2(x * 0.004, z * 0.004, 3, 2) const g = 0.6 + n * 0.5 col.push(0.04 * g, 0.075 * g, 0.04 * g) } } const idx: number[] = [] const w = seg + 1 for (let ri = 0; ri < rings.length - 1; ri++) for (let a = 0; a < seg; a++) { const p = ri * w + a, q = p + w idx.push(p, p + 1, q, p + 1, q + 1, q) } const geo = new THREE.BufferGeometry() geo.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3)) geo.setAttribute('color', new THREE.Float32BufferAttribute(col, 3)) geo.setIndex(idx) geo.computeVertexNormals() const mat = std({ vertexColors: true, roughness: 1, metalness: 0 }, { key: 'far' }) const mesh = new THREE.Mesh(geo, mat) mesh.receiveShadow = false return mesh }