File size: 21,139 Bytes
dd53693
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Places, time of day and weather. Everything is plain three.js objects (no custom shaders).
import * as THREE from 'three';
import { at, ball, box, cone, cyl, group, mat, orb, rng, rockGeo, tint, torus } from './prims.js';
import { THINGS } from './things.js';

const { sin, cos, PI } = Math;
const HAS_DOM = typeof document !== 'undefined';

const SKY = {
  day: { top: 0x4a90e2, hor: 0xcfe8ff, sun: 0xfff4e0, si: 2.6, hemi: 1.0, dir: [8, 14, 7] },
  sunset: { top: 0x2b3a67, hor: 0xff9a56, sun: 0xffb27a, si: 1.7, hemi: 0.95, dir: [-15, 4.5, 3] },
  dawn: { top: 0x6f8fd6, hor: 0xffd3b0, sun: 0xffd0b0, si: 2.0, hemi: 0.8, dir: [15, 5, 4] },
  night: { top: 0x05070f, hor: 0x1a2444, sun: 0xa9bcff, si: 1.25, hemi: 0.75, dir: [-7, 13, 5] },
};
const WEATHER_TINT = { rain: [0x6f7c8a, 0.65], storm: [0x2e3540, 0.82], fog: [0xc9d1d6, 0.8], snow: [0xc3ccd6, 0.55], clear: [0, 0] };
const GROUND = {
  field: 0x7fb650, desert: 0xe2c28a, snow: 0xeef3f7, beach: 0xead7a4, forest: 0x47743e, city: 0x53565e, moon: 0x8e9096,
  ocean: 0x2c6fa8, room: 0xb98a5a, mountain: 0x78985e, farm: 0x86b857, road: 0x74ad4c, park: 0x76b455, studio: 0xdfe3ea,
};

const mix = (a, b, k) => new THREE.Color(a).lerp(new THREE.Color(b), k);

function gradient(top, hor) {
  if (!HAS_DOM) return hor.clone();
  const c = document.createElement('canvas');
  c.width = 4; c.height = 256;
  const x = c.getContext('2d'), g = x.createLinearGradient(0, 0, 0, 256);
  g.addColorStop(0, '#' + top.getHexString());
  g.addColorStop(0.62, '#' + hor.getHexString());
  g.addColorStop(1, '#' + hor.getHexString());
  x.fillStyle = g;
  x.fillRect(0, 0, 4, 256);
  const t = new THREE.CanvasTexture(c);
  t.colorSpace = THREE.SRGBColorSpace;
  return t;
}

function windows(lit) {
  if (!HAS_DOM) return null;
  const c = document.createElement('canvas');
  c.width = 64; c.height = 128;
  const x = c.getContext('2d'), r = rng(lit ? 7 : 3);
  x.fillStyle = lit ? '#000' : '#fff';
  x.fillRect(0, 0, 64, 128);
  for (let j = 0; j < 10; j++) for (let i = 0; i < 4; i++) {
    const on = r() < 0.55;
    x.fillStyle = lit ? (on ? '#ffd98a' : '#000') : (on ? '#b9d4ee' : '#7f93a8');
    x.fillRect(4 + i * 15, 5 + j * 12.4, 10, 8);
  }
  const t = new THREE.CanvasTexture(c);
  t.colorSpace = THREE.SRGBColorSpace;
  return t;
}

// ------------------------------------------------------------------ small scenery pieces
const pine = (snowy) => group(at(cyl(0.14, 0.2, 1.0, 0x6b4a2a, null, 6), 0, 0.5, 0), at(cone(1.1, 1.6, snowy ? 0xdfe9ee : 0x2f6b3f, null, 7), 0, 1.6, 0),
  at(cone(0.85, 1.4, snowy ? 0xeef3f7 : 0x37794a, null, 7), 0, 2.5, 0), at(cone(0.55, 1.2, snowy ? 0xffffff : 0x3f8a55, null, 7), 0, 3.3, 0));
