Spaces:
Paused
Paused
File size: 22,817 Bytes
2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b b02b26f 2b0432b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | // Vegetation: the ancient camphor tree, village trees, weeping willows,
// bamboo groves, hillside forest and riverside reeds.
import * as THREE from 'three'
import { Builder } from './geom'
import {
buildHouses, GROUND_Y, PAVILION_POS, riverHalfWidth, riverPoint, riverQuery, terraceMask, terrainHeight, TREE_POS, VILLAGE_S0, VILLAGE_S1, villageFactor,
} from './layout'
import { patch, U } from './materials'
import { fbm2, mulberry32, Rng, rrange } from './rng'
import { getTextures } from './textures'
import { NEAR, worldHeight } from './terrain'
const UP = new THREE.Vector3(0, 1, 0)
function leafMaterial(map: THREE.Texture, opts: { center?: THREE.Vector3; sway?: number; key: string }) {
const center = opts.center || new THREE.Vector3()
const mat = patch(new THREE.MeshStandardMaterial({ map, alphaTest: 0.3, alphaToCoverage: true, side: THREE.DoubleSide, roughness: 0.82, metalness: 0 }), {
key: opts.key,
vertexPars: 'uniform vec3 uCrown; uniform float uSway; attribute vec3 instanceTint; varying vec3 vTint;',
vertex: `
vTint = instanceTint;
#ifdef USE_INSTANCING
vec3 ip = (modelMatrix * instanceMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
#else
vec3 ip = vec3(0.0);
#endif
float ph = ip.x * 0.21 + ip.z * 0.13;
float sw = sin(uTime * 1.4 + ph) * 0.6 + sin(uTime * 2.6 + ph * 1.9) * 0.3;
float rustle = sin(uTime * 6.0 + position.x * 4.0 + position.y * 3.0 + ph * 5.0);
transformed += vec3(sw * 0.6, sw * 0.15, sw * 0.3) * uSway * uWind + normal * rustle * 0.04 * uWind;
`,
fragmentPars: 'uniform vec3 uSunColor; varying vec3 vTint;',
colorFrag: 'diffuseColor.rgb *= vTint;',
fragment: `
vec3 vd = normalize(vFogWorld - cameraPosition);
float back = pow(max(dot(vd, uSunDir), 0.0), 3.0);
totalEmissiveRadiance += diffuseColor.rgb * uSunColor * back * 0.55;
`,
uniforms: { uCrown: { value: center }, uSway: { value: opts.sway ?? 0.15 }, uSunColor: U.uSunColor },
})
const prev = mat.onBeforeCompile
mat.onBeforeCompile = (s, r) => {
prev.call(mat, s, r)
// spherical "crown" normals give soft volumetric shading instead of flat cards
s.vertexShader = s.vertexShader.replace('#include <beginnormal_vertex>', `
vec3 objectNormal = normal;
#ifdef USE_INSTANCING
vec3 ipn = (modelMatrix * instanceMatrix * vec4(position * 0.6, 1.0)).xyz;
vec3 wn = normalize(ipn - uCrown + vec3(0.0, 2.0, 0.0));
objectNormal = normalize(transpose(mat3(modelMatrix * instanceMatrix)) * wn);
#endif
#ifdef USE_TANGENT
vec3 objectTangent = vec3( tangent.xyz );
#endif
`)
}
return mat
}
function barkMaterial(key: string) {
const T = getTextures()
return patch(new THREE.MeshStandardMaterial({ map: T.bark, normalMap: T.barkN, roughness: 0.92, color: new THREE.Color(0.85, 0.82, 0.78) }), { key })
}
/** Cluster card: three crossed quads. */
function clusterGeometry(size: number) {
const b = new Builder()
const h = size / 2
for (let k = 0; k < 3; k++) {
const a = (k / 3) * Math.PI
const c = Math.cos(a) * h, s = Math.sin(a) * h
const tilt = k === 2 ? 0.5 : 0
b.quad({ x: -c, y: -h + tilt, z: -s }, { x: c, y: -h - tilt, z: s }, { x: c, y: h - tilt, z: s }, { x: -c, y: h + tilt, z: -s }, [0, 0, 1, 0, 1, 1, 0, 1])
}
// a horizontal card to fill the crown from above
b.quad({ x: -h, y: 0, z: h }, { x: h, y: 0, z: h }, { x: h, y: 0, z: -h }, { x: -h, y: 0, z: -h }, [0, 0, 1, 0, 1, 1, 0, 1], { x: 0, y: 1, z: 0 })
return b.build()
}
interface BranchOpts { maxDepth: number; spread: number; lenFall: number; childMin: number; childMax: number; up: number; leafEvery: number }
/** Recursive branching skeleton → bark geometry + leaf cluster anchors. */
function growTree(rng: Rng, base: THREE.Vector3, height: number, radius: number, o: BranchOpts) {
const bark = new Builder()
const leaves: { p: THREE.Vector3; s: number }[] = []
const seg = (a: THREE.Vector3, b: THREE.Vector3, r0: number, r1: number) => {
const d = new THREE.Vector3().subVectors(b, a)
const L = d.length()
const radial = r0 > 0.5 ? 14 : r0 > 0.15 ? 8 : 5
const g = new THREE.CylinderGeometry(r1, r0, L * 1.04, radial, 1, true)
const uv = g.getAttribute('uv') as THREE.BufferAttribute
for (let i = 0; i < uv.count; i++) uv.setXY(i, uv.getX(i) * Math.max(1, Math.round(r0 * 6)), uv.getY(i) * L / 2.5)
const q = new THREE.Quaternion().setFromUnitVectors(UP, d.clone().normalize())
const m = new THREE.Matrix4().compose(a.clone().addScaledVector(d, 0.5), q, new THREE.Vector3(1, 1, 1))
bark.geometry(g, m)
}
const branch = (start: THREE.Vector3, dir: THREE.Vector3, len: number, r: number, depth: number) => {
const steps = depth === 0 ? 5 : 3
let p = start.clone()
let d = dir.clone().normalize()
let rr = r
for (let i = 0; i < steps; i++) {
const sl = len / steps
// gnarled growth: random bends + gentle upward tendency
d.add(new THREE.Vector3(rrange(rng, -0.28, 0.28), rrange(rng, -0.12, 0.2) + o.up * 0.15, rrange(rng, -0.28, 0.28))).normalize()
const np = p.clone().addScaledVector(d, sl)
const nr = rr * (depth === 0 ? 0.88 : 0.8)
seg(p, np, rr, nr)
if (depth >= o.maxDepth - 1 && i >= steps - 2 && rng() < o.leafEvery) leaves.push({ p: np.clone(), s: 1 })
p = np
rr = nr
}
if (depth >= o.maxDepth) {
leaves.push({ p: p.clone(), s: 1.15 })
return
}
const kids = o.childMin + Math.floor(rng() * (o.childMax - o.childMin + 1))
for (let k = 0; k < kids; k++) {
const az = (k / kids) * Math.PI * 2 + rng() * 1.2
const tilt = o.spread * (0.6 + rng() * 0.6)
// child direction: rotate parent dir away by `tilt` around a random perpendicular axis
const perp = new THREE.Vector3(Math.cos(az), 0, Math.sin(az)).cross(d).normalize()
if (perp.lengthSq() < 1e-4) perp.set(1, 0, 0)
const cd = d.clone().applyAxisAngle(perp, tilt)
cd.y = Math.max(cd.y, -0.15)
branch(p, cd, len * o.lenFall * (0.8 + rng() * 0.4), rr * 0.72, depth + 1)
}
}
branch(base, UP.clone(), height * 0.32, radius, 0)
return { bark, leaves }
}
export interface TreesOut { group: THREE.Group; camphorTop: THREE.Vector3 }
export function createTrees(quality: 'low' | 'medium' | 'high' = 'high'): TreesOut {
const density = quality === 'high' ? 1 : quality === 'medium' ? 0.55 : 0.32
const T = getTextures()
const group = new THREE.Group()
const rng = mulberry32(777)
// ------------------------------------------------ ancient camphor (古樟)
const tb = new THREE.Vector3(TREE_POS.x, GROUND_Y - 0.4, TREE_POS.z)
const camphor = growTree(rng, tb, 24, 1.25, { maxDepth: 4, spread: 0.75, lenFall: 0.72, childMin: 2, childMax: 4, up: 0.2, leafEvery: 0.7 })
// buttress roots
for (let k = 0; k < 7; k++) {
const a = (k / 7) * Math.PI * 2 + rng() * 0.4
const len = 3 + rng() * 2.5
const g = new THREE.CylinderGeometry(0.12, 0.55, len, 8, 1, true)
const end = new THREE.Vector3(Math.cos(a) * len * 0.9, -0.55, Math.sin(a) * len * 0.9)
const startP = new THREE.Vector3(Math.cos(a) * 0.6, 1.1, Math.sin(a) * 0.6)
const d = end.clone().sub(startP)
const q = new THREE.Quaternion().setFromUnitVectors(UP, d.clone().normalize().negate())
camphor.bark.geometry(g, new THREE.Matrix4().compose(tb.clone().add(startP).addScaledVector(d, 0.5), q, new THREE.Vector3(1, 1, 1)))
}
const barkMat = barkMaterial('bark')
const barkMesh = new THREE.Mesh(camphor.bark.build(), barkMat)
barkMesh.castShadow = true
barkMesh.receiveShadow = true
group.add(barkMesh)
const crown = new THREE.Vector3()
camphor.leaves.forEach((l) => crown.add(l.p))
crown.multiplyScalar(1 / camphor.leaves.length)
{
const geo = clusterGeometry(3.4)
const list: { p: THREE.Vector3; s: number }[] = []
for (const l of camphor.leaves) {
list.push(l)
// densify around each tip
for (let k = 0; k < 3; k++) list.push({ p: l.p.clone().add(new THREE.Vector3(rrange(rng, -1.6, 1.6), rrange(rng, -0.8, 1.0), rrange(rng, -1.6, 1.6))), s: 0.9 })
}
const mat = leafMaterial(T.leaf, { center: crown, sway: 0.06, key: 'camphorLeaf' })
const inst = instancedClusters(geo, mat, list, rng, [0.36, 0.5, 0.22], 0.25)
group.add(inst)
}
// ------------------------------------------------ village trees & willows
const houses = buildHouses()
const occupied = (x: number, z: number, r: number) => houses.some((h) => Math.hypot(h.x - x, h.z - z) < Math.max(h.w, h.d) / 2 + r)
const smallBark = new Builder()
const smallLeaves: { p: THREE.Vector3; s: number }[] = []
const willowStrands: { p: THREE.Vector3; s: number }[] = []
let placed = 0
for (let tries = 0; tries < 600 && placed < 46; tries++) {
const s = rrange(rng, VILLAGE_S0 - 60, VILLAGE_S1 + 80)
const side = rng() < 0.5 ? -1 : 1
const off = side * rrange(rng, 18, 75)
const p = riverPoint(s, off)
if (occupied(p.x, p.z, 3.5)) continue
if (Math.hypot(p.x - TREE_POS.x, p.z - TREE_POS.z) < 22) continue
const y = terrainHeight(p.x, p.z)
if (y < GROUND_Y - 0.5) continue
const t = growTree(rng, new THREE.Vector3(p.x, y - 0.2, p.z), rrange(rng, 9, 14), rrange(rng, 0.22, 0.38), { maxDepth: 3, spread: 0.7, lenFall: 0.7, childMin: 2, childMax: 3, up: 0.3, leafEvery: 0.6 })
smallBark.geometry(t.bark.build())
smallLeaves.push(...t.leaves)
placed++
}
// weeping willows on the banks just outside the houses
const willowBark = new Builder()
for (const [s, side] of [[VILLAGE_S0 - 40, 1], [VILLAGE_S0 - 18, -1], [VILLAGE_S1 + 12, 1], [VILLAGE_S1 + 34, -1], [VILLAGE_S1 + 60, 1], [VILLAGE_S0 - 70, 1], [VILLAGE_S0 - 95, -1]] as [number, number][]) {
const w = riverHalfWidth(s)
const p = riverPoint(s, side * (w + 3.5))
const y = terrainHeight(p.x, p.z)
const t = growTree(rng, new THREE.Vector3(p.x, y - 0.2, p.z), 11, 0.4, { maxDepth: 2, spread: 0.9, lenFall: 0.75, childMin: 3, childMax: 4, up: 0.1, leafEvery: 0.9 })
willowBark.geometry(t.bark.build())
for (const l of t.leaves) willowStrands.push(l)
}
smallBark.geometry(willowBark.build())
const sbMesh = new THREE.Mesh(smallBark.build(), barkMaterial('bark2'))
sbMesh.castShadow = true
group.add(sbMesh)
{
const geo = clusterGeometry(2.8)
const mat = leafMaterial(T.leaf, { sway: 0.08, key: 'treeLeaf' })
group.add(instancedClusters(geo, mat, smallLeaves, rng, [0.32, 0.46, 0.2], 0.3))
}
{
// willow: long hanging strands (vertical cards anchored at the top)
const b = new Builder()
for (let k = 0; k < 3; k++) {
const a = (k / 3) * Math.PI
const c = Math.cos(a) * 1.4, s = Math.sin(a) * 1.4
b.quad({ x: -c, y: -5.5, z: -s }, { x: c, y: -5.5, z: s }, { x: c, y: 0, z: s }, { x: -c, y: 0, z: -s }, [0, 0, 1, 0, 1, 1, 0, 1])
}
const geo = b.build()
const mat = leafMaterial(T.bambooLeaf, { sway: 0.0, key: 'willow' })
const prev = mat.onBeforeCompile
mat.onBeforeCompile = (sh, r) => {
prev.call(mat, sh, r)
sh.vertexShader = sh.vertexShader.replace('#include <project_vertex>', `
{
float hang = clamp(-position.y / 5.5, 0.0, 1.0);
transformed.x += sin(uTime * 1.2 + position.z + vTint.x * 9.0) * hang * hang * 0.7 * uWind;
transformed.z += cos(uTime * 0.9 + position.x) * hang * hang * 0.4 * uWind;
}
#include <project_vertex>`)
}
const list: { p: THREE.Vector3; s: number }[] = []
for (const l of willowStrands) for (let k = 0; k < 4; k++) list.push({ p: l.p.clone().add(new THREE.Vector3(rrange(rng, -1.5, 1.5), rrange(rng, -0.3, 0.6), rrange(rng, -1.5, 1.5))), s: 1 })
group.add(instancedClusters(geo, mat, list, rng, [0.42, 0.58, 0.22], 0.2, true))
}
// ------------------------------------------------ bamboo groves
group.add(createBamboo(rng, density))
// ------------------------------------------------ hillside forest
group.add(createForest(rng, density))
// ------------------------------------------------ reeds
group.add(createReeds(rng))
return { group, camphorTop: crown }
}
function instancedClusters(geo: THREE.BufferGeometry, mat: THREE.Material, list: { p: THREE.Vector3; s: number }[], rng: Rng, tint: [number, number, number], vary: number, upright = false) {
const inst = new THREE.InstancedMesh(geo, mat, list.length)
const tints = new Float32Array(list.length * 3)
const m = new THREE.Matrix4(), q = new THREE.Quaternion(), e = new THREE.Euler(), sc = new THREE.Vector3()
list.forEach((l, i) => {
e.set(upright ? 0 : rrange(rng, -0.4, 0.4), rng() * Math.PI * 2, upright ? 0 : rrange(rng, -0.4, 0.4))
q.setFromEuler(e)
const s = l.s * rrange(rng, 0.8, 1.2)
sc.set(s, s, s)
m.compose(l.p, q, sc)
inst.setMatrixAt(i, m)
const k = 1 + rrange(rng, -vary, vary)
const warm = rrange(rng, -0.08, 0.1)
tints[i * 3] = tint[0] * k * (1 + warm)
tints[i * 3 + 1] = tint[1] * k
tints[i * 3 + 2] = tint[2] * k * (1 - warm)
})
geo.setAttribute('instanceTint', new THREE.InstancedBufferAttribute(tints, 3))
inst.castShadow = true
inst.receiveShadow = true
inst.computeBoundingSphere()
return inst
}
function createBamboo(rng: Rng, density = 1) {
const T = getTextures()
const group = new THREE.Group()
// one clump: culms + leaf cards
const culms = new Builder()
const leafAnchors: THREE.Matrix4[] = []
const clumpRng = mulberry32(31)
for (let c = 0; c < 11; c++) {
const a = clumpRng() * Math.PI * 2, r = Math.sqrt(clumpRng()) * 1.2
const h = 9 + clumpRng() * 5
const lean = new THREE.Vector3(Math.cos(a), 0, Math.sin(a)).multiplyScalar(0.06 + clumpRng() * 0.1)
const base = new THREE.Vector3(Math.cos(a) * r, 0, Math.sin(a) * r)
const segs = 4
for (let i = 0; i < segs; i++) {
const f0 = i / segs, f1 = (i + 1) / segs
const p0 = base.clone().add(new THREE.Vector3(lean.x * h * f0 * f0 * 2, h * f0, lean.z * h * f0 * f0 * 2))
const p1 = base.clone().add(new THREE.Vector3(lean.x * h * f1 * f1 * 2, h * f1, lean.z * h * f1 * f1 * 2))
const d = p1.clone().sub(p0)
const g = new THREE.CylinderGeometry(0.045 * (1 - f1 * 0.6), 0.05 * (1 - f0 * 0.6), d.length(), 4, 1, true)
culms.geometry(g, new THREE.Matrix4().compose(p0.clone().addScaledVector(d, 0.5), new THREE.Quaternion().setFromUnitVectors(UP, d.clone().normalize()), new THREE.Vector3(1, 1, 1)))
if (f0 > 0.45) {
for (let k = 0; k < 2; k++) {
const lp = p1.clone()
const la = clumpRng() * Math.PI * 2
leafAnchors.push(new THREE.Matrix4().compose(lp.add(new THREE.Vector3(Math.cos(la) * 0.7, 0, Math.sin(la) * 0.7)), new THREE.Quaternion().setFromEuler(new THREE.Euler(clumpRng() - 0.5, la, clumpRng() - 0.5)), new THREE.Vector3(1, 1, 1)))
}
}
}
}
const leafCards = new Builder()
const lc = clusterGeometry(2.2)
for (const m of leafAnchors) leafCards.geometry(lc, m)
const culmGeo = culms.build()
const leafGeo = leafCards.build()
// grove placement: knolls and hill feet around the village, plus behind houses
const houses = buildHouses()
const spots: THREE.Matrix4[] = []
const tints: number[] = []
for (let tries = 0; tries < 9000 && spots.length < Math.round(650 * density); tries++) {
const x = rrange(rng, -520, 560), z = rrange(rng, -420, 380)
const q = riverQuery(x, z)
if (q.d < 22) continue
const grove = fbm2(x * 0.012, z * 0.012, 3, 61)
const nearHill = q.d > 60
if (grove < (nearHill ? 0.5 : 0.62)) continue
if (terraceMask(x, z) > 0.01) continue
if (houses.some((h) => Math.hypot(h.x - x, h.z - z) < Math.max(h.w, h.d) / 2 + 3)) continue
if (Math.hypot(x - TREE_POS.x, z - TREE_POS.z) < 30) continue
if (Math.hypot(x - PAVILION_POS.x, z - PAVILION_POS.z) < 10) continue
const y = terrainHeight(x, z)
if (y > 160) continue
const s = rrange(rng, 0.8, 1.25)
spots.push(new THREE.Matrix4().compose(new THREE.Vector3(x, y - 0.3, z), new THREE.Quaternion().setFromAxisAngle(UP, rng() * 6.28), new THREE.Vector3(s, s * rrange(rng, 0.85, 1.2), s)))
const k = rrange(rng, 0.85, 1.15)
tints.push(0.4 * k, 0.56 * k, 0.2 * k)
}
const culmMat = patch(new THREE.MeshStandardMaterial({ color: new THREE.Color(0.32, 0.4, 0.14), roughness: 0.5 }), {
key: 'culm',
vertex: `
#ifdef USE_INSTANCING
vec3 ip = (modelMatrix * instanceMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
#else
vec3 ip = vec3(0.0);
#endif
float hb = max(position.y, 0.0) / 12.0;
float sw = sin(uTime * 1.1 + ip.x * 0.2 + ip.z * 0.1) * 0.7 + sin(uTime * 2.3 + ip.x * 0.3) * 0.25;
transformed.x += sw * hb * hb * 0.9 * uWind;
transformed.z += sw * hb * hb * 0.4 * uWind;
`,
})
const leafMat = leafMaterial(T.bambooLeaf, { sway: 0.0, key: 'bambooLeaf' })
const prev = leafMat.onBeforeCompile
leafMat.onBeforeCompile = (sh, r) => {
prev.call(leafMat, sh, r)
sh.vertexShader = sh.vertexShader.replace('#include <project_vertex>', `
{
float hb = max(position.y, 0.0) / 12.0;
float sw = sin(uTime * 1.1 + ip.x * 0.2 + ip.z * 0.1) * 0.7 + sin(uTime * 2.3 + ip.x * 0.3) * 0.25;
transformed.x += sw * hb * hb * 0.9 * uWind;
transformed.z += sw * hb * hb * 0.4 * uWind;
}
#include <project_vertex>`)
}
const culmInst = new THREE.InstancedMesh(culmGeo, culmMat, spots.length)
const leafInst = new THREE.InstancedMesh(leafGeo, leafMat, spots.length)
spots.forEach((m, i) => { culmInst.setMatrixAt(i, m); leafInst.setMatrixAt(i, m) })
leafGeo.setAttribute('instanceTint', new THREE.InstancedBufferAttribute(new Float32Array(tints), 3))
culmInst.castShadow = false
leafInst.castShadow = true
leafInst.receiveShadow = true
culmInst.layers.set(1)
leafInst.layers.set(1)
culmInst.computeBoundingSphere()
leafInst.computeBoundingSphere()
group.add(culmInst, leafInst)
return group
}
function createForest(rng: Rng, density = 1) {
const group = new THREE.Group()
// broadleaf blob + fir cone, vertex-noise deformed
const blob = new THREE.IcosahedronGeometry(1, 1)
{
const p = blob.getAttribute('position') as THREE.BufferAttribute
for (let i = 0; i < p.count; i++) {
const v = new THREE.Vector3(p.getX(i), p.getY(i), p.getZ(i))
const n = fbm2(v.x * 1.7 + v.z, v.y * 1.7 + v.z * 0.5, 3, 4)
v.multiplyScalar(0.75 + n * 0.55)
v.y = v.y * 0.85 + 0.6
p.setXYZ(i, v.x, v.y, v.z)
}
blob.computeVertexNormals()
}
const fir = new THREE.ConeGeometry(0.55, 2.6, 7, 3)
fir.translate(0, 1.3, 0)
{
const p = fir.getAttribute('position') as THREE.BufferAttribute
for (let i = 0; i < p.count; i++) {
const y = p.getY(i)
const ring = Math.sin(y * 7.0) * 0.08
p.setX(i, p.getX(i) * (1 + ring))
p.setZ(i, p.getZ(i) * (1 + ring))
}
fir.computeVertexNormals()
}
const mats = [blob, fir].map((g, gi) => patch(new THREE.MeshStandardMaterial({ roughness: 0.95, flatShading: false }), {
key: 'forest' + gi,
vertexPars: 'attribute vec3 instanceTint; varying vec3 vTint; varying float vLocalY;',
vertex: 'vTint = instanceTint; vLocalY = position.y;',
fragmentPars: 'varying vec3 vTint; varying float vLocalY;',
colorFrag: 'diffuseColor.rgb = vTint * (0.7 + 0.3 * smoothstep(0.0, 1.6, vLocalY));',
}))
const lists: THREE.Matrix4[][] = [[], []]
const tints: number[][] = [[], []]
const area = { x0: -1500, x1: 1500, z0: -1300, z1: 1200 }
for (let tries = 0; tries < 60000 && lists[0].length + lists[1].length < Math.round(9000 * density); tries++) {
const x = rrange(rng, area.x0, area.x1), z = rrange(rng, area.z0, area.z1)
const q = riverQuery(x, z)
if (q.d < 70) continue
if (villageFactor(q.s) > 0.2 && q.d < 140) continue
if (terraceMask(x, z) > 0.02) continue
const inside = x > NEAR.x0 && x < NEAR.x1 && z > NEAR.z0 && z < NEAR.z1
const y = inside ? terrainHeight(x, z) : worldHeight(x, z)
if (y < 8) continue
const dens = fbm2(x * 0.006, z * 0.006, 3, 13)
if (dens < 0.42) continue
const kind = fbm2(x * 0.003, z * 0.003, 2, 99) > 0.55 ? 1 : 0
const s = kind === 0 ? rrange(rng, 3.2, 6.5) : rrange(rng, 2.6, 4.2)
const m = new THREE.Matrix4().compose(new THREE.Vector3(x, y - 0.6, z), new THREE.Quaternion().setFromAxisAngle(UP, rng() * 6.28), new THREE.Vector3(s * rrange(rng, 0.8, 1.2), s * (kind ? rrange(rng, 2.2, 3.2) : rrange(rng, 0.8, 1.1)), s * rrange(rng, 0.8, 1.2)))
lists[kind].push(m)
const k = rrange(rng, 0.7, 1.25)
const autumn = rng() < 0.06 ? 1 : 0
tints[kind].push((kind ? 0.035 : 0.06 + autumn * 0.12) * k, (kind ? 0.07 : 0.1 + autumn * 0.04) * k, 0.035 * k)
}
;[blob, fir].forEach((g, gi) => {
const inst = new THREE.InstancedMesh(g, mats[gi], lists[gi].length)
lists[gi].forEach((m, i) => inst.setMatrixAt(i, m))
g.setAttribute('instanceTint', new THREE.InstancedBufferAttribute(new Float32Array(tints[gi]), 3))
// distant hillside forest: main view only (no shadow / reflection passes)
inst.castShadow = false
inst.receiveShadow = false
inst.layers.set(1)
inst.computeBoundingSphere()
group.add(inst)
})
return group
}
function createReeds(rng: Rng) {
const T = getTextures()
const b = new Builder()
for (let k = 0; k < 2; k++) {
const a = (k / 2) * Math.PI
const c = Math.cos(a) * 0.9, s = Math.sin(a) * 0.9
b.quad({ x: -c, y: 0, z: -s }, { x: c, y: 0, z: s }, { x: c, y: 1.9, z: s }, { x: -c, y: 1.9, z: -s }, [0.25, 0, 0.75, 0, 0.75, 1, 0.25, 1])
}
const geo = b.build()
const list: THREE.Matrix4[] = []
const tints: number[] = []
for (let s = 0; s < 3600; s += rrange(rng, 1.5, 4)) {
const vf = villageFactor(s)
if (vf > 0.05) continue
for (const side of [-1, 1]) {
if (rng() < 0.35) continue
const w = riverHalfWidth(s)
const p = riverPoint(s, side * (w + rrange(rng, -0.8, 1.6)))
if (Math.abs(p.x) > 1400) continue
const y = terrainHeight(p.x, p.z)
const sc = rrange(rng, 0.7, 1.3)
list.push(new THREE.Matrix4().compose(new THREE.Vector3(p.x, Math.max(y, -0.2), p.z), new THREE.Quaternion().setFromAxisAngle(UP, rng() * 6.28), new THREE.Vector3(sc, sc * rrange(rng, 0.8, 1.4), sc)))
const k = rrange(rng, 0.8, 1.2)
tints.push(0.55 * k, 0.55 * k, 0.28 * k)
}
}
const mat = leafMaterial(T.bambooLeaf, { sway: 0.05, key: 'reed' })
const inst = new THREE.InstancedMesh(geo, mat, list.length)
list.forEach((m, i) => inst.setMatrixAt(i, m))
geo.setAttribute('instanceTint', new THREE.InstancedBufferAttribute(new Float32Array(tints), 3))
inst.computeBoundingSphere()
return inst
}
|