@zaisma/effects 0.0.0-stage → 1.0.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.
Files changed (123) hide show
  1. package/EFFECT-FILES.md +301 -0
  2. package/LICENSE.txt +93 -0
  3. package/README.md +63 -2
  4. package/batches/chunks.js +294 -0
  5. package/batches/marks.js +160 -0
  6. package/batches/ribbons.js +233 -0
  7. package/batches/shared.js +155 -0
  8. package/batches/sprites.js +447 -0
  9. package/batches/warps.js +222 -0
  10. package/budget.js +115 -0
  11. package/debris.js +173 -0
  12. package/emit.js +143 -0
  13. package/flashes.js +113 -0
  14. package/format.js +365 -0
  15. package/index.d.ts +373 -0
  16. package/index.js +1168 -0
  17. package/library/bubbles_rising.json +47 -0
  18. package/library/bullet_impact.json +588 -0
  19. package/library/campfire.json +235 -0
  20. package/library/candle_flame.json +100 -0
  21. package/library/confetti.json +165 -0
  22. package/library/dash_burst.json +73 -0
  23. package/library/debris_glass.json +96 -0
  24. package/library/debris_metal.json +106 -0
  25. package/library/debris_stone.json +106 -0
  26. package/library/debris_wood.json +101 -0
  27. package/library/dust_motes.json +51 -0
  28. package/library/dust_puff.json +49 -0
  29. package/library/electric_sparks.json +175 -0
  30. package/library/embers_drift.json +55 -0
  31. package/library/exhaust_puff.json +62 -0
  32. package/library/explosion_air.json +232 -0
  33. package/library/explosion_large.json +373 -0
  34. package/library/explosion_magic.json +266 -0
  35. package/library/explosion_medium.json +289 -0
  36. package/library/explosion_small.json +289 -0
  37. package/library/explosion_water.json +177 -0
  38. package/library/falling_leaves.json +54 -0
  39. package/library/fire_burst.json +185 -0
  40. package/library/fire_large.json +279 -0
  41. package/library/fireball.json +288 -0
  42. package/library/fireflies.json +51 -0
  43. package/library/firework_burst.json +127 -0
  44. package/library/footstep.json +270 -0
  45. package/library/fountain_spray.json +80 -0
  46. package/library/frost_burst.json +202 -0
  47. package/library/ground_mist.json +50 -0
  48. package/library/ground_slam.json +122 -0
  49. package/library/heat_haze.json +24 -0
  50. package/library/hit_blade.json +150 -0
  51. package/library/hit_block.json +157 -0
  52. package/library/hit_critical.json +293 -0
  53. package/library/hit_heavy.json +211 -0
  54. package/library/hit_light.json +122 -0
  55. package/library/hit_magic.json +114 -0
  56. package/library/impact.json +417 -0
  57. package/library/impact_heavy.json +279 -0
  58. package/library/index.json +464 -0
  59. package/library/jump_puff.json +46 -0
  60. package/library/landing_heavy.json +105 -0
  61. package/library/landing_puff.json +75 -0
  62. package/library/level_up.json +164 -0
  63. package/library/lightning_arc.json +90 -0
  64. package/library/lightning_strike.json +387 -0
  65. package/library/magic_aura.json +127 -0
  66. package/library/magic_bolt.json +107 -0
  67. package/library/magic_bolt_impact.json +181 -0
  68. package/library/magic_cast.json +115 -0
  69. package/library/magic_charge.json +91 -0
  70. package/library/magic_heal.json +123 -0
  71. package/library/magic_shield.json +89 -0
  72. package/library/magic_teleport.json +177 -0
  73. package/library/mark_bullet_hole.json +20 -0
  74. package/library/mark_crack.json +18 -0
  75. package/library/mark_footprint.json +20 -0
  76. package/library/mark_frost.json +18 -0
  77. package/library/mark_puddle.json +18 -0
  78. package/library/mark_scorch.json +18 -0
  79. package/library/mark_slash.json +18 -0
  80. package/library/mark_splat.json +18 -0
  81. package/library/pickup_collect.json +117 -0
  82. package/library/pickup_glow.json +76 -0
  83. package/library/poison_cloud.json +107 -0
  84. package/library/portal.json +328 -0
  85. package/library/shockwave_air.json +71 -0
  86. package/library/shockwave_ground.json +95 -0
  87. package/library/slide_dust.json +48 -0
  88. package/library/smoke_column.json +93 -0
  89. package/library/smoke_puff.json +48 -0
  90. package/library/smoke_screen.json +87 -0
  91. package/library/sparks_burst.json +85 -0
  92. package/library/sparks_fountain.json +93 -0
  93. package/library/sparks_shower.json +89 -0
  94. package/library/splash_large.json +181 -0
  95. package/library/splash_small.json +111 -0
  96. package/library/steam_vent.json +64 -0
  97. package/library/summon_circle.json +494 -0
  98. package/library/swim_splash.json +77 -0
  99. package/library/torch_flame.json +219 -0
  100. package/library/trail_blade.json +39 -0
  101. package/library/trail_blade_fire.json +90 -0
  102. package/library/trail_dash.json +80 -0
  103. package/library/trail_magic.json +94 -0
  104. package/library/trail_projectile.json +31 -0
  105. package/library/trail_smoke.json +125 -0
  106. package/library/warp_pulse.json +22 -0
  107. package/library/warp_ripple.json +23 -0
  108. package/library/water_ripple.json +69 -0
  109. package/library/waterfall_mist.json +95 -0
  110. package/looks.js +162 -0
  111. package/motion.js +145 -0
  112. package/online.d.ts +79 -0
  113. package/online.js +32 -0
  114. package/package.json +69 -4
  115. package/partners.js +255 -0
  116. package/piece.json +415 -0
  117. package/piece.json.js +416 -0
  118. package/server.d.ts +55 -0
  119. package/server.js +26 -0
  120. package/shapes.js +269 -0
  121. package/sync.js +528 -0
  122. package/tables.js +109 -0
  123. package/weather.js +289 -0
