File size: 14,483 Bytes
59c7fd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import * as THREE from 'three';
import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
import { brickMaps, woodMaps, brassMaps, rubberMaps } from './textures.js';

export function createEnvironment(scene) {
  const env = new THREE.Group();
  env.name = 'environment';
  scene.add(env);

  // ---------------- sky dome ----------------
  const skyGeo = new THREE.SphereGeometry(160, 48, 32);
  const skyMat = new THREE.ShaderMaterial({
    side: THREE.BackSide,
    depthWrite: false,
    uniforms: {
      topColor: { value: new THREE.Color(0x35526f) },
      midColor: { value: new THREE.Color(0x9fb0bc) },
      horizonColor: { value: new THREE.Color(0xf2c79a) },
      sunColor: { value: new THREE.Color(0xffd9a2) },
      sunDir: { value: new THREE.Vector3(0.45, 0.32, 0.82).normalize() },
    },
    vertexShader: `
      varying vec3 vWorld;
      void main(){
        vec4 wp = modelMatrix * vec4(position,1.0);
        vWorld = wp.xyz;
        gl_Position = projectionMatrix * viewMatrix * wp;
      }`,
    fragmentShader: `
      varying vec3 vWorld;
      uniform vec3 topColor, midColor, horizonColor, sunColor, sunDir;
      void main(){
        vec3 d = normalize(vWorld);
        float h = clamp(d.y, -0.2, 1.0);
        vec3 col = mix(horizonColor, midColor, smoothstep(0.0, 0.24, h));
        col = mix(col, topColor, smoothstep(0.18, 0.75, h));
        float sun = pow(max(dot(d, sunDir), 0.0), 180.0);
        col += sunColor * sun * 1.4;
        float glow = pow(max(dot(d, sunDir), 0.0), 8.0);
        col += sunColor * glow * 0.16;
        // subtle haze band
        col = mix(col, vec3(0.93,0.76,0.62), (1.0 - smoothstep(0.0,0.12,abs(h-0.02))) * 0.16);
        gl_FragColor = vec4(col, 1.0);
      }`,
  });
  const sky = new THREE.Mesh(skyGeo, skyMat);
  env.add(sky);

  // ---------------- ground ----------------
  const courtyardMaps = brickMaps(17, 17, 'warm');
  const courtyard = new THREE.Mesh(
    new THREE.PlaneGeometry(44, 44),
    new THREE.MeshStandardMaterial({
      ...courtyardMaps, roughness: 0.9, metalness: 0.02, bumpScale: 0.06,
      color: 0xbcaa99,
    })
  );
  courtyard.rotation.x = -Math.PI / 2;
  courtyard.position.y = 0.001;
  courtyard.receiveShadow = true;
  env.add(courtyard);

  // outer darker ground / street
  const outerMaps = brickMaps(60, 60, 'wall');
  const outer = new THREE.Mesh(
    new THREE.PlaneGeometry(220, 220),
    new THREE.MeshStandardMaterial({ ...outerMaps, color: 0x6b5b4a, roughness: 1.0, bumpScale: 0.02 })
  );
  outer.rotation.x = -Math.PI / 2;
  outer.position.y = -0.012;
  outer.receiveShadow = true;
  env.add(outer);

  // stone border around courtyard
  const borderMat = new THREE.MeshStandardMaterial({
    color: 0x6f6a62, roughness: 0.7, metalness: 0.05,
  });
  for (const [x, z, w, d] of [[0, 22.2, 44.6, 0.5], [0, -22.2, 44.6, 0.5], [22.2, 0, 0.5, 44.6], [-22.2, 0, 0.5, 44.6]]) {
    const b = new THREE.Mesh(new RoundedBoxGeometry(w, 0.16, d, 2, 0.03), borderMat);
    b.position.set(x, 0.07, z);
    b.receiveShadow = true;
    env.add(b);
  }

  // ---------------- low brick walls ----------------
  const wallMaps = brickMaps(12, 2, 'wall');
  const wallMat = new THREE.MeshStandardMaterial({ ...wallMaps, roughness: 0.9, bumpScale: 0.05, color: 0xc9b6a6 });
  const capMat = new THREE.MeshStandardMaterial({ color: 0x8b8278, roughness: 0.7 });

  function wall(len, x, z, rotY) {
    const g = new THREE.Group();
    const base = new THREE.Mesh(new THREE.BoxGeometry(len, 1.25, 0.55), wallMat);
    base.position.y = 0.625;
    base.castShadow = base.receiveShadow = true;
    g.add(base);
    // brick pillar caps
    for (let i = -1; i <= 1; i += 2) {
      const p = new THREE.Mesh(new THREE.BoxGeometry(0.8, 1.55, 0.8), wallMat);
      p.position.set(i * len * 0.5, 0.775, 0);
      p.castShadow = p.receiveShadow = true;
      g.add(p);
      const cap = new THREE.Mesh(new RoundedBoxGeometry(0.95, 0.13, 0.95, 2, 0.03), capMat);
      cap.position.set(i * len * 0.5, 1.62, 0);
      cap.castShadow = true;
      g.add(cap);
    }
    const top = new THREE.Mesh(new RoundedBoxGeometry(len, 0.12, 0.68, 2, 0.03), capMat);
    top.position.y = 1.31;
    top.castShadow = top.receiveShadow = true;
    g.add(top);
    g.position.set(x, 0, z);
    g.rotation.y = rotY;
    env.add(g);
  }
  wall(13, 0, -18.5, 0);
  wall(13, -18.5, -2, Math.PI / 2);
  wall(13, 18.5, -2, Math.PI / 2);

  // ---------------- distant buildings ----------------
  const facadeMaps = brickMaps(9, 6, 'wall');
  const facadeMat = new THREE.MeshStandardMaterial({ ...facadeMaps, roughness: 0.92, color: 0xb9a89a });
  const roofMat = new THREE.MeshStandardMaterial({ color: 0x4a4440, roughness: 0.8 });
  const windowMat = new THREE.MeshStandardMaterial({
    color: 0x1e2a30, emissive: 0x2a1c10, emissiveIntensity: 0.35,
    roughness: 0.25, metalness: 0.4,
  });
  const buildings = [
    { x: -30, z: -34, w: 15, h: 7.5, d: 11, rot: 0.1 },
    { x: 5, z: -42, w: 20, h: 9, d: 12, rot: -0.05 },
    { x: 34, z: -28, w: 11, h: 6.5, d: 11, rot: -0.2 },
    { x: -38, z: 10, w: 11, h: 8, d: 18, rot: 0.08 },
    { x: 38, z: 16, w: 13, h: 6.2, d: 15, rot: 0.05 },
  ];
  for (const b of buildings) {
    const g = new THREE.Group();
    const base = new THREE.Mesh(new THREE.BoxGeometry(b.w, b.h, b.d), facadeMat);
    base.position.y = b.h / 2;
    base.castShadow = base.receiveShadow = true;
    g.add(base);
    // pitched roof
    const roofGeo = new THREE.ConeGeometry(Math.max(b.w, b.d) * 0.78, 2.4, 4);
    roofGeo.rotateY(Math.PI / 4);
    const roof = new THREE.Mesh(roofGeo, roofMat);
    roof.scale.set(b.w / Math.max(b.w, b.d), 1, b.d / Math.max(b.w, b.d));
    roof.position.y = b.h + 1.2;
    roof.rotation.y = 0.02;
    g.add(roof);
    // windows on all four facades
    const frameMat = new THREE.MeshStandardMaterial({ color: 0x8e8274, roughness: 0.7 });
    const faces = [
      { w: b.w, rot: 0, ox: 0, oz: b.d / 2 },
      { w: b.w, rot: Math.PI, ox: 0, oz: -b.d / 2 },
      { w: b.d, rot: Math.PI / 2, ox: b.w / 2, oz: 0 },
      { w: b.d, rot: -Math.PI / 2, ox: -b.w / 2, oz: 0 },
    ];
    for (const f of faces) {
      const cols = Math.max(2, Math.floor(f.w / 2.7));
      const rows = Math.max(2, Math.floor((b.h - 1.5) / 2.5));
      for (let c = 0; c < cols; c++) {
        for (let r = 0; r < rows; r++) {
          const frame = new THREE.Mesh(new RoundedBoxGeometry(1.15, 1.55, 0.12, 2, 0.02), frameMat);
          frame.position.set(0, 0, 0);
          frame.rotation.y = f.rot;
          const cx = -f.w / 2 + f.w / cols * (c + 0.5);
          const win = new THREE.Mesh(new THREE.PlaneGeometry(0.9, 1.2), windowMat);
          win.position.z = 0.07;
          frame.add(win);
          const holder = new THREE.Group();
          holder.add(frame);
          holder.position.set(f.ox, 1.8 + r * 2.35, f.oz);
          holder.position.x += Math.cos(f.rot) * cx;
          holder.position.z -= Math.sin(f.rot) * cx;
          // push frame out along facade normal
          holder.position.x += Math.sin(f.rot) * 0.03;
          holder.position.z += Math.cos(f.rot) * 0.03;
          g.add(holder);
        }
      }
    }
    g.position.set(b.x, 0, b.z);
    g.rotation.y = b.rot;
    env.add(g);
  }

  // ---------------- props (outside subject) ----------------
  // fire hydrant
  {
    const g = new THREE.Group();
    const red = new THREE.MeshStandardMaterial({ color: 0x9b1d16, roughness: 0.42, metalness: 0.35 });
    const brass = new THREE.MeshStandardMaterial({ color: 0xb08b3c, roughness: 0.35, metalness: 0.9 });
    const body = new THREE.Mesh(new THREE.CylinderGeometry(0.16, 0.2, 0.62, 14), red);
    body.position.y = 0.38;
    g.add(body);
    const dome = new THREE.Mesh(new THREE.SphereGeometry(0.17, 14, 8, 0, Math.PI * 2, 0, Math.PI / 2), red);
    dome.position.y = 0.69;
    g.add(dome);
    const cap = new THREE.Mesh(new THREE.CylinderGeometry(0.035, 0.05, 0.1, 8), brass);
    cap.position.y = 0.84;
    g.add(cap);
    for (const a of [0, Math.PI]) {
      const arm = new THREE.Mesh(new THREE.CylinderGeometry(0.055, 0.055, 0.16, 10), brass);
      arm.rotation.z = Math.PI / 2;
      arm.position.set(Math.cos(a) * 0.2, 0.54, Math.sin(a) * 0.2);
      g.add(arm);
    }
    const flange = new THREE.Mesh(new THREE.CylinderGeometry(0.26, 0.28, 0.08, 14), red);
    flange.position.y = 0.04;
    g.add(flange);
    g.traverse((o) => { if (o.isMesh) { o.castShadow = true; o.receiveShadow = true; } });
    g.position.set(6.2, 0, 5.4);
    g.rotation.y = 0.5;
    env.add(g);
  }

  // wooden crate / bucket
  {
    const wood = new THREE.MeshStandardMaterial({ ...woodMaps(1, 1), roughness: 0.75 });
    const bucket = new THREE.Group();
    const body = new THREE.Mesh(new THREE.CylinderGeometry(0.28, 0.22, 0.38, 16, 1, true), wood);
    body.material.side = THREE.DoubleSide;
    bucket.add(body);
    const bottom = new THREE.Mesh(new THREE.CircleGeometry(0.22, 16), wood);
    bottom.rotation.x = -Math.PI / 2;
    bottom.position.y = -0.19;
    bucket.add(bottom);
    for (const y of [-0.12, 0.14]) {
      const hoop = new THREE.Mesh(new THREE.TorusGeometry(0.255, 0.012, 6, 24), new THREE.MeshStandardMaterial({ color: 0x3b3b3b, metalness: 0.8, roughness: 0.5 }));
      hoop.rotation.x = Math.PI / 2;
      hoop.position.y = y;
      bucket.add(hoop);
    }
    bucket.traverse((o) => { if (o.isMesh) { o.castShadow = true; o.receiveShadow = true; } });
    bucket.position.set(-5.2, 0.19, 5.0);
    bucket.rotation.y = 0.7;
    env.add(bucket);
  }

  // cobbles & grass tufts along wall base (instanced)
  const cobbleGeo = new THREE.DodecahedronGeometry(0.12, 0);
  const cobbleMat = new THREE.MeshStandardMaterial({ color: 0x7a746b, roughness: 0.9 });
  const cobbles = new THREE.InstancedMesh(cobbleGeo, cobbleMat, 160);
  const cd = new THREE.Object3D();
  for (let i = 0; i < 160; i++) {
    const edge = i % 4;
    let x, z;
    if (edge === 0) { x = (Math.random() - 0.5) * 36; z = -17.6 - Math.random() * 0.6; }
    else if (edge === 1) { x = -17.6 - Math.random() * 0.6; z = (Math.random() - 0.5) * 34; }
    else if (edge === 2) { x = 17.6 + Math.random() * 0.6; z = (Math.random() - 0.5) * 34; }
    else { x = (Math.random() - 0.5) * 36; z = 20.8 + Math.random() * 0.4; }
    cd.position.set(x, 0.03 + Math.random() * 0.02, z);
    cd.rotation.set(Math.random() * 3, Math.random() * 3, Math.random() * 3);
    cd.scale.set(1, 0.5 + Math.random() * 0.4, 1);
    cd.updateMatrix();
    cobbles.setMatrixAt(i, cd.matrix);
  }
  cobbles.receiveShadow = true;
  env.add(cobbles);

  const grassGeo = new THREE.ConeGeometry(0.055, 0.34, 4);
  grassGeo.translate(0, 0.17, 0);
  const grassMat = new THREE.MeshStandardMaterial({ color: 0x5d6b3a, roughness: 0.9 });
  const grass = new THREE.InstancedMesh(grassGeo, grassMat, 110);
  for (let i = 0; i < 110; i++) {
    let x, z;
    const edge = i % 4;
    if (edge === 0) { x = (Math.random() - 0.5) * 34; z = -17.8 + Math.random() * 0.5; }
    else if (edge === 1) { x = -17.8 + Math.random() * 0.5; z = (Math.random() - 0.5) * 32; }
    else if (edge === 2) { x = 17.8 - Math.random() * 0.5; z = (Math.random() - 0.5) * 32; }
    else { x = (Math.random() - 0.5) * 34; z = 20.9; }
    cd.position.set(x, 0, z);
    cd.rotation.set(0, Math.random() * 6.28, (Math.random() - 0.5) * 0.3);
    cd.scale.setScalar(0.6 + Math.random() * 0.9);
    cd.updateMatrix();
    grass.setMatrixAt(i, cd.matrix);
  }
  grass.castShadow = true;
  env.add(grass);

  // hedge line just beyond the courtyard wall
  const hedgeMat = new THREE.MeshStandardMaterial({ color: 0x36452a, roughness: 1.0 });
  const hedgeGeo = new THREE.IcosahedronGeometry(1, 1);
  const hedge = new THREE.InstancedMesh(hedgeGeo, hedgeMat, 110);
  const hd = new THREE.Object3D();
  for (let i = 0; i < 110; i++) {
    const a = (i / 110) * Math.PI * 2;
    const rad = 24.5 + Math.random() * 3.5;
    hd.position.set(Math.cos(a) * rad, 0.75 + Math.random() * 0.3, Math.sin(a) * rad);
    hd.scale.set(1.4 + Math.random(), 0.95 + Math.random() * 0.45, 1.4 + Math.random());
    hd.rotation.set(Math.random(), Math.random() * 3, Math.random());
    hd.updateMatrix();
    hedge.setMatrixAt(i, hd.matrix);
  }
  hedge.castShadow = true;
  hedge.receiveShadow = true;
  env.add(hedge);

  // distant tree line / hedge masking the horizon
  const treeMat = new THREE.MeshStandardMaterial({ color: 0x2f3a22, roughness: 1.0 });
  const trunkMat = new THREE.MeshStandardMaterial({ color: 0x3a2a1c, roughness: 1.0 });
  const treeGeo = new THREE.IcosahedronGeometry(1, 1);
  const trunkGeo = new THREE.CylinderGeometry(0.16, 0.22, 2.0, 6);
  const trees = new THREE.Group();
  for (let i = 0; i < 78; i++) {
    const a = (i / 46) * Math.PI * 2 + Math.random() * 0.1;
    const rad = 48 + Math.random() * 24;
    const x = Math.cos(a) * rad, z = Math.sin(a) * rad;
    const h = 3.0 + Math.random() * 2.2;
    const trunk = new THREE.Mesh(trunkGeo, trunkMat);
    trunk.position.set(x, 1, z);
    trunk.scale.setScalar(0.8 + Math.random() * 0.5);
    trees.add(trunk);
    const crown = new THREE.Mesh(treeGeo, treeMat);
    crown.position.set(x, 2.3 + h * 0.55, z);
    crown.scale.set(2.0 + Math.random() * 1.6, h * 0.7, 2.0 + Math.random() * 1.6);
    trees.add(crown);
  }
  trees.traverse(o => { if (o.isMesh) { o.castShadow = false; o.receiveShadow = false; } });
  env.add(trees);

  // ---------------- lights ----------------
  const hemi = new THREE.HemisphereLight(0xaccbe4, 0x6a4c38, 0.55);
  scene.add(hemi);

  const sun = new THREE.DirectionalLight(0xffd2a1, 2.55);
  sun.position.set(13, 17, 9);
  sun.castShadow = true;
  sun.shadow.mapSize.set(2048, 2048);
  sun.shadow.camera.left = -11;
  sun.shadow.camera.right = 11;
  sun.shadow.camera.top = 11;
  sun.shadow.camera.bottom = -11;
  sun.shadow.camera.near = 1;
  sun.shadow.camera.far = 55;
  sun.shadow.bias = -0.0006;
  sun.shadow.normalBias = 0.02;
  sun.shadow.radius = 3;
  scene.add(sun);
  scene.add(sun.target);

  const fill = new THREE.DirectionalLight(0x8fb4d8, 0.85);
  fill.position.set(-12, 8, -10);
  scene.add(fill);

  const rim = new THREE.DirectionalLight(0xffb072, 0.7);
  rim.position.set(-6, 5, 11);
  scene.add(rim);

  // warm fill near the truck front
  const bounce = new THREE.PointLight(0xffc88f, 8, 12, 2);
  bounce.position.set(4, 2.2, 4.5);
  scene.add(bounce);

  scene.fog = new THREE.FogExp2(0xc9a988, 0.009);
  scene.background = new THREE.Color(0xd9b995);

  return env;
}