@driftengine/core 4.8.5 → 4.8.6

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 (136) hide show
  1. package/CHANGELOG.json +4321 -4250
  2. package/dist/index.d.ts +10 -2
  3. package/dist/index.js +11 -1
  4. package/dist/render/ambientHarmonics.d.ts +46 -0
  5. package/dist/render/ambientHarmonics.js +80 -0
  6. package/dist/render/areaLights.d.ts +19 -0
  7. package/dist/render/areaLights.js +20 -0
  8. package/dist/render/backend/api.d.ts +13 -2
  9. package/dist/render/backend/webgl2/instanced.d.ts +2 -0
  10. package/dist/render/backend/webgl2/instanced.js +11 -2
  11. package/dist/render/backend/webgl2/renderer.d.ts +74 -1
  12. package/dist/render/backend/webgl2/renderer.js +229 -37
  13. package/dist/render/backend/webgl2/skinScatter.d.ts +14 -0
  14. package/dist/render/backend/webgl2/skinScatter.js +88 -26
  15. package/dist/render/backend/webgpu/buffers.js +2 -1
  16. package/dist/render/backend/webgpu/clusterBinner.js +3 -2
  17. package/dist/render/backend/webgpu/flatPass.d.ts +7 -5
  18. package/dist/render/backend/webgpu/flatPass.js +15 -13
  19. package/dist/render/backend/webgpu/particlePass.d.ts +37 -4
  20. package/dist/render/backend/webgpu/particlePass.js +80 -12
  21. package/dist/render/backend/webgpu/postPass.d.ts +6 -1
  22. package/dist/render/backend/webgpu/postPass.js +7 -2
  23. package/dist/render/backend/webgpu/renderer.d.ts +58 -7
  24. package/dist/render/backend/webgpu/renderer.js +329 -78
  25. package/dist/render/backend/webgpu/shaders/clusterBin.wgsl.d.ts +1 -1
  26. package/dist/render/backend/webgpu/shaders/clusterBin.wgsl.js +41 -1
  27. package/dist/render/backend/webgpu/skinPaletteRing.d.ts +40 -28
  28. package/dist/render/backend/webgpu/skinPaletteRing.js +69 -32
  29. package/dist/render/backend/webgpu/skinScatterPass.d.ts +10 -2
  30. package/dist/render/backend/webgpu/skinScatterPass.js +59 -9
  31. package/dist/render/backend/webgpu/surfaceTexturePass.d.ts +2 -1
  32. package/dist/render/backend/webgpu/surfaceTexturePass.js +18 -4
  33. package/dist/render/bloomChain.d.ts +44 -0
  34. package/dist/render/bloomChain.js +28 -0
  35. package/dist/render/bloomPass.d.ts +3 -1
  36. package/dist/render/bloomPass.js +16 -2
  37. package/dist/render/clusteredLights.d.ts +55 -1
  38. package/dist/render/clusteredLights.js +182 -8
  39. package/dist/render/fixtureAtlas.js +1 -1
  40. package/dist/render/instanceCull.js +7 -0
  41. package/dist/render/instances.d.ts +32 -10
  42. package/dist/render/instances.js +17 -8
  43. package/dist/render/lightBudget.d.ts +26 -1
  44. package/dist/render/lightBudget.js +26 -3
  45. package/dist/render/lightmap.d.ts +102 -0
  46. package/dist/render/lightmap.js +118 -0
  47. package/dist/render/mesh.js +2 -1
  48. package/dist/render/particleBatch.d.ts +59 -3
  49. package/dist/render/particleBatch.js +53 -2
  50. package/dist/render/particleMaterial.d.ts +6 -1
  51. package/dist/render/particleMaterial.js +10 -2
  52. package/dist/render/particlePool.d.ts +11 -0
  53. package/dist/render/particlePool.js +2 -0
  54. package/dist/render/particleSort.d.ts +34 -0
  55. package/dist/render/particleSort.js +96 -0
  56. package/dist/render/sceneTarget.d.ts +5 -0
  57. package/dist/render/sceneTarget.js +8 -1
  58. package/dist/render/shaders/bloom.d.ts +8 -4
  59. package/dist/render/shaders/bloom.js +20 -3
  60. package/dist/render/shaders/flat/areaLight.d.ts +10 -0
  61. package/dist/render/shaders/flat/areaLight.js +96 -0
  62. package/dist/render/shaders/flat/index.js +30 -4
  63. package/dist/render/shaders/flat/main.d.ts +1 -1
  64. package/dist/render/shaders/flat/main.js +107 -105
  65. package/dist/render/shaders/flat/models.d.ts +16 -4
  66. package/dist/render/shaders/flat/models.js +64 -4
  67. package/dist/render/shaders/flat/normalMap.d.ts +10 -0
  68. package/dist/render/shaders/flat/normalMap.js +25 -0
  69. package/dist/render/shaders/flat/preamble.js +74 -37
  70. package/dist/render/shaders/flat/skin.js +6 -5
  71. package/dist/render/shaders/generated/bloom.wgsl.d.ts +13 -3
  72. package/dist/render/shaders/generated/bloom.wgsl.js +13 -3
  73. package/dist/render/shaders/generated/flat.wgsl.d.ts +1631 -2044
  74. package/dist/render/shaders/generated/flat.wgsl.js +1669 -2082
  75. package/dist/render/shaders/generated/particle.wgsl.d.ts +115 -1
  76. package/dist/render/shaders/generated/particle.wgsl.js +115 -1
  77. package/dist/render/shaders/generated/skinBlur.wgsl.d.ts +12 -2
  78. package/dist/render/shaders/generated/skinBlur.wgsl.js +12 -2
  79. package/dist/render/shaders/particle.d.ts +17 -1
  80. package/dist/render/shaders/particle.js +133 -1
  81. package/dist/render/shaders/skinBlur.js +25 -3
  82. package/dist/render/skinBlur.d.ts +8 -5
  83. package/dist/render/skinBlur.js +8 -5
  84. package/dist/render/surfaceModel.d.ts +21 -1
  85. package/dist/render/surfaceModel.js +22 -0
  86. package/dist/render/surfaceTexture.d.ts +14 -1
  87. package/dist/render/surfaceTexture.js +15 -2
  88. package/package.json +6 -6
  89. package/src/index.ts +20 -1
  90. package/src/render/ambientHarmonics.ts +92 -0
  91. package/src/render/areaLights.ts +39 -0
  92. package/src/render/backend/api.ts +14 -1
  93. package/src/render/backend/webgl2/instanced.ts +12 -2
  94. package/src/render/backend/webgl2/renderer.ts +292 -35
  95. package/src/render/backend/webgl2/skinScatter.ts +120 -43
  96. package/src/render/backend/webgpu/buffers.ts +2 -1
  97. package/src/render/backend/webgpu/clusterBinner.ts +3 -1
  98. package/src/render/backend/webgpu/flatPass.ts +18 -13
  99. package/src/render/backend/webgpu/particlePass.ts +118 -12
  100. package/src/render/backend/webgpu/postPass.ts +7 -2
  101. package/src/render/backend/webgpu/renderer.ts +396 -95
  102. package/src/render/backend/webgpu/shaders/clusterBin.wgsl.ts +41 -1
  103. package/src/render/backend/webgpu/skinPaletteRing.ts +65 -33
  104. package/src/render/backend/webgpu/skinScatterPass.ts +59 -9
  105. package/src/render/backend/webgpu/surfaceTexturePass.ts +26 -5
  106. package/src/render/bloomChain.ts +69 -0
  107. package/src/render/bloomPass.ts +26 -2
  108. package/src/render/clusteredLights.ts +221 -7
  109. package/src/render/fixtureAtlas.ts +1 -1
  110. package/src/render/instanceCull.ts +5 -0
  111. package/src/render/instances.ts +40 -11
  112. package/src/render/lightBudget.ts +48 -2
  113. package/src/render/lightmap.ts +202 -0
  114. package/src/render/mesh.ts +2 -1
  115. package/src/render/particleBatch.ts +95 -4
  116. package/src/render/particleMaterial.ts +11 -1
  117. package/src/render/particlePool.ts +13 -0
  118. package/src/render/particleSort.ts +133 -0
  119. package/src/render/sceneTarget.ts +18 -1
  120. package/src/render/shaders/bloom.ts +20 -3
  121. package/src/render/shaders/flat/areaLight.ts +96 -0
  122. package/src/render/shaders/flat/index.ts +30 -4
  123. package/src/render/shaders/flat/main.ts +107 -105
  124. package/src/render/shaders/flat/models.ts +73 -8
  125. package/src/render/shaders/flat/normalMap.ts +25 -0
  126. package/src/render/shaders/flat/preamble.ts +74 -37
  127. package/src/render/shaders/flat/skin.ts +6 -5
  128. package/src/render/shaders/generated/bloom.wgsl.ts +13 -3
  129. package/src/render/shaders/generated/flat.wgsl.ts +1669 -2082
  130. package/src/render/shaders/generated/particle.wgsl.ts +116 -1
  131. package/src/render/shaders/generated/skinBlur.wgsl.ts +12 -2
  132. package/src/render/shaders/particle.ts +134 -1
  133. package/src/render/shaders/skinBlur.ts +25 -3
  134. package/src/render/skinBlur.ts +8 -5
  135. package/src/render/surfaceModel.ts +36 -1
  136. package/src/render/surfaceTexture.ts +41 -3
