reze-engine 0.55.15 → 0.56.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 (42) hide show
  1. package/dist/camera.d.ts +12 -0
  2. package/dist/camera.d.ts.map +1 -1
  3. package/dist/camera.js +20 -3
  4. package/dist/engine.d.ts +210 -5
  5. package/dist/engine.d.ts.map +1 -1
  6. package/dist/engine.js +1013 -104
  7. package/dist/model.d.ts +12 -2
  8. package/dist/model.d.ts.map +1 -1
  9. package/dist/model.js +47 -4
  10. package/dist/overlay.d.ts +0 -5
  11. package/dist/overlay.d.ts.map +1 -1
  12. package/dist/overlay.js +0 -5
  13. package/dist/reflection.d.ts +17 -4
  14. package/dist/reflection.d.ts.map +1 -1
  15. package/dist/reflection.js +93 -38
  16. package/dist/shaders/cast-api.d.ts +1 -1
  17. package/dist/shaders/cast-api.d.ts.map +1 -1
  18. package/dist/shaders/cast-api.js +7 -6
  19. package/dist/shaders/directives.d.ts +15 -0
  20. package/dist/shaders/directives.d.ts.map +1 -1
  21. package/dist/shaders/directives.js +5 -0
  22. package/dist/shaders/passes/composite.d.ts +1 -1
  23. package/dist/shaders/passes/composite.d.ts.map +1 -1
  24. package/dist/shaders/passes/ground.d.ts.map +1 -1
  25. package/dist/shaders/passes/ground.js +42 -6
  26. package/dist/shaders/passes/mirror.d.ts +32 -0
  27. package/dist/shaders/passes/mirror.d.ts.map +1 -0
  28. package/dist/shaders/passes/mirror.js +284 -0
  29. package/dist/shaders/passes/scene-contract.d.ts +6 -0
  30. package/dist/shaders/passes/scene-contract.d.ts.map +1 -1
  31. package/dist/shaders/passes/scene-contract.js +8 -0
  32. package/package.json +1 -1
  33. package/src/camera.ts +21 -4
  34. package/src/engine.ts +1081 -108
  35. package/src/model.ts +46 -4
  36. package/src/overlay.ts +0 -5
  37. package/src/reflection.ts +103 -37
  38. package/src/shaders/cast-api.ts +7 -6
  39. package/src/shaders/directives.ts +20 -0
  40. package/src/shaders/passes/ground.ts +42 -6
  41. package/src/shaders/passes/mirror.ts +289 -0
  42. package/src/shaders/passes/scene-contract.ts +14 -0
