@driftengine/core 4.11.0 → 4.12.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 (77) hide show
  1. package/CHANGELOG.json +72 -0
  2. package/README.md +1 -1
  3. package/dist/index.d.ts +4 -2
  4. package/dist/index.js +1 -1
  5. package/dist/render/ambientOcclusionPass.d.ts +3 -2
  6. package/dist/render/ambientOcclusionPass.js +4 -2
  7. package/dist/render/backend/api.d.ts +21 -1
  8. package/dist/render/backend/webgl2/renderer.d.ts +61 -5
  9. package/dist/render/backend/webgl2/renderer.js +153 -21
  10. package/dist/render/backend/webgpu/renderer.d.ts +54 -1
  11. package/dist/render/backend/webgpu/renderer.js +246 -89
  12. package/dist/render/instances.d.ts +13 -1
  13. package/dist/render/instances.js +10 -0
  14. package/dist/render/occlusionFade.d.ts +21 -0
  15. package/dist/render/occlusionFade.js +27 -0
  16. package/dist/render/sceneTarget.d.ts +2 -0
  17. package/dist/render/sceneTarget.js +1 -1
  18. package/dist/render/shaders/ambientOcclusion.js +12 -1
  19. package/dist/render/shaders/flat/areaLight.d.ts +1 -1
  20. package/dist/render/shaders/flat/areaLight.js +1 -1
  21. package/dist/render/shaders/flat/glassTint.d.ts +1 -1
  22. package/dist/render/shaders/flat/glassTint.js +1 -0
  23. package/dist/render/shaders/flat/index.d.ts +5 -0
  24. package/dist/render/shaders/flat/index.js +10 -3
  25. package/dist/render/shaders/flat/layerLooks.d.ts +16 -0
  26. package/dist/render/shaders/flat/layerLooks.js +116 -0
  27. package/dist/render/shaders/flat/layered.d.ts +19 -9
  28. package/dist/render/shaders/flat/layered.js +169 -37
  29. package/dist/render/shaders/flat/main.d.ts +1 -1
  30. package/dist/render/shaders/flat/main.js +17 -5
  31. package/dist/render/shaders/flat/models.js +1 -1
  32. package/dist/render/shaders/flat/normalMap.d.ts +1 -1
  33. package/dist/render/shaders/flat/normalMap.js +11 -2
  34. package/dist/render/shaders/flat/preamble.js +6 -0
  35. package/dist/render/shaders/generated/ambientOcclusion.wgsl.d.ts +6 -1
  36. package/dist/render/shaders/generated/ambientOcclusion.wgsl.js +6 -1
  37. package/dist/render/shaders/generated/flat.wgsl.d.ts +176 -48
  38. package/dist/render/shaders/generated/flat.wgsl.js +215 -87
  39. package/dist/render/surfaceLayerLooks.d.ts +80 -0
  40. package/dist/render/surfaceLayerLooks.js +83 -0
  41. package/dist/render/surfaceLayers.d.ts +128 -19
  42. package/dist/render/surfaceLayers.js +73 -13
  43. package/dist/render/surfaceTexture.d.ts +13 -4
  44. package/dist/render/transmission.d.ts +21 -0
  45. package/dist/render/transmission.js +21 -0
  46. package/dist/ui/splash.d.ts +42 -2
  47. package/dist/ui/splash.js +78 -13
  48. package/package.json +6 -6
  49. package/scripts/browser.d.mts +5 -0
  50. package/scripts/browser.mjs +68 -14
  51. package/scripts/look.d.mts +81 -0
  52. package/scripts/look.mjs +380 -0
  53. package/src/index.ts +4 -0
  54. package/src/render/ambientOcclusionPass.ts +4 -1
  55. package/src/render/backend/api.ts +31 -0
  56. package/src/render/backend/webgl2/renderer.ts +170 -22
  57. package/src/render/backend/webgpu/renderer.ts +379 -139
  58. package/src/render/instances.ts +18 -1
  59. package/src/render/occlusionFade.ts +29 -0
  60. package/src/render/sceneTarget.ts +3 -0
  61. package/src/render/shaders/ambientOcclusion.ts +12 -1
  62. package/src/render/shaders/flat/areaLight.ts +1 -1
  63. package/src/render/shaders/flat/glassTint.ts +3 -1
  64. package/src/render/shaders/flat/index.ts +15 -3
  65. package/src/render/shaders/flat/layerLooks.ts +116 -0
  66. package/src/render/shaders/flat/layered.ts +170 -37
  67. package/src/render/shaders/flat/main.ts +17 -5
  68. package/src/render/shaders/flat/models.ts +1 -1
  69. package/src/render/shaders/flat/normalMap.ts +11 -2
  70. package/src/render/shaders/flat/preamble.ts +6 -0
  71. package/src/render/shaders/generated/ambientOcclusion.wgsl.ts +6 -1
  72. package/src/render/shaders/generated/flat.wgsl.ts +215 -87
  73. package/src/render/surfaceLayerLooks.ts +155 -0
  74. package/src/render/surfaceLayers.ts +187 -23
  75. package/src/render/surfaceTexture.ts +13 -4
  76. package/src/render/transmission.ts +27 -0
  77. package/src/ui/splash.ts +83 -14
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Which layer of a layered material's arrays each of its layers reads, and how each looks: the
3
+ * fifteen vectors `shaders/flat/layerLooks.ts` reads, packed from `SurfaceMaterial.layers`.
4
+ *
5
+ * **Why a material needs these at all.** Layers read from the material's own arrays in order, so
6
+ * a texture worn by fifty materials in fifty tints had to be baked fifty times, each with its tint,
7
+ * its roughness range and its normal strength in the pixels. Picked by index from arrays the
8
+ * materials share, and given their look as numbers, each texture is held once: a set of layered
9
+ * materials built from a few textures costs those textures rather than one copy per material.
10
+ *
11
+ * **What it gives up**: fifteen fragment uniform vectors, so it is a lit switch of its own,
12
+ * `LAYER_LOOKS`, which a device short of vectors refuses while still drawing the layers; a layer's
13
+ * look is a handful of numbers, not a graph; and every layer is still read wherever any shows, as
14
+ * `surfaceLayers.ts` says, now from arrays that may be large.
15
+ */
16
+ import type { Vec3 } from '../math/color.ts';
17
+ import type { SurfaceLayers } from './surfaceLayers.ts';
18
+ import { MAX_SURFACE_LAYERS, across } from './surfaceLayers.ts';
19
+
20
+ /**
21
+ * One layer's own look, applied to what it reads before the layers are blended. Every field is the
22
+ * identity where it is absent, so a material names only what differs.
23
+ */
24
+ export interface SurfaceLayerLook {
25
+ /** Multiplies the layer's colour, linear. Above 1 brightens it. Absent: `[1, 1, 1]`. */
26
+ readonly tint?: Vec3;
27
+ /** The roughness the ORM's green channel is spread over, from its 0 to its 1. Absent: `[0, 1]`. */
28
+ readonly roughness?: readonly [number, number];
29
+ /** The metalness the ORM's blue channel is spread over, the same way. Absent: `[0, 1]`. */
30
+ readonly metalness?: readonly [number, number];
31
+ /**
32
+ * How far the layer's normal map bends the surface: its tangent-space x and y times this, before
33
+ * the layers' normals are blended. 0 is flat, 1 the map as it is. Absent: 1.
34
+ */
35
+ readonly normalStrength?: number;
36
+ /**
37
+ * The layer's specular, in the units a mesh's vertices carry theirs: how strong a highlight is,
38
+ * and under `physicalSpecular` the reflectance head-on, `0.08 × specular`. Blended across the
39
+ * layers as their colours are. Absent: the surface's own, as its vertices give it.
40
+ */
41
+ readonly specular?: number;
42
+ }
43
+
44
+ /** What an occlusion read at the mesh's own coordinates darkens, and how far. */
45
+ export interface SurfaceMeshOcclusion {
46
+ /** 0 to 1: the surface times `mix(1, occlusion, strength)`. Absent: 1. */
47
+ readonly strength?: number;
48
+ /**
49
+ * `'color'`, the default, darkens the blended colour, and so every light on it, the sun's too.
50
+ * `'ambient'` darkens the ambient light alone, sky, ground and probe, as an ambient occlusion
51
+ * does, leaving the sun, the lamps and the reflections as they were.
52
+ */
53
+ readonly into?: 'color' | 'ambient';
54
+ /** The range the occlusion's red is spread over, from its 0 to its 1. Absent: `[0, 1]`. */
55
+ readonly range?: readonly [number, number];
56
+ }
57
+
58
+ /** Floats `packLayerLooks` writes: fifteen vectors. */
59
+ export const LOOK_FLOATS = 60;
60
+
61
+ /** The specular a layer is packed with when it names none: the surface's own, as its vertices give it. */
62
+ const OWN_SPECULAR = -1;
63
+
64
+ /**
65
+ * Whether a material asks for anything the `LAYER_LOOKS` switch carries: layers picked by index,
66
+ * extra maps placed somewhere other than just past the layers, a look, a repeat that is a pair, or
67
+ * a mesh occlusion.
68
+ */
69
+ export function asksLayerLooks<Texture>(
70
+ layers: SurfaceLayers<Texture> | null | undefined,
71
+ ): boolean {
72
+ if (layers === null || layers === undefined) return false;
73
+ return (
74
+ layers.arrayLayers !== undefined ||
75
+ layers.extrasAt !== undefined ||
76
+ layers.looks !== undefined ||
77
+ layers.repeats.some((repeat) => typeof repeat !== 'number') ||
78
+ occlusionOf(layers.meshOcclusion).strength > 0
79
+ );
80
+ }
81
+
82
+ /**
83
+ * `layers` into `out`, fifteen vectors, the identity wherever a material says nothing — which is
84
+ * also what a layered material that asks for none of this is drawn with once another has switched
85
+ * `LAYER_LOOKS` on, so it must draw exactly as it did before.
86
+ *
87
+ * - **0 to 7**: the array layer each layer reads, base first (layer `i` where not given); where the
88
+ * maps beyond the layers start (the layer count where not given); the mesh occlusion's strength,
89
+ * and what it darkens, 0 the colour and 1 the ambient light.
90
+ * - **8 to 47**: two vectors a layer, base first: its tint and its normal strength, then its
91
+ * roughness range and its metalness range.
92
+ * - **48 to 59**: each layer's specular (−1 for the surface's own), each layer's repeat down (its
93
+ * repeat across where it gave one number), and the range the occlusion is spread over.
94
+ *
95
+ * An array layer that is not a whole number at or above 0 reads layer `i`; a start that is not one
96
+ * is the count; a strength is held to 0..1; a tint channel, a normal strength or a specular that is
97
+ * not a number at or above 0 is the identity; a repeat down that is not a positive number is the
98
+ * repeat across; a range end that is not a number is the identity's end.
99
+ */
100
+ export function packLayerLooks<Texture>(
101
+ layers: SurfaceLayers<Texture> | null | undefined,
102
+ out: Float32Array,
103
+ ): void {
104
+ const count = Math.min(layers?.repeats.length ?? 0, MAX_SURFACE_LAYERS);
105
+ const picks = layers?.arrayLayers;
106
+ for (let i = 0; i < MAX_SURFACE_LAYERS; i++) out[i] = arrayLayer(picks?.[i], i);
107
+ out[5] = arrayLayer(layers?.extrasAt, count);
108
+ const occlusion = occlusionOf(layers?.meshOcclusion);
109
+ out[6] = occlusion.strength;
110
+ out[7] = occlusion.into === 'ambient' ? 1 : 0;
111
+ for (let i = 0; i < MAX_SURFACE_LAYERS; i++) {
112
+ const look = layers?.looks?.[i] ?? null;
113
+ const at = 8 + i * 8;
114
+ out[at] = atLeastZero(look?.tint?.[0], 1);
115
+ out[at + 1] = atLeastZero(look?.tint?.[1], 1);
116
+ out[at + 2] = atLeastZero(look?.tint?.[2], 1);
117
+ out[at + 3] = atLeastZero(look?.normalStrength, 1);
118
+ out[at + 4] = finiteOr(look?.roughness?.[0], 0);
119
+ out[at + 5] = finiteOr(look?.roughness?.[1], 1);
120
+ out[at + 6] = finiteOr(look?.metalness?.[0], 0);
121
+ out[at + 7] = finiteOr(look?.metalness?.[1], 1);
122
+ out[48 + i] = atLeastZero(look?.specular, OWN_SPECULAR);
123
+ const repeat = layers?.repeats[i];
124
+ const acrossIt = positiveOr(across(repeat), 1);
125
+ out[53 + i] = typeof repeat === 'object' ? positiveOr(repeat[1], acrossIt) : acrossIt;
126
+ }
127
+ out[58] = finiteOr(occlusion.range?.[0], 0);
128
+ out[59] = finiteOr(occlusion.range?.[1], 1);
129
+ }
130
+
131
+ /** The number form of `meshOcclusion` as the object form, the strength held to 0..1. */
132
+ function occlusionOf(
133
+ occlusion: number | SurfaceMeshOcclusion | undefined,
134
+ ): SurfaceMeshOcclusion & { readonly strength: number } {
135
+ const given =
136
+ typeof occlusion === 'number' ? { strength: occlusion } : (occlusion ?? { strength: 0 });
137
+ const strength = given.strength ?? 1;
138
+ return { ...given, strength: Number.isFinite(strength) ? Math.min(Math.max(strength, 0), 1) : 0 };
139
+ }
140
+
141
+ function arrayLayer(value: number | undefined, otherwise: number): number {
142
+ return value !== undefined && Number.isInteger(value) && value >= 0 ? value : otherwise;
143
+ }
144
+
145
+ function atLeastZero(value: number | undefined, otherwise: number): number {
146
+ return value !== undefined && Number.isFinite(value) && value >= 0 ? value : otherwise;
147
+ }
148
+
149
+ function positiveOr(value: number | undefined, otherwise: number): number {
150
+ return value !== undefined && Number.isFinite(value) && value > 0 ? value : otherwise;
151
+ }
152
+
153
+ function finiteOr(value: number | undefined, otherwise: number): number {
154
+ return value !== undefined && Number.isFinite(value) ? value : otherwise;
155
+ }
@@ -1,7 +1,9 @@
1
1
  /**
2
- * A material blended from layers by a mask: `SurfaceMaterial.layers`, and the two vectors
3
- * `shaders/flat/layered.ts` reads it from.
2
+ * A material blended from layers by a mask: `SurfaceMaterial.layers`, the four vectors
3
+ * `shaders/flat/layered.ts` reads it from, and the decisions both backends take about it.
4
4
  */
