@bendyline/molen-client 0.0.12 → 0.0.15

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.
@@ -1,3 +1,4 @@
1
+ import { a as SnowCoverUniforms } from "./snow-cover-tEG1uO98.mjs";
1
2
  import { EarthObserver, Quat, ResolvedWeather, SkyData, SkyPalette, SkyTime, Vec3, WeatherData } from "@bendyline/molen-schema";
2
3
  import * as THREE from "three";
3
4
  import { WebGPURenderer } from "three/webgpu";
@@ -9,7 +10,8 @@ interface AdmissionOptions {
9
10
  signal?: AbortSignal;
10
11
  label?: string;
11
12
  /**
12
- * Background jobs run only in frame budget that no normal job is waiting for. Urgent jobs run
13
+ * Background jobs run in frame budget that no normal job is waiting for, and once one has waited
14
+ * `backgroundWaitMs` it takes a turn after every `backgroundShare` other jobs. Urgent jobs run
13
15
  * before normal ones, in their own order: small work the view is visibly waiting on, such as
14
16
  * terrain ground. Sustained urgent traffic shares service with waiting normal work.
15
17
  */
@@ -28,11 +30,21 @@ interface AdmissionSample {
28
30
  }
29
31
  interface SceneAdmission {
30
32
  run<T>(task: () => T, options?: AdmissionOptions): Promise<T>;
33
+ /** Jobs waiting, where the queue can say, so optional work can hold off behind a backlog. */
34
+ readonly pending?: number;
31
35
  }
32
36
  interface FrameAdmissionOptions {
33
37
  maxMilliseconds?: number;
34
38
  maxBytes?: number;
35
39
  maxJobs?: number;
40
+ /**
41
+ * How long a background job waits (ms, default 750) before the lane takes turns with the others,
42
+ * and how many other jobs run between its turns (default 8). In flight normal work never runs
43
+ * out, and what waited behind it (texture upgrades, the levels an object is not showing yet)
44
+ * arrived seconds late: a second on average over a town, five at worst.
45
+ */
46
+ backgroundWaitMs?: number;
47
+ backgroundShare?: number;
36
48
  now?: () => number;
37
49
  schedule?: (callback: () => void) => void;
38
50
  onSample?: (sample: AdmissionSample) => void;
@@ -45,6 +57,14 @@ declare class FrameAdmissionQueue implements SceneAdmission {
45
57
  private scheduled;
46
58
  private disposed;
47
59
  private paused;
60
+ /** Milliseconds past its budget the last flush ran, repaid from the frames after it. */
61
+ private overrunMs;
62
+ /** When the last flush finished, so a repaying frame knows how long it was. */
63
+ private lastFlushEnd;
64
+ /** The last flush left work waiting, so the time since it ended is a frame, not idle time. */
65
+ private continuing;
66
+ /** Frame lengths outside the queue while work waited, newest last, at most SLOW_FRAMES. */
67
+ private readonly recentFrames;
48
68
  private readonly clock;
49
69
  private readonly schedule;
50
70
  private maxMs;
@@ -52,6 +72,10 @@ declare class FrameAdmissionQueue implements SceneAdmission {
52
72
  private maxJobs;
53
73
  private urgentStreak;
54
74
  private orderedStreak;
75
+ /** Jobs from the other lanes since the background lane last ran one. */
76
+ private foregroundStreak;
77
+ private readonly backgroundWaitMs;
78
+ private readonly backgroundShare;
55
79
  /** Recent work time per label (ms, smoothed): what the next job of that kind will likely take. */
56
80
  private readonly expected;
57
81
  constructor(options?: FrameAdmissionOptions);
@@ -78,8 +102,26 @@ declare class FrameAdmissionQueue implements SceneAdmission {
78
102
  /** Hold queued uploads/preparation while the view is inactive; keep resources for resume. */
79
103
  setPaused(paused: boolean): void;
80
104
  private request;
81
- /** One frame's budget. Exposed for hosts with a manual frame loop and deterministic tests. */
105
+ /**
106
+ * One frame's budget. Exposed for hosts with a manual frame loop and deterministic tests.
107
+ *
108
+ * A job cannot be split, so one can take several frames' budget by itself, and a queue of them
109
+ * would hold every frame at that length however small the budget. A frame that overruns repays
110
+ * the excess from the frames after it, so the budget holds on average: a 20 ms shader build
111
+ * every third frame, rather than every frame at half the frame rate, for the same throughput.
112
+ *
113
+ * The budget is a share of a frame at 60 a second. A frame that comes later than that repays
114
+ * the same share of its own length: under software rendering a frame takes 200 ms or more, and
115
+ * a job that waits on the GPU once is charged for a whole such frame, so repaying 2 ms a frame
116
+ * left the queue idle for minutes. At 60 frames a second and faster nothing changes. Sustained
117
+ * frames that slow also run that share of themselves (see SLOW_FRAME_MS).
118
+ */
82
119
  flush(): void;
120
+ /**
121
+ * This frame's work allowance: the fixed budget, or for sustained software-rendering frames the
122
+ * same share of the shortest recent one. One long frame (a hitch) changes nothing.
123
+ */
124
+ private frameBudget;
83
125
  dispose(): void;
84
126
  }
85
127
  //#endregion
@@ -201,6 +243,7 @@ declare class SkyVisual {
201
243
  readonly data: SkyData;
202
244
  private readonly dome;
203
245
  private readonly sun;
246
+ private readonly glare;
204
247
  private readonly moon;
205
248
  private readonly stars;
206
249
  private catalog;
@@ -236,6 +279,10 @@ declare class SkyVisual {
236
279
  prepareCamera(camera: THREE.Camera): void;
237
280
  private updateDome;
238
281
  private updateBody;
282
+ private sunColor;
283
+ private updateGlare;
284
+ /** The drawn dome's color toward a unit direction, interpolated across its vertex grid. */
285
+ private domeColor;
239
286
  /**
240
287
  * Replace the star catalog, e.g. once a catalog loaded from a content pack arrives. `undefined`
241
288
  * removes the stars. Takes effect on the next update.
@@ -473,6 +520,16 @@ declare class WeatherVisual {
473
520
  dispose(): void;
474
521
  }
475
522
  //#endregion
523
+ //#region src/three/far-shadow.d.ts
524
+ interface FarShadowInfo {
525
+ /** Half-width of the map as drawn, meters. */
526
+ radius: number;
527
+ /** Times the map has been drawn. */
528
+ draws: number;
529
+ /** Main-thread time of its last drawing, ms. */
530
+ lastDrawMs: number;
531
+ }
532
+ //#endregion
476
533
  //#region src/three/gpu-frame-timer.d.ts
477
534
  interface GpuFrameTimerOptions {
478
535
  /** Maximum outstanding queries, including an active measurement. Default 4. */
@@ -503,6 +560,15 @@ interface ShadowFocus {
503
560
  center: readonly [number, number, number];
504
561
  /** Minimum half-width of the shadowed square, meters. Rounded up for a stable texel grid. */
505
562
  radius: number;
563
+ /**
564
+ * A second, wider map around `center` (absolute world coordinates) covering at least
565
+ * ±`radius` meters, for the scenery named by `Renderer.setFarShadowCasters`: surfaces past the
566
+ * focused map read it instead of ending in sunlight. WebGL.
567
+ */
568
+ far?: {
569
+ center: readonly [number, number, number];
570
+ radius: number;
571
+ };
506
572
  }
507
573
  //#endregion
508
574
  //#region src/three/webgpu-ambient-occlusion.d.ts
@@ -565,6 +631,8 @@ type ThreeRenderer = THREE.WebGLRenderer | WebGPURenderer;
565
631
  interface CloudBackend {
566
632
  material(uniforms: CloudUniforms, textures: CloudTextures): THREE.Material;
567
633
  installShadows(uniforms: CloudShadowUniforms): void;
634
+ /** Lying snow on marked materials (WebGPU: webgpu-snow-cover.ts). */
635
+ installSnowCover(uniforms: SnowCoverUniforms): void;
568
636
  }
569
637
  interface InitializedRenderer {
570
638
  three: ThreeRenderer;
@@ -729,6 +797,8 @@ declare class Renderer {
729
797
  /** Cloud textures and the cloud-shadow uniforms every lit shader reads, for the renderer's life. */
730
798
  private readonly cloudTextures;
731
799
  private readonly cloudShadows;
800
+ /** The weather's lying snow, read by every material marked with `enableSnowCover`. */
801
+ private readonly snowCover;
732
802
  private readonly cloudBackend;
733
803
  private releaseCloudShadows;
734
804
  private cloudQualityLevel;
@@ -742,6 +812,10 @@ declare class Renderer {
742
812
  private shadowFocus;
743
813
  private readonly shadowCenter;
744
814
  private readonly shadowDirection;
815
+ /** The sun's far shadow map (WebGL); see setFarShadowCasters. */
816
+ private farShadow;
817
+ private readonly farShadowCenter;
818
+ private readonly farShadowDirection;
745
819
  /** Active clear-sky visual and sampled ephemeris, when environment.sky is configured. */
746
820
  get sky(): SkyVisual | undefined;
747
821
  /**
@@ -751,6 +825,24 @@ declare class Renderer {
751
825
  */
752
826
  setShadowFocus(focus: ShadowFocus | undefined): void;
753
827
  /**
828
+ * The scenery that does not move (a terrain stream's object: ground, buildings, trees), for the
829
+ * sun's far shadow map. With it and a focus that names a `far` square, surfaces past the
830
+ * focused map are shadowed from a second, wider map instead of ending in sunlight: over a
831
+ * city the focused map ends a few hundred meters out, and buildings gained their shadows as
832
+ * it reached them. Only what is under `root` casts into it, and it is redrawn when the camera
833
+ * has left its middle, when the sun has moved and about once a second for scenery that has
834
+ * arrived, so a moving caster would leave its shadow behind. WebGL; `undefined` turns it off.
835
+ */
836
+ setFarShadowCasters(root: THREE.Object3D | undefined): void;
837
+ /** The far shadow map as last drawn, or `undefined` while there is none. */
838
+ get farShadowInfo(): FarShadowInfo | undefined;
839
+ /**
840
+ * Meters one texel of the sun's shadow map covers as of the last frame, or `undefined` while sun
841
+ * shadows are off or parked. A focus over a wide view coarsens it: hosts use it to keep a subject
842
+ * the map cannot resolve out of the shadow pass (`AircraftVisual.updateShadows`).
843
+ */
844
+ get shadowTexelSize(): number | undefined;
845
+ /**
754
846
  * Turn sun shadows on at a map resolution, or off, without rebuilding the environment. Once a
755
847
  * light has cast shadows, 'off' stops redrawing its map and fades it out, so every lit shader
756
848
  * keeps the shadow inputs it was built with: a host that turns shadows on once before its first
@@ -945,4 +1037,4 @@ declare class Renderer {
945
1037
  dispose(): void;
946
1038
  }
947
1039
  //#endregion
948
- export { SkyReflectionState as A, defaultSkyPalette as C, skyTimeMs as D, evaluateEarthSky as E, AdmissionSample as F, FrameAdmissionOptions as I, FrameAdmissionQueue as L, createWebGlSkyReflectionFilter as M, reflectionStateFromLights as N, starDirection as O, AdmissionOptions as P, SceneAdmission as R, SkyVisualOptions as S, EarthSkyState as T, WeatherVisualOptions as _, RendererBackendPreference as a, SkyStar as b, TopDownOrtho as c, ShadowFocus as d, ShadowQuality as f, WeatherVisual as g, createGpuFrameTimer as h, RendererBackend as i, SkyReflections as j, SkyReflectionFilter as k, nextWorldOrigin as l, GpuFrameTimerOptions as m, CameraPose as n, RendererOptions as o, GpuFrameTimer as p, Renderer as r, ThreeRenderer as s, AutoWorldOriginOptions as t, orthoFrustum as u, CloudQuality as v, CelestialPosition as w, SkyVisual as x, SkyFrame as y };
1040
+ export { SkyReflectionFilter as A, SkyVisualOptions as C, evaluateEarthSky as D, EarthSkyState as E, AdmissionOptions as F, AdmissionSample as I, FrameAdmissionOptions as L, SkyReflections as M, createWebGlSkyReflectionFilter as N, skyTimeMs as O, reflectionStateFromLights as P, FrameAdmissionQueue as R, SkyVisual as S, CelestialPosition as T, WeatherVisual as _, RendererBackendPreference as a, SkyFrame as b, TopDownOrtho as c, ShadowFocus as d, ShadowQuality as f, FarShadowInfo as g, createGpuFrameTimer as h, RendererBackend as i, SkyReflectionState as j, starDirection as k, nextWorldOrigin as l, GpuFrameTimerOptions as m, CameraPose as n, RendererOptions as o, GpuFrameTimer as p, Renderer as r, ThreeRenderer as s, AutoWorldOriginOptions as t, orthoFrustum as u, WeatherVisualOptions as v, defaultSkyPalette as w, SkyStar as x, CloudQuality as y, SceneAdmission as z };
@@ -0,0 +1,218 @@
1
+ import { resolveSnowCover } from "@bendyline/molen-schema";
2
+ import * as THREE from "three";
3
+ //#region src/weather/snow-cover.ts
4
+ /** Linear albedo of lying snow: bright, a little blue, short of white so sunlit slopes keep form. */
5
+ const SNOW_COVER_COLOR = [
6
+ .78,
7
+ .82,
8
+ .87
9
+ ];
10
+ /**
11
+ * Meters after which the snow line's wander repeats. A host passes world coordinates reduced by
12
+ * this period, which keeps float precision and survives floating-origin rebases; the terrain
13
+ * ground's own noise shares the period.
14
+ */
15
+ const SNOW_COVER_PERIOD = 4096;
16
+ /** Noise cells in meters that move the line, and how far each moves it, as shares of the band. */
17
+ const WANDER = [[
18
+ 512,
19
+ .5,
20
+ 401,
21
+ 59
22
+ ], [
23
+ 64,
24
+ .2,
25
+ 83,
26
+ 131
27
+ ]];
28
+ /** The most the wander can add to or take from a height, as a share of the transition band. */
29
+ const WANDER_REACH = WANDER.reduce((sum, [, share]) => sum + share / 2, 0);
30
+ /**
31
+ * World-normal up component across which a marked surface goes from bare to dusted, unless the
32
+ * material names its own range: walls stay clean and anything flatter than about 40 degrees is
33
+ * covered.
34
+ */
35
+ const SNOW_COVER_FACING = [.3, .75];
36
+ /** Most of a dusted surface's own color that snow hides. */
37
+ const SURFACE_COVER = .94;
38
+ function createSnowCoverUniforms() {
39
+ return {
40
+ molenSnowCover: { value: new THREE.Vector4(0, 150, 0, 0) },
41
+ molenSnowOrigin: { value: new THREE.Vector2() }
42
+ };
43
+ }
44
+ const wrap = (value) => (value % SNOW_COVER_PERIOD + SNOW_COVER_PERIOD) % SNOW_COVER_PERIOD;
45
+ /**
46
+ * Write a snow cover (or none) into a `vec4` uniform value: the line in rendered Y, which is
47
+ * world Y less the floating origin's, the transition band, and an amount of 1.
48
+ */
49
+ function writeSnowCoverValue(target, cover, worldOriginY = 0) {
50
+ if (cover === void 0) {
51
+ target.z = 0;
52
+ return;
53
+ }
54
+ const { level, transition } = resolveSnowCover(cover);
55
+ target.set(level - worldOriginY, transition, 1, 0);
56
+ }
57
+ /** Point a renderer's snow uniforms at a cover (or none) for a world origin. */
58
+ function writeSnowCover(uniforms, cover, worldOrigin) {
59
+ writeSnowCoverValue(uniforms.molenSnowCover.value, cover, worldOrigin[1]);
60
+ uniforms.molenSnowOrigin.value.set(wrap(worldOrigin[0]), wrap(worldOrigin[2]));
61
+ }
62
+ /**
63
+ * GLSL for the shared snow line. `molenSnowLine(p, height, snow)` is the share of ground covered
64
+ * at world XZ `p` (reduced by the period) and rendered height, for a `snow` uniform as
65
+ * `writeSnowCoverValue` fills it. Guarded, so a ground material that includes it and also takes
66
+ * the renderer's hook defines it once.
67
+ */
68
+ const SNOW_COVER_GLSL = `
69
+ #ifndef MOLEN_SNOW_LINE
70
+ #define MOLEN_SNOW_LINE
71
+ float molenSnowHash(vec2 c, float period) {
72
+ c -= period * floor(c / period);
73
+ vec3 p3 = fract(vec3(c.xyx) * 0.1031);
74
+ p3 += dot(p3, p3.yzx + 33.33);
75
+ return fract((p3.x + p3.y) * p3.z);
76
+ }
77
+ float molenSnowNoise(vec2 p, float cell) {
78
+ float period = ${SNOW_COVER_PERIOD.toFixed(1)} / cell;
79
+ vec2 q = p / cell;
80
+ vec2 i = floor(q);
81
+ vec2 f = q - i;
82
+ vec2 u = f * f * (3.0 - 2.0 * f);
83
+ return mix(
84
+ mix(molenSnowHash(i, period), molenSnowHash(i + vec2(1.0, 0.0), period), u.x),
85
+ mix(molenSnowHash(i + vec2(0.0, 1.0), period), molenSnowHash(i + vec2(1.0, 1.0), period), u.x),
86
+ u.y);
87
+ }
88
+ float molenSnowLine(vec2 p, float height, vec4 snow) {
89
+ if (snow.z <= 0.0) return 0.0;
90
+ float above = (height - snow.x) / max(snow.y, 1.0);
91
+ // Out of the wander's reach the answer is known: most of a scene skips the noise.
92
+ if (above <= -${WANDER_REACH.toFixed(2)}) return 0.0;
93
+ if (above >= ${(1 + WANDER_REACH).toFixed(2)}) return snow.z;
94
+ float wander = ${WANDER.map(([cell, share, x, z]) => `(molenSnowNoise(p + vec2(${x.toFixed(1)}, ${z.toFixed(1)}), ${cell.toFixed(1)}) - 0.5) * ${share.toFixed(2)}`).join(" +\n ")};
95
+ float t = clamp(above + wander, 0.0, 1.0);
96
+ return t * t * (3.0 - 2.0 * t) * snow.z;
97
+ }
98
+ #endif
99
+ `;
100
+ const SURFACE_GLSL = `
101
+ uniform vec4 molenSnowCover;
102
+ uniform vec2 molenSnowOrigin;
103
+ // This material's amount (0: takes no snow) and the facing range it dusts over.
104
+ uniform vec3 molenSnowSurface;
105
+ ${SNOW_COVER_GLSL}
106
+ `;
107
+ const SURFACE_INCLUDE = "#include <normal_fragment_maps>";
108
+ /** In every lit fragment shader, and only those: they declare the view position read below. */
109
+ const LIT_INCLUDE = "#include <lights_pars_begin>";
110
+ /**
111
+ * Add the snow dusting to a WebGL shader built from three's lit chunks; false if it has none.
112
+ * Every such shader carries the code, so materials keep sharing programs; `material` only sets
113
+ * its own amount and facing range (an unmarked material's amount is 0).
114
+ */
115
+ function injectSnowCover(shader, uniforms, material) {
116
+ if (!shader.fragmentShader.includes(SURFACE_INCLUDE) || !shader.fragmentShader.includes(LIT_INCLUDE)) return false;
117
+ shader.fragmentShader = `${SURFACE_GLSL}\n${shader.fragmentShader.replace(SURFACE_INCLUDE, `${SURFACE_INCLUDE}
118
+ if ( molenSnowSurface.x > 0.0 && molenSnowCover.z > 0.0 ) {
119
+ vec3 molenSnowWorld = cameraPosition + ( vec4( - vViewPosition, 0.0 ) * viewMatrix ).xyz;
120
+ float molenSnowUp = ( vec4( normal, 0.0 ) * viewMatrix ).y;
121
+ float molenSnow = molenSnowLine( molenSnowWorld.xz + molenSnowOrigin, molenSnowWorld.y, molenSnowCover ) *
122
+ smoothstep( molenSnowSurface.y, molenSnowSurface.z, molenSnowUp ) * ${SURFACE_COVER.toFixed(2)} * molenSnowSurface.x;
123
+ diffuseColor.rgb = mix( diffuseColor.rgb, vec3( ${SNOW_COVER_COLOR.map((c) => c.toFixed(3)).join(", ")} ), molenSnow );
124
+ #ifdef STANDARD
125
+ roughnessFactor = mix( roughnessFactor, 0.9, molenSnow );
126
+ metalnessFactor *= 1.0 - molenSnow;
127
+ #endif
128
+ }`)}`;
129
+ const [from, to] = snowCoverFacing(material);
130
+ Object.assign(shader.uniforms, uniforms, { molenSnowSurface: { value: new THREE.Vector3(snowCoverAmount(material), from, to) } });
131
+ return true;
132
+ }
133
+ const keyed = /* @__PURE__ */ new WeakSet();
134
+ /**
135
+ * How much of the weather's lying snow settles on a material: 0 unless `enableSnowCover` marked
136
+ * it (or the material it was cloned from).
137
+ */
138
+ function snowCoverAmount(material) {
139
+ const amount = material.userData.molenSnowCover;
140
+ return typeof amount === "number" && amount > 0 ? Math.min(1, amount) : 0;
141
+ }
142
+ /** The up-component range over which a material's surfaces go from bare to dusted. */
143
+ function snowCoverFacing(material) {
144
+ const facing = material.userData.molenSnowFacing;
145
+ return Array.isArray(facing) && facing.length === 2 ? facing : SNOW_COVER_FACING;
146
+ }
147
+ /**
148
+ * Let lying snow settle on a lit material: above the weather's snow line its up-facing surfaces
149
+ * whiten, on either backend, with no further calls. For roofs, ledges, crowns, props and parked
150
+ * cars; not for the ground (its materials draw their own snow), water, or anything that moves
151
+ * through the weather, such as an aircraft or a person. `amount` (0–1, default 1) thins the
152
+ * dusting: foliage holds less than a roof. `facing` is the range of the surface normal's up
153
+ * component over which it goes from bare to dusted (default `SNOW_COVER_FACING`: flat roofs
154
+ * white, walls clean); a lower range lets snow cling to steep sides, as on a conifer. A `clone()`
155
+ * stays marked, as does a `copy()` target marked again afterwards. Call it before the material
156
+ * first draws.
157
+ */
158
+ function enableSnowCover(material, amount = 1, facing) {
159
+ if (!(amount > 0 && amount <= 1)) throw new RangeError("Snow cover amount must be in (0, 1]");
160
+ if (facing !== void 0 && !(facing[0] < facing[1])) throw new RangeError("Snow cover facing must be an ascending range");
161
+ const data = material.userData;
162
+ data.molenSnowCover = amount;
163
+ if (facing === void 0) delete data.molenSnowFacing;
164
+ else data.molenSnowFacing = [...facing];
165
+ if (!keyed.has(material)) {
166
+ keyed.add(material);
167
+ const previous = material.customProgramCacheKey.bind(material);
168
+ material.customProgramCacheKey = () => `${previous()}|molen-snow-cover:${snowCoverAmount(material)}:${snowCoverFacing(material).join(",")}`;
169
+ }
170
+ material.needsUpdate = true;
171
+ return material;
172
+ }
173
+ /** `molenSnowLine` as TSL nodes; `tsl` is the caller's lazily loaded `three/tsl`. */
174
+ function snowLineNode(tsl, p, height, snow) {
175
+ const { clamp, dot, float, floor, Fn, fract, If, max, mix, vec2, vec3 } = tsl;
176
+ const hash = (cell, period) => {
177
+ const c = cell.sub(floor(cell.div(period)).mul(period));
178
+ const a = fract(vec3(c.x, c.y, c.x).mul(.1031));
179
+ const b = a.add(dot(a, a.yzx.add(33.33)));
180
+ return fract(b.x.add(b.y).mul(b.z));
181
+ };
182
+ const noise = (at, size) => {
183
+ const period = SNOW_COVER_PERIOD / size;
184
+ const q = at.div(size);
185
+ const i = floor(q).toVar();
186
+ const f = q.sub(i);
187
+ const u = f.mul(f).mul(f.mul(-2).add(3)).toVar();
188
+ return mix(mix(hash(i, period), hash(i.add(vec2(1, 0)), period), u.x), mix(hash(i.add(vec2(0, 1)), period), hash(i.add(vec2(1, 1)), period), u.x), u.y);
189
+ };
190
+ return Fn(([at, rendered, lying]) => {
191
+ const result = float(0).toVar();
192
+ If(lying.z.greaterThan(0), () => {
193
+ const above = rendered.sub(lying.x).div(max(lying.y, 1)).toVar();
194
+ If(above.greaterThanEqual(1 + WANDER_REACH), () => {
195
+ result.assign(lying.z);
196
+ }).ElseIf(above.greaterThan(-WANDER_REACH), () => {
197
+ let wander;
198
+ for (const [cell, share, x, z] of WANDER) {
199
+ const term = noise(at.add(vec2(x, z)), cell).sub(.5).mul(share);
200
+ wander = wander === void 0 ? term : wander.add(term);
201
+ }
202
+ const t = clamp(above.add(wander), 0, 1).toVar();
203
+ result.assign(t.mul(t).mul(t.mul(-2).add(3)).mul(lying.z));
204
+ });
205
+ });
206
+ return result;
207
+ })(p, height, snow);
208
+ }
209
+ /** The dusting on a marked surface, from its world position and normal, as TSL nodes. */
210
+ function snowSurfaceNode(tsl, p, height, up, snow, amount, facing = SNOW_COVER_FACING) {
211
+ const { smoothstep, vec3 } = tsl;
212
+ return {
213
+ cover: snowLineNode(tsl, p, height, snow).mul(smoothstep(facing[0], facing[1], up)).mul(SURFACE_COVER * amount),
214
+ color: vec3(...SNOW_COVER_COLOR)
215
+ };
216
+ }
217
+ //#endregion
218
+ export { createSnowCoverUniforms as a, snowCoverAmount as c, snowSurfaceNode as d, writeSnowCover as f, SNOW_COVER_PERIOD as i, snowCoverFacing as l, SNOW_COVER_FACING as n, enableSnowCover as o, writeSnowCoverValue as p, SNOW_COVER_GLSL as r, injectSnowCover as s, SNOW_COVER_COLOR as t, snowLineNode as u };
@@ -0,0 +1,67 @@
1
+ import { SnowCoverData } from "@bendyline/molen-schema";
2
+ import * as THREE from "three";
3
+ import { Node } from "three/webgpu";
4
+
5
+ //#region src/weather/snow-cover.d.ts
6
+ /** Linear albedo of lying snow: bright, a little blue, short of white so sunlit slopes keep form. */
7
+ declare const SNOW_COVER_COLOR: readonly [number, number, number];
8
+ /**
9
+ * Meters after which the snow line's wander repeats. A host passes world coordinates reduced by
10
+ * this period, which keeps float precision and survives floating-origin rebases; the terrain
11
+ * ground's own noise shares the period.
12
+ */
13
+ declare const SNOW_COVER_PERIOD = 4096;
14
+ /**
15
+ * World-normal up component across which a marked surface goes from bare to dusted, unless the
16
+ * material names its own range: walls stay clean and anything flatter than about 40 degrees is
17
+ * covered.
18
+ */
19
+ declare const SNOW_COVER_FACING: readonly [number, number];
20
+ interface SnowCoverUniforms {
21
+ /** Snow line (rendered Y), transition band in meters, amount (0 is none), unused. */
22
+ molenSnowCover: {
23
+ value: THREE.Vector4;
24
+ };
25
+ /** The renderer's world origin X and Z, wrapped to `SNOW_COVER_PERIOD`. */
26
+ molenSnowOrigin: {
27
+ value: THREE.Vector2;
28
+ };
29
+ }
30
+ /**
31
+ * Write a snow cover (or none) into a `vec4` uniform value: the line in rendered Y, which is
32
+ * world Y less the floating origin's, the transition band, and an amount of 1.
33
+ */
34
+ declare function writeSnowCoverValue(target: THREE.Vector4, cover: SnowCoverData | undefined, worldOriginY?: number): void;
35
+ /**
36
+ * GLSL for the shared snow line. `molenSnowLine(p, height, snow)` is the share of ground covered
37
+ * at world XZ `p` (reduced by the period) and rendered height, for a `snow` uniform as
38
+ * `writeSnowCoverValue` fills it. Guarded, so a ground material that includes it and also takes
39
+ * the renderer's hook defines it once.
40
+ */
41
+ declare const SNOW_COVER_GLSL: string;
42
+ /**
43
+ * How much of the weather's lying snow settles on a material: 0 unless `enableSnowCover` marked
44
+ * it (or the material it was cloned from).
45
+ */
46
+ declare function snowCoverAmount(material: THREE.Material): number;
47
+ /** The up-component range over which a material's surfaces go from bare to dusted. */
48
+ declare function snowCoverFacing(material: THREE.Material): readonly [number, number];
49
+ /**
50
+ * Let lying snow settle on a lit material: above the weather's snow line its up-facing surfaces
51
+ * whiten, on either backend, with no further calls. For roofs, ledges, crowns, props and parked
52
+ * cars; not for the ground (its materials draw their own snow), water, or anything that moves
53
+ * through the weather, such as an aircraft or a person. `amount` (0–1, default 1) thins the
54
+ * dusting: foliage holds less than a roof. `facing` is the range of the surface normal's up
55
+ * component over which it goes from bare to dusted (default `SNOW_COVER_FACING`: flat roofs
56
+ * white, walls clean); a lower range lets snow cling to steep sides, as on a conifer. A `clone()`
57
+ * stays marked, as does a `copy()` target marked again afterwards. Call it before the material
58
+ * first draws.
59
+ */
60
+ declare function enableSnowCover<T extends THREE.Material>(material: T, amount?: number, facing?: readonly [number, number]): T;
61
+ type Tsl = typeof import("three/tsl");
62
+ type F = Node<"float">;
63
+ type V2 = Node<"vec2">;
64
+ /** `molenSnowLine` as TSL nodes; `tsl` is the caller's lazily loaded `three/tsl`. */
65
+ declare function snowLineNode(tsl: Tsl, p: V2, height: F, snow: Node<"vec4">): F;
66
+ //#endregion
67
+ export { SnowCoverUniforms as a, snowCoverFacing as c, SNOW_COVER_PERIOD as i, snowLineNode as l, SNOW_COVER_FACING as n, enableSnowCover as o, SNOW_COVER_GLSL as r, snowCoverAmount as s, SNOW_COVER_COLOR as t, writeSnowCoverValue as u };
@@ -0,0 +1,2 @@
1
+ import { c as snowCoverFacing, i as SNOW_COVER_PERIOD, l as snowLineNode, n as SNOW_COVER_FACING, o as enableSnowCover, r as SNOW_COVER_GLSL, s as snowCoverAmount, t as SNOW_COVER_COLOR, u as writeSnowCoverValue } from "./snow-cover-tEG1uO98.mjs";
2
+ export { SNOW_COVER_COLOR, SNOW_COVER_FACING, SNOW_COVER_GLSL, SNOW_COVER_PERIOD, enableSnowCover, snowCoverAmount, snowCoverFacing, snowLineNode, writeSnowCoverValue };
package/dist/snow.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import { c as snowCoverAmount, i as SNOW_COVER_PERIOD, l as snowCoverFacing, n as SNOW_COVER_FACING, o as enableSnowCover, p as writeSnowCoverValue, r as SNOW_COVER_GLSL, t as SNOW_COVER_COLOR, u as snowLineNode } from "./snow-cover-Bh0woabf.mjs";
2
+ export { SNOW_COVER_COLOR, SNOW_COVER_FACING, SNOW_COVER_GLSL, SNOW_COVER_PERIOD, enableSnowCover, snowCoverAmount, snowCoverFacing, snowLineNode, writeSnowCoverValue };
@@ -439,6 +439,27 @@ function aircraftObstacleContacts(probes, scene) {
439
439
  return contacts;
440
440
  }
441
441
  //#endregion
442
+ //#region src/vehicle-glass.ts
443
+ /**
444
+ * Draw a model's double-sided transparent materials (canopies, windscreens, instrument covers) in
445
+ * one pass. Three otherwise draws each such mesh twice, back faces then front, and flags the
446
+ * material for a new program before each pass: two program lookups and two full uniform uploads
447
+ * per pane, every frame. A P-51 on an apron of parked aircraft switched 52 programs a frame that
448
+ * way. The panes are thin and blended without depth writes (glTF BLEND), so only the order in
449
+ * which a pane's two faces blend can change.
450
+ */
451
+ function drawGlassInOnePass(mesh) {
452
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) if (material.transparent && material.side === THREE.DoubleSide) material.forceSinglePass = true;
453
+ }
454
+ /**
455
+ * Whether a mesh is see-through glass (every material blended at less than full opacity), which
456
+ * must not cast sun shadows: three's shadow pass draws a pane as solid, so a windscreen put its
457
+ * whole cockpit in shade. Lettering decals blend too, but at full opacity, and still cast.
458
+ */
459
+ function isGlass(mesh) {
460
+ return (Array.isArray(mesh.material) ? mesh.material : [mesh.material]).every((material) => material.transparent && material.opacity < 1);
461
+ }
462
+ //#endregion
442
463
  //#region src/vehicle-interior.ts
443
464
  /** Vehicle convenience wrapper around the platform's model-signal binding system. */
444
465
  function createVehicleInteriorVisual(model, spec) {
@@ -452,4 +473,4 @@ function createVehicleInteriorVisual(model, spec) {
452
473
  };
453
474
  }
454
475
  //#endregion
455
- export { aircraftObstacleContacts as n, createAircraftDamageVisual as r, createVehicleInteriorVisual as t };
476
+ export { createAircraftDamageVisual as a, aircraftObstacleContacts as i, drawGlassInOnePass as n, isGlass as r, createVehicleInteriorVisual as t };