File size: 37,279 Bytes
83fd0a4
 
 
 
f2b420f
 
 
 
 
 
 
 
 
83fd0a4
 
 
 
 
 
 
 
 
 
 
 
f2b420f
 
 
 
83fd0a4
f2b420f
 
 
 
59961f5
f2b420f
 
 
2f85d31
 
 
f2b420f
2f85d31
 
 
 
 
 
59961f5
2f85d31
83fd0a4
2f85d31
 
f2b420f
 
 
 
 
59961f5
f2b420f
83fd0a4
 
 
 
f2b420f
2f85d31
f2b420f
 
 
 
 
 
 
59961f5
 
f2b420f
 
 
83fd0a4
f2b420f
 
83fd0a4
 
 
f2b420f
83fd0a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2b420f
 
83fd0a4
 
 
 
 
 
f2b420f
 
 
83fd0a4
 
 
 
 
 
 
 
 
 
f2b420f
83fd0a4
f2b420f
 
83fd0a4
 
 
 
 
 
 
f2b420f
 
 
 
 
 
 
 
 
 
2f85d31
f2b420f
 
 
83fd0a4
 
 
 
f2b420f
 
83fd0a4
 
 
 
f2b420f
83fd0a4
 
 
 
 
 
 
 
 
f2b420f
83fd0a4
 
 
 
 
 
f2b420f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83fd0a4
 
 
 
 
f2b420f
 
 
 
 
 
 
 
 
 
 
 
83fd0a4
 
f2b420f
 
83fd0a4
 
 
 
 
 
 
 
 
 
 
 
 
f2b420f
83fd0a4
 
 
 
 
f2b420f
83fd0a4
 
 
f2b420f
83fd0a4
f2b420f
83fd0a4
 
f2b420f
 
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
// STUDIUS World P8 (realistic, compact): a colossal fossil bee stinger (~10 km) lies crashed and half-buried in the plain like a
// mountain range, base (broken end) in the east, tip dug into the ground in the west. Soil, meadow and forest overgrew its back;
// its exposed chitin flanks carry a tarn, a river and a waterfall down to a valley lake. The temple stands on the snowy summit near
// the base; the 5 category terraces step down the back along one short path. Sun rises in the east (+X). Units: metres.
import * as THREE from './vendor/three.module.min.js';
import { ImprovedNoise } from './vendor/addons/math/ImprovedNoise.js';
import { Sky } from './vendor/addons/objects/Sky.js';

const N3 = new ImprovedNoise(); const nz = (x, y, z = 0) => N3.noise(x, y, z);
const fbm = (x, z, o = 5, s = 0) => { let a = 0, f = 1, w = .5; for (let i = 0; i < o; i++) { a += w * nz(x * f, z * f, s + i * 7.1); f *= 2.03; w *= .5; } return a; };
const ridged = (x, z, o = 5) => { let a = 0, f = 1, w = .55; for (let i = 0; i < o; i++) { const n = 1 - Math.abs(nz(x * f, z * f, 3.3 + i)); a += w * n * n; f *= 2.07; w *= .48; } return a; };
const sstep = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); };
let seed = 11; const rnd = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
export const SUN_DIR = new THREE.Vector3(1, .085, .32).normalize();
const XB = 1500, LEN = 10000, R0 = 1700; // base x, length, base radius
const SU = { x: 700 }; export const MC = new THREE.Vector2(SU.x, 0); // summit (temple); MC.y set from the axis below
const CATN = ['Erschaffen', 'Veröffentlichen', 'Verkaufen', 'Team', 'Dev'];
const TX = [-60, -620, -1180, -1740, -2300]; // terrace centres (x) down the back
const zA = (x) => 420 * Math.sin(Math.min(1, Math.max(0, (XB - x) / LEN)) * Math.PI * .9); // gently curved axis
MC.y = zA(SU.x);
const sR = (u) => R0 * Math.pow(Math.max(0, 1 - u), .9), sC = (u) => -.28 * sR(u) - 200 * u * u; // radius, centre height (tip dives in)
const uOf = (x) => (XB - x) / LEN;
const TARN = { x: -900, z: zA(-900) + 300, r: 85 }; let TARN_Y = 0;
const LAKE = { x: -1500, z: 0, r: 420 }; LAKE.z = zA(-1500) + sR(uOf(-1500)) + 900; let LAKE_Y = 0;
const VILLAGE = { x: -400, z: LAKE.z + 650, r: 300 };
/* ---------- textures ---------- */
const TL = new THREE.TextureLoader();
const tex = (p, srgb, rep = 1) => { const t = TL.load(p); t.wrapS = t.wrapT = THREE.RepeatWrapping; t.repeat.set(rep, rep); t.anisotropy = 8; if (srgb) t.colorSpace = THREE.SRGBColorSpace; return t; };
function canvasTex(w, h, draw, srgb = true) { const c = document.createElement('canvas'); c.width = w; c.height = h; draw(c.getContext('2d'), w, h); const t = new THREE.CanvasTexture(c); t.wrapS = t.wrapT = THREE.RepeatWrapping; t.anisotropy = 8; if (srgb) t.colorSpace = THREE.SRGBColorSpace; return t; }
export const M = {}; const SNOW = { value: 1300, type: 'float' }, TOPU = { value: new THREE.Vector3(), type: 'vec3' };
// shared triplanar injection (world-space, no stretching on cliffs)
const TRI = `vec3 triT(sampler2D t, vec3 p, vec3 n, float sc){ vec3 b = pow(abs(n), vec3(4.0)); b /= (b.x+b.y+b.z); return texture2D(t,p.zy*sc).rgb*b.x + texture2D(t,p.xz*sc).rgb*b.y + texture2D(t,p.xy*sc).rgb*b.z; }`;
function withWorld(m, frag, uniforms) { m.onBeforeCompile = (s) => { Object.assign(s.uniforms, uniforms);
  s.vertexShader = s.vertexShader.replace('#include <common>', '#include <common>\nvarying vec3 vWP; varying vec3 vWN;').replace('#include <worldpos_vertex>', '#include <worldpos_vertex>\nvWP = (modelMatrix * vec4(transformed,1.0)).xyz; vWN = normalize(mat3(modelMatrix) * objectNormal);');
  s.fragmentShader = s.fragmentShader.replace('#include <common>', '#include <common>\nvarying vec3 vWP; varying vec3 vWN;\n' + Object.keys(uniforms).map(k => `uniform ${uniforms[k].type || 'sampler2D'} ${k};`).join('\n') + '\n' + TRI).replace('#include <map_fragment>', frag).replace('#include <color_fragment>', ''); }; m.customProgramCacheKey = () => frag; return m; }
