@driftengine/core 4.11.1 → 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 (46) hide show
  1. package/CHANGELOG.json +36 -0
  2. package/README.md +1 -1
  3. package/dist/index.d.ts +2 -0
  4. package/dist/render/backend/webgl2/renderer.d.ts +16 -1
  5. package/dist/render/backend/webgl2/renderer.js +41 -2
  6. package/dist/render/backend/webgpu/renderer.d.ts +2 -0
  7. package/dist/render/backend/webgpu/renderer.js +9 -0
  8. package/dist/render/shaders/flat/areaLight.d.ts +1 -1
  9. package/dist/render/shaders/flat/areaLight.js +1 -1
  10. package/dist/render/shaders/flat/glassTint.d.ts +1 -1
  11. package/dist/render/shaders/flat/glassTint.js +1 -0
  12. package/dist/render/shaders/flat/index.d.ts +5 -0
  13. package/dist/render/shaders/flat/index.js +1 -1
  14. package/dist/render/shaders/flat/layerLooks.d.ts +16 -0
  15. package/dist/render/shaders/flat/layerLooks.js +116 -0
  16. package/dist/render/shaders/flat/layered.d.ts +6 -4
  17. package/dist/render/shaders/flat/layered.js +62 -13
  18. package/dist/render/shaders/flat/main.d.ts +1 -1
  19. package/dist/render/shaders/flat/main.js +7 -2
  20. package/dist/render/shaders/flat/models.js +1 -1
  21. package/dist/render/shaders/flat/preamble.js +6 -0
  22. package/dist/render/shaders/generated/flat.wgsl.d.ts +144 -16
  23. package/dist/render/shaders/generated/flat.wgsl.js +183 -55
  24. package/dist/render/surfaceLayerLooks.d.ts +80 -0
  25. package/dist/render/surfaceLayerLooks.js +83 -0
  26. package/dist/render/surfaceLayers.d.ts +47 -6
  27. package/dist/render/surfaceLayers.js +5 -1
  28. package/package.json +6 -6
  29. package/scripts/browser.d.mts +5 -0
  30. package/scripts/browser.mjs +68 -14
  31. package/scripts/look.d.mts +81 -0
  32. package/scripts/look.mjs +380 -0
  33. package/src/index.ts +2 -0
  34. package/src/render/backend/webgl2/renderer.ts +40 -2
  35. package/src/render/backend/webgpu/renderer.ts +8 -0
  36. package/src/render/shaders/flat/areaLight.ts +1 -1
  37. package/src/render/shaders/flat/glassTint.ts +3 -1
  38. package/src/render/shaders/flat/index.ts +6 -1
  39. package/src/render/shaders/flat/layerLooks.ts +116 -0
  40. package/src/render/shaders/flat/layered.ts +63 -13
  41. package/src/render/shaders/flat/main.ts +7 -2
  42. package/src/render/shaders/flat/models.ts +1 -1
  43. package/src/render/shaders/flat/preamble.ts +6 -0
  44. package/src/render/shaders/generated/flat.wgsl.ts +183 -55
  45. package/src/render/surfaceLayerLooks.ts +155 -0
  46. package/src/render/surfaceLayers.ts +52 -7
@@ -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
+ }
@@ -2,6 +2,7 @@
2
2
  * A material blended from layers by a mask: `SurfaceMaterial.layers`, the four vectors
3
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';
5
6
  import type { SurfaceProjection } from './surfaceProjection.ts';
6
7
 
7
8
  /** The most layers a material blends: a base and one for each of the mask's four channels. */
@@ -10,8 +11,9 @@ export const MAX_SURFACE_LAYERS = 5;
10
11
  /**
11
12
  * Up to five layers, each its own colour, normal and roughness at its own repeat, blended by a mask.
12
13
  *
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
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
15
17
  * `repeats[i]`, so rock can repeat forty times across a cliff while the moss on it repeats three.
16
18
  * Where the material has a `projection`, the layers are read at the world's horizontal position
17
19
  * instead, so a repeat is so many a metre (with the projection's own scale at 1). **The mask** is
@@ -23,8 +25,13 @@ export const MAX_SURFACE_LAYERS = 5;
23
25
  * **Maps beyond the layers ride the material's own arrays, after its layers**, so they take no
24
26
  * texture binding of their own — the lit stage has none to spare: a mask the ORM array carries
25
27
  * (`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
+ * 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.
28
35
  *
29
36
  * **What it gives up**: every layer's maps are read wherever any shows, three reads a layer, so five
30
37
  * layers are fifteen reads where one material is three; layers share an array's size and format; a
@@ -40,8 +47,12 @@ export interface SurfaceLayers<Texture> {
40
47
  * vertex colour's red, green and blue, which then weights the layers instead of tinting them.
41
48
  */
42
49
  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[];
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])[];
45
56
  /** The layer the emissive map glows on, 0 to 4. Absent: everywhere, as on any material. */
46
57
  readonly emissiveLayer?: number;
47
58
  /**
@@ -77,6 +88,34 @@ export interface SurfaceLayers<Texture> {
77
88
  * layer repeating forty times across it cannot carry.
78
89
  */
79
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;
80
119
  }
81
120
 
82
121
  /** Floats `packSurfaceLayers` writes: four vectors. */
@@ -104,7 +143,8 @@ export function packSurfaceLayers<Texture>(
104
143
  out.fill(0);
105
144
  if (layers === null || layers === undefined || layers.repeats.length === 0) return;
106
145
  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);
146
+ for (let i = 0; i < MAX_SURFACE_LAYERS; i++)
147
+ out[i] = positive(i < count ? across(layers.repeats[i]) : 1);
108
148
  out[5] = count;
109
149
  const glow = layers.emissiveLayer;
110
150
  out[6] = glow !== undefined && Number.isInteger(glow) && glow >= 0 && glow < count ? glow : -1;
@@ -162,6 +202,11 @@ export function layeredProjection<Texture>(
162
202
  return { kind: 'planar', scale: projection.scale };
163
203
  }
164
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
+
165
210
  /** A layer an added mask or the facing may name: one a channel lays, 1 to `count − 1`, or −1. */
166
211
  function laidLayer(layer: number | undefined, count: number): number {
167
212
  return layer !== undefined && Number.isInteger(layer) && layer >= 1 && layer < count ? layer : -1;