@@ -0,0 +1,289 @@
1
+ // The mirror surface — the one thing that consumes the reflection pass as a
2
+ // SURFACE rather than as a layer under the floor.
3
+ //
4
+ // The ground has sampled the mirror target since step 7D, and it does it the
5
+ // only way a planar reflection can be sampled correctly: project the fragment's
6
+ // world position through the camera that rendered the reflection, and the texel
7
+ // you land on is exactly where that fragment's reflection was drawn. No
8
+ // screen-space ray, no guess. This file is that same projection on a quad you
9
+ // can put anywhere, which is all a standing mirror is.
10
+ //
11
+ // What it deliberately does NOT do:
12
+ // - light. A mirror shows what it reflects; a sun term on top of that is a
13
+ // second surface nobody asked for.
14
+ // - receive shadow. The reflection already carries the shadow the reflected
15
+ // geometry is standing in.
16
+ // - carry a strength. Coverage is 1 — the tint is how a mirror falls short of
17
+ // perfect, the same way real glass does, and it is a colour rather than a
18
+ // dial that fades the surface out into nothing.
19
+
20
+ import { sceneFsOutWgsl, sceneIdPadWgsl } from "./scene-contract"
21
+
22
+ /** Bytes in MirrorMat below: mat4x4f + three 16-byte rows. */
23
+ export const MIRROR_MAT_BYTES = 64 + 16 * 3
24
+
25
+ export function mirrorShaderWgsl(): string {
26
+ return /* wgsl */ `
27
+ struct CameraUniforms { view: mat4x4f, projection: mat4x4f, viewPos: vec3f, _p: f32, };
28
+ // params = (projA, projB, _, _) — the depth-linearisation pair, carried beside
29
+ // the matrix exactly as the ground reads it. Unused here (the surface takes a
30
+ // flat blur level, not a depth-proportional one) and kept so the two consumers
31
+ // bind one buffer rather than two shapes of the same thing.
32
+ struct MirrorVP { viewProj: mat4x4f, params: vec4f, };
33
+ struct MirrorMat {
34
+ model: mat4x4f,
35
+ tint: vec3f, blur: f32,
36
+ // frame = the band's width in WORLD units, 0 for a bare pane. World rather
37
+ // than a fraction of the quad so that a wide mirror and a narrow one in the
38
+ // same scene are framed in the same moulding.
39
+ frameColor: vec3f, frame: f32,
40
+ size: vec2f, _pad: vec2f,
41
+ };
42
+
43
+ @group(0) @binding(0) var<uniform> camera: CameraUniforms;
44
+ @group(0) @binding(1) var<uniform> mirrorVP: MirrorVP;
45
+ @group(0) @binding(2) var<uniform> material: MirrorMat;
46
+ @group(0) @binding(3) var mirrorTex: texture_2d<f32>;
47
+ @group(0) @binding(4) var linearSampler: sampler;
48
+ // The mirror pass's aux, resolved. .g is accumulated alpha — the reflection's
49
+ // COVERAGE, and the only place it exists: the HDR attachment is rg11b10ufloat
50
+ // wherever the device allows it, and that format has no alpha channel, so a
51
+ // mirror reading the colour target's own alpha reads 1 everywhere and paints
52
+ // the empty black of the reflection across the whole pane.
53
+ @group(0) @binding(5) var mirrorMask: texture_2d<f32>;
54
+
55
+ struct VO {
56
+ @builtin(position) position: vec4f,
57
+ @location(0) worldPos: vec3f,
58
+ @location(1) normal: vec3f,
59
+ /** Across the quad, 0..1 — what the frame is measured in. */
60
+ @location(2) uv: vec2f,
61
+ };
62
+
63
+ @vertex fn vs(@builtin(vertex_index) vi: u32) -> VO {
64
+ // The quad is generated, not buffered: it is six vertices of a unit square
65
+ // and the model matrix carries every difference between one mirror and
66
+ // another, so a vertex buffer would be an allocation per mirror holding the
67
+ // same twelve numbers.
68
+ //
69
+ // Local XY, facing local +Z. A mirror at identity therefore faces the way the
70
+ // default camera looks FROM, which is the orientation someone dropping one
71
+ // into a scene means by "in front of her".
72
+ // Local Y runs 0..1 from the BASE, not -0.5..0.5 from the centre. A mirror
73
+ // stands on the floor: shortening it has to take the height off the top and
74
+ // leave the foot where it is, and turning it has to pivot on that foot. With
75
+ // a centred quad both of those move the whole mirror.
76
+ var q = array<vec2f, 6>(
77
+ vec2f(-0.5, 0.0), vec2f(0.5, 0.0), vec2f(0.5, 1.0),
78
+ vec2f(-0.5, 0.0), vec2f(0.5, 1.0), vec2f(-0.5, 1.0),
79
+ );
80
+ let local = vec4f(q[vi], 0.0, 1.0);
81
+ let uv = q[vi] + vec2f(0.5, 0.0);
82
+ let world = material.model * local;
83
+ var o: VO;
84
+ o.worldPos = world.xyz;
85
+ o.uv = uv;
86
+ // The local +Z axis through the model's rotation. Column 2 of a matrix whose
87
+ // scale is uniform per axis is that axis scaled — normalized here because the
88
+ // side test below is a sign, and a scaled normal would still give the right
89
+ // sign but a wrong-length one is a trap for whatever reads this next.
90
+ o.normal = normalize((material.model * vec4f(0.0, 0.0, 1.0, 0.0)).xyz);
91
+ o.position = camera.projection * camera.view * world;
92
+ return o;
93
+ }
94
+
95
+ ${sceneFsOutWgsl()}@fragment fn fs(i: VO) -> FSOut {
96
+ var out: FSOut;
97
+ ${sceneIdPadWgsl("out")}
98
+ let toEye = camera.viewPos - i.worldPos;
99
+ // Facing the viewer whichever side is up: the quad has one normal and two
100
+ // sides, and the moulding is on both.
101
+ let nrm = select(-i.normal, i.normal, dot(toEye, i.normal) > 0.0);
102
+
103
+ // ── The moulding ──
104
+ //
105
+ // Measured in world units off each edge, so the width means the same thing on
106
+ // a tall mirror as on a wide one.
107
+ let dx = min(i.uv.x, 1.0 - i.uv.x) * material.size.x;
108
+ let dy = min(i.uv.y, 1.0 - i.uv.y) * material.size.y;
109
+ let edge = min(dx, dy);
110
+ if (edge < material.frame) {
111
+ // A MOULDING with a cross-section, not a painted border. The band's normal
112
+ // rolls from facing outward at the rim to facing the viewer at the inner
113
+ // lip, and that roll is the whole trick: a flat rectangle of colour reads as
114
+ // a decal on a plane, while a normal that turns catches the view and reads
115
+ // as metal bent around glass.
116
+ let t = clamp(edge / max(material.frame, 1e-4), 0.0, 1.0);
117
+ // Which way this point of the band faces, in the plane. The axes come off
118
+ // the model matrix, so a rotated mirror's moulding rotates with it.
119
+ let ax = normalize(material.model[0].xyz);
120
+ let ay = normalize(material.model[1].xyz);
121
+ var outward = ax * select(-1.0, 1.0, i.uv.x > 0.5);
122
+ if (dy < dx) { outward = ay * select(-1.0, 1.0, i.uv.y > 0.5); }
123
+ let roll = cos(t * 1.5707963);
124
+ let n = normalize(nrm * (1.0 - roll) + outward * roll);
125
+ let v = normalize(toEye);
126
+ // Two FIXED studio lights, not the scene's. A frame lit by the scene goes
127
+ // black whenever the sun is behind it, and a black frame reads as a hole cut
128
+ // in the picture rather than as metal in shadow.
129
+ let l1 = normalize(vec3f(0.40, 0.80, 0.45));
130
+ let l2 = normalize(vec3f(-0.50, 0.20, -0.60));
131
+ let spec = pow(max(dot(n, normalize(l1 + v)), 0.0), 64.0) * 2.6
132
+ + pow(max(dot(n, normalize(l2 + v)), 0.0), 20.0) * 0.7;
133
+ let diff = max(dot(n, l1), 0.0) * 0.55 + max(dot(n, l2), 0.0) * 0.25;
134
+ // The grazing rim is what makes a metal edge read as an edge.
135
+ let fres = pow(1.0 - abs(dot(n, v)), 4.0);
136
+ // A METAL TINTS ITS OWN HIGHLIGHT. White specular on a gold frame reads as
137
+ // cream plastic; carrying the frame's colour into the highlight and the rim
138
+ // is what separates gold from a yellow-painted thing.
139
+ let sheen = mix(vec3f(1.0), material.frameColor, 0.65);
140
+ out.color = vec4f(material.frameColor * (0.45 + diff) + sheen * (spec + fres * 0.40), 1.0);
141
+ out.mask = vec4f(0.0, 1.0, 0.0, 1.0);
142
+ return out;
143
+ }
144
+
145
+ // Which side is being looked at. Behind a mirror there is no reflection to
146
+ // show — the fold puts the mirror camera on the viewer's own side and the
147
+ // projection returns a smear — so the back reads as what the back of a mirror
148
+ // is: a flat panel. Without this a mirror rotated the wrong way is not
149
+ // wrong-looking, it is broken-looking, and the fix is invisible from the
150
+ // transform panel.
151
+ if (dot(toEye, i.normal) <= 0.0) {
152
+ // The BACKING BOARD. A mirror seen from behind is a board in a frame, and
153
+ // it is opaque: the scene must not show through it, or the pane reads as a
154
+ // pane of dark glass with the cast standing inside it.
155
+ out.color = vec4f(material.frameColor * 0.55, 1.0);
156
+ out.mask = vec4f(0.0, 1.0, 0.0, 1.0);
157
+ return out;
158
+ }
159
+
160
+ // The projective sample: this fragment's world position through the mirror
161
+ // camera IS the texel its reflection landed on.
162
+ let mc = mirrorVP.viewProj * vec4f(i.worldPos, 1.0);
163
+ let mndc = mc.xyz / max(mc.w, 1e-6);
164
+ let muv = clamp(vec2f(mndc.x * 0.5 + 0.5, 0.5 - mndc.y * 0.5), vec2f(0.0), vec2f(1.0));
165
+ // A flat level, not the ground's depth-proportional one. That trick
166
+ // reconstructs how far behind the surface the reflected geometry sits by
167
+ // mirroring the eye through y — arithmetic that is only true for a floor.
168
+ // The general form belongs here eventually; a uniform frosting is the honest
169
+ // thing to ship before it, and blur 0 (the default) reads level 0 exactly.
170
+ let refl = textureSampleLevel(mirrorTex, linearSampler, muv, material.blur * 5.0);
171
+ // THE SAME LEVEL as the colour, and that is the whole of it. refl.rgb is
172
+ // premultiplied by this coverage, so the two are one quantity in two
173
+ // textures: blur the colour into the empty part of the reflection while
174
+ // reading coverage sharp, and the pane divides a spread-out colour by a
175
+ // coverage that never spread — which is a reflection that goes dark as the
176
+ // frosting comes up, exactly backwards.
177
+ let cov = textureSampleLevel(mirrorMask, linearSampler, muv, material.blur * 5.0).g;
178
+
179
+ // ALPHA IS COVERAGE, and it is what keeps the glass clear.
180
+ //
181
+ // The reflection target clears TRANSPARENT, so coverage is 1 exactly where
182
+ // the mirror pass drew something and 0 where it drew nothing. Filling the empty
183
+ // part with the scene's background colour is the obvious thing and it is
184
+ // wrong: that colour would then ride the view transform a second time (the
185
+ // real backdrop composites after it), and the panel comes out paler and
186
+ // flatter than the backdrop it sits against — a mirror that fogs.
187
+ //
188
+ // Letting it through instead costs nothing and is also what a mirror does: an
189
+ // empty mirror shows the room, and here the room IS the backdrop.
190
+ // PREMULTIPLIED, and it must not be premultiplied twice. refl.rgb came out of
191
+ // the HDR target, which stores colour already weighted by its own alpha, and
192
+ // the mirror class blends premultiplied for exactly that reason. Multiplying
193
+ // by cov here as well is what made the reflection a ghost.
194
+ //
195
+ // ALPHA 1, ALWAYS — the pane REPLACES what is behind it. A mirror is opaque;
196
+ // blending here let the real floor show through the glass beside the
197
+ // reflected one, which is two grids in one pane. Premultiplied over at alpha
198
+ // 1 is a straight overwrite, and refl.rgb is already premultiplied, so the
199
+ // empty sky of the reflection writes 0 rather than black-over-something.
200
+ out.color = vec4f(refl.rgb * material.tint, 1.0);
201
+ // .g is the ACCUMULATED CANVAS ALPHA the composite un-premultiplies by, and a
202
+ // surface that writes nothing here is a surface the composite treats as
203
+ // absent — the reflection then survived only where something else had already
204
+ // written alpha under it, which is to say only against the reflected floor.
205
+ // .r is the bloom gate, left at 0: the glow of a reflected ribbon is already
206
+ // in refl.rgb, and gating it again would bloom the one thing in the scene
207
+ // most likely to be over the threshold twice.
208
+ // COVERAGE goes here instead, and it REPLACES too (.a = 1, so the alpha-over
209
+ // blend takes the source whole). The composite reads .g as accumulated alpha:
210
+ // it un-premultiplies the colour by it and paints the scene's backdrop where
211
+ // it falls short. That is what puts the true sky behind the reflection at the
212
+ // true colour — the pane cannot paint the backdrop itself, because anything
213
+ // it writes here goes through the view transform a second time.
214
+ out.mask = vec4f(0.0, cov, 0.0, 1.0);
215
+ return out;
216
+ }
217
+ `
218
+ }
219
+
220
+ /**
221
+ * The mirror in the SHADOW pass — the frame throwing shade on the floor.
222
+ *
223
+ * Its own shader because the mirror has no vertex buffer and no skeleton: the
224
+ * quad is six generated vertices and the model matrix is the whole of it, while
225
+ * the ordinary shadow shader is built for skinned geometry with an alpha test.
226
+ *
227
+ * Depth only, no fragment stage. The pane is opaque across its whole rectangle
228
+ * — glass included, because this glass is not see-through — so there is nothing
229
+ * to alpha-test and nothing to colour.
230
+ *
231
+ * The cascade index arrives in its own tiny uniform rather than as a dynamic
232
+ * offset: the cascade matrices sit 64 bytes apart in one array, and a dynamic
233
+ * uniform offset has to be 256-aligned.
234
+ */
235
+ /**
236
+ * Downsample for the reflection's COVERAGE chain.
237
+ *
238
+ * Colour and coverage are a premultiplied PAIR, and a blur has to move them
239
+ * together. Blurring colour alone drags it toward zero wherever it spreads into
240
+ * the empty part of the reflection, while a coverage still sampled sharp says
241
+ * "fully covered" — so the reflection simply got darker as the dial came up.
242
+ *
243
+ * One bilinear tap at the destination texel centre averages the source's 2x2
244
+ * exactly, which is the box filter this wants and the cheapest way to write it.
245
+ */
246
+ export const MIRROR_MASK_DOWNSAMPLE_WGSL = /* wgsl */ `
247
+ @group(0) @binding(0) var src: texture_2d<f32>;
248
+ @group(0) @binding(1) var samp: sampler;
249
+
250
+ struct VSOut { @builtin(position) pos: vec4f, @location(0) uv: vec2f, };
251
+
252
+ @vertex fn vs(@builtin(vertex_index) i: u32) -> VSOut {
253
+ var out: VSOut;
254
+ let x = f32(i32(i / 2u) * 4 - 1);
255
+ let y = f32(i32(i % 2u) * 4 - 1);
256
+ out.pos = vec4f(x, y, 0.0, 1.0);
257
+ out.uv = vec2f(x * 0.5 + 0.5, 0.5 - y * 0.5);
258
+ return out;
259
+ }
260
+
261
+ @fragment fn fs(in: VSOut) -> @location(0) vec4f {
262
+ return textureSampleLevel(src, samp, in.uv, 0.0);
263
+ }
264
+ `
265
+
266
+ export function mirrorShadowWgsl(cascades: number): string {
267
+ return /* wgsl */ `
268
+ struct LightVP { viewProj: array<mat4x4f, ${cascades}>, };
269
+ struct Cascade { index: u32, _p0: u32, _p1: u32, _p2: u32, };
270
+ struct MirrorMat {
271
+ model: mat4x4f,
272
+ tint: vec3f, blur: f32,
273
+ frameColor: vec3f, frame: f32,
274
+ size: vec2f, _pad: vec2f,
275
+ };
276
+
277
+ @group(0) @binding(0) var<uniform> lightVP: LightVP;
278
+ @group(0) @binding(1) var<uniform> cascade: Cascade;
279
+ @group(0) @binding(2) var<uniform> material: MirrorMat;
280
+
281
+ @vertex fn vs(@builtin(vertex_index) vi: u32) -> @builtin(position) vec4f {
282
+ var q = array<vec2f, 6>(
283
+ vec2f(-0.5, 0.0), vec2f(0.5, 0.0), vec2f(0.5, 1.0),
284
+ vec2f(-0.5, 0.0), vec2f(0.5, 1.0), vec2f(-0.5, 1.0),
285
+ );
286
+ return lightVP.viewProj[cascade.index] * material.model * vec4f(q[vi], 0.0, 1.0);
287
+ }
288
+ `
289
+ }
@@ -84,6 +84,12 @@ type SceneRenderClass =
84
84
  | "ground"