function makeMaterials(q) {
  const rockD = tex('assets/tex/rock_face-diff.webp', true), rockN = tex('assets/tex/rock_face-nor_gl.webp'), aerD = tex('assets/tex/aerial_rocks_02-diff.webp', true), aerN = tex('assets/tex/aerial_rocks_02-nor_gl.webp');
  // terrain: grass/forest floor on flats, rock on slopes, snow on high flats; colour from vertex attr + triplanar detail
  const T = (n, srgb) => tex('assets/tex/' + n + '.webp', srgb), grD = T('aerial_grass_rock-diff', true), grN = T('aerial_grass_rock-nor_gl'), fgD = T('forrest_ground_01-diff', true), snD = T('snow_02-diff', true);
  // terrain splat: grass / forest floor / dirt / rock / snow by slope, height and the land mask (vertex colour = biome tint)
  M.terrain = withWorld(new THREE.MeshStandardMaterial({ vertexColors: true, roughness: .95, normalMap: grN, normalScale: new THREE.Vector2(1.0, 1.0) }), `
    float slope = 1.0 - clamp(vWN.y, 0.0, 1.0);
    vec3 macro = triT(tGr, vWP, vWN, 0.00013);
    vec3 gr = triT(tGr, vWP, vWN, 0.012) * 0.55 + triT(tGr, vWP, vWN, 0.0011) * 0.65;
    vec3 fg = triT(tFg, vWP, vWN, 0.05);
    vec3 rk = triT(tRock, vWP, vWN, 0.01) * 0.6 + triT(tAer, vWP, vWN, 0.0012) * 0.5;
    float forest = vColor.g - vColor.r; // >0 on wooded land
    vec3 c = mix(gr, fg * vec3(0.8,0.9,0.7), clamp(forest * 4.0, 0.0, 0.6)) * (0.75 + 0.5 * macro);
    c *= mix(vec3(1.0), vColor.rgb * 2.4, 0.55) * vec3(0.8, 0.9, 0.75);
    c = mix(c, rk * vec3(0.85,0.82,0.78), smoothstep(0.28, 0.5, slope));
    float snow = smoothstep(uSnow, uSnow + 140.0, vWP.y + 120.0 * (macro.g - 0.4) + 40.0 * sin(vWP.x*0.01) * cos(vWP.z*0.012)) * smoothstep(1400.0, 700.0, length(vWP.xz - uTop.xz)) * (1.0 - smoothstep(0.7, 0.92, slope));
    c = mix(c, vec3(0.86, 0.88, 0.92) * (0.8 + 0.25 * triT(tSn, vWP, vWN, 0.02).r), snow);
    diffuseColor.rgb = c;`, { tAer: { value: aerD }, tRock: { value: rockD }, tGr: { value: grD }, tFg: { value: fgD }, tSn: { value: snD }, uSnow: SNOW, uTop: TOPU });
  // stinger: weathered stone-chitin, natural amber-ochre banding, lichen; huge-scale + close-scale detail
  M.stinger = withWorld(new THREE.MeshStandardMaterial({ vertexColors: true, roughness: .93, metalness: 0, normalMap: rockN, normalScale: new THREE.Vector2(1.1, 1.1) }), `
    vec3 a = triT(tRock, vWP, vWN, 0.004), b = triT(tRock, vWP, vWN, 0.00021), d = triT(tAer, vWP, vWN, 0.0000135);
    vec3 c = vColor.rgb * (a * 0.55 + b * 0.75 + d * 0.5) * 1.15;
    diffuseColor.rgb = c;`, { tRock: { value: rockD }, tAer: { value: aerD } });
  M.spire = withWorld(new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 1, envMapIntensity: .5, normalMap: rockN, normalScale: new THREE.Vector2(1.3, 1.3) }), `
    vec3 a = triT(tRock, vWP, vWN, 0.02), b = triT(tRock, vWP, vWN, 0.0016);
    vec3 c = vColor.rgb * dot(a * 0.7 + b * 0.7, vec3(0.3, 0.5, 0.2)) * 1.6;
    float moss = smoothstep(0.6, 0.92, vWN.y) * smoothstep(-0.2, 0.4, sin(vWP.x*0.004)*cos(vWP.z*0.005) + sin(vWP.y*0.003));
    c = min(c, vec3(0.36, 0.29, 0.21));
    c = mix(c, vec3(0.2,0.25,0.1) * (0.7 + 0.6*b.g), moss * 0.12);
    diffuseColor.rgb = c;`, { tRock: { value: rockD } });
  M.barb = new THREE.MeshStandardMaterial({ map: tex('assets/tex/rock_face-diff.webp', true, 1), normalMap: rockN, color: 0x3a3028, roughness: .9 });
  M.stone = new THREE.MeshStandardMaterial({ map: tex('assets/tex/rock_face-diff.webp', true, 1.5), normalMap: rockN, color: 0xb3a691, roughness: .92 });
  M.darkStone = new THREE.MeshStandardMaterial({ map: tex('assets/tex/rock_face-diff.webp', true, 1.2), normalMap: rockN, color: 0x6f665c, roughness: .9 });
  M.bronze = new THREE.MeshStandardMaterial({ color: 0x8a6a3c, metalness: .85, roughness: .48 });
  M.patina = new THREE.MeshStandardMaterial({ color: 0x4f6d5a, metalness: .55, roughness: .7 });
  M.wood = new THREE.MeshStandardMaterial({ color: 0x5a4330, roughness: .85 });
  M.roof = new THREE.MeshStandardMaterial({ color: 0x3b3430, roughness: .8 });
  M.wall = new THREE.MeshStandardMaterial({ color: 0xcfc2a8, roughness: .9 });
  M.pine = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: .95, color: 0x6a7a60 }); M.trunk = new THREE.MeshStandardMaterial({ color: 0x3d2c1f, roughness: 1 });
  M.water = new THREE.MeshPhysicalMaterial({ color: 0x0f2230, roughness: .015, ior: 1.33, metalness: 0, transmission: 0, clearcoat: 1, clearcoatRoughness: .05, envMapIntensity: 2.2 });
  M.cloth = new THREE.MeshStandardMaterial({ color: 0x8b6b2e, roughness: .9, side: THREE.DoubleSide });
}

