@adatechnology/game-weather-three 0.5.20 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,6 +22,7 @@ var GameWeatherThree = (() => {
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
24
  ASH: () => ASH,
25
+ CLOUD_COVER: () => CLOUD_COVER,
25
26
  FOG_EXP_FROM: () => FOG_EXP_FROM,
26
27
  FOG_NEAR: () => FOG_NEAR,
27
28
  FOG_ZONE_NEAR: () => FOG_ZONE_NEAR,
@@ -37,11 +38,14 @@ var GameWeatherThree = (() => {
37
38
  RAIN_CURTAIN_PROFILES: () => RAIN_CURTAIN_PROFILES,
38
39
  RAIN_CURTAIN_VERTEX: () => RAIN_CURTAIN_VERTEX,
39
40
  SKY_DAY: () => SKY_DAY,
41
+ SKY_DOME: () => SKY_DOME,
40
42
  SKY_NIGHT: () => SKY_NIGHT,
41
43
  SPLASH_FROM_MM: () => SPLASH_FROM_MM,
42
44
  WEATHER_LAYERS: () => WEATHER_LAYERS,
43
45
  ZONE_FOG_DEFAULTS: () => ZONE_FOG_DEFAULTS,
44
46
  ZONE_LIGHT: () => ZONE_LIGHT,
47
+ cloudCoverOf: () => cloudCoverOf,
48
+ cloudDarknessOf: () => cloudDarknessOf,
45
49
  createFog: () => createFog2,
46
50
  createHeadlights: () => createHeadlights,
47
51
  createHeatHaze: () => createHeatHaze,
@@ -55,7 +59,12 @@ var GameWeatherThree = (() => {
55
59
  densityForFar: () => densityForFar,
56
60
  emitAirborne: () => emitAirborne,
57
61
  fogOf: () => fogOf,
62
+ nightnessOf: () => nightnessOf,
58
63
  rainCurtainPositions: () => rainCurtainPositions,
64
+ starVisibilityOf: () => starVisibilityOf,
65
+ sunDirectionOf: () => sunDirectionOf,
66
+ sunVisibilityOf: () => sunVisibilityOf,
67
+ sunsetOf: () => sunsetOf,
59
68
  weatherVisualTextOf: () => weatherVisualTextOf
60
69
  });
61
70
 
@@ -236,22 +245,282 @@ var GameWeatherThree = (() => {
236
245
  }
237
246
  var EFFECT_STEP_SECONDS = 1 / 50;
238
247
 
248
+ // src/sky-shader.ts
249
+ var SKY_VERTEX = (
250
+ /* glsl */
251
+ `
252
+ varying vec3 vDir;
253
+ void main() {
254
+ vDir = normalize(position);
255
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
256
+ }
257
+ `
258
+ );
259
+ var SKY_FRAGMENT = (
260
+ /* glsl */
261
+ `
262
+ precision highp float;
263
+ uniform vec3 uZenith;
264
+ uniform vec3 uHorizon;
265
+ uniform vec3 uTint;
266
+ uniform vec3 uSunDir;
267
+ uniform vec3 uSunColor;
268
+ uniform vec3 uMoonDir;
269
+ uniform vec3 uCloudLit;
270
+ uniform vec3 uCloudShade;
271
+ uniform vec3 uDrift;
272
+ uniform float uSunsetAmount;
273
+ uniform float uSunVis;
274
+ uniform float uMoonVis;
275
+ uniform float uStars;
276
+ uniform float uCover;
277
+ uniform float uTime;
278
+ uniform float uFlash;
279
+ varying vec3 vDir;
280
+
281
+ float hash2(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
282
+ float hash3(vec3 p) { return fract(sin(dot(p, vec3(127.1, 311.7, 74.7))) * 43758.5453); }
283
+ float noise(vec2 p) {
284
+ vec2 i = floor(p);
285
+ vec2 f = fract(p);
286
+ f = f * f * (3.0 - 2.0 * f);
287
+ return mix(mix(hash2(i), hash2(i + vec2(1.0, 0.0)), f.x), mix(hash2(i + vec2(0.0, 1.0)), hash2(i + vec2(1.0, 1.0)), f.x), f.y);
288
+ }
289
+ float fbm(vec2 p) {
290
+ float value = 0.0;
291
+ float amplitude = 0.5;
292
+ for (int octave = 0; octave < 4; octave++) {
293
+ value += amplitude * noise(p);
294
+ p = p * 2.03 + vec2(17.1, 9.2);
295
+ amplitude *= 0.5;
296
+ }
297
+ return value;
298
+ }
299
+
300
+ void main() {
301
+ vec3 d = normalize(vDir);
302
+ float height = max(d.y, 0.0);
303
+ vec3 color = mix(uHorizon, uZenith, pow(height, 0.6));
304
+
305
+ vec2 azimuth = normalize(d.xz + vec2(1e-5));
306
+ vec2 sunAzimuth = normalize(uSunDir.xz + vec2(1e-5));
307
+ float towardSun = max(dot(azimuth, sunAzimuth), 0.0);
308
+ color = mix(color, uTint, uSunsetAmount * exp(-height * 6.0) * (0.25 + 0.75 * towardSun * towardSun));
309
+
310
+ float aboveHorizon = smoothstep(-0.05, 0.05, d.y);
311
+ if (uStars > 0.001 && d.y > 0.0) {
312
+ vec3 p = d * 120.0;
313
+ vec3 cell = floor(p);
314
+ float pick = hash3(cell);
315
+ if (pick > 0.985) {
316
+ vec3 spot = vec3(hash3(cell + 1.7), hash3(cell + 5.3), hash3(cell + 9.1)) * 0.4 + 0.3;
317
+ float twinkle = 0.78 + 0.22 * sin(uTime * 2.0 + pick * 60.0);
318
+ float star = (1.0 - smoothstep(0.04, 0.32, length(fract(p) - spot))) * (0.45 + 0.55 * fract(pick * 97.0)) * twinkle;
319
+ color += vec3(0.85, 0.9, 1.0) * star * uStars * smoothstep(0.0, 0.12, d.y);
320
+ }
321
+ }
322
+
323
+ if (uMoonVis > 0.001) {
324
+ float toMoon = dot(d, uMoonDir);
325
+ vec3 tangent = normalize(cross(uMoonDir, vec3(0.0, 1.0, 0.0)));
326
+ vec2 onDisc = vec2(dot(d, tangent), dot(d, cross(uMoonDir, tangent))) / 0.045;
327
+ float disc = (1.0 - smoothstep(0.93, 1.0, length(onDisc))) * step(0.0, toMoon);
328
+ vec3 moon = vec3(0.7, 0.73, 0.8) * (0.62 + 0.38 * fbm(onDisc * 3.0 + 3.0));
329
+ color += (moon * disc + vec3(0.55, 0.62, 0.8) * pow(max(toMoon, 0.0), 220.0) * 0.5) * uMoonVis * aboveHorizon;
330
+ }
331
+
332
+ if (uSunVis > 0.001) {
333
+ float toSun = dot(d, uSunDir);
334
+ float disc = smoothstep(0.99935, 0.99965, toSun);
335
+ float glow = pow(max(toSun, 0.0), 48.0) * 0.5 + pow(max(toSun, 0.0), 6.0) * 0.14;
336
+ color += uSunColor * (glow + disc * 2.5) * uSunVis * aboveHorizon;
337
+ }
338
+
339
+ if (d.y > -0.05) {
340
+ vec2 uv = d.xz / (height + 0.18) * 1.1 + uDrift.xy;
341
+ float density = fbm(uv);
342
+ float threshold = 0.74 - uCover * 0.9;
343
+ float coverage = smoothstep(threshold, threshold + 0.22, density) * smoothstep(-0.05, 0.12, d.y);
344
+ float thick = smoothstep(threshold, threshold + 0.5, density);
345
+ vec3 cloud = mix(uCloudLit, uCloudShade, thick * 0.8);
346
+ cloud = mix(uHorizon, cloud, smoothstep(0.0, 0.3, d.y));
347
+ color = mix(color, cloud, coverage);
348
+ }
349
+
350
+ color = mix(color, vec3(0.78, 0.82, 0.95), uFlash * 0.7);
351
+ gl_FragColor = vec4(color, 1.0);
352
+ #include <colorspace_fragment>
353
+ }
354
+ `
355
+ );
356
+
357
+ // src/sky-state.ts
358
+ var clamp01 = (value) => value < 0 ? 0 : value > 1 ? 1 : value;
359
+ var smoothstep = (from, to, value) => {
360
+ const t = clamp01((value - from) / (to - from));
361
+ return t * t * (3 - 2 * t);
362
+ };
363
+ var CLOUD_COVER = { clear: 0.2, perSkyDimming: 7, perRainMm: 0.4, rainFullAtMm: 5, windMax: 0.15, windFullAt: 20 };
364
+ var DARK_FROM_SKY_SCALE = 0.95;
365
+ var DARK_SPAN = 0.45;
366
+ function cloudCoverOf(sample) {
367
+ const dimming = clamp01(1 - sample.skyScale);
368
+ const rain = Math.min(1, sample.rain / CLOUD_COVER.rainFullAtMm);
369
+ const wind = Math.min(1, Math.hypot(sample.windX, sample.windZ) / CLOUD_COVER.windFullAt) * CLOUD_COVER.windMax;
370
+ return clamp01(CLOUD_COVER.clear + dimming * CLOUD_COVER.perSkyDimming + rain * CLOUD_COVER.perRainMm + wind);
371
+ }
372
+ function cloudDarknessOf(sample) {
373
+ return clamp01((DARK_FROM_SKY_SCALE - sample.skyScale) / DARK_SPAN);
374
+ }
375
+ var nightnessOf = (sample) => clamp01(sample.nightFactor) ** 3;
376
+ function starVisibilityOf(sample) {
377
+ return nightnessOf(sample) * clamp01(1 - cloudCoverOf(sample) * 1.25);
378
+ }
379
+ function sunsetOf(sample) {
380
+ const low = smoothstep(0.02, 0.15, sample.sun) * (1 - smoothstep(0.15, 0.5, sample.sun));
381
+ return low * (1 - cloudCoverOf(sample) * 0.75);
382
+ }
383
+ var sunVisibilityOf = (sample) => smoothstep(0.02, 0.15, sample.sun);
384
+ function sunDirectionOf(sample, host, out) {
385
+ if (host) {
386
+ const target = host.target?.position;
387
+ out.x = host.position.x - (target?.x ?? 0);
388
+ out.y = host.position.y - (target?.y ?? 0);
389
+ out.z = host.position.z - (target?.z ?? 0);
390
+ } else {
391
+ const angle = (sample.dayFraction - 0.25) * Math.PI * 2;
392
+ out.x = Math.cos(angle);
393
+ out.y = Math.sin(angle);
394
+ out.z = -0.3;
395
+ }
396
+ const length = Math.hypot(out.x, out.y, out.z) || 1;
397
+ out.x /= length;
398
+ out.y /= length;
399
+ out.z /= length;
400
+ return out;
401
+ }
402
+
239
403
  // src/sky.ts
240
404
  var SKY_NIGHT = 724760;
241
405
  var SKY_DAY = 8885926;
242
406
  var ZONE_LIGHT = { base: 0.35, sun: 0.65 };
243
407
  var ZONE_FOG_DEFAULTS = { color: 3820090, far: 60 };
244
- function createSky(THREE, scene) {
408
+ var SKY_DOME = {
409
+ zenithDay: 4159684,
410
+ zenithNight: 132106,
411
+ sunsetTint: 16742973,
412
+ sunDay: 16774368,
413
+ sunSet: 16751184,
414
+ cloudDay: 16054010,
415
+ cloudOvercast: 12107462,
416
+ cloudStorm: 4080460,
417
+ cloudNight: 1712176,
418
+ /** How much of the sunset warmth the horizon (and so the fog) takes. */
419
+ fogWarmth: 0.3,
420
+ /** Clouds drift this many noise units per second for each m/s of wind (wind capped at `driftMaxWind`). */
421
+ drift: 25e-4,
422
+ driftMaxWind: 20,
423
+ /** Largest radius of the dome; it is 90 % of the camera's far plane when that is smaller. */
424
+ radius: 900,
425
+ renderOrder: -1e3
426
+ };
427
+ function createDome(THREE, camera2) {
428
+ const vector2 = () => ({ value: new THREE.Vector3() });
429
+ const colour = (hex) => ({ value: new THREE.Color(hex) });
430
+ const uniforms = {
431
+ uZenith: colour(SKY_DOME.zenithDay),
432
+ uHorizon: colour(SKY_DAY),
433
+ uTint: colour(SKY_DOME.sunsetTint),
434
+ uSunDir: vector2(),
435
+ uSunColor: colour(SKY_DOME.sunDay),
436
+ uMoonDir: vector2(),
437
+ uCloudLit: colour(SKY_DOME.cloudDay),
438
+ uCloudShade: colour(SKY_DOME.cloudOvercast),
439
+ uDrift: vector2(),
440
+ uSunsetAmount: { value: 0 },
441
+ uSunVis: { value: 0 },
442
+ uMoonVis: { value: 0 },
443
+ uStars: { value: 0 },
444
+ uCover: { value: 0 },
445
+ uTime: { value: 0 },
446
+ uFlash: { value: 0 }
447
+ };
448
+ const geometry = new THREE.SphereGeometry(1, 24, 12);
449
+ const material = new THREE.ShaderMaterial({ uniforms, vertexShader: SKY_VERTEX, fragmentShader: SKY_FRAGMENT, side: THREE.BackSide, depthTest: false, depthWrite: false, fog: false, toneMapped: false });
450
+ const mesh2 = new THREE.Mesh(geometry, material);
451
+ mesh2.name = "sky-dome";
452
+ mesh2.renderOrder = SKY_DOME.renderOrder;
453
+ mesh2.frustumCulled = false;
454
+ mesh2.raycast = () => void 0;
455
+ const far = camera2.far;
456
+ mesh2.scale.setScalar(far > 0 ? Math.min(SKY_DOME.radius, far * 0.9) : SKY_DOME.radius);
457
+ return {
458
+ mesh: mesh2,
459
+ uniforms,
460
+ dispose() {
461
+ mesh2.removeFromParent();
462
+ geometry.dispose();
463
+ material.dispose();
464
+ }
465
+ };
466
+ }
467
+ function createSky(THREE, scene, options = {}) {
245
468
  const night = new THREE.Color(SKY_NIGHT);
246
469
  const day = new THREE.Color(SKY_DAY);
247
470
  const zoneColor = new THREE.Color(ZONE_FOG_DEFAULTS.color);
248
471
  const tint = new THREE.Color();
249
472
  const color = new THREE.Color();
473
+ const sunsetColor = new THREE.Color(SKY_DOME.sunsetTint);
474
+ const zenithDay = new THREE.Color(SKY_DOME.zenithDay);
475
+ const zenithNight = new THREE.Color(SKY_DOME.zenithNight);
476
+ const sunDay = new THREE.Color(SKY_DOME.sunDay);
477
+ const sunSet = new THREE.Color(SKY_DOME.sunSet);
478
+ const cloudDay = new THREE.Color(SKY_DOME.cloudDay);
479
+ const cloudOvercast = new THREE.Color(SKY_DOME.cloudOvercast);
480
+ const cloudStorm = new THREE.Color(SKY_DOME.cloudStorm);
481
+ const cloudNight = new THREE.Color(SKY_DOME.cloudNight);
482
+ const sunDirection = { x: 0, y: 1, z: 0 };
483
+ const dome = options.camera ? createDome(THREE, options.camera) : void 0;
484
+ let time = 0;
485
+ const updateDome = (sample, dt) => {
486
+ if (!dome || !options.camera) return;
487
+ const { uniforms, mesh: mesh2 } = dome;
488
+ time += dt;
489
+ const cover = cloudCoverOf(sample);
490
+ const sunset = sunsetOf(sample);
491
+ const dark = cloudDarknessOf(sample);
492
+ mesh2.visible = true;
493
+ mesh2.position.copy(options.camera.position);
494
+ const zenith = uniforms.uZenith.value;
495
+ zenith.lerpColors(zenithNight, zenithDay, sample.sun).multiplyScalar(sample.skyScale).lerp(color, cover * 0.85);
496
+ uniforms.uHorizon.value.copy(color);
497
+ const lit = uniforms.uCloudLit.value;
498
+ lit.lerpColors(cloudDay, cloudOvercast, cover * cover).lerp(cloudStorm, dark).lerp(cloudNight, 1 - Math.min(1, sample.sun * 2.2));
499
+ lit.lerp(sunsetColor, sunset * 0.45);
500
+ uniforms.uCloudShade.value.copy(lit).multiplyScalar(0.62);
501
+ uniforms.uSunColor.value.lerpColors(sunDay, sunSet, sunset);
502
+ const sunVector = uniforms.uSunDir.value;
503
+ sunDirectionOf(sample, options.sun, sunDirection);
504
+ sunVector.set(sunDirection.x, sunDirection.y, sunDirection.z);
505
+ uniforms.uMoonDir.value.set(-sunDirection.x, Math.abs(sunDirection.y) * 0.9 + 0.1, -sunDirection.z).normalize();
506
+ const drift = uniforms.uDrift.value;
507
+ drift.x += Math.max(-SKY_DOME.driftMaxWind, Math.min(SKY_DOME.driftMaxWind, sample.windX)) * SKY_DOME.drift * dt;
508
+ drift.y += Math.max(-SKY_DOME.driftMaxWind, Math.min(SKY_DOME.driftMaxWind, sample.windZ)) * SKY_DOME.drift * dt;
509
+ uniforms.uCover.value = cover;
510
+ uniforms.uSunsetAmount.value = sunset;
511
+ uniforms.uSunVis.value = sunVisibilityOf(sample);
512
+ uniforms.uMoonVis.value = nightnessOf(sample);
513
+ uniforms.uStars.value = starVisibilityOf(sample);
514
+ uniforms.uTime.value = time;
515
+ uniforms.uFlash.value = sample.flash;
516
+ };
250
517
  return {
251
518
  color,
252
- update(sample, zone) {
519
+ ...dome ? { object: dome.mesh } : {},
520
+ update(sample, zone, dt = 0) {
253
521
  color.lerpColors(night, day, sample.sun);
254
522
  color.multiplyScalar(sample.skyScale);
523
+ if (dome) color.lerp(sunsetColor, sunsetOf(sample) * SKY_DOME.fogWarmth);
255
524
  if (zone && zone.weight > 0) {
256
525
  zoneColor.setHex(zone.color ?? ZONE_FOG_DEFAULTS.color);
257
526
  color.lerp(tint.copy(zoneColor).multiplyScalar(ZONE_LIGHT.base + sample.sun * ZONE_LIGHT.sun), zone.weight);
@@ -259,7 +528,11 @@ var GameWeatherThree = (() => {
259
528
  const background = scene.background;
260
529
  if (background && background.isColor) background.copy(color);
261
530
  else scene.background = color.clone();
531
+ updateDome(sample, dt);
262
532
  return color;
533
+ },
534
+ dispose() {
535
+ dome?.dispose();
263
536
  }
264
537
  };
265
538
  }
@@ -2562,7 +2835,8 @@ var GameWeatherThree = (() => {
2562
2835
  const curtain = createRainCurtain(THREE, { ...options.random ? { random: options.random } : {}, ...options.profile ? { profile: options.profile } : {} });
2563
2836
  const haze = createHeatHaze(THREE);
2564
2837
  group.add(curtain.object, haze.object);
2565
- const sky = createSky(THREE, scene);
2838
+ const sky = createSky(THREE, scene, { camera: camera2, ...options.sun ? { sun: options.sun } : {} });
2839
+ if (sky.object) group.add(sky.object);
2566
2840
  const fog = createFog2(THREE, scene, { stable: options.lights !== void 0 });
2567
2841
  const lightning = createLightning(THREE, scene, vfx, options.lights ? { lights: options.lights } : {});
2568
2842
  const headlights = createHeadlights(THREE, options.lights ? { ...options.headlights, lights: options.lights } : options.headlights);
@@ -2577,6 +2851,7 @@ var GameWeatherThree = (() => {
2577
2851
  else if (id === "fog") fog.clear();
2578
2852
  else if (id === "lightning") lightning.light.intensity = 0;
2579
2853
  else if (id === "haze") haze.object.visible = false;
2854
+ else if (id === "sky" && sky.object) sky.object.visible = false;
2580
2855
  };
2581
2856
  return {
2582
2857
  curtain,
@@ -2596,7 +2871,7 @@ var GameWeatherThree = (() => {
2596
2871
  const where = [eye.x, eye.y, eye.z];
2597
2872
  const background = scene.background ?? void 0;
2598
2873
  const ownSky = on.get("sky") === true;
2599
- const color = ownSky ? sky.update(sample, zone) : background && background.isColor ? background : sky.color;
2874
+ const color = ownSky ? sky.update(sample, zone, dt) : background && background.isColor ? background : sky.color;
2600
2875
  if (on.get("fog")) fog.update(sample, color, zone);
2601
2876
  if (on.get("lightning")) lightning.update(sample, ownSky ? scene.background : void 0);
2602
2877
  if (on.get("far_rain")) curtain.update(sample, eye, time, sample.sun);
@@ -2623,6 +2898,7 @@ var GameWeatherThree = (() => {
2623
2898
  scene.remove(group);
2624
2899
  curtain.dispose();
2625
2900
  haze.dispose();
2901
+ sky.dispose();
2626
2902
  lightning.dispose();
2627
2903
  headlights.dispose();
2628
2904
  nearRain.clear();
@@ -2678,6 +2954,7 @@ var GameWeatherThree = (() => {
2678
2954
  ...options.puddleAt ? { puddleAt: options.puddleAt } : {},
2679
2955
  ...options.headlights ? { headlights: options.headlights } : {},
2680
2956
  ...options.layers ? { layers: options.layers } : {},
2957
+ ...sunOption(options.sun ?? sunOf(host.scene)),
2681
2958
  ...isLightBudget(host.lights) ? { lights: host.lights } : {}
2682
2959
  });
2683
2960
  const item = options.rigItem && host.addObject ? options.rigItem : void 0;
@@ -2685,6 +2962,15 @@ var GameWeatherThree = (() => {
2685
2962
  }
2686
2963
  };
2687
2964
  }
2965
+ function sunOf(scene) {
2966
+ let found;
2967
+ scene.traverse((node) => {
2968
+ const light = node;
2969
+ if (!found && light.isDirectionalLight && light.castShadow) found = light;
2970
+ });
2971
+ return found;
2972
+ }
2973
+ var sunOption = (sun) => sun ? { sun } : {};
2688
2974
  function footprintOf(car) {
2689
2975
  if (!car.width) return void 0;
2690
2976
  const z = car.group.position.z;