@margoengine/oit 0.0.0-stage → 1.0.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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +560 -2
- package/dist/compat-CLA5cArM.js +25 -0
- package/dist/depth.d.ts +61 -0
- package/dist/depth.js +111 -0
- package/dist/exact.d.ts +85 -0
- package/dist/exact.js +312 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +348 -0
- package/dist/meshes.d.ts +15 -0
- package/dist/meshes.js +43 -0
- package/dist/moments.d.ts +78 -0
- package/dist/moments.js +393 -0
- package/dist/pass-D7U8kWzv.js +92 -0
- package/dist/pass-DdYrWNpT.js +84 -0
- package/dist/pass.d.ts +80 -0
- package/dist/peel.d.ts +50 -0
- package/dist/peel.js +219 -0
- package/dist/precise.d.ts +49 -0
- package/dist/precise.js +256 -0
- package/dist/shared/pass.d.ts +53 -0
- package/dist/vfx.d.ts +11 -0
- package/dist/vfx.js +17 -0
- package/package.json +83 -4
package/README.md
CHANGED
|
@@ -1,3 +1,561 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @margoengine/oit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Import `margoengine/oit` from the `margoengine` package, or `@margoengine/oit` from this package alone; the two are the same module. Neither is on npm yet: the [Install](../../README.md#install) section lists the two routes that work today (run in a clone, or install local tarballs).
|
|
4
|
+
|
|
5
|
+
Order-independent transparency for glTF PBR BLEND primitives and `@margoengine/materials` transparent meshes:
|
|
6
|
+
weighted blended OIT (McGuire and Bavoil, JCGT 2013) by default, [moments](#moments-momentoit) from
|
|
7
|
+
`margoengine/oit/moments` for a closer result, or exact per-pixel layers from
|
|
8
|
+
[`margoengine/oit/exact`](#exact-layers-exactoit). Overlapping transparent parts (a translucent CAD housing over
|
|
9
|
+
translucent and opaque gears) no longer pop as the camera orbits, because nothing depends on draw order any more. Core
|
|
10
|
+
meshes are opaque; `addMesh` from `margoengine/oit/meshes` draws one see-through, and
|
|
11
|
+
[`addEffect`](#particles-splats-and-points-addeffect) from `margoengine/oit/vfx` brings particles, splats and points into
|
|
12
|
+
the same pass. WebGPU only.
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import {OIT} from 'margoengine/oit';
|
|
16
|
+
import {loadGLTF} from 'margoengine/gltf/pbr';
|
|
17
|
+
import {Material, createMesh} from 'margoengine/materials';
|
|
18
|
+
import {boxGeometry} from 'margoengine/geometry';
|
|
19
|
+
import {Engine} from 'margoengine/core';
|
|
20
|
+
const engine = await Engine.create(document.querySelector('canvas')!);
|
|
21
|
+
|
|
22
|
+
const housingGeometry = boxGeometry(1, 1, 1);
|
|
23
|
+
const oit = OIT.attach(engine); // or OIT.attach(engine, {depthScale: 2})
|
|
24
|
+
oit.add(await loadGLTF(engine, '/gearbox.glb')); // its BLEND primitives
|
|
25
|
+
oit.add(createMesh(engine, housingGeometry, new Material({color: '#9cc3e6', opacity: 0.5, transparent: true, doubleSided: true})));
|
|
26
|
+
// oit.add(model) returns a remover; oit.dispose() puts every model back on sorted blending.
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Models that are not added keep glTF PBR's per-primitive back-to-front sorting, unchanged: the package hooks in through
|
|
30
|
+
glTF PBR's declared transparency hook (`PBRModel.setTransparency`, documented on `PBRTransparency`), not text
|
|
31
|
+
patches, and without it every GPU call is identical (`examples/package-benchmarks/proofs/oit-detached.proof.js`).
|
|
32
|
+
Size: 2,210 B min+gzip (`examples/package-benchmarks/results/entry-sizes.json`); the hook adds 323 B to glTF PBR's
|
|
33
|
+
root entry. Core and the stage are unchanged.
|
|
34
|
+
|
|
35
|
+
## Core meshes
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import {Engine, box, PbrMaterial} from 'margoengine/core';
|
|
39
|
+
import {OIT} from 'margoengine/oit';
|
|
40
|
+
import {addMesh} from 'margoengine/oit/meshes';
|
|
41
|
+
const engine = await Engine.create(document.querySelector('canvas')!);
|
|
42
|
+
const oit = OIT.attach(engine);
|
|
43
|
+
const mesh = engine.createMesh().setGeometry(box(1, 1, 1)).setMaterial(new PbrMaterial({color: '#9cc3e6'}));
|
|
44
|
+
const restore = addMesh(oit, engine, mesh, {opacity: 0.4});
|
|
45
|
+
mesh.setPosition([1, 0, 0]); // the see-through copy follows
|
|
46
|
+
restore(); // back to the opaque mesh, the same frame as before
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The geometry and material are copied once, so a later `setMaterial` or `setGeometry` needs a new `addMesh`. While
|
|
50
|
+
see-through `mesh.geometry` is null: the mesh casts no shadow and `engine.pick` does not see it, as for every transparent surface.
|
|
51
|
+
|
|
52
|
+
## Tints, glows and glass (`blending`)
|
|
53
|
+
|
|
54
|
+
`OIT.attach(engine, {blending: true})` brings the surfaces that otherwise stay sorted into the pass: `additive` and
|
|
55
|
+
`multiply` materials, and native transmission (`KHR_materials_transmission` tints, with no
|
|
56
|
+
`@margoengine/materials/transmission` attached, which keeps its own draws).
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import {Engine} from 'margoengine/core';
|
|
60
|
+
import {OIT} from 'margoengine/oit';
|
|
61
|
+
import {Material, createMesh} from 'margoengine/materials';
|
|
62
|
+
import {boxGeometry} from 'margoengine/geometry';
|
|
63
|
+
const engine = await Engine.create(document.querySelector('canvas')!);
|
|
64
|
+
|
|
65
|
+
const oit = OIT.attach(engine, {blending: true});
|
|
66
|
+
oit.add(createMesh(engine, boxGeometry(1, 1, 1), new Material({color: '#ff8040', opacity: 0.5, transparent: true, blending: 'additive'})));
|
|
67
|
+
oit.add(createMesh(engine, boxGeometry(2, 2, 2), new Material({color: '#c0e0d0', transmission: 0.9, roughness: 0.2})));
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The result is `background x dimming + average colour x coverage + light`, whatever the order: a third `rgba16float`
|
|
71
|
+
target sums light (additive materials, the lit draw of glass), the revealage target turns `rgba16float` and multiplies
|
|
72
|
+
per channel (normal layers by `1 - alpha`, `multiply` materials by their colour, glass by its tint), and its alpha keeps
|
|
73
|
+
the coverage of normal layers alone. The composite is two draws, one multiplying and one adding, because a per-channel
|
|
74
|
+
factor and a sum need two blends.
|
|
75
|
+
|
|
76
|
+
Measured on an RTX A6000 (`npm run test:gpu --workspace packages/oit`), at 1x and 4x MSAA: each kind alone, glass with
|
|
77
|
+
maps and volume attenuation included, draws the same 8-bit pixel as the sorted path, and additive over multiply equals
|
|
78
|
+
multiply over additive equals the formula. Time is not measured yet.
|
|
79
|
+
|
|
80
|
+
Limits:
|
|
81
|
+
|
|
82
|
+
- Sorted blending treats these in depth order: glass in front of a glow dims it. Here dimming applies to the background
|
|
83
|
+
only, so light and coloured layers are not dimmed by tints in front of them. That is the price of taking order out.
|
|
84
|
+
- Only this pass takes them. `ExactOIT` and `MomentOIT` leave these surfaces on the sorted path.
|
|
85
|
+
- `blending: 'none'`, other blends and any further render state (`depthWrite`, `depthTest`, stencil, polygon offset)
|
|
86
|
+
stay on the sorted path.
|
|
87
|
+
- Cost: four targets, 25 bytes per pixel in all (the plain pass: 12), four times that under 4x MSAA, and one more
|
|
88
|
+
full-screen draw. The fourth target is the `r8unorm` mark of [linear-light layers](#linear-light-layers-blendspace-linear).
|
|
89
|
+
|
|
90
|
+
## Linear-light layers (`blendSpace: 'linear'`)
|
|
91
|
+
|
|
92
|
+
A material with `blendSpace: 'linear'` (materials `Material.blendSpace`, glTF PBR `renderState: {blendSpace: 'linear'}`)
|
|
93
|
+
goes into the pass like any other transparent material, so it stays order-independent and mixes in linear light on a
|
|
94
|
+
display-encoded canvas. Every pass here takes it: `OIT` (plain and `blending`), `ExactOIT`, `MomentOIT`, `PeelOIT` and
|
|
95
|
+
`PreciseOIT`.
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import {Engine} from 'margoengine/core';
|
|
99
|
+
import {OIT} from 'margoengine/oit';
|
|
100
|
+
import {Material, createMesh} from 'margoengine/materials';
|
|
101
|
+
import {boxGeometry} from 'margoengine/geometry';
|
|
102
|
+
const engine = await Engine.create(document.querySelector('canvas')!);
|
|
103
|
+
|
|
104
|
+
const oit = OIT.attach(engine);
|
|
105
|
+
const glass = new Material({color: '#ff0000', opacity: 0.5, transparent: true, blendSpace: 'linear'});
|
|
106
|
+
oit.add(createMesh(engine, boxGeometry(1, 1, 1), glass));
|
|
107
|
+
oit.add(createMesh(engine, boxGeometry(2, 2, 2), glass));
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
How: a linear layer accumulates like any other and also clears a mark (the revealage target's second channel, or an
|
|
111
|
+
`r8unorm` target with `blending`). When one drew, the pass copies the colour drawn so far (the native glass snapshot)
|
|
112
|
+
and, after the usual composite, redraws only the marked pixels with blending off:
|
|
113
|
+
`encode(decode(average) * (1 - revealage) + decode(scene) * revealage)`, the weighted average of McGuire and Bavoil (JCGT
|
|
114
|
+
2013) composited with the over operator (Porter and Duff 1984) on linear values, sRGB transfer per IEC 61966-2-1 (why
|
|
115
|
+
linear: Gritz and d'Eon, "The Importance of Being Linear", GPU Gems 3, 2007). With `blending`, the dimming of tints
|
|
116
|
+
applies to the decoded scene as well, and added light is summed after the encode. A pixel half covered at a 4x edge
|
|
117
|
+
mixes the two results by its mark. `MomentOIT` marks in its optical-depth target's fourth channel and `PreciseOIT` in a
|
|
118
|
+
second revealage channel (`rg16float`), and both redraw the same way from their own average and coverage.
|
|
119
|
+
|
|
120
|
+
`ExactOIT` and `PeelOIT` keep every layer of a pixel apart, so they composite a linear layer exactly where it sits: the
|
|
121
|
+
layer stores its alpha negated, and in a frame where one drew the resolve goes one layer index at a time, far to near.
|
|
122
|
+
Each index takes a snapshot of the colour so far, draws its display layers with the usual hardware blend, then its
|
|
123
|
+
linear ones with blending off, `encode(decode(layer) * alpha + decode(snapshot) * (1 - alpha))`, the over operator on
|
|
124
|
+
linear values. A linear layer in front of a display one therefore reads as sorted blending would (measured 150 of 255).
|
|
125
|
+
|
|
126
|
+
Measured on the RTX A6000, native WebGPU and the WebGL2 layer, 1x and 4x MSAA, every pass (`ExactOIT` only natively,
|
|
127
|
+
it refuses the layer by design), materials meshes and glTF (`packages/oit/test/gpu/linear-check.mjs`): 50 % red (linear
|
|
128
|
+
0.5) over black reads 137 of 255 for one layer and 165 for two, the sorted linear values; `'display'` reads 94 and 141 as
|
|
129
|
+
before.
|
|
130
|
+
|
|
131
|
+
Limits:
|
|
132
|
+
|
|
133
|
+
- Weighted passes (`OIT`, `MomentOIT`, `PreciseOIT`): a pixel that any linear layer covers composites all its layers in
|
|
134
|
+
linear light, display ones included: the pass keeps one average per pixel. The average itself is taken on encoded colours and decoded once, which is exact for one layer
|
|
135
|
+
and for layers of one colour, and adds to the weighted blend's own approximation when layers of different colours meet.
|
|
136
|
+
- Takes effect from the frame after a model first draws the option: until the native glass capture joins the frame
|
|
137
|
+
graph, the layer composites as a display layer. On an HDR target (Bloom, tonemapping) every layer is linear already
|
|
138
|
+
and no mark, copy or draw is added.
|
|
139
|
+
- Cost only when a linear layer drew: for the weighted passes one full-target copy (4x: a resolve too) and one more
|
|
140
|
+
full-screen draw; pixels without a linear layer are untouched by that draw, so their values are those of the plain
|
|
141
|
+
composite. `ExactOIT` and `PeelOIT` take one copy and one render pass per kept layer instead of one resolve pass
|
|
142
|
+
(`layers`: 8 and 10 by default), so keep `layers` near what the scene needs. Frames without a linear layer cost what
|
|
143
|
+
they did. `PreciseOIT`'s revealage is `rg16float`, 2 bytes per pixel and sample more than before.
|
|
144
|
+
- At a 4x edge the snapshot is the resolved colour, so a linear layer's covered samples blend over the pixel's average,
|
|
145
|
+
as the sorted linear path does. `MomentOIT`'s mark there is the mean count of linear layers per sample.
|
|
146
|
+
|
|
147
|
+
## Exact layers (`ExactOIT`)
|
|
148
|
+
|
|
149
|
+
`margoengine/oit/exact` stores every transparent fragment in a per-pixel list, keeps the nearest `layers` of each pixel
|
|
150
|
+
and blends them in true depth order. The result is what sorted blending would give if every pixel were sorted on its
|
|
151
|
+
own, so surfaces that cut through each other come out right, where the weighted pass above only approximates.
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import {Engine} from 'margoengine/core';
|
|
155
|
+
import {ExactOIT} from 'margoengine/oit/exact';
|
|
156
|
+
import {Material, createMesh} from 'margoengine/materials';
|
|
157
|
+
import {boxGeometry} from 'margoengine/geometry';
|
|
158
|
+
const engine = await Engine.create(document.querySelector('canvas')!);
|
|
159
|
+
|
|
160
|
+
const oit = ExactOIT.attach(engine, {layers: 8}); // the default; 1 to 32
|
|
161
|
+
oit.add(createMesh(engine, boxGeometry(1, 1, 1), new Material({color: '#9cc3e6', opacity: 0.4, transparent: true, doubleSided: true})));
|
|
162
|
+
engine.render();
|
|
163
|
+
console.log(oit.stats); // {fragments, dropped, capacity, limit}, a few frames behind
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
`add`, `addMesh`, the frame graph place and `dispose` work as for `OIT`. Options, all fixed at construction:
|
|
167
|
+
|
|
168
|
+
| Option | Default | Meaning |
|
|
169
|
+
| --- | --- | --- |
|
|
170
|
+
| `layers` | 8 | Nearest layers kept per pixel, 1 to 32. Farther ones are left out, so a pixel with more loses the far end. Fragments behind opaque surfaces sort last and do not take a place from visible ones. |
|
|
171
|
+
| `pool` | 2 | Starting list size in fragments per screen pixel. The list grows by itself, see below. |
|
|
172
|
+
| `order` | 8 | Frame graph order, as for `OIT`. |
|
|
173
|
+
|
|
174
|
+
How it runs: the transparent draws append to the list in a pass with no colour targets (four halves of colour and
|
|
175
|
+
alpha, depth, the coverage mask and the link: 16 bytes a fragment). A compute pass sorts each pixel's list and cuts it
|
|
176
|
+
to `layers`. The resolve then draws one full-screen triangle per layer, far to near, with normal blending. A fragment
|
|
177
|
+
shader that writes storage runs ahead of the depth test, and core's scene depth cannot be sampled, so hidden fragments
|
|
178
|
+
reach the list too; each resolve layer writes `frag_depth` and leaves the depth test to the hardware, so hidden layers
|
|
179
|
+
drop out per sample, and the stored coverage mask keeps MSAA edges. Coplanar layers order by their colour bits, the same
|
|
180
|
+
on every frame.
|
|
181
|
+
|
|
182
|
+
Measured on an RTX A6000 (`npm run test:gpu --workspace packages/oit`), the same four unlit planes as the table below:
|
|
183
|
+
0.66 of 255 from blending worked out by hand, at 1x and 4x MSAA; the weighted pass gives more than 3 times that. Adding
|
|
184
|
+
the models in another order changes no pixel, and two sheets that cross swap at the line. Time is not measured yet.
|
|
185
|
+
|
|
186
|
+
Limits:
|
|
187
|
+
|
|
188
|
+
- The list holds what one storage binding allows: 8,388,608 fragments on a default device (128 MiB). That is every
|
|
189
|
+
transparent fragment of the frame, hidden ones included: 4 screen layers at 1920x1080, 1 at 3840x2160. A short list
|
|
190
|
+
drops fragments for the few frames until its read-back makes it grow, and it never shrinks; past `limit` it keeps
|
|
191
|
+
dropping, which `stats.dropped` shows and which looks like missing layers, not wrong blending.
|
|
192
|
+
- Memory: 16 bytes per list fragment (the default `pool` starts at 32 bytes per pixel, about 63 MiB at 1920x1080) plus 4
|
|
193
|
+
bytes per pixel.
|
|
194
|
+
- Colour is held in half floats: at most 65,504, 11 bits of precision.
|
|
195
|
+
- It needs storage buffers in fragment shaders, so WebGPU only, and not compatibility mode: `setup` throws
|
|
196
|
+
`'unsupported'` there; use `OIT`. The guard checks the WebGL2 device tier and native compatibility feature level as well as explicit fragment-storage limits, before allocating list buffers. `test/oit.test.js` covers all three refusal paths; VFX's `test/lit-check.mjs` checks the real WebGL2 rejection and the same lit-particle pixels through weighted `OIT`.
|
|
197
|
+
- Transmission, custom blending and depth overrides keep the sorted path: `blending` is an `OIT` option only.
|
|
198
|
+
|
|
199
|
+
## Moments (`MomentOIT`)
|
|
200
|
+
|
|
201
|
+
`margoengine/oit/moments` sits between the two others: fixed memory like `OIT`, and much closer to the right answer once
|
|
202
|
+
layers pile up, because a near layer really does dim what lies behind it.
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import {Engine} from 'margoengine/core';
|
|
206
|
+
import {MomentOIT} from 'margoengine/oit/moments';
|
|
207
|
+
import {Material, createMesh} from 'margoengine/materials';
|
|
208
|
+
import {boxGeometry} from 'margoengine/geometry';
|
|
209
|
+
const engine = await Engine.create(document.querySelector('canvas')!);
|
|
210
|
+
|
|
211
|
+
const oit = MomentOIT.attach(engine);
|
|
212
|
+
const glass = createMesh(engine, boxGeometry(1, 1, 1), new Material({color: '#9cc3e6', opacity: 0.4, transparent: true, doubleSided: true}));
|
|
213
|
+
oit.add(glass);
|
|
214
|
+
glass.root.setPosition([0, 0, 3]);
|
|
215
|
+
oit.fit(); // after moving models far: measures them again
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
How it works (Muenstermann, Krumpen, Klein and Peters, I3D 2018): the first draw of each model adds up, per pixel, the
|
|
219
|
+
optical depth `-ln(1 - alpha)` of its layers and three trigonometric moments of their log-warped depth (the paper's
|
|
220
|
+
Section 5). The second draw shades each fragment and scales it by the transmittance in front of it, recovered from the
|
|
221
|
+
moments by the paper's Algorithm 2. A composite then normalises the sum and lays it over the scene at the coverage
|
|
222
|
+
`1 - exp(-optical depth)`. Every target is `rgba16float`, which blends on every WebGPU device, so no feature is needed.
|
|
223
|
+
Trigonometric moments, not power moments, because half-float sums leave power moments ill-posed where a pixel has two
|
|
224
|
+
or three layers: there one half-float step in the sums moved a layer's transmittance from 0.51 to 0.93 on an Apple M6,
|
|
225
|
+
and the order models were added in moved pixels by 4. With trigonometric moments and a bias of 3e-3, one step moves it
|
|
226
|
+
by under 0.001.
|
|
227
|
+
|
|
228
|
+
| Option | Default | Meaning |
|
|
229
|
+
| --- | --- | --- |
|
|
230
|
+
| `depthRange` | the box round the added models | `[near, far]` view depth the moments resolve, 0 < near < far. Layers outside blend as if at the end they passed. |
|
|
231
|
+
| `order` | 8 | Frame graph order, as for `OIT`. |
|
|
232
|
+
|
|
233
|
+
Measured on an Apple M6 (Chrome, `npm run test:gpu --workspace packages/oit`), worst channel, 0-255, against sorted
|
|
234
|
+
blending worked out by hand, at 1x and 4x MSAA:
|
|
235
|
+
|
|
236
|
+
| Scene | Moments | Weighted |
|
|
237
|
+
| --- | --- | --- |
|
|
238
|
+
| Four layers at alphas 0.5, 0.4, 0.6, 0.5 | 2.34 | 13.26 |
|
|
239
|
+
| Two dense layers at alpha 0.9 | 0.26 | 71.81 |
|
|
240
|
+
| One layer | under 1.5 | under 1.5 |
|
|
241
|
+
|
|
242
|
+
Adding models in another order moved a pixel by at most 1 in these runs. Time is not measured yet.
|
|
243
|
+
|
|
244
|
+
Limits:
|
|
245
|
+
|
|
246
|
+
- It is an approximation: a layer dims itself a little (a quarter of its own optical depth, the paper's overestimation)
|
|
247
|
+
and close layers blur together, so results sit between `OIT` and `ExactOIT`. Total coverage is exact.
|
|
248
|
+
- Precision follows the range. The default box is measured once, reading every vertex, on the first frame after `add` or
|
|
249
|
+
`fit`: a model moved far from there falls outside and loses depth order until `fit()` runs (in the check, the error
|
|
250
|
+
more than doubles). Wide ranges with close layers cost accuracy too: in the check, a hand range of 3 to 6 round planes
|
|
251
|
+
from 3.4 to 4.3 gave 3.26 where the box gave 2.34. `depthRange` sets it by hand.
|
|
252
|
+
- Half-float sums round as they add. The bias of 3e-3 holds a one-step change in every stored channel to under 0.001 of
|
|
253
|
+
transmittance for one to four layers; many more layers in one pixel round more, and the paper raises the bias for
|
|
254
|
+
large overdraw. That case is not measured yet.
|
|
255
|
+
- Every model draws twice, and the second draw reads two more textures, which lowers the 16-texture budget of a material
|
|
256
|
+
to 14. Memory: 24 bytes per pixel, plus 96 under 4x MSAA.
|
|
257
|
+
- Transmission, custom blending and depth overrides keep the sorted path: `blending` is an `OIT` option only.
|
|
258
|
+
|
|
259
|
+
## Depth peeling (`PeelOIT`)
|
|
260
|
+
|
|
261
|
+
`margoengine/oit/peel` gives the exact per-pixel order of `ExactOIT` without storage buffers, so it runs on the full
|
|
262
|
+
WebGL2 compatibility layer as well as WebGPU. Each layer redraws the added models and keeps the nearest surface behind
|
|
263
|
+
the layer before it; the kept layers then composite far to near over the scene, stopped by opaque depth.
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
import {Engine} from 'margoengine/core';
|
|
267
|
+
import {PeelOIT} from 'margoengine/oit/peel';
|
|
268
|
+
import {Material, createMesh} from 'margoengine/materials';
|
|
269
|
+
import {boxGeometry} from 'margoengine/geometry';
|
|
270
|
+
const engine = await Engine.create(document.querySelector('canvas')!);
|
|
271
|
+
|
|
272
|
+
const oit = PeelOIT.attach(engine, {layers: 10}); // the default
|
|
273
|
+
oit.add(createMesh(engine, boxGeometry(1, 1, 1), new Material({color: '#9cc3e6', opacity: 0.4, transparent: true, doubleSided: true})));
|
|
274
|
+
engine.render();
|
|
275
|
+
console.log(oit.layers); // 10
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
| Option | Default | Meaning |
|
|
279
|
+
| --- | --- | --- |
|
|
280
|
+
| `layers` | 10 | Nearest distinct depth layers kept per pixel, a positive integer; set it to the deepest stack the view shows. Farther layers are left out. |
|
|
281
|
+
| `order` | 8 | Frame graph order, as for `OIT`. |
|
|
282
|
+
|
|
283
|
+
Both are fixed at construction; to change them, `dispose()` the pass and attach a new one. `add`, `addMesh` and
|
|
284
|
+
`dispose` work as for `OIT`.
|
|
285
|
+
|
|
286
|
+
Measured by `packages/oit/test/gpu/peel-check.mjs` on native WebGPU and on the WebGL2 layer (`--compat`), at 1x and 4x
|
|
287
|
+
MSAA: four analytic layers within 2.5 of 255 of hand-worked sorted blending, adding the models in another order changes
|
|
288
|
+
no pixel, `layers: 2` keeps the nearest two, opaque depth hides the layers behind it, and two crossing sheets swap at
|
|
289
|
+
the line. Time is not measured yet.
|
|
290
|
+
|
|
291
|
+
Limits:
|
|
292
|
+
|
|
293
|
+
- Cost: every layer draws every added model again, so `layers: 10` is ten transparent draws of the scene plus one
|
|
294
|
+
full-screen draw per layer in the composite. Memory: 12 bytes per pixel per layer (an `rgba16float` colour and an
|
|
295
|
+
`r32float` depth) plus 8 bytes per pixel: 128 bytes a pixel at the default, about 253 MiB at 1920x1080 and 1 GiB at
|
|
296
|
+
3840x2160. Targets follow the view size.
|
|
297
|
+
- Transparent coverage is single-sample, also in an MSAA scene: opaque edges keep their MSAA, glass edges do not.
|
|
298
|
+
- Coplanar fragments share one layer, so two surfaces at the same depth show only one.
|
|
299
|
+
- Models only: `addEffect` (particles, splats, points) throws a `TypeError`; use `OIT`, `ExactOIT` or `MomentOIT` for
|
|
300
|
+
effects. Transmission and custom blending keep the sorted path.
|
|
301
|
+
- The small preview device throws `MargoError` `'unsupported'`; the full WebGL2 compatibility layer works.
|
|
302
|
+
|
|
303
|
+
## Float32 sums (`PreciseOIT`)
|
|
304
|
+
|
|
305
|
+
`margoengine/oit/precise` is the weighted pass of `OIT` with its sums in four `r32float` targets instead of half
|
|
306
|
+
floats, so bright stacked layers (emissive glass, HDR particles) no longer overflow at 65,504 or lose low bits. Weights
|
|
307
|
+
and coverage are those of `OIT`.
|
|
308
|
+
|
|
309
|
+
```ts
|
|
310
|
+
import {Engine} from 'margoengine/core';
|
|
311
|
+
import {PreciseOIT} from 'margoengine/oit/precise';
|
|
312
|
+
import {Material, createMesh} from 'margoengine/materials';
|
|
313
|
+
import {boxGeometry} from 'margoengine/geometry';
|
|
314
|
+
const engine = await Engine.create(document.querySelector('canvas')!, {features: ['float32-blendable']});
|
|
315
|
+
|
|
316
|
+
const oit = PreciseOIT.attach(engine); // depthScale follows the camera
|
|
317
|
+
oit.add(createMesh(engine, boxGeometry(1, 1, 1), new Material({color: '#9cc3e6', opacity: 0.4, transparent: true, doubleSided: true})));
|
|
318
|
+
engine.render();
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
| Option | Default | Meaning |
|
|
322
|
+
| --- | --- | --- |
|
|
323
|
+
| `depthScale` | the camera's distance to its target, each frame | Depth-weight distance in world units, positive and finite. |
|
|
324
|
+
| `order` | 8 | Frame graph order, as for `OIT`. |
|
|
325
|
+
|
|
326
|
+
- It needs float32 blending and five colour targets: on WebGPU create the engine with
|
|
327
|
+
`features: ['float32-blendable']`; on the WebGL2 layer the browser needs `EXT_float_blend`. Without them `setup`
|
|
328
|
+
throws `MargoError` `'unsupported'` before allocating anything; use `OIT` there.
|
|
329
|
+
- Scene MSAA is kept: the composite reads and averages the samples itself, with no hardware resolve, at 1x and 4x.
|
|
330
|
+
- Memory: 20 bytes per pixel and sample (four `r32float` sums and an `rg16float` revealage), 80 under 4x MSAA.
|
|
331
|
+
- It is still the weighted approximation: depth order between layers is estimated, as in `OIT` (see Limits below);
|
|
332
|
+
use `ExactOIT` or `PeelOIT` for exact order. Radiance must be finite.
|
|
333
|
+
- Measured by `packages/oit/test/gpu/precise-check.mjs`: HDR sums within 0.5 of the analytic value at 1x and 4x, the
|
|
334
|
+
same result in any draw order, scene depth occluding bright layers, and edge coverage within 1 of `OIT`.
|
|
335
|
+
|
|
336
|
+
## Depth and motion for glass (`TransparentDepth`)
|
|
337
|
+
|
|
338
|
+
Transparent surfaces write no depth, so TAA, depth of field and SSAO look straight through them. `margoengine/oit/depth`
|
|
339
|
+
adds them to the scene depth after their colour has drawn, whichever pass drew it.
|
|
340
|
+
|
|
341
|
+
```ts
|
|
342
|
+
import {Engine} from 'margoengine/core';
|
|
343
|
+
import {OIT} from 'margoengine/oit';
|
|
344
|
+
import {TransparentDepth} from 'margoengine/oit/depth';
|
|
345
|
+
import {Material, createMesh} from 'margoengine/materials';
|
|
346
|
+
import {boxGeometry} from 'margoengine/geometry';
|
|
347
|
+
import {PostStack, FXAA} from 'margoengine/post';
|
|
348
|
+
import {TAA} from 'margoengine/taa';
|
|
349
|
+
const engine = await Engine.create(document.querySelector('canvas')!);
|
|
350
|
+
|
|
351
|
+
const stack = PostStack.attach(engine, [new FXAA()]);
|
|
352
|
+
const taa = TAA.attach(engine, {velocityOnly: true, depth: () => stack.depthTexture});
|
|
353
|
+
const pane = createMesh(engine, boxGeometry(2, 1, 0.1), new Material({color: '#9cc3e6', opacity: 0.4, transparent: true}));
|
|
354
|
+
OIT.attach(engine).add(pane);
|
|
355
|
+
const glass = TransparentDepth.attach(engine, {threshold: 0.2});
|
|
356
|
+
glass.add(pane); // into the depth
|
|
357
|
+
taa.add(glass.velocity(pane)); // its own motion vectors, for glass that moves
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
| Option | Default | Meaning |
|
|
361
|
+
| --- | --- | --- |
|
|
362
|
+
| `threshold` | 0.1 | Least alpha that enters the depth, above 0, at most 1. Native transmission always enters. Additive and multiply materials never do. |
|
|
363
|
+
| `order` | 8.5 | Frame graph order: after the transparent colour at 8, before TAA at 9 and SSAO at 10. |
|
|
364
|
+
|
|
365
|
+
- TAA: camera motion reprojects from the glass, and `glass.velocity(model)` gives moving glass its vectors. Where layers
|
|
366
|
+
overlap the nearest wins.
|
|
367
|
+
- Depth of field and SSAO read the glass through the same depth. Focus lands on it, and what is behind it blurs with it.
|
|
368
|
+
- Picking needs nothing from this entry: `Picking` takes BLEND primitives as they are, `picking.add(model)`.
|
|
369
|
+
|
|
370
|
+
Measured on an RTX A6000 (`npm run test:gpu --workspace packages/oit`), at 1x and 4x MSAA: glass writes the same depth
|
|
371
|
+
value as an opaque plane in its place, under OIT too, and parts under the threshold stay out. Moving glass shows its
|
|
372
|
+
vector where it read zero before, and a glass edge in front of a focused wall blurs only with this entry. All on
|
|
373
|
+
generated planes; time is not measured yet.
|
|
374
|
+
|
|
375
|
+
Limits:
|
|
376
|
+
|
|
377
|
+
- Everything depth-tested after `order` sees the glass as solid: lines (order 20) behind it disappear. A later `order`
|
|
378
|
+
keeps them, and then TAA, SSAO and the rest no longer see the glass.
|
|
379
|
+
- What is behind the glass gets the glass's depth, so it blurs and reprojects as the glass does. One depth per pixel.
|
|
380
|
+
- The surfaces draw once more, shader included, for depth alone.
|
|
381
|
+
- Without `model` in an order-independent pass its BLEND primitives still draw sorted; this entry works the same.
|
|
382
|
+
|
|
383
|
+
## Particles, splats and points (`addEffect`)
|
|
384
|
+
|
|
385
|
+
Particles, splats and points blend over the scene in their own passes, so glass and smoke land in draw order: a particle
|
|
386
|
+
behind a pane still paints over it. `addEffect` from `margoengine/oit/vfx` draws them in the same pass as the models,
|
|
387
|
+
so each pixel's layers meet in one place.
|
|
388
|
+
|
|
389
|
+
```ts
|
|
390
|
+
import {Engine} from 'margoengine/core';
|
|
391
|
+
import {ExactOIT} from 'margoengine/oit/exact';
|
|
392
|
+
import {addEffect} from 'margoengine/oit/vfx';
|
|
393
|
+
import {Material, createMesh} from 'margoengine/materials';
|
|
394
|
+
import {boxGeometry} from 'margoengine/geometry';
|
|
395
|
+
import {VFX} from 'margoengine/vfx';
|
|
396
|
+
import {Splats} from 'margoengine/splats';
|
|
397
|
+
import {PointsPlugin, PointCloud} from 'margoengine/points';
|
|
398
|
+
const engine = await Engine.create(document.querySelector('canvas')!);
|
|
399
|
+
|
|
400
|
+
const oit = ExactOIT.attach(engine); // or OIT, or MomentOIT
|
|
401
|
+
oit.add(createMesh(engine, boxGeometry(2, 1, 0.1), new Material({color: '#9cc3e6', opacity: 0.4, transparent: true})));
|
|
402
|
+
|
|
403
|
+
const remove = addEffect(oit, VFX.attach(engine, {capacity: 10_000, color: [1, 0.5, 0.1, 0.6]}));
|
|
404
|
+
addEffect(oit, Splats.attach(engine, {
|
|
405
|
+
positions: new Float32Array([0, 0, -1]),
|
|
406
|
+
covariances: new Float32Array([0.1, 0, 0, 0.1, 0, 0.1]),
|
|
407
|
+
colors: new Float32Array([0.2, 0.6, 1, 0.8]),
|
|
408
|
+
}));
|
|
409
|
+
const points = PointsPlugin.attach(engine);
|
|
410
|
+
points.add(new PointCloud({positions: new Float32Array([0, 0, 1]), material: {size: 8, transparent: true, opacity: 0.5}}));
|
|
411
|
+
addEffect(oit, points); // its transparent clouds and sprites
|
|
412
|
+
remove(); // the particles go back to their own draw
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
Fog, soft particles, particle textures and sphere-shaded points keep working in the pass. Disposing the effect takes it
|
|
416
|
+
out; disposing the pass puts every effect back on its own draw, bit for bit.
|
|
417
|
+
|
|
418
|
+
Measured on an RTX A6000 (`npm run test:gpu --workspace packages/oit`), at 1x and 4x MSAA, for particles, splats, points
|
|
419
|
+
and sprites alike: in `ExactOIT` with a 50% pane, each lands within 2 of 255 of the formula on either side of the pane.
|
|
420
|
+
On their own draws a red particle behind the pane reads (221, 17, 42), as if in front; in the pass it reads
|
|
421
|
+
(119, 17, 144). Alone in any pass each stays within 2 of 255 of its own draw. The view depth each hands the pass reads
|
|
422
|
+
3.5 for a layer 3.5 from the eye, under perspective and orthographic cameras. All on generated layers; time is not
|
|
423
|
+
measured yet.
|
|
424
|
+
|
|
425
|
+
Limits:
|
|
426
|
+
|
|
427
|
+
- `PointsPlugin`'s clouds and sprites join if they are `transparent`, test depth and write none. Additive ones need
|
|
428
|
+
`OIT` with `blending`, and keep their own draw otherwise; there they add to the canvas alpha as additive materials do.
|
|
429
|
+
- A pass draws what the plugin culled for that view, so the point budget still applies. Surfels stay out.
|
|
430
|
+
- Sphere-shaded points test depth at their flat quad, without the sphere's depth, and take no eye-dome lighting.
|
|
431
|
+
- `Splats` stop sorting while in a pass: `gpuState.sorted` holds the keys in data order. Splats far past `layers` deep
|
|
432
|
+
lose the rest under `ExactOIT`, and weighted blending of dense clouds is far from the sorted picture: a trained capture
|
|
433
|
+
wants its own sorted draw unless glass must cut through it.
|
|
434
|
+
- `MomentOIT` has no box for an effect, so with one added its default range is the camera's near and far. On a pane and
|
|
435
|
+
one effect that cost nothing, at near 0.05 and far 100: the far layer read 112 of 255, against 106 with
|
|
436
|
+
`depthRange: [3, 6]` and 119 exact. Many layers close together are untested; set `depthRange` if they blur.
|
|
437
|
+
- With 4x MSAA a textured particle covers the samples of its triangle, not only of its quad: under a pixel at the edge.
|
|
438
|
+
- `TransparentDepth` takes models only; effects stay out of the depth.
|
|
439
|
+
|
|
440
|
+
## Real files
|
|
441
|
+
|
|
442
|
+
`packages/oit/test/gpu/real-check.mjs` runs six Khronos sample models through every pass at 4x MSAA from eight sides, in
|
|
443
|
+
a real environment, with no GPU error. Share of pixels more than 2 of 255 apart, the worst of the eight sides, RTX
|
|
444
|
+
A6000:
|
|
445
|
+
|
|
446
|
+
| Model | Parts in `ExactOIT` | Exact against sorted | `blending` against sorted | Weighted against exact | Moments against exact |
|
|
447
|
+
| --- | --- | --- | --- | --- | --- |
|
|
448
|
+
| GlassVaseFlowers | 1 BLEND | 2.56%, worst 95 | 3.29%, worst 117 | 2.30%, worst 45 | 0.63%, worst 43 |
|
|
449
|
+
| DiffuseTransmissionPlant | 4 BLEND | under 0.01%, worst 13 | under 0.01%, worst 18 | under 0.01%, worst 18 | 0.01%, worst 28 |
|
|
450
|
+
| AlphaBlendModeTest | 2 BLEND | 0.02%, worst 16 | 0.06%, worst 26 | 0.07%, worst 17 | 0.06%, worst 19 |
|
|
451
|
+
| LightsPunctualLamp | 1 BLEND | 0, nothing overlaps | 0.17%, worst 64 | 0 | 0 |
|
|
452
|
+
| TransmissionTest | none | 0, nothing taken | 0.99%, worst 247 | 0 | 0 |
|
|
453
|
+
| CommercialRefrigerator | none | 0, nothing taken | 0.02%, worst 27 | 0 | 0 |
|
|
454
|
+
|
|
455
|
+
The Moments column predates trigonometric moments (2026-11-07). Since then, on an Apple M6 (Chrome), GlassVaseFlowers
|
|
456
|
+
reads 0.68%, worst 42, against exact.
|
|
457
|
+
|
|
458
|
+
What that shows:
|
|
459
|
+
|
|
460
|
+
- Most real glass is `KHR_materials_transmission`, which only `OIT` with `blending` takes. `ExactOIT` and `MomentOIT`
|
|
461
|
+
leave those models as they were: on the last two rows they draw nothing.
|
|
462
|
+
- Where layers stack, moments stay closest to exact, as on generated planes.
|
|
463
|
+
- A BLEND part that sits inside native transmission draws over it unless `blending` is on, because the transmission
|
|
464
|
+
stays in the scene pass: the vase's 2.56%.
|
|
465
|
+
- `blending` gives up what sorted glass does between glass parts. Seen along a row of spheres in TransmissionTest, each
|
|
466
|
+
sphere's light adds up and none dims the next, so 1% of the picture moves by as much as 247 of 255. No reference
|
|
467
|
+
picture says which is right there; for glass behind glass, stay on the sorted path.
|
|
468
|
+
- Two identical sorted runs of GlassVaseFlowers can differ on a few pixels by up to 9 of 255 (docs/known-bugs.md), so
|
|
469
|
+
gaps that small mean nothing.
|
|
470
|
+
|
|
471
|
+
`TransparentDepth` brought pixels nearer on all six and none farther (9.72% of the vase's picture, 13.58% of the
|
|
472
|
+
refrigerator's). Particles through the middle of each model draw with its BLEND parts in `ExactOIT` with no GPU error.
|
|
473
|
+
|
|
474
|
+
## Hashed alpha
|
|
475
|
+
|
|
476
|
+
Noise-based transparency that writes depth, for hair and foliage under TAA, shadows and SSAO, is a material feature,
|
|
477
|
+
not a pass: `alphaHash()` from `margoengine/materials/alpha-hash` (materials README, "Hashed alpha").
|
|
478
|
+
|
|
479
|
+
## How it works
|
|
480
|
+
|
|
481
|
+
- BLEND primitives leave the beauty pass. In OIT's pass (frame graph order 8 by default: after the scene, edges and
|
|
482
|
+
section caps, before TAA, SSAO and the post composites) they draw with their normal lit shader into two targets,
|
|
483
|
+
depth tested against the opaque scene and not writing depth: accumulation (`rgba16float`, additive:
|
|
484
|
+
premultiplied colour × w, alpha × w) and revealage (`rg16float`, multiplied by 1 − alpha; the second channel marks
|
|
485
|
+
[linear-light layers](#linear-light-layers-blendspace-linear)).
|
|
486
|
+
- Weight: `w = clamp(10 / (depth / depthScale)^6, 0.01, 1000)`, `depth` the view-space depth (perspective and
|
|
487
|
+
orthographic alike), `depthScale` by default the camera's distance to its target each frame. A layer 10 % nearer
|
|
488
|
+
than another weighs 1.9× as much.
|
|
489
|
+
- A composite pass draws one full-screen triangle over the scene: average colour `accum.rgb / accum.a` at coverage
|
|
490
|
+
`1 − revealage`, blended premultiplied-over, so `TransparentCanvas` keeps valid alpha. It is the only pass that reads
|
|
491
|
+
textures (the two targets, with `textureLoad`); the transparent draws add no texture bindings, so the 16-texture
|
|
492
|
+
budget in the glTF PBR README is unchanged.
|
|
493
|
+
- MSAA: with a 4x scene the two targets are 4x and resolve at the end of the pass; the composite runs per pixel on the
|
|
494
|
+
4x scene target. Inside `PostStack`, Tonemap, TAA or DOF captures the pass follows their target (HDR `rgba16float`
|
|
495
|
+
colours accumulate linear). No transparent primitive drawn: no OIT pass at all.
|
|
496
|
+
|
|
497
|
+
## Accuracy (real GPU, `examples/package-benchmarks/oit-gpu.mjs`)
|
|
498
|
+
|
|
499
|
+
Arc 140V, Edge 155, AC power, results in `examples/package-benchmarks/results/oit-gpu.json`.
|
|
500
|
+
|
|
501
|
+
Colour, centre pixel, 0-255 per channel: unlit planes (materials `'basic'`) at 0.6/0.3/0/-0.3 m in front of an opaque grey
|
|
502
|
+
plane, camera 4 m away, alphas 0.5/0.4/0.6/0.5 in red, green, blue, yellow. The same at MSAA 1x and 4x, perspective and
|
|
503
|
+
orthographic.
|
|
504
|
+
|
|
505
|
+
| Layers | OIT vs analytic sorted blending (approximation error) | OIT vs the WBOIT formula (implementation error) | Sorted path vs analytic |
|
|
506
|
+
| --- | --- | --- | --- |
|
|
507
|
+
| 1 | 0.5 (exact) | 0.5 | 0.5 |
|
|
508
|
+
| 2 | 4.6 | 0.3 | 0.4 |
|
|
509
|
+
| 4 | 8.7 | 0.6 | 0.8 |
|
|
510
|
+
|
|
511
|
+
Over a `TransparentCanvas` the recovered alpha is within 0.002 of 1 − Π(1 − alpha) and the premultiplied colour within
|
|
512
|
+
1.1 for 1, 2 and 4 layers. Inside a `PostStack` (Bloom + FXAA, HDR capture) one layer equals the sorted path exactly.
|
|
513
|
+
|
|
514
|
+
Popping: a 50 % double-sided housing over two opaque gears and a 55 % gear, orbited 360° in 1° steps at 640×360,
|
|
515
|
+
MSAA 4x, against a reference that sorts every translucent triangle separately. A pop is a pixel that jumps by more than
|
|
516
|
+
16 between steps where the reference stays within 3.
|
|
517
|
+
|
|
518
|
+
| Path | Steps with > 1 % of covered pixels popping | Steps with > 0.1 % | Worst step | Mean / p95 error vs reference |
|
|
519
|
+
| --- | --- | --- | --- | --- |
|
|
520
|
+
| Sorted per primitive (default) | 13 | 78 | 1.41 % | 7.5 / 29.8 |
|
|
521
|
+
| OIT | 0 | 5 | 0.36 % | 6.7 / 22.8 |
|
|
522
|
+
|
|
523
|
+
GPU time (Stats timestamp profiler, median, MSAA 4x, the same scene; diagnostic, measured while other jobs kept the
|
|
524
|
+
CPU at 45-100 %):
|
|
525
|
+
|
|
526
|
+
| Resolution | Opaque only | + sorted transparency | + OIT | OIT accumulate pass | OIT composite pass |
|
|
527
|
+
| --- | --- | --- | --- | --- | --- |
|
|
528
|
+
| 1280×720 | 0.07 ms | 0.39 ms (+0.33) | 0.62 ms (+0.56) | 0.33 ms | 0.23 ms |
|
|
529
|
+
| 1920×1080 | 0.13 ms | 0.72 ms (+0.59) | 1.41 ms (+1.28) | 0.79 ms | 0.49 ms |
|
|
530
|
+
|
|
531
|
+
## Limits (the WBOIT approximation)
|
|
532
|
+
|
|
533
|
+
- It is an approximation, exact only for one layer. With several overlapping layers the colour is a depth-weighted
|
|
534
|
+
average, not the front-to-back result: a near layer does not fully hide a far one, and two layers at similar depth
|
|
535
|
+
mix whatever their order. Errors grow with layer count and with colour contrast between layers (table above).
|
|
536
|
+
Choose `depthScale` near the distance of the transparent parts if the default (orbit distance) is far off.
|
|
537
|
+
- Outside about 0.46 to 3.2 × `depthScale` the weight is clamped: nearer or farther layers lose depth ordering among
|
|
538
|
+
themselves (they still blend order-independently). Very bright HDR colours (above ~60 in linear units) can overflow
|
|
539
|
+
the `rgba16float` accumulation when stacked.
|
|
540
|
+
- By default transmission (KHR_materials_transmission tints) and materials with custom blending (`additive`,
|
|
541
|
+
`multiply`, `none`) keep the sorted path; [`blending`](#tints-glows-and-glass-blending) brings all but `none` in.
|
|
542
|
+
Depth overrides (`depthWrite`, `depthTest: false`) always keep it. Transparent instances (`PBRInstances`) go through
|
|
543
|
+
OIT like other primitives.
|
|
544
|
+
- Transparent surfaces write no depth, as before: SSAO and DOF see the opaque scene only, and TAA has no motion vectors
|
|
545
|
+
for them, until [`TransparentDepth`](#depth-and-motion-for-glass-transparentdepth) adds them.
|
|
546
|
+
- Cost: two 1x targets (12 bytes per pixel) plus 4x copies (48 bytes per pixel) under MSAA, and one full-screen
|
|
547
|
+
composite whenever something transparent is visible: about twice the GPU time of sorted blending in the table above.
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
## Reference measurement
|
|
551
|
+
|
|
552
|
+
The comparison was measured with a rival harness that was removed on 2026-11-26.
|
|
553
|
+
The external depth-peeling install stays in engine-bench/batch-168 (setup and versions in
|
|
554
|
+
[reproduce](../../docs/whitepaper/reproduce.md)). Five binary-colour panes are compared against exact
|
|
555
|
+
ray-sorted emission/coverage transport, with common sRGB encoding after readback. The measured picture
|
|
556
|
+
MAE is 1.567519 versus the reference; weighted blending remains an approximation. The diagnostic busy-machine
|
|
557
|
+
frame-plus-completion minimum is 2.000 ms. See [all results and caveats](../../docs/whitepaper/perf-vs-rivals.md).
|
|
558
|
+
|
|
559
|
+
## Preview tier contract
|
|
560
|
+
|
|
561
|
+
GPU-only attachments refuse the small WebGL2 preview during setup with `MargoError` code `unsupported` and a help link. Native WebGPU and the full WebGL2 compatibility layer expose the WebGPU-shaped device these passes need. Use [automatic escalation](../webgl2-compat/README.md#automatic-escalation) with awaited setup to load the full layer when required. Existing real preview routes remain available.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region ../shared/errors.js
|
|
2
|
+
var e = (e, t) => Object.assign(Error(t), {
|
|
3
|
+
name: "MargoError",
|
|
4
|
+
code: e
|
|
5
|
+
}), t = (e, t) => `${e}; see https://margoengine.com/errors#${t}`, n = (n, r, i = n) => e(n, t(r, i));
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region ../shared/compat.js
|
|
8
|
+
function r(e, t, r = "full") {
|
|
9
|
+
let i = {
|
|
10
|
+
ldr: 0,
|
|
11
|
+
"half-float": 1,
|
|
12
|
+
full: 2
|
|
13
|
+
};
|
|
14
|
+
if (!Object.hasOwn(i, r)) throw TypeError("minimum must be full, half-float or ldr");
|
|
15
|
+
let a = e?.adapter?.capabilities?.tier;
|
|
16
|
+
if (a && i[a] < i[r]) throw n("unsupported", `${t} requires the ${r} compatibility tier; this device is ${a}`, "compatibility-layer");
|
|
17
|
+
}
|
|
18
|
+
function i(e) {
|
|
19
|
+
return e?.adapterInfo?.vendor === "margo-webgl2" ? "webgl2" : "native";
|
|
20
|
+
}
|
|
21
|
+
function a(e) {
|
|
22
|
+
return i(e) === "native" && e?.features?.has("core-features-and-limits") === !1;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { n as i, a as n, r, i as t };
|
package/dist/depth.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin, type FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
import type { PBRModel, PBRTransparency } from '@margoengine/gltf/pbr';
|
|
5
|
+
/** `TransparentDepth` settings, fixed at construction. */
|
|
6
|
+
export interface TransparentDepthOptions {
|
|
7
|
+
/** Frame graph order: after the transparent colour (8), before TAA (9), SSAO (10) and the post composites. Default 8.5. */
|
|
8
|
+
order?: number;
|
|
9
|
+
/** Least alpha that enters the depth, above 0, at most 1: fainter parts stay out, as if not there. Default 0.1. */
|
|
10
|
+
threshold?: number;
|
|
11
|
+
}
|
|
12
|
+
/** What `TAA.add` takes (`VelocitySource`): the model's transparent primitives. */
|
|
13
|
+
export interface TransparentVelocitySource {
|
|
14
|
+
readonly shadowPrimitives: PBRModel['tintPrimitives'];
|
|
15
|
+
readonly disposed: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Writes transparent surfaces into the scene depth: `glass.add(model)` draws the model's BLEND primitives and native
|
|
19
|
+
* transmission, depth only, after their colour. TAA, depth of field, SSAO and anything else that reads depth later then
|
|
20
|
+
* find the glass, not what is behind it; `taa.add(glass.velocity(model))` adds its motion. Later depth-tested drawing
|
|
21
|
+
* sees it too, so lines at order 20 behind the glass disappear; raise `order` past them to keep them, at the price of TAA
|
|
22
|
+
* and the rest. WebGPU only.
|
|
23
|
+
* @throws RangeError from the constructor for an `order` or `threshold` out of range.
|
|
24
|
+
* @throws MargoError `'unsupported'` on a WebGL engine, `'disposed'` after `dispose()`, `'state'` from `add` before
|
|
25
|
+
* `attach`.
|
|
26
|
+
*/
|
|
27
|
+
export declare class TransparentDepth implements EnginePlugin, PBRTransparency {
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly order: number;
|
|
30
|
+
readonly targets: readonly GPUColorTargetState[];
|
|
31
|
+
readonly blends = true;
|
|
32
|
+
readonly depthWrite = true;
|
|
33
|
+
private readonly threshold;
|
|
34
|
+
private readonly code;
|
|
35
|
+
private readonly models;
|
|
36
|
+
private engine?;
|
|
37
|
+
private device?;
|
|
38
|
+
private disposed;
|
|
39
|
+
private uniform?;
|
|
40
|
+
private context?;
|
|
41
|
+
private pass?;
|
|
42
|
+
static attach(engine: EngineKernel, options?: TransparentDepthOptions): TransparentDepth;
|
|
43
|
+
constructor({ order, threshold }?: TransparentDepthOptions);
|
|
44
|
+
/** Writes a gltf\/pbr model's transparent surfaces (or a transparent materials mesh) into the depth. Returns a remover. */
|
|
45
|
+
add(model: PBRModel): () => void;
|
|
46
|
+
/** The model's transparent primitives for `taa.add`: moving glass gets its own motion vectors where it wrote depth. */
|
|
47
|
+
velocity(model: PBRModel): TransparentVelocitySource;
|
|
48
|
+
setup(engine: Engine): void;
|
|
49
|
+
registerPasses(graph: FrameGraph): void;
|
|
50
|
+
/** @internal gltf\/pbr transparency hook. */
|
|
51
|
+
declarations(group: number): string;
|
|
52
|
+
/** @internal gltf\/pbr transparency hook. */
|
|
53
|
+
bindings(): GPUBindGroupEntry[];
|
|
54
|
+
/** @internal gltf\/pbr transparency hook: the depth pass, begun on the first transparent draw of the frame. */
|
|
55
|
+
begin(): GPURenderPassEncoder;
|
|
56
|
+
private render;
|
|
57
|
+
onDeviceRestored(engine: Engine): void;
|
|
58
|
+
/** Stops writing and frees the GPU resources. */
|
|
59
|
+
dispose(): void;
|
|
60
|
+
[Symbol.dispose]: () => void;
|
|
61
|
+
}
|