85
85
  /** Backface-expanded hulls. Also a material blend — it is geometry. */
86
86
  | "outline"
87
+ /** A mirror surface: opaque geometry carrying a projective sample of the
88
+ * reflection target. Blends like a material because it IS one, and is out of
89
+ * WRITES_ID because what it shows is a picture of the cast rather than the
90
+ * cast — a reflection that claimed her object id would seed the distance
91
+ * field twice and put every silhouette effect's border around the glass. */
92
+ | "mirror"
87
93
  /** Particles and ribbons in their default, non-additive mode. */
88
94
  | "particle"
89
95
  /** Particles declaring `#blend additive` — LIGHT rather than matter, so
@@ -158,12 +164,20 @@ const ADD_PREMULTIPLIED: GPUBlendState = {
158
164
  * consequence. Outline hulls, particles and ribbons are OUT: they are not
159
165
  * things you would ever address by id, and a hull would overwrite the id of the
160
166
  * body it traces.
167
+ *
168
+ * So is the mirror — see the class's own note.
161
169
  */
162
170
  const WRITES_ID = new Set<SceneRenderClass>(["material", "ground"])
163
171
 
164
172
  /** The blends each class writes its two attachments with. */
165
173
  const BLENDS: Record<Exclude<SceneRenderClass, "depth-prepass">, [GPUBlendState, GPUBlendState]> = {
166
174
  material: [ALPHA_OVER, ALPHA_OVER],
175
+ // PREMULTIPLIED, like the ground and for the same reason: what a mirror
176
+ // writes is a sample of the HDR target, and that target already holds colour
177
+ // premultiplied by its own alpha. Handed to the src-alpha blend it would be
178
+ // weighted a second time, and a reflection at coverage 0.5 would arrive at a
179
+ // quarter strength.
180
+ mirror: [PREMULTIPLIED_OVER, ALPHA_OVER],
167
181
  // PREMULTIPLIED colour, alone among the classes — see the blend's own note.
168
182
  // The aux is ordinary alpha-over: the ground writes its mask unweighted, like
169
183
  // everything else, and coverage is what the blend applies.