@flare-engine/postfx 0.4.11 → 0.4.12

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.
package/dist/index.mjs CHANGED
@@ -58,7 +58,12 @@ var EffectChain = class {
58
58
  return true;
59
59
  }
60
60
  get(id) {
61
- return this._effects.find((e) => e.id === id) ?? null;
61
+ const effects = this._effects;
62
+ for (let i = 0; i < effects.length; i++) {
63
+ const e = effects[i];
64
+ if (e !== void 0 && e.id === id) return e;
65
+ }
66
+ return null;
62
67
  }
63
68
  setEnabled(id, enabled) {
64
69
  const e = this.get(id);
@@ -205,26 +210,18 @@ half4 main(float2 coord) {
205
210
  return half4(result, scene.a);
206
211
  }
207
212
  `;
208
- const WEATHER_RAIN_SKSL = `
209
- uniform shader src;
210
- uniform float2 resolution;
211
- uniform float intensity;
212
- uniform float direction;
213
- uniform float scrollOffsetY;
214
- uniform float3 color;
215
- uniform float density;
216
- uniform float streakLen;
217
-
218
- // scrollOffsetY is a normalized fall phase in [0,1). Increase it each tick
219
- // by (speed * dt / screenHeight) and wrap with fract() on the game side.
220
-
221
- float hash(float2 p) { return fract(sin(dot(p, float2(127.1, 311.7))) * 43758.5453); }
222
-
223
- half4 main(float2 coord) {
224
- half4 scene = src.eval(coord);
225
- float2 uv = coord / resolution;
226
- float aspect = resolution.x / resolution.y;
227
- float rad = direction * 3.14159 / 180.0;
213
+ const HASH_FN_SKSL = "float hash(float2 p) { return fract(sin(dot(p, float2(127.1, 311.7))) * 43758.5453); }";
214
+ const CLOUD_NOISE_FNS_SKSL = `float vnoise(float2 p) {
215
+ float2 i = floor(p); float2 f = fract(p);
216
+ float2 u = f * f * (3.0 - 2.0 * f);
217
+ return mix(mix(hash(i), hash(i + float2(1.0, 0.0)), u.x),
218
+ mix(hash(i + float2(0.0, 1.0)), hash(i + float2(1.0, 1.0)), u.x), u.y);
219
+ }
220
+ float fbm(float2 p) { return vnoise(p) * 0.6 + vnoise(p * 2.3) * 0.4; }`;
221
+ /** Indents a 2-space fragment to the depth of a composite weather branch. */
222
+ const nest = (fragment) => fragment.replace(/^(?=.)/gm, " ");
223
+ const rainBody = (n) => ` float aspect = resolution.x / resolution.y;
224
+ float rad = ${n.direction} * 3.14159 / 180.0;
228
225
  float sinA = sin(rad);
229
226
  float cosA = cos(rad);
230
227
  // aspect-corrected rotated axes: fallAxis along drop travel, perpAxis across
@@ -240,12 +237,12 @@ half4 main(float2 coord) {
240
237
  float brightness = 1.0;
241
238
  float cid = floor(perpAxis * COLS);
242
239
  float colHash = hash(float2(cid, 0.0));
243
- if (colHash < density) {
240
+ if (colHash < ${n.density}) {
244
241
  float phase = hash(float2(cid, 1.0));
245
242
  float colBright = brightness * (0.6 + hash(float2(cid, 2.0)) * 0.4);
246
243
  float center = 0.5 + (hash(float2(cid, 3.0)) - 0.5) * 0.4;
247
- float seg = fract(fallAxis * tiling - scrollOffsetY + phase);
248
- float coverageY = smoothstep(streakLen, 0.0, seg);
244
+ float seg = fract(fallAxis * tiling - ${n.scrollY} + phase);
245
+ float coverageY = smoothstep(${n.streakLen}, 0.0, seg);
249
246
  float width = 0.04;
250
247
  float xMask = smoothstep(width, 0.0, abs(fract(perpAxis * COLS) - center));
251
248
  coverage = max(coverage, coverageY * xMask * colBright);
@@ -259,19 +256,87 @@ half4 main(float2 coord) {
259
256
  float brightness = 0.5;
260
257
  float cid = floor(perpAxis * COLS);
261
258
  float colHash = hash(float2(cid, 10.0));
262
- if (colHash < density) {
259
+ if (colHash < ${n.density}) {
263
260
  float phase = hash(float2(cid, 11.0));
264
261
  float colBright = brightness * (0.6 + hash(float2(cid, 12.0)) * 0.4);
265
262
  float center = 0.5 + (hash(float2(cid, 13.0)) - 0.5) * 0.4;
266
- float seg = fract(fallAxis * tiling - scrollOffsetY + phase);
267
- float coverageY = smoothstep(streakLen, 0.0, seg);
263
+ float seg = fract(fallAxis * tiling - ${n.scrollY} + phase);
264
+ float coverageY = smoothstep(${n.streakLen}, 0.0, seg);
268
265
  float width = 0.03;
269
266
  float xMask = smoothstep(width, 0.0, abs(fract(perpAxis * COLS) - center));
270
267
  coverage = max(coverage, coverageY * xMask * colBright);
271
268
  }
272
- }
269
+ }`;
270
+ const rainBlend = (rgb, color, intensity) => `mix(${rgb}, half3(${color}), half(coverage * ${intensity}))`;
271
+ const sunShaftBody = (direction, intensity) => ` float rad = ${direction} * 3.14159 / 180.0;
272
+ float2 src_point = float2(0.5 + sin(rad) * 0.3, -0.05);
273
+ float2 toFrag = uv - src_point;
274
+ float angle = atan(toFrag.x, toFrag.y);
275
+ float shafts = 0.5 + 0.5 * sin(angle * 8.0);
276
+ float dist = length(toFrag);
277
+ float falloff = 1.0 - smoothstep(0.0, 1.2, dist);
278
+ half shaft = half(shafts * falloff * ${intensity} * 0.35);`;
279
+ const sunShaftBlend = (rgb) => `${rgb} + half3(shaft * 0.95, shaft * 0.9, shaft * 0.6)`;
280
+ const darkVignetteBody = (centered, uvExpr, intensity) => ` float2 ${centered} = ${uvExpr} - 0.5;
281
+ float d = length(${centered});
282
+ float v = smoothstep(0.3, 0.85, d);
283
+ half dark = half(v * ${intensity});`;
284
+ const darkVignetteBlend = (rgb) => `${rgb} * (1.0 - dark)`;
285
+ const cloudsBody = (n) => ` // ${n.direction}=0 → pure horizontal scroll (cos=1, sin=0), matching the pre-${n.direction} default.
286
+ float rad = ${n.direction} * 3.14159 / 180.0;
287
+ float2 scrollDir = float2(cos(rad), sin(rad));
288
+ float2 cloudUv = float2(
289
+ uv.x + ${n.scrollX} * scrollDir.x / resolution.x,
290
+ uv.y * 0.5 + ${n.scrollX} * scrollDir.y / resolution.y
291
+ );
292
+ float cloud = fbm(cloudUv * 3.0);
293
+ float heightFade = smoothstep(0.6, 0.2, uv.y);
294
+ float alpha = cloud * heightFade * ${n.intensity} * 0.5;
295
+ half3 cloudColor = half3(0.9, 0.9, 0.95);`;
296
+ const cloudsBlend = (rgb) => `mix(${rgb}, cloudColor, half(alpha))`;
297
+ const RAIN_STANDALONE = {
298
+ direction: "direction",
299
+ scrollY: "scrollOffsetY",
300
+ density: "density",
301
+ streakLen: "streakLen"
302
+ };
303
+ const RAIN_COMPOSITE = {
304
+ direction: "weatherDirection",
305
+ scrollY: "weatherScrollY",
306
+ density: "weatherDensity",
307
+ streakLen: "weatherStreakLen"
308
+ };
309
+ const CLOUDS_STANDALONE = {
310
+ direction: "direction",
311
+ scrollX: "scrollOffsetX",
312
+ intensity: "intensity"
313
+ };
314
+ const CLOUDS_COMPOSITE = {
315
+ direction: "weatherDirection",
316
+ scrollX: "weatherScrollX",
317
+ intensity: "weatherIntensity"
318
+ };
319
+ const WEATHER_RAIN_SKSL = `
320
+ uniform shader src;
321
+ uniform float2 resolution;
322
+ uniform float intensity;
323
+ uniform float direction;
324
+ uniform float scrollOffsetY;
325
+ uniform float3 color;
326
+ uniform float density;
327
+ uniform float streakLen;
328
+
329
+ // scrollOffsetY is a normalized fall phase in [0,1). Increase it each tick
330
+ // by (speed * dt / screenHeight) and wrap with fract() on the game side.
331
+
332
+ ${HASH_FN_SKSL}
333
+
334
+ half4 main(float2 coord) {
335
+ half4 scene = src.eval(coord);
336
+ float2 uv = coord / resolution;
337
+ ${rainBody(RAIN_STANDALONE)}
273
338
 
274
- half3 mixed = mix(scene.rgb, half3(color), half(coverage * intensity));
339
+ half3 mixed = ${rainBlend("scene.rgb", "color", "intensity")};
275
340
  return half4(mixed, scene.a);
276
341
  }
277
342
  `;
@@ -284,15 +349,8 @@ uniform float direction;
284
349
  half4 main(float2 coord) {
285
350
  half4 scene = src.eval(coord);
286
351
  float2 uv = coord / resolution;
287
- float rad = direction * 3.14159 / 180.0;
288
- float2 src_point = float2(0.5 + sin(rad) * 0.3, -0.05);
289
- float2 toFrag = uv - src_point;
290
- float angle = atan(toFrag.x, toFrag.y);
291
- float shafts = 0.5 + 0.5 * sin(angle * 8.0);
292
- float dist = length(toFrag);
293
- float falloff = 1.0 - smoothstep(0.0, 1.2, dist);
294
- half shaft = half(shafts * falloff * intensity * 0.35);
295
- return half4(scene.rgb + half3(shaft * 0.95, shaft * 0.9, shaft * 0.6), scene.a);
352
+ ${sunShaftBody("direction", "intensity")}
353
+ return half4(${sunShaftBlend("scene.rgb")}, scene.a);
296
354
  }
297
355
  `;
298
356
  const WEATHER_DARK_VIGNETTE_SKSL = `
@@ -302,11 +360,8 @@ uniform float intensity;
302
360
 
303
361
  half4 main(float2 coord) {
304
362
  half4 scene = src.eval(coord);
305
- float2 uv = coord / resolution - 0.5;
306
- float d = length(uv);
307
- float v = smoothstep(0.3, 0.85, d);
308
- half dark = half(v * intensity);
309
- return half4(scene.rgb * (1.0 - dark), scene.a);
363
+ ${darkVignetteBody("uv", "coord / resolution", "intensity")}
364
+ return half4(${darkVignetteBlend("scene.rgb")}, scene.a);
310
365
  }
311
366
  `;
312
367
  const WEATHER_CLOUDS_SKSL = `
@@ -316,30 +371,14 @@ uniform float intensity;
316
371
  uniform float scrollOffsetX;
317
372
  uniform float direction;
318
373
 
319
- float hash(float2 p) { return fract(sin(dot(p, float2(127.1, 311.7))) * 43758.5453); }
320
- float vnoise(float2 p) {
321
- float2 i = floor(p); float2 f = fract(p);
322
- float2 u = f * f * (3.0 - 2.0 * f);
323
- return mix(mix(hash(i), hash(i + float2(1.0, 0.0)), u.x),
324
- mix(hash(i + float2(0.0, 1.0)), hash(i + float2(1.0, 1.0)), u.x), u.y);
325
- }
326
- float fbm(float2 p) { return vnoise(p) * 0.6 + vnoise(p * 2.3) * 0.4; }
374
+ ${HASH_FN_SKSL}
375
+ ${CLOUD_NOISE_FNS_SKSL}
327
376
 
328
377
  half4 main(float2 coord) {
329
378
  half4 scene = src.eval(coord);
330
379
  float2 uv = coord / resolution;
331
- // direction=0 → pure horizontal scroll (cos=1, sin=0), matching the pre-direction default.
332
- float rad = direction * 3.14159 / 180.0;
333
- float2 scrollDir = float2(cos(rad), sin(rad));
334
- float2 cloudUv = float2(
335
- uv.x + scrollOffsetX * scrollDir.x / resolution.x,
336
- uv.y * 0.5 + scrollOffsetX * scrollDir.y / resolution.y
337
- );
338
- float cloud = fbm(cloudUv * 3.0);
339
- float heightFade = smoothstep(0.6, 0.2, uv.y);
340
- float alpha = cloud * heightFade * intensity * 0.5;
341
- half3 cloudColor = half3(0.9, 0.9, 0.95);
342
- return half4(mix(scene.rgb, cloudColor, half(alpha)), scene.a);
380
+ ${cloudsBody(CLOUDS_STANDALONE)}
381
+ return half4(${cloudsBlend("scene.rgb")}, scene.a);
343
382
  }
344
383
  `;
345
384
  const COMPOSITE_SKSL = `
@@ -365,14 +404,8 @@ uniform float3 weatherColor;
365
404
  uniform float weatherDensity;
366
405
  uniform float weatherStreakLen;
367
406
 
368
- float hash(float2 p) { return fract(sin(dot(p, float2(127.1, 311.7))) * 43758.5453); }
369
- float vnoise(float2 p) {
370
- float2 i = floor(p); float2 f = fract(p);
371
- float2 u = f * f * (3.0 - 2.0 * f);
372
- return mix(mix(hash(i), hash(i + float2(1.0, 0.0)), u.x),
373
- mix(hash(i + float2(0.0, 1.0)), hash(i + float2(1.0, 1.0)), u.x), u.y);
374
- }
375
- float fbm(float2 p) { return vnoise(p) * 0.6 + vnoise(p * 2.3) * 0.4; }
407
+ ${HASH_FN_SKSL}
408
+ ${CLOUD_NOISE_FNS_SKSL}
376
409
 
377
410
  half4 main(float2 coord) {
378
411
  // --- chromatic aberration ---
@@ -411,85 +444,21 @@ half4 main(float2 coord) {
411
444
  if (wk >= 0.5 && wk < 1.5) {
412
445
  // rain — falling sawtooth streaks, 2 parallax layers, alpha-over blend
413
446
  // weatherScrollY is normalized [0,1) fall phase (increment speed*dt/screenH each tick)
414
- float rad = weatherDirection * 3.14159 / 180.0;
415
- float sinA = sin(rad);
416
- float cosA = cos(rad);
417
- float aspect = resolution.x / resolution.y;
418
- float perpAxis = (cosA * uv.x - sinA * uv.y) * aspect;
419
- float fallAxis = sinA * uv.x + cosA * uv.y;
420
-
421
- float coverage = 0.0;
422
-
423
- // near layer — COLS=45, tiling=6, brightness=1.0
424
- {
425
- float COLS = 45.0;
426
- float tiling = 6.0;
427
- float brightness = 1.0;
428
- float cid = floor(perpAxis * COLS);
429
- float colHash = hash(float2(cid, 0.0));
430
- if (colHash < weatherDensity) {
431
- float phase = hash(float2(cid, 1.0));
432
- float colBright = brightness * (0.6 + hash(float2(cid, 2.0)) * 0.4);
433
- float center = 0.5 + (hash(float2(cid, 3.0)) - 0.5) * 0.4;
434
- float seg = fract(fallAxis * tiling - weatherScrollY + phase);
435
- float coverageY = smoothstep(weatherStreakLen, 0.0, seg);
436
- float width = 0.04;
437
- float xMask = smoothstep(width, 0.0, abs(fract(perpAxis * COLS) - center));
438
- coverage = max(coverage, coverageY * xMask * colBright);
439
- }
440
- }
447
+ ${nest(rainBody(RAIN_COMPOSITE))}
441
448
 
442
- // far layer — COLS=85, tiling=11, brightness=0.5
443
- {
444
- float COLS = 85.0;
445
- float tiling = 11.0;
446
- float brightness = 0.5;
447
- float cid = floor(perpAxis * COLS);
448
- float colHash = hash(float2(cid, 10.0));
449
- if (colHash < weatherDensity) {
450
- float phase = hash(float2(cid, 11.0));
451
- float colBright = brightness * (0.6 + hash(float2(cid, 12.0)) * 0.4);
452
- float center = 0.5 + (hash(float2(cid, 13.0)) - 0.5) * 0.4;
453
- float seg = fract(fallAxis * tiling - weatherScrollY + phase);
454
- float coverageY = smoothstep(weatherStreakLen, 0.0, seg);
455
- float width = 0.03;
456
- float xMask = smoothstep(width, 0.0, abs(fract(perpAxis * COLS) - center));
457
- coverage = max(coverage, coverageY * xMask * colBright);
458
- }
459
- }
460
-
461
- c = mix(c, half3(weatherColor), half(coverage * weatherIntensity));
449
+ c = ${rainBlend("c", "weatherColor", "weatherIntensity")};
462
450
  } else if (wk >= 1.5 && wk < 2.5) {
463
451
  // sun-shaft
464
- float rad = weatherDirection * 3.14159 / 180.0;
465
- float2 srcPoint = float2(0.5 + sin(rad) * 0.3, -0.05);
466
- float2 toFrag = uv - srcPoint;
467
- float angle = atan(toFrag.x, toFrag.y);
468
- float shafts = 0.5 + 0.5 * sin(angle * 8.0);
469
- float dist = length(toFrag);
470
- float falloff = 1.0 - smoothstep(0.0, 1.2, dist);
471
- half shaft = half(shafts * falloff * weatherIntensity * 0.35);
472
- c = c + half3(shaft * 0.95, shaft * 0.9, shaft * 0.6);
452
+ ${nest(sunShaftBody("weatherDirection", "weatherIntensity"))}
453
+ c = ${sunShaftBlend("c")};
473
454
  } else if (wk >= 2.5 && wk < 3.5) {
474
455
  // dark-vignette
475
- float2 uvShift = uv - 0.5;
476
- float d2 = length(uvShift);
477
- float vd = smoothstep(0.3, 0.85, d2);
478
- half dark = half(vd * weatherIntensity);
479
- c = c * (1.0 - dark);
456
+ ${nest(darkVignetteBody("uvShift", "uv", "weatherIntensity"))}
457
+ c = ${darkVignetteBlend("c")};
480
458
  } else if (wk >= 3.5) {
481
- // clouds — rotated scroll, mirrored from WEATHER_CLOUDS_SKSL (direction=0 → horizontal)
482
- float rad = weatherDirection * 3.14159 / 180.0;
483
- float2 scrollDir = float2(cos(rad), sin(rad));
484
- float2 cloudUv = float2(
485
- uv.x + weatherScrollX * scrollDir.x / resolution.x,
486
- uv.y * 0.5 + weatherScrollX * scrollDir.y / resolution.y
487
- );
488
- float cloud = fbm(cloudUv * 3.0);
489
- float heightFade = smoothstep(0.6, 0.2, uv.y);
490
- float alpha = cloud * heightFade * weatherIntensity * 0.5;
491
- half3 cloudColor = half3(0.9, 0.9, 0.95);
492
- c = mix(c, cloudColor, half(alpha));
459
+ // clouds
460
+ ${nest(cloudsBody(CLOUDS_COMPOSITE))}
461
+ c = ${cloudsBlend("c")};
493
462
  }
494
463
  }
495
464
 
@@ -805,41 +774,46 @@ function createPostFxController(options) {
805
774
  let flashIntensity = 0;
806
775
  const timedRemaining = /* @__PURE__ */ new Map();
807
776
  const timedRestore = /* @__PURE__ */ new Map();
777
+ const expired = [];
778
+ let stepMs = 0;
779
+ const stepTimed = (remaining, id) => {
780
+ const next = remaining - stepMs;
781
+ if (next <= 0) expired.push(id);
782
+ else timedRemaining.set(id, next);
783
+ };
784
+ const flashUniforms = {
785
+ kind: "grayscale-flash",
786
+ intensity: 0
787
+ };
808
788
  return {
809
789
  chain,
810
790
  runtime,
811
791
  triggerHitFlash() {
812
792
  if (!flashCfg) return;
813
793
  flashIntensity = 1;
814
- chain.setUniforms(flashCfg.id, {
815
- kind: "grayscale-flash",
816
- intensity: 1
817
- });
794
+ flashUniforms.intensity = 1;
795
+ chain.setUniforms(flashCfg.id, flashUniforms);
818
796
  },
819
797
  triggerTimed(effectId, decayMs) {
820
798
  const effect = chain.get(effectId);
821
799
  if (!effect) return;
822
- timedRestore.set(effectId, { ...effect.uniforms });
800
+ if (!timedRemaining.has(effectId)) timedRestore.set(effectId, { ...effect.uniforms });
823
801
  chain.setEnabled(effectId, true);
824
802
  timedRemaining.set(effectId, decayMs);
825
803
  },
826
804
  update(dt) {
827
805
  if (flashCfg && flashIntensity > 0) {
828
806
  flashIntensity = Math.max(0, flashIntensity - dt * flashDecay);
829
- chain.setUniforms(flashCfg.id, {
830
- kind: "grayscale-flash",
831
- intensity: flashIntensity
832
- });
807
+ flashUniforms.intensity = flashIntensity;
808
+ chain.setUniforms(flashCfg.id, flashUniforms);
833
809
  }
834
810
  if (timedRemaining.size > 0) {
835
- const dtMs = dt * 1e3;
836
- const expired = [];
837
- for (const [id, rem] of timedRemaining) {
838
- const next = rem - dtMs;
839
- if (next <= 0) expired.push(id);
840
- else timedRemaining.set(id, next);
841
- }
842
- if (expired.length > 0) for (const id of expired) {
811
+ stepMs = dt * 1e3;
812
+ expired.length = 0;
813
+ timedRemaining.forEach(stepTimed);
814
+ for (let i = 0; i < expired.length; i++) {
815
+ const id = expired[i];
816
+ if (id === void 0) continue;
843
817
  const snapshot = timedRestore.get(id);
844
818
  if (snapshot !== void 0) chain.setUniforms(id, snapshot);
845
819
  chain.setEnabled(id, false);