@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/dist/index.js
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const s=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { a as e, i as t, n, o as r, r as i } from "./pass-D7U8kWzv.js";
|
|
5
|
+
import { i as a, t as o } from "./pass-DdYrWNpT.js";
|
|
6
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
|
+
//#region src/index.ts
|
|
8
|
+
var c = 120, l = (e) => `
|
|
9
|
+
override margoLinear:bool=false;
|
|
10
|
+
struct MargoTransparent {@location(0) accum:vec4f, @location(1) reveal:vec4f}
|
|
11
|
+
@group(${e}) @binding(${c}) var<uniform> margoOIT:vec4f;
|
|
12
|
+
fn margoTransparentAt(color:vec4f,clip:vec4f,depth:f32)->MargoTransparent {
|
|
13
|
+
let a=clamp(color.a,0.0,1.0); let d=max(depth*margoOIT.x,1e-4);
|
|
14
|
+
let w=a*clamp(10.0/pow(d,6.0),0.01,1000.0); var o:MargoTransparent;
|
|
15
|
+
o.accum=vec4f(color.rgb*w,w); o.reveal=vec4f(a,select(0.0,1.0,margoLinear),0,0); return o;
|
|
16
|
+
}
|
|
17
|
+
`, u = (e) => `
|
|
18
|
+
override margoKind:u32=0u;
|
|
19
|
+
override margoLinear:bool=false;
|
|
20
|
+
struct MargoTransparent {@location(0) accum:vec4f, @location(1) reveal:vec4f, @location(2) added:vec4f, @location(3) linear:vec4f}
|
|
21
|
+
@group(${e}) @binding(${c}) var<uniform> margoOIT:vec4f;
|
|
22
|
+
fn margoTransparentTint(share:vec4f)->MargoTransparent {
|
|
23
|
+
var o:MargoTransparent; o.reveal=vec4f(share.rgb,1.0); o.linear=vec4f(0); return o;
|
|
24
|
+
}
|
|
25
|
+
fn margoTransparentAt(color:vec4f,clip:vec4f,depth:f32)->MargoTransparent {
|
|
26
|
+
let a=clamp(color.a,0.0,1.0); var o:MargoTransparent; o.reveal=vec4f(1.0); o.linear=vec4f(select(0.0,1.0,margoLinear));
|
|
27
|
+
if(margoKind==0u){
|
|
28
|
+
let d=max(depth*margoOIT.x,1e-4);
|
|
29
|
+
let w=a*clamp(10.0/pow(d,6.0),0.01,1000.0); o.accum=vec4f(color.rgb*w,w); o.reveal=vec4f(1.0-a);
|
|
30
|
+
} else if(margoKind==1u){o.added=vec4f(color.rgb*a,a);}
|
|
31
|
+
else if(margoKind==2u){o.reveal=vec4f(color.rgb,1.0);}
|
|
32
|
+
else {o.added=vec4f(color.rgb,a);}
|
|
33
|
+
return o;
|
|
34
|
+
}
|
|
35
|
+
`, d = "\n@group(0) @binding(0) var accumMap:texture_2d<f32>;\n@group(0) @binding(1) var revealMap:texture_2d<f32>;\n@vertex fn vertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f {return vec4f(f32(i&1u)*4-1,f32(i>>1u)*4-1,0,1);}\n@fragment fn fragment(@builtin(position) p:vec4f)->@location(0) vec4f {\n let q=vec2i(p.xy); let a=textureLoad(accumMap,q,0); return vec4f(a.rgb/max(a.a,1e-5),1-textureLoad(revealMap,q,0).r);\n}\n", f = "\n@group(0) @binding(0) var accumMap:texture_2d<f32>;\n@group(0) @binding(1) var revealMap:texture_2d<f32>;\n@group(0) @binding(2) var addedMap:texture_2d<f32>;\n@vertex fn vertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f {return vec4f(f32(i&1u)*4-1,f32(i>>1u)*4-1,0,1);}\n@fragment fn dim(@builtin(position) p:vec4f)->@location(0) vec4f {return textureLoad(revealMap,vec2i(p.xy),0);}\n@fragment fn light(@builtin(position) p:vec4f)->@location(0) vec4f {\n let q=vec2i(p.xy); let a=textureLoad(accumMap,q,0); let r=textureLoad(revealMap,q,0).a; let l=textureLoad(addedMap,q,0);\n return vec4f(a.rgb/max(a.a,1e-5)*(1-r)+l.rgb,1-r+l.a);\n}\n", p = (e) => `
|
|
36
|
+
@group(0) @binding(0) var accumMap:texture_2d<f32>;
|
|
37
|
+
@group(0) @binding(1) var revealMap:texture_2d<f32>;
|
|
38
|
+
@group(0) @binding(2) var sceneMap:texture_2d<f32>;
|
|
39
|
+
${e ? "@group(0) @binding(3) var addedMap:texture_2d<f32>; @group(0) @binding(4) var markMap:texture_2d<f32>;" : ""}
|
|
40
|
+
fn decode(c:vec3f)->vec3f {let x=max(c,vec3f(0));return select(x/12.92,pow((x+0.055)/1.055,vec3f(2.4)),x>vec3f(0.04045));}
|
|
41
|
+
fn encode(c:vec3f)->vec3f {let x=max(c,vec3f(0));return select(x*12.92,1.055*pow(x,vec3f(1.0/2.4))-0.055,x>vec3f(0.0031308));}
|
|
42
|
+
@vertex fn vertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f {return vec4f(f32(i&1u)*4-1,f32(i>>1u)*4-1,0,1);}
|
|
43
|
+
@fragment fn fragment(@builtin(position) p:vec4f)->@location(0) vec4f {
|
|
44
|
+
let q=vec2i(p.xy); let v=textureLoad(revealMap,q,0);
|
|
45
|
+
${e ? "let mark=textureLoad(markMap,q,0).r; let r=v.a; let dim=v.rgb; let l=textureLoad(addedMap,q,0);" : "let mark=v.g; let r=v.r; let dim=vec3f(r); let l=vec4f(0);"}
|
|
46
|
+
if(mark>=1.0){discard;}
|
|
47
|
+
let a=textureLoad(accumMap,q,0); let avg=a.rgb/max(a.a,1e-5); let d=textureLoad(sceneMap,q,0);
|
|
48
|
+
let shown=d.rgb*dim+avg*(1-r)+l.rgb; let light=encode(decode(d.rgb)*dim+decode(avg)*(1-r))+l.rgb;
|
|
49
|
+
return vec4f(mix(light,shown,mark),d.a*r+1-r+l.a);
|
|
50
|
+
}
|
|
51
|
+
`, m = {
|
|
52
|
+
color: {
|
|
53
|
+
srcFactor: "one",
|
|
54
|
+
dstFactor: "one"
|
|
55
|
+
},
|
|
56
|
+
alpha: {
|
|
57
|
+
srcFactor: "one",
|
|
58
|
+
dstFactor: "one"
|
|
59
|
+
}
|
|
60
|
+
}, h = {
|
|
61
|
+
color: {
|
|
62
|
+
srcFactor: "zero",
|
|
63
|
+
dstFactor: "src"
|
|
64
|
+
},
|
|
65
|
+
alpha: {
|
|
66
|
+
srcFactor: "zero",
|
|
67
|
+
dstFactor: "src"
|
|
68
|
+
}
|
|
69
|
+
}, g = {
|
|
70
|
+
color: {
|
|
71
|
+
srcFactor: "zero",
|
|
72
|
+
dstFactor: "one-minus-src"
|
|
73
|
+
},
|
|
74
|
+
alpha: {
|
|
75
|
+
srcFactor: "zero",
|
|
76
|
+
dstFactor: "one-minus-src"
|
|
77
|
+
}
|
|
78
|
+
}, _ = {
|
|
79
|
+
color: {
|
|
80
|
+
srcFactor: "src-alpha",
|
|
81
|
+
dstFactor: "one-minus-src-alpha"
|
|
82
|
+
},
|
|
83
|
+
alpha: {
|
|
84
|
+
srcFactor: "one",
|
|
85
|
+
dstFactor: "one-minus-src-alpha"
|
|
86
|
+
}
|
|
87
|
+
}, v = class v extends o {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
88
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
89
|
+
let n = new v(t);
|
|
90
|
+
return s(e).use(n), n;
|
|
91
|
+
}
|
|
92
|
+
constructor({ order: e = 8, depthScale: t, blending: n = !1 } = {}) {
|
|
93
|
+
if (t !== void 0 && !(t > 0 && Number.isFinite(t))) throw RangeError("OIT order must be > 0 and depthScale a positive number");
|
|
94
|
+
super(e), this.layout = [{
|
|
95
|
+
binding: c,
|
|
96
|
+
visibility: 2,
|
|
97
|
+
buffer: {}
|
|
98
|
+
}], this.code = /* @__PURE__ */ new Map(), this.surfaceCode = /* @__PURE__ */ new Map(), this.values = /* @__PURE__ */ new Float32Array(4), this.textures = [], this.views = [], this.size = "", this.pipelines = /* @__PURE__ */ new Map(), this.blendedPipelines = /* @__PURE__ */ new Map(), this.groups = /* @__PURE__ */ new Map(), this.linearPipelines = /* @__PURE__ */ new Map(), this.depthScale = t, this.blends = !!n, this.targets = n ? [
|
|
99
|
+
{
|
|
100
|
+
format: "rgba16float",
|
|
101
|
+
blend: m
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
format: "rgba16float",
|
|
105
|
+
blend: h
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
format: "rgba16float",
|
|
109
|
+
blend: m
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
format: "r8unorm",
|
|
113
|
+
blend: g
|
|
114
|
+
}
|
|
115
|
+
] : [{
|
|
116
|
+
format: "rgba16float",
|
|
117
|
+
blend: m
|
|
118
|
+
}, {
|
|
119
|
+
format: "rg16float",
|
|
120
|
+
blend: g
|
|
121
|
+
}];
|
|
122
|
+
}
|
|
123
|
+
setup(e) {
|
|
124
|
+
super.setup(e), this.uniform = e.gpu.createBuffer({
|
|
125
|
+
size: 16,
|
|
126
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
declarations(e) {
|
|
130
|
+
return n(this.code, e, (e) => this.surface(e) + a);
|
|
131
|
+
}
|
|
132
|
+
surface(e) {
|
|
133
|
+
return n(this.surfaceCode, e, this.blends ? u : l);
|
|
134
|
+
}
|
|
135
|
+
bindings() {
|
|
136
|
+
return [{
|
|
137
|
+
binding: c,
|
|
138
|
+
resource: this.binding ?? { buffer: this.uniform }
|
|
139
|
+
}];
|
|
140
|
+
}
|
|
141
|
+
begin() {
|
|
142
|
+
if (this.pass) return this.pass;
|
|
143
|
+
let e = this.context, t = this.targets.length, n = this.views.length > t, i = this.views, a = this.pass = e.encoder.beginRenderPass({
|
|
144
|
+
label: "oit:accumulate",
|
|
145
|
+
colorAttachments: this.targets.map((e, r) => ({
|
|
146
|
+
view: n ? i[r + t] : i[r],
|
|
147
|
+
resolveTarget: n ? i[r] : void 0,
|
|
148
|
+
clearValue: r === 1 || r === 3 ? [
|
|
149
|
+
1,
|
|
150
|
+
1,
|
|
151
|
+
1,
|
|
152
|
+
1
|
|
153
|
+
] : [
|
|
154
|
+
0,
|
|
155
|
+
0,
|
|
156
|
+
0,
|
|
157
|
+
0
|
|
158
|
+
],
|
|
159
|
+
loadOp: "clear",
|
|
160
|
+
storeOp: n ? "discard" : "store"
|
|
161
|
+
})),
|
|
162
|
+
depthStencilAttachment: {
|
|
163
|
+
view: e.depth,
|
|
164
|
+
depthReadOnly: !0,
|
|
165
|
+
stencilReadOnly: !0
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
return r(a, e), a;
|
|
169
|
+
}
|
|
170
|
+
render(t) {
|
|
171
|
+
if (i(t.device, t.view ?? t), this.disposed || this.empty) return;
|
|
172
|
+
let n = t.descriptor.multisample?.count ?? 1, a = t.width + "x" + t.height + "x" + n, o = t.device;
|
|
173
|
+
if (a !== this.size) {
|
|
174
|
+
this.release(), this.size = a;
|
|
175
|
+
let e = this.targets.length;
|
|
176
|
+
for (let r = 0; r < (n > 1 ? e * 2 : e); r++) {
|
|
177
|
+
let i = this.targets[r % e].format, a = r < e ? 1 : n, s = o.createTexture({
|
|
178
|
+
label: "oit:" + i,
|
|
179
|
+
size: [t.width, t.height],
|
|
180
|
+
format: i,
|
|
181
|
+
sampleCount: a,
|
|
182
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | (a === 1 ? GPUTextureUsage.TEXTURE_BINDING : 0)
|
|
183
|
+
});
|
|
184
|
+
this.textures.push(s), this.views.push(s.createView());
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
let s = this.depthScale ?? Math.hypot(...t.camera.position.map((e, n) => e - t.camera.target[n]));
|
|
188
|
+
this.values[0] = 1 / (s || 1), this.binding = e(o, this.uniform, this.values, t.view ?? t), this.scene = void 0;
|
|
189
|
+
let c;
|
|
190
|
+
try {
|
|
191
|
+
if (!this.drawModels(t)) return;
|
|
192
|
+
c = this.scene?.();
|
|
193
|
+
} finally {
|
|
194
|
+
this.binding = void 0, this.scene = void 0;
|
|
195
|
+
}
|
|
196
|
+
if (this.blends) return this.compositeBlended(t, n, c);
|
|
197
|
+
let l = t.format + n, u = this.pipelines.get(l);
|
|
198
|
+
if (!u) {
|
|
199
|
+
let e = o.createShaderModule({ code: d });
|
|
200
|
+
this.pipelines.set(l, u = o.createRenderPipeline({
|
|
201
|
+
label: "oit:composite",
|
|
202
|
+
layout: "auto",
|
|
203
|
+
vertex: {
|
|
204
|
+
module: e,
|
|
205
|
+
entryPoint: "vertex"
|
|
206
|
+
},
|
|
207
|
+
fragment: {
|
|
208
|
+
module: e,
|
|
209
|
+
entryPoint: "fragment",
|
|
210
|
+
targets: [{
|
|
211
|
+
format: t.format,
|
|
212
|
+
blend: _
|
|
213
|
+
}]
|
|
214
|
+
},
|
|
215
|
+
multisample: { count: n }
|
|
216
|
+
}));
|
|
217
|
+
}
|
|
218
|
+
let f = this.groups.get(u);
|
|
219
|
+
f || this.groups.set(u, f = o.createBindGroup({
|
|
220
|
+
layout: u.getBindGroupLayout(0),
|
|
221
|
+
entries: [{
|
|
222
|
+
binding: 0,
|
|
223
|
+
resource: this.views[0]
|
|
224
|
+
}, {
|
|
225
|
+
binding: 1,
|
|
226
|
+
resource: this.views[1]
|
|
227
|
+
}]
|
|
228
|
+
}));
|
|
229
|
+
let p = t.encoder.beginRenderPass({
|
|
230
|
+
label: "oit:composite",
|
|
231
|
+
colorAttachments: [{
|
|
232
|
+
view: t.color,
|
|
233
|
+
loadOp: "load",
|
|
234
|
+
storeOp: "store"
|
|
235
|
+
}]
|
|
236
|
+
});
|
|
237
|
+
r(p, t), p.setPipeline(u), p.setBindGroup(0, f), p.draw(3), c && this.drawLinear(p, t, n, c), p.end();
|
|
238
|
+
}
|
|
239
|
+
drawLinear(e, t, n, r) {
|
|
240
|
+
let i = t.device, a = t.format + n, o = this.linearPipelines.get(a);
|
|
241
|
+
if (!o) {
|
|
242
|
+
let e = i.createShaderModule({ code: p(this.blends) });
|
|
243
|
+
this.linearPipelines.set(a, o = i.createRenderPipeline({
|
|
244
|
+
label: "oit:composite-linear",
|
|
245
|
+
layout: "auto",
|
|
246
|
+
vertex: {
|
|
247
|
+
module: e,
|
|
248
|
+
entryPoint: "vertex"
|
|
249
|
+
},
|
|
250
|
+
fragment: {
|
|
251
|
+
module: e,
|
|
252
|
+
entryPoint: "fragment",
|
|
253
|
+
targets: [{ format: t.format }]
|
|
254
|
+
},
|
|
255
|
+
multisample: { count: n }
|
|
256
|
+
}));
|
|
257
|
+
}
|
|
258
|
+
let s = this.linearGroup;
|
|
259
|
+
if (s?.view !== r || s.pipeline !== o) {
|
|
260
|
+
let e = this.views, t = this.blends ? [
|
|
261
|
+
e[0],
|
|
262
|
+
e[1],
|
|
263
|
+
r,
|
|
264
|
+
e[2],
|
|
265
|
+
e[3]
|
|
266
|
+
] : [
|
|
267
|
+
e[0],
|
|
268
|
+
e[1],
|
|
269
|
+
r
|
|
270
|
+
];
|
|
271
|
+
this.linearGroup = s = {
|
|
272
|
+
view: r,
|
|
273
|
+
pipeline: o,
|
|
274
|
+
group: i.createBindGroup({
|
|
275
|
+
layout: o.getBindGroupLayout(0),
|
|
276
|
+
entries: t.map((e, t) => ({
|
|
277
|
+
binding: t,
|
|
278
|
+
resource: e
|
|
279
|
+
}))
|
|
280
|
+
})
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
e.setPipeline(o), e.setBindGroup(0, s.group), e.draw(3);
|
|
284
|
+
}
|
|
285
|
+
compositeBlended(e, t, n) {
|
|
286
|
+
let i = e.device, a = e.format + t, o = this.blendedPipelines.get(a);
|
|
287
|
+
if (!o) {
|
|
288
|
+
let n = i.createShaderModule({ code: f }), r = i.createPipelineLayout({ bindGroupLayouts: [i.createBindGroupLayout({ entries: [
|
|
289
|
+
0,
|
|
290
|
+
1,
|
|
291
|
+
2
|
|
292
|
+
].map((e) => ({
|
|
293
|
+
binding: e,
|
|
294
|
+
visibility: 2,
|
|
295
|
+
texture: { sampleType: "unfilterable-float" }
|
|
296
|
+
})) })] }), s = (a, o) => i.createRenderPipeline({
|
|
297
|
+
label: "oit:composite",
|
|
298
|
+
layout: r,
|
|
299
|
+
vertex: {
|
|
300
|
+
module: n,
|
|
301
|
+
entryPoint: "vertex"
|
|
302
|
+
},
|
|
303
|
+
fragment: {
|
|
304
|
+
module: n,
|
|
305
|
+
entryPoint: a,
|
|
306
|
+
targets: [{
|
|
307
|
+
format: e.format,
|
|
308
|
+
blend: o
|
|
309
|
+
}]
|
|
310
|
+
},
|
|
311
|
+
multisample: { count: t }
|
|
312
|
+
});
|
|
313
|
+
this.blendedPipelines.set(a, o = [s("dim", h), s("light", m)]);
|
|
314
|
+
}
|
|
315
|
+
let s = this.groups.get(o[0]);
|
|
316
|
+
s || this.groups.set(o[0], s = i.createBindGroup({
|
|
317
|
+
layout: o[0].getBindGroupLayout(0),
|
|
318
|
+
entries: [
|
|
319
|
+
0,
|
|
320
|
+
1,
|
|
321
|
+
2
|
|
322
|
+
].map((e) => ({
|
|
323
|
+
binding: e,
|
|
324
|
+
resource: this.views[e]
|
|
325
|
+
}))
|
|
326
|
+
}));
|
|
327
|
+
let c = e.encoder.beginRenderPass({
|
|
328
|
+
label: "oit:composite",
|
|
329
|
+
colorAttachments: [{
|
|
330
|
+
view: e.color,
|
|
331
|
+
loadOp: "load",
|
|
332
|
+
storeOp: "store"
|
|
333
|
+
}]
|
|
334
|
+
});
|
|
335
|
+
r(c, e), c.setBindGroup(0, s);
|
|
336
|
+
for (let e of o) c.setPipeline(e), c.draw(3);
|
|
337
|
+
n && this.drawLinear(c, e, t, n), c.end();
|
|
338
|
+
}
|
|
339
|
+
release() {
|
|
340
|
+
for (let e of this.textures) t(this.device, e);
|
|
341
|
+
this.textures = [], this.views = [], this.size = "", this.groups.clear(), this.linearGroup = void 0;
|
|
342
|
+
}
|
|
343
|
+
freeDevice() {
|
|
344
|
+
this.release(), this.pipelines.clear(), this.blendedPipelines.clear(), this.linearPipelines.clear(), t(this.device, this.uniform), this.uniform = void 0;
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
//#endregion
|
|
348
|
+
export { v as OIT };
|
package/dist/meshes.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { EngineKernel, Mesh } from '@margoengine/core';
|
|
2
|
+
import type { TransparentPass } from './pass.js';
|
|
3
|
+
/** `addMesh` settings. */
|
|
4
|
+
export interface OITMeshOptions {
|
|
5
|
+
/** Coverage from 0 (invisible) to 1. Default 0.5. */
|
|
6
|
+
opacity?: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Draws a core mesh see-through in an order-independent pass (`OIT`, `ExactOIT`). Core meshes are opaque, so its colour, roughness, metalness, sides
|
|
10
|
+
* and geometry are copied at this call to a transparent materials mesh that follows its transforms and `visible`. The
|
|
11
|
+
* core mesh gives up its geometry (no opaque draw, no shadow, no pick) until the returned function restores it.
|
|
12
|
+
* @throws TypeError for a mesh without geometry.
|
|
13
|
+
* @throws RangeError for an `opacity` outside 0 to 1.
|
|
14
|
+
*/
|
|
15
|
+
export declare function addMesh(oit: TransparentPass, engine: EngineKernel, mesh: Mesh, { opacity }?: OITMeshOptions): () => void;
|
package/dist/meshes.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { Material as e, createMesh as t } from "@margoengine/materials";
|
|
5
|
+
//#region src/meshes.ts
|
|
6
|
+
var n = 0;
|
|
7
|
+
function r(r, i, a, { opacity: o = .5 } = {}) {
|
|
8
|
+
let s = a?.geometry;
|
|
9
|
+
if (!s) throw TypeError("addMesh needs a core mesh with geometry; call setGeometry first");
|
|
10
|
+
if (!(o >= 0 && o <= 1)) throw RangeError("Invalid addMesh opacity; use 0 to 1");
|
|
11
|
+
let c = s.vertices.length / 6, l = new Float32Array(c * 3), u = new Float32Array(c * 3);
|
|
12
|
+
for (let e = 0; e < c; e++) for (let t = 0; t < 3; t++) l[e * 3 + t] = s.vertices[e * 6 + t], u[e * 3 + t] = s.vertices[e * 6 + 3 + t];
|
|
13
|
+
let d = a.material.data, f = new e({
|
|
14
|
+
color: [
|
|
15
|
+
d[0],
|
|
16
|
+
d[1],
|
|
17
|
+
d[2]
|
|
18
|
+
],
|
|
19
|
+
roughness: d[4],
|
|
20
|
+
metalness: d[5],
|
|
21
|
+
doubleSided: !!d[6],
|
|
22
|
+
opacity: o,
|
|
23
|
+
transparent: !0
|
|
24
|
+
}), p = a.scene ?? i.scene, m = `oit-mesh-copy:${++n}`, h;
|
|
25
|
+
p.use({coreRequirement:/*@__PURE__*/__margoCoreRequirement("1.0.0",import.meta.url),
|
|
26
|
+
name: m,
|
|
27
|
+
setup() {
|
|
28
|
+
h = t(i, {
|
|
29
|
+
positions: l,
|
|
30
|
+
normals: u,
|
|
31
|
+
indices: s.indices
|
|
32
|
+
}, f);
|
|
33
|
+
}
|
|
34
|
+
}), p.removePlugin(m), h.root.setParent(a);
|
|
35
|
+
let g = r.add(h);
|
|
36
|
+
a.setGeometry(null);
|
|
37
|
+
let _ = !1;
|
|
38
|
+
return () => {
|
|
39
|
+
_ || (_ = !0, g(), h.destroy(), a.setGeometry(s));
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { r as addMesh };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
2
|
+
import { type Engine, type PassContext } from '@margoengine/core/extension';
|
|
3
|
+
import type { PBRModel } from '@margoengine/gltf/pbr';
|
|
4
|
+
import { TransparentPass } from './pass.js';
|
|
5
|
+
export type { TransparentPass };
|
|
6
|
+
type Context = Extract<PassContext, {
|
|
7
|
+
backend: 'webgpu';
|
|
8
|
+
}>;
|
|
9
|
+
/** `MomentOIT` settings, fixed at construction. */
|
|
10
|
+
export interface MomentOITOptions {
|
|
11
|
+
/** Frame graph order of the transparent passes, as in `OITOptions`. Default 8. */
|
|
12
|
+
order?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Nearest and farthest view depth the moments resolve, `[near, far]`, with 0 < near < far; layers outside blend as if
|
|
15
|
+
* at the end they passed. A range close around the transparent parts is the sharpest. Default: the box round the added
|
|
16
|
+
* models, as they stood on the first frame after `add` or `fit`, seen from each frame's camera; with an effect added
|
|
17
|
+
* (`@margoengine/oit/vfx`), which has no box, the camera's near and far.
|
|
18
|
+
*/
|
|
19
|
+
depthRange?: readonly [number, number];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Moment-based order-independent transparency: `oit.add(model)` draws the model's BLEND primitives (gltf/pbr) or a
|
|
23
|
+
* transparent materials mesh twice, once to sum the moments of each pixel's layers and once to shade each layer by the
|
|
24
|
+
* transmittance in front of it, so a near layer really does hide a far one. Closer to exact than `OIT` with several
|
|
25
|
+
* layers, at about twice its geometry cost, and with fixed memory, unlike `ExactOIT`. Still an approximation. Models not
|
|
26
|
+
* added keep sorted blending. WebGPU only.
|
|
27
|
+
* @throws RangeError from the constructor for an `order` or `depthRange` out of range.
|
|
28
|
+
* @throws MargoError `'unsupported'` on a WebGL engine, `'disposed'` after `dispose()`, `'state'` from `add` before
|
|
29
|
+
* `attach`.
|
|
30
|
+
*/
|
|
31
|
+
export declare class MomentOIT extends TransparentPass {
|
|
32
|
+
readonly targets: readonly GPUColorTargetState[];
|
|
33
|
+
private readonly range?;
|
|
34
|
+
/** World box of the added models: min x, y, z, max x, y, z. */
|
|
35
|
+
private box?;
|
|
36
|
+
readonly layout: readonly GPUBindGroupLayoutEntry[];
|
|
37
|
+
private readonly code;
|
|
38
|
+
private readonly surfaceCode;
|
|
39
|
+
private readonly shadeCode;
|
|
40
|
+
private readonly shadeSurface;
|
|
41
|
+
private readonly values;
|
|
42
|
+
private uniform?;
|
|
43
|
+
/** This view's uniform binding while its models draw (a frame-arena slice for a second view). */
|
|
44
|
+
private binding?;
|
|
45
|
+
/** Resolved targets (moments, optical depth, colour), then their multisampled draw targets. */
|
|
46
|
+
private textures;
|
|
47
|
+
private views;
|
|
48
|
+
private size;
|
|
49
|
+
private readonly pipelines;
|
|
50
|
+
private readonly groups;
|
|
51
|
+
private shading?;
|
|
52
|
+
/** The second draw of each model, a hook of its own because its shader and target differ. */
|
|
53
|
+
private readonly shader;
|
|
54
|
+
static attach(engine: EngineKernel, options?: MomentOITOptions): MomentOIT;
|
|
55
|
+
constructor({ order, depthRange }?: MomentOITOptions);
|
|
56
|
+
setup(engine: Engine): void;
|
|
57
|
+
add(model: PBRModel): () => void;
|
|
58
|
+
/** Measures the added models again on the next frame; call it after moving them far. Measuring reads every vertex. */
|
|
59
|
+
fit(): void;
|
|
60
|
+
/** The view depths the box round the added models spans from this camera, a little wider so its faces stay inside. */
|
|
61
|
+
private depthSpan;
|
|
62
|
+
/** @internal gltf\/pbr transparency hook. */
|
|
63
|
+
declarations(group: number): string;
|
|
64
|
+
/** @internal Effect hook. */
|
|
65
|
+
surface(group: number): string;
|
|
66
|
+
/** @internal gltf\/pbr transparency hook. */
|
|
67
|
+
bindings(): GPUBindGroupEntry[];
|
|
68
|
+
/** @internal gltf\/pbr transparency hook: the moment pass, begun on the first transparent draw of the frame. */
|
|
69
|
+
begin(): GPURenderPassEncoder;
|
|
70
|
+
/** A pass on the targets at `slots`: with MSAA the multisampled ones, which resolve as the pass ends. */
|
|
71
|
+
private open;
|
|
72
|
+
protected render(c: Context): void;
|
|
73
|
+
private linearGroups?;
|
|
74
|
+
/** The linear composite's group: colour, optical depth and the snapshot. */
|
|
75
|
+
private linearGroup;
|
|
76
|
+
protected release(): void;
|
|
77
|
+
protected freeDevice(): void;
|
|
78
|
+
}
|