5
+ import type { SurfaceLayerLook, SurfaceMeshOcclusion } from './surfaceLayerLooks.ts';
6
+ import type { SurfaceProjection } from './surfaceProjection.ts';
5
7
 
6
8
  /** The most layers a material blends: a base and one for each of the mask's four channels. */
7
9
  export const MAX_SURFACE_LAYERS = 5;
@@ -9,36 +11,130 @@ export const MAX_SURFACE_LAYERS = 5;
9
11
  /**
10
12
  * Up to five layers, each its own colour, normal and roughness at its own repeat, blended by a mask.
11
13
  *
12
- * **Layer `i` is layer `i` of the material's arrays**: `albedo`, `normal` and `orm` each an array
13
- * whose layers are the material's layers in order, read at the mesh's coordinates times
14
+ * **Layer `i` is layer `i` of the material's arrays**, unless `arrayLayers` picks another: `albedo`,
15
+ * `normal` and `orm` each an array whose layers are the material's layers, read at the mesh's
16
+ * coordinates times
14
17
  * `repeats[i]`, so rock can repeat forty times across a cliff while the moss on it repeats three.
15
- * **The mask** is read at the mesh's own coordinates and lays each layer over those before it: red
16
- * lays layer 1 over the base, green layer 2 over that, blue layer 3, alpha layer 4. Its layer 0 is
17
- * read, from where a shading model's map goes, which is why a layered material carries no
18
- * `modelMap`. `emissiveLayer` gives the material's emissive map to one layer, glowing where that
19
- * layer shows and nowhere it is covered; absent, the map glows as on any material.
18
+ * Where the material has a `projection`, the layers are read at the world's horizontal position
19
+ * instead, so a repeat is so many a metre (with the projection's own scale at 1). **The mask** is
20
+ * read at the mesh's own coordinates and lays each layer over those before it: red lays layer 1
21
+ * over the base, green layer 2 over that, blue layer 3, alpha layer 4. `emissiveLayer` gives the
22
+ * material's emissive map to one layer, glowing where that layer shows and nowhere it is covered;
23
+ * absent, the map glows as on any material.
24
+ *
25
+ * **Maps beyond the layers ride the material's own arrays, after its layers**, so they take no
26
+ * texture binding of their own — the lit stage has none to spare: a mask the ORM array carries
27
+ * (`mask: 'orm'`) is the ORM array's layer just past the layers, an `addMask` the one after that,
28
+ * a `meshOcclusion` the one after any of those, and `meshNormal` the normal array's layer just past
29
+ * the layers. `extrasAt` moves where "just past" is, for materials that share arrays. Each shares
30
+ * its array's size and format, so a four-channel mask wants an array that stores four.
31
+ *
32
+ * **Shared arrays and a look of each layer's own** — `arrayLayers`, `extrasAt`, `looks` and
33
+ * `meshOcclusion` — are a lit switch of their own, `LAYER_LOOKS`, and `surfaceLayerLooks.ts` says
34
+ * what they cost.
20
35
  *
21
36
  * **What it gives up**: every layer's maps are read wherever any shows, three reads a layer, so five
22
37
  * layers are fifteen reads where one material is three; layers share an array's size and format; a
23
- * cutout reads the base layer's alpha; and the mask is sampled linear, so it is uploaded as data
38
+ * cutout reads the base layer's alpha; a mask read where a model's map goes leaves no room for one,
39
+ * so a lightmapped surface carries its mask in its ORM array or its vertices; a triplanar projection
40
+ * lays layers on the horizontal plane alone; and a mask is sampled linear, so it is uploaded as data
24
41
  * (`colorSpace: 'linear'`).
25
42
  */
