File size: 25,107 Bytes
2b0432b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdeed5c
 
2b0432b
 
cdeed5c
 
 
 
2b0432b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b02b26f
cdeed5c
2b0432b
 
 
 
 
 
 
 
 
cdeed5c
2b0432b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b7c857
 
2b0432b
 
 
4b7c857
2b0432b
 
4b7c857
2b0432b
 
 
 
 
 
 
 
 
 
 
4b7c857
2b0432b
4b7c857
2b0432b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b02b26f
2b0432b
 
 
 
b02b26f
2b0432b
cdeed5c
2b0432b
 
 
 
 
 
b02b26f
cdeed5c
b02b26f
 
2b0432b
 
cdeed5c
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// Landmarks & props: stone arch bridge, water wheel, wupeng boats, market
// stalls, stone mill, benches, well, hillside pavilion, indigo cloth racks.
import * as THREE from 'three'
import { Builder, curvedRoof, trs, V3 } from './geom'
import {
  BRIDGE_S, GROUND_Y, MARKET_S0, MARKET_S1, PAVILION_POS, riverHalfWidth, riverPoint, riverSAtX, terrainHeight, TREE_POS, WHEEL_S,
} from './layout'
import { patch, std } from './materials'
import { mulberry32, rrange } from './rng'
import { getTextures } from './textures'

const UP = new THREE.Vector3(0, 1, 0)

function projectUV(geo: THREE.BufferGeometry, scale: number) {
  const g = geo.index ? geo.toNonIndexed() : geo
  g.computeVertexNormals()
  const p = g.getAttribute('position'), n = g.getAttribute('normal')
  const uv = new Float32Array(p.count * 2)
  for (let i = 0; i < p.count; i++) {
    const ax = Math.abs(n.getX(i)), ay = Math.abs(n.getY(i)), az = Math.abs(n.getZ(i))
    let u: number, v: number
    if (az >= ax && az >= ay) { u = p.getX(i); v = p.getY(i) } else if (ay >= ax) { u = p.getX(i); v = p.getZ(i) } else { u = p.getZ(i); v = p.getY(i) }
    uv[i * 2] = u / scale
    uv[i * 2 + 1] = v / scale
  }
  g.setAttribute('uv', new THREE.BufferAttribute(uv, 2))
  return g
}

export interface StallSpot { m: THREE.Matrix4; kind: number; dressed: boolean }

export interface PropsOut {
  group: THREE.Group
  /** market stall tables (local frame: table top 2.2 × 1.0 m at y = 0.82, street toward +z) */
  stalls: StallSpot[]
  /** procedural goods (hidden when scanned assets are dressed onto the stalls) */
  proceduralGoods: THREE.Mesh[]
  wheel: THREE.Group
  wheelInfo: { center: THREE.Vector3; axis: THREE.Vector3; radius: number; tangent: THREE.Vector3 }
  bridgeInfo: { center: THREE.Vector3; across: THREE.Vector3; along: THREE.Vector3; crestY: number; halfLen: number; deckY: (x: number) => number }
  lanternSpots: { p: THREE.Vector3; on: number }[]
  boats: THREE.Object3D[]
  movingBoat: THREE.Group
  cloths: THREE.Mesh[]
  pavilion: THREE.Vector3
  mats: Record<string, THREE.Material>
}