function palm() {
  const g = group();
  let y = 0, x = 0;
  for (let i = 0; i < 6; i++) { g.add(at(cyl(0.13, 0.16, 0.62, 0x9a7650, null, 6), x, y + 0.3, 0, 0, 0, -0.07 * i)); y += 0.58; x += 0.045 * i; }
  for (let i = 0; i < 7; i++) g.add(at(box(1.9, 0.05, 0.4, 0x3f9a4a), x + cos(i * 0.9) * 0.85, y - 0.12, sin(i * 0.9) * 0.85, 0, -i * 0.9, -0.35));
  g.add(at(orb(0.13, 0x6b4a2a), x + 0.1, y - 0.15, 0.1), at(orb(0.13, 0x6b4a2a), x - 0.12, y - 0.18, 0));
  return g;
}
const hill = (r, c) => at(orb(r, c), 0, -r * 0.22, 0, 0, 0, 0, [1, 0.3, 1]);
const peak = (r, h, c, snow) => group(at(cone(r, h, c, null, 7), 0, h / 2, 0), snow ? at(cone(r * 0.32, h * 0.32, 0xf7fafc, null, 7), 0, h * 0.842, 0) : group());
const cloud = () => group(at(ball(2.2, 0xffffff, { noShadow: 1 }), 0, 0, 0), at(ball(1.7, 0xffffff, { noShadow: 1 }), 2.2, -0.2, 0.3), at(ball(1.5, 0xffffff, { noShadow: 1 }), -2.1, -0.3, -0.2), at(ball(1.3, 0xffffff, { noShadow: 1 }), 0.8, 0.9, -0.3));
const bush = (c) => group(at(ball(0.5, c), 0, 0.35, 0), at(ball(0.38, tint(c, 0.05)), 0.4, 0.28, 0.1), at(ball(0.34, tint(c, -0.05)), -0.38, 0.25, -0.1));
const grass = (c) => group(at(cone(0.06, 0.4, c, { noShadow: 1 }, 4), 0, 0.2, 0), at(cone(0.05, 0.3, c, { noShadow: 1 }, 4), 0.1, 0.15, 0.05, 0, 0, -0.3), at(cone(0.05, 0.3, c, { noShadow: 1 }, 4), -0.1, 0.15, -0.03, 0, 0, 0.3));
const fence = () => group(at(box(0.1, 1.0, 0.1, 0x9a7650), -1, 0.5, 0), at(box(0.1, 1.0, 0.1, 0x9a7650), 1, 0.5, 0), at(box(2.1, 0.1, 0.06, 0xb08a60), 0, 0.75, 0), at(box(2.1, 0.1, 0.06, 0xb08a60), 0, 0.4, 0));
const hay = () => at(cyl(0.6, 0.6, 1.0, 0xe2c15a, null, 12), 0, 0.6, 0, 0, 0, PI / 2);
const barn = () => group(at(box(5, 3.2, 6, 0xb5372e), 0, 1.6, 0), at(cone(4.1, 2.0, 0x6e2a25, null, 4), 0, 4.2, 0, 0, PI / 4, 0, [1, 1, 1.2]),
  at(box(1.8, 2.2, 0.1, 0xf4f4f0), 0, 1.1, 3.01), at(box(1.6, 2.0, 0.12, 0x8f2a23), 0, 1.05, 3.02), at(box(0.9, 0.7, 0.1, 0xf4f4f0), 0, 3.2, 3.01));

/** Drop `n` copies around the action area. `back` keeps them out of the camera's front wedge. */
function scatter(g, rand, n, r0, r1, make, back = true, s0 = 0.8, s1 = 1.4) {
  for (let i = 0; i < n; i++) {
    const a = back ? PI + (rand() * 2 - 1) * 1.95 : rand() * PI * 2, r = r0 + rand() * (r1 - r0);
    const o = make(rand);
    o.position.x += sin(a) * r;
    o.position.z += cos(a) * r;
    o.rotation.y = rand() * 6.28;
    o.scale.multiplyScalar(s0 + rand() * (s1 - s0));
    g.add(o);
  }
}