@@ -9,25 +9,27 @@ export function createMeshInstances(capacity) {
9
9
  return {
10
10
  models: new Float32Array(capacity * 16),
11
11
  tints: new Float32Array(capacity * 3),
12
+ alphas: new Float32Array(capacity).fill(1),
12
13
  capacity,
13
14
  count: 0,
14
15
  };
15
16
  }
16
17
  /**
17
18
  * Floats one instance occupies in the interleaved vertex buffer: sixteen of matrix, three of
18
- * tint, and one of padding.
19
+ * tint, and the opacity.
19
20
  *
20
- * **Padded to twenty so the stride is 80 bytes rather than 76.** A vertex buffer's stride must be
21
- * a multiple of four on both backends, which 76 already is — the padding is for the *attribute*
22
- * offsets: the tint sits at byte 64 and a three-float attribute ending at 76 leaves the next
23
- * instance's first column starting there, which is legal but puts every second instance on an
24
- * offset no driver aligns well. One wasted float an instance is 4 bytes against 76.
21
+ * **Twenty so the stride is 80 bytes rather than 76.** A vertex buffer's stride must be a multiple
22
+ * of four on both backends, which 76 already is — the twentieth float was padding for the
23
+ * *attribute* offsets: the tint sits at byte 64 and a three-float attribute ending at 76 leaves the
24
+ * next instance's first column starting there, which is legal but puts every second instance on an
25
+ * offset no driver aligns well. Since 4.8.6 it carries the instance's opacity, read with the tint
26
+ * as one four-float attribute, so the opacity cost no attribute and no byte.
25
27
  */
26
28
  export const INSTANCE_FLOATS = 20;
27
29
  /** Bytes one instance occupies. See `INSTANCE_FLOATS`. */
28
30
  export const INSTANCE_STRIDE = INSTANCE_FLOATS * 4;
29
31
  /**
30
- * Interleave `count` instances into `out`, matrix then tint, at `INSTANCE_FLOATS` apart.
32
+ * Interleave `count` instances into `out`, matrix, tint and opacity, at `INSTANCE_FLOATS` apart.
31
33
  *
32
34
  * **Interleaved rather than two buffers** so a batch's upload is one `writeBuffer` and an
33
35
  * instance's colour sits beside the matrix that places it. Shared by both backends, because
@@ -37,16 +39,23 @@ export const INSTANCE_STRIDE = INSTANCE_FLOATS * 4;
37
39
  * Writes nothing beyond `count`, and allocates nothing: `out` is the caller's staging array.
38
40
  */