26
43
  export interface SurfaceLayers<Texture> {
27
- /** The weights, red to alpha laying layers 1 to 4, read at the mesh's coordinates. */
28
- readonly mask: Texture;
29
- /** Each layer's repeats across the mesh's coordinates, base first: one to five of them. */
30
- readonly repeats: readonly number[];
44
+ /**
45
+ * Where the weights come from, red to alpha laying layers 1 to 4: a map read where a shading
46
+ * model's map goes; `'orm'`, the ORM array's layer just past the layers; or `'vertex'`, the
47
+ * vertex colour's red, green and blue, which then weights the layers instead of tinting them.
48
+ */
49
+ readonly mask: Texture | 'orm' | 'vertex';
50
+ /**
51
+ * Each layer's repeats across the mesh's coordinates, base first: one to five of them. A pair is a
52
+ * repeat across and a repeat down apart, moss stretched along a trunk; a pair is drawn through the
53
+ * `LAYER_LOOKS` switch, which a single number does not need.
54
+ */
55
+ readonly repeats: readonly (number | readonly [number, number])[];
31
56
  /** The layer the emissive map glows on, 0 to 4. Absent: everywhere, as on any material. */
32
57
  readonly emissiveLayer?: number;
58
+ /**
59
+ * How the layers combine. `'over'`, the default, lays each over the ones before it by its
60
+ * channel. `'sum'` mixes the base toward the sum of each layer times its channel, by the channels'
61
+ * sum held to 1: two layers at half each are both half there, where laying over would let the
62
+ * second cover half of the first.
63
+ */
64
+ readonly blend?: 'over' | 'sum';
65
+ /**
66
+ * A second mask added to one layer's weight, read where the layers are (the world, under a
67
+ * `projection`) times `repeat`, its red times `intensity`: sand drifting into the gravel's layer at
68
+ * a scale of its own. The ORM array's layer after the layers and any mask it carries.
69
+ */
70
+ readonly addMask?: {
71
+ readonly layer: number;
72
+ readonly repeat: number;
73
+ readonly intensity: number;
74
+ };
75
+ /**
76
+ * One layer weighted by how much the surface faces up rather than by its mask channel:
77
+ * `saturate(bias + sharpness × (up / 2 + 1/2))`, `up` the world normal's upward share before any
78
+ * normal map. Snow on the tops of rocks, moss on a ledge.
79
+ */
80
+ readonly facing?: {
81
+ readonly layer: number;
82
+ readonly bias: number;
83
+ readonly sharpness: number;
84
+ };
85
+ /**
86
+ * A normal map read at the mesh's own coordinates and tangents, the normal array's layer just past
87
+ * the layers, under the layers' blended normal: the large shapes of a rock or a cliff, which a
88
+ * layer repeating forty times across it cannot carry.
89
+ */
90
+ readonly meshNormal?: boolean;
91
+ /**
92
+ * Which layer of the material's albedo, normal and ORM arrays each layer reads, base first, so
93
+ * many materials can share one array of each kind and hold every texture once. Absent, or past
94
+ * its end, layer `i` reads layer `i`. The same index reads all three arrays, so a texture's three
95
+ * maps sit at the same layer of each.
96
+ */
97
+ readonly arrayLayers?: readonly number[];
98
+ /**
99
+ * The layer of each array where the maps beyond the layers start: the ORM array's mask, added
100
+ * mask and mesh occlusion, in that order, and the normal array's mesh normal. Absent: just past
101
+ * the layers, at the layer count — which is no place for them once materials share arrays.
102
+ */
103
+ readonly extrasAt?: number;
104
+ /**
105
+ * Each layer's own look, base first: a tint, the ranges its ORM's roughness and metalness are
106
+ * spread over, and how far its normal map bends the surface. Absent or null for a layer: the map
107
+ * as it is.
108
+ */
109
+ readonly looks?: readonly (SurfaceLayerLook | null | undefined)[];
110
+ /**
111
+ * An occlusion read at the mesh's own coordinates — the red of the ORM array's layer after the
112
+ * layers and any mask or added mask the array carries — darkening the surface by a strength from 0
113
+ * to 1: the large shading of a cliff that layers repeating across it cannot carry, as
114
+ * `meshNormal` carries its shape. A number is that strength, darkening the blended colour; the
115
+ * object form also chooses what it darkens and the range its red is spread over. Absent or 0:
116
+ * none.
117
+ */
118
+ readonly meshOcclusion?: number | SurfaceMeshOcclusion;
33
119
  }