export function createProps(): PropsOut {
  const T = getTextures()
  const group = new THREE.Group()
  const stone = new Builder(), wood = new Builder(), dark = new Builder(), red = new Builder(), roof = new Builder()
  const lanternSpots: PropsOut['lanternSpots'] = []

  const stoneMat = std({ map: T.stone, normalMap: T.stoneN, roughness: 0.86, color: new THREE.Color(0.78, 0.78, 0.75) }, { key: 'pstone' })
  const woodMat = std({ map: T.wood, roughness: 0.75, color: new THREE.Color(0.85, 0.78, 0.7) }, { key: 'pwood' })
  const darkMat = std({ color: new THREE.Color(0.05, 0.045, 0.04), roughness: 0.65 }, { key: 'pdark' })
  const redMat = std({ color: new THREE.Color(0.42, 0.07, 0.04), roughness: 0.55 }, { key: 'pred' })
  const roofMat = std({ map: T.roof, normalMap: T.roofN, roughness: 0.62, color: new THREE.Color(0.95, 0.95, 1) }, { key: 'proof' })

  // ------------------------------------------------------------ arch bridge
  const bp = riverPoint(BRIDGE_S, 0)
  const across = new THREE.Vector3(bp.nx, 0, bp.nz) // local +x points to the south bank
  const along = new THREE.Vector3(bp.tx, 0, bp.tz)
  const bw = riverHalfWidth(BRIDGE_S)
  const R = bw - 1.6
  const archC = -0.35
  const crest = archC + R + 1.15
  const halfLen = bw + 9
  const deckY = (x: number) => {
    const a = Math.abs(x)
    if (a >= halfLen) return GROUND_Y + 0.05
    const f = a / halfLen
    return GROUND_Y + 0.05 + (crest - GROUND_Y) * (1 - f * f * (0.35 + 0.65 * f))
  }
  {
    const shape = new THREE.Shape()
    shape.moveTo(-halfLen - 0.5, -2.2)
    shape.lineTo(-halfLen - 0.5, GROUND_Y + 0.05)
    // stepped deck (stairs every 0.17 m of rise)
    const stepsPts: [number, number][] = []
    let x = -halfLen
    let y = deckY(x)
    stepsPts.push([x, y])
    while (x < -0.9) {
      // find run for one riser
      let nx = x
      while (nx < -0.9 && deckY(nx) - y < 0.17) nx += 0.05
      stepsPts.push([nx, y])
      y = deckY(nx)
      stepsPts.push([nx, y])
      x = nx
    }
    const left = stepsPts
    for (const [px, py] of left) shape.lineTo(px, py)
    for (let i = left.length - 1; i >= 0; i--) shape.lineTo(-left[i][0], left[i][1])
    shape.lineTo(halfLen + 0.5, GROUND_Y + 0.05)
    shape.lineTo(halfLen + 0.5, -2.2)
    shape.lineTo(R, -2.2)
    shape.lineTo(R, archC)
    for (let a = 0; a <= 48; a++) {
      const ang = (a / 48) * Math.PI
      shape.lineTo(Math.cos(ang) * R, archC + Math.sin(ang) * R)
    }
    shape.lineTo(-R, -2.2)
    shape.closePath()
    const depth = 4.4
    const ex = new THREE.ExtrudeGeometry(shape, { depth, bevelEnabled: false, curveSegments: 1 })
    ex.translate(0, 0, -depth / 2)
    const g = projectUV(ex, 3)
    const m = new THREE.Matrix4().makeBasis(across, UP, along.clone().negate()).setPosition(bp.x, 0, bp.z)
    stone.setMatrix(m)
    stone.geometry(g)
    // voussoir ring around the arch (slightly proud of the face)
    for (const zs of [-1, 1]) {
      for (let a = 0; a < 21; a++) {
        const a0 = (a / 21) * Math.PI, a1 = ((a + 1) / 21) * Math.PI
        const am = (a0 + a1) / 2
        const rr = R + 0.32
        const bx = Math.cos(am) * rr, by = archC + Math.sin(am) * rr
        const mm = new THREE.Matrix4().compose(V3(bx, by, zs * (depth / 2 + 0.04)), new THREE.Quaternion().setFromAxisAngle(V3(0, 0, 1), am), V3(1, 1, 1))
        stone.setMatrix(m.clone().multiply(mm))
        stone.box(0, 0, 0, 0.62, R * (a1 - a0) - 0.05, 0.1, 1.5)
      }
    }
    // parapets following the smooth deck line + posts
    stone.setMatrix(m)
    for (const zs of [-1, 1]) {
      const z = zs * (depth / 2 - 0.14)
      const nseg = 26
      for (let i = 0; i < nseg; i++) {
        const xa = -halfLen + 1 + ((2 * halfLen - 2) * i) / nseg, xb = -halfLen + 1 + ((2 * halfLen - 2) * (i + 1)) / nseg
        const ya = deckY(xa), yb = deckY(xb)
        const ang = Math.atan2(yb - ya, xb - xa)
        const len = Math.hypot(xb - xa, yb - ya)
        const mm = new THREE.Matrix4().compose(V3((xa + xb) / 2, (ya + yb) / 2 + 0.42, z), new THREE.Quaternion().setFromAxisAngle(V3(0, 0, 1), ang), V3(1, 1, 1))
        stone.setMatrix(m.clone().multiply(mm))
        stone.box(0, 0, 0, len + 0.02, 0.5, 0.24, 1.5)
        stone.box(0, 0.3, 0, len + 0.04, 0.1, 0.32, 1.5)
        stone.setMatrix(m)
        if (i % 3 === 0 || i === nseg - 1) {
          const px = i === nseg - 1 ? xb : xa
          stone.box(px, deckY(px) + 0.55, z, 0.3, 0.95, 0.3, 1.5)
          stone.box(px, deckY(px) + 1.07, z, 0.22, 0.14, 0.22, 1.5)
        }
      }
    }
    // a pair of lanterns on posts at the crest
    for (const zs of [-1, 1]) {
      const p = V3(0.0, crest + 1.75, zs * (depth / 2 - 0.14)).applyMatrix4(m)
      wood.setMatrix(new THREE.Matrix4())
      wood.box(p.x, crest + 0.95, p.z, 0.1, 1.9, 0.1)
      lanternSpots.push({ p, on: 57.0 })
    }
  }

  // ------------------------------------------------------------ water wheel
  const wheel = new THREE.Group()
  const ws = WHEEL_S
  const wwid = riverHalfWidth(ws)
  const wp = riverPoint(ws, wwid - 3.0)
  const wheelR = 3.3
  const wheelC = new THREE.Vector3(wp.x, 1.25, wp.z)
  const wheelAxis = new THREE.Vector3(wp.nx, 0, wp.nz)
  const wheelTan = new THREE.Vector3(wp.tx, 0, wp.tz)
  {
    const wb = new Builder(), bamboo = new Builder()
    // wheel in local frame: axis = +z, wheel plane = xy
    const hub = new THREE.CylinderGeometry(0.42, 0.42, 1.6, 14)
    hub.rotateX(Math.PI / 2)
    wb.geometry(hub)
    for (const zs of [-0.55, 0.55]) {
      const rim = new THREE.TorusGeometry(wheelR, 0.09, 6, 64)
      wb.geometry(rim, new THREE.Matrix4().makeTranslation(0, 0, zs))
      const rim2 = new THREE.TorusGeometry(wheelR * 0.62, 0.06, 6, 48)
      wb.geometry(rim2, new THREE.Matrix4().makeTranslation(0, 0, zs))
      for (let k = 0; k < 12; k++) {
        const a = (k / 12) * Math.PI * 2 + (zs > 0 ? 0.1 : 0)
        const mm = new THREE.Matrix4().compose(V3(Math.cos(a) * wheelR * 0.5, Math.sin(a) * wheelR * 0.5, zs), new THREE.Quaternion().setFromAxisAngle(V3(0, 0, 1), a), V3(1, 1, 1))
        wb.setMatrix(mm)
        wb.box(0, 0, 0, wheelR, 0.1, 0.1)
        wb.setMatrix(new THREE.Matrix4())
      }
    }
    for (let k = 0; k < 18; k++) {
      const a = (k / 18) * Math.PI * 2
      const mm = new THREE.Matrix4().compose(V3(Math.cos(a) * (wheelR + 0.05), Math.sin(a) * (wheelR + 0.05), 0), new THREE.Quaternion().setFromAxisAngle(V3(0, 0, 1), a), V3(1, 1, 1))
      wb.setMatrix(mm)
      wb.box(0, 0, 0, 0.62, 0.05, 1.3) // paddle board
      wb.setMatrix(new THREE.Matrix4())
      // bamboo scoops tied to the rim, tilted
      const tube = new THREE.CylinderGeometry(0.075, 0.075, 0.85, 8, 1, true)
      const mt = new THREE.Matrix4().compose(V3(Math.cos(a + 0.17) * (wheelR + 0.1), Math.sin(a + 0.17) * (wheelR + 0.1), 0.78), new THREE.Quaternion().setFromEuler(new THREE.Euler(0, 0, a + 0.6)), V3(1, 1, 1))
      bamboo.geometry(tube, mt)
    }
    const wheelMesh = new THREE.Mesh(wb.build(), woodMat)
    wheelMesh.castShadow = true
    const bambooMat = std({ color: new THREE.Color(0.42, 0.4, 0.2), roughness: 0.6, side: THREE.DoubleSide }, { key: 'bamboo' })
    const bambooMesh = new THREE.Mesh(bamboo.build(), bambooMat)
    bambooMesh.castShadow = true
    wheel.add(wheelMesh, bambooMesh)
    // orient: local z -> axis
    const basis = new THREE.Matrix4().makeBasis(wheelTan, UP, wheelAxis)
    wheel.quaternion.setFromRotationMatrix(basis)
    wheel.position.copy(wheelC)
    // static frame: posts + axle bearings + trough to the bank
    const fm = new THREE.Matrix4().makeBasis(wheelTan, UP, wheelAxis).setPosition(wheelC)
    wood.setMatrix(fm)
    for (const zs of [-1.25, 1.25]) {
      for (const xs of [-1.1, 1.1]) {
        const mm = new THREE.Matrix4().compose(V3(xs * 0.6, -1.5, zs), new THREE.Quaternion().setFromAxisAngle(V3(0, 0, 1), -xs * 0.22), V3(1, 1, 1))
        wood.setMatrix(fm.clone().multiply(mm))
        wood.box(0, 0, 0, 0.22, 4.4, 0.22)
      }
      wood.setMatrix(fm)
      wood.box(0, 0, zs, 0.5, 0.35, 0.3)
    }
    wood.box(0, 0, 0, 0.16, 0.16, 3.0)
    // trough carrying lifted water over the embankment
    const tStart = V3(wheelR * 0.15, wheelR + 0.25, 1.05)
    wood.box(tStart.x, tStart.y, 1.05 + 2.6, 0.5, 0.18, 5.6)
    wood.box(tStart.x - 0.22, tStart.y + 0.15, 1.05 + 2.6, 0.06, 0.3, 5.6)
    wood.box(tStart.x + 0.22, tStart.y + 0.15, 1.05 + 2.6, 0.06, 0.3, 5.6)
    for (const zz of [3.4, 6.0]) wood.box(tStart.x, (tStart.y - GROUND_Y) / 2 + GROUND_Y - wheelC.y - 0.2, zz, 0.16, tStart.y - (GROUND_Y - wheelC.y) + 0.4, 0.16)
  }
  group.add(wheel)

  // ------------------------------------------------------------ boats
  const boats: THREE.Object3D[] = []
  const hullGeo = makeHull()
  const canopyGeo = makeCanopy()
  const hullMat = std({ color: new THREE.Color(0.1, 0.07, 0.05), roughness: 0.45, map: T.wood }, { key: 'hull' })
  const canopyMat = std({ color: new THREE.Color(0.035, 0.035, 0.035), roughness: 0.9, side: THREE.DoubleSide, map: T.basket }, { key: 'canopy' })
  const mkBoat = () => {
    const b = new THREE.Group()
    const hull = new THREE.Mesh(hullGeo, hullMat)
    const can = new THREE.Mesh(canopyGeo, canopyMat)
    hull.castShadow = can.castShadow = true
    b.add(hull, can)
    return b
  }
  const moor: [number, number, number][] = [[-165, -1, 0.15], [-152, -1, -0.1], [-92, -1, 0.05], [-5, -1, 0.1], [96, -1, -0.05], [120, 1, 0.1], [-60, 1, -0.1], [175, -1, 0.0]]
  for (const [x, side, yaw] of moor) {
    const s = riverSAtX(x)
    const w = riverHalfWidth(s)
    const p = riverPoint(s, side * (w - 1.3))
    const b = mkBoat()
    b.position.set(p.x, 0.02, p.z)
    b.rotation.y = Math.atan2(p.tx, p.tz) + yaw + (side > 0 ? Math.PI : 0)
    group.add(b)
    boats.push(b)
  }
  const movingBoat = mkBoat()
  group.add(movingBoat)

  // ------------------------------------------------------------ market stalls
  const goods = new Builder(), basket = new Builder(), jars = new Builder(), meat = new Builder(), awning = new Builder()
  const rng = mulberry32(88)
  const jarProfile = (r: number, h: number) => {
    const pts: THREE.Vector2[] = []
    for (let i = 0; i <= 10; i++) {
      const v = i / 10
      const rad = r * (0.55 + 0.6 * Math.sin(Math.PI * Math.min(1, v * 1.15)) ** 0.8) * (v > 0.85 ? 0.62 : 1)
      pts.push(new THREE.Vector2(Math.max(0.02, rad), v * h))
    }
    return new THREE.LatheGeometry(pts, 14)
  }
  const jarGeos = [jarProfile(0.28, 0.7), jarProfile(0.4, 0.9), jarProfile(0.2, 0.45)]
  const basketGeo = new THREE.CylinderGeometry(0.42, 0.32, 0.35, 16, 1, true)
  const fruitGeo = new THREE.IcosahedronGeometry(0.075, 0)
  const stalls: StallSpot[] = []
  for (let s = MARKET_S0; s < MARKET_S1; s += rrange(rng, 5.5, 8)) {
    if (Math.abs(s - BRIDGE_S) < 12) continue
    const p = riverPoint(s, -9.6)
    const m = trs(p.x, GROUND_Y, p.z, Math.atan2(-p.nx, -p.nz) + Math.PI)
    // table
    wood.setMatrix(m)
    wood.box(0, 0.78, 0, 2.2, 0.08, 1.0)
    for (const [lx, lz] of [[-1, -0.42], [1, -0.42], [-1, 0.42], [1, 0.42]]) wood.box(lx, 0.39, lz, 0.07, 0.78, 0.07)
    const kind = Math.floor(rng() * 4)
    stalls.push({ m: m.clone(), kind, dressed: false })
    if (kind === 0 || kind === 3) {
      // baskets of produce
      for (let k = 0; k < 3; k++) {
        const bx = -0.7 + k * 0.7
        basket.setMatrix(m)
        basket.geometry(basketGeo, new THREE.Matrix4().makeTranslation(bx, 0.99, 0))
        goods.setMatrix(m)
        const col = [0.75, 0.33, 0.05, 0.2, 0.45, 0.08, 0.6, 0.1, 0.06][k * 3]
        for (let f = 0; f < 14; f++) {
          const a = rng() * Math.PI * 2, r2 = Math.sqrt(rng()) * 0.3
          goods.set('aTint', k + col * 0)
          goods.geometry(fruitGeo, new THREE.Matrix4().makeTranslation(bx + Math.cos(a) * r2, 1.12 + rng() * 0.06, Math.sin(a) * r2))
        }
      }
    }
    if (kind === 1 || kind === 3) {
      // pottery jars on and around the table
      for (let k = 0; k < 5; k++) {
        const g = jarGeos[k % 3]
        const onTable = k < 3
        jars.setMatrix(m)
        jars.geometry(g, new THREE.Matrix4().makeTranslation(onTable ? -0.7 + k * 0.7 : -0.6 + (k - 3) * 1.4, onTable ? 0.82 : 0, onTable ? 0 : 0.9))
      }
    }
    if (kind === 2 || rng() < 0.4) {
      // cured-meat rack: bamboo pole on two posts with hanging strips
      wood.setMatrix(m)
      wood.box(-1.2, 1.2, -0.55, 0.06, 2.4, 0.06)
      wood.box(1.2, 1.2, -0.55, 0.06, 2.4, 0.06)
      wood.box(0, 2.35, -0.55, 2.5, 0.05, 0.05)
      meat.setMatrix(m)
      for (let k = 0; k < 7; k++) {
        const len = 0.45 + rng() * 0.3
        meat.box(-1.0 + k * 0.33, 2.3 - len / 2 - 0.05, -0.55, 0.13, len, 0.05)
      }
    }
    if (rng() < 0.55) {
      // cloth awning on poles
      wood.setMatrix(m)
      wood.box(-1.25, 1.3, 0.6, 0.05, 2.6, 0.05)
      wood.box(1.25, 1.3, 0.6, 0.05, 2.6, 0.05)
      awning.setMatrix(m)
      awning.set('aTint', rng())
      awning.quad(V3(-1.4, 2.25, 0.85), V3(1.4, 2.25, 0.85), V3(1.4, 2.65, -0.8), V3(-1.4, 2.65, -0.8))
      awning.quad(V3(-1.4, 2.65, -0.8), V3(1.4, 2.65, -0.8), V3(1.4, 2.25, 0.85), V3(-1.4, 2.25, 0.85))
    }
  }
  const fruitMat = std({ roughness: 0.5, color: new THREE.Color(1, 1, 1) }, {
    key: 'fruit', vertexPars: 'attribute float aTint; varying float vTint;', vertex: 'vTint = aTint;', fragmentPars: 'varying float vTint;',
    colorFrag: 'diffuseColor.rgb = vTint < 0.5 ? vec3(0.8, 0.32, 0.04) : vTint < 1.5 ? vec3(0.22, 0.42, 0.08) : vec3(0.62, 0.12, 0.08);',
  })
  const basketMat = std({ map: T.basket, roughness: 0.85, side: THREE.DoubleSide }, { key: 'basket' })
  const jarMat = std({ color: new THREE.Color(0.32, 0.17, 0.08), roughness: 0.28, metalness: 0.05 }, { key: 'jar' })
  const meatMat = std({ color: new THREE.Color(0.32, 0.08, 0.04), roughness: 0.4 }, { key: 'meat' })
  const awningMat = std({ roughness: 0.9, side: THREE.DoubleSide }, {
    key: 'awning', vertexPars: 'attribute float aTint; varying float vTint;', vertex: 'vTint = aTint;', fragmentPars: 'varying float vTint;',
    colorFrag: 'diffuseColor.rgb = vTint < 0.5 ? vec3(0.62, 0.55, 0.42) : vec3(0.12, 0.18, 0.36);',
  })

  // ------------------------------------------------------------ village entrance: mill, benches, well
  {
    const tp = TREE_POS
    const gy = GROUND_Y
    const millP = V3(tp.x + 9, gy, tp.z + 6)
    const m = trs(millP.x, gy, millP.z, 0.4)
    const millBase = new THREE.CylinderGeometry(1.5, 1.6, 0.65, 28)
    stone.setMatrix(m)
    stone.geometry(projectUV(millBase, 1.5), new THREE.Matrix4().makeTranslation(0, 0.33, 0))
    const roller = new THREE.CylinderGeometry(0.42, 0.42, 1.1, 20)
    roller.rotateZ(Math.PI / 2)
    stone.geometry(projectUV(roller, 1.2), new THREE.Matrix4().makeTranslation(0.65, 1.07, 0))
    stone.geometry(projectUV(new THREE.CylinderGeometry(0.12, 0.12, 0.7, 8), 1), new THREE.Matrix4().makeTranslation(0, 0.95, 0))
    wood.setMatrix(m)
    wood.box(0.2, 1.07, 0, 2.6, 0.08, 0.08)
    // benches around the tree
    for (let k = 0; k < 4; k++) {
      const a = 0.3 + k * 1.15
      const bm = trs(tp.x + Math.cos(a) * 5.2, gy, tp.z + Math.sin(a) * 5.2, -a + Math.PI / 2)
      stone.setMatrix(bm)
      stone.box(0, 0.48, 0, 2.2, 0.12, 0.55, 1.5)
      stone.box(-0.8, 0.22, 0, 0.18, 0.44, 0.45, 1.5)
      stone.box(0.8, 0.22, 0, 0.18, 0.44, 0.45, 1.5)
    }
    // old well
    const wm = trs(tp.x - 7.5, gy, tp.z + 7, 0)
    stone.setMatrix(wm)
    const ring = new THREE.CylinderGeometry(0.75, 0.8, 0.8, 18, 1, true)
    stone.geometry(projectUV(ring, 1.2), new THREE.Matrix4().makeTranslation(0, 0.4, 0))
    const ringT = new THREE.TorusGeometry(0.72, 0.1, 6, 18)
    ringT.rotateX(Math.PI / 2)
    stone.geometry(projectUV(ringT, 1), new THREE.Matrix4().makeTranslation(0, 0.8, 0))
    dark.setMatrix(wm)
    dark.quad(V3(-0.68, 0.5, 0.0), V3(0.68, 0.5, 0.0), V3(0.68, 0.5, -0.01), V3(-0.68, 0.5, -0.01))
    // plaza paving
    const pave = new THREE.CircleGeometry(15, 40)
    pave.rotateX(-Math.PI / 2)
    stone.setMatrix(new THREE.Matrix4())
    stone.geometry(projectUV(pave, 4), new THREE.Matrix4().makeTranslation(tp.x, gy + 0.025, tp.z))
  }

  // ------------------------------------------------------------ indigo cloth racks
  const cloths: THREE.Mesh[] = []
  {
    const tp = TREE_POS
    const base = V3(tp.x + 6, GROUND_Y, tp.z + 13.5)
    const rot = 0.99
    const m = trs(base.x, base.y, base.z, rot)
    wood.setMatrix(m)
    for (const z of [0, 2.4]) {
      for (const x of [-4.4, 0, 4.4]) wood.box(x, 2.9, z, 0.12, 5.8, 0.12)
      // bamboo crossbar
    }
    const bar = new THREE.CylinderGeometry(0.07, 0.07, 9.4, 8)
    bar.rotateZ(Math.PI / 2)
    const barMat = std({ color: new THREE.Color(0.45, 0.42, 0.22), roughness: 0.55 }, { key: 'bar' })
    for (const z of [0, 2.4]) {
      const bm = new THREE.Mesh(bar, barMat)
      bm.position.set(0, 5.7, z).applyMatrix4(m)
      bm.rotation.y = rot
      bm.castShadow = true
      group.add(bm)
    }
    const clothGeo = new THREE.PlaneGeometry(1.15, 4.9, 3, 18)
    clothGeo.translate(0, -2.45, 0)
    for (let k = 0; k < 10; k++) {
      const row = k % 2
      const x = -3.9 + Math.floor(k / 2) * 1.75 + row * 0.6
      const mat = patch(new THREE.MeshStandardMaterial({ map: T.cloth[k % 3], roughness: 0.92, side: THREE.DoubleSide }), {
        key: 'cloth',
        vertex: `
          float hang = clamp(-position.y / 4.9, 0.0, 1.0);
          float ph = ${(k * 1.37).toFixed(2)};
          float wv = sin(uTime * 1.7 + ph + hang * 2.6) * 0.5 + sin(uTime * 2.9 + ph * 1.3 + hang * 4.0) * 0.22;
          transformed.z += wv * hang * hang * 0.55 * uWind + hang * hang * 0.35 * uWind;
          transformed.x += sin(uTime * 1.1 + ph + hang * 1.5) * hang * 0.06;
        `,
      })
      const cm = new THREE.Mesh(clothGeo, mat)
      cm.position.set(x, 5.72, row * 2.4).applyMatrix4(m)
      cm.rotation.y = rot
      cm.castShadow = true
      cm.receiveShadow = true
      group.add(cm)
      cloths.push(cm)
    }
  }

  // ------------------------------------------------------------ hillside pavilion (六角亭)
  const pv = V3(PAVILION_POS.x, terrainHeight(PAVILION_POS.x, PAVILION_POS.z), PAVILION_POS.z)
  {
    const m = trs(pv.x, pv.y, pv.z, 0.3)
    stone.setMatrix(m)
    const plat = new THREE.CylinderGeometry(3.3, 3.6, 1.6, 6)
    stone.geometry(projectUV(plat, 2), new THREE.Matrix4().makeTranslation(0, -0.2, 0))
    red.setMatrix(m)
    const Rp = 2.5
    for (let k = 0; k < 6; k++) {
      const a = (k / 6) * Math.PI * 2
      red.box(Math.cos(a) * Rp, 0.6 + 1.6, Math.sin(a) * Rp, 0.24, 3.2, 0.24)
    }
    // beams
    for (let k = 0; k < 6; k++) {
      const a0 = (k / 6) * Math.PI * 2, a1 = ((k + 1) / 6) * Math.PI * 2
      const x0 = Math.cos(a0) * Rp, z0 = Math.sin(a0) * Rp, x1 = Math.cos(a1) * Rp, z1 = Math.sin(a1) * Rp
      const mm = new THREE.Matrix4().compose(V3((x0 + x1) / 2, 3.65, (z0 + z1) / 2), new THREE.Quaternion().setFromAxisAngle(UP, -Math.atan2(z1 - z0, x1 - x0)), V3(1, 1, 1))
      red.setMatrix(m.clone().multiply(mm))
      red.box(0, 0, 0, Rp + 0.1, 0.3, 0.18)
      red.box(0, -2.85, 0, Rp, 0.08, 0.3) // bench rail
    }
    roof.setMatrix(m)
    const corners: THREE.Vector2[] = []
    const tops: THREE.Vector3[] = []
    for (let k = 0; k < 6; k++) {
      const a = (k / 6) * Math.PI * 2
      corners.push(new THREE.Vector2(Math.cos(a) * 3.9, Math.sin(a) * 3.9))
      tops.push(V3(0, 6.6, 0))
    }
    curvedRoof(roof, corners, tops, 3.85, 0.9, 0.55, 1.2, 6, 7)
    dark.setMatrix(m)
    dark.geometry(new THREE.SphereGeometry(0.28, 10, 8), new THREE.Matrix4().makeTranslation(0, 6.75, 0))
    dark.geometry(new THREE.ConeGeometry(0.12, 0.8, 8), new THREE.Matrix4().makeTranslation(0, 7.2, 0))
    lanternSpots.push({ p: V3(0, 3.3, 0).applyMatrix4(m), on: 58.5 })
  }

  const add = (b: Builder, mat: THREE.Material, cast = true, reflect = true) => {
    if (!b.count) return
    const mesh = new THREE.Mesh(b.build(), mat)
    mesh.castShadow = cast
    mesh.receiveShadow = true
    if (!reflect) mesh.layers.set(1)
    group.add(mesh)
    return mesh
  }
  add(stone, stoneMat)
  add(wood, woodMat)
  add(dark, darkMat)
  add(red, redMat)
  add(roof, roofMat)
  // market goods: small, main view only
  const proceduralGoods = [add(goods, fruitMat, false, false), add(basket, basketMat, false, false), add(jars, jarMat, false, false)].filter((m) => !!m) as THREE.Mesh[]
  add(meat, meatMat, true, false)
  add(awning, awningMat, true, false)

  return {
    group, wheel, stalls, proceduralGoods,
    wheelInfo: { center: wheelC, axis: wheelAxis, radius: wheelR, tangent: wheelTan },
    bridgeInfo: { center: V3(bp.x, 0, bp.z), across, along, crestY: crest, halfLen, deckY },
    lanternSpots, boats, movingBoat, cloths, pavilion: pv,
    mats: { stone: stoneMat, wood: woodMat, dark: darkMat },
  }
}