39
41
  export function packInstances(instances, out) {
40
- const { models, tints } = instances;
42
+ const { models, tints, alphas, lightmapRegions } = instances;
41
43
  const count = Math.min(instances.count, instances.capacity);
42
44
  for (let i = 0; i < count; i += 1) {
43
45
  const at = i * INSTANCE_FLOATS;
44
46
  const m = i * 16;
45
47
  for (let c = 0; c < 16; c += 1)
46
48
  out[at + c] = models[m + c];
49
+ if (lightmapRegions !== undefined) {
50
+ /* The region in the tint's and the opacity's place. See `lightmapRegions`. */
51
+ for (let c = 0; c < 4; c += 1)
52
+ out[at + 16 + c] = lightmapRegions[i * 4 + c];
53
+ continue;
54
+ }
47
55
  const t = i * 3;
48
56
  out[at + 16] = tints[t];
49
57
  out[at + 17] = tints[t + 1];
50
58
  out[at + 18] = tints[t + 2];
59
+ out[at + 19] = alphas?.[i] ?? 1;
51
60
  }
52
61
  }
@@ -229,12 +229,21 @@ export declare const CLOTH_REST_TEXTURE_UNIT: number;
229
229
  */
230
230
  export declare const MODEL_MAP_TEXTURE_UNIT: number;
231
231
  /**
232
- * The two units skin's screen-space blur reads on WebGL2 — its target and the frame's depth — past
232
+ * The units skin's screen-space blur reads on WebGL2 — its target and the frame's depth — past
233
233
  * every unit a lit program binds, so spreading skin in the middle of a frame unbinds nothing the
234
234
  * draws after it sample. WebGL2 guarantees thirty-two.
235
235
  */
236
236
  export declare const SKIN_BLUR_TEXTURE_UNIT: number;
237
237
  export declare const SKIN_BLUR_DEPTH_UNIT: number;
238
+ /** And the skin's colour, which the blur's second axis multiplies the spread light by. */
239
+ export declare const SKIN_BLUR_ALBEDO_UNIT: number;
240
+ /**
241
+ * A sprite particle's image and the frame's depth copy its soft edge reads, on WebGL2: past every
242
+ * unit a lit program binds, for the skin blur's reason — a particle is drawn in the middle of a
243
+ * frame, and the draws after it must find their textures where they left them.
244
+ */
245
+ export declare const PARTICLE_SPRITE_UNIT: number;
246
+ export declare const PARTICLE_DEPTH_UNIT: number;
238
247
  /**
239
248
  * A cookie's tile, in texels a side.
240
249
  *
@@ -349,6 +358,16 @@ export interface PointLightSet {
349
358
  * drawn for one spot works in another with a different angle.
350
359
  */
351
360
  readonly lightCookies?: Float32Array;
361
+ /**
362
+ * One per light: the lighting channels it lights, as a bitmask in a float — 1, 2, 3 … 255.
363
+ * **Absent, or a short array, is channel 1 for every light**, which is every surface's default
364
+ * too, so a scene that never names a channel lights everything with everything as it did.
365
+ *
366
+ * A light shades a surface only where its mask and the surface's `SurfaceMaterial.lightChannels`
367
+ * share a bit. What it is for: a rig of lights that belongs to some draws and not others — a
368
+ * character's own key and rim lights, which the floor around the character must not take.
369
+ */
370
+ readonly lightChannels?: Float32Array;
352
371
  }
353
372
  /**
354
373
  * A cone that admits every direction, for every light.
@@ -416,6 +435,12 @@ export interface ResolvedPointLights {
416
435
  iesProfiles: Float32Array;
417
436
  iesAxes: Float32Array;
418
437
  cookies: Float32Array;
438
+ /**
439
+ * Owned, four a light: the photometric azimuth in xyz and the light's channels in w, which is how
440
+ * `uLightIesAxis` carries them — the channels in a lane the axis left, rather than in a uniform
441
+ * array of their own, which would cost a row a light of a budget that has none to give.
442
+ */
443
+ readonly axesAndChannels: Float32Array;
419
444
  /**
420
445
  * Owned, `MAX_POINT_LIGHTS` long: the emitter sizes and weights as the froxel record carries
421
446
  * them, at half precision. `sourceRadii` and `weights` point here once a light set resolves, so
@@ -1,4 +1,4 @@
1
- import { POINT_LIGHT_COS_INNER, POINT_LIGHT_COS_OUTER, lampSourceRadius, lampWeight, } from './clusteredLights.js';
1
+ import { POINT_LIGHT_COS_INNER, POINT_LIGHT_COS_OUTER, lampSourceRadius, lampWeight, lightChannelsOf, } from './clusteredLights.js';
2
2
  /**
3
3
  * How many point lights the shading pass shades against at once.
4
4
  *
@@ -227,12 +227,21 @@ export const CLOTH_REST_TEXTURE_UNIT = CLOTH_PARTICLES_TEXTURE_UNIT + 1;
227
227
  */
228
228
  export const MODEL_MAP_TEXTURE_UNIT = CLOTH_REST_TEXTURE_UNIT + 1;
229
229
  /**
230
- * The two units skin's screen-space blur reads on WebGL2 — its target and the frame's depth — past
230
+ * The units skin's screen-space blur reads on WebGL2 — its target and the frame's depth — past
231
231
  * every unit a lit program binds, so spreading skin in the middle of a frame unbinds nothing the
232
232
  * draws after it sample. WebGL2 guarantees thirty-two.
233
233
  */
234
234
  export const SKIN_BLUR_TEXTURE_UNIT = MODEL_MAP_TEXTURE_UNIT + 1;
235
235
  export const SKIN_BLUR_DEPTH_UNIT = SKIN_BLUR_TEXTURE_UNIT + 1;
236
+ /** And the skin's colour, which the blur's second axis multiplies the spread light by. */
237
+ export const SKIN_BLUR_ALBEDO_UNIT = SKIN_BLUR_DEPTH_UNIT + 1;
238
+ /**
239
+ * A sprite particle's image and the frame's depth copy its soft edge reads, on WebGL2: past every
240
+ * unit a lit program binds, for the skin blur's reason — a particle is drawn in the middle of a
241
+ * frame, and the draws after it must find their textures where they left them.
242
+ */
243
+ export const PARTICLE_SPRITE_UNIT = SKIN_BLUR_ALBEDO_UNIT + 1;
244
+ export const PARTICLE_DEPTH_UNIT = PARTICLE_SPRITE_UNIT + 1;
236
245
  /**
237
246
  * A cookie's tile, in texels a side.
238
247
  *
@@ -387,7 +396,8 @@ export function bindPointLights(gl, uniforms, lights, falloff) {
387
396
  gl.uniform3fv(uniforms['uLightDir[0]'] ?? null, r.directions);
388
397
  gl.uniform2fv(uniforms['uLightCone[0]'] ?? null, r.coneCos);
389
398
  gl.uniform1fv(uniforms['uLightIesProfile[0]'] ?? null, r.iesProfiles);
390
- gl.uniform3fv(uniforms['uLightIesAxis[0]'] ?? null, r.iesAxes);
399
+ /* The azimuth and, in w, the channels. See `axesAndChannels`. */
400
+ gl.uniform4fv(uniforms['uLightIesAxis[0]'] ?? null, r.axesAndChannels);
391
401
  gl.uniform1fv(uniforms['uLightCookie[0]'] ?? null, r.cookies);
