babylon.quarks 0.26.0 → 0.28.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 (83) hide show
  1. package/CHANGELOG.md +1194 -8
  2. package/README.md +13 -9
  3. package/dist/babylon.quarks.cjs +1 -1
  4. package/dist/babylon.quarks.cjs.map +1 -1
  5. package/dist/babylon.quarks.esm.js +1 -1
  6. package/dist/babylon.quarks.esm.js.map +1 -1
  7. package/dist/babylon.quarks.umd.min.js +1 -1
  8. package/dist/babylon.quarks.umd.min.js.map +1 -1
  9. package/dist/types/index.d.cts +10108 -0
  10. package/dist/types/index.d.ts +5794 -998
  11. package/package.json +18 -7
  12. package/src/BatchedRenderer.ts +274 -156
  13. package/src/EffectPool.ts +94 -12
  14. package/src/MeshSurfaceEmitter.ts +50 -14
  15. package/src/ParticleEmitter.ts +75 -17
  16. package/src/ParticleSystem.ts +981 -2066
  17. package/src/QuarksLoader.ts +666 -194
  18. package/src/QuarksPrefab.ts +367 -73
  19. package/src/QuarksSerializer.ts +512 -0
  20. package/src/QuarksUtil.ts +77 -7
  21. package/src/SpriteBatch.ts +127 -252
  22. package/src/TrailBatch.ts +545 -348
  23. package/src/VFXBatch.ts +238 -596
  24. package/src/batchKey.ts +239 -0
  25. package/src/batchSettings.ts +546 -0
  26. package/src/effectDocumentJSON.ts +261 -0
  27. package/src/effectResources.ts +76 -0
  28. package/src/envAtlas.ts +126 -18
  29. package/src/geometryUtil.ts +1 -1
  30. package/src/index.ts +69 -35
  31. package/src/nodeId.ts +36 -0
  32. package/src/shaderProgram.ts +419 -0
  33. package/src/shaderVariants.ts +245 -0
  34. package/src/shaders/chunks/blend.ts +31 -0
  35. package/src/shaders/chunks/fog.ts +12 -5
  36. package/src/shaders/chunks/sheet.ts +111 -0
  37. package/src/shaders/chunks/soft.ts +85 -0
  38. package/src/shaders/chunks/surface.ts +12 -2
  39. package/src/shaders/local_particle_physics_vert.glsl.ts +14 -28
  40. package/src/shaders/local_particle_physics_vert.wgsl.ts +16 -29
  41. package/src/shaders/particle_frag.glsl.ts +5 -22
  42. package/src/shaders/particle_frag.wgsl.ts +5 -23
  43. package/src/shaders/particle_physics_frag.glsl.ts +16 -26
  44. package/src/shaders/particle_physics_frag.wgsl.ts +14 -26
  45. package/src/shaders/particle_vert.glsl.ts +5 -27
  46. package/src/shaders/particle_vert.wgsl.ts +5 -26
  47. package/src/shaders/stretched_bb_particle_vert.glsl.ts +7 -33
  48. package/src/shaders/stretched_bb_particle_vert.wgsl.ts +7 -33
  49. package/src/shaders/trail_frag.glsl.ts +13 -0
  50. package/src/shaders/trail_frag.wgsl.ts +13 -0
  51. package/src/shaders/trail_vert.glsl.ts +45 -36
  52. package/src/shaders/trail_vert.wgsl.ts +46 -40
  53. package/src/softDepth.ts +136 -0
  54. package/src/system/BehaviorPlan.ts +85 -0
  55. package/src/system/BoundsTracker.ts +181 -0
  56. package/src/system/EmissionController.ts +358 -0
  57. package/src/system/EventDispatcher.ts +60 -0
  58. package/src/system/ParticleSpawner.ts +274 -0
  59. package/src/system/SimulationClock.ts +130 -0
  60. package/src/system/geometry.ts +104 -0
  61. package/src/system/integrate.ts +428 -0
  62. package/src/system/materialResolve.ts +165 -0
  63. package/src/system/normalize.ts +100 -0
  64. package/src/system/serialization.ts +422 -0
  65. package/src/system/settingsSchema.ts +339 -0
  66. package/src/system/types.ts +449 -0
  67. package/src/textureSource.ts +30 -0
  68. package/src/trailSettings.ts +54 -0
  69. package/src/BatchedParticleRenderer.ts +0 -3
  70. package/src/materials/ParticleMaterials.ts +0 -23
  71. package/src/materials/index.ts +0 -1
  72. package/src/shaders/chunks/index.ts +0 -1
  73. package/src/shaders/chunks/register-shader-chunks.ts +0 -32
  74. package/src/shaders/chunks/soft_fragment.glsl.ts +0 -13
  75. package/src/shaders/chunks/soft_pars_fragment.glsl.ts +0 -20
  76. package/src/shaders/chunks/soft_pars_vertex.glsl.ts +0 -6
  77. package/src/shaders/chunks/soft_vertex.glsl.ts +0 -6
  78. package/src/shaders/chunks/tile_fragment.glsl.ts +0 -9
  79. package/src/shaders/chunks/tile_pars_fragment.glsl.ts +0 -9
  80. package/src/shaders/chunks/tile_pars_vertex.glsl.ts +0 -28
  81. package/src/shaders/chunks/tile_vertex.glsl.ts +0 -18
  82. package/src/shaders/index.ts +0 -9
  83. package/src/shaders/local_particle_vert.glsl.ts +0 -50