34
120
 
35
- /** Floats `packSurfaceLayers` writes: two vectors. */
36
- export const LAYER_FLOATS = 8;
121
+ /** Floats `packSurfaceLayers` writes: four vectors. */
122
+ export const LAYER_FLOATS = 16;
123
+
124
+ /** Where the mask is read, as the fourth float of the second vector carries it. */
125
+ const MASK_SOURCE = { map: 0, orm: 1, vertex: 2 } as const;
37
126
 
38
127
  /**
39
- * `layers` into `out`: the five repeats (1 for a layer not given), then how many layers there are
40
- * and which one glows (−1 for none); zeros, no layers, where there are none. A repeat that is not a
41
- * positive number is 1, so a layer is never laid at a scale that collapses it to a point.
128
+ * `layers` into `out`, four vectors. The first five floats are the repeats (1 for a layer not
129
+ * given), then how many layers there are, which one glows (−1 for none) and where the mask comes
130
+ * from (0 a map, 1 the ORM array, 2 the vertex colour). Then the blend (0 over, 1 sum), whether a
131
+ * mesh normal is laid under the layers, the added mask's layer (−1 for none) and repeat; its
132
+ * intensity; and the facing layer (−1 for none), its bias and its sharpness. Zeros, no layers, where
133
+ * there are none.
134
+ *
135
+ * A repeat that is not a positive number is 1, so a layer is never laid at a scale that collapses it
136
+ * to a point; a glow, an added mask or a facing layer naming a layer there is not is none, and the
137
+ * base, which no channel lays, is not one an added mask or the facing can name.
42
138
  */
