@driftengine/core 4.10.0 → 4.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +107 -0
- package/README.md +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.js +2 -2
- package/dist/render/ambientOcclusionPass.d.ts +3 -2
- package/dist/render/ambientOcclusionPass.js +4 -2
- package/dist/render/backend/api.d.ts +21 -1
- package/dist/render/backend/webgl2/renderer.d.ts +141 -3
- package/dist/render/backend/webgl2/renderer.js +454 -42
- package/dist/render/backend/webgpu/buffers.d.ts +5 -0
- package/dist/render/backend/webgpu/buffers.js +98 -48
- package/dist/render/backend/webgpu/clothKernels.d.ts +8 -0
- package/dist/render/backend/webgpu/clothKernels.js +76 -0
- package/dist/render/backend/webgpu/clothOps.d.ts +39 -0
- package/dist/render/backend/webgpu/clothOps.js +78 -0
- package/dist/render/backend/webgpu/clothSetLayout.d.ts +25 -0
- package/dist/render/backend/webgpu/clothSetLayout.js +84 -0
- package/dist/render/backend/webgpu/clothSetPack.d.ts +33 -0
- package/dist/render/backend/webgpu/clothSetPack.js +144 -0
- package/dist/render/backend/webgpu/drawCommand.d.ts +2 -0
- package/dist/render/backend/webgpu/drawCommand.js +1 -0
- package/dist/render/backend/webgpu/flatPass.d.ts +1 -1
- package/dist/render/backend/webgpu/flatPass.js +2 -1
- package/dist/render/backend/webgpu/gpuClothSet.d.ts +80 -0
- package/dist/render/backend/webgpu/gpuClothSet.js +462 -0
- package/dist/render/backend/webgpu/latePass.js +1 -0
- package/dist/render/backend/webgpu/motionPass.js +15 -6
- package/dist/render/backend/webgpu/pipelineCache.d.ts +22 -1
- package/dist/render/backend/webgpu/pipelineCache.js +39 -1
- package/dist/render/backend/webgpu/postPass.d.ts +1 -1
- package/dist/render/backend/webgpu/renderer.d.ts +100 -1
- package/dist/render/backend/webgpu/renderer.js +423 -95
- package/dist/render/backend/webgpu/shaders/clothSolve.wgsl.d.ts +9 -2
- package/dist/render/backend/webgpu/shaders/clothSolve.wgsl.js +121 -47
- package/dist/render/backend/webgpu/staticBundles.js +2 -1
- package/dist/render/backend/webgpu/vertexFormats.d.ts +22 -0
- package/dist/render/backend/webgpu/vertexFormats.js +97 -0
- package/dist/render/globalMediumPass.d.ts +2 -0
- package/dist/render/globalMediumPass.js +1 -0
- package/dist/render/indexWidth.d.ts +17 -0
- package/dist/render/indexWidth.js +23 -0
- package/dist/render/instances.d.ts +13 -1
- package/dist/render/instances.js +10 -0
- package/dist/render/lightMatrix.d.ts +11 -0
- package/dist/render/lightMatrix.js +14 -0
- package/dist/render/mesh.d.ts +9 -0
- package/dist/render/mesh.js +38 -14
- package/dist/render/occlusionFade.d.ts +21 -0
- package/dist/render/occlusionFade.js +27 -0
- package/dist/render/renderQuality.d.ts +30 -0
- package/dist/render/renderQuality.js +5 -0
- package/dist/render/rendererHarness.d.ts +7 -0
- package/dist/render/rendererHarness.js +12 -0
- package/dist/render/sceneTarget.d.ts +2 -0
- package/dist/render/sceneTarget.js +1 -1
- package/dist/render/shader.d.ts +15 -0
- package/dist/render/shader.js +41 -0
- package/dist/render/shaders/ambientOcclusion.js +12 -1
- package/dist/render/shaders/flat/directionalShadow.js +84 -10
- package/dist/render/shaders/flat/glassTint.d.ts +1 -1
- package/dist/render/shaders/flat/glassTint.js +3 -0
- package/dist/render/shaders/flat/index.d.ts +15 -0
- package/dist/render/shaders/flat/index.js +24 -4
- package/dist/render/shaders/flat/layered.d.ts +27 -0
- package/dist/render/shaders/flat/layered.js +198 -0
- package/dist/render/shaders/flat/main.d.ts +1 -1
- package/dist/render/shaders/flat/main.js +50 -16
- package/dist/render/shaders/flat/mapReads.d.ts +10 -0
- package/dist/render/shaders/flat/mapReads.js +38 -0
- package/dist/render/shaders/flat/movingSun.d.ts +21 -0
- package/dist/render/shaders/flat/movingSun.js +46 -0
- package/dist/render/shaders/flat/normalMap.d.ts +6 -2
- package/dist/render/shaders/flat/normalMap.js +23 -3
- package/dist/render/shaders/flat/overlay.d.ts +3 -2
- package/dist/render/shaders/flat/overlay.js +41 -19
- package/dist/render/shaders/flat/preamble.js +23 -5
- package/dist/render/shaders/flat/worldUv.d.ts +22 -0
- package/dist/render/shaders/flat/worldUv.js +113 -0
- package/dist/render/shaders/generated/ambientOcclusion.wgsl.d.ts +6 -1
- package/dist/render/shaders/generated/ambientOcclusion.wgsl.js +6 -1
- package/dist/render/shaders/generated/flat.wgsl.d.ts +768 -368
- package/dist/render/shaders/generated/flat.wgsl.js +791 -391
- package/dist/render/shaders/generated/globalMedium.wgsl.d.ts +8 -3
- package/dist/render/shaders/generated/globalMedium.wgsl.js +8 -3
- package/dist/render/shaders/generated/lightVolume.wgsl.d.ts +15 -10
- package/dist/render/shaders/generated/lightVolume.wgsl.js +16 -11
- package/dist/render/shaders/globalMedium.js +9 -1
- package/dist/render/shaders/lightVolume.js +9 -1
- package/dist/render/shaders/tangentFrame.d.ts +1 -1
- package/dist/render/shaders/tangentFrame.js +16 -3
- package/dist/render/skinnedClothRun.d.ts +28 -0
- package/dist/render/skinnedClothRun.js +103 -35
- package/dist/render/surfaceLayers.d.ts +107 -0
- package/dist/render/surfaceLayers.js +82 -0
- package/dist/render/surfaceOverlay.d.ts +16 -3
- package/dist/render/surfaceOverlay.js +11 -6
- package/dist/render/surfaceProjection.d.ts +34 -0
- package/dist/render/surfaceProjection.js +30 -0
- package/dist/render/surfaceTexture.d.ts +39 -0
- package/dist/render/transmission.d.ts +21 -0
- package/dist/render/transmission.js +21 -0
- package/dist/render/vertexPacking.d.ts +48 -0
- package/dist/render/vertexPacking.js +68 -0
- package/dist/ui/splash.d.ts +42 -2
- package/dist/ui/splash.js +78 -13
- package/package.json +6 -6
- package/src/index.ts +6 -2
- package/src/render/ambientOcclusionPass.ts +4 -1
- package/src/render/backend/api.ts +31 -0
- package/src/render/backend/webgl2/renderer.ts +519 -56
- package/src/render/backend/webgpu/buffers.ts +153 -54
- package/src/render/backend/webgpu/clothKernels.ts +86 -0
- package/src/render/backend/webgpu/clothOps.ts +86 -0
- package/src/render/backend/webgpu/clothSetLayout.ts +102 -0
- package/src/render/backend/webgpu/clothSetPack.ts +197 -0
- package/src/render/backend/webgpu/drawCommand.ts +3 -0
- package/src/render/backend/webgpu/flatPass.ts +26 -28
- package/src/render/backend/webgpu/gpuClothSet.ts +564 -0
- package/src/render/backend/webgpu/latePass.ts +1 -0
- package/src/render/backend/webgpu/motionPass.ts +18 -7
- package/src/render/backend/webgpu/pipelineCache.ts +46 -7
- package/src/render/backend/webgpu/renderer.ts +622 -164
- package/src/render/backend/webgpu/shaders/clothSolve.wgsl.ts +121 -47
- package/src/render/backend/webgpu/staticBundles.ts +2 -1
- package/src/render/backend/webgpu/vertexFormats.ts +113 -0
- package/src/render/globalMediumPass.ts +7 -0
- package/src/render/indexWidth.ts +27 -0
- package/src/render/instances.ts +18 -1
- package/src/render/lightMatrix.ts +15 -0
- package/src/render/mesh.ts +66 -14
- package/src/render/occlusionFade.ts +29 -0
- package/src/render/renderQuality.ts +38 -0
- package/src/render/rendererHarness.ts +15 -0
- package/src/render/sceneTarget.ts +3 -0
- package/src/render/shader.ts +59 -0
- package/src/render/shaders/ambientOcclusion.ts +12 -1
- package/src/render/shaders/flat/directionalShadow.ts +84 -10
- package/src/render/shaders/flat/glassTint.ts +7 -1
- package/src/render/shaders/flat/index.ts +39 -4
- package/src/render/shaders/flat/layered.ts +198 -0
- package/src/render/shaders/flat/main.ts +50 -16
- package/src/render/shaders/flat/mapReads.ts +38 -0
- package/src/render/shaders/flat/movingSun.ts +46 -0
- package/src/render/shaders/flat/normalMap.ts +23 -3
- package/src/render/shaders/flat/overlay.ts +41 -19
- package/src/render/shaders/flat/preamble.ts +23 -5
- package/src/render/shaders/flat/worldUv.ts +113 -0
- package/src/render/shaders/generated/ambientOcclusion.wgsl.ts +6 -1
- package/src/render/shaders/generated/flat.wgsl.ts +791 -391
- package/src/render/shaders/generated/globalMedium.wgsl.ts +8 -3
- package/src/render/shaders/generated/lightVolume.wgsl.ts +16 -11
- package/src/render/shaders/globalMedium.ts +9 -1
- package/src/render/shaders/lightVolume.ts +9 -1
- package/src/render/shaders/tangentFrame.ts +16 -3
- package/src/render/skinnedClothRun.ts +138 -38
- package/src/render/surfaceLayers.ts +176 -0
- package/src/render/surfaceOverlay.ts +24 -7
- package/src/render/surfaceProjection.ts +57 -0
- package/src/render/surfaceTexture.ts +39 -0
- package/src/render/transmission.ts +27 -0
- package/src/render/vertexPacking.ts +109 -0
- package/src/ui/splash.ts +83 -14
- package/dist/render/backend/webgpu/gpuSkinnedCloth.d.ts +0 -74
- package/dist/render/backend/webgpu/gpuSkinnedCloth.js +0 -452
- package/src/render/backend/webgpu/gpuSkinnedCloth.ts +0 -502
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
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
|
+
*/
|
|
5
|
+
import type { SurfaceProjection } from './surfaceProjection.ts';
|
|
6
|
+
|
|
7
|
+
/** The most layers a material blends: a base and one for each of the mask's four channels. */
|
|
8
|
+
export const MAX_SURFACE_LAYERS = 5;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Up to five layers, each its own colour, normal and roughness at its own repeat, blended by a mask.
|
|
12
|
+
*
|
|
13
|
+
* **Layer `i` is layer `i` of the material's arrays**: `albedo`, `normal` and `orm` each an array
|
|
14
|
+
* whose layers are the material's layers in order, read at the mesh's coordinates times
|
|
15
|
+
* `repeats[i]`, so rock can repeat forty times across a cliff while the moss on it repeats three.
|
|
16
|
+
* Where the material has a `projection`, the layers are read at the world's horizontal position
|
|
17
|
+
* instead, so a repeat is so many a metre (with the projection's own scale at 1). **The mask** is
|
|
18
|
+
* read at the mesh's own coordinates and lays each layer over those before it: red lays layer 1
|
|
19
|
+
* over the base, green layer 2 over that, blue layer 3, alpha layer 4. `emissiveLayer` gives the
|
|
20
|
+
* material's emissive map to one layer, glowing where that layer shows and nowhere it is covered;
|
|
21
|
+
* absent, the map glows as on any material.
|
|
22
|
+
*
|
|
23
|
+
* **Maps beyond the layers ride the material's own arrays, after its layers**, so they take no
|
|
24
|
+
* texture binding of their own — the lit stage has none to spare: a mask the ORM array carries
|
|
25
|
+
* (`mask: 'orm'`) is the ORM array's layer just past the layers, an `addMask` the one after that,
|
|
26
|
+
* and `meshNormal` the normal array's layer just past the layers. Each shares its array's size and
|
|
27
|
+
* format, so a four-channel mask wants an array that stores four.
|
|
28
|
+
*
|
|
29
|
+
* **What it gives up**: every layer's maps are read wherever any shows, three reads a layer, so five
|
|
30
|
+
* layers are fifteen reads where one material is three; layers share an array's size and format; a
|
|
31
|
+
* cutout reads the base layer's alpha; a mask read where a model's map goes leaves no room for one,
|
|
32
|
+
* so a lightmapped surface carries its mask in its ORM array or its vertices; a triplanar projection
|
|
33
|
+
* lays layers on the horizontal plane alone; and a mask is sampled linear, so it is uploaded as data
|
|
34
|
+
* (`colorSpace: 'linear'`).
|
|
35
|
+
*/
|
|
36
|
+
export interface SurfaceLayers<Texture> {
|
|
37
|
+
/**
|
|
38
|
+
* Where the weights come from, red to alpha laying layers 1 to 4: a map read where a shading
|
|
39
|
+
* model's map goes; `'orm'`, the ORM array's layer just past the layers; or `'vertex'`, the
|
|
40
|
+
* vertex colour's red, green and blue, which then weights the layers instead of tinting them.
|
|
41
|
+
*/
|
|
42
|
+
readonly mask: Texture | 'orm' | 'vertex';
|
|
43
|
+
/** Each layer's repeats across the mesh's coordinates, base first: one to five of them. */
|
|
44
|
+
readonly repeats: readonly number[];
|
|
45
|
+
/** The layer the emissive map glows on, 0 to 4. Absent: everywhere, as on any material. */
|
|
46
|
+
readonly emissiveLayer?: number;
|
|
47
|
+
/**
|
|
48
|
+
* How the layers combine. `'over'`, the default, lays each over the ones before it by its
|
|
49
|
+
* channel. `'sum'` mixes the base toward the sum of each layer times its channel, by the channels'
|
|
50
|
+
* sum held to 1: two layers at half each are both half there, where laying over would let the
|
|
51
|
+
* second cover half of the first.
|
|
52
|
+
*/
|
|
53
|
+
readonly blend?: 'over' | 'sum';
|
|
54
|
+
/**
|
|
55
|
+
* A second mask added to one layer's weight, read where the layers are (the world, under a
|
|
56
|
+
* `projection`) times `repeat`, its red times `intensity`: sand drifting into the gravel's layer at
|
|
57
|
+
* a scale of its own. The ORM array's layer after the layers and any mask it carries.
|
|
58
|
+
*/
|
|
59
|
+
readonly addMask?: {
|
|
60
|
+
readonly layer: number;
|
|
61
|
+
readonly repeat: number;
|
|
62
|
+
readonly intensity: number;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* One layer weighted by how much the surface faces up rather than by its mask channel:
|
|
66
|
+
* `saturate(bias + sharpness × (up / 2 + 1/2))`, `up` the world normal's upward share before any
|
|
67
|
+
* normal map. Snow on the tops of rocks, moss on a ledge.
|
|
68
|
+
*/
|
|
69
|
+
readonly facing?: {
|
|
70
|
+
readonly layer: number;
|
|
71
|
+
readonly bias: number;
|
|
72
|
+
readonly sharpness: number;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* A normal map read at the mesh's own coordinates and tangents, the normal array's layer just past
|
|
76
|
+
* the layers, under the layers' blended normal: the large shapes of a rock or a cliff, which a
|
|
77
|
+
* layer repeating forty times across it cannot carry.
|
|
78
|
+
*/
|
|
79
|
+
readonly meshNormal?: boolean;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Floats `packSurfaceLayers` writes: four vectors. */
|
|
83
|
+
export const LAYER_FLOATS = 16;
|
|
84
|
+
|
|
85
|
+
/** Where the mask is read, as the fourth float of the second vector carries it. */
|
|
86
|
+
const MASK_SOURCE = { map: 0, orm: 1, vertex: 2 } as const;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* `layers` into `out`, four vectors. The first five floats are the repeats (1 for a layer not
|
|
90
|
+
* given), then how many layers there are, which one glows (−1 for none) and where the mask comes
|
|
91
|
+
* from (0 a map, 1 the ORM array, 2 the vertex colour). Then the blend (0 over, 1 sum), whether a
|
|
92
|
+
* mesh normal is laid under the layers, the added mask's layer (−1 for none) and repeat; its
|
|
93
|
+
* intensity; and the facing layer (−1 for none), its bias and its sharpness. Zeros, no layers, where
|
|
94
|
+
* there are none.
|
|
95
|
+
*
|
|
96
|
+
* A repeat that is not a positive number is 1, so a layer is never laid at a scale that collapses it
|
|
97
|
+
* to a point; a glow, an added mask or a facing layer naming a layer there is not is none, and the
|
|
98
|
+
* base, which no channel lays, is not one an added mask or the facing can name.
|
|
99
|
+
*/
|
|
100
|
+
export function packSurfaceLayers<Texture>(
|
|
101
|
+
layers: SurfaceLayers<Texture> | null | undefined,
|
|
102
|
+
out: Float32Array,
|
|
103
|
+
): void {
|
|
104
|
+
out.fill(0);
|
|
105
|
+
if (layers === null || layers === undefined || layers.repeats.length === 0) return;
|
|
106
|
+
const count = Math.min(layers.repeats.length, MAX_SURFACE_LAYERS);
|
|
107
|
+
for (let i = 0; i < MAX_SURFACE_LAYERS; i++) out[i] = positive(i < count ? layers.repeats[i] : 1);
|
|
108
|
+
out[5] = count;
|
|
109
|
+
const glow = layers.emissiveLayer;
|
|
110
|
+
out[6] = glow !== undefined && Number.isInteger(glow) && glow >= 0 && glow < count ? glow : -1;
|
|
111
|
+
out[7] =
|
|
112
|
+
layers.mask === 'orm'
|
|
113
|
+
? MASK_SOURCE.orm
|
|
114
|
+
: layers.mask === 'vertex'
|
|
115
|
+
? MASK_SOURCE.vertex
|
|
116
|
+
: MASK_SOURCE.map;
|
|
117
|
+
out[8] = layers.blend === 'sum' ? 1 : 0;
|
|
118
|
+
out[9] = layers.meshNormal === true ? 1 : 0;
|
|
119
|
+
const added = layers.addMask;
|
|
120
|
+
const addLayer = laidLayer(added?.layer, count);
|
|
121
|
+
out[10] = addLayer;
|
|
122
|
+
out[11] = addLayer < 0 ? 0 : positive(added?.repeat);
|
|
123
|
+
out[12] = addLayer < 0 ? 0 : finite(added?.intensity);
|
|
124
|
+
const facing = layers.facing;
|
|
125
|
+
const faceLayer = laidLayer(facing?.layer, count);
|
|
126
|
+
out[13] = faceLayer;
|
|
127
|
+
out[14] = faceLayer < 0 ? 0 : finite(facing?.bias);
|
|
128
|
+
out[15] = faceLayer < 0 ? 0 : finite(facing?.sharpness);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The texture a layered material's mask is, where it is one rather than another array or the vertices. */
|
|
132
|
+
export function layerMaskMap<Texture>(
|
|
133
|
+
layers: SurfaceLayers<Texture> | null | undefined,
|
|
134
|
+
): Texture | null {
|
|
135
|
+
const mask = layers?.mask;
|
|
136
|
+
return mask === undefined || mask === 'orm' || mask === 'vertex' ? null : mask;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Whether a material's layers can be drawn beside its `modelMap`: always, unless the mask is a map,
|
|
141
|
+
* which is read where a model's map goes and so cannot share the slot with one — a lightmap's page
|
|
142
|
+
* among them. The backends draw such a material as one layer and say so once.
|
|
143
|
+
*/
|
|
144
|
+
export function layersFitBeside<Texture>(
|
|
145
|
+
layers: SurfaceLayers<Texture>,
|
|
146
|
+
modelMap: Texture | null | undefined,
|
|
147
|
+
): boolean {
|
|
148
|
+
return modelMap === null || modelMap === undefined || layerMaskMap(layers) === null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The projection a layered material is drawn with: a triplanar one is laid on the horizontal plane,
|
|
153
|
+
* at its own scale, since every layer's maps read three times over would be forty-five reads a pixel.
|
|
154
|
+
* Anything else as it was. Whether to say so is the caller's.
|
|
155
|
+
*/
|
|
156
|
+
export function layeredProjection<Texture>(
|
|
157
|
+
projection: SurfaceProjection | null | undefined,
|
|
158
|
+
layers: SurfaceLayers<Texture> | null | undefined,
|
|
159
|
+
): SurfaceProjection | null {
|
|
160
|
+
if (projection === null || projection === undefined) return null;
|
|
161
|
+
if (layers === null || layers === undefined || projection.kind !== 'triplanar') return projection;
|
|
162
|
+
return { kind: 'planar', scale: projection.scale };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** A layer an added mask or the facing may name: one a channel lays, 1 to `count − 1`, or −1. */
|
|
166
|
+
function laidLayer(layer: number | undefined, count: number): number {
|
|
167
|
+
return layer !== undefined && Number.isInteger(layer) && layer >= 1 && layer < count ? layer : -1;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function positive(value: number | undefined): number {
|
|
171
|
+
return value !== undefined && Number.isFinite(value) && value > 0 ? value : 1;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function finite(value: number | undefined): number {
|
|
175
|
+
return value !== undefined && Number.isFinite(value) ? value : 0;
|
|
176
|
+
}
|
|
@@ -29,12 +29,24 @@ export interface OverlayRegion {
|
|
|
29
29
|
* raised to a contrast, times a noise laid in screen space, a slow pulse and a mask, in a colour.
|
|
30
30
|
* Added to what the surface emits, and not gated on the night as emission is: it is a glow the
|
|
31
31
|
* surface wears, not light it gives off.
|
|
32
|
+
*
|
|
33
|
+
* **Or, with `mode: 'blend'`, a colour the surface is pulled toward**, lit like the rest of it: the
|
|
34
|
+
* base colour mixed toward the rim's by the edge times `alpha`, before any light reads it, so the
|
|
35
|
+
* rim is bright where light falls and dark where none does. The colour is `colour` times
|
|
36
|
+
* `intensity`, each channel held at 1 as a base colour is, so a colour of 8 red is a red of 1 and
|
|
37
|
+
* not a glow; `alpha` is the weight, which may pass 1 to saturate the edge sooner.
|
|
32
38
|
*/
|
|
33
39
|
export interface SurfaceRim {
|
|
34
40
|
readonly colour: Vec3;
|
|
35
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* How bright, 0 for none, multiplied with `alpha`. Where the rim blends it scales `colour`
|
|
43
|
+
* instead, so 0 there is a rim of black, and `alpha` alone says how much: 0 for none.
|
|
44
|
+
*/
|
|
36
45
|
readonly intensity: number;
|
|
46
|
+
/** The rim's weight, 1 unless given. */
|
|
37
47
|
readonly alpha?: number;
|
|
48
|
+
/** `'add'`, the default, lays the rim on as glow; `'blend'` pulls the base colour toward it, lit. */
|
|
49
|
+
readonly mode?: 'add' | 'blend';
|
|
38
50
|
/** The edge's exponent: larger keeps the rim nearer the silhouette. 1.5 unless given. */
|
|
39
51
|
readonly falloff?: number;
|
|
40
52
|
/**
|
|
@@ -122,10 +134,11 @@ function region(out: Float32Array, at: number, r: OverlayRegion | undefined | nu
|
|
|
122
134
|
* no overlay. A region with a zero scale is an image not given, and its term reads as none — a
|
|
123
135
|
* noise of 1, a mask of 0, no wrinkle — and every region is one without `maps`. Fifteen vectors:
|
|
124
136
|
*
|
|
125
|
-
* - 0: rim colour, rim strength (intensity times alpha; 0 is no rim)
|
|
137
|
+
* - 0: rim colour, rim strength (intensity times alpha; 0 is no rim). A blending rim: the colour
|
|
138
|
+
* times intensity, each channel held at 1, and the alpha alone
|
|
126
139
|
* - 1: falloff, upward lean, contrast, mask weight
|
|
127
140
|
* - 2: the rim's noise region; 3: its scroll along x and y, its tiling, the pulse's rate
|
|
128
|
-
* - 4: the pulse's low and high (1 and 1 where none)
|
|
141
|
+
* - 4: the pulse's low and high (1 and 1 where none), and the mode: 0 adds, 1 blends
|
|
129
142
|
* - 5: the rim's mask region
|
|
130
143
|
* - 6: the dissolve's noise region (a zero scale is no dissolve); 7: its tiling, threshold, edge
|
|
131
144
|
* - 8: the edge's light and its strength; 9: the laid-over colour and its amount
|
|
@@ -138,10 +151,14 @@ export function packSurfaceOverlay(overlay: SurfaceOverlay | null, out: Float32A
|
|
|
138
151
|
const images = overlay.maps !== undefined;
|
|
139
152
|
const rim = overlay.rim;
|
|
140
153
|
if (rim !== undefined) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
154
|
+
const blend = rim.mode === 'blend';
|
|
155
|
+
const scale = blend ? finite(rim.intensity, 0) : 1;
|
|
156
|
+
for (let c = 0; c < 3; c++) {
|
|
157
|
+
const channel = finite(rim.colour[c], 0) * scale;
|
|
158
|
+
out[c] = blend ? Math.min(Math.max(channel, 0), 1) : channel;
|
|
159
|
+
}
|
|
160
|
+
out[3] = Math.max(0, (blend ? 1 : finite(rim.intensity, 0)) * finite(rim.alpha, 1));
|
|
161
|
+
out[18] = blend ? 1 : 0;
|
|
145
162
|
out[4] = Math.max(0, finite(rim.falloff, 1.5));
|
|
146
163
|
out[5] = Math.max(0, finite(rim.upward, 0.4));
|
|
147
164
|
out[6] = Math.max(0, finite(rim.contrast, 1));
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A material's maps placed by where a point is in the world: `SurfaceMaterial.projection`, and the
|
|
3
|
+
* one vector `shaders/flat/worldUv.ts` reads it from.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Texture coordinates from the world rather than from the mesh.
|
|
8
|
+
*
|
|
9
|
+
* **`'planar'`** lays the maps across the two horizontal axes, `x` and `z`: ground whose mesh
|
|
10
|
+
* carries flat coordinates, or none worth reading, shows its texture at `scale` repeats a metre.
|
|
11
|
+
* **`'triplanar'`** reads each map on the three planes across `x`, `y` and `z` and blends them by
|
|
12
|
+
* how squarely the surface faces each, the normal's components raised to `sharpness`, so walls and
|
|
13
|
+
* rock wear a texture without stretching. See `worldUv.ts` for the normal map's blend.
|
|
14
|
+
*
|
|
15
|
+
* **What it gives up**: the world's coordinates do not move with a mesh, so a moving mesh slides
|
|
16
|
+
* under its texture; triplanar reads every map three times; and the material's `uScale`, `vScale`
|
|
17
|
+
* and offsets, which are the mesh's coordinates', do not apply.
|
|
18
|
+
*/
|
|
19
|
+
export interface SurfaceProjection {
|
|
20
|
+
readonly kind: 'planar' | 'triplanar';
|
|
21
|
+
/** Repeats a metre: 2 lays a texture every half metre. */
|
|
22
|
+
readonly scale: number;
|
|
23
|
+
/** How sharply triplanar's planes meet: the exponent on the normal. 4 unless given. */
|
|
24
|
+
readonly sharpness?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Floats `packSurfaceProjection` writes: one vector. */
|
|
28
|
+
export const PROJECTION_FLOATS = 4;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* `projection` into `out`: its kind (0 none, 1 planar, 2 triplanar), its repeats a metre and its
|
|
32
|
+
* sharpness; zeros for none. A scale that is not finite and above zero projects nothing, so a
|
|
33
|
+
* material is never drawn at a scale that collapses its texture to a point.
|
|
34
|
+
*/
|
|
35
|
+
export function packSurfaceProjection(
|
|
36
|
+
projection: SurfaceProjection | null | undefined,
|
|
37
|
+
out: Float32Array,
|
|
38
|
+
): void {
|
|
39
|
+
out.fill(0);
|
|
40
|
+
if (projection === null || projection === undefined) return;
|
|
41
|
+
const scale = projection.scale;
|
|
42
|
+
if (!Number.isFinite(scale) || scale <= 0) return;
|
|
43
|
+
out[0] = projection.kind === 'triplanar' ? 2 : 1;
|
|
44
|
+
out[1] = scale;
|
|
45
|
+
const sharpness = projection.sharpness ?? 4;
|
|
46
|
+
out[2] = Number.isFinite(sharpness) ? Math.min(Math.max(sharpness, 1), 64) : 4;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Whether a material projects its maps at all. */
|
|
50
|
+
export function projects(projection: SurfaceProjection | null | undefined): boolean {
|
|
51
|
+
return (
|
|
52
|
+
projection !== null &&
|
|
53
|
+
projection !== undefined &&
|
|
54
|
+
Number.isFinite(projection.scale) &&
|
|
55
|
+
projection.scale > 0
|
|
56
|
+
);
|
|
57
|
+
}
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
* array samples exactly as a 2D texture does. A mesh names its layer per vertex (`MeshData.layers`),
|
|
9
9
|
* so a merged block wearing forty facades is one draw.
|
|
10
10
|
*/
|
|
11
|
+
import type { SurfaceLayers } from './surfaceLayers.ts';
|
|
12
|
+
import type { SurfaceProjection } from './surfaceProjection.ts';
|
|
11
13
|
import type { SurfaceModel } from './surfaceModel.ts';
|
|
12
14
|
import { layerSize, refuseArrayUpdate, sourceSize } from './textureSource.ts';
|
|
13
15
|
import {
|
|
@@ -213,6 +215,43 @@ export interface SurfaceMaterial<Texture = SurfaceTexture> {
|
|
|
213
215
|
* and an 8-bit frame clips the brighter peak without `hdrScene`.
|
|
214
216
|
*/
|
|
215
217
|
physicalSpecular?: boolean;
|
|
218
|
+
/**
|
|
219
|
+
* Texture coordinates from where a point is in the world rather than from the mesh: `'planar'`
|
|
220
|
+
* across the horizontal axes for ground, `'triplanar'` on three planes for walls and rock, at so
|
|
221
|
+
* many repeats a metre. Absent, the mesh's, as every material's were. **A lit switch**, compiled in
|
|
222
|
+
* the first time a material asks, so a scene that never asks pays nothing. See
|
|
223
|
+
* `surfaceProjection.ts` for what it gives up.
|
|
224
|
+
*/
|
|
225
|
+
projection?: SurfaceProjection | null;
|
|
226
|
+
/**
|
|
227
|
+
* How much light from behind a thin surface lets through, 0 to 1: a banner lit from behind, a
|
|
228
|
+
* leaf against the sun, a lampshade. The light falling on the far side — the sun, lamps, area
|
|
229
|
+
* lights, DriftLight — reaches the eye through the surface, coloured by its own colour; a metal
|
|
230
|
+
* lets none through. 0, the default, is every material as it was. Two-sided or not: a one-sided
|
|
231
|
+
* surface seen from its front shows the light behind it as well.
|
|
232
|
+
*
|
|
233
|
+
* **What it gives up**: a thin surface, not a volume, so a thick one lets through as much as a
|
|
234
|
+
* sheet; the light behind is not blurred by the surface; and none of it on glass, which lets
|
|
235
|
+
* light through by its own rule (`TranslucentMeshOptions.glass`).
|
|
236
|
+
*/
|
|
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;
|
|
245
|
+
/**
|
|
246
|
+
* Up to five layers blended by a mask, each at its own repeat: `albedo`, `normal` and `orm` arrays
|
|
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.
|
|
253
|
+
*/
|
|
254
|
+
layers?: SurfaceLayers<Texture> | null;
|
|
216
255
|
}
|
|
217
256
|
|
|
218
257
|
export interface SurfaceTextureOptions {
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { MeshData } from '@driftengine/drft';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How each of a mesh's vertex attributes travels to the device, decided once from its data and
|
|
5
|
+
* bound by each backend: as floats, as sixteen-bit fixed point, or as one value every vertex
|
|
6
|
+
* shares.
|
|
7
|
+
*
|
|
8
|
+
* **Only what reads back as it went, or near enough that no picture can tell.** One value every
|
|
9
|
+
* vertex shares is exact, and is a constant rather than a stream: a colour or an emissive naming
|
|
10
|
+
* no difference between vertices costs nothing a vertex, where both were always stored per vertex.
|
|
11
|
+
* A unit direction — a normal, a tangent and its handedness — in sixteen-bit signed fixed point is
|
|
12
|
+
* within 1/32767 a component, a few thousandths of a degree: measured against floats, 12 pixels of
|
|
13
|
+
* 921,600 moved in one published scene, by 2 of 255 on average, and none in the other eight.
|
|
14
|
+
* Colours and skin weights inside [0, 1] in sixteen-bit unsigned fixed point are within 1/65535. A
|
|
15
|
+
* textured vertex with a tangent frame, white and not glowing, goes from 64 bytes to 36 on WebGPU.
|
|
16
|
+
*
|
|
17
|
+
* **What it gives up.** Eight-bit normals would save four bytes more and are not taken: a normal in
|
|
18
|
+
* steps of 1/127 can band a highlight. **Texture coordinates stay floats**, in any range: a
|
|
19
|
+
* coordinate on a tile's far edge rounds past it in sixteen-bit fixed point, by 1/131,070, and a
|
|
20
|
+
* pixel there samples the neighbouring tile of an atlas — measured as 29 pixels moved by 62 of 255
|
|
21
|
+
* in the voxel scene, every one at a face's edge — and a half float, which steps by a 2048th near
|
|
22
|
+
* 1.0, is worse. A dynamic mesh's normals stay floats, because each rewrite would have to be packed
|
|
23
|
+
* on the CPU. What would make this wrong is a value outside its range, which the range check sends
|
|
24
|
+
* to floats rather than clamping, or a shader reading an attribute as something other than the
|
|
25
|
+
* value it carries.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/** Floats; sixteen-bit fixed point, signed or not; or one value for every vertex. */
|
|
29
|
+
export type AttributePacking = 'float' | 'snorm16' | 'unorm16' | 'constant';
|
|
30
|
+
|
|
31
|
+
/** The attributes this decides for. Every other one travels as floats. */
|
|
32
|
+
export const PACKED_ATTRIBUTES = [
|
|
33
|
+
'normals',
|
|
34
|
+
'tangents',
|
|
35
|
+
'colors',
|
|
36
|
+
'emissive',
|
|
37
|
+
'weights',
|
|
38
|
+
'weights2',
|
|
39
|
+
] as const;
|
|
40
|
+
|
|
41
|
+
export type PackedAttribute = (typeof PACKED_ATTRIBUTES)[number];
|
|
42
|
+
|
|
43
|
+
export type VertexPacking = Readonly<Record<PackedAttribute, AttributePacking>>;
|
|
44
|
+
|
|
45
|
+
/** The choice for this mesh. `dynamic` is a mesh whose positions and normals are rewritten. */
|
|
46
|
+
export function vertexPackingOf(data: MeshData, dynamic: boolean): VertexPacking {
|
|
47
|
+
return {
|
|
48
|
+
normals: dynamic ? 'float' : fixedOrFloat(data.normals, -1),
|
|
49
|
+
tangents: fixedOrFloat(data.tangents, -1),
|
|
50
|
+
colors: shared(data.colors, 3) ? 'constant' : fixedOrFloat(data.colors, 0),
|
|
51
|
+
emissive: shared(data.emissive, 1) ? 'constant' : 'float',
|
|
52
|
+
weights: fixedOrFloat(data.weights, 0),
|
|
53
|
+
weights2: fixedOrFloat(data.weights2, 0),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* What 1 is in each fixed point, the values WebGPU's `snorm16` and `unorm16` and WebGL2's
|
|
59
|
+
* normalised `SHORT` and `UNSIGNED_SHORT` divide by. A value is stored rounded to nearest, so 1, 0
|
|
60
|
+
* and -1 come back exactly.
|
|
61
|
+
*/
|
|
62
|
+
export const SNORM16_ONE = 32767;
|
|
63
|
+
export const UNORM16_ONE = 65535;
|
|
64
|
+
|
|
65
|
+
/** One value in the fixed point whose 1 is `one`, rounded to nearest: the only place it is. */
|
|
66
|
+
export function toFixed16(value: number, one: number): number {
|
|
67
|
+
return Math.round(value * one);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** An attribute's values in its fixed point, tightly, for a backend reading separate arrays. */
|
|
71
|
+
export function fixedArray(
|
|
72
|
+
values: Float32Array,
|
|
73
|
+
packing: 'snorm16' | 'unorm16',
|
|
74
|
+
): Int16Array | Uint16Array {
|
|
75
|
+
const out =
|
|
76
|
+
packing === 'snorm16' ? new Int16Array(values.length) : new Uint16Array(values.length);
|
|
77
|
+
const one = packing === 'snorm16' ? SNORM16_ONE : UNORM16_ONE;
|
|
78
|
+
for (let at = 0; at < values.length; at++) out[at] = toFixed16(values[at] as number, one);
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** How `name` travels under this packing: floats for an attribute it does not decide for. */
|
|
83
|
+
export function packingFor(packing: VertexPacking, name: string): AttributePacking {
|
|
84
|
+
return (packing as Readonly<Record<string, AttributePacking>>)[name] ?? 'float';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The value every vertex shares, for an attribute packed as `'constant'`. */
|
|
88
|
+
export function sharedValue(values: Float32Array, width: number): number[] {
|
|
89
|
+
return Array.from(values.subarray(0, width));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Fixed point where every value lies in [low, 1]; floats otherwise, and for a NaN. */
|
|
93
|
+
function fixedOrFloat(values: Float32Array | undefined, low: -1 | 0): AttributePacking {
|
|
94
|
+
if (values === undefined) return 'float';
|
|
95
|
+
for (let at = 0; at < values.length; at++) {
|
|
96
|
+
const value = values[at] as number;
|
|
97
|
+
if (!(value >= low && value <= 1)) return 'float';
|
|
98
|
+
}
|
|
99
|
+
return low < 0 ? 'snorm16' : 'unorm16';
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Whether every vertex holds the first vertex's value. Not for a mesh of none. */
|
|
103
|
+
function shared(values: Float32Array, width: number): boolean {
|
|
104
|
+
if (values.length < width) return false;
|
|
105
|
+
for (let at = width; at < values.length; at++) {
|
|
106
|
+
if (values[at] !== values[at % width]) return false;
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
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
|
|
105
|
-
*
|
|
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
|
-
|
|
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
|
-
*
|
|
286
|
-
* game would otherwise spend running behind an opaque screen.
|
|
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(() =>
|
|
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,
|
|
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 =
|
|
395
|
+
const next = state.ready ? minMs - elapsed : state.capMs - elapsed;
|
|
334
396
|
timer = setTimeout(settle, Math.max(0, next));
|
|
335
397
|
};
|
|
336
398
|
|
|
337
|
-
|
|
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
|
}
|