/* ---------- terrain: plain to the horizon + the overgrown back of the stinger ---------- */
const PADS = [];
function distSeg(px, pz, ax, az, bx, bz) { const vx = bx - ax, vz = bz - az, t = Math.max(0, Math.min(1, ((px - ax) * vx + (pz - az) * vz) / (vx * vx + vz * vz))); return Math.hypot(px - ax - vx * t, pz - az - vz * t); }
// exposed stinger surface height (or -1e9 off the stinger)
export function stingH(x, z) { const u = uOf(x); if (u < 0 || u > 1) return -1e9; const r = sR(u) * .96, dz = z - zA(x); if (Math.abs(dz) >= r) return -1e9; return sC(u) + Math.sqrt(r * r - dz * dz); }
const landMask = (x, z) => sstep(4200, 1800, Math.abs(z - zA(x)) + 300 * fbm(x * .0003, z * .0003, 2)) * sstep(-10500, -8000, x) * sstep(5500, 3500, x);
function rawH(x, z) {
  const u = uOf(x), uc = Math.min(1, Math.max(0, u)), r = sR(uc), dz = z - zA(x), q = Math.abs(dz) / Math.max(1, r);
  // regional plain + distant ranges on the horizon
  let g = 120 * fbm(x * .00012, z * .00012, 6, 1) + 1100 * Math.max(0, ridged(x * .00006, z * .00006, 4) - .5) * sstep(9000, 20000, Math.hypot(x + 3500, z)) + 25 * fbm(x * .0016, z * .0016, 3, 2);
  // crash ejecta: soil heaped against both flanks, rubble mound at the broken base
  if (u > .02 && u < 1.05) g += (60 + .12 * r) * Math.exp(-Math.pow((Math.abs(dz) - r * 1.05) / (r * .45 + 120), 2)) * (.75 + .25 * fbm(x * .001, z * .001, 3, 4)) * sstep(1.05, .9, u);
  g += 140 * Math.exp(-Math.pow(Math.hypot(x - XB - 1100, dz) / 800, 2)) * (.6 + .4 * ridged(x * .002, z * .002, 3));
  if (u < 0 || u > 1) return g;
  // soil cover on the back: a crowned meadow surface slightly above the chitin, ending in ragged edges above the bare flanks
  const n = .07 * fbm(x * .0011, z * .0011, 3, 9);
  const top = sC(uc) + r * Math.sqrt(Math.max(0, 1 - q * q)), cover = sstep(.5, .3, q + n) * sstep(XB + 40, XB - 300, x) * sstep(.95, .85, u);
  let h = top + (14 + .12 * r * q * q) * cover - (.08 * r + 10) * (1 - cover); // soil skin on the back; bare chitin where it thins out
  h += cover * (430 * Math.exp(-(Math.pow(x - SU.x, 2) + dz * dz) / 2.2e5) * (.7 + .3 * ridged(x * .003, z * .003, 3)) + 8 * fbm(x * .004, z * .004, 3, 5)); // summit crag
  h -= 14 * sstep(TARN.r * 1.4, TARN.r * .6, Math.hypot(x - TARN.x, z - TARN.z));
  g = Math.max(g, h);
  g -= 45 * sstep(LAKE.r * 1.3, LAKE.r * .5, Math.hypot(x - LAKE.x, z - LAKE.z));
  return g; }
function groundH(x, z) { let h = rawH(x, z); for (const p of PADS) { const d = Math.hypot(x - p.x, z - p.z); if (d < p.r * 1.6) h = THREE.MathUtils.lerp(h, p.y, sstep(p.r * 1.6, p.r, d)); } return h; }
export function landH(x, z) { return Math.max(groundH(x, z), stingH(x, z)); }
function terrainMesh(cx, cz, size, S, drop, q) {
  const g = new THREE.PlaneGeometry(size, size, S, S); g.rotateX(-Math.PI / 2); g.translate(cx, 0, cz); const p = g.attributes.position, col = new Float32Array(p.count * 3), c = new THREE.Color();
  for (let i = 0; i < p.count; i++) { const x = p.getX(i), z = p.getZ(i); let y = groundH(x, z); const lm = landMask(x, z);
    if (drop && x > -9300 && x < 4300 && Math.abs(z) < 6800) y = Math.min(y, 0) - 300; // hidden under the near mesh
    p.setY(i, y - Math.pow(Math.max(0, Math.hypot(x + 3500, z) - 14000), 2) / 12.7e6); const m = .5 + .5 * fbm(x * .0011, z * .0011, 3, 8);
    c.setRGB(.32 + .06 * m, .33 + .05 * m, .2); // dry grassland (regional)
    c.lerp(new THREE.Color(.2 + .05 * m, .32 + .07 * m, .12), lm * (.8 + .2 * m)); // green land around and on the stinger
    const fd = fbm(x * .0016, z * .0016, 3, 12), fr = fbm(x * .00035, z * .00035, 3, 14); c.lerp(new THREE.Color(.07, .12, .05), Math.min(1, lm * sstep(-.05, .2, fd) * .9 + (1 - lm) * sstep(0, .3, fr) * .7) * sstep(1050, 850, y)); // forests
    col.set([c.r, c.g, c.b], i * 3); }
  g.setAttribute('color', new THREE.BufferAttribute(col, 3)); g.computeVertexNormals(); const m = new THREE.Mesh(g, M.terrain); m.receiveShadow = true; return m; }