@@ -0,0 +1,546 @@
1
+ import type {BaseTexture} from '@babylonjs/core/Materials/Textures/baseTexture';
2
+ import {Color3} from '@babylonjs/core/Maths/math.color';
3
+ import {Vector3 as BVector3} from '@babylonjs/core/Maths/math.vector';
4
+ import type {Vector3} from 'quarks.core';
5
+
6
+ // The settings a batch's systems share and the helpers that normalise and compare them. They live
7
+ // apart from VFXBatch so the batch key (batchKey.ts) can read them without importing the batch.
8
+
9
+ /**
10
+ * The order the particles of a batch are drawn in (Unity's Sort Mode). Alpha
11
+ * blended particles cover what was drawn before them, so the order shows.
12
+ *
13
+ * - `'none'`: as they are, system by system. Right for additive blending.
14
+ * - `'distance'`: farthest from the camera first, so nearer particles cover
15
+ * farther ones. The order is computed once a frame, from the first of the
16
+ * scene's active cameras (`scene.activeCameras[0]`, else `activeCamera`);
17
+ * other cameras, a second viewport or a render target, draw the same order.
18
+ * - `'oldestInFront'` / `'youngestInFront'`: by age.
19
+ *
20
+ * Sorting runs over the whole batch after each fill. Trails are not sorted.
21
+ */
22
+ export type SortMode = 'none' | 'distance' | 'oldestInFront' | 'youngestInFront';
23
+
24
+ /**
25
+ * Which faces of a mesh particle are drawn (Unity's Render Face, Unreal's Two Sided): `'both'`
26
+ * (the default), `'front'` (back faces culled, the usual choice for a closed mesh) or `'back'`
27
+ * (front faces culled, to see the inside of a dome or a shell). Front faces are the ones a Babylon
28
+ * mesh shows with back-face culling on, the outside of a box or sphere. It applies to the Mesh
29
+ * render mode only: billboards, stretched billboards, beams and trails are flat sheets whose
30
+ * winding follows the camera or the particle's path, so they always draw both faces.
31
+ *
32
+ * The culling is set once for the whole batch. A mesh particle mirrored by a negative size on an
33
+ * odd number of axes turns its winding inside out, so `'front'` then shows its inside and
34
+ * `'back'` its outside.
35
+ */
36
+ export type RenderFace = 'both' | 'front' | 'back';
37
+
38
+ /** How a system draws its particles (Unity's Render Mode), see {@link ParticleSystem.renderMode}. */
39
+ export enum RenderMode {
40
+ /** A quad facing the camera (Unity's Billboard). The default. */
41
+ BillBoard = 0,
42
+ /**
43
+ * A quad facing the camera and stretched along the particle's velocity (Unity's Stretched
44
+ * Billboard), by `speedFactor` and `lengthFactor` in the system's `rendererEmitterSettings`.
45
+ */
46
+ StretchedBillBoard = 1,
47
+ /** The system's mesh, one instance per particle (Unity's Mesh), see {@link ParticleSystem.instancingGeometry}. */
48
+ Mesh = 2,
49
+ /**
50
+ * A ribbon drawn through the particle's past positions (Unity's Trails module, Niagara's
51
+ * ribbon), facing the camera; its settings are `TrailSettings` in `rendererEmitterSettings`.
52
+ */
53
+ Trail = 3,
54
+ /** A quad lying flat in the world's XZ plane, facing up (Unity's Horizontal Billboard). */
55
+ HorizontalBillBoard = 4,
56
+ /** A quad standing upright and turning only about the world's Y axis to face the camera (Unity's Vertical Billboard). */
57
+ VerticalBillBoard = 5,
58
+ /**
59
+ * A quad laid from each particle to the system's {@link ParticleSystem.beamEnd}
60
+ * and turned about its length to face the camera (a line renderer): U runs
61
+ * from the particle (0) to the end (1), V across, the particle's size is
62
+ * the width. Lightning, tethers, lasers.
63
+ */
64
+ Beam = 6,
65
+ }
66
+
67
+ /**
68
+ * How mesh particles are lit.
69
+ *
70
+ * - `'fixed'` (default): one directional light plus ambient, the same for every
71
+ * mesh batch of a renderer, set with {@link BatchedRenderer.setMeshLighting}.
72
+ * - `'unlit'`: no lighting term — the texture times the particle colour, for
73
+ * emissive meshes (fire, energy, glowing blades).
74
+ * - `'scene'`: follows the scene's first enabled `DirectionalLight` or
75
+ * `HemisphericLight` every frame, falling back to the fixed light when there
76
+ * is none.
77
+ *
78
+ * Only mesh particles are lit; other render modes ignore it.
79
+ */
80
+ export type MeshLighting = 'unlit' | 'fixed' | 'scene';
81
+
82
+ /** The light {@link MeshLighting} `'fixed'` mesh particles are lit by. */
83
+ export interface MeshLightingSettings {
84
+ /** Direction the light travels in, world space. Default `(0.4, -1, 0.6)`. */
85
+ direction: BVector3;
86
+ /** Light colour, intensity folded in. Default white. */
87
+ color: Color3;
88
+ /** Light every face gets regardless of its direction. Default `(0.35, 0.35, 0.35)`. */
89
+ ambient: Color3;
90
+ }
91
+
92
+ /** The light mesh particles have always been lit by. */
93
+ export function defaultMeshLighting(): MeshLightingSettings {
94
+ return {
95
+ direction: new BVector3(0.4, -1, 0.6),
96
+ color: new Color3(1, 1, 1),
97
+ ambient: new Color3(0.35, 0.35, 0.35),
98
+ };
99
+ }
100
+
101
+ /**
102
+ * A rim (Fresnel) term on mesh particles: a colour that grows where the surface
103
+ * turns away from the camera, `pow(1 - |N·V|, power) * strength`. Unset: no rim.
104
+ * Only mesh particles draw it.
105
+ */
106
+ export interface MeshRim {
107
+ /** Edge colour, linear RGB. Default `[1, 1, 1]`. */
108
+ color: [number, number, number];
109
+ /** Falloff exponent; higher keeps the rim to a thinner edge. Default 2. */
110
+ power: number;
111
+ /** How much of the rim is applied, usually 0…1. Default 1. */
112
+ strength: number;
113
+ /**
114
+ * `'add'` (default) adds the colour, for a glowing edge; `'mix'` blends the
115
+ * surface toward it, for an outline (a dark ink line with a dark colour);
116
+ * `'fade'` leaves the colour and fades the faces turned to the camera by
117
+ * `strength` instead, alpha times `1 - strength * (1 - edge)`: a wall of
118
+ * light that reads thick along its silhouette and thin across its face.
119
+ */
120
+ mode: 'add' | 'mix' | 'fade';
121
+ }
122
+
123
+ /**
124
+ * A complete rim from a partial one, every field defaulted and kept in range,
125
+ * or undefined when `rim` is unset.
126
+ */
127
+ export function normalizeMeshRim(rim: Partial<MeshRim> | null | undefined): MeshRim | undefined {
128
+ if (!rim || typeof rim !== 'object') {
129
+ return undefined;
130
+ }
131
+ const color = Array.isArray(rim.color) ? rim.color : [1, 1, 1];
132
+ const channel = (value: unknown, fallback: number) =>
133
+ typeof value === 'number' && Number.isFinite(value) ? value : fallback;
134
+ return {
135
+ color: [channel(color[0], 1), channel(color[1], 1), channel(color[2], 1)],
136
+ power: Math.max(0.01, channel(rim.power, 2)),
137
+ strength: Math.max(0, channel(rim.strength, 1)),
138
+ mode: rim.mode === 'mix' || rim.mode === 'fade' ? rim.mode : 'add',
139
+ };
140
+ }
141
+
142
+ /** True when both describe the same rim (or both none). */
143
+ export function meshRimEqual(a: MeshRim | undefined, b: MeshRim | undefined): boolean {
144
+ if (a === b) {
145
+ return true;
146
+ }
147
+ if (!a || !b) {
148
+ return false;
149
+ }
150
+ return (
151
+ a.mode === b.mode &&
152
+ a.power === b.power &&
153
+ a.strength === b.strength &&
154
+ a.color[0] === b.color[0] &&
155
+ a.color[1] === b.color[1] &&
156
+ a.color[2] === b.color[2]
157
+ );
158
+ }
159
+
160
+ /**
161
+ * Which texture coordinates a texture is sampled with, as Unity's UV channels
162
+ * and Niagara's ribbon UV0 / UV1:
163
+ *
164
+ * - `0`: the particle's first UV, with the sprite sheet's cell applied: on a
165
+ * sheet the current frame's cell, on a trail the cell's stretch along it, on
166
+ * a mesh its first UV set. Read before the map's own layout and distortion,
167
+ * so a texture on it does not move with them.
168
+ * - `1`: the particle's own UV, never moved by the sheet. The whole quad on a
169
+ * billboard, a stretched billboard or a beam (mirrored with the quad when it
170
+ * flips); on a trail U along it the way the map's runs (from the tail, or
171
+ * from the head with `uvFromHead`) and V across it; on a mesh its second UV
172
+ * set (`instancingUVs2`), or its first when it has none.
173
+ *
174
+ * A mesh can then repeat its picture along UV0 while a mask on UV1 runs once
175
+ * across its whole length, as a lightmap UV does in a game engine.
176
+ */
177
+ export type UVChannel = 0 | 1;
178
+
179
+ /**
180
+ * How a texture is laid over the particle: its UV channel, then tiling and
181
+ * offset as in Unity's material Tiling / Offset, then a scroll on the
182
+ * renderer's clock as Unreal's Panner. Sampled at
183
+ * `uv(channel) * tiling + offset + scroll * time`; the offset is used as
184
+ * given, so a clamped texture can be moved off its edge, and only the
185
+ * scroll's share is wrapped (over two repeats, so a mirrored texture stays
186
+ * seamless). A texture that scrolls or tiles should repeat.
187
+ */
188
+ export interface TextureUV {
189
+ /** See {@link UVChannel}. Default 0. */
190
+ channel: UVChannel;
191
+ /** Repeats across the channel's 0…1, `[u, v]`. Default `[1, 1]`. */
192
+ tiling: [number, number];
193
+ /** Added after tiling, in texture UV units. Default `[0, 0]`. */
194
+ offset: [number, number];
195
+ /** UV units per second on the renderer's clock. Default `[0, 0]`. */
196
+ scroll: [number, number];
197
+ }
198
+
199
+ /** A complete {@link TextureUV} from a partial one, with `defaults` under the fields left out. */
200
+ export function normalizeTextureUV(
201
+ uv: Partial<TextureUV> | null | undefined,
202
+ defaults: Partial<TextureUV> = {}
203
+ ): TextureUV {
204
+ const source = uv && typeof uv === 'object' ? uv : {};
205
+ const num = (value: unknown, fallback: number) =>
206
+ typeof value === 'number' && Number.isFinite(value) ? value : fallback;
207
+ const pair = (value: unknown, fallback: [number, number]): [number, number] =>
208
+ Array.isArray(value) ? [num(value[0], fallback[0]), num(value[1], fallback[1])] : [fallback[0], fallback[1]];
209
+ const channel = source.channel ?? defaults.channel;
210
+ return {
211
+ channel: channel === 1 ? 1 : 0,
212
+ tiling: pair(source.tiling, defaults.tiling ?? [1, 1]),
213
+ offset: pair(source.offset, defaults.offset ?? [0, 0]),
214
+ scroll: pair(source.scroll, defaults.scroll ?? [0, 0]),
215
+ };
216
+ }
217
+
218
+ /** True when both lay a texture the same way. */
219
+ export function textureUVEqual(a: TextureUV, b: TextureUV): boolean {
220
+ return (
221
+ a === b ||
222
+ (a.channel === b.channel &&
223
+ a.tiling[0] === b.tiling[0] &&
224
+ a.tiling[1] === b.tiling[1] &&
225
+ a.offset[0] === b.offset[0] &&
226
+ a.offset[1] === b.offset[1] &&
227
+ a.scroll[0] === b.scroll[0] &&
228
+ a.scroll[1] === b.scroll[1])
229
+ );
230
+ }
231
+
232
+ /** The fields of `uv` that differ from `defaults`, for JSON written only when set. */
233
+ export function textureUVToJSON(uv: TextureUV, defaults: Partial<TextureUV> = {}): Partial<TextureUV> | undefined {
234
+ const base = normalizeTextureUV(undefined, defaults);
235
+ const json: Partial<TextureUV> = {};
236
+ if (uv.channel !== base.channel) json.channel = uv.channel;
237
+ if (uv.tiling[0] !== base.tiling[0] || uv.tiling[1] !== base.tiling[1]) json.tiling = [...uv.tiling];
238
+ if (uv.offset[0] !== base.offset[0] || uv.offset[1] !== base.offset[1]) json.offset = [...uv.offset];
239
+ if (uv.scroll[0] !== base.scroll[0] || uv.scroll[1] !== base.scroll[1]) json.scroll = [...uv.scroll];
240
+ return Object.keys(json).length > 0 ? json : undefined;
241
+ }
242
+
243
+ /**
244
+ * Which value of the map's texel picks the colour along a gradient map's
245
+ * ramp: one channel, so a texture can pack several masks (Unreal's and
246
+ * Unity's channel-packed textures), or the Rec. 709 luminance of its colour.
247
+ */
248
+ export type GradientMapChannel = 'r' | 'g' | 'b' | 'a' | 'luminance';
249
+
250
+ /**
251
+ * A gradient map (colour ramp): the map is recoloured by looking its
252
+ * {@link channel} up along U of {@link ramp}. Without a map the lookup reads
253
+ * white, so the ramp's right-hand end tints every particle, in every render
254
+ * mode alike. See {@link VFXBatchSettings.gradientMap}.
255
+ */
256
+ export interface ParticleGradientMap {
257
+ /**
258
+ * The ramp, read along U at mid V: low values on the left. Make it without
259
+ * mipmaps and clamped. Required: without one there is no gradient map.
260
+ */
261
+ ramp: BaseTexture | null;
262
+ /**
263
+ * The texel value the ramp is looked up by. Default `'r'`. With `'a'` the
264
+ * ramp's alpha becomes the texel's alpha, so a soft shape can glow at its
265
+ * fringe and stay solid at its heart; with any other channel the ramp's
266
+ * alpha multiplies the map's.
267
+ */
268
+ channel: GradientMapChannel;
269
+ }
270
+
271
+ /** The weights a texel is dotted with to look up the ramp, per {@link GradientMapChannel}. */
272
+ export const GRADIENT_MAP_WEIGHTS: Readonly<Record<GradientMapChannel, readonly [number, number, number, number]>> = {
273
+ r: [1, 0, 0, 0],
274
+ g: [0, 1, 0, 0],
275
+ b: [0, 0, 1, 0],
276
+ a: [0, 0, 0, 1],
277
+ luminance: [0.2126, 0.7152, 0.0722, 0],
278
+ };
279
+
280
+ /** A complete gradient map, or undefined when unset or without a ramp. */
281
+ export function normalizeGradientMap(
282
+ gradientMap: Partial<ParticleGradientMap> | null | undefined
283
+ ): ParticleGradientMap | undefined {
284
+ if (!gradientMap || typeof gradientMap !== 'object' || !gradientMap.ramp) {
285
+ return undefined;
286
+ }
287
+ const channel = gradientMap.channel;
288
+ return {
289
+ ramp: gradientMap.ramp,
290
+ channel: channel !== undefined && channel in GRADIENT_MAP_WEIGHTS ? channel : 'r',
291
+ };
292
+ }
293
+
294
+ /** True when both recolour the same way (or both not at all). */
295
+ export function gradientMapEqual(a: ParticleGradientMap | undefined, b: ParticleGradientMap | undefined): boolean {
296
+ return a === b || (!!a && !!b && a.ramp === b.ramp && a.channel === b.channel);
297
+ }
298
+
299
+ /**
300
+ * Alpha erosion: the particle's alpha decides how much of its shape is left
301
+ * instead of how transparent it is, so `ColorOverLife` fading alpha out
302
+ * dissolves (noise mask) or wipes (gradient mask) the shape away. See
303
+ * {@link VFXBatchSettings.erosion}.
304
+ */
305
+ export interface ParticleErosion {
306
+ /**
307
+ * Mask whose red channel orders what goes first, laid by {@link maskUV}.
308
+ * Null: the map's own alpha, so a soft shape shrinks from its edges.
309
+ */
310
+ mask: BaseTexture | null;
311
+ /**
312
+ * How the mask is laid over the particle, see {@link TextureUV}. On UV
313
+ * channel 1 a gradient mask wipes a whole trail or mesh however its map
314
+ * repeats. Default channel 0, untiled, still.
315
+ */
316
+ maskUV: TextureUV;
317
+ /** Width of the soft transition at the cut, in mask units. Default 0.05. */
318
+ softness: number;
319
+ /** Width of the glowing band above the cut, in mask units; 0 (default) draws none. */
320
+ edgeWidth: number;
321
+ /** Colour added in the edge band, linear RGB, may exceed 1. Default `[1, 1, 1]`. */
322
+ edgeColor: [number, number, number];
323
+ }
324
+ // Limits worth knowing: with neither a mask nor a map there is
325
+ // nothing to order the cut by, so the shape goes all at once near alpha 0.
326
+ // Scrolling runs on the renderer's clock, not a system's playback speed.
327
+
328
+ /** {@link ParticleErosion} as given: any field may be left out, the mask's layout too. */
329
+ export type ParticleErosionOptions = Partial<Omit<ParticleErosion, 'maskUV'>> & {
330
+ /** See {@link ParticleErosion.maskUV}; fields left out take their defaults. */
331
+ maskUV?: Partial<TextureUV>;
332
+ };
333
+
334
+ /** The width of the soft band an erosion cut fades over when `softness` is left out, in mask units. */
335
+ export const EROSION_DEFAULT_SOFTNESS = 0.05;
336
+
337
+ /** A complete erosion from a partial one, every field defaulted, or undefined when `erosion` is unset. */
338
+ export function normalizeErosion(erosion: ParticleErosionOptions | null | undefined): ParticleErosion | undefined {
339
+ if (!erosion || typeof erosion !== 'object') {
340
+ return undefined;
341
+ }
342
+ const num = (value: unknown, fallback: number) =>
343
+ typeof value === 'number' && Number.isFinite(value) ? value : fallback;
344
+ const color = Array.isArray(erosion.edgeColor) ? erosion.edgeColor : [1, 1, 1];
345
+ return {
346
+ mask: erosion.mask ?? null,
347
+ maskUV: normalizeTextureUV(erosion.maskUV),
348
+ softness: Math.max(0.0001, num(erosion.softness, EROSION_DEFAULT_SOFTNESS)),
349
+ edgeWidth: Math.max(0, num(erosion.edgeWidth, 0)),
350
+ edgeColor: [num(color[0], 1), num(color[1], 1), num(color[2], 1)],
351
+ };
352
+ }
353
+
354
+ /** True when both describe the same erosion (or both none). */
355
+ export function erosionEqual(a: ParticleErosion | undefined, b: ParticleErosion | undefined): boolean {
356
+ if (a === b) {
357
+ return true;
358
+ }
359
+ if (!a || !b) {
360
+ return false;
361
+ }
362
+ return (
363
+ a.mask === b.mask &&
364
+ textureUVEqual(a.maskUV, b.maskUV) &&
365
+ a.softness === b.softness &&
366
+ a.edgeWidth === b.edgeWidth &&
367
+ a.edgeColor[0] === b.edgeColor[0] &&
368
+ a.edgeColor[1] === b.edgeColor[1] &&
369
+ a.edgeColor[2] === b.edgeColor[2]
370
+ );
371
+ }
372
+
373
+ /** The distortion noise's default layout: drifting up half a repeat a second. */
374
+ export const UV_DISTORTION_DEFAULT_UV: Partial<TextureUV> = {scroll: [0, 0.5]};
375
+
376
+ /**
377
+ * UV distortion: the map's UV pushed about by a noise texture, for heat
378
+ * shimmer, licking flames and breathing auras. See
379
+ * {@link VFXBatchSettings.uvDistortion}.
380
+ */
381
+ export interface ParticleUVDistortion {
382
+ /**
383
+ * Noise whose red and green channels, around 0.5, push U and V. It should
384
+ * wrap (repeat). Required: without one there is no distortion.
385
+ */
386
+ map: BaseTexture | null;
387
+ /**
388
+ * How the noise is laid over the particle, see {@link TextureUV}. Default
389
+ * channel 0, untiled, scrolling `[0, 0.5]`.
390
+ */
391
+ mapUV: TextureUV;
392
+ /** How far the map's UV is pushed at most, in UV units. Default 0.05. */
393
+ strength: number;
394
+ }
395
+
396
+ /** {@link ParticleUVDistortion} as given: any field may be left out, the noise's layout too. */
397
+ export type ParticleUVDistortionOptions = Partial<Omit<ParticleUVDistortion, 'mapUV'>> & {
398
+ /** See {@link ParticleUVDistortion.mapUV}; fields left out take their defaults. */
399
+ mapUV?: Partial<TextureUV>;
400
+ };
401
+
402
+ /** How far a distortion pushes the map's UV at most when `strength` is left out, in UV units. */
403
+ export const UV_DISTORTION_DEFAULT_STRENGTH = 0.05;
404
+
405
+ /** A complete distortion from a partial one, or undefined when unset or without a map. */
406
+ export function normalizeUVDistortion(
407
+ distortion: ParticleUVDistortionOptions | null | undefined
408
+ ): ParticleUVDistortion | undefined {
409
+ if (!distortion || typeof distortion !== 'object' || !distortion.map) {
410
+ return undefined;
411
+ }
412
+ const strength = distortion.strength;
413
+ return {
414
+ map: distortion.map,
415
+ mapUV: normalizeTextureUV(distortion.mapUV, UV_DISTORTION_DEFAULT_UV),
416
+ strength: Math.max(
417
+ 0,
418
+ typeof strength === 'number' && Number.isFinite(strength) ? strength : UV_DISTORTION_DEFAULT_STRENGTH
419
+ ),
420
+ };
421
+ }
422
+
423
+ /** True when both describe the same distortion (or both none). */
424
+ export function uvDistortionEqual(a: ParticleUVDistortion | undefined, b: ParticleUVDistortion | undefined): boolean {
425
+ if (a === b) {
426
+ return true;
427
+ }
428
+ if (!a || !b) {
429
+ return false;
430
+ }
431
+ return a.map === b.map && a.strength === b.strength && textureUVEqual(a.mapUV, b.mapUV);
432
+ }
433
+
434
+ /**
435
+ * The main map's layout, or undefined when it is the identity (channel 0,
436
+ * untiled, unmoved, still), which costs nothing in the shader.
437
+ */
438
+ export function normalizeMapUV(uv: Partial<TextureUV> | null | undefined): TextureUV | undefined {
439
+ if (!uv || typeof uv !== 'object') {
440
+ return undefined;
441
+ }
442
+ const normalized = normalizeTextureUV(uv);
443
+ return textureUVToJSON(normalized) === undefined ? undefined : normalized;
444
+ }
445
+
446
+ /** True when both lay the map the same way (or both as drawn). */
447
+ export function mapUVEqual(a: TextureUV | undefined, b: TextureUV | undefined): boolean {
448
+ return a === b || (!!a && !!b && textureUVEqual(a, b));
449
+ }
450
+
451
+ /**
452
+ * True when a texture effect of `settings` samples UV channel 1, see
453
+ * {@link UVChannel}: the map, the distortion noise or an erosion mask laid
454
+ * on it.
455
+ */
456
+ export function readsUV1(settings: {
457
+ mapUV?: TextureUV;
458
+ uvDistortion?: ParticleUVDistortion;
459
+ erosion?: ParticleErosion;
460
+ }): boolean {
461
+ const {mapUV, uvDistortion, erosion} = settings;
462
+ return (
463
+ mapUV?.channel === 1 || uvDistortion?.mapUV.channel === 1 || (!!erosion?.mask && erosion.maskUV.channel === 1)
464
+ );
465
+ }
466
+
467
+ /**
468
+ * A batch's copy of the {@link VFXBatchSettings} its systems share: every optional field
469
+ * defaulted, and every field the batch's render mode ignores at its default (an opaque batch's
470
+ * `materialBlendMode` reads `ALPHA_DISABLE`, a billboard batch's `rim` is unset). The renderer
471
+ * adds a system to the batch whose settings match its own.
472
+ */
473
+ export interface StoredBatchSettings {
474
+ /** See {@link VFXBatchSettings.instancingGeometry}. */
475
+ instancingGeometry: Float32Array;
476
+ /** See {@link VFXBatchSettings.instancingIndices}. */
477
+ instancingIndices: Uint32Array | Uint16Array;
478
+ /** See {@link VFXBatchSettings.instancingUVs}. */
479
+ instancingUVs?: Float32Array;
480
+ /** The mesh's second UV set, see {@link UVChannel}. */
481
+ instancingUVs2?: Float32Array;
482
+ /** See {@link VFXBatchSettings.instancingNormals}. */
483
+ instancingNormals?: Float32Array;
484
+ /** See {@link VFXBatchSettings.renderMode}. */
485
+ renderMode: RenderMode;
486
+ /** See {@link VFXBatchSettings.renderOrder}. */
487
+ renderOrder: number;
488
+ /** See {@link VFXBatchSettings.uTileCount}. */
489
+ uTileCount: number;
490
+ /** See {@link VFXBatchSettings.vTileCount}. */
491
+ vTileCount: number;
492
+ /** See {@link VFXBatchSettings.blendTiles}. */
493
+ blendTiles: boolean;
494
+ /** See {@link VFXBatchSettings.softParticles}. */
495
+ softParticles: boolean;
496
+ /** See {@link VFXBatchSettings.softNearFade}. */
497
+ softNearFade: number;
498
+ /** See {@link VFXBatchSettings.softFarFade}. */
499
+ softFarFade: number;
500
+ /** See {@link VFXBatchSettings.materialBlendMode}. */
501
+ materialBlendMode: number;
502
+ /** See {@link VFXBatchSettings.materialTransparent}. */
503
+ materialTransparent: boolean;
504
+ /** See {@link VFXBatchSettings.materialDepthTest}. */
505
+ materialDepthTest: boolean;
506
+ /** See {@link VFXBatchSettings.materialDepthWrite}. */
507
+ materialDepthWrite: boolean;
508
+ /** See {@link VFXBatchSettings.materialAlphaTest}. */
509
+ materialAlphaTest: number;
510
+ /** See {@link VFXBatchSettings.texture}. */
511
+ texture: BaseTexture | null;
512
+ /** See {@link VFXBatchSettings.reflectionTexture}. */
513
+ reflectionTexture: BaseTexture | null;
514
+ /** See {@link VFXBatchSettings.reflectionLevel}. */
515
+ reflectionLevel: number;
516
+ /** See {@link VFXBatchSettings.reflectionAtlas}. */
517
+ reflectionAtlas: BaseTexture | null;
518
+ /** See {@link VFXBatchSettings.layerMask}. */
519
+ layerMask: number;
520
+ /** Babylon rendering group the batch mesh draws in, see {@link VFXBatchSettings.renderingGroupId}. */
521
+ renderingGroupId: number;
522
+ /** Whether the scene's fog is applied, see {@link VFXBatchSettings.applyFog}. */
523
+ applyFog: boolean;
524
+ /** Billboard pivot in particle-size units, see {@link VFXBatchSettings.pivot}. */
525
+ pivot?: Vector3;
526
+ /** Draw order within the batch, see {@link SortMode}. */
527
+ sortMode: SortMode;
528
+ /** Which faces a mesh particle draws, see {@link RenderFace}; `'both'` outside the Mesh render mode. */
529
+ renderFace: RenderFace;
530
+ /** Smallest size a particle may have on screen, see {@link VFXBatchSettings.minParticleSize}. */
531
+ minParticleSize: number;
532
+ /** Largest size a particle may have on screen, see {@link VFXBatchSettings.maxParticleSize}. */
533
+ maxParticleSize: number;
534
+ /** Mesh particle lighting, see {@link VFXBatchSettings.lighting}. */
535
+ lighting?: MeshLighting;
536
+ /** Mesh particle rim, see {@link VFXBatchSettings.rim}. */
537
+ rim?: MeshRim;
538
+ /** The main map's layout, see {@link VFXBatchSettings.mapUV}. */
539
+ mapUV?: TextureUV;
540
+ /** Map UV distortion, see {@link VFXBatchSettings.uvDistortion}. */
541
+ uvDistortion?: ParticleUVDistortion;
542
+ /** Colour ramp for the map, see {@link VFXBatchSettings.gradientMap}. */
543
+ gradientMap?: ParticleGradientMap;
544
+ /** Alpha erosion, see {@link VFXBatchSettings.erosion}. */
545
+ erosion?: ParticleErosion;
546
+ }