@margoengine/html 0.0.0-stage → 0.1.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 +88 -3
- package/dist/blend.d.ts +33 -0
- package/dist/blend.js +185 -0
- package/dist/dom-CgZN0DP1.js +17 -0
- package/dist/dom.d.ts +2 -0
- package/dist/errors-fc4i1pSL.js +7 -0
- package/dist/gl-j8yV2Jj1.js +31 -0
- package/dist/gl.d.ts +4 -0
- package/dist/index.d.ts +88 -0
- package/dist/index.js +599 -0
- package/dist/inline.d.ts +13 -0
- package/dist/inline.js +153 -0
- package/dist/pass-BVRaGB09.js +16 -0
- package/dist/texture-gl.d.ts +17 -0
- package/dist/texture-gl.js +103 -0
- package/dist/texture-kX-VTlgW.js +313 -0
- package/dist/texture.d.ts +126 -0
- package/dist/texture.js +1 -0
- package/dist/view-rect-Be4T67Kk.js +16 -0
- package/package.json +66 -6
package/dist/inline.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { HTMLResourceInliner } from './texture.js';
|
|
2
|
+
/** Options for a self-contained foreignObject snapshot. No network operation starts until called. */
|
|
3
|
+
export interface InlineResourceOptions {
|
|
4
|
+
signal?: AbortSignal;
|
|
5
|
+
/** Whole snapshot's network/read deadline in milliseconds, default 15000. */
|
|
6
|
+
timeout?: number;
|
|
7
|
+
/** Explicit @font-face CSS instead of document-sheet discovery; URLs resolve against the element's baseURI. */
|
|
8
|
+
fontCSS?: string;
|
|
9
|
+
}
|
|
10
|
+
/** Fetches same-origin/CORS images and fonts into a styled detached copy; never mutates the live source. */
|
|
11
|
+
export declare function inlineResources(element: HTMLElement, options?: InlineResourceOptions): Promise<HTMLElement>;
|
|
12
|
+
/** Creates HTMLTexture's prepare hook, combining caller cancellation with surface removal. */
|
|
13
|
+
export declare function resourceInliner(options?: InlineResourceOptions): HTMLResourceInliner;
|
package/dist/inline.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { t as e } from "./errors-fc4i1pSL.js";
|
|
2
|
+
import { t } from "./dom-CgZN0DP1.js";
|
|
3
|
+
//#region src/inline.ts
|
|
4
|
+
var n = (e) => e.replace(/\\(?:\r\n|[\n\r\f])/g, "").replace(/\\([\da-f]{1,6}\s?|.)/gi, (e, t) => {
|
|
5
|
+
if (!/^[\da-f]/i.test(t)) return t;
|
|
6
|
+
let n = parseInt(t, 16);
|
|
7
|
+
return String.fromCodePoint(n === 0 || n > 1114111 || n >= 55296 && n <= 57343 ? 65533 : n);
|
|
8
|
+
}), r = (e) => n(e.trim().replace(/^(['"])(.*)\1$/, "$2")).toLowerCase();
|
|
9
|
+
async function i(e, t) {
|
|
10
|
+
let r = [];
|
|
11
|
+
for (let i = 0; i < e.length;) {
|
|
12
|
+
if (e.startsWith("/*", i)) {
|
|
13
|
+
let t = e.indexOf("*/", i + 2);
|
|
14
|
+
i = t < 0 ? e.length : t + 2;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (e[i] === "\"" || e[i] === "'") {
|
|
18
|
+
let t = e[i++];
|
|
19
|
+
for (; i < e.length;) if (e[i++] === "\\") i++;
|
|
20
|
+
else if (e[i - 1] === t) break;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (e.slice(i, i + 4).toLowerCase() !== "url(" || i > 0 && /[\w-]/.test(e[i - 1])) {
|
|
24
|
+
i++;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
let a = i;
|
|
28
|
+
for (i += 4; /\s/.test(e[i] ?? "") && i < e.length;) i++;
|
|
29
|
+
let o = e[i] === "\"" || e[i] === "'" ? e[i++] : null, s = i;
|
|
30
|
+
for (; i < e.length;) if (e[i] === "\\") i += 2;
|
|
31
|
+
else {
|
|
32
|
+
if (o ? e[i] === o : e[i] === ")") break;
|
|
33
|
+
i++;
|
|
34
|
+
}
|
|
35
|
+
let c = n(e.slice(s, i).trim());
|
|
36
|
+
if (o) for (i++; i < e.length && /\s/.test(e[i]);) i++;
|
|
37
|
+
e[i] === ")" && (i++, r.push({
|
|
38
|
+
start: a,
|
|
39
|
+
end: i,
|
|
40
|
+
value: t(c).then((e) => `url(${JSON.stringify(e)})`)
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
let i = await Promise.all(r.map((e) => e.value)), a = "", o = 0;
|
|
44
|
+
return r.forEach((t, n) => {
|
|
45
|
+
a += e.slice(o, t.start) + i[n], o = t.end;
|
|
46
|
+
}), a + e.slice(o);
|
|
47
|
+
}
|
|
48
|
+
async function a(a, o = {}) {
|
|
49
|
+
let s = o.timeout ?? 15e3;
|
|
50
|
+
if (!Number.isInteger(s) || s < 1 || s > 2147483647) throw RangeError("Invalid inline timeout; pass whole milliseconds from 1 to 2147483647");
|
|
51
|
+
o.signal?.throwIfAborted();
|
|
52
|
+
let c = AbortSignal.timeout(s), l = o.signal ? AbortSignal.any([o.signal, c]) : c, u = a.ownerDocument, d = u.defaultView, f = t(a), p = /* @__PURE__ */ new Map(), m = /* @__PURE__ */ new Set(), h = (t) => {
|
|
53
|
+
let n = p.get(t);
|
|
54
|
+
return n || (n = (async () => {
|
|
55
|
+
try {
|
|
56
|
+
let n = await fetch(t, {
|
|
57
|
+
mode: "cors",
|
|
58
|
+
credentials: "same-origin",
|
|
59
|
+
signal: l
|
|
60
|
+
});
|
|
61
|
+
if (!n.ok) throw e("failed", `Resource ${t} returned ${n.status}; serve a successful CORS response`);
|
|
62
|
+
let r = await n.arrayBuffer();
|
|
63
|
+
return l.throwIfAborted(), {
|
|
64
|
+
data: await new Promise((e, t) => {
|
|
65
|
+
let i = new d.FileReader(), a = () => i.abort(), o = () => l.removeEventListener("abort", a);
|
|
66
|
+
i.onload = () => {
|
|
67
|
+
o(), e(String(i.result));
|
|
68
|
+
}, i.onerror = () => {
|
|
69
|
+
o(), t(i.error);
|
|
70
|
+
}, i.onabort = () => {
|
|
71
|
+
o(), t(l.reason);
|
|
72
|
+
}, l.addEventListener("abort", a, { once: !0 }), i.readAsDataURL(new d.Blob([r], { type: n.headers.get("content-type") ?? "application/octet-stream" }));
|
|
73
|
+
}),
|
|
74
|
+
text: new TextDecoder().decode(r)
|
|
75
|
+
};
|
|
76
|
+
} catch (n) {
|
|
77
|
+
throw l.aborted ? l.reason : e("failed", `Could not inline ${t}: ${String(n)}; serve it same-origin or with CORS`);
|
|
78
|
+
}
|
|
79
|
+
})(), p.set(t, n)), n;
|
|
80
|
+
}, g = async (e, t) => {
|
|
81
|
+
if (!e || e.startsWith("#") || /^data:/i.test(e)) return e;
|
|
82
|
+
l.throwIfAborted();
|
|
83
|
+
let n = new URL(e, t), r = n.hash;
|
|
84
|
+
return n.hash = "", (await h(n.href)).data + r;
|
|
85
|
+
}, _ = [a, ...a.querySelectorAll("*")], v = [f, ...f.querySelectorAll("*")];
|
|
86
|
+
await Promise.all(_.map(async (e, t) => {
|
|
87
|
+
let n = v[t], a = e.baseURI, o = d.getComputedStyle(e);
|
|
88
|
+
for (let e of o.fontFamily.split(/,(?=(?:[^"']|"[^"]*"|'[^']*')*$)/)) m.add(r(e));
|
|
89
|
+
let s = n.getAttribute("style");
|
|
90
|
+
if (s && n.setAttribute("style", await i(s, (e) => g(e, a))), e instanceof d.HTMLImageElement) {
|
|
91
|
+
n.removeAttribute("srcset"), n.removeAttribute("sizes");
|
|
92
|
+
let t = e.currentSrc || e.getAttribute("src");
|
|
93
|
+
t && n.setAttribute("src", await g(t, a));
|
|
94
|
+
} else if (e instanceof d.HTMLSourceElement) n.removeAttribute("srcset");
|
|
95
|
+
else if (e instanceof d.HTMLInputElement && e.type === "image") {
|
|
96
|
+
let t = e.getAttribute("src");
|
|
97
|
+
t && n.setAttribute("src", await g(t, a));
|
|
98
|
+
} else if (e.namespaceURI === "http://www.w3.org/2000/svg" && e.localName === "image") for (let t of ["href", "xlink:href"]) {
|
|
99
|
+
let r = e.getAttribute(t);
|
|
100
|
+
r && n.setAttribute(t, await g(r, a));
|
|
101
|
+
}
|
|
102
|
+
}));
|
|
103
|
+
for (let e of f.querySelectorAll("style,link[rel=\"stylesheet\"]")) e.remove();
|
|
104
|
+
let y = [], b = /* @__PURE__ */ new Set(), x = /* @__PURE__ */ new Set(), S = async (e, t) => {
|
|
105
|
+
e = e.replace(/(@import\s*)(["'])((?:\\.|[^\\])*?)\2/gi, (e, r, i, a) => `${r}url(${JSON.stringify(new URL(n(a), t).href)})`), e = await i(e, async (e) => e.startsWith("#") ? e : new URL(e, t).href);
|
|
106
|
+
let r = u.createElement("style");
|
|
107
|
+
r.media = "not all", r.textContent = e, u.head.append(r);
|
|
108
|
+
try {
|
|
109
|
+
return Array.from(r.sheet.cssRules);
|
|
110
|
+
} finally {
|
|
111
|
+
r.remove();
|
|
112
|
+
}
|
|
113
|
+
}, C = async (e) => {
|
|
114
|
+
b.has(e) || (b.add(e), await T(await S((await h(e)).text, e), e));
|
|
115
|
+
}, w = async (e, t) => {
|
|
116
|
+
if (x.has(e)) return;
|
|
117
|
+
x.add(e);
|
|
118
|
+
let n;
|
|
119
|
+
try {
|
|
120
|
+
n = Array.from(e.cssRules);
|
|
121
|
+
} catch (t) {
|
|
122
|
+
if (t.name !== "SecurityError" || !e.href) throw t;
|
|
123
|
+
return C(e.href);
|
|
124
|
+
}
|
|
125
|
+
await T(n, e.href ?? t);
|
|
126
|
+
}, T = async (e, t) => {
|
|
127
|
+
for (let n of e) if (l.throwIfAborted(), n.type === 5) {
|
|
128
|
+
let e = n;
|
|
129
|
+
m.has(r(e.style.getPropertyValue("font-family"))) && y.push(await i(e.cssText, (e) => g(e, t)));
|
|
130
|
+
} else if (n.type === 3) {
|
|
131
|
+
let e = new URL(n.href, t).href;
|
|
132
|
+
await C(e);
|
|
133
|
+
} else "cssRules" in n && await T(Array.from(n.cssRules), t);
|
|
134
|
+
};
|
|
135
|
+
if (o.fontCSS !== void 0) await T(await S(o.fontCSS, a.baseURI), a.baseURI);
|
|
136
|
+
else {
|
|
137
|
+
let e = [...Array.from(u.styleSheets), ...Array.from(u.adoptedStyleSheets ?? [])];
|
|
138
|
+
for (let t of e) await w(t, a.baseURI);
|
|
139
|
+
}
|
|
140
|
+
if (y.length) {
|
|
141
|
+
let e = u.createElement("style");
|
|
142
|
+
e.textContent = [...new Set(y)].join("\n"), f.prepend(e);
|
|
143
|
+
}
|
|
144
|
+
return l.throwIfAborted(), f;
|
|
145
|
+
}
|
|
146
|
+
function o(e = {}) {
|
|
147
|
+
return (t, n) => a(t, {
|
|
148
|
+
...e,
|
|
149
|
+
signal: e.signal ? AbortSignal.any([n, e.signal]) : n
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
//#endregion
|
|
153
|
+
export { a as inlineResources, o as resourceInliner };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region ../shared/pass.js
|
|
2
|
+
var e = () => ({
|
|
3
|
+
colorAttachments: [{
|
|
4
|
+
view: void 0,
|
|
5
|
+
loadOp: "load",
|
|
6
|
+
storeOp: "store"
|
|
7
|
+
}],
|
|
8
|
+
depthStencilAttachment: {
|
|
9
|
+
view: void 0,
|
|
10
|
+
depthLoadOp: "load",
|
|
11
|
+
depthStoreOp: "store",
|
|
12
|
+
stencilReadOnly: !0
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
//#endregion
|
|
16
|
+
export { e as t };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
2
|
+
import { type FrameGraph } from '@margoengine/core/extension';
|
|
3
|
+
import { HTMLTexture, type HTMLSurface } from './texture.js';
|
|
4
|
+
/** HTMLTexture with a native WebGL2 preview route, sharing rasterization, hit testing and WebGPU rendering. */
|
|
5
|
+
export declare class HTMLTextureGL extends HTMLTexture {
|
|
6
|
+
protected readonly preview = true;
|
|
7
|
+
private glResources?;
|
|
8
|
+
static attach(engine: EngineKernel, options?: ConstructorParameters<typeof HTMLTexture>[0]): HTMLTextureGL;
|
|
9
|
+
registerPasses(graph: FrameGraph): void;
|
|
10
|
+
private resources;
|
|
11
|
+
protected upload(surface: HTMLSurface, source: HTMLCanvasElement, width: number, height: number): void;
|
|
12
|
+
private drawGL;
|
|
13
|
+
remove(surface: HTMLSurface): void;
|
|
14
|
+
private releaseGL;
|
|
15
|
+
onDeviceRestored(): void;
|
|
16
|
+
dispose(): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const r=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { n as e } from "./texture-kX-VTlgW.js";
|
|
5
|
+
import { n as t, t as n } from "./gl-j8yV2Jj1.js";
|
|
6
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
|
+
//#region src/texture-gl.ts
|
|
8
|
+
var i = "#version 300 es\nuniform mat4 camera,model;uniform vec2 size;out vec2 uv;\nvoid main(){\n vec2 corners[6]=vec2[6](vec2(0,0),vec2(1,0),vec2(0,1),vec2(0,1),vec2(1,0),vec2(1,1));\n uv=corners[gl_VertexID];gl_Position=camera*model*vec4((uv.x-.5)*size.x,(.5-uv.y)*size.y,0,1);\n}", a = "#version 300 es\nprecision highp float;uniform sampler2D image;in vec2 uv;out vec4 color;\nvoid main(){color=texture(image,uv);if(color.a<.004)discard;}", o = class o extends e {
|
|
9
|
+
constructor(...e) {
|
|
10
|
+
super(...e), this.preview = !0;
|
|
11
|
+
}
|
|
12
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
13
|
+
let n = new o(t);
|
|
14
|
+
return r(e).use(n), n;
|
|
15
|
+
}
|
|
16
|
+
registerPasses(e) {
|
|
17
|
+
super.registerPasses(e), e.add({
|
|
18
|
+
name: `${this.name}-gl`,
|
|
19
|
+
order: .75,
|
|
20
|
+
execute: (e) => {
|
|
21
|
+
e.backend === "webgl2" && this.drawGL(e);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
resources(e) {
|
|
26
|
+
if (this.glResources?.gl !== e && this.releaseGL(), !this.glResources) {
|
|
27
|
+
let n = t(e, i, a);
|
|
28
|
+
this.glResources = {
|
|
29
|
+
gl: e,
|
|
30
|
+
program: n,
|
|
31
|
+
vao: e.createVertexArray(),
|
|
32
|
+
camera: e.getUniformLocation(n, "camera"),
|
|
33
|
+
model: e.getUniformLocation(n, "model"),
|
|
34
|
+
size: e.getUniformLocation(n, "size"),
|
|
35
|
+
image: e.getUniformLocation(n, "image"),
|
|
36
|
+
textures: /* @__PURE__ */ new Map()
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return this.glResources;
|
|
40
|
+
}
|
|
41
|
+
upload(e, t, r, i) {
|
|
42
|
+
if (this.runtime?.gpu) return super.upload(e, t, r, i);
|
|
43
|
+
let a = this.glResources;
|
|
44
|
+
if (!a || a.gl.isContextLost()) {
|
|
45
|
+
e.invalidate();
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
let o = a.gl, s = n(o), c = o.getParameter(o.PIXEL_UNPACK_BUFFER_BINDING), l = [
|
|
49
|
+
o.UNPACK_PREMULTIPLY_ALPHA_WEBGL,
|
|
50
|
+
o.UNPACK_FLIP_Y_WEBGL,
|
|
51
|
+
o.UNPACK_ROW_LENGTH,
|
|
52
|
+
o.UNPACK_SKIP_ROWS,
|
|
53
|
+
o.UNPACK_SKIP_PIXELS,
|
|
54
|
+
o.UNPACK_IMAGE_HEIGHT,
|
|
55
|
+
o.UNPACK_SKIP_IMAGES
|
|
56
|
+
], u = l.map((e) => o.getParameter(e));
|
|
57
|
+
try {
|
|
58
|
+
let n = a.textures.get(e);
|
|
59
|
+
n || (n = o.createTexture(), a.textures.set(e, n)), o.bindTexture(o.TEXTURE_2D, n), o.bindBuffer(o.PIXEL_UNPACK_BUFFER, null), l.forEach((e, t) => o.pixelStorei(e, +(t === 0))), o.texImage2D(o.TEXTURE_2D, 0, o.RGBA, o.RGBA, o.UNSIGNED_BYTE, t), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_MIN_FILTER, o.LINEAR), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_MAG_FILTER, o.LINEAR), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_WRAP_S, o.CLAMP_TO_EDGE), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_WRAP_T, o.CLAMP_TO_EDGE);
|
|
60
|
+
} finally {
|
|
61
|
+
l.forEach((e, t) => o.pixelStorei(e, u[t])), o.bindBuffer(o.PIXEL_UNPACK_BUFFER, c), s();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
drawGL(e) {
|
|
65
|
+
if (!this.size || e.gl.isContextLost()) return;
|
|
66
|
+
let t = this.resources(e.gl), r = e.gl, i = n(r);
|
|
67
|
+
try {
|
|
68
|
+
let [n, i, a, o] = e.view?.viewport ?? [
|
|
69
|
+
0,
|
|
70
|
+
0,
|
|
71
|
+
r.drawingBufferWidth,
|
|
72
|
+
r.drawingBufferHeight
|
|
73
|
+
];
|
|
74
|
+
r.viewport(n, r.drawingBufferHeight - i - o, a, o), r.useProgram(t.program), r.bindVertexArray(t.vao), r.bindSampler(0, null), r.enable(r.DEPTH_TEST), r.depthFunc(r.LESS), r.depthMask(!0), r.colorMask(!0, !0, !0, !0), r.disable(r.CULL_FACE), r.enable(r.BLEND), r.blendEquation(r.FUNC_ADD), r.blendFunc(r.ONE, r.ONE_MINUS_SRC_ALPHA), r.uniformMatrix4fv(t.camera, !1, e.camera.matrix), r.uniform1i(t.image, 0);
|
|
75
|
+
for (let e of this.surfaces) {
|
|
76
|
+
this.refresh(e);
|
|
77
|
+
let n = t.textures.get(e);
|
|
78
|
+
n && (r.uniformMatrix4fv(t.model, !1, this.worldMatrix(e)), r.uniform2f(t.size, e.width, e.height), r.bindTexture(r.TEXTURE_2D, n), r.drawArrays(r.TRIANGLES, 0, 6));
|
|
79
|
+
}
|
|
80
|
+
} finally {
|
|
81
|
+
i();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
remove(e) {
|
|
85
|
+
let t = this.glResources, n = t?.textures.get(e);
|
|
86
|
+
n && (t.gl.deleteTexture(n), t.textures.delete(e)), super.remove(e);
|
|
87
|
+
}
|
|
88
|
+
releaseGL() {
|
|
89
|
+
let e = this.glResources;
|
|
90
|
+
if (this.glResources = void 0, e && !e.gl.isContextLost()) {
|
|
91
|
+
for (let t of e.textures.values()) e.gl.deleteTexture(t);
|
|
92
|
+
e.gl.deleteVertexArray(e.vao), e.gl.deleteProgram(e.program);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
onDeviceRestored() {
|
|
96
|
+
this.releaseGL(), super.onDeviceRestored();
|
|
97
|
+
}
|
|
98
|
+
dispose() {
|
|
99
|
+
this.releaseGL(), super.dispose();
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
//#endregion
|
|
103
|
+
export { o as HTMLTextureGL };
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const i=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { t as e } from "./errors-fc4i1pSL.js";
|
|
5
|
+
import { t } from "./view-rect-Be4T67Kk.js";
|
|
6
|
+
import { t as n } from "./pass-BVRaGB09.js";
|
|
7
|
+
import { t as r } from "./dom-CgZN0DP1.js";
|
|
8
|
+
import { engineRuntime as __margoRuntime, meshRuntime as a } from "@margoengine/core/extension";
|
|
9
|
+
//#region ../shared/hdr-format.js
|
|
10
|
+
var o = (e) => typeof e == "string" && e.endsWith("float"), s = "\nstruct Surface {camera:mat4x4f,model:mat4x4f,size:vec4f}\n@group(0) @binding(0) var<uniform> surface:Surface;\n@group(0) @binding(1) var image:texture_2d<f32>;\n@group(0) @binding(2) var linearSampler:sampler;\noverride hdr=false;\nstruct Varyings {@builtin(position) position:vec4f,@location(0) uv:vec2f}\n@vertex fn vertex(@builtin(vertex_index) index:u32)->Varyings {\n let c=array<vec2f,6>(vec2f(0,0),vec2f(1,0),vec2f(0,1),vec2f(0,1),vec2f(1,0),vec2f(1,1))[index];\n let local=vec4f((c.x-0.5)*surface.size.x,(0.5-c.y)*surface.size.y,0,1);\n return Varyings(surface.camera*(surface.model*local),c);\n}\n@fragment fn fragment(v:Varyings)->@location(0) vec4f {\n let color=textureSample(image,linearSampler,v.uv);\n if(color.a<0.004){discard;}\n if(hdr){let c=color.rgb/color.a;return vec4f(select(pow((c+.055)/1.055,vec3f(2.4)),c/12.92,c<=vec3f(.04045))*color.a,color.a);}\n return color;\n}", c = [
|
|
11
|
+
1,
|
|
12
|
+
0,
|
|
13
|
+
0,
|
|
14
|
+
0,
|
|
15
|
+
0,
|
|
16
|
+
1,
|
|
17
|
+
0,
|
|
18
|
+
0,
|
|
19
|
+
0,
|
|
20
|
+
0,
|
|
21
|
+
1,
|
|
22
|
+
0,
|
|
23
|
+
0,
|
|
24
|
+
0,
|
|
25
|
+
0,
|
|
26
|
+
1
|
|
27
|
+
], l = (e, t, n, r, i) => [
|
|
28
|
+
0,
|
|
29
|
+
1,
|
|
30
|
+
2,
|
|
31
|
+
3
|
|
32
|
+
].map((a) => e[a] * t + e[a + 4] * n + e[a + 8] * r + e[a + 12] * i);
|
|
33
|
+
function u(e, t, n, r, i, a) {
|
|
34
|
+
let o = (n) => l(e, t[n], t[n + 1], t[n + 2], t[n + 3]), s = o(0), c = o(4), u = o(12), d = s[0] - i * s[3], f = c[0] - i * c[3], p = i * u[3] - u[0], m = s[1] - a * s[3], h = c[1] - a * c[3], g = a * u[3] - u[1], _ = d * h - f * m;
|
|
35
|
+
if (!(Math.abs(_) > 1e-12)) return null;
|
|
36
|
+
let v = (p * h - f * g) / _, y = (d * g - p * m) / _, b = v / n + .5, x = .5 - y / r, S = s[3] * v + c[3] * y + u[3];
|
|
37
|
+
return !(b >= 0 && b <= 1 && x >= 0 && x <= 1) || !(S > 0) ? null : {
|
|
38
|
+
u: b,
|
|
39
|
+
v: x,
|
|
40
|
+
t: (s[2] * v + c[2] * y + u[2]) / S
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
var d = class {
|
|
44
|
+
get signal() {
|
|
45
|
+
return this.cancellation.signal;
|
|
46
|
+
}
|
|
47
|
+
constructor(e, t, n) {
|
|
48
|
+
this.owner = e, this.matrix = new Float32Array(c), this.draws = 0, this.dirty = !0, this.busy = !1, this.world = /* @__PURE__ */ new Float32Array(16), this.cancellation = new AbortController(), this.mark = () => this.invalidate(), this.paint = () => {
|
|
49
|
+
let e = this.painting;
|
|
50
|
+
this.painting = void 0, e ? e() : this.invalidate();
|
|
51
|
+
}, this.element = t;
|
|
52
|
+
let r = t.ownerDocument, i = r.defaultView, a = n.pixelRatio ?? i.devicePixelRatio ?? 1;
|
|
53
|
+
if (!(a >= .25 && a <= 4)) throw RangeError("Invalid HTML surface pixelRatio; pass 0.25 to 4 texture pixels per CSS pixel");
|
|
54
|
+
this.pixelRatio = a;
|
|
55
|
+
let o = e.rasterizer === "element-image" ? r.createElement("canvas") : r.createElement("div");
|
|
56
|
+
e.rasterizer === "element-image" && (o.setAttribute("content", "drawable"), t.setAttribute("drawable", "")), o.setAttribute("data-margoengine-html-surface", "");
|
|
57
|
+
let s = o.style;
|
|
58
|
+
s.position = "fixed", s.left = s.top = "0", s.opacity = "0", s.pointerEvents = "none", s.zIndex = "2147483647", s.transformOrigin = "0 0", s.transform = "none", o.appendChild(t), r.body.appendChild(o);
|
|
59
|
+
let l = t.getBoundingClientRect(), u = t.offsetWidth || l.width, d = t.offsetHeight || l.height;
|
|
60
|
+
if (o instanceof i.HTMLCanvasElement && (o.width = Math.max(1, Math.round(u * a)), o.height = Math.max(1, Math.round(d * a)), o.style.width = `${u}px`, o.style.height = `${d}px`, o.addEventListener("paint", this.paint)), this.width = n.width ?? u / 100, this.height = n.height ?? d / 100, !(this.width > 0 && this.height > 0 && Number.isFinite(this.width) && Number.isFinite(this.height))) throw RangeError("Invalid HTML surface size; give the element a CSS size, or pass width and height above 0");
|
|
61
|
+
this.mesh = n.mesh ?? null, n.matrix && this.setMatrix(n.matrix), this.interactive = n.interactive ?? !0, this.holder = o, typeof i.MutationObserver == "function" && (this.observer = new i.MutationObserver(this.mark), this.observer.observe(t, {
|
|
62
|
+
subtree: !0,
|
|
63
|
+
childList: !0,
|
|
64
|
+
attributes: !0,
|
|
65
|
+
characterData: !0
|
|
66
|
+
}));
|
|
67
|
+
for (let e of [
|
|
68
|
+
"input",
|
|
69
|
+
"change",
|
|
70
|
+
"focusin",
|
|
71
|
+
"focusout",
|
|
72
|
+
"pointerover",
|
|
73
|
+
"pointerout",
|
|
74
|
+
"pointerdown",
|
|
75
|
+
"pointerup"
|
|
76
|
+
]) t.addEventListener(e, this.mark);
|
|
77
|
+
}
|
|
78
|
+
setMatrix(e) {
|
|
79
|
+
if (e.length !== 16 || !Array.from(e).every(Number.isFinite)) throw RangeError("Invalid HTML surface matrix; pass 16 finite numbers, column-major");
|
|
80
|
+
return this.matrix.set(e), this;
|
|
81
|
+
}
|
|
82
|
+
invalidate() {
|
|
83
|
+
this.dirty = !0;
|
|
84
|
+
}
|
|
85
|
+
get cssSize() {
|
|
86
|
+
return [this.element.offsetWidth, this.element.offsetHeight];
|
|
87
|
+
}
|
|
88
|
+
remove() {
|
|
89
|
+
this.owner.remove(this);
|
|
90
|
+
}
|
|
91
|
+
detach() {
|
|
92
|
+
this.cancellation.abort(), this.observer?.disconnect();
|
|
93
|
+
for (let e of [
|
|
94
|
+
"input",
|
|
95
|
+
"change",
|
|
96
|
+
"focusin",
|
|
97
|
+
"focusout",
|
|
98
|
+
"pointerover",
|
|
99
|
+
"pointerout",
|
|
100
|
+
"pointerdown",
|
|
101
|
+
"pointerup"
|
|
102
|
+
]) this.element.removeEventListener(e, this.mark);
|
|
103
|
+
this.holder.removeEventListener("paint", this.paint), this.owner.rasterizer === "element-image" && this.element.removeAttribute("drawable"), this.painting = void 0, this.holder.remove(), this.texture?.destroy(), this.uniform?.destroy(), this.texture = this.uniform = this.group = void 0;
|
|
104
|
+
}
|
|
105
|
+
}, f = class c {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
106
|
+
constructor(t = {}) {
|
|
107
|
+
this.surfaces = /* @__PURE__ */ new Set(), this.preview = !1, this.pipelines = /* @__PURE__ */ new Map(), this.disposed = !1, this.hovered = null, this.reversed = !1, this.descriptor = n(), this.values = /* @__PURE__ */ new Float32Array(36), this.pointer = (e) => this.route(e), this.name = t.name ?? "html-texture", this.prepare = t.prepare;
|
|
108
|
+
let r = typeof CanvasRenderingContext2D < "u" && "drawElementImage" in CanvasRenderingContext2D.prototype;
|
|
109
|
+
if (this.rasterizer = t.rasterizer ?? (r ? "element-image" : "foreign-object"), this.rasterizer !== "element-image" && this.rasterizer !== "foreign-object") throw RangeError("Invalid HTMLTexture rasterizer; pass 'element-image' or 'foreign-object'");
|
|
110
|
+
if (this.rasterizer === "element-image" && !r) throw e("unsupported", "This browser has no HTML-in-canvas drawElementImage; use rasterizer: 'foreign-object'");
|
|
111
|
+
}
|
|
112
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
113
|
+
let n = new c(t);
|
|
114
|
+
return i(e).use(n), n;
|
|
115
|
+
}
|
|
116
|
+
add(t, n = {}) {
|
|
117
|
+
if (this.disposed) throw e("disposed", "HTMLTexture is disposed; create a new one");
|
|
118
|
+
let r = new d(this, t, n);
|
|
119
|
+
return this.surfaces.add(r), r;
|
|
120
|
+
}
|
|
121
|
+
remove(e) {
|
|
122
|
+
this.surfaces.delete(e) && (this.hovered === e && (this.hovered = null), e.detach());
|
|
123
|
+
}
|
|
124
|
+
get size() {
|
|
125
|
+
return this.surfaces.size;
|
|
126
|
+
}
|
|
127
|
+
setup(t) {
|
|
128
|
+
if (this.disposed) throw e("disposed", "HTMLTexture is disposed; create a new one");
|
|
129
|
+
let n = i(t);
|
|
130
|
+
if (!n.gpu && !this.preview) throw e("unsupported", "HTMLTexture requires WebGPU, not the WebGL2 fallback; create the engine where WebGPU is available; see https://margoengine.com/errors#webgpu");
|
|
131
|
+
this.runtime = n, n.canvas.ownerDocument.addEventListener("pointermove", this.pointer, !0), n.canvas.ownerDocument.addEventListener("pointerdown", this.pointer, !0);
|
|
132
|
+
}
|
|
133
|
+
registerPasses(e) {
|
|
134
|
+
e.add({
|
|
135
|
+
name: this.name,
|
|
136
|
+
order: .75,
|
|
137
|
+
execute: (e) => {
|
|
138
|
+
e.backend === "webgpu" && this.draw(e);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
worldMatrix(e) {
|
|
143
|
+
let t = e.mesh, n = e.world;
|
|
144
|
+
if (t && this.runtime) {
|
|
145
|
+
let e = a(t).id;
|
|
146
|
+
n.set(this.runtime.store.matrices.subarray(e * 16, e * 16 + 16));
|
|
147
|
+
} else n.set(e.matrix);
|
|
148
|
+
return n;
|
|
149
|
+
}
|
|
150
|
+
route(e) {
|
|
151
|
+
let t = this.runtime;
|
|
152
|
+
if (!t) return;
|
|
153
|
+
let n = t.canvas.getBoundingClientRect(), r = (e.clientX - n.left) / n.width * 2 - 1, i = 1 - (e.clientY - n.top) / n.height * 2, a = null;
|
|
154
|
+
if (r >= -1 && r <= 1 && i >= -1 && i <= 1) for (let e of this.surfaces) {
|
|
155
|
+
if (!e.interactive) continue;
|
|
156
|
+
let n = u(t.camera.matrix, this.worldMatrix(e), e.width, e.height, r, i);
|
|
157
|
+
n && (!a || (this.reversed ? n.t > a.t : n.t < a.t)) && (a = {
|
|
158
|
+
surface: e,
|
|
159
|
+
...n
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
if (this.hovered && this.hovered !== a?.surface && (this.hovered.holder.style.pointerEvents = "none", this.hovered.holder.style.transform = "none"), this.hovered = a?.surface ?? null, !a) return;
|
|
163
|
+
let [o, s] = a.surface.cssSize, c = a.surface.holder.style;
|
|
164
|
+
c.transform = `translate(${e.clientX - a.u * o}px,${e.clientY - a.v * s}px)`, c.pointerEvents = "auto";
|
|
165
|
+
}
|
|
166
|
+
pipeline(e) {
|
|
167
|
+
let t = e.descriptor, n = `${t.multisample?.count} ${e.format} ${t.depthStencil?.format} ${t.depthStencil?.depthCompare}`, r = this.pipelines.get(n);
|
|
168
|
+
if (!r) {
|
|
169
|
+
let i = e.device.createShaderModule({ code: s });
|
|
170
|
+
r = e.device.createRenderPipeline({
|
|
171
|
+
layout: "auto",
|
|
172
|
+
vertex: {
|
|
173
|
+
module: i,
|
|
174
|
+
entryPoint: "vertex"
|
|
175
|
+
},
|
|
176
|
+
fragment: {
|
|
177
|
+
module: i,
|
|
178
|
+
entryPoint: "fragment",
|
|
179
|
+
constants: { hdr: +!!o(e.format) },
|
|
180
|
+
targets: [{
|
|
181
|
+
format: e.format,
|
|
182
|
+
blend: {
|
|
183
|
+
color: {
|
|
184
|
+
srcFactor: "one",
|
|
185
|
+
dstFactor: "one-minus-src-alpha"
|
|
186
|
+
},
|
|
187
|
+
alpha: {
|
|
188
|
+
srcFactor: "one",
|
|
189
|
+
dstFactor: "one-minus-src-alpha"
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}]
|
|
193
|
+
},
|
|
194
|
+
primitive: {
|
|
195
|
+
topology: "triangle-list",
|
|
196
|
+
cullMode: "none"
|
|
197
|
+
},
|
|
198
|
+
depthStencil: t.depthStencil && {
|
|
199
|
+
format: t.depthStencil.format,
|
|
200
|
+
depthWriteEnabled: !0,
|
|
201
|
+
depthCompare: t.depthStencil.depthCompare ?? "less"
|
|
202
|
+
},
|
|
203
|
+
multisample: t.multisample
|
|
204
|
+
}), this.pipelines.set(n, r);
|
|
205
|
+
}
|
|
206
|
+
return r;
|
|
207
|
+
}
|
|
208
|
+
refresh(t) {
|
|
209
|
+
if (!t.dirty || t.busy) return;
|
|
210
|
+
t.dirty = !1, t.busy = !0;
|
|
211
|
+
let [n, r] = t.cssSize, i = Math.max(1, Math.round(n * t.pixelRatio)), a = Math.max(1, Math.round(r * t.pixelRatio));
|
|
212
|
+
this.rasterize(t, n, r, i, a).then((e) => {
|
|
213
|
+
!this.disposed && this.surfaces.has(t) && (this.upload(t, e, i, a), t.draws++);
|
|
214
|
+
}).catch((n) => {
|
|
215
|
+
if (t.signal.aborted || this.disposed || !this.surfaces.has(t)) return;
|
|
216
|
+
let r = n instanceof Error ? n.message : String(n);
|
|
217
|
+
this.runtime?.onError(Object.assign(e("failed", `HTMLTexture could not draw its element (${r}); fix the element or its resources and call surface.invalidate(); see https://margoengine.com/errors#html-texture`), { cause: n }));
|
|
218
|
+
}).finally(() => {
|
|
219
|
+
t.busy = !1;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
upload(e, t, n, r) {
|
|
223
|
+
let i = this.runtime.gpu;
|
|
224
|
+
(!e.texture || e.texture.width !== n || e.texture.height !== r) && (e.texture?.destroy(), e.texture = i.createTexture({
|
|
225
|
+
size: [n, r],
|
|
226
|
+
format: "rgba8unorm",
|
|
227
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT
|
|
228
|
+
}), e.group = void 0), i.queue.copyExternalImageToTexture({ source: t }, {
|
|
229
|
+
texture: e.texture,
|
|
230
|
+
premultipliedAlpha: !0
|
|
231
|
+
}, [n, r]);
|
|
232
|
+
}
|
|
233
|
+
async rasterize(e, t, n, i, a) {
|
|
234
|
+
let o = e.holder;
|
|
235
|
+
if (o instanceof o.ownerDocument.defaultView.HTMLCanvasElement) {
|
|
236
|
+
o.width !== i && (o.width = i), o.height !== a && (o.height = a);
|
|
237
|
+
let t = o.getContext("2d");
|
|
238
|
+
return await new Promise((n, r) => {
|
|
239
|
+
e.painting = () => {
|
|
240
|
+
try {
|
|
241
|
+
t.reset(), t.drawElementImage(e.element, 0, 0, i, a), n();
|
|
242
|
+
} catch (e) {
|
|
243
|
+
r(e);
|
|
244
|
+
}
|
|
245
|
+
}, o.requestPaint?.();
|
|
246
|
+
}), o;
|
|
247
|
+
}
|
|
248
|
+
let s = this.prepare ? await this.prepare(e.element, e.signal) : r(e.element);
|
|
249
|
+
e.signal.throwIfAborted();
|
|
250
|
+
let c = `<svg xmlns="http://www.w3.org/2000/svg" width="${i}" height="${a}" viewBox="0 0 ${t} ${n}"><foreignObject x="0" y="0" width="${t}" height="${n}">${new XMLSerializer().serializeToString(s)}</foreignObject></svg>`, l = new Image(i, a);
|
|
251
|
+
l.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(c)}`, await l.decode();
|
|
252
|
+
let u = e.scratch ??= o.ownerDocument.createElement("canvas");
|
|
253
|
+
u.width !== i && (u.width = i), u.height !== a && (u.height = a);
|
|
254
|
+
let d = u.getContext("2d");
|
|
255
|
+
return d.clearRect(0, 0, i, a), d.drawImage(l, 0, 0, i, a), u;
|
|
256
|
+
}
|
|
257
|
+
draw(e) {
|
|
258
|
+
if (!this.surfaces.size) return;
|
|
259
|
+
let n = this.pipeline(e);
|
|
260
|
+
this.reversed = (e.descriptor.depthStencil?.depthCompare ?? "less").startsWith("greater"), this.sampler ??= e.device.createSampler({
|
|
261
|
+
magFilter: "linear",
|
|
262
|
+
minFilter: "linear"
|
|
263
|
+
});
|
|
264
|
+
let r;
|
|
265
|
+
for (let i of this.surfaces) {
|
|
266
|
+
if (this.refresh(i), !i.texture) continue;
|
|
267
|
+
i.uniform ??= e.device.createBuffer({
|
|
268
|
+
size: 144,
|
|
269
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
270
|
+
}), i.pipeline !== n && (i.group = void 0, i.pipeline = n), i.group ??= e.device.createBindGroup({
|
|
271
|
+
layout: n.getBindGroupLayout(0),
|
|
272
|
+
entries: [
|
|
273
|
+
{
|
|
274
|
+
binding: 0,
|
|
275
|
+
resource: { buffer: i.uniform }
|
|
276
|
+
},
|
|
277
|
+
{
|
|
278
|
+
binding: 1,
|
|
279
|
+
resource: i.texture.createView()
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
binding: 2,
|
|
283
|
+
resource: this.sampler
|
|
284
|
+
}
|
|
285
|
+
]
|
|
286
|
+
});
|
|
287
|
+
let a = this.values;
|
|
288
|
+
if (a.set(e.camera.matrix), a.set(this.worldMatrix(i), 16), a[32] = i.width, a[33] = i.height, e.device.queue.writeBuffer(i.uniform, 0, a), !r) {
|
|
289
|
+
let i = this.descriptor;
|
|
290
|
+
i.colorAttachments[0].view = e.color, i.depthStencilAttachment.view = e.depth, r = e.encoder.beginRenderPass(i), t(r, e), r.setPipeline(n);
|
|
291
|
+
}
|
|
292
|
+
r.setBindGroup(0, i.group), r.draw(6);
|
|
293
|
+
}
|
|
294
|
+
r?.end();
|
|
295
|
+
}
|
|
296
|
+
onDeviceRestored() {
|
|
297
|
+
this.pipelines.clear(), this.sampler = void 0;
|
|
298
|
+
for (let e of this.surfaces) e.texture = e.uniform = e.group = void 0, e.dirty = !0;
|
|
299
|
+
}
|
|
300
|
+
dispose() {
|
|
301
|
+
if (this.disposed) return;
|
|
302
|
+
this.disposed = !0;
|
|
303
|
+
let e = this.runtime;
|
|
304
|
+
e?.canvas.ownerDocument.removeEventListener("pointermove", this.pointer, !0), e?.canvas.ownerDocument.removeEventListener("pointerdown", this.pointer, !0);
|
|
305
|
+
for (let e of this.surfaces) e.detach();
|
|
306
|
+
this.surfaces.clear(), e?.sceneOf(this)?.removePlugin(this.name), this.runtime = void 0;
|
|
307
|
+
}
|
|
308
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
309
|
+
this.dispose();
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
//#endregion
|
|
313
|
+
export { f as n, u as r, d as t };
|