/* ---------- the stinger: a tapered, plated lancet lying in the ground ---------- */
function buildSpire(q) { const grp = new THREE.Group(); const L = q === 'high' ? 360 : 200, A = q === 'high' ? 160 : 96, pos = [], col = [], idx = [];
  const chit = new THREE.Color(.16, .12, .085), amber = new THREE.Color(.62, .42, .14), c = new THREE.Color(), SEG = 380;
  for (let i = 0; i <= L; i++) { const u = Math.pow(i / L, 1.1) * .995, x = XB - u * LEN, t = u * LEN, ph = (t / SEG) % 1, seg = Math.floor(t / SEG), r0 = sR(u), cy = sC(u), cz = zA(x);
    const plate = 1 + .05 * Math.pow(1 - ph, 2.2) - .03 - .03 * sstep(.97, 1, 1 - ph); // overlapping ring plates, flaring toward the base
    const band = sstep(.82, .97, 1 - ph) + sstep(0, .05, 1 - ph) * (1 - sstep(.05, .12, 1 - ph));
    const brk = i === 0 ? .82 : 1; // broken, ragged base
    for (let j = 0; j <= A; j++) { const a = j / A * Math.PI * 2; const r = r0 * brk * plate * (1 + .012 * fbm(t * .0015, a * 6, 4, seg % 7) + .006 * nz(t * .01, a * 30)) * (1 - .015 * Math.pow(Math.abs(Math.sin(a * 9)), 8)) * (i === 0 ? 1 + .12 * nz(a * 3, 1) : 1);
      pos.push(x + (i === 0 ? 60 * nz(a * 4, 2) : 0), cy + Math.sin(a) * r, cz + Math.cos(a) * r);
      const gg = .85 + .3 * fbm(t * .003, a * 9, 3, 2); c.copy(chit).multiplyScalar(gg).lerp(amber, Math.min(1, band * (.6 + .5 * fbm(t * .002 + 4, a * 7, 3)))); col.push(c.r, c.g, c.b); } }
  for (let i = 0; i < L; i++) for (let j = 0; j < A; j++) { const a = i * (A + 1) + j, b = a + A + 1; idx.push(a, a + 1, b, b, a + 1, b + 1); }
  // broken base face (fossil cross-section)
  const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3)); g.setAttribute('color', new THREE.Float32BufferAttribute(col, 3)); g.setIndex(idx); g.computeVertexNormals();
  const shell = new THREE.Mesh(g, M.spire); shell.castShadow = shell.receiveShadow = true; grp.add(shell);
  // barbs: two rows of backward-pointing teeth along the flanks, like a real stinger lancet
  const nb = q === 'high' ? 44 : 26, cone = new THREE.ConeGeometry(1, 1, 7); cone.translate(0, .5, 0); const bi = new THREE.InstancedMesh(cone, M.barb, nb * 2), o = new THREE.Object3D(); let k = 0;
  for (const s of [-1, 1]) for (let i = 0; i < nb; i++) { const u = .06 + i * (.62 / nb), x = XB - u * LEN, r = sR(u), a = .5, cz = zA(x) + s * Math.cos(a) * r * .97, cy = sC(u) + Math.sin(a) * r * .97, sz = r * .16 + 25;
    o.position.set(x, cy, cz); o.lookAt(x + r * 1.6, cy + r * .4, cz + s * r * 1.2); o.rotateX(Math.PI / 2); o.scale.set(sz * .35, sz, sz * .35); o.updateMatrix(); bi.setMatrixAt(k++, o.matrix); }
  bi.castShadow = true; grp.add(bi); return grp; }
function buildTalus(q) { const n = q === 'high' ? 700 : 300; const geo = new THREE.IcosahedronGeometry(1, 1); const p = geo.attributes.position; for (let i = 0; i < p.count; i++) { const v = new THREE.Vector3().fromBufferAttribute(p, i); v.multiplyScalar(1 + .3 * nz(v.x * 2.1, v.y * 2.1, v.z * 2.1)); v.y *= .7; p.setXYZ(i, v.x, v.y, v.z); } geo.computeVertexNormals();
  const im = new THREE.InstancedMesh(geo, M.stone, n), o = new THREE.Object3D(); for (let i = 0; i < n; i++) { const u = rnd() * .9, x = XB - u * LEN + (i % 5 === 0 ? 900 * rnd() : 0), s1 = rnd() < .5 ? -1 : 1, z = zA(x) + s1 * sR(Math.max(0, u)) * (1 + Math.pow(rnd(), 2) * .5); const s = 8 + Math.pow(rnd(), 3) * 90; o.position.set(x, landH(x, z) - s * .25, z); o.rotation.set(rnd() * 6, rnd() * 6, rnd() * 6); o.scale.set(s, s * (.6 + rnd() * .6), s * (.8 + rnd() * .4)); o.updateMatrix(); im.setMatrixAt(i, o.matrix); }
  im.castShadow = im.receiveShadow = true; return im; }

/* ---------- sky & clouds (real clouds: morning valley fog + high altocumulus) ---------- */
function cloudDeck(y, alpha, scale) { return new THREE.Mesh(new THREE.PlaneGeometry(6e5, 6e5, 1, 1), new THREE.ShaderMaterial({ transparent: true, depthWrite: false, fog: false, side: THREE.DoubleSide, uniforms: { t: { value: 0 }, A: { value: alpha }, S: { value: scale } },
  vertexShader: 'varying vec3 vW; void main(){ vec4 w=modelMatrix*vec4(position,1.0); vW=w.xyz; gl_Position=projectionMatrix*viewMatrix*w; }',
  fragmentShader: `uniform float t; uniform float A; uniform float S; varying vec3 vW;
    float hash(vec2 p){return fract(sin(dot(p,vec2(127.1,311.7)))*43758.5453);} float n2(vec2 p){vec2 i=floor(p),f=fract(p);f=f*f*(3.0-2.0*f);return mix(mix(hash(i),hash(i+vec2(1,0)),f.x),mix(hash(i+vec2(0,1)),hash(i+vec2(1,1)),f.x),f.y);}
    float fb(vec2 p){float a=0.0,w=0.5;for(int i=0;i<6;i++){a+=w*n2(p);p=p*2.02+vec2(1.7,9.2);w*=0.5;}return a;}
    void main(){ vec2 p=vW.xz*S+vec2(t*0.0006,t*0.0002); float d=fb(p+0.5*fb(p*2.1)); float cov=smoothstep(0.5,0.78,d);
      float toward=pow(max(0.0,normalize(vW.xz-cameraPosition.xz).x),2.0); vec3 c=mix(vec3(0.62,0.6,0.64),vec3(1.0,0.78,0.55),0.4+0.6*toward*d);
      float dist=length(vW.xz-cameraPosition.xz); vec3 v=normalize(vW-cameraPosition); gl_FragColor=vec4(c, cov*A*smoothstep(0.02,0.25,abs(v.y))); }` })); }
function buildClouds(q) { const g = new THREE.Group(); const hi = cloudDeck(0, .55, .00006); hi.rotation.x = -Math.PI / 2; hi.position.y = 14500; g.add(hi);
  g.userData = { hi }; return g; }