392
402
  }
393
403
  /**
@@ -452,6 +462,7 @@ const glLights = {
452
462
  iesProfiles: NO_PROFILES,
453
463
  iesAxes: NO_IES_AXES,
454
464
  cookies: NO_COOKIES,
465
+ axesAndChannels: new Float32Array(MAX_POINT_LIGHTS * 4),
455
466
  heldSourceRadii: new Float32Array(MAX_POINT_LIGHTS),
456
467
  heldWeights: new Float32Array(MAX_POINT_LIGHTS),
457
468
  };
@@ -481,6 +492,7 @@ export function resolvePointLights(lights, falloff, out) {
481
492
  out.iesProfiles = NO_PROFILES;
482
493
  out.iesAxes = NO_IES_AXES;
483
494
  out.cookies = NO_COOKIES;
495
+ packAxesAndChannels(NO_IES_AXES, undefined, out.axesAndChannels);
484
496
  return out;
485
497
  }
486
498
  /*
@@ -561,5 +573,16 @@ export function resolvePointLights(lights, falloff, out) {
561
573
  out.iesProfiles = iesProfiles;
562
574
  out.iesAxes = iesAxes;
563
575
  out.cookies = cookies;
576
+ packAxesAndChannels(iesAxes, lights.lightChannels, out.axesAndChannels);
564
577
  return out;
565
578
  }
579
+ /** The azimuth and the channels, four a light, as `uLightIesAxis` holds them. */
580
+ function packAxesAndChannels(axes, channels, out) {
581
+ const named = (channels?.length ?? 0) >= MAX_POINT_LIGHTS ? channels : undefined;
582
+ for (let light = 0; light < MAX_POINT_LIGHTS; light++) {
583
+ out[light * 4] = axes[light * 3] ?? 0;
584
+ out[light * 4 + 1] = axes[light * 3 + 1] ?? 0;
585
+ out[light * 4 + 2] = axes[light * 3 + 2] ?? 0;
586
+ out[light * 4 + 3] = lightChannelsOf(named?.[light]);
587
+ }
588
+ }
@@ -0,0 +1,102 @@
1
+ /**
2
+ * A baked lightmap: what a consumer hands over, how both backends lay it out, and what a surface
3
+ * reads from it — the backend-neutral half of `createLightmap` and `lightmapModel`, by the
4
+ * 2026-08-13 rule.
5
+ *
6
+ * **What it is for.** A stage built in another engine carries its static light baked: the bounce
7
+ * off its walls, the soft occlusion in its corners, most of the colour a reference render shows.
8
+ * The dynamic lights alone leave everything they do not reach flat. A consumer decodes the bake
9
+ * into pages — one irradiance image and one direction image each — and every lightmapped surface
10
+ * reads its own region of its page, at its second texture coordinates scaled and offset into it.
11
+ *
12
+ * **What a surface adds**, for its shading normal `n` (the normal map's, where it has one):
13
+ *
14
+ * d = direction * 2 - 1
15
+ * light = irradiance(uv) * max(0, dot(d.xyz, n) + d.w)
16
+ * diffuse += albedo * (1 - metal) * light
17
+ *
18
+ * Unreal's high-quality lightmap read, `GetLightMapColorHQ`, once its own vectors have decoded the
19
+ * page — the first-order spherical harmonic its direction carries, in this engine's axes. **Added
20
+ * to what the dynamic lights give**, and to the ambient: a consumer whose bake already holds the
21
+ * sky's light zeroes the ambient for those draws itself, with `setAmbientSH` and nine zeros.
22
+ *
23
+ * **A shading model, because everything it would otherwise take is spent.** The page binds where a
24
+ * model's map does and the region is the model's numbers (`lightmapModel`), so a lightmapped
25
+ * material is a pipeline constant of its own and nothing else pays for it; the surface is otherwise
26
+ * the standard model, unchanged. The second coordinates ride the grain and relief attributes
27
+ * (`MeshData.lightmapUvs`, stored below zero by `withLightmapUvs`), and an instance's region rides
28
+ * its tint and opacity (`MeshInstances.lightmapRegions`). **What it gives up**: a lightmapped
29
+ * surface has no procedural grain or relief, no other shading model, and an instanced batch with
30
+ * regions no tint. **What it does not do**: the GPU-driven pipeline and splats read no page.
31
+ */
32
+ import type { MeshData } from '@driftengine/drft';
33
+ /** One page as a consumer decoded it. */
34
+ export interface LightmapPage {
35
+ readonly width: number;
36
+ readonly height: number;
37
+ /**
38
+ * Linear irradiance, three floats a texel, row-major from the top, in the same units every other
39
+ * light here is in — a bake from another engine scaled by that engine's exposure, as its lights are.
40
+ */
41
+ readonly irradiance: Float32Array;
42
+ /**
43
+ * The direction the light arrives from, four bytes a texel: a first-order spherical harmonic in
44
+ * this engine's world axes, each value stored as `v * 0.5 + 0.5` — xyz the axis, w the constant.
45
+ */
46
+ readonly direction: Uint8Array;
47
+ }
48
+ /** Layers a page is uploaded as: the irradiance, then the direction. */
49
+ export declare const LIGHTMAP_LAYERS = 2;
50
+ /**
51
+ * Where a surface's region of its page is: `lightmapUv = uv2 * scale + bias`, as
52
+ * `[scaleU, scaleV, biasU, biasV]`. The whole page is the identity.
53
+ */
54
+ export type LightmapRegion = readonly [number, number, number, number];
55
+ /** The whole page. */
56
+ export declare const WHOLE_PAGE: LightmapRegion;
57
+ /** A page as both backends upload it: `layers` images of half-float RGBA, row-major from the top. */
58
+ export interface LightmapTexels {
59
+ readonly kind: 'lightmap-texels';
60
+ readonly width: number;
61
+ readonly height: number;
62
+ readonly layers: number;
63
+ readonly texels: Uint16Array;
64
+ }
65
+ /** Whether a surface texture's source is a page's texels rather than an image or blocks. */
66
+ export declare function isLightmapTexels(source: unknown): source is LightmapTexels;
67
+ /**
68
+ * A page as both backends upload it: two layers of half-float RGBA, the irradiance with an alpha of
69
+ * one and the direction as the fractions its bytes are. Refuses a page whose arrays are not its
70
+ * size, naming the one that is short.
71
+ */
72
+ export declare function lightmapTexels(page: LightmapPage): LightmapTexels;
73
+ /**
74
+ * A mesh with its second coordinates moved onto the grain and relief streams they ride, or the
75
+ * mesh itself where it has none. Called by both backends' `createMesh`, before anything records
76
+ * which streams the mesh has.
77
+ *
78
+ * **Stored as `-1 - uv`, below zero, and that is what keeps the lanes safe to share.** Grain takes
79
+ * effect only above zero and relief is read through `max(relief, 0)`, so the same mesh drawn by a
80
+ * material that is not lightmapped is a mesh with no grain and no relief — rather than one mottled
81
+ * and bumped by its own coordinates, which is what storing them as they are drew. **What it gives
82
+ * up** is coordinates at or below −1, which read as nothing; a page's coordinates are from 0 to 1.
83
+ */
84
+ export declare function withLightmapUvs(data: MeshData): MeshData;
85
+ /** A material's region, checked: four finite numbers, the whole page where none was named. */
86
+ export declare function lightmapRegionOf(region: LightmapRegion | undefined): LightmapRegion;
87
+ /**
88
+ * Says once that a lightmapped material was set with no page. The lit stage adds nothing for one,
89
+ * which is the defined state; this is what keeps it from also being a silent one.
90
+ */
91
+ export declare function warnUnpagedLightmap(model: {
92
+ readonly kind: string;
93
+ } | null | undefined, map: unknown): void;
94
+ /**
95
+ * Says once that an instanced batch and its material disagree about lightmap regions: a batch with
96
+ * regions drawn by a material that is not lightmapped, or a lightmapped one drawn without them. The
97
+ * regions ride the lane a tint and an opacity do (`MeshInstances.lightmapRegions`), so either draw
98
+ * reads one as the other: drawn, and wrong, which is why it is said rather than left.
99
+ */
100
+ export declare function warnRegionsWithoutPage(model: {
101
+ readonly kind: string;
102
+ } | null | undefined, regions: Float32Array | undefined): void;
@@ -0,0 +1,118 @@
1
+ import { toHalfFloats } from './halfFloat.js';
2
+ /** Layers a page is uploaded as: the irradiance, then the direction. */
3
+ export const LIGHTMAP_LAYERS = 2;
4
+ /** The whole page. */
5
+ export const WHOLE_PAGE = [1, 1, 0, 0];
6
+ /** Whether a surface texture's source is a page's texels rather than an image or blocks. */
7
+ export function isLightmapTexels(source) {
8
+ return (typeof source === 'object' &&
9
+ source !== null &&
10
+ source.kind === 'lightmap-texels');
11
+ }
12
+ /**
13
+ * A page as both backends upload it: two layers of half-float RGBA, the irradiance with an alpha of
14
+ * one and the direction as the fractions its bytes are. Refuses a page whose arrays are not its
15
+ * size, naming the one that is short.
16
+ */
17
+ export function lightmapTexels(page) {
18
+ const { width, height } = page;
19
+ if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1) {
20
+ throw new Error(`createLightmap: a ${width} by ${height} page has nothing in it`);
21
+ }
22
+ const texels = width * height;
23
+ if (page.irradiance.length !== texels * 3) {
24
+ throw new Error(`createLightmap: ${page.irradiance.length} irradiance floats for a ${width} by ${height} ` +
25
+ `page, which wants ${texels * 3}: three a texel`);
26
+ }
27
+ if (page.direction.length !== texels * 4) {
28
+ throw new Error(`createLightmap: ${page.direction.length} direction bytes for a ${width} by ${height} ` +
29
+ `page, which wants ${texels * 4}: four a texel`);
30
+ }
31
+ const floats = new Float32Array(texels * 4 * LIGHTMAP_LAYERS);
32
+ for (let t = 0; t < texels; t++) {
33
+ floats[t * 4] = page.irradiance[t * 3];
34
+ floats[t * 4 + 1] = page.irradiance[t * 3 + 1];
35
+ floats[t * 4 + 2] = page.irradiance[t * 3 + 2];
36
+ floats[t * 4 + 3] = 1;
37
+ const at = (texels + t) * 4;
38
+ for (let c = 0; c < 4; c++)
39
+ floats[at + c] = page.direction[t * 4 + c] / 255;
40
+ }
41
+ return {
42
+ kind: 'lightmap-texels',
43
+ width,
44
+ height,
45
+ layers: LIGHTMAP_LAYERS,
46
+ texels: toHalfFloats(floats),
47
+ };
48
+ }
49
+ /**
50
+ * A mesh with its second coordinates moved onto the grain and relief streams they ride, or the
51
+ * mesh itself where it has none. Called by both backends' `createMesh`, before anything records
52
+ * which streams the mesh has.
53
+ *
54
+ * **Stored as `-1 - uv`, below zero, and that is what keeps the lanes safe to share.** Grain takes
55
+ * effect only above zero and relief is read through `max(relief, 0)`, so the same mesh drawn by a
56
+ * material that is not lightmapped is a mesh with no grain and no relief — rather than one mottled
57
+ * and bumped by its own coordinates, which is what storing them as they are drew. **What it gives
58
+ * up** is coordinates at or below −1, which read as nothing; a page's coordinates are from 0 to 1.
59
+ */
60
+ export function withLightmapUvs(data) {
61
+ const uvs = data.lightmapUvs;
62
+ if (uvs === undefined)
63
+ return data;
64
+ const vertices = uvs.length / 2;
65
+ const grain = new Float32Array(vertices);
66
+ const relief = new Float32Array(vertices);
67
+ for (let v = 0; v < vertices; v++) {
68
+ grain[v] = -1 - uvs[v * 2];
69
+ relief[v] = -1 - uvs[v * 2 + 1];
70
+ }
71
+ return { ...data, grain, relief, lightmapUvs: undefined };
72
+ }
73
+ /** A material's region, checked: four finite numbers, the whole page where none was named. */
74
+ export function lightmapRegionOf(region) {
75
+ if (region === undefined)
76
+ return WHOLE_PAGE;
77
+ let finite = region.length === 4;
78
+ for (let i = 0; i < region.length; i++)
79
+ finite &&= Number.isFinite(region[i]);
80
+ if (!finite) {
81
+ throw new Error('lightmapModel: a region is four numbers, [scaleU, scaleV, biasU, biasV], ' +
82
+ `and was given ${String(region)}`);
83
+ }
84
+ return region;
85
+ }
86
+ let warnedUnpaged = false;
87
+ /**
88
+ * Says once that a lightmapped material was set with no page. The lit stage adds nothing for one,
89
+ * which is the defined state; this is what keeps it from also being a silent one.
90
+ */
91
+ export function warnUnpagedLightmap(model, map) {
92
+ if (warnedUnpaged || model?.kind !== 'lightmap' || (map !== null && map !== undefined))
93
+ return;
94
+ warnedUnpaged = true;
95
+ console.warn('setMaterial: a lightmapModel material with no modelMap reads no page and adds no light. ' +
96
+ 'Pass the page createLightmap returned as its modelMap.');
97
+ }
98
+ let warnedRegions = false;
99
+ /**
100
+ * Says once that an instanced batch and its material disagree about lightmap regions: a batch with
101
+ * regions drawn by a material that is not lightmapped, or a lightmapped one drawn without them. The
102
+ * regions ride the lane a tint and an opacity do (`MeshInstances.lightmapRegions`), so either draw
103
+ * reads one as the other: drawn, and wrong, which is why it is said rather than left.
104
+ */
105
+ export function warnRegionsWithoutPage(model, regions) {
106
+ if (warnedRegions)
107
+ return;
108
+ const lightmapped = model?.kind === 'lightmap';
109
+ if (lightmapped === (regions !== undefined))
110
+ return;
111
+ warnedRegions = true;
112
+ console.warn(lightmapped
113
+ ? 'drawInstanced: a lightmapModel material drawing a batch with no lightmapRegions reads ' +
114
+ 'its tints as regions. Give each instance its region of the page.'
115
+ : 'drawInstanced: a batch with lightmapRegions drawn by a material that is not a ' +
116
+ 'lightmapModel reads its regions as its tints and opacities. Draw it with its page, or ' +
117
+ 'without regions.');
118
+ }
@@ -380,7 +380,8 @@ export class Mesh {
380
380
  gl.vertexAttribDivisor(location, 1);
381
381
  }