43
139
  export function packSurfaceLayers<Texture>(
44
140
  layers: SurfaceLayers<Texture> | null | undefined,
@@ -47,11 +143,79 @@ export function packSurfaceLayers<Texture>(
47
143
  out.fill(0);
48
144
  if (layers === null || layers === undefined || layers.repeats.length === 0) return;
49
145
  const count = Math.min(layers.repeats.length, MAX_SURFACE_LAYERS);
50
- for (let i = 0; i < MAX_SURFACE_LAYERS; i++) {
51
- const repeat = i < count ? (layers.repeats[i] as number) : 1;
52
- out[i] = Number.isFinite(repeat) && repeat > 0 ? repeat : 1;
53
- }
146
+ for (let i = 0; i < MAX_SURFACE_LAYERS; i++)
147
+ out[i] = positive(i < count ? across(layers.repeats[i]) : 1);
54
148
  out[5] = count;
55
149
  const glow = layers.emissiveLayer;
56
150
  out[6] = glow !== undefined && Number.isInteger(glow) && glow >= 0 && glow < count ? glow : -1;
151
+ out[7] =
152
+ layers.mask === 'orm'
153
+ ? MASK_SOURCE.orm
154
+ : layers.mask === 'vertex'
155
+ ? MASK_SOURCE.vertex
156
+ : MASK_SOURCE.map;
157
+ out[8] = layers.blend === 'sum' ? 1 : 0;
158
+ out[9] = layers.meshNormal === true ? 1 : 0;
159
+ const added = layers.addMask;
160
+ const addLayer = laidLayer(added?.layer, count);
161
+ out[10] = addLayer;
162
+ out[11] = addLayer < 0 ? 0 : positive(added?.repeat);
163
+ out[12] = addLayer < 0 ? 0 : finite(added?.intensity);
164
+ const facing = layers.facing;
165
+ const faceLayer = laidLayer(facing?.layer, count);
166
+ out[13] = faceLayer;
167
+ out[14] = faceLayer < 0 ? 0 : finite(facing?.bias);
168
+ out[15] = faceLayer < 0 ? 0 : finite(facing?.sharpness);
169
+ }
170
+
171
+ /** The texture a layered material's mask is, where it is one rather than another array or the vertices. */
172
+ export function layerMaskMap<Texture>(
173
+ layers: SurfaceLayers<Texture> | null | undefined,
174
+ ): Texture | null {
175
+ const mask = layers?.mask;
176
+ return mask === undefined || mask === 'orm' || mask === 'vertex' ? null : mask;
177
+ }
178
+
179
+ /**
180
+ * Whether a material's layers can be drawn beside its `modelMap`: always, unless the mask is a map,
181
+ * which is read where a model's map goes and so cannot share the slot with one — a lightmap's page
182
+ * among them. The backends draw such a material as one layer and say so once.
183
+ */
184
+ export function layersFitBeside<Texture>(
185
+ layers: SurfaceLayers<Texture>,
186
+ modelMap: Texture | null | undefined,
187
+ ): boolean {
188
+ return modelMap === null || modelMap === undefined || layerMaskMap(layers) === null;
189
+ }
190
+
191
+ /**
192
+ * The projection a layered material is drawn with: a triplanar one is laid on the horizontal plane,
193
+ * at its own scale, since every layer's maps read three times over would be forty-five reads a pixel.
194
+ * Anything else as it was. Whether to say so is the caller's.
195
+ */
196
+ export function layeredProjection<Texture>(
197
+ projection: SurfaceProjection | null | undefined,
198
+ layers: SurfaceLayers<Texture> | null | undefined,
199
+ ): SurfaceProjection | null {
200
+ if (projection === null || projection === undefined) return null;
201
+ if (layers === null || layers === undefined || projection.kind !== 'triplanar') return projection;
202
+ return { kind: 'planar', scale: projection.scale };
203
+ }
204
+
205
+ /** A layer's repeat across: the number, or the first of a pair. The second is `LAYER_LOOKS`'. */
206
+ export function across(repeat: number | readonly [number, number] | undefined): number | undefined {
207
+ return typeof repeat === 'number' || repeat === undefined ? repeat : repeat[0];
208
+ }
209
+
210
+ /** A layer an added mask or the facing may name: one a channel lays, 1 to `count − 1`, or −1. */
211
+ function laidLayer(layer: number | undefined, count: number): number {
212
+ return layer !== undefined && Number.isInteger(layer) && layer >= 1 && layer < count ? layer : -1;
213
+ }
214
+
215
+ function positive(value: number | undefined): number {
216
+ return value !== undefined && Number.isFinite(value) && value > 0 ? value : 1;
217
+ }
218
+
219
+ function finite(value: number | undefined): number {
220
+ return value !== undefined && Number.isFinite(value) ? value : 0;
57
221
  }
@@ -235,12 +235,21 @@ export interface SurfaceMaterial<Texture = SurfaceTexture> {
235
235
  * light through by its own rule (`TranslucentMeshOptions.glass`).
236
236
  */
237
237
  diffuseTransmission?: number;
238
+ /**
239
+ * The colour the light from behind takes through the surface, linear, in place of its own colour:
240
+ * a banner whose cloth glows a flat red behind a printed face, a leaf whose veins are not what the
241
+ * light through it shows. Absent, the surface's colour, as `diffuseTransmission` has always used.
242
+ * Each component is held at zero or above. Read only where `diffuseTransmission` lets light through.
243
+ */
244
+ transmissionColor?: readonly [number, number, number] | null;
238
245
  /**
239
246
  * Up to five layers blended by a mask, each at its own repeat: `albedo`, `normal` and `orm` arrays
240
- * whose layers are the material's, and a mask laying each over the ones before it. Absent, the
241
- * material is one layer, as every material was. **A lit switch**, compiled in the first time a
242
- * material asks. The mask is read where `modelMap` goes, so a material carries one or the other.
243
- * See `surfaceLayers.ts` for what it gives up.
247
+ * whose layers are the material's, and a mask laying each over the ones before it or summing them,
248
+ * from a map, the ORM array or the vertex colour; under a `projection` the layers are placed by the
249
+ * world. Absent, the material is one layer, as every material was. **A lit switch**, compiled in the
250
+ * first time a material asks. A mask that is a map is read where `modelMap` goes, so such a
251
+ * material carries one or the other; a lightmapped one keeps its mask in its ORM array or its
252
+ * vertices. See `surfaceLayers.ts` for the rest and what it gives up.
244
253
  */
245
254
  layers?: SurfaceLayers<Texture> | null;
246
255
  }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * A thin surface's light from behind as the lit stage reads it: `SurfaceMaterial.diffuseTransmission`
3
+ * and `transmissionColor` into the four lanes of the glass tint an opaque material leaves unused.
4
+ *
5
+ * **The colour rides lanes glass owns**, because the lit stage's uniform rows are spent
6
+ * (`flat/preamble.ts`): a pane writes its own tint there for its draw, so whatever binds a pane
7
+ * puts this back after it, or the next draw of the material lets light through in the pane's colour.
8
+ * **−1 in the colour lanes means "the albedo"**, which is what a material naming no colour has
9
+ * always let through, so a scene that names none draws to the bit as it did.
10
+ */
11
+
12
+ /** What a material lets through from behind and in which colour, as the glass tint carries it. */
13
+ export interface Transmission {
14
+ readonly diffuseTransmission?: number;
15
+ readonly transmissionColor?: readonly [number, number, number] | null;
16
+ }
17
+
18
+ /**
19
+ * `material` into `out`: the colour, or −1 three times where it names none, then the amount held
20
+ * to 0..1. A colour component below zero is held at zero, so a mistyped colour lets no light
21
+ * through rather than reading as "the albedo".
22
+ */
23
+ export function resolveTransmission(material: Transmission | null, out: Float32Array): void {
24
+ const colour = material?.transmissionColor ?? null;
25
+ for (let i = 0; i < 3; i++) out[i] = colour === null ? -1 : Math.max(colour[i] ?? 0, 0) || 0;
26
+ out[3] = Math.min(Math.max(material?.diffuseTransmission ?? 0, 0), 1) || 0;
27
+ }
package/src/ui/splash.ts CHANGED
@@ -101,8 +101,9 @@ export const SPLASH_FADE_MS = 420;
101
101
  /**
102
102
  * When the badge gives the screen to the game. Two conditions and a cap, each for a named failure.
103
103
  *
104
- * - **The game has painted.** Swapping before that shows the empty canvas the badge exists to
105
- * cover.
104
+ * - **The game has painted**, and every promise it asked the badge to wait for has settled
105
+ * (`holdSplash`). Swapping before that shows the empty canvas, or the half-loaded first screen,
106
+ * the badge exists to cover.
106
107
  * - **The minimum has passed.** A badge that vanishes the instant a fast machine paints is a
107
108
  * flicker, and the machines that boot fastest are the ones a developer tests on.
108
109
  * - **The cap overrides both**, and is checked first so that a minimum longer than the cap
@@ -138,6 +139,65 @@ export interface MountedSplash {
138
139
  present(): void;
139
140
  /** Take it down now, whatever the clock says. Idempotent. */
140
141
  release(): void;
142
+ /** Keep it up until `until` settles as well, under `capMs`. False once it has left. */
143
+ holdUntil(until: PromiseLike<unknown>, capMs?: number): boolean;
144
+ }
145
+
146
+ /**
147
+ * What a badge waits for besides the clock: the game's first frame, and every promise the game asked
148
+ * it to wait for (`holdSplash`), under the cap the game named, the longest of several, or the engine's.
149
+ *
150
+ * **A hold settles whichever way its promise does.** A load that failed must not pin the logo: the
151
+ * game is shown, broken and visible, which is the cap's own argument. Pure apart from the promises,
152
+ * so the rule is testable in Node.
153
+ */
154
+ export class SplashState {
155
+ /** Whether a frame of the game has reached the screen. */
156
+ painted = false;
157
+ private pending = 0;
158
+ /** The longest cap a hold has named, or 0 for none. */
159
+ private asked = 0;
160
+
161
+ /** The longest the badge may stay: the cap a hold named, the longest of several, or the engine's. */
162
+ get capMs(): number {
163
+ return this.asked > 0 ? this.asked : SPLASH_HARD_CAP_MS;
164
+ }
165
+
166
+ /** Wait for `until` too, under `capMs` where it names one; `settled` once it has. */
167
+ hold(until: PromiseLike<unknown>, capMs: number | undefined, settled: () => void): void {
168
+ this.pending += 1;
169
+ if (capMs !== undefined && Number.isFinite(capMs) && capMs > this.asked) this.asked = capMs;
170
+ const done = (): void => {
171
+ this.pending -= 1;
172
+ settled();
173
+ };
174
+ Promise.resolve(until).then(done, done);
175
+ }
176
+
177
+ /** Painted, with every hold settled: what `splashDecision` calls the content being ready. */
178
+ get ready(): boolean {
179
+ return this.painted && this.pending === 0;
180
+ }
181
+ }
182
+
183
+ /** The badge on the page, which `holdSplash` holds. One screen, so one badge: see `bootGate.ts`. */
184
+ let mounted: MountedSplash | null = null;
185
+
186
+ /**
187
+ * Keep the engine's badge up until `until` settles, as well as until the game's first frame: a game
188
+ * whose first screen loads for longer than that frame — a stage, figures and their pictures on a
189
+ * first visit — is seen whole rather than loading. **`capMs` is the game's own ceiling** on the
190
+ * badge in place of the engine's twenty seconds, longer for a slow first visit or shorter, so a load
191
+ * that never settles is still shown in the end; the longest of several holds' wins, and the minimum
192
+ * hold is unchanged.
193
+ *
194
+ * **Frames run while the game loads behind the plate**, since a load may need them, and are held
195
+ * once it has settled, for whatever is left of the minimum, as they are after a first frame with no
196
+ * hold. Returns whether there was a badge to hold: none in a packaged build, whose shell shows its
197
+ * own until the first frame, nor where `splash: false` or `?splash=0` turned it off.
198
+ */
199
+ export function holdSplash(until: PromiseLike<unknown>, options: { capMs?: number } = {}): boolean {
200
+ return mounted?.holdUntil(until, options.capMs) ?? false;
141
201
  }