/* ---------- forest, path, tarn/river/waterfall/lake, village ---------- */
let PATHP = []; const pathD = (x, z) => { let d = 1e9; for (let i = 0; i < PATHP.length - 1; i++) d = Math.min(d, distSeg(x, z, PATHP[i].x, PATHP[i].z, PATHP[i + 1].x, PATHP[i + 1].z)); return d; };
let RIVP = []; const riverD = (x, z) => { let d = 1e9; for (let i = 0; i < RIVP.length - 1; i += 3) d = Math.min(d, distSeg(x, z, RIVP[i].x, RIVP[i].z, RIVP[Math.min(i + 3, RIVP.length - 1)].x, RIVP[Math.min(i + 3, RIVP.length - 1)].z)); return d; };
function buildForest(q) { const n = q === 'high' ? 50000 : 14000; const cone = new THREE.ConeGeometry(1, 1, 7, 3); cone.translate(0, .5, 0);
  const cc = new Float32Array(cone.attributes.position.count * 3); for (let i = 0; i < cc.length / 3; i++) { const y = cone.attributes.position.getY(i); cc.set([.75 + y * .4, .85 + y * .3, .7], i * 3); } cone.setAttribute('color', new THREE.BufferAttribute(cc, 3));
  const im = new THREE.InstancedMesh(cone, M.pine, n), o = new THREE.Object3D(), c = new THREE.Color(); let k = 0;
  for (let i = 0; i < n * 5 && k < n; i++) { const x = -8000 + rnd() * 10500, z = zA(x) + (rnd() * 2 - 1) * 3600; if (landMask(x, z) < .6) continue; const dens = fbm(x * .0016, z * .0016, 3, 12); if (dens < -.02) continue;
    const y = groundH(x, z); if (y < stingH(x, z) + 3 || y > 980 + 120 * dens) continue; if (Math.hypot(x - LAKE.x, z - LAKE.z) < LAKE.r * 1.15 || Math.hypot(x - TARN.x, z - TARN.z) < TARN.r * 1.4 || riverD(x, z) < 20 || pathD(x, z) < 12 || Math.hypot(x - VILLAGE.x, z - VILLAGE.z) < VILLAGE.r) continue;
    const y2 = groundH(x + 4, z), y3 = groundH(x, z + 4); if (Math.hypot(y2 - y, y3 - y) > 3) continue; if (PADS.some(p => Math.hypot(p.x - x, p.z - z) < p.r * 1.25)) continue;
    const h = 20 + rnd() * 24; o.position.set(x, y - .5, z); o.scale.set(h * .22, h, h * .22); o.rotation.y = rnd() * 6; o.updateMatrix(); im.setMatrixAt(k, o.matrix); c.setRGB(.06 + rnd() * .03, .1 + rnd() * .05, .05 + rnd() * .02); im.setColorAt(k++, c); }
  im.count = k; im.castShadow = q === 'high'; im.receiveShadow = true; return im; }
// draped ribbon along a polyline (path, river); keep(i) selects segments
function ribbon(pts, w, lift, mat, keep = () => true) { const pos = [], idx = []; for (let i = 0; i < pts.length; i++) { const a = pts[Math.max(0, i - 1)], b = pts[Math.min(pts.length - 1, i + 1)], dx = b.x - a.x, dz = b.z - a.z, l = Math.hypot(dx, dz) || 1, nx = -dz / l, nzz = dx / l;
    for (const s of [-1, 1]) pos.push(pts[i].x + nx * w * s, pts[i].y + lift, pts[i].z + nzz * w * s); if (i < pts.length - 1 && keep(i)) idx.push(i * 2, i * 2 + 2, i * 2 + 1, i * 2 + 1, i * 2 + 2, i * 2 + 3); }
  const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3)); g.setIndex(idx); g.computeVertexNormals(); const m = new THREE.Mesh(g, mat); m.receiveShadow = true; return m; }
function sampleLine(ctrl, step) { const out = []; for (let i = 0; i < ctrl.length - 1; i++) { const [ax, az] = ctrl[i], [bx, bz] = ctrl[i + 1], n = Math.max(1, Math.ceil(Math.hypot(bx - ax, bz - az) / step)); for (let k = 0; k < n; k++) { const x = ax + (bx - ax) * k / n, z = az + (bz - az) * k / n; out.push({ x, z, y: 0 }); } } const e = ctrl[ctrl.length - 1]; out.push({ x: e[0], z: e[1], y: 0 }); return out; }
function buildWater() { const g = new THREE.Group();
  TARN_Y = Math.min(...[0, 1, 2, 3, 4, 5].map(a => groundH(TARN.x + Math.cos(a) * TARN.r * 1.2, TARN.z + Math.sin(a) * TARN.r * 1.2))) - 2;
  const tarn = new THREE.Mesh(new THREE.CircleGeometry(TARN.r * 1.3, 48), M.water); tarn.rotation.x = -Math.PI / 2; tarn.position.set(TARN.x, TARN_Y, TARN.z); g.add(tarn);
  LAKE_Y = Math.min(...[0, 1, 2, 3, 4, 5].map(a => groundH(LAKE.x + Math.cos(a) * LAKE.r * 1.15, LAKE.z + Math.sin(a) * LAKE.r * 1.15))) - 3;
  const lake = new THREE.Mesh(new THREE.CircleGeometry(LAKE.r * 1.35, 64), M.water); lake.rotation.x = -Math.PI / 2; lake.position.set(LAKE.x, LAKE_Y, LAKE.z); lake.receiveShadow = true; g.add(lake);
  // river: tarn outflow → over the bare south flank (waterfall) → valley lake; height never rises downstream
  let yl = 1e9; RIVP.forEach(p => { p.y = Math.min(yl, landH(p.x, p.z)); yl = p.y; });
  g.add(ribbon(RIVP, 7, 1.2, M.water));
  const steep = (i) => RIVP[i].y - RIVP[i + 1].y > 4; g.add(ribbon(RIVP, 8, 2, M.foam, steep));
  return g; }
function buildVillage(q, VILLAGE) { const n = q === 'high' ? 110 : 60; const box = new THREE.BoxGeometry(1, 1, 1); box.translate(0, .5, 0); const roof = new THREE.CylinderGeometry(.0, .75, .5, 4, 1); roof.rotateY(Math.PI / 4); roof.scale(1, 1, .72); roof.translate(0, 1.25, 0);
  const walls = new THREE.InstancedMesh(box, M.wall, n), roofs = new THREE.InstancedMesh(roof, M.roof, n), o = new THREE.Object3D(); let k = 0;
  for (let i = 0; i < n * 3 && k < n; i++) { const a = rnd() * 6.28, r = Math.sqrt(rnd()) * VILLAGE.r, x = VILLAGE.x + Math.cos(a) * r, z = VILLAGE.z + Math.sin(a) * r * .7; const y = landH(x, z);
    const w = 7 + rnd() * 6, d = 9 + rnd() * 8, h = 5 + rnd() * 4; o.position.set(x, y - .4, z); o.rotation.y = Math.round(a * 2) / 2 + (rnd() - .5) * .2; o.scale.set(w, h, d); o.updateMatrix(); walls.setMatrixAt(k, o.matrix); o.scale.set(w * 1.45, h * 1.4, d * 1.45); o.position.y = y - .4 - h * .4; o.updateMatrix(); roofs.setMatrixAt(k++, o.matrix); }
  walls.count = roofs.count = k; [walls, roofs].forEach(m => { m.castShadow = true; m.receiveShadow = true; }); const g = new THREE.Group(); g.add(walls, roofs);
  const tower = new THREE.Mesh(new THREE.CylinderGeometry(4, 5, 26, 8), M.stone); tower.position.set(VILLAGE.x, landH(VILLAGE.x, VILLAGE.z) + 12, VILLAGE.z); tower.castShadow = true; g.add(tower); return g; }