382
382
  gl.enableVertexAttribArray(15);
383
- gl.vertexAttribPointer(15, 3, gl.FLOAT, false, stride, 64);
383
+ /* The tint and, in w, the opacity: one attribute for both. See `INSTANCE_FLOATS`. */
384
+ gl.vertexAttribPointer(15, 4, gl.FLOAT, false, stride, 64);
384
385
  gl.vertexAttribDivisor(15, 1);
385
386
  gl.bindVertexArray(null);
386
387
  return vao;
@@ -1,5 +1,13 @@
1
1
  import { type ParticleMaterial } from './particleMaterial.ts';
2
2
  import type { ParticleInstances } from './particlePool.ts';
3
+ import type { SurfaceTexture } from './surfaceTexture.ts';
4
+ /** The vertex stage's `uCameraFacing` for a facing: the cross 0, the camera 1, the travel 2. */
5
+ export declare function facingCode(facing: ParticleFacing): number;
6
+ /**
7
+ * A sprite's frame and half-height, two floats a particle, into `out` — the stream the vertex
8
+ * stage reads at location 10. Shared by both backends, so the two read one layout.
9
+ */
10
+ export declare function packSpriteStream(data: ParticleInstances, count: number, out: Float32Array): void;
3
11
  /**
4
12
  * One draw call for a live particle pool: one camera-facing quad per particle, or a
5
13
  * crossed pair of world-fixed ones when `facing` asks for them.
@@ -21,9 +29,11 @@ export type ParticleBlend = 'additive' | 'alpha';
21
29
  * `'camera'` is one quad facing the viewer, and the default. `'cross'` is the pair of
22
30
  * world-fixed blades this batch drew before the option existed — see `uCameraFacing` in
23
31
  * `shaders/particle.ts` for the artifact that made the default the other one, and for what
24
- * the cross still buys a caller who wants it back.
32
+ * the cross still buys a caller who wants it back. `'velocity'` faces the camera with its up along
33
+ * the particle's travel across the view — a streak, a spark stretched by its motion — and a
34
+ * particle at rest faces the camera as `'camera'` does.
25
35
  */