@@ -0,0 +1,155 @@
1
+ // What every batch's shader reads: the effects' clock, the wind, the scene's light and the flash lights, the weather's
2
+ // boxes and the shelter map. One set of values, written once a frame, shared by reference by every batch's material.
3
+ import * as THREE from "three";
4
+
5
+ /** How many flash lights reach the particles themselves; the scene's own lights are the flash pool's. */
6
+ export const PARTICLE_FLASH_COUNT = 4;
7
+ /** The weather's layers: rain, snow, mist, and rain landing. */
8
+ export const WRAP_LAYER_COUNT = 4;
9
+ export const LANDING_LAYER_INDEX = 3;
10
+
11
+ export function createSharedUniforms() {
12
+ const vectors = (count, make) => Array.from({ length: count }, make);
13
+ return {
14
+ uElements: { value: null },
15
+ uPlays: { value: null },
16
+ uAtlas: { value: null },
17
+ uTime: { value: 0 },
18
+ // The weather's own clock, which never steps back as uTime does when the effects' clock starts over.
19
+ uWeatherTime: { value: 0 },
20
+ uWind: { value: new THREE.Vector3() },
21
+ uSunDirection: { value: new THREE.Vector3(0.3, 0.8, 0.5).normalize() },
22
+ uSunColor: { value: new THREE.Color(3, 3, 3) },
23
+ uSkyColor: { value: new THREE.Color(1, 1, 1) },
24
+ uGroundColor: { value: new THREE.Color(0.4, 0.4, 0.4) },
25
+ // The sun's shadow map, as three drew it for the scene: what turns a place in the world into a place in it, how
26
+ // much of the sun a shadow takes (0 when there's none to read), and how far apart its four readings are taken.
27
+ uSunShadow: { value: null },
28
+ uSunShadowMatrix: { value: new THREE.Matrix4() },
29
+ uSunShadowShare: { value: 0 },
30
+ uSunShadowSpread: { value: 0 },
31
+ uFlashPlace: { value: vectors(PARTICLE_FLASH_COUNT, () => new THREE.Vector4(0, 0, 0, 1)) },
32
+ uFlashColor: { value: vectors(PARTICLE_FLASH_COUNT, () => new THREE.Color(0, 0, 0)) },
33
+ uWrapCenter: { value: vectors(WRAP_LAYER_COUNT, () => new THREE.Vector3()) },
34
+ uWrapShift: { value: vectors(WRAP_LAYER_COUNT, () => new THREE.Vector3()) },
35
+ uWrapBox: { value: vectors(WRAP_LAYER_COUNT, () => new THREE.Vector3(1, 1, 1)) },
36
+ uWrapVelocity: { value: vectors(WRAP_LAYER_COUNT, () => new THREE.Vector3()) },
37
+ uWrapShare: { value: new Array(WRAP_LAYER_COUNT).fill(0) },
38
+ uShelter: { value: null },
39
+ // The shelter map's centre (x, z), how many metres across it is, and whether it's on (1 or 0).
40
+ uShelterBox: { value: new THREE.Vector4(0, 0, 1, 0) },
41
+ // What the shelter map's heights are counted from, in metres: a height of 0 in it means nothing is there.
42
+ uShelterBaseM: { value: 0 },
43
+ uGroundHeightM: { value: 0 },
44
+ // Pixels a metre covers at one metre away: what a particle too small to see is dropped by. 0 for a camera with no perspective.
45
+ uPxPerM: { value: 0 },
46
+ };
47
+ }
48
+
49
+ /** The declarations of those values, for a vertex shader. */
50
+ export const SHARED_VERTEX_GLSL = /* glsl */ `
51
+ uniform highp sampler2D uElements;
52
+ uniform highp sampler2D uPlays;
53
+ uniform float uTime;
54
+ uniform float uWeatherTime;
55
+ uniform vec3 uWind;
56
+ uniform vec3 uSunDirection;
57
+ uniform vec3 uSunColor;
58
+ uniform vec3 uSkyColor;
59
+ uniform vec3 uGroundColor;
60
+ uniform highp sampler2DShadow uSunShadow;
61
+ uniform mat4 uSunShadowMatrix;
62
+ uniform float uSunShadowShare;
63
+ uniform float uSunShadowSpread;
64
+ uniform vec4 uFlashPlace[${PARTICLE_FLASH_COUNT}];
65
+ uniform vec3 uFlashColor[${PARTICLE_FLASH_COUNT}];
66
+ uniform vec3 uWrapCenter[${WRAP_LAYER_COUNT}];
67
+ uniform vec3 uWrapShift[${WRAP_LAYER_COUNT}];
68
+ uniform vec3 uWrapBox[${WRAP_LAYER_COUNT}];
69
+ uniform vec3 uWrapVelocity[${WRAP_LAYER_COUNT}];
70
+ uniform float uWrapShare[${WRAP_LAYER_COUNT}];
71
+ uniform highp sampler2D uShelter;
72
+ uniform vec4 uShelterBox;
73
+ uniform float uShelterBaseM;
74
+ uniform float uGroundHeightM;
75
+ uniform float uPxPerM;
76
+
77
+ vec4 zaismaElement(int row, int texel) { return texelFetch(uElements, ivec2(texel, row), 0); }
78
+ vec4 zaismaPlay(int row, int texel) { return texelFetch(uPlays, ivec2(texel, row), 0); }
79
+
80
+ // How much of the sun reaches a place: 1 in the open, less inside a shadow the scene's own things cast. Four
81
+ // readings a little apart, so smoke drifting into a shadow darkens over a step and not at a line.
82
+ float zaismaSunShare(vec3 place) {
83
+ if (uSunShadowShare <= 0.0) return 1.0;
84
+ vec4 at = uSunShadowMatrix * vec4(place, 1.0);
85
+ at.xyz /= at.w;
86
+ if (at.x < 0.0 || at.x > 1.0 || at.y < 0.0 || at.y > 1.0 || at.z > 1.0) return 1.0;
87
+ float depth = at.z - 0.0015;
88
+ float lit = texture(uSunShadow, vec3(at.xy + vec2(uSunShadowSpread, uSunShadowSpread * 0.4), depth))
89
+ + texture(uSunShadow, vec3(at.xy + vec2(-uSunShadowSpread * 0.4, uSunShadowSpread), depth))
90
+ + texture(uSunShadow, vec3(at.xy - vec2(uSunShadowSpread, uSunShadowSpread * 0.4), depth))
91
+ + texture(uSunShadow, vec3(at.xy + vec2(uSunShadowSpread * 0.4, -uSunShadowSpread), depth));
92
+ return mix(1.0, lit * 0.25, uSunShadowShare);
93
+ }
94
+
95
+ // The light that falls on something at a place, facing a way: the sky's and the ground's fill, the sun, and the
96
+ // flash lights near it. As three's own lights give it, so a lit particle sits in the scene's light.
97
+ vec3 zaismaLightAt(vec3 place, vec3 facing, float wrapShare) {
98
+ vec3 light = mix(uGroundColor, uSkyColor, 0.5 + 0.5 * facing.y);
99
+ float toSun = dot(facing, uSunDirection);
100
+ light += uSunColor * (mix(max(toSun, 0.0), 0.5 + 0.5 * toSun, wrapShare) * zaismaSunShare(place));
101
+ for (int i = 0; i < ${PARTICLE_FLASH_COUNT}; i++) {
102
+ vec3 toFlash = uFlashPlace[i].xyz - place;
103
+ float distanceSquared = dot(toFlash, toFlash);
104
+ float reach = clamp(1.0 - distanceSquared / (uFlashPlace[i].w * uFlashPlace[i].w), 0.0, 1.0);
105
+ float facingShare = mix(max(dot(facing, toFlash * inversesqrt(distanceSquared + 1e-4)), 0.0), 1.0, wrapShare);
106
+ light += uFlashColor[i] * (reach * reach * facingShare / max(distanceSquared, 0.25));
107
+ }
108
+ return light * 0.31831;
109
+ }
110
+
111
+ // The roof over a place: the height nothing falls below, from the shelter map, or the ground's height without one.
112
+ float zaismaRoofHeightM(vec2 placeXZ) {
113
+ if (uShelterBox.w < 0.5) return uGroundHeightM;
114
+ // The map is drawn from above with -z up the picture.
115
+ vec2 at = vec2(placeXZ.x - uShelterBox.x, uShelterBox.y - placeXZ.y) / uShelterBox.z + 0.5;
116
+ if (at.x < 0.0 || at.x > 1.0 || at.y < 0.0 || at.y > 1.0) return uGroundHeightM;
117
+ float held = texture2D(uShelter, at).r;
118
+ return held < 0.5 ? uGroundHeightM : held + uShelterBaseM;
119
+ }
120
+ `;
121
+
122
+ /**
123
+ * The end of a fragment shader that blends as the sprites do: gl_FragColor holds the plain colour and how solid it
124
+ * is; addShare says how much of it is light added to what's behind it. Fog, tone mapping and the screen's colour
125
+ * space are applied as three applies them to its own materials, then the colour is made ready for one blend that
126
+ * serves both smoke and fire.
127
+ */
128
+ export const BLEND_END_GLSL = /* glsl */ `
129
+ vec3 zaismaPlainColor = gl_FragColor.rgb;
130
+ float zaismaSolid = gl_FragColor.a;
131
+ #include <fog_fragment>
132
+ #ifdef USE_FOG
133
+ gl_FragColor.rgb = mix(gl_FragColor.rgb, zaismaPlainColor * (1.0 - fogFactor), zaismaAddShare);
134
+ #endif
135
+ #include <tonemapping_fragment>
136
+ #include <colorspace_fragment>
137
+ gl_FragColor = vec4(gl_FragColor.rgb * zaismaSolid, zaismaSolid * (1.0 - zaismaAddShare));
138
+ `;
139
+
140
+ /** A material's settings for that one blend: colour already multiplied by how solid it is. */
141
+ export const ONE_BLEND = {
142
+ transparent: true,
143
+ depthWrite: false,
144
+ blending: THREE.CustomBlending,
145
+ blendEquation: THREE.AddEquation,
146
+ blendSrc: THREE.OneFactor,
147
+ blendDst: THREE.OneMinusSrcAlphaFactor,
148
+ blendSrcAlpha: THREE.OneFactor,
149
+ blendDstAlpha: THREE.OneMinusSrcAlphaFactor,
150
+ };
151
+
152
+ /** three's fog values for a shader material, so the scene's fog (and the lighting piece's, when it's there) reaches it. */
153
+ export function fogUniforms() {
154
+ return THREE.UniformsUtils.clone(THREE.UniformsLib.fog);
155
+ }
@@ -0,0 +1,447 @@
1
+ // The sprite batch: every flat particle of every effect, in one draw call. A particle is four corners written once
2
+ // as it's born; the vertex shader works out where it is from its age (motion.js), sizes, colours and lights it from
3
+ // its element's row, and turns it to face the camera, its own motion or the ground. Smoke that covers and fire that
4
+ // adds its light share one blend, so they layer right with each other.
5
+ //
6
+ // Each emitter holds one run of particles (a slab) and writes into it as a ring. What's drawn is an index list built
7
+ // each frame from the live part of every slab, the farthest effect first.
8
+ import * as THREE from "three";
9
+ import { MOTION_GLSL } from "../motion.js";
10
+ import { FILM_FRAME_COUNT } from "../shapes.js";
11
+ import { createSlotPool } from "../tables.js";
12
+ import { BLEND_END_GLSL, LANDING_LAYER_INDEX, ONE_BLEND, SHARED_VERTEX_GLSL, fogUniforms } from "./shared.js";
13
+
14
+ /** What each texel of an element's row holds. */
15
+ export const ELEMENT_ROW = {
16
+ /** drag per sec, gravity in m/s2 (down), wind share, turbulence in m */
17
+ motionA: 0,
18
+ /** turbulence per sec, swirl in rad/s, pull share, 1 when attached */
19
+ motionB: 1,
20
+ sizeKeys: 2,
21
+ opacityKeys: 3,
22
+ /** size key count, opacity key count, colour key count, size variation share */
23
+ keyCounts: 4,
24
+ /** colour keys (linear rgb); their fourth values: glow, add share, lit share, tone step count */
25
+ colorKeys: 5,
26
+ /** the shape's cell: u, v, size, film frame count (0 for a still shape) */
27
+ cell: 9,
28
+ /** facing (0 camera, 1 velocity, 2 ground, 3 along), streak in sec, spin in rad/s (below 0: upright, swaying), round share */
29
+ facing: 10,
30
+ /** hard share, outline share, weather layer (0 none, 1 to 4), chunk shape index */
31
+ style: 11,
32
+ /** core share: how much the shape's core lightens; how deep inside its edge the shape goes */
33
+ tone: 12,
34
+ };
35
+
36
+ const FLOATS_PER_CORNER = 12;
37
+ const CORNERS_PER_PARTICLE = 4;
38
+ const FLOATS_PER_PARTICLE = FLOATS_PER_CORNER * CORNERS_PER_PARTICLE;
39
+ const INDICES_PER_PARTICLE = 6;
40
+
41
+ const SPRITE_VERTEX = /* glsl */ `
42
+ attribute vec4 aBirth;
43
+ attribute vec4 aVelocity;
44
+ attribute vec4 aSeed;
45
+ ${SHARED_VERTEX_GLSL}
46
+ ${MOTION_GLSL}
47
+ varying vec2 vUv;
48
+ varying vec2 vUvNext;
49
+ varying float vFrameBlend;
50
+ varying vec4 vColor;
51
+ varying vec3 vLight;
52
+ varying vec4 vStyle;
53
+ varying vec4 vRound;
54
+ varying vec2 vCorner;
55
+ varying vec2 vTone;
56
+ varying float vAbove;
57
+ #include <fog_pars_vertex>
58
+
59
+ vec4 zaismaGone() { return vec4(0.0, 0.0, 2.0, 1.0); }
60
+
61
+ void main() {
62
+ int row = int(aSeed.y + 0.5);
63
+ int playRow = int(aSeed.z + 0.5);
64
+ float seed = aSeed.x;
65
+ float age = uTime - aBirth.w;
66
+ float life = aVelocity.w;
67
+ float lifeShare = age / life;
68
+ vec4 motionA = zaismaElement(row, ${ELEMENT_ROW.motionA});
69
+ vec4 motionB = zaismaElement(row, ${ELEMENT_ROW.motionB});
70
+ vec4 facing = zaismaElement(row, ${ELEMENT_ROW.facing});
71
+ vec4 style = zaismaElement(row, ${ELEMENT_ROW.style});
72
+ vec4 playPlace = zaismaPlay(playRow, 0);
73
+ vec4 playTurn = zaismaPlay(playRow, 1);
74
+ vec4 playTint = zaismaPlay(playRow, 2);
75
+
76
+ vec3 world;
77
+ vec3 velocity = vec3(0.0, 1.0, 0.0);
78
+ float edgeFade = 1.0;
79
+ bool isGone = false;
80
+ if (style.z > 0.5) {
81
+ // Weather: a fixed crowd in a box that follows the camera, each wrapped back in as it leaves.
82
+ int layer = int(style.z - 0.5);
83
+ vec3 box = uWrapBox[layer];
84
+ vec3 boxMin = uWrapCenter[layer] - box * 0.5;
85
+ isGone = seed >= uWrapShare[layer];
86
+ if (layer == ${LANDING_LAYER_INDEX}) {
87
+ // Rain landing: each lands somewhere new every time round, on whatever is under it.
88
+ float turn = uWeatherTime * uWrapVelocity[layer].x + seed * 13.0;
89
+ lifeShare = fract(turn);
90
+ float round = floor(turn);
91
+ vec2 landed = aBirth.xz + vec2(zaismaHash(seed, round), zaismaHash(seed + 0.37, round)) * box.xz;
92
+ world.xz = boxMin.xz + mod(landed - boxMin.xz, box.xz);
93
+ world.y = zaismaRoofHeightM(world.xz) + 0.03;
94
+ } else {
95
+ world = boxMin + mod(aBirth.xyz + uWrapShift[layer] - boxMin, box);
96
+ if (motionA.w > 0.0) {
97
+ float turn = uWeatherTime * motionB.x;
98
+ float phase = seed * 6.2831853;
99
+ world.xz += motionA.w * 0.5 * vec2(sin(turn * 2.1 + phase * 3.0) + sin(turn * 1.3 + phase * 7.0), sin(turn * 1.9 + phase * 13.0) + sin(turn * 1.1 + phase * 17.0));
100
+ }
101
+ lifeShare = 0.5;
102
+ velocity = uWrapVelocity[layer];
103
+ if (world.y < zaismaRoofHeightM(world.xz)) isGone = true;
104
+ vec3 fromEdge = min(world - boxMin, boxMin + box - world) / box;
105
+ edgeFade = smoothstep(0.0, 0.1, fromEdge.x) * smoothstep(0.0, 0.1, fromEdge.z);
106
+ }
107
+ age = lifeShare;
108
+ } else {
109
+ isGone = age < 0.0 || lifeShare >= 1.0;
110
+ bool isAttached = motionB.w > 0.5;
111
+ vec3 center = isAttached ? vec3(0.0) : playPlace.xyz;
112
+ vec3 axis = isAttached ? vec3(0.0, 1.0, 0.0) : zaismaTurn(playTurn, vec3(0.0, 1.0, 0.0));
113
+ vec3 local = zaismaLocalPlace(aBirth.xyz, aVelocity.xyz, age, life, seed, motionA, motionB, center, axis);
114
+ world = (isAttached ? playPlace.xyz + zaismaTurn(playTurn, local) : local) + zaismaWorldDrift(age, motionA, uWind);
115
+ if (facing.x > 0.5 && facing.x < 1.5) {
116
+ float earlier = max(age - 0.02, 0.0);
117
+ vec3 localEarlier = zaismaLocalPlace(aBirth.xyz, aVelocity.xyz, earlier, life, seed, motionA, motionB, center, axis);
118
+ vec3 worldEarlier = (isAttached ? playPlace.xyz + zaismaTurn(playTurn, localEarlier) : localEarlier) + zaismaWorldDrift(earlier, motionA, uWind);
119
+ velocity = (world - worldEarlier) / max(age - earlier, 1e-4);
120
+ }
121
+ }
122
+ if (isGone) {
123
+ gl_Position = zaismaGone();
124
+ return;
125
+ }
126
+
127
+ vec4 keyCounts = zaismaElement(row, ${ELEMENT_ROW.keyCounts});
128
+ float size = zaismaOverLife(zaismaElement(row, ${ELEMENT_ROW.sizeKeys}), keyCounts.x, lifeShare);
129
+ size *= playPlace.w * (1.0 + keyCounts.w * (zaismaHash(seed, 1.0) * 2.0 - 1.0));
130
+ float opacity = zaismaOverLife(zaismaElement(row, ${ELEMENT_ROW.opacityKeys}), keyCounts.y, lifeShare) * playTint.a * edgeFade;
131
+
132
+ // An effect that stands on a surface keeps its shapes on it: one heading into the ground slides along it instead,
133
+ // as a blast spreads over the floor. Only near the effect: farther off, the surface may have ended.
134
+ vec4 playSurface = zaismaPlay(playRow, 3);
135
+ vec3 fromPlay = world - playPlace.xyz;
136
+ bool isOnSurface = facing.x < 1.5 && dot(playSurface.xyz, playSurface.xyz) > 0.5 && dot(fromPlay, fromPlay) < 36.0 * playPlace.w * playPlace.w;
137
+ if (isOnSurface) world += playSurface.xyz * max(size * 0.22 - (dot(playSurface.xyz, world) - playSurface.w), 0.0);
138
+
139
+ vec4 mvPosition = viewMatrix * vec4(world, 1.0);
140
+ float distanceM = max(-mvPosition.z, 0.001);
141
+ // Too small to see, or right on the lens: not drawn.
142
+ bool isStreak = facing.x > 0.5 && facing.x < 1.5;
143
+ if (uPxPerM > 0.0 && !isStreak && size * uPxPerM / distanceM < 0.35) {
144
+ gl_Position = zaismaGone();
145
+ return;
146
+ }
147
+ // A streak far off (rain, sparks) is thinner than the screen can draw: it keeps a width that shows, and is fainter for it.
148
+ float streakWidthM = isStreak && uPxPerM > 0.0 ? max(size, 1.3 * distanceM / uPxPerM) : size;
149
+ opacity *= max(size / streakWidthM, 0.3);
150
+ opacity *= clamp((distanceM - 0.08) / (size * 0.5 + 0.15), 0.0, 1.0);
151
+
152
+ float cornerIndex = aSeed.w;
153
+ vec2 corner = vec2(mod(cornerIndex, 2.0), floor(cornerIndex * 0.5 + 0.01)) * 2.0 - 1.0;
154
+ vec2 across = corner;
155
+ if (facing.z != 0.0) {
156
+ // Below 0 is a shape that stands upright (a flame, a drop) and only sways; above it each turns its own way.
157
+ float angle = facing.z < 0.0 ? 0.3 * sin(age * 5.0 + seed * 6.2831853) : zaismaHash(seed, 2.0) * 6.2831853 + facing.z * age * (zaismaHash(seed, 3.0) < 0.5 ? -1.0 : 1.0);
158
+ float cosAngle = cos(angle);
159
+ float sinAngle = sin(angle);
160
+ across = vec2(corner.x * cosAngle - corner.y * sinAngle, corner.x * sinAngle + corner.y * cosAngle);
161
+ }
162
+ vec3 facingWay;
163
+ if (facing.x < 0.5) {
164
+ mvPosition.xy += across * size * 0.5;
165
+ facingWay = normalize(cameraPosition - world);
166
+ } else if (facing.x < 1.5) {
167
+ // Stretched along its own motion, as long as the way it travels in its streak time.
168
+ vec3 viewVelocity = (viewMatrix * vec4(velocity, 0.0)).xyz;
169
+ float speed = length(viewVelocity);
170
+ vec2 along = speed > 1e-4 ? viewVelocity.xy / max(length(viewVelocity.xy), 1e-4) : vec2(0.0, 1.0);
171
+ float lengthM = max(size, length(viewVelocity.xy) * facing.y);
172
+ mvPosition.xy += along * corner.y * lengthM * 0.5 + vec2(-along.y, along.x) * corner.x * streakWidthM * 0.5;
173
+ facingWay = normalize(cameraPosition - world);
174
+ } else {
175
+ // Flat on the ground, or flat across the effect's direction.
176
+ vec3 normal = facing.x < 2.5 ? vec3(0.0, 1.0, 0.0) : zaismaTurn(playTurn, vec3(0.0, 1.0, 0.0));
177
+ vec3 side = normalize(abs(normal.y) < 0.99 ? cross(vec3(0.0, 1.0, 0.0), normal) : vec3(1.0, 0.0, 0.0));
178
+ vec3 forward = cross(normal, side);
179
+ world += (side * across.x + forward * across.y) * size * 0.5 + normal * 0.02;
180
+ mvPosition = viewMatrix * vec4(world, 1.0);
181
+ facingWay = normal;
182
+ }
183
+ gl_Position = projectionMatrix * mvPosition;
184
+ // How far this corner is above the surface its effect stands on, in shares of the band a soft shape fades over.
185
+ vAbove = 1.0;
186
+ if (isOnSurface) {
187
+ vec3 surfaceWay = (viewMatrix * vec4(playSurface.xyz, 0.0)).xyz;
188
+ vec3 surfacePoint = (viewMatrix * vec4(playSurface.xyz * playSurface.w, 1.0)).xyz;
189
+ vAbove = dot(surfaceWay, mvPosition.xyz - surfacePoint) / max(size * 0.3, 0.03);
190
+ }
191
+ if (facing.x < 1.5) {
192
+ // Its depth is taken half its size nearer, so the ground it stands on doesn't cut a line through it.
193
+ vec4 nearer = projectionMatrix * vec4(mvPosition.xy, mvPosition.z + min(size * 0.5, distanceM * 0.5), 1.0);
194
+ gl_Position.z = nearer.z / nearer.w * gl_Position.w;
195
+ }
196
+
197
+ // The shape's cell, or two frames of its moving picture to blend between.
198
+ vec4 cell = zaismaElement(row, ${ELEMENT_ROW.cell});
199
+ vec2 inCell = (corner * 0.5 + 0.5) * 0.98 + 0.01;
200
+ vFrameBlend = 0.0;
201
+ vUv = cell.xy + inCell * cell.z;
202
+ vUvNext = vUv;
203
+ if (cell.w > 0.5) {
204
+ float frame = clamp(lifeShare, 0.0, 0.999) * (cell.w - 1.0);
205
+ float first = floor(frame);
206
+ float second = min(first + 1.0, cell.w - 1.0);
207
+ vUv = cell.xy + (vec2(mod(first, 4.0), floor(first / 4.0)) + inCell) * cell.z;
208
+ vUvNext = cell.xy + (vec2(mod(second, 4.0), floor(second / 4.0)) + inCell) * cell.z;
209
+ vFrameBlend = frame - first;
210
+ }
211
+
212
+ // Its colour through its life, from up to four keys.
213
+ float colorPlace = clamp(lifeShare, 0.0, 1.0) * (keyCounts.z - 1.0);
214
+ float colorIndex = min(floor(colorPlace), max(keyCounts.z - 2.0, 0.0));
215
+ vec4 colorFrom = zaismaElement(row, ${ELEMENT_ROW.colorKeys} + int(colorIndex));
216
+ vec4 colorTo = zaismaElement(row, ${ELEMENT_ROW.colorKeys} + int(min(colorIndex + 1.0, keyCounts.z - 1.0)));
217
+ vec4 glowKey = zaismaElement(row, ${ELEMENT_ROW.colorKeys});
218
+ vec4 addKey = zaismaElement(row, ${ELEMENT_ROW.colorKeys + 1});
219
+ vec4 litKey = zaismaElement(row, ${ELEMENT_ROW.colorKeys + 2});
220
+ vec4 toneKey = zaismaElement(row, ${ELEMENT_ROW.colorKeys + 3});
221
+ vColor = vec4(mix(colorFrom.rgb, colorTo.rgb, colorPlace - colorIndex) * playTint.rgb * glowKey.a, opacity);
222
+ vLight = mix(vec3(1.0), zaismaLightAt(world, facingWay, 0.7), litKey.a);
223
+ vStyle = vec4(addKey.a, style.x, toneKey.a, style.y);
224
+ vRound = vec4(normalize((viewMatrix * vec4(uSunDirection, 0.0)).xyz), facing.w * litKey.a);
225
+ vCorner = corner;
226
+ vTone = zaismaElement(row, ${ELEMENT_ROW.tone}).xy;
227
+ #include <fog_vertex>
228
+ }`;
229
+
230
+ const SPRITE_FRAGMENT = /* glsl */ `
231
+ uniform sampler2D uAtlas;
232
+ varying vec2 vUv;
233
+ varying vec2 vUvNext;
234
+ varying float vFrameBlend;
235
+ varying vec4 vColor;
236
+ varying vec3 vLight;
237
+ varying vec4 vStyle;
238
+ varying vec4 vRound;
239
+ varying vec2 vCorner;
240
+ varying vec2 vTone;
241
+ varying float vAbove;
242
+ #include <common>
243
+ #include <fog_pars_fragment>
244
+
245
+ void main() {
246
+ vec4 held = texture2D(uAtlas, vUv);
247
+ if (vFrameBlend > 0.001) held = mix(held, texture2D(uAtlas, vUvNext), vFrameBlend);
248
+ // A crisp edge from how far inside the shape this point is, at any size on screen.
249
+ float edgeWidth = max(fwidth(held.r) * 0.75, 0.004);
250
+ // A hard shape is never see-through: as its effect fades it, its edge closes in until nothing is left.
251
+ float fadeLeft = 1.0 - vColor.a;
252
+ float edge = 0.5 + (vTone.y - 0.5) * 0.97 * vStyle.y * fadeLeft * fadeLeft;
253
+ float cut = smoothstep(edge - edgeWidth, edge + edgeWidth, held.r);
254
+ float solid = mix(held.g, cut, vStyle.y);
255
+ float tone = held.b;
256
+ if (vStyle.z > 0.5) tone = (floor(tone * vStyle.z * 0.999) + 0.5) / vStyle.z;
257
+ vec3 color = vColor.rgb * (0.62 + 0.55 * tone);
258
+ // A hot core: the middle of the shape lightens toward white, in the look's tones.
259
+ color = mix(color, mix(color, vec3(max(max(color.r, color.g), color.b)), vec3(0.85, 0.65, 0.38)) * 1.15, tone * tone * vTone.x);
260
+ if (vStyle.w > 0.001) {
261
+ float outer = smoothstep(edge - edgeWidth, edge + edgeWidth, held.r + vStyle.w * 0.1);
262
+ color = mix(color * 0.16, color, cut / max(outer, 0.001));
263
+ solid = max(solid, outer);
264
+ }
265
+ // Shaded as a ball, bright toward the sun.
266
+ if (vRound.w > 0.001) {
267
+ float fromMiddle = min(dot(vCorner, vCorner), 1.0);
268
+ vec3 normal = vec3(vCorner, sqrt(1.0 - fromMiddle));
269
+ color *= mix(1.0, clamp(dot(normal, vRound.xyz) * 0.55 + 0.75, 0.3, 1.35), vRound.w);
270
+ }
271
+ float zaismaAddShare = vStyle.x;
272
+ // A soft shape fades into the surface it stands on; a hard one is cut by it, as a drawn one would be.
273
+ solid *= mix(clamp(vAbove, 0.0, 1.0), 1.0, vStyle.y);
274
+ gl_FragColor = vec4(color * vLight, solid * mix(vColor.a, 1.0, vStyle.y));
275
+ ${BLEND_END_GLSL}
276
+ }`;
277
+
278
+ /**
279
+ * Makes the batch. shared: the uniforms every batch reads. Answers { mesh, takeSlab, giveSlab, write, update,
280
+ * liveCount, capacity, dispose }.
281
+ */
282
+ export function createSpriteBatch({ particleCount, sharedUniforms }) {
283
+ const vertices = new Float32Array(particleCount * FLOATS_PER_PARTICLE);
284
+ const buffer = new THREE.InterleavedBuffer(vertices, FLOATS_PER_CORNER);
285
+ buffer.setUsage(THREE.DynamicDrawUsage);
286
+ const geometry = new THREE.BufferGeometry();
287
+ geometry.setAttribute("aBirth", new THREE.InterleavedBufferAttribute(buffer, 4, 0));
288
+ geometry.setAttribute("aVelocity", new THREE.InterleavedBufferAttribute(buffer, 4, 4));
289
+ geometry.setAttribute("aSeed", new THREE.InterleavedBufferAttribute(buffer, 4, 8));
290
+ const indices = new Uint32Array(particleCount * INDICES_PER_PARTICLE);
291
+ const index = new THREE.BufferAttribute(indices, 1);
292
+ index.setUsage(THREE.DynamicDrawUsage);
293
+ geometry.setIndex(index);
294
+ geometry.setDrawRange(0, 0);
295
+
296
+ const material = new THREE.ShaderMaterial({
297
+ name: "zaisma_effect_sprites",
298
+ vertexShader: SPRITE_VERTEX,
299
+ fragmentShader: SPRITE_FRAGMENT,
300
+ uniforms: { ...fogUniforms(), ...sharedUniforms },
301
+ fog: true,
302
+ side: THREE.DoubleSide,
303
+ ...ONE_BLEND,
304
+ });
305
+ const mesh = new THREE.Mesh(geometry, material);
306
+ mesh.name = "zaisma_effect_sprites";
307
+ mesh.frustumCulled = false;
308
+ mesh.renderOrder = 900;
309
+ mesh.visible = false;
310
+
311
+ const pool = createSlotPool(particleCount);
312
+ /** When each particle's life ends, on the effects' clock: what moves a slab's live part on. */
313
+ const endSec = new Float32Array(particleCount);
314
+ const slabs = new Set();
315
+ let dirtyStart = Infinity;
316
+ let dirtyEnd = -1;
317
+ let liveCount = 0;
318
+ let drawnOrder = [];
319
+
320
+ function takeSlab(wantedCount, owner) {
321
+ const count = Math.min(wantedCount, pool.longestFreeCount());
322
+ if (count <= 0) return null;
323
+ const slab = { start: pool.take(count), capacity: count, nextOffset: 0, liveStartOffset: 0, liveCount: 0, owner, distanceSquared: 0, isForever: false };
324
+ slabs.add(slab);
325
+ return slab;
326
+ }
327
+
328
+ function giveSlab(slab) {
329
+ if (!slabs.delete(slab)) return;
330
+ // Its particles are written as never born, so a later owner of these slots starts clean.
331
+ const from = slab.start * FLOATS_PER_PARTICLE;
332
+ vertices.fill(0, from, from + slab.capacity * FLOATS_PER_PARTICLE);
333
+ for (let offset = 0; offset < slab.capacity * CORNERS_PER_PARTICLE; offset += 1) vertices[from + offset * FLOATS_PER_CORNER + 3] = 1e9;
334
+ markDirty(slab.start, slab.capacity);
335
+ pool.give(slab.start, slab.capacity);
336
+ }
337
+
338
+ function markDirty(particleIndex, count) {
339
+ dirtyStart = Math.min(dirtyStart, particleIndex);
340
+ dirtyEnd = Math.max(dirtyEnd, particleIndex + count);
341
+ }
342
+
343
+ /** Writes one particle into a slab's ring, over its oldest when it's full. */
344
+ function write(slab, x, y, z, velocityX, velocityY, velocityZ, birthSec, lifeSec, seedNumber, elementRow, playRow) {
345
+ const particleIndex = slab.start + slab.nextOffset;
346
+ slab.nextOffset = (slab.nextOffset + 1) % slab.capacity;
347
+ if (slab.liveCount < slab.capacity) slab.liveCount += 1;
348
+ else slab.liveStartOffset = (slab.liveStartOffset + 1) % slab.capacity;
349
+ endSec[particleIndex] = birthSec + lifeSec;
350
+ let at = particleIndex * FLOATS_PER_PARTICLE;
351
+ for (let corner = 0; corner < CORNERS_PER_PARTICLE; corner += 1) {
352
+ vertices[at] = x;
353
+ vertices[at + 1] = y;
354
+ vertices[at + 2] = z;
355
+ vertices[at + 3] = birthSec;
356
+ vertices[at + 4] = velocityX;
357
+ vertices[at + 5] = velocityY;
358
+ vertices[at + 6] = velocityZ;
359
+ vertices[at + 7] = lifeSec;
360
+ vertices[at + 8] = seedNumber;
361
+ vertices[at + 9] = elementRow;
362
+ vertices[at + 10] = playRow;
363
+ vertices[at + 11] = corner;
364
+ at += FLOATS_PER_CORNER;
365
+ }
366
+ markDirty(particleIndex, 1);
367
+ }
368
+
369
+ /** Once a frame: moves each slab's live part past what has ended, and lists what's left to draw, far to near. */
370
+ function update(nowSec, cameraPosition) {
371
+ drawnOrder.length = 0;
372
+ liveCount = 0;
373
+ for (const slab of slabs) {
374
+ if (!slab.isForever) {
375
+ while (slab.liveCount > 0 && endSec[slab.start + slab.liveStartOffset] <= nowSec) {
376
+ slab.liveStartOffset = (slab.liveStartOffset + 1) % slab.capacity;
377
+ slab.liveCount -= 1;
378
+ }
379
+ }
380
+ if (slab.liveCount === 0) continue;
381
+ liveCount += slab.liveCount;
382
+ const place = slab.owner?.place;
383
+ slab.distanceSquared = slab.isForever ? -1 : place ? (place.x - cameraPosition.x) ** 2 + (place.y - cameraPosition.y) ** 2 + (place.z - cameraPosition.z) ** 2 : 0;
384
+ drawnOrder.push(slab);
385
+ }
386
+ // Weather is all round the camera, so it's drawn last; the rest, the farthest effect first.
387
+ drawnOrder.sort((first, second) => (first.isForever !== second.isForever ? (first.isForever ? 1 : -1) : second.distanceSquared - first.distanceSquared));
388
+ let indexCount = 0;
389
+ for (const slab of drawnOrder) {
390
+ for (let step = 0; step < slab.liveCount; step += 1) {
391
+ const corner = (slab.start + ((slab.liveStartOffset + step) % slab.capacity)) * CORNERS_PER_PARTICLE;
392
+ indices[indexCount] = corner;
393
+ indices[indexCount + 1] = corner + 1;
394
+ indices[indexCount + 2] = corner + 2;
395
+ indices[indexCount + 3] = corner + 2;
396
+ indices[indexCount + 4] = corner + 1;
397
+ indices[indexCount + 5] = corner + 3;
398
+ indexCount += INDICES_PER_PARTICLE;
399
+ }
400
+ }
401
+ geometry.setDrawRange(0, indexCount);
402
+ mesh.visible = indexCount > 0;
403
+ if (indexCount > 0) {
404
+ index.clearUpdateRanges();
405
+ index.addUpdateRange(0, indexCount);
406
+ index.needsUpdate = true;
407
+ }
408
+ if (dirtyEnd > dirtyStart) {
409
+ buffer.clearUpdateRanges();
410
+ buffer.addUpdateRange(dirtyStart * FLOATS_PER_PARTICLE, (dirtyEnd - dirtyStart) * FLOATS_PER_PARTICLE);
411
+ buffer.needsUpdate = true;
412
+ dirtyStart = Infinity;
413
+ dirtyEnd = -1;
414
+ }
415
+ }
416
+
417
+ /** Moves every time written here back by a span, as the effects' clock starts over: see index.js. */
418
+ function shiftTime(spanSec) {
419
+ for (let at = 3; at < vertices.length; at += FLOATS_PER_CORNER) vertices[at] -= spanSec;
420
+ for (let index = 0; index < endSec.length; index += 1) endSec[index] -= spanSec;
421
+ markDirty(0, particleCount);
422
+ }
423
+
424
+ return {
425
+ mesh,
426
+ takeSlab,
427
+ giveSlab,
428
+ write,
429
+ update,
430
+ shiftTime,
431
+ get liveCount() {
432
+ return liveCount;
433
+ },
434
+ get capacity() {
435
+ return particleCount;
436
+ },
437
+ get freeCount() {
438
+ return pool.freeCount;
439
+ },
440
+ dispose() {
441
+ geometry.dispose();
442
+ material.dispose();
443
+ },
444
+ };
445
+ }
446
+
447
+ export { FILM_FRAME_COUNT };