/* ---------- bee (player) — realistic honey bee ---------- */
const stripe = canvasTex(512, 128, (x, w, h) => { x.fillStyle = '#1a140e'; x.fillRect(0, 0, w, h); for (let i = 0; i < 4; i++) { const g = x.createLinearGradient(30 + i * 118, 0, 90 + i * 118, 0); g.addColorStop(0, '#9a6a1c'); g.addColorStop(.5, '#d6a443'); g.addColorStop(1, '#9a6a1c'); x.fillStyle = g; x.fillRect(30 + i * 118, 0, 62, h); }
  for (let i = 0; i < 4000; i++) { x.fillStyle = `rgba(${200 + Math.random() * 55},${170 + Math.random() * 50},${110 + Math.random() * 40},${Math.random() * .25})`; x.fillRect(Math.random() * w, Math.random() * h, 1, 3); } });
export function makeBee() { const g = new THREE.Group();
  const body = new THREE.MeshPhysicalMaterial({ map: stripe, roughness: .62, sheen: 1, sheenColor: new THREE.Color(0xe8c27a), sheenRoughness: .5 });
  const fur = new THREE.MeshPhysicalMaterial({ color: 0x8c6a3a, roughness: .8, sheen: 1, sheenColor: new THREE.Color(0xf0d49a), sheenRoughness: .4 });
  const dark = new THREE.MeshStandardMaterial({ color: 0x1a140f, roughness: .55 }), eye = new THREE.MeshPhysicalMaterial({ color: 0x0c0906, roughness: .15, clearcoat: 1 });
  const abd = new THREE.Mesh(new THREE.SphereGeometry(.5, 40, 24), body); abd.scale.set(1.45, .92, .92); abd.rotation.z = Math.PI / 2; abd.position.set(-.62, -.06, 0);
  const th = new THREE.Mesh(new THREE.SphereGeometry(.37, 32, 20), fur); th.position.set(.1, .02, 0);
  const hd = new THREE.Mesh(new THREE.SphereGeometry(.27, 32, 20), dark); hd.scale.set(.85, 1, 1.05); hd.position.set(.52, 0, 0);
  [-1, 1].forEach(s => { const e = new THREE.Mesh(new THREE.SphereGeometry(.13, 24, 16), eye); e.scale.set(.75, 1.25, .65); e.position.set(.57, .06, .16 * s); g.add(e);
    const an = new THREE.Mesh(new THREE.CylinderGeometry(.011, .011, .42, 6), dark); an.position.set(.74, .27, .08 * s); an.rotation.set(.3 * s, 0, -.75); g.add(an);
    for (let k = 0; k < 3; k++) { const l = new THREE.Mesh(new THREE.CylinderGeometry(.018, .01, .55, 6), dark); l.position.set(.2 - k * .2, -.3, .2 * s); l.rotation.set(.7 * s, 0, .2 - k * .2); g.add(l); } });
  const wmat = new THREE.MeshPhysicalMaterial({ color: 0xf4efe4, transparent: true, opacity: .22, roughness: .1, iridescence: .6, side: THREE.DoubleSide, depthWrite: false });
  const wg = new THREE.CircleGeometry(.5, 24); wg.scale(1.5, .52, 1); wg.translate(-.55, 0, 0); const wings = [];
  [-1, 1].forEach(s => { const piv = new THREE.Group(); piv.position.set(.05, .32, .12 * s); const w = new THREE.Mesh(wg, wmat); w.rotation.x = -Math.PI / 2 * s; w.rotation.y = .15 * s; piv.add(w); g.add(piv); wings.push({ piv, s }); });
  g.add(abd, th, hd); g.traverse(o => { if (o.isMesh && o.material !== wmat) o.castShadow = true; }); g.userData.wings = wings; return g; }

/* ---------- temple on the summit (stone + bronze), core = warm light inside ---------- */
function buildTemple(top) { const g = new THREE.Group(); g.position.copy(top); const add = (geo, mat, x, y, z, ry = 0) => { const m = new THREE.Mesh(geo, mat); m.position.set(x, y, z); m.rotation.y = ry; m.castShadow = m.receiveShadow = true; g.add(m); return m; };
  [[70, 8], [58, 8], [48, 8]].reduce((y, [r, h]) => { add(new THREE.CylinderGeometry(r * .97, r, h, 6), M.stone, 0, y + h / 2, 0, Math.PI / 6); return y + h; }, 0);
  const y0 = 24; add(new THREE.CylinderGeometry(30, 32, 34, 6), M.stone, 0, y0 + 17, 0, Math.PI / 6);
  for (let i = 0; i < 18; i++) { const a = i / 18 * 6.28; add(new THREE.CylinderGeometry(1.6, 1.9, 30, 12), M.stone, Math.cos(a) * 40, y0 + 15, Math.sin(a) * 40); }
  add(new THREE.CylinderGeometry(44, 44, 3, 6), M.darkStone, 0, y0 + 31.5, 0, Math.PI / 6);
  add(new THREE.CylinderGeometry(4, 36, 30, 6), M.patina, 0, y0 + 48, 0, Math.PI / 6); add(new THREE.CylinderGeometry(9, 12, 22, 6), M.stone, 0, y0 + 66, 0, Math.PI / 6); add(new THREE.ConeGeometry(10, 26, 6), M.patina, 0, y0 + 90, 0, Math.PI / 6);
  // east door (dark opening) with warm interior light = the CORE
  add(new THREE.BoxGeometry(2, 22, 16), new THREE.MeshStandardMaterial({ color: 0x120c06, emissive: 0x6a3a10, emissiveIntensity: .35, roughness: 1 }), 31.2, y0 + 11, 0);
  add(new THREE.BoxGeometry(3, 3, 22), M.bronze, 32, y0 + 23.5, 0);
  for (let i = 0; i < 14; i++) add(new THREE.BoxGeometry(5, 1.6, 26), M.stone, 74 + i * 5, 20 - i * 3, 0); // stairs down the east face
  const core = new THREE.Group(); core.position.set(10, y0 + 12, 0); const cl = new THREE.PointLight(0xffa94d, 3000, 120, 2); core.add(cl); g.add(core);
  g.userData = { core, cl }; return g; }