// ------------------------------------------------------------------ main builder
export function buildEnvironment(sc, { R = 6, island = 0 } = {}) {
  const loc = GROUND[sc.loc] || sc.loc === 'space' ? sc.loc : 'studio', time = SKY[sc.time] ? sc.time : 'day';
  const weather = WEATHER_TINT[sc.weather] ? sc.weather : 'clear';
  const g = new THREE.Group(), rand = rng(1234 + loc.length * 77), far = R + 3;
  const outer = loc === 'space' || loc === 'moon', indoor = loc === 'room';
  const sky = SKY[time], [wc, wk] = WEATHER_TINT[weather];
  let top = new THREE.Color(sky.top), hor = new THREE.Color(sky.hor);
  if (outer) { top = new THREE.Color(0x000004); hor = new THREE.Color(0x070a18); }
  else if (loc === 'studio') { top = mix(0xaebbd0, sky.top, time === 'day' ? 0 : 0.75); hor = mix(0xeef1f5, sky.hor, time === 'day' ? 0 : 0.7); }
  else if (indoor) { top = mix(0x3b2f2a, 0x0c0a10, time === 'night' ? 0.6 : 0); hor = top.clone(); }
  if (wk && !outer && !indoor) {
    const dark = time === 'night' ? 0.25 : 1;
    top.lerp(new THREE.Color(wc).multiplyScalar(dark * 0.8), wk);
    hor.lerp(new THREE.Color(wc).multiplyScalar(dark), wk);
  }
  const env = { group: g, background: gradient(top, hor), fog: null, waterY: null, zeroG: loc === 'space', tick: [], dispose: [] };
  if (env.background.isTexture) env.dispose.push(env.background);

  // lights
  const dim = !outer && !indoor ? { rain: 0.6, storm: 0.4, fog: 0.7, snow: 0.8, clear: 1 }[weather] : 1;
  const hemi = new THREE.HemisphereLight(mix(time === 'night' ? 0x6f86c9 : sky.hor, 0xffffff, time === 'night' ? 0.2 : 0.65), mix(GROUND[loc] ?? 0x888888, 0x222222, 0.4), (outer ? 0.35 : sky.hemi) * (0.6 + 0.4 * dim));
  const sun = new THREE.DirectionalLight(outer ? 0xffffff : sky.sun, (outer ? 2.6 : sky.si) * dim * (indoor ? 0.45 : 1));
  const sd = new THREE.Vector3(...(outer ? [9, 12, 6] : sky.dir)).normalize();
  sun.position.copy(sd).multiplyScalar(60);
  sun.castShadow = true;
  const S = Math.max(14, R * 1.6 + 8);
  Object.assign(sun.shadow.camera, { left: -S, right: S, top: S, bottom: -S, near: 1, far: 160 });
  sun.shadow.mapSize.set(2048, 2048);
  sun.shadow.bias = -0.0004;
  sun.shadow.normalBias = 0.03;
  g.add(hemi, sun, sun.target);
  env.sun = sun; env.hemi = hemi;
  if (indoor) {
    const lampL = new THREE.PointLight(0xffd9a0, time === 'night' ? 60 : 35, 40, 1.5);
    lampL.position.set(2, 6, 3);
    g.add(lampL);
  }

  // fog blends the far ground into the horizon
  if (!outer && !indoor) {
    env.fog = weather === 'fog' ? new THREE.Fog(hor.getHex(), 3, Math.max(34, R * 5))
      : new THREE.Fog(hor.getHex(), weather === 'clear' ? 70 : 35, weather === 'clear' ? 230 : 150);
  }

  // sun / moon disc and stars
  if (!indoor) {
    const disc = new THREE.Mesh(new THREE.SphereGeometry(time === 'night' ? 5 : 7, 16, 12),
      new THREE.MeshBasicMaterial({ color: time === 'night' ? 0xf2f4ff : sky.sun, fog: false }));
    disc.position.copy(sd).multiplyScalar(190);
    if (outer) disc.scale.setScalar(0.5);
    disc.visible = weather === 'clear' || weather === 'snow' || outer;
    g.add(disc);
    env.dispose.push(disc.geometry, disc.material);
  }
  if ((time === 'night' && !indoor && weather !== 'fog' && weather !== 'storm') || outer) {
    const n = outer ? 1800 : 700, p = new Float32Array(n * 3);
    for (let i = 0; i < n; i++) {
      const a = rand() * 6.283, e = outer ? Math.acos(rand() * 2 - 1) : Math.acos(rand() * 0.95);
      p.set([sin(e) * cos(a) * 200, cos(e) * 200, sin(e) * sin(a) * 200], i * 3);
    }
    const geo = new THREE.BufferGeometry();
    geo.setAttribute('position', new THREE.BufferAttribute(p, 3));
    const m = new THREE.PointsMaterial({ color: 0xffffff, size: 1.3, sizeAttenuation: true, fog: false });
    g.add(new THREE.Points(geo, m));
    env.dispose.push(geo, m);
  }
  if (!outer && !indoor && loc !== 'studio' && weather !== 'fog') {
    const cl = group();
    for (let i = 0; i < 7; i++) cl.add(at(cloud(), (rand() - 0.5) * 220, 32 + rand() * 14, -30 - rand() * 110, 0, 0, 0, 1.6 + rand() * 1.6));
    if (weather !== 'clear' || time !== 'day') cl.traverse((o) => { if (o.isMesh) o.material = mat(time === 'night' ? 0x2a3350 : weather === 'clear' ? 0xffd9c0 : 0x8a939d, { basic: 1 }); });
    g.add(cl);
    env.tick.push((t) => { cl.position.x = ((t * 0.6) % 60) - 30; });
  }

  // ground
  const gc = GROUND[loc];
  if (loc === 'room') {
    g.add(at(box(40, 0.2, 40, gc), 0, -0.1, 0));
    const bz = -(R + 4), lx = -(R + 6), wall = 0xe9dcc6;
    g.add(at(box(60, 14, 0.3, wall), 0, 7, bz), at(box(0.3, 14, 60, tint(wall, -0.05)), lx, 7, 0), at(box(60, 0.4, 0.4, 0xf4f4f0), 0, 0.2, bz + 0.2),
      at(box(5, 3.6, 0.2, 0xf4f4f0), 3, 5.4, bz + 0.1), at(new THREE.Mesh(new THREE.BoxGeometry(4.6, 3.2, 0.22), new THREE.MeshBasicMaterial({ color: mix(sky.hor, sky.top, 0.4) })), 3, 5.4, bz + 0.12),
      at(box(0.12, 3.2, 0.26, 0xf4f4f0), 3, 5.4, bz + 0.12), at(box(4.6, 0.12, 0.26, 0xf4f4f0), 3, 5.4, bz + 0.12),
      at(box(3, 2.2, 0.15, 0x6b4a2a), -6, 5.2, bz + 0.1), at(box(2.6, 1.8, 0.17, 0x7fb6c9), -6, 5.2, bz + 0.12),
      at(cyl(R + 1.5, R + 1.5, 0.04, 0xb5483a, null, 36), 0, 0.02, 0), at(cyl(R + 0.9, R + 0.9, 0.05, 0xe7d3a8, null, 36), 0, 0.025, 0));
  } else if (loc !== 'space') {
    const geo = new THREE.CircleGeometry(loc === 'studio' ? 120 : 260, 64);
    const ground = new THREE.Mesh(geo, mat(loc === 'ocean' ? 0xd9c58f : gc));
    ground.rotation.x = -PI / 2;
    ground.position.y = loc === 'ocean' ? -1.2 : 0;
    ground.receiveShadow = true;
    g.add(ground);
    env.dispose.push(geo);
  }

  const things = (name) => () => THINGS[name]();
  const water = (z, depth, y) => {
    const geo = new THREE.PlaneGeometry(520, depth, 64, Math.max(8, Math.round(depth / 8)));
    geo.rotateX(-PI / 2);
    const m = new THREE.MeshStandardMaterial({ color: 0x2f86c9, flatShading: true, roughness: 0.35, metalness: 0.1, transparent: true, opacity: 0.9 });
    const mesh = new THREE.Mesh(geo, m);
    mesh.position.set(0, y, z);
    mesh.receiveShadow = true;
    g.add(mesh);
    env.dispose.push(geo, m);
    const pos = geo.attributes.position, base = pos.array.slice();
    env.tick.push((t) => {
      for (let i = 0; i < pos.count; i++) {
        const x = base[i * 3], zz = base[i * 3 + 2];
        pos.array[i * 3 + 1] = sin(x * 0.35 + t * 1.3) * 0.09 + sin(zz * 0.5 + t * 0.9) * 0.07 + sin((x + zz) * 0.12 + t * 0.6) * 0.1;
      }
      pos.needsUpdate = true;
    });
  };

  if (loc === 'field') {
    scatter(g, rand, 9, far + 4, far + 45, things('tree'), true, 1, 1.7);
    scatter(g, rand, 14, far, far + 30, () => bush(0x4f9a52));
    scatter(g, rand, 40, 2, far + 14, () => THINGS.flower([0xf284b6, 0xf2c230, 0xf4f4f0, 0xd63c33, 0x8a4fd3][(rand() * 5) | 0]), false, 0.5, 0.9);
    scatter(g, rand, 60, 1, far + 18, () => grass(0x5f9a3c), false);
    scatter(g, rand, 6, 90, 150, () => hill(30 + rand() * 25, 0x6fa646), true, 1, 1);
  } else if (loc === 'desert') {
    scatter(g, rand, 10, far + 1, far + 40, things('cactus'), true, 1, 2.2);
    scatter(g, rand, 12, far, far + 40, things('rock'), true, 0.8, 2.5);
    scatter(g, rand, 9, 50, 150, () => hill(22 + rand() * 30, 0xd9b67a), false, 1, 1);
    scatter(g, rand, 8, 4, far + 10, things('rock'), false, 0.2, 0.5);
  } else if (loc === 'snow') {
    scatter(g, rand, 16, far + 2, far + 50, () => pine(true), true, 1, 1.9);
    scatter(g, rand, 8, 60, 150, () => hill(25 + rand() * 30, 0xe3ebf1), false, 1, 1);
    scatter(g, rand, 6, far, far + 25, () => at(rockGeo(0.7, 0xf7fafc), 0, 0.2, 0, 0, 0, 0, [1.4, 0.6, 1.2]));
  } else if (loc === 'beach') {
    water(-(R + 9) - 150, 300, 0.3);
    g.add(at(box(520, 0.06, 300, 0x2a78b8, { noShadow: 1 }), 0, 0.03, -(R + 9) - 150));
    g.add(at(box(520, 0.1, 1.2, 0xffffff, { noShadow: 1 }), 0, 0.09, -(R + 8.8)));
    for (let i = 0; i < 12; i++) {
      const side = i % 2 ? 1 : -1;
      g.add(at(palm(), side * (R + 3 + rand() * 34), 0, -(R + 5) + rand() * (R + 4), 0, rand() * 6.28, 0, 1.3 + rand() * 0.6));
    }
    scatter(g, rand, 6, far, far + 6, things('rock'), true, 0.6, 1.3);
    const um = group(at(cyl(0.04, 0.04, 2.4, 0xf4f4f0, null, 6), 0, 1.2, 0), at(cone(1.5, 0.6, 0xd63c33, null, 8), 0, 2.5, 0), at(box(1.0, 0.04, 2.0, 0x2ec4d6), 1.2, 0.03, 0.2));
    g.add(at(um, -(R + 4), 0, -1, 0, 0.4, 0.08));
  } else if (loc === 'forest') {
    scatter(g, rand, 34, far + 1.5, far + 42, (r) => (r() < 0.5 ? THINGS.tree() : pine(false)), true, 1.1, 2.0);
    scatter(g, rand, 10, far + 20, far + 50, () => pine(false), false, 1.2, 2.0);
    scatter(g, rand, 14, 2.5, far + 8, things('mushroom'), false, 0.5, 1.0);
    scatter(g, rand, 16, far, far + 20, () => bush(0x356b3a));
    scatter(g, rand, 50, 1, far + 10, () => grass(0x3a6b34), false);
  } else if (loc === 'city') {
    const day = windows(false), lit = windows(true), night = time === 'night' || time === 'sunset';
    scatter(g, rand, 26, far + 9, far + 70, (r) => {
      const w = 5 + r() * 5, h = 8 + r() * 26, c = [0x8d99a6, 0xa9957e, 0x7a8794, 0xb3a89a, 0x6f7c8f][(r() * 5) | 0];
      const m = new THREE.MeshStandardMaterial({ color: c, map: day, flatShading: true, roughness: 0.8, emissive: night ? 0xffffff : 0, emissiveMap: night ? lit : null, emissiveIntensity: 0.9 });
      const b = new THREE.Mesh(new THREE.BoxGeometry(w, h, w), m);
      b.position.y = h / 2;
      b.castShadow = b.receiveShadow = true;
      env.dispose.push(b.geometry, m);
      const o = group(b);
      o.userData.keepRot = true;
      return o;
    }, true, 1, 1);
    g.children.forEach((o) => {
      if (!o.userData.keepRot) return;
      o.rotation.y = Math.round(o.rotation.y / (PI / 2)) * (PI / 2);
      if (Math.abs(o.position.z) < 14) o.position.z = -(16 + Math.abs(o.position.z));
    });
    if (day) env.dispose.push(day, lit);
    g.add(at(box(400, 0.04, 9, 0x35373d, { noShadow: 1 }), 0, 0.02, 0), at(box(400, 0.2, 3, 0x9a9da3), 0, 0.1, -6), at(box(400, 0.2, 3, 0x9a9da3), 0, 0.1, 6));
    for (let x = -120; x <= 120; x += 4) g.add(at(box(1.8, 0.05, 0.16, 0xf4f4f0, { noShadow: 1 }), x, 0.03, 0));
    for (let x = -36; x <= 36; x += 12) g.add(at(THINGS.lamp(), x, 0.2, -5.4), at(THINGS.tree(), x + 6, 0.2, -6.4, 0, 0, 0, 0.8));
  } else if (loc === 'space') {
    const p1 = group(at(orb(14, 0xd98a4a), 0, 0, 0), at(torus(22, 1.2, 0xe8d0a0, { noShadow: 1 }), 0, 0, 0, PI / 2 - 0.3, 0, 0, [1, 1, 0.08]));
    g.add(at(p1, -70, 30, -120, 0, 0, 0.3), at(orb(6, 0x6fb0e8), 60, -12, -90), at(orb(2.5, 0xc9ccd6), 30, 26, -60));
    scatter(g, rand, 16, far + 4, far + 50, () => at(rockGeo(0.5 + rand() * 1.2, 0x7d7f88), 0, (rand() - 0.5) * 24, 0), false, 0.6, 1.6);
    env.tick.push((t) => { p1.rotation.y = t * 0.02; });
  } else if (loc === 'moon') {
    scatter(g, rand, 30, 3, far + 60, () => group(at(torus(1.4, 0.22, 0x9ea0a6, { noShadow: 1 }), 0, 0.02, 0, PI / 2), at(cyl(1.3, 1.3, 0.03, 0x6f7177, { noShadow: 1 }, 14), 0, 0.015, 0)), true, 0.5, 2.6);
    scatter(g, rand, 20, far, far + 50, () => THINGS.rock(0x8a8c92), false, 0.5, 2.5);
    scatter(g, rand, 5, 70, 150, () => hill(20 + rand() * 20, 0x7f8187), true, 1, 1);
    g.add(at(group(orb(11, 0x2f6fde), at(orb(11.05, 0x3fa34d), 0.6, 0.4, 0.3, 0, 0, 0, [0.9, 0.7, 0.95]), at(orb(11.3, 0xffffff, { opacity: 0.35 }), 0, 0, 0)), -50, 55, -150));
  } else if (loc === 'ocean') {
    water(0, 520, 0);
    env.waterY = 0;
    if (island > 0) {
      g.add(at(cyl(island, island + 2.5, 1.4, 0xead7a4, null, 28), 0, -0.6, 0), at(palm(), -(island - 1.2), 0.08, -(island - 1.5), 0, 0, 0, 1.4));
    }
    scatter(g, rand, 5, far + 16 + island, far + 80, () => group(at(rockGeo(2.4, 0x6f7177), 0, 0.5, 0, 0.3, 0, 0, [1.3, 1, 1]), at(rockGeo(1.2, 0x7d7f88), 2.2, 0.2, 0.6)), true, 0.8, 2);
    const buoy = at(group(at(cone(0.5, 1.2, 0xd63c33, null, 8), 0, 0.5, 0), at(orb(0.12, 0xffe24a, { emissive: 0xffe24a, glow: 2 }), 0, 1.2, 0)), R + 5, 0, -(R + 3));
    g.add(buoy);
    env.tick.push((t) => { buoy.position.y = sin(t * 1.4) * 0.1; buoy.rotation.z = sin(t * 1.1) * 0.12; });
  } else if (loc === 'mountain') {
    scatter(g, rand, 9, 110, 190, () => peak(40 + rand() * 30, 50 + rand() * 45, 0x6b7a86, true), true, 1, 1);
    scatter(g, rand, 6, 70, 105, () => peak(26 + rand() * 14, 22 + rand() * 16, 0x5f7f58, false), true, 1, 1);
    scatter(g, rand, 22, far + 2, far + 55, () => pine(false), true, 1.1, 2.1);
    scatter(g, rand, 12, far, far + 30, things('rock'), true, 0.8, 3);
    scatter(g, rand, 40, 1, far + 12, () => grass(0x5a8a48), false);
  } else if (loc === 'farm') {
    g.add(at(barn(), -(R + 9), 0, -(R + 10), 0, 0.5, 0));
    for (let i = -7; i <= 7; i++) g.add(at(fence(), i * 2.1, 0, -(R + 5)));
    scatter(g, rand, 6, far + 1, far + 9, () => hay(), true, 0.9, 1.2);
    scatter(g, rand, 6, far + 12, far + 50, things('tree'), true, 1, 1.6);
    scatter(g, rand, 50, 1, far + 15, () => grass(0x67a23f), false);
    scatter(g, rand, 5, 90, 150, () => hill(28 + rand() * 22, 0x74ab49), true, 1, 1);
  } else if (loc === 'road') {
    g.add(at(box(520, 0.04, 9, 0x3c3e44, { noShadow: 1 }), 0, 0.02, 0), at(box(520, 0.05, 0.18, 0xf4f4f0, { noShadow: 1 }), 0, 0.03, 4.1), at(box(520, 0.05, 0.18, 0xf4f4f0, { noShadow: 1 }), 0, 0.03, -4.1));
    for (let x = -150; x <= 150; x += 5) g.add(at(box(2.2, 0.05, 0.18, 0xf2c230, { noShadow: 1 }), x, 0.03, 0));
    for (let x = -80; x <= 80; x += 10) g.add(at(THINGS.tree(), x + rand() * 3, 0, -(7.5 + rand() * 4), 0, rand() * 6, 0, 1 + rand() * 0.6));
    scatter(g, rand, 10, far + 14, far + 60, things('tree'), true, 1, 1.7);
    scatter(g, rand, 5, 90, 150, () => hill(28 + rand() * 22, 0x69a244), true, 1, 1);
  } else if (loc === 'park') {
    g.add(at(box(520, 0.04, 3.2, 0xd8c39a, { noShadow: 1 }), 0, 0.02, -(R + 2.5)), at(cyl(7, 7, 0.06, 0x3f93cf, { noShadow: 1 }, 24), R + 11, 0.03, -(R + 14)));
    for (let x = -18; x <= 18; x += 12) g.add(at(THINGS.bench(), x, 0, -(R + 4.6)), at(THINGS.lamp(), x + 6, 0, -(R + 4.8)));
    scatter(g, rand, 14, far + 5, far + 45, things('tree'), true, 1, 1.7);
    scatter(g, rand, 12, far + 3, far + 25, () => bush(0x4f9a52));
    scatter(g, rand, 26, 2, far + 10, () => THINGS.flower([0xf284b6, 0xf2c230, 0xf4f4f0][(rand() * 3) | 0]), false, 0.5, 0.9);
    scatter(g, rand, 40, 1, far + 12, () => grass(0x5f9a3c), false);
  }

  // weather particles
  if (!outer && !indoor && (weather === 'rain' || weather === 'storm' || weather === 'snow')) {
    const snow = weather === 'snow', n = snow ? 1400 : weather === 'storm' ? 2600 : 1500, W = R * 2 + 26, H = 26;
    const p = new Float32Array(n * (snow ? 3 : 6)), seed = new Float32Array(n * 3);
    for (let i = 0; i < n * 3; i++) seed[i] = rand();
    const geo = new THREE.BufferGeometry();
    geo.setAttribute('position', new THREE.BufferAttribute(p, 3));
    const obj = snow
      ? new THREE.Points(geo, new THREE.PointsMaterial({ color: 0xffffff, size: 0.16, transparent: true, opacity: 0.95 }))
      : new THREE.LineSegments(geo, new THREE.LineBasicMaterial({ color: 0xbfd6ea, transparent: true, opacity: 0.55 }));
    obj.frustumCulled = false;
    g.add(obj);
    env.dispose.push(geo, obj.material);
    const v = snow ? 1.7 : weather === 'storm' ? 34 : 26, slant = weather === 'storm' ? 0.35 : 0.08;
    env.tick.push((t) => {
      for (let i = 0; i < n; i++) {
        const y = H - ((seed[i * 3 + 1] * H + t * v * (0.8 + seed[i * 3 + 2] * 0.4)) % H);
        let x = (seed[i * 3] - 0.5) * W, z = (seed[i * 3 + 2] - 0.5) * W;
        if (snow) {
          x += sin(t * 0.9 + i) * 0.5;
          p.set([x, y, z], i * 3);
        } else {
          x += (H - y) * slant;
          p.set([x, y, z, x + slant * 0.7, y - 0.7, z], i * 6);
        }
      }
      geo.attributes.position.needsUpdate = true;
    });
    if (weather === 'storm') {
      const base = hemi.intensity;
      env.tick.push((t) => {
        const c = t % 5.3;
        hemi.intensity = base + (c < 0.1 || (c > 0.2 && c < 0.27) ? 3.2 : 0);
      });
    }
  }

  env.update = (t) => { for (const f of env.tick) f(t); };
  return env;
}