142
202
 
143
203
  /**
@@ -275,18 +335,19 @@ export function mountSplash(options: SplashOptions = {}): MountedSplash | null {
275
335
  }
276
336
  }
277
337
 
278
- let painted = false;
338
+ const state = new SplashState();
279
339
  let leaving = false;
280
340
  let timer: ReturnType<typeof setTimeout> | null = null;
281
341
 
282
342
  /*
283
- * **Freeze the game the moment it has proved it can draw.**
343
+ * **Freeze the game the moment it has proved it can draw and has loaded.**
284
344
  *
285
- * `painted` is the first `endFrame`, and everything after it until the plate leaves is time the
286
- * game would otherwise spend running behind an opaque screen. `bootGate` is read by `startLoop`
345
+ * Ready is the first `endFrame` with every hold settled (`SplashState`), and everything after it
346
+ * until the plate leaves is time the game would otherwise spend running behind an opaque screen.
347
+ * Before it, frames run: a load the game is holding the badge for may need them. `bootGate` is read by `startLoop`
287
348
  * and named by neither of them; see its own comment for why the state is shared that way.
288
349
  */
289
- const openGate = holdFrames(() => painted && !leaving);
350
+ const openGate = holdFrames(() => state.ready && !leaving);
290
351
 
291
352
  const remove = (): void => {
292
353
  /* Leaving the top layer is what `hidePopover` is for. `remove()` alone takes the element out
@@ -304,6 +365,7 @@ export function mountSplash(options: SplashOptions = {}): MountedSplash | null {
304
365
  const release = (): void => {
305
366
  if (leaving) return;
306
367
  leaving = true;
368
+ if (mounted === badge) mounted = null;
307
369
  /* Before the fade, not after it: the game resumes as the plate becomes transparent, so what
308
370
  shows through is a running frame rather than the still one it was frozen on. */
309
371
  openGate();
@@ -325,23 +387,30 @@ export function mountSplash(options: SplashOptions = {}): MountedSplash | null {
325
387
  const settle = (): void => {
326
388
  if (leaving) return;
327
389
  const elapsed = performance.now() - started;
328
- if (splashDecision(elapsed, minMs, painted) === 'swap') {
390
+ if (splashDecision(elapsed, minMs, state.ready, state.capMs) === 'swap') {
329
391
  release();
330
392
  return;
331
393
  }
332
394
  if (timer !== null) clearTimeout(timer);
333
- const next = painted ? minMs - elapsed : SPLASH_HARD_CAP_MS - elapsed;
395
+ const next = state.ready ? minMs - elapsed : state.capMs - elapsed;
334
396
  timer = setTimeout(settle, Math.max(0, next));
335
397
  };
336
398
 
337
- settle();
338
-
339
- return {
399
+ const badge: MountedSplash = {
340
400
  present(): void {
341
- if (painted || leaving) return;
342
- painted = true;
401
+ if (state.painted || leaving) return;
402
+ state.painted = true;
343
403
  settle();
344
404
  },
345
405
  release,
406
+ holdUntil(until: PromiseLike<unknown>, capMs?: number): boolean {
407
+ if (leaving) return false;
408
+ state.hold(until, capMs, settle);
409
+ settle();
410
+ return true;
411
+ },
346
412
  };
413
+ mounted = badge;
414
+ settle();
415
+ return badge;
347
416
  }