/* ---------- shrines on the terraces (stone + bronze), one per studio ---------- */
const THEME = { radio: 'mast', music: 'bells', master: 'bells', audio: 'bells', stems: 'bells', avatar: 'mirror', shop: 'stall', social: 'banners', community: 'banners', videogen: 'wall', videoedit: 'wall', agent: 'hive', auto: 'hive', image: 'easel', canvas: 'easel', design: 'easel' };
function monument(kind) { const g = new THREE.Group(); const add = (geo, mat, x, y, z, r = [0, 0, 0], s = [1, 1, 1]) => { const m = new THREE.Mesh(geo, mat); m.position.set(x, y, z); m.rotation.set(...r); m.scale.set(...s); m.castShadow = m.receiveShadow = true; g.add(m); return m; };
  if (kind === 'mast') { add(new THREE.CylinderGeometry(.4, .9, 34, 8), M.bronze, 0, 17, 0); for (let i = 0; i < 4; i++) add(new THREE.CylinderGeometry(.08, .08, 36, 4), M.bronze, Math.cos(i * 1.57) * 6, 16, Math.sin(i * 1.57) * 6, [Math.sin(i * 1.57) * .17, 0, -Math.cos(i * 1.57) * .17]); add(new THREE.BoxGeometry(4, 1, 1), M.bronze, 0, 30, 0); }
  else if (kind === 'bells') { add(new THREE.BoxGeometry(14, 1.6, 3), M.wood, 0, 10, 0); [-1, 1].forEach(x => add(new THREE.BoxGeometry(1.4, 11, 1.4), M.stone, x * 6.5, 5, 0)); [-4, 0, 4].forEach((x, i) => add(new THREE.CylinderGeometry(.6, 1.6 + i * .3, 2.4 + i * .4, 16, 1, true), M.bronze, x, 8, 0)); }
  else if (kind === 'mirror') { add(new THREE.CylinderGeometry(4, 4, .5, 48), new THREE.MeshStandardMaterial({ color: 0xb8a88a, metalness: 1, roughness: .12 }), 0, 9, 0, [Math.PI / 2, 0, 0], [1, 1, 1.3]); add(new THREE.TorusGeometry(4.1, .35, 8, 48), M.bronze, 0, 9, 0, [0, 0, 0], [1, 1.3, 1]); add(new THREE.BoxGeometry(2, 4, 2), M.stone, 0, 2, 0); }
  else if (kind === 'stall') { add(new THREE.BoxGeometry(10, 3, 4), M.wood, 0, 1.5, 0); [-4.6, 4.6].forEach(x => add(new THREE.BoxGeometry(.4, 7, .4), M.wood, x, 3.5, 2)); add(new THREE.PlaneGeometry(10.5, 5, 1, 1), M.cloth, 0, 7, 0.8, [-1.1, 0, 0]); for (let i = 0; i < 6; i++) add(new THREE.BoxGeometry(1, .8, 1), i % 2 ? M.cloth : M.wood, -3.5 + i * 1.4, 3.4, 0); }
  else if (kind === 'banners') { [-4, 0, 4].forEach(x => { add(new THREE.CylinderGeometry(.2, .2, 14, 8), M.wood, x, 7, 0); add(new THREE.PlaneGeometry(2.4, 7, 1, 1), M.cloth, x + 1.3, 9.5, 0); }); }
  else if (kind === 'wall') { add(new THREE.BoxGeometry(16, 9, 1.6), M.stone, 0, 4.5, 0); add(new THREE.PlaneGeometry(14, 7), new THREE.MeshStandardMaterial({ color: 0xe6dccb, roughness: .9 }), 0, 4.8, .82); }
  else if (kind === 'hive') { add(new THREE.SphereGeometry(4, 24, 16), new THREE.MeshStandardMaterial({ color: 0x9a7a4a, roughness: .95 }), 0, 5, 0, [0, 0, 0], [1, 1.25, 1]); add(new THREE.CylinderGeometry(2, 3.5, 2, 12), M.stone, 0, 1, 0); }
  else if (kind === 'easel') { add(new THREE.BoxGeometry(6, 8, .5), M.wood, 0, 6, 0, [-.15, 0, 0]); [-2.5, 2.5].forEach(x => add(new THREE.BoxGeometry(.3, 11, .3), M.wood, x, 5, -.6, [-.15, 0, 0])); }
  else { add(new THREE.BoxGeometry(3, 16, 3), M.stone, 0, 8, 0); add(new THREE.ConeGeometry(2.2, 3, 4), M.bronze, 0, 17.5, 0, [0, Math.PI / 4, 0]); }
  return g; }
export function shrineSpots(studios) { // ring of shrines on each category terrace, all facing the terrace centre
  const T = CATN.map((c, k) => ({ cat: c, x: TX[k], z: zA(TX[k]) + (k % 2 ? 90 : -90), list: [] })); studios.forEach((s, i) => (T[CATN.indexOf(s.cat)] || T[0]).list.push(i));
  const out = []; T.forEach(t => { const n = t.list.length, R = Math.max(42, n * 46 / (2 * Math.PI)); t.r = R + 34; t.list.forEach((i, j) => { const a = (j + .5) / Math.max(1, n) * Math.PI * 2 + Math.PI / 2; out[i] = { x: t.x + Math.cos(a) * R, z: t.z + Math.sin(a) * R, t }; }); });
  return { spots: out, T }; }
function buildShrine(s, spot) { const g = new THREE.Group(); g.position.set(spot.x, spot.y, spot.z); g.rotation.y = Math.atan2(spot.t.x - spot.x, spot.t.z - spot.z);
  const add = (geo, mat, x, y, z) => { const m = new THREE.Mesh(geo, mat); m.position.set(x, y, z); m.castShadow = m.receiveShadow = true; g.add(m); return m; };
  add(new THREE.CylinderGeometry(15, 16.5, 2.4, 6), M.stone, 0, .4, 0); add(new THREE.BoxGeometry(7, .8, 6), M.stone, 0, -.2, 17);
  // gate (fly through it)
  [-1, 1].forEach(x => add(new THREE.BoxGeometry(1.6, 13, 1.6), M.stone, x * 6, 7.4, 9)); add(new THREE.BoxGeometry(16, 1.6, 2.2), M.stone, 0, 14.4, 9); add(new THREE.BoxGeometry(13, .8, 1.6), M.bronze, 0, 12.6, 9);
  const portal = new THREE.Mesh(new THREE.PlaneGeometry(10.4, 11.5), new THREE.MeshBasicMaterial({ transparent: true, opacity: 0, depthWrite: false })); portal.position.set(0, 7.2, 9); g.add(portal);
  const mon = monument(THEME[s.id] || 'obelisk'); mon.position.z = -4; g.add(mon);
  // two small bronze braziers (real fire light, subtle)
  const fl = new THREE.PointLight(0xff9a4a, 0, 28, 2); fl.position.set(0, 3, 12); g.add(fl);
  g.userData = { portal, fl }; return { group: g, portal }; }