26
- export type ParticleFacing = 'camera' | 'cross';
36
+ export type ParticleFacing = 'camera' | 'cross' | 'velocity';
27
37
  export interface ParticleBatchOptions {
28
38
  /**
29
39
  * Which particle this is, by name.
@@ -72,6 +82,41 @@ export interface ParticleBatchOptions {
72
82
  * shader's own comment on `uCameraFacing` lays out.
73
83
  */
74
84
  facing?: ParticleFacing;
85
+ /**
86
+ * The image a `'sprite'` draws: a surface texture, its first layer. **Required by the sprite** and
87
+ * ignored by the procedural materials. Colour is the particle's times the image's, unlit.
88
+ */
89
+ texture?: SurfaceTexture;
90
+ /**
91
+ * A `'sprite'`'s flipbook: the image as `[columns, rows]` cells, numbered across then down from
92
+ * the top-left, a particle's `frames` naming one. `[1, 1]`, the default, is the whole image.
93
+ */
94
+ cells?: readonly [number, number];
95
+ /**
96
+ * Whether a `'sprite'` blends from a particle's cell toward the next by the fraction of its
97
+ * frame, so a slow flipbook moves rather than steps. Off by default: one cell, sampled once.
98
+ */
99
+ blendCells?: boolean;
100
+ /**
101
+ * Metres over which a `'sprite'` fades where it meets the opaque scene: the hard line a flat card
102
+ * draws where it cuts a floor or a body, softened. **0, the default, is a hard edge.** It reads a
103
+ * copy of the frame's depth, which needs `screenEffects`; without one the edge stays hard and
104
+ * the renderer says so once.
105
+ */
106
+ softDepth?: number;
107
+ /** Metres in front of the eye over which a `'sprite'` fades in. 0, the default, is no fade. */
108
+ cameraFade?: number;
109
+ /**
110
+ * Metres each particle is moved toward the eye before it is drawn, so a sprite born inside a
111
+ * surface is drawn in front of it. 0 by default. Every material reads it.
112
+ */
113
+ cameraOffset?: number;
114
+ /**
115
+ * Whether the live particles are drawn farthest first, the order an alpha blend assumes. Off
116
+ * by default, because sorting costs a pass over the particles on the CPU each frame and an
117
+ * additive blend is a sum that does not care. See `particleSort.ts`.
118
+ */
119
+ sort?: boolean;
75
120
  /**
76
121
  * Another batch whose compiled program this one should reuse.
77
122
  *
@@ -102,6 +147,17 @@ export declare class ParticleBatch {
102
147
  private readonly ageBuffer;
103
148
  private readonly seedBuffer;
104
149
  private readonly velocityBuffer;
150
+ private readonly spriteBuffer;
151
+ private readonly spriteStaging;
152
+ /** Room to draw farthest first, where the batch asked. See `ParticleBatchOptions.sort`. */
153
+ private readonly order;
154
+ /** The sprite's own: its image, its flipbook and its fades. See `ParticleBatchOptions`. */
155
+ readonly texture: SurfaceTexture | null;
156
+ readonly cells: readonly [number, number];
157
+ readonly blendCells: boolean;
158
+ readonly softDepth: number;
159
+ readonly cameraFade: number;
160
+ readonly cameraOffset: number;
105
161
  private readonly blend;
106
162
  private readonly stretchSec;
107
163
  private readonly erosion;
@@ -119,7 +175,7 @@ export declare class ParticleBatch {
119
175
  * Only the live prefix of each array, which is what the pool's compaction
120
176
  * exists for: a pool at a tenth of its capacity uploads a tenth of the bytes.
121
177
  */
122
- upload(gl: WebGL2RenderingContext, data: ParticleInstances): void;
178
+ upload(gl: WebGL2RenderingContext, given: ParticleInstances, eye: ArrayLike<number>): void;
123
179
  /** Bind this material's own constants. Called once per draw, before `drawTo`. */
124
180
  bindMaterial(gl: WebGL2RenderingContext): void;
125
181
  /**
@@ -2,6 +2,24 @@ import { compileProgram, uniformLocations } from './shader.js';
2
2
  import { particleShaders } from './particleMaterial.js';
3
3
  import { PARTICLE_BLADES, PARTICLE_INDICES, PARTICLE_VERTS } from './shaders/particle.js';
4
4
  import { blendCovering, blendKeeping } from './sceneCoverage.js';
5
+ import { PARTICLE_SPRITE_UNIT } from './lightBudget.js';
6
+ import { createParticleSort, sortBackToFront } from './particleSort.js';
7
+ /** The vertex stage's `uCameraFacing` for a facing: the cross 0, the camera 1, the travel 2. */
8
+ export function facingCode(facing) {
9
+ return facing === 'cross' ? 0 : facing === 'velocity' ? 2 : 1;
10
+ }
11
+ /**
12
+ * A sprite's frame and half-height, two floats a particle, into `out` — the stream the vertex
13
+ * stage reads at location 10. Shared by both backends, so the two read one layout.
14
+ */
15
+ export function packSpriteStream(data, count, out) {
16
+ const frames = data.frames;
17
+ const heights = data.heights;
18
+ for (let i = 0; i < count; i++) {
19
+ out[i * 2] = frames?.[i] ?? 0;
20
+ out[i * 2 + 1] = heights?.[i] ?? 0;
21
+ }
22
+ }
5
23
  /** Corners of one blade, as a triangle-strip-friendly quad. */
6
24
  const CORNERS = [
7
25
  [-1, -1],
@@ -22,6 +40,17 @@ export class ParticleBatch {
22
40
  ageBuffer;
23
41
  seedBuffer;
24
42
  velocityBuffer;
43
+ spriteBuffer;
44
+ spriteStaging;
45
+ /** Room to draw farthest first, where the batch asked. See `ParticleBatchOptions.sort`. */
46
+ order;
47
+ /** The sprite's own: its image, its flipbook and its fades. See `ParticleBatchOptions`. */
48
+ texture;
49
+ cells;
50
+ blendCells;
51
+ softDepth;
52
+ cameraFade;
53
+ cameraOffset;
25
54
  blend;
26
55
  stretchSec;
27
56
  erosion;
@@ -53,6 +82,17 @@ export class ParticleBatch {
53
82
  this.coreGain = options.coreGain ?? 1;
54
83
  this.fog = options.fog ?? false;
55
84
  this.facing = options.facing ?? 'camera';
85
+ this.texture = options.texture ?? null;
86
+ this.cells = options.cells ?? [1, 1];
87
+ this.blendCells = options.blendCells === true;
88
+ this.softDepth = Math.max(0, options.softDepth ?? 0);
89
+ this.cameraFade = Math.max(0, options.cameraFade ?? 0);
90
+ this.cameraOffset = options.cameraOffset ?? 0;
91
+ this.order = options.sort === true ? createParticleSort(capacity) : null;
92
+ this.spriteStaging = new Float32Array(capacity * 2);
93
+ if (options.material === 'sprite' && this.texture === null) {
94
+ throw new Error("createParticles: a 'sprite' draws an image, and was given no `texture`");
95
+ }
56
96
  const corners = new Float32Array(PARTICLE_VERTS * 2);
57
97
  const blades = new Float32Array(PARTICLE_VERTS);
58
98
  const indices = new Uint16Array(PARTICLE_INDICES);
@@ -88,6 +128,7 @@ export class ParticleBatch {
88
128
  this.ageBuffer = this.attach(gl, 7, capacity, 1, 1, shaders.label);
89
129
  this.seedBuffer = this.attach(gl, 8, capacity, 1, 1, shaders.label);
90
130
  this.velocityBuffer = this.attach(gl, 9, capacity * 3, 3, 1, shaders.label);
131
+ this.spriteBuffer = this.attach(gl, 10, capacity * 2, 2, 1, shaders.label);
91
132
  const indexBuffer = gl.createBuffer();
92
133
  if (indexBuffer === null)
93
134
  throw new Error(`${shaders.label}: createBuffer failed`);
@@ -102,10 +143,13 @@ export class ParticleBatch {
102
143
  * Only the live prefix of each array, which is what the pool's compaction
103
144
  * exists for: a pool at a tenth of its capacity uploads a tenth of the bytes.
104
145
  */
105
- upload(gl, data) {
146
+ upload(gl, given, eye) {
147
+ const data = this.order === null ? given : sortBackToFront(given, eye, this.order);
106
148
  const n = data.count;
107
149
  if (n === 0)
108
150
  return;
151
+ packSpriteStream(data, n, this.spriteStaging);
152
+ this.send(gl, this.spriteBuffer, this.spriteStaging, n * 2);
109
153
  this.send(gl, this.positionBuffer, data.positions, n * 3);
110
154
  this.send(gl, this.sizeBuffer, data.sizes, n);
111
155
  this.send(gl, this.spinBuffer, data.spins, n);
@@ -119,7 +163,14 @@ export class ParticleBatch {
119
163
  bindMaterial(gl) {
120
164
  const u = this.uniforms;
121
165
  gl.uniform1f(u['uStretchSec'] ?? null, this.stretchSec);
122
- gl.uniform1f(u['uCameraFacing'] ?? null, this.facing === 'camera' ? 1 : 0);
166
+ gl.uniform1f(u['uCameraFacing'] ?? null, facingCode(this.facing));
167
+ gl.uniform1f(u['uCameraOffset'] ?? null, this.cameraOffset);
168
+ /* The sprite's flipbook and its image; the fades are the renderer's, which owns the depth. */
169
+ if (this.texture !== null) {
170
+ gl.uniform4f(u['uCells'] ?? null, this.cells[0], this.cells[1], this.blendCells ? 1 : 0, 0);
171
+ this.texture.bind(gl, PARTICLE_SPRITE_UNIT);
172
+ gl.uniform1i(u['uSprite'] ?? null, PARTICLE_SPRITE_UNIT);
173
+ }
123
174
  gl.uniform1f(u['uErosion'] ?? null, this.erosion);
124
175
  gl.uniform1f(u['uCoreGain'] ?? null, this.coreGain);
125
176
  /* Only the mote program declares uFogEnabled, so this is a no-op — not a leak into the
@@ -13,7 +13,7 @@
13
13
  * document that. Naming them makes the pairing explicit instead of leaving it to a caller to
14
14
  * match a source with the constants that suit it.
15
15
  */
16
- export type ParticleMaterial = 'smoke' | 'spark' | 'mote';
16
+ export type ParticleMaterial = 'smoke' | 'spark' | 'mote' | 'sprite';
17
17
  /** Both compilations of one material, and what the generator recorded about each stage. */
18
18
  export interface ParticleShaders {
19
19
  readonly label: string;
@@ -47,6 +47,11 @@ export declare const PARTICLE_VERT_BINDINGS: {
47
47
  readonly size: 4;
48
48
  readonly type: "float";
49
49
  };
50
+ readonly uCameraOffset: {
51
+ readonly offset: 84;
52
+ readonly size: 4;
53
+ readonly type: "float";
54
+ };
50
55
  };
51
56
  readonly textures: {};
52
57
  };
@@ -1,5 +1,5 @@
1
- import { PARTICLE_MOTE_FRAG, PARTICLE_SMOKE_FRAG, PARTICLE_SPARK_FRAG, PARTICLE_VERT, } from './shaders/particle.js';
2
- import { PARTICLE_BINDINGS, PARTICLE_MOTE_FRAG_WGSL, PARTICLE_SMOKE_FRAG_WGSL, PARTICLE_SPARK_FRAG_WGSL, PARTICLE_VERT_WGSL, } from './shaders/generated/particle.wgsl.js';
1
+ import { PARTICLE_MOTE_FRAG, PARTICLE_SMOKE_FRAG, PARTICLE_SPARK_FRAG, PARTICLE_SPRITE_FRAG, PARTICLE_VERT, } from './shaders/particle.js';
2
+ import { PARTICLE_BINDINGS, PARTICLE_MOTE_FRAG_WGSL, PARTICLE_SMOKE_FRAG_WGSL, PARTICLE_SPARK_FRAG_WGSL, PARTICLE_SPRITE_FRAG_WGSL, PARTICLE_VERT_WGSL, } from './shaders/generated/particle.wgsl.js';
3
3
  /**
4
4
  * Every particle material this engine ships.
5
5
  *
@@ -28,6 +28,14 @@ const MATERIALS = {
28
28
  vertexWgsl: PARTICLE_VERT_WGSL,
29
29
  fragmentWgsl: PARTICLE_MOTE_FRAG_WGSL,
30
30
  },
31
+ /* An image a consumer supplies, not a shape this engine draws: see `PARTICLE_SPRITE_FRAG`. */
32
+ sprite: {
33
+ label: 'particle:sprite',
34
+ vertexSource: PARTICLE_VERT,
35
+ fragmentSource: PARTICLE_SPRITE_FRAG,
36
+ vertexWgsl: PARTICLE_VERT_WGSL,
37
+ fragmentWgsl: PARTICLE_SPRITE_FRAG_WGSL,
38
+ },
31
39
  };
32
40
  /** What the generator recorded about the shared vertex stage. */
33
41
  export const PARTICLE_VERT_BINDINGS = PARTICLE_BINDINGS.PARTICLE_VERT;