function makeHull() {
  // lofted hull: length along z (bow +z), 7.6 m long
  const L = 7.6, segL = 24, segC = 10
  const pos: number[] = [], idx: number[] = []
  for (let i = 0; i <= segL; i++) {
    const u = (i / segL) * 2 - 1 // -1 stern .. 1 bow
    const z = u * L * 0.5
    const beam = 0.78 * Math.pow(Math.max(0, 1 - Math.pow(Math.abs(u), 2.4)), 0.55) + 0.04
    const sheer = 0.55 + 0.35 * Math.pow(Math.abs(u), 3) // raised ends
    const keel = -0.35 + 0.25 * Math.pow(Math.abs(u), 2)
    for (let j = 0; j <= segC; j++) {
      const a = (j / segC) * Math.PI // 0..pi from port gunwale under keel to starboard
      const x = -Math.cos(a) * beam
      const y = keel + (sheer - keel) * (1 - Math.sin(a)) ** 0.8
      pos.push(x, y, z)
    }
  }
  const w = segC + 1
  for (let i = 0; i < segL; i++) for (let j = 0; j < segC; j++) {
    const a = i * w + j, b = a + w
    idx.push(a, a + 1, b, a + 1, b + 1, b)
  }
  // deck (cover) as a flat-ish strip
  const base = pos.length / 3
  for (let i = 0; i <= segL; i++) {
    const u = (i / segL) * 2 - 1
    const beam = 0.78 * Math.pow(Math.max(0, 1 - Math.pow(Math.abs(u), 2.4)), 0.55) + 0.04
    const sheer = 0.55 + 0.35 * Math.pow(Math.abs(u), 3)
    pos.push(-beam, sheer - 0.02, u * L * 0.5, beam, sheer - 0.02, u * L * 0.5)
  }
  for (let i = 0; i < segL; i++) {
    const a = base + i * 2
    idx.push(a, a + 2, a + 1, a + 1, a + 2, a + 3)
  }
  const g = new THREE.BufferGeometry()
  g.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3))
  g.setIndex(idx)
  g.computeVertexNormals()
  const uv: number[] = []
  for (let i = 0; i < pos.length / 3; i++) uv.push(pos[i * 3 + 2] / 2, pos[i * 3 + 1] / 2)
  g.setAttribute('uv', new THREE.Float32BufferAttribute(uv, 2))
  return g
}

function makeCanopy() {
  // three overlapping black bamboo-mat arcs (乌篷)
  const b = new Builder()
  const arcs = [[-0.9, 1.1, 0.72], [0.05, 1.0, 0.76], [0.95, 0.9, 0.7]]
  for (const [z, len, r] of arcs) {
    const g = new THREE.CylinderGeometry(r, r, len, 16, 1, true, -Math.PI / 2, Math.PI)
    g.rotateX(Math.PI / 2)
    g.rotateZ(0)
    b.geometry(g, new THREE.Matrix4().makeTranslation(0, 0.55, z))
  }
  return b.build()
}