/* ---------- build ---------- */
export function buildWorld(scene, renderer, studios, colOf, q) {
  makeMaterials(q); M.foam = new THREE.MeshStandardMaterial({ color: 0xdfe5e8, roughness: .55, transparent: true, opacity: .85 }); M.path = new THREE.MeshStandardMaterial({ color: 0x8c7b62, roughness: 1, polygonOffset: true, polygonOffsetFactor: -2 });
  const sky = new Sky(); sky.scale.setScalar(4e5); const u = sky.material.uniforms; u.turbidity.value = 5; u.rayleigh.value = 2; u.mieCoefficient.value = .005; u.mieDirectionalG.value = .86; u.sunPosition.value.copy(SUN_DIR).multiplyScalar(1e5); scene.add(sky);
  if (renderer) { const pm = new THREE.PMREMGenerator(renderer); const es = new THREE.Scene(); const s2 = new Sky(); s2.scale.setScalar(1000); s2.material.uniforms.sunPosition.value.copy(SUN_DIR).multiplyScalar(500); ['turbidity', 'rayleigh', 'mieCoefficient', 'mieDirectionalG'].forEach(k => s2.material.uniforms[k].value = u[k].value); es.add(s2);
    scene.environment = pm.fromScene(es, 0, .1, 2000).texture; scene.environmentIntensity = .55; }
  scene.fog = new THREE.FogExp2(0xa9b4c0, .00004);
  const sun = new THREE.DirectionalLight(0xffd2a0, 4.2); sun.castShadow = true; sun.shadow.mapSize.setScalar(q === 'high' ? 4096 : 2048); sun.shadow.bias = -.0004; sun.shadow.normalBias = 2; scene.add(sun, sun.target);
  scene.add(new THREE.HemisphereLight(0xa8bcd8, 0x54432e, .9));
  // summit (temple) + terraces
  const top = new THREE.Vector3(SU.x, -1e9, MC.y); for (let i = 0; i < 400; i++) { const x = SU.x + (rnd() - .5) * 500, z = MC.y + (rnd() - .5) * 500, h = rawH(x, z); if (h > top.y) top.set(x, h, z); }
  MC.set(top.x, top.z); PADS.push({ x: top.x, z: top.z, r: 80, y: top.y - 30 }); SNOW.value = top.y - 260; TOPU.value.copy(top);
  const { spots, T } = shrineSpots(studios); T.forEach(t => { t.y = rawH(t.x, t.z) + 1; PADS.push({ x: t.x, z: t.z, r: t.r, y: t.y }); }); spots.forEach(s => s.y = s.t.y);
  // path: temple stairs → terraces, river: tarn → south flank → lake
  PATHP = sampleLine([[top.x + 40, top.z], ...T.map(t => [t.x, t.z])], 12); PATHP.forEach(p => p.y = groundH(p.x, p.z));
  RIVP = sampleLine([[TARN.x, TARN.z], [TARN.x - 120, TARN.z + 500], [LAKE.x + 150, LAKE.z - LAKE.r * .9], [LAKE.x, LAKE.z]], 10);
  scene.add(terrainMesh(-2500, 0, 14000, q === 'high' ? 520 : 300, false, q));
  const mid = terrainMesh(-2500, 0, 60000, q === 'high' ? 300 : 180, true, q); mid.position.y = -2; scene.add(mid);
  const far = terrainMesh(-2500, 0, 320000, 160, true, q); far.position.y = -40; far.receiveShadow = false; scene.add(far);
  const spire = buildSpire(q); scene.add(spire); scene.add(buildTalus(q));
  const clouds = buildClouds(q); scene.add(clouds); scene.add(buildWater()); scene.add(buildForest(q)); scene.add(buildVillage(q, VILLAGE));
  scene.add(ribbon(PATHP, 3, .6, M.path));
  // terrace rims: low dry-stone walls make each category plateau readable
  T.forEach(t => { const w = new THREE.Mesh(new THREE.CylinderGeometry(t.r + 8, t.r + 10, 4, 48, 1, true), M.stone); w.position.set(t.x, t.y - 1, t.z); w.receiveShadow = true; scene.add(w); });
  const temple = buildTemple(new THREE.Vector3(top.x, top.y - 30, top.z)); scene.add(temple);
  const shrines = studios.map((s, i) => { const r = buildShrine(s, spots[i]); scene.add(r.group); r.portal.userData.i = i; return r; });
  const terraces = T.map(t => ({ cat: t.cat, pos: new THREE.Vector3(t.x, t.y + 40, t.z), list: t.list }));
  const W = { sky, sun, spire, clouds, temple, shrines, spots, terraces, peak: top.y - 30, core: temple.userData.core, top,
    outline: [...Array(21)].map((_, i) => { const uu = i / 20, x = XB - uu * LEN; return [x, zA(x), sR(uu) * .8]; }),
    update(t, dt, camera, focus) { clouds.userData.hi.material.uniforms.t.value = t;
      const alt = Math.max(0, camera.position.y - 500); scene.fog.density = .00004 * Math.exp(-alt / 4000) + .000005; const nr = alt > 2500 ? Math.min(30, alt / 300) : 1; if (Math.abs(camera.near - nr) > .4) { camera.near = nr; camera.updateProjectionMatrix(); }
      temple.userData.cl.intensity = 2600 + Math.sin(t * 1.3) * 250;
      const wide = alt > 1000, ext = wide ? 9000 : 450, sc = sun.shadow.camera; if (sc.right !== ext) { sc.left = sc.bottom = -ext; sc.right = sc.top = ext; sc.far = wide ? 50000 : 8000; sc.updateProjectionMatrix(); }
      const f = wide ? new THREE.Vector3(-2500, 300, 0) : focus; if (f) { sun.position.copy(f).addScaledVector(SUN_DIR, wide ? 20000 : 3000); sun.target.position.copy(f); } } };
  return W;
}