@margoengine/pixel 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 +334 -2
- package/dist/blit-DyxX_nQq.js +165 -0
- package/dist/blit.d.ts +49 -0
- package/dist/camera.d.ts +81 -0
- package/dist/crt.d.ts +63 -0
- package/dist/crt.js +117 -0
- package/dist/errors-CDgOgzCd.js +7 -0
- package/dist/follow.d.ts +90 -0
- package/dist/follow.js +172 -0
- package/dist/index.d.ts +160 -0
- package/dist/index.js +345 -0
- package/dist/palette.d.ts +124 -0
- package/dist/palette.js +344 -0
- package/dist/render3d.d.ts +139 -0
- package/dist/render3d.js +367 -0
- package/dist/scale.d.ts +38 -0
- package/dist/scale.js +123 -0
- package/dist/texture-option-3VFmNske.js +39 -0
- package/dist/view-rect-sVPGRhlO.js +39 -0
- package/package.json +69 -4
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { n as e, r as t, t as n } from "./errors-CDgOgzCd.js";
|
|
5
|
+
import { n as r, t as i } from "./view-rect-sVPGRhlO.js";
|
|
6
|
+
import { t as a } from "./texture-option-3VFmNske.js";
|
|
7
|
+
//#region src/blit.ts
|
|
8
|
+
function o(e, t, n, r, i = "contain") {
|
|
9
|
+
if (i === "stretch") return [
|
|
10
|
+
0,
|
|
11
|
+
0,
|
|
12
|
+
n,
|
|
13
|
+
r
|
|
14
|
+
];
|
|
15
|
+
let a = n * t <= r * e, o = a ? n : Math.floor(e * r / t), s = a ? Math.floor(t * n / e) : r;
|
|
16
|
+
return [
|
|
17
|
+
Math.floor((n - o) / 2),
|
|
18
|
+
Math.floor((r - s) / 2),
|
|
19
|
+
o,
|
|
20
|
+
s
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
var s = {
|
|
24
|
+
color: {
|
|
25
|
+
srcFactor: "one",
|
|
26
|
+
dstFactor: "one-minus-src-alpha"
|
|
27
|
+
},
|
|
28
|
+
alpha: {
|
|
29
|
+
srcFactor: "one",
|
|
30
|
+
dstFactor: "one-minus-src-alpha"
|
|
31
|
+
}
|
|
32
|
+
}, c = class {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
33
|
+
#e;
|
|
34
|
+
#t;
|
|
35
|
+
#n;
|
|
36
|
+
#r;
|
|
37
|
+
#i;
|
|
38
|
+
#a;
|
|
39
|
+
#o;
|
|
40
|
+
#s;
|
|
41
|
+
#c;
|
|
42
|
+
#l;
|
|
43
|
+
#u;
|
|
44
|
+
#d = /* @__PURE__ */ new Map();
|
|
45
|
+
#f = new i(() => ({}), (e) => e.buffer?.destroy());
|
|
46
|
+
#p = !1;
|
|
47
|
+
constructor(n, i, o, s, c) {
|
|
48
|
+
if (!n?.source) throw t(`${i} needs a source texture`, "pixel-options");
|
|
49
|
+
let l = n.order ?? 35;
|
|
50
|
+
if (!Number.isFinite(l)) throw e(`${i} order must be a finite number`, "pixel-options");
|
|
51
|
+
this.#e = new a(n.source, `${i} source`, "setSource"), this.#t = new a(n.target, `${i} target`, "setTarget"), this.#n = l, this.#r = r(n.layers ?? 1, `${i} layers`), this.fit = n.fit ?? "contain", this.#i = i, this.#a = o, this.#o = s, this.#s = new ArrayBuffer(s), this.name = n.name ?? c;
|
|
52
|
+
}
|
|
53
|
+
setSource(e) {
|
|
54
|
+
if (!e) throw t(`${this.#i} needs a source texture`, "pixel-options");
|
|
55
|
+
this.#e.set(e, this.#c?.gpu);
|
|
56
|
+
}
|
|
57
|
+
setTarget(e) {
|
|
58
|
+
this.#t.set(e, this.#c?.gpu);
|
|
59
|
+
}
|
|
60
|
+
setup(e) {
|
|
61
|
+
if (this.#p) throw n("disposed", `${this.#i} disposed; create a new one`, "disposed");
|
|
62
|
+
if (e.backend !== "webgpu") throw n("unsupported", `${this.#i} needs WebGPU`, "webgpu");
|
|
63
|
+
this.#c = e, this.#e.fix(e.gpu), this.#t.fix(e.gpu), e.frameGraph.add({
|
|
64
|
+
name: this.name,
|
|
65
|
+
order: this.#n,
|
|
66
|
+
execute: (e) => {
|
|
67
|
+
e.backend === "webgpu" && this.#h(e);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
onPreUpdate(e) {
|
|
72
|
+
e > 0 && this.#f.prune();
|
|
73
|
+
}
|
|
74
|
+
onDeviceRestored() {
|
|
75
|
+
this.#m();
|
|
76
|
+
}
|
|
77
|
+
dispose() {
|
|
78
|
+
this.#p || (this.#p = !0, this.#c?.sceneOf(this)?.removePlugin(this.name), this.#c = void 0, this.#m());
|
|
79
|
+
}
|
|
80
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
81
|
+
this.dispose();
|
|
82
|
+
}
|
|
83
|
+
#m() {
|
|
84
|
+
this.#f.clear(), this.#d.clear(), this.#u = this.#l = void 0;
|
|
85
|
+
}
|
|
86
|
+
#h(t) {
|
|
87
|
+
let r = t.view, i = this.#t.value !== void 0;
|
|
88
|
+
if (i ? r?.index : r?.target || r?.layers !== void 0 && !(r.layers & this.#r)) return;
|
|
89
|
+
if (this.fit !== "contain" && this.fit !== "stretch") throw e(`${this.#i} fit must be contain or stretch`, "pixel-options");
|
|
90
|
+
let a = t.device;
|
|
91
|
+
this.#l !== a && (this.#m(), this.#l = a), this.#u ??= a.createShaderModule({
|
|
92
|
+
label: this.#i,
|
|
93
|
+
code: this.#a
|
|
94
|
+
});
|
|
95
|
+
let c = this.#e.get(a, this.#c), l = i ? this.#t.get(a, this.#c) : void 0;
|
|
96
|
+
if (!c || i && !l) return;
|
|
97
|
+
if (c === l) throw n("state", `${this.#i} source and target must differ`, "pixel-options");
|
|
98
|
+
let u = l ? l.format : t.format, d = l ? 1 : t.descriptor.multisample?.count ?? 1, f = `${u}/${d}`, p = this.#d.get(f);
|
|
99
|
+
p || (p = a.createRenderPipeline({
|
|
100
|
+
label: this.#i,
|
|
101
|
+
layout: "auto",
|
|
102
|
+
vertex: {
|
|
103
|
+
module: this.#u,
|
|
104
|
+
entryPoint: "vs"
|
|
105
|
+
},
|
|
106
|
+
fragment: {
|
|
107
|
+
module: this.#u,
|
|
108
|
+
entryPoint: "fs",
|
|
109
|
+
targets: [{
|
|
110
|
+
format: u,
|
|
111
|
+
blend: s
|
|
112
|
+
}]
|
|
113
|
+
},
|
|
114
|
+
multisample: { count: d }
|
|
115
|
+
}), this.#d.set(f, p));
|
|
116
|
+
let m = l ? 0 : r ? r.viewport[0] : 0, h = l ? 0 : r ? r.viewport[1] : 0, g = l ? l.width : r ? r.viewport[2] : t.width, _ = l ? l.height : r ? r.viewport[3] : t.height, v = o(c.width, c.height, g, _, this.fit), y = [
|
|
117
|
+
m + v[0],
|
|
118
|
+
h + v[1],
|
|
119
|
+
v[2],
|
|
120
|
+
v[3]
|
|
121
|
+
], b = this.#f.get(t);
|
|
122
|
+
b.buffer ??= a.createBuffer({
|
|
123
|
+
label: this.#i,
|
|
124
|
+
size: this.#o,
|
|
125
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
126
|
+
}), (!b.group || b.groupFor !== c || b.groupPipeline !== p) && (b.group = a.createBindGroup({
|
|
127
|
+
layout: p.getBindGroupLayout(0),
|
|
128
|
+
entries: [{
|
|
129
|
+
binding: 0,
|
|
130
|
+
resource: { buffer: b.buffer }
|
|
131
|
+
}, {
|
|
132
|
+
binding: 1,
|
|
133
|
+
resource: c.createView()
|
|
134
|
+
}]
|
|
135
|
+
}), b.groupFor = c, b.groupPipeline = p), this.fill(this.#s, y, c), a.queue.writeBuffer(b.buffer, 0, this.#s);
|
|
136
|
+
let x = t.encoder.beginRenderPass({ colorAttachments: [l ? {
|
|
137
|
+
view: l.createView(),
|
|
138
|
+
loadOp: "clear",
|
|
139
|
+
clearValue: [
|
|
140
|
+
0,
|
|
141
|
+
0,
|
|
142
|
+
0,
|
|
143
|
+
0
|
|
144
|
+
],
|
|
145
|
+
storeOp: "store"
|
|
146
|
+
} : {
|
|
147
|
+
view: t.color,
|
|
148
|
+
loadOp: "load",
|
|
149
|
+
storeOp: "store"
|
|
150
|
+
}] }), S = l ? l.width : t.width, C = l ? l.height : t.height, w = l ? [
|
|
151
|
+
0,
|
|
152
|
+
0,
|
|
153
|
+
S,
|
|
154
|
+
C
|
|
155
|
+
] : r?.scissor ?? [
|
|
156
|
+
0,
|
|
157
|
+
0,
|
|
158
|
+
S,
|
|
159
|
+
C
|
|
160
|
+
], T = Math.max(0, y[0], w[0]), E = Math.max(0, y[1], w[1]), D = Math.min(S, y[0] + y[2], w[0] + w[2]), O = Math.min(C, y[1] + y[3], w[1] + w[3]);
|
|
161
|
+
D > T && O > E && (x.setScissorRect(T, E, D - T, O - E), x.setPipeline(p), x.setBindGroup(0, b.group), x.draw(3)), x.end();
|
|
162
|
+
}
|
|
163
|
+
}, l = "@vertex fn vs(@builtin(vertex_index) i:u32)->@builtin(position) vec4f{\nreturn vec4f(f32(i&1u)*4-1,f32(i>>1u)*4-1,0,1);\n}";
|
|
164
|
+
//#endregion
|
|
165
|
+
export { l as n, o as r, c as t };
|
package/dist/blit.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Engine, EnginePlugin } from '@margoengine/core/extension';
|
|
2
|
+
/**
|
|
3
|
+
* A texture, or a function returning one each frame such as a pixel layer's `target`. A function is read every frame and
|
|
4
|
+
* needs nothing at a device restore; a plain texture belongs to the device it was given on (see `setSource`).
|
|
5
|
+
*/
|
|
6
|
+
export type TextureSource = GPUTexture | (() => GPUTexture);
|
|
7
|
+
/** How the picture fills its view: as large as fits keeping its shape (`'contain'`), or all of it (`'stretch'`). */
|
|
8
|
+
export type PixelFit = 'contain' | 'stretch';
|
|
9
|
+
/** Settings the scale and CRT passes share. */
|
|
10
|
+
export interface ScreenPassOptions {
|
|
11
|
+
/** The low-res picture, colours premultiplied by alpha as in a pixel layer. Required. */
|
|
12
|
+
source: TextureSource;
|
|
13
|
+
/** Where to draw; it is cleared first. Default each canvas view, drawn over what is there. */
|
|
14
|
+
target?: TextureSource;
|
|
15
|
+
/** Read each frame. Default `'contain'`. */
|
|
16
|
+
fit?: PixelFit;
|
|
17
|
+
/** Render-layer bits; a canvas view whose `layers` shares none is left alone. Default 1. */
|
|
18
|
+
layers?: number;
|
|
19
|
+
/** Frame-graph order. Default 35, after a compositor (30). */
|
|
20
|
+
order?: number;
|
|
21
|
+
/** Plugin name, unique per scene. */
|
|
22
|
+
name?: string;
|
|
23
|
+
}
|
|
24
|
+
/** The whole output pixels a picture of `width` x `height` covers in a view of `viewWidth` x `viewHeight`. */
|
|
25
|
+
export declare function fitRect(width: number, height: number, viewWidth: number, viewHeight: number, fit?: PixelFit): [number, number, number, number];
|
|
26
|
+
/** The base of `PixelScale` and `CRT`. */
|
|
27
|
+
export declare abstract class ScreenPass implements EnginePlugin {
|
|
28
|
+
#private;
|
|
29
|
+
readonly name: string;
|
|
30
|
+
/** Read each frame. */
|
|
31
|
+
fit: PixelFit;
|
|
32
|
+
protected constructor(options: ScreenPassOptions, label: string, code: string, bytes: number, name: string);
|
|
33
|
+
/**
|
|
34
|
+
* Replaces the source from the next frame. A plain texture belongs to the engine's device as it is now, so call this from
|
|
35
|
+
* `onDeviceRestored` to bring a lost one back.
|
|
36
|
+
*/
|
|
37
|
+
setSource(source: TextureSource): void;
|
|
38
|
+
/** Replaces the target from the next frame (undefined: draw over each canvas view). The same rule as `setSource`. */
|
|
39
|
+
setTarget(target: TextureSource | undefined): void;
|
|
40
|
+
/** Writes the pass's uniforms: the drawn rectangle in output pixels, and the source's size. */
|
|
41
|
+
protected abstract fill(values: ArrayBuffer, rect: readonly number[], source: GPUTexture): void;
|
|
42
|
+
setup(engine: Engine): void;
|
|
43
|
+
onPreUpdate(delta: number): void;
|
|
44
|
+
onDeviceRestored(): void;
|
|
45
|
+
dispose(): void;
|
|
46
|
+
[Symbol.dispose]: () => void;
|
|
47
|
+
}
|
|
48
|
+
/** The full-screen triangle every screen pass draws. */
|
|
49
|
+
export declare const VERTEX = "@vertex fn vs(@builtin(vertex_index) i:u32)->@builtin(position) vec4f{\nreturn vec4f(f32(i&1u)*4-1,f32(i>>1u)*4-1,0,1);\n}";
|
package/dist/camera.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/** How fast a layer scrolls against the camera: one factor for both axes, or `[x, y]`. 1 moves with the camera, 0 stays put. */
|
|
2
|
+
export type Parallax = number | readonly [number, number];
|
|
3
|
+
/** Settings of {@link PixelCamera}, fixed when it is created except where a member says otherwise. */
|
|
4
|
+
export interface PixelCameraOptions {
|
|
5
|
+
/** Low-res width in pixels, a whole number from 1. Required. */
|
|
6
|
+
width: number;
|
|
7
|
+
/** Low-res height in pixels, a whole number from 1. Required. */
|
|
8
|
+
height: number;
|
|
9
|
+
/** World position at the top-left of the view, y down. Default 0. */
|
|
10
|
+
x?: number;
|
|
11
|
+
/** World position at the top-left of the view, y down. Default 0. */
|
|
12
|
+
y?: number;
|
|
13
|
+
/** Output pixels per low-res pixel, a whole number. Default: the largest that fits the canvas, every frame. */
|
|
14
|
+
zoom?: number;
|
|
15
|
+
/** Smallest zoom, a whole number from 1. Default 1. */
|
|
16
|
+
minZoom?: number;
|
|
17
|
+
/** Largest zoom, a whole number. Default 64. */
|
|
18
|
+
maxZoom?: number;
|
|
19
|
+
/** World units in one low-res pixel, above 0. Default 1. */
|
|
20
|
+
unitsPerPixel?: number;
|
|
21
|
+
/**
|
|
22
|
+
* Carries the part of the scroll below one low-res pixel into the composite, in whole output pixels. Default false:
|
|
23
|
+
* the picture moves only when the scroll reaches a whole low-res pixel.
|
|
24
|
+
*/
|
|
25
|
+
smooth?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** A scroll split into whole low-res pixels and what is left over, from 0 up to 1. */
|
|
28
|
+
export interface LayerScroll {
|
|
29
|
+
x: number;
|
|
30
|
+
y: number;
|
|
31
|
+
subX: number;
|
|
32
|
+
subY: number;
|
|
33
|
+
}
|
|
34
|
+
/** Orthographic bounds for `CameraProjection` of `@margoengine/cameras`. */
|
|
35
|
+
export interface PixelProjection {
|
|
36
|
+
mode: 'orthographic';
|
|
37
|
+
left: number;
|
|
38
|
+
right: number;
|
|
39
|
+
top: number;
|
|
40
|
+
bottom: number;
|
|
41
|
+
zoom: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The view of a pixel-art world: a low-res size, a position in world units, a whole-number zoom and parallax for each
|
|
45
|
+
* layer. Scrolling goes by whole low-res pixels, so nothing shimmers. Runs without a GPU.
|
|
46
|
+
* @throws RangeError when a size or zoom is not a whole number in range, or a position or parallax is not finite.
|
|
47
|
+
*/
|
|
48
|
+
export declare class PixelCamera {
|
|
49
|
+
/** World position at the top-left of the view, y down, read each frame. */
|
|
50
|
+
x: number;
|
|
51
|
+
/** World position at the top-left of the view, y down, read each frame. */
|
|
52
|
+
y: number;
|
|
53
|
+
/** While true the compositor sets the zoom to the largest that fits, every frame. Setting {@link zoom} turns it off. */
|
|
54
|
+
autoFit: boolean;
|
|
55
|
+
readonly width: number;
|
|
56
|
+
readonly height: number;
|
|
57
|
+
readonly minZoom: number;
|
|
58
|
+
readonly maxZoom: number;
|
|
59
|
+
readonly unitsPerPixel: number;
|
|
60
|
+
readonly smooth: boolean;
|
|
61
|
+
private current;
|
|
62
|
+
constructor(options: PixelCameraOptions);
|
|
63
|
+
/** Output pixels per low-res pixel. Whole numbers only: anything else throws, as a blurred scale has no place here. */
|
|
64
|
+
get zoom(): number;
|
|
65
|
+
set zoom(value: number);
|
|
66
|
+
/** Sets the zoom to the largest whole number that fits `width` x `height` output pixels, inside the bounds, and returns it. */
|
|
67
|
+
fit(width: number, height: number): number;
|
|
68
|
+
/** Whole low-res pixels scrolled along x at this parallax. */
|
|
69
|
+
pixelX(parallax?: Parallax): number;
|
|
70
|
+
/** Whole low-res pixels scrolled along y at this parallax. */
|
|
71
|
+
pixelY(parallax?: Parallax): number;
|
|
72
|
+
/** The scroll at this parallax, split into whole low-res pixels and the rest. Fills and returns `out` when given. */
|
|
73
|
+
scroll(parallax?: Parallax, out?: LayerScroll): LayerScroll;
|
|
74
|
+
/**
|
|
75
|
+
* Orthographic bounds that show this view through a core camera looking down -z with +y up: world `(x, -y)` is the
|
|
76
|
+
* low-res pixel `(x, y)`. `new CameraProjection(camera.projection())`, or `Object.assign` them each frame to scroll.
|
|
77
|
+
*/
|
|
78
|
+
projection(parallax?: Parallax): PixelProjection;
|
|
79
|
+
private exactX;
|
|
80
|
+
private exactY;
|
|
81
|
+
}
|
package/dist/crt.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { ScreenPass, type ScreenPassOptions } from './blit.js';
|
|
4
|
+
export { fitRect, type PixelFit, type ScreenPassOptions, type TextureSource } from './blit.js';
|
|
5
|
+
/** Horizontal source-pixel offset, or [horizontal, vertical], each finite in [-32,32]. */
|
|
6
|
+
export type ChromaticOffset = number | readonly [number, number];
|
|
7
|
+
/** Settings of {@link CRT}; every look setting is also a field read each frame. */
|
|
8
|
+
export interface CRTOptions extends ScreenPassOptions {
|
|
9
|
+
/** How much the glass bends, 0 (flat) to 0.5. Default 0.08. */
|
|
10
|
+
curvature?: number;
|
|
11
|
+
/** How dark the gaps between source rows get, 0 to 1. Default 0.5. */
|
|
12
|
+
scanlines?: number;
|
|
13
|
+
/** How much the grille dims the two colours a column does not pass, 0 to 1. Default 0.2. */
|
|
14
|
+
mask?: number;
|
|
15
|
+
/** How much of a blurred copy of the source is added, 0 to 1. Default 0.25. */
|
|
16
|
+
glow?: number;
|
|
17
|
+
/** Multiplies the beam, to win back what the lines and grille take, 0 to 4. Default 1.2. */
|
|
18
|
+
brightness?: number;
|
|
19
|
+
/** NTSC-style horizontal YIQ chroma bleed, 0 to 1. Default 0 (off). Luma stays sharp. */
|
|
20
|
+
colorBleed?: number;
|
|
21
|
+
/** Red samples at +offset and blue at -offset, in source pixels. Default 0. */
|
|
22
|
+
chromaticOffset?: ChromaticOffset;
|
|
23
|
+
/** Extra independent red-channel source-pixel offset, added to chromaticOffset. Default 0. */
|
|
24
|
+
redOffset?: ChromaticOffset;
|
|
25
|
+
/** Independent green-channel source-pixel offset. Default 0. */
|
|
26
|
+
greenOffset?: ChromaticOffset;
|
|
27
|
+
/** Extra independent blue-channel source-pixel offset, added to -chromaticOffset. Default 0. */
|
|
28
|
+
blueOffset?: ChromaticOffset;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Draws a low-res picture to the canvas (or a target) as on a CRT: curved glass, scanlines, an aperture grille and
|
|
32
|
+
* phosphor glow. One frame-graph pass a view. WebGPU only.
|
|
33
|
+
* @throws TypeError / RangeError on invalid options or, each frame, a look setting out of its range.
|
|
34
|
+
* @throws MargoError `'unsupported'` on a WebGL engine, `'disposed'` after `dispose()`, `'state'` when the source and the
|
|
35
|
+
* target are the same texture.
|
|
36
|
+
*/
|
|
37
|
+
export declare class CRT extends ScreenPass {
|
|
38
|
+
#private;
|
|
39
|
+
/** Read each frame. */
|
|
40
|
+
curvature: number;
|
|
41
|
+
/** Read each frame. */
|
|
42
|
+
scanlines: number;
|
|
43
|
+
/** Read each frame. */
|
|
44
|
+
mask: number;
|
|
45
|
+
/** Read each frame. */
|
|
46
|
+
glow: number;
|
|
47
|
+
/** Read each frame. */
|
|
48
|
+
brightness: number;
|
|
49
|
+
/** Live NTSC-style chroma bleed amount, 0 to 1. Default 0. */
|
|
50
|
+
colorBleed: number;
|
|
51
|
+
/** Live symmetric red/blue source-pixel displacement. Default 0. */
|
|
52
|
+
chromaticOffset: ChromaticOffset;
|
|
53
|
+
/** Live extra red source-pixel displacement. Default 0. */
|
|
54
|
+
redOffset: ChromaticOffset;
|
|
55
|
+
/** Live green source-pixel displacement. Default 0. */
|
|
56
|
+
greenOffset: ChromaticOffset;
|
|
57
|
+
/** Live extra blue source-pixel displacement. Default 0. */
|
|
58
|
+
blueOffset: ChromaticOffset;
|
|
59
|
+
constructor(options: CRTOptions);
|
|
60
|
+
/** Creates a pass and attaches it to `instance`. */
|
|
61
|
+
static attach(instance: EngineKernel, options: CRTOptions): CRT;
|
|
62
|
+
protected fill(values: ArrayBuffer, rect: readonly number[]): void;
|
|
63
|
+
}
|
package/dist/crt.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const i=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { n as e } from "./errors-CDgOgzCd.js";
|
|
5
|
+
import { n as t, r as n, t as r } from "./blit-DyxX_nQq.js";
|
|
6
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
|
+
//#region src/crt.ts
|
|
8
|
+
var a = `
|
|
9
|
+
struct S{rect:vec4i,curvature:f32,scanlines:f32,mask:f32,glow:f32,brightness:f32,
|
|
10
|
+
bleed:f32,offset:vec2f,red:vec2f,green:vec2f,blue:vec2f}
|
|
11
|
+
@group(0) @binding(0) var<uniform> s:S;
|
|
12
|
+
@group(0) @binding(1) var image:texture_2d<f32>;
|
|
13
|
+
${t}
|
|
14
|
+
fn at(t:vec2i)->vec4f{
|
|
15
|
+
return textureLoad(image,clamp(t,vec2i(0),vec2i(textureDimensions(image))-1),0);
|
|
16
|
+
}
|
|
17
|
+
fn signal(t:vec2i)->vec4f{
|
|
18
|
+
let e=at(t);
|
|
19
|
+
if(s.bleed==0){return e;}
|
|
20
|
+
let near1=at(t-vec2i(1,0)).rgb+at(t+vec2i(1,0)).rgb;
|
|
21
|
+
let near2=at(t-vec2i(2,0)).rgb+at(t+vec2i(2,0)).rgb;
|
|
22
|
+
let near3=at(t-vec2i(3,0)).rgb+at(t+vec2i(3,0)).rgb;
|
|
23
|
+
let near4=at(t-vec2i(4,0)).rgb+at(t+vec2i(4,0)).rgb;
|
|
24
|
+
|
|
25
|
+
let di=dot((6*e.rgb+4*near1+near2)/16-e.rgb,vec3f(0.5959,-0.2746,-0.3213));
|
|
26
|
+
let dq=dot((70*e.rgb+56*near1+28*near2+8*near3+near4)/256-e.rgb,vec3f(0.2115,-0.5227,0.3112));
|
|
27
|
+
let rgb=e.rgb+s.bleed*(di*vec3f(0.956050226,-0.272052344,-1.106704315)
|
|
28
|
+
+dq*vec3f(0.620754941,-0.647205713,1.704421284));
|
|
29
|
+
return vec4f(clamp(rgb,vec3f(0),vec3f(e.a)),e.a);
|
|
30
|
+
}
|
|
31
|
+
fn shifted(t:vec2i,offset:vec2f)->vec4f{
|
|
32
|
+
let p=vec2f(t)+offset;
|
|
33
|
+
let lo=vec2i(floor(p)); let f=fract(p);
|
|
34
|
+
var top=signal(lo);
|
|
35
|
+
if(f.x!=0){top=mix(top,signal(lo+vec2i(1,0)),f.x);}
|
|
36
|
+
if(f.y==0){return top;}
|
|
37
|
+
var bottom=signal(lo+vec2i(0,1));
|
|
38
|
+
if(f.x!=0){bottom=mix(bottom,signal(lo+vec2i(1,1)),f.x);}
|
|
39
|
+
return mix(top,bottom,f.y);
|
|
40
|
+
}
|
|
41
|
+
fn composite(t:vec2i)->vec4f{
|
|
42
|
+
let red=shifted(t,s.offset+s.red);
|
|
43
|
+
let green=shifted(t,s.green);
|
|
44
|
+
let blue=shifted(t,-s.offset+s.blue);
|
|
45
|
+
return vec4f(red.r,green.g,blue.b,max(red.a,max(green.a,blue.a)));
|
|
46
|
+
}
|
|
47
|
+
@fragment fn fs(@builtin(position) p:vec4f)->@location(0) vec4f{
|
|
48
|
+
let o=floor(p.xy)-vec2f(s.rect.xy);
|
|
49
|
+
let w=vec2f(s.rect.zw);
|
|
50
|
+
let n=vec2f(textureDimensions(image));
|
|
51
|
+
|
|
52
|
+
var t=(o+0.5)*n/w;
|
|
53
|
+
var i=clamp((vec2i(o)*2+1)*vec2i(n)/(s.rect.zw*2),vec2i(0),vec2i(n)-1);
|
|
54
|
+
if(s.curvature!=0){
|
|
55
|
+
let c=(o+0.5)/w*2-1;
|
|
56
|
+
let b=c*(1+s.curvature*c.yx*c.yx);
|
|
57
|
+
if(any(abs(b)>vec2f(1))){return vec4f(0,0,0,1);}
|
|
58
|
+
t=(b*0.5+0.5)*n;
|
|
59
|
+
i=vec2i(min(floor(t),n-1));
|
|
60
|
+
}
|
|
61
|
+
let f=t.y-floor(t.y);
|
|
62
|
+
let beam=1-s.scanlines*(1-sin(3.14159265*f)*sin(3.14159265*f));
|
|
63
|
+
let column=u32(o.x)%3u;
|
|
64
|
+
var grille=vec3f(1-s.mask);
|
|
65
|
+
grille[column]=1;
|
|
66
|
+
let e=composite(i);
|
|
67
|
+
var blur=4*at(i);
|
|
68
|
+
blur+=2*(at(i+vec2i(1,0))+at(i-vec2i(1,0))+at(i+vec2i(0,1))+at(i-vec2i(0,1)));
|
|
69
|
+
blur+=at(i+vec2i(1,1))+at(i+vec2i(-1,1))+at(i+vec2i(1,-1))+at(i+vec2i(-1,-1));
|
|
70
|
+
blur/=16;
|
|
71
|
+
let rgb=e.rgb*beam*grille*s.brightness+s.glow*blur.rgb;
|
|
72
|
+
let a=clamp(e.a+s.glow*blur.a,0,1);
|
|
73
|
+
return vec4f(min(rgb,vec3f(a)),a);
|
|
74
|
+
}`, o = 0, s = class t extends r {
|
|
75
|
+
constructor(e) {
|
|
76
|
+
super(e, "CRT", a, 80, `crt-${++o}`), this.curvature = e.curvature ?? .08, this.scanlines = e.scanlines ?? .5, this.mask = e.mask ?? .2, this.glow = e.glow ?? .25, this.brightness = e.brightness ?? 1.2, this.colorBleed = e.colorBleed ?? 0, this.chromaticOffset = e.chromaticOffset ?? 0, this.redOffset = e.redOffset ?? 0, this.greenOffset = e.greenOffset ?? 0, this.blueOffset = e.blueOffset ?? 0, this.#e();
|
|
77
|
+
}
|
|
78
|
+
static attach(e, n) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
79
|
+
let r = new t(n);
|
|
80
|
+
return i(e).use(r), r;
|
|
81
|
+
}
|
|
82
|
+
#e() {
|
|
83
|
+
for (let [t, n] of [
|
|
84
|
+
["curvature", .5],
|
|
85
|
+
["scanlines", 1],
|
|
86
|
+
["mask", 1],
|
|
87
|
+
["glow", 1],
|
|
88
|
+
["brightness", 4],
|
|
89
|
+
["colorBleed", 1]
|
|
90
|
+
]) if (!(this[t] >= 0 && this[t] <= n)) throw e(`CRT ${t} must be between 0 and ${n}, got ${this[t]}`, "pixel-options");
|
|
91
|
+
for (let t of [
|
|
92
|
+
"chromaticOffset",
|
|
93
|
+
"redOffset",
|
|
94
|
+
"greenOffset",
|
|
95
|
+
"blueOffset"
|
|
96
|
+
]) {
|
|
97
|
+
let n = c(this[t]);
|
|
98
|
+
if (n.length !== 2 || n.some((e) => !Number.isFinite(e) || Math.abs(e) > 32)) throw e(`CRT ${t} must be finite source pixels in [-32,32], or an [x,y] pair in that range`, "pixel-options");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
fill(e, t) {
|
|
102
|
+
this.#e(), new Int32Array(e).set(t, 0), new Float32Array(e).set([
|
|
103
|
+
this.curvature,
|
|
104
|
+
this.scanlines,
|
|
105
|
+
this.mask,
|
|
106
|
+
this.glow,
|
|
107
|
+
this.brightness,
|
|
108
|
+
this.colorBleed,
|
|
109
|
+
...c(this.chromaticOffset),
|
|
110
|
+
...c(this.redOffset),
|
|
111
|
+
...c(this.greenOffset),
|
|
112
|
+
...c(this.blueOffset)
|
|
113
|
+
], 4);
|
|
114
|
+
}
|
|
115
|
+
}, c = (e) => typeof e == "number" ? [e, 0] : e;
|
|
116
|
+
//#endregion
|
|
117
|
+
export { s as CRT, n as fitRect };
|
|
@@ -0,0 +1,7 @@
|
|
|
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)), r = (e, n) => TypeError(t(e, n)), i = (e, n) => RangeError(t(e, n));
|
|
6
|
+
//#endregion
|
|
7
|
+
export { i as n, r, n as t };
|
package/dist/follow.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type Engine } from '@margoengine/core/extension';
|
|
4
|
+
import { ScreenPass } from './blit.js';
|
|
5
|
+
import type { PixelCamera } from './camera.js';
|
|
6
|
+
/** A position in world units, y down. Objects and live getters can be followed. */
|
|
7
|
+
export interface FollowTarget {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
}
|
|
11
|
+
/** World rectangle, in the same units as the camera. */
|
|
12
|
+
export interface FollowBounds extends FollowTarget {
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
}
|
|
16
|
+
/** Linear RGB components in [0, 1]. */
|
|
17
|
+
export type FollowColor = readonly [number, number, number];
|
|
18
|
+
/** Settings for the whole-pixel camera rig. */
|
|
19
|
+
export interface PixelFollowOptions {
|
|
20
|
+
/** Existing camera to drive. Required. */
|
|
21
|
+
camera: PixelCamera;
|
|
22
|
+
/** Live position or getter. Default no following. */
|
|
23
|
+
target?: FollowTarget | (() => FollowTarget);
|
|
24
|
+
/** Fraction closed per 60 Hz frame, scalar or [x, y], in [0,1]. Default 1. Time-correct at other rates. */
|
|
25
|
+
lerp?: number | readonly [number, number];
|
|
26
|
+
/** Centered stationary window in low-res pixels. Default [0,0]. */
|
|
27
|
+
deadZone?: readonly [number, number];
|
|
28
|
+
/** World-space target offset. Default [0,0]. */
|
|
29
|
+
offset?: readonly [number, number];
|
|
30
|
+
/** World bounds. Default unlimited; a smaller-than-view world is centered. */
|
|
31
|
+
bounds?: FollowBounds;
|
|
32
|
+
/** Overlay frame-graph order, after CRT and scaling by default. Default 100. */
|
|
33
|
+
order?: number;
|
|
34
|
+
/** Overlay render-layer bits. Default 1. */
|
|
35
|
+
layers?: number;
|
|
36
|
+
/** Plugin name. Default an automatically unique name. */
|
|
37
|
+
name?: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A GPU-free camera controller when updated manually; attach it to an engine for automatic updates and flash/fade
|
|
41
|
+
* overlays on every matching canvas view. Positions and shakes always land on the low-res pixel lattice.
|
|
42
|
+
* @throws RangeError for invalid durations, positions, bounds or settings; MargoError disposed after disposal.
|
|
43
|
+
*/
|
|
44
|
+
export declare class PixelFollow extends ScreenPass {
|
|
45
|
+
#private;
|
|
46
|
+
/** Camera driven by the rig. */
|
|
47
|
+
readonly camera: PixelCamera;
|
|
48
|
+
/** Live target, or undefined to stop following. Read each update. */
|
|
49
|
+
target?: FollowTarget | (() => FollowTarget);
|
|
50
|
+
/** Per-axis smoothing, read each update. Default 1. */
|
|
51
|
+
lerp: number | readonly [number, number];
|
|
52
|
+
/** Centered dead zone in low-res pixels, read each update. Default [0,0]. */
|
|
53
|
+
deadZone: readonly [number, number];
|
|
54
|
+
/** Target offset in world units, read each update. Default [0,0]. */
|
|
55
|
+
offset: readonly [number, number];
|
|
56
|
+
/** World-space bounds, read each update. */
|
|
57
|
+
bounds?: FollowBounds;
|
|
58
|
+
constructor(options: PixelFollowOptions);
|
|
59
|
+
/** Attaches automatic updates and the view overlays to an engine. */
|
|
60
|
+
static attach(instance: EngineKernel, options: PixelFollowOptions): PixelFollow;
|
|
61
|
+
setup(engine: Engine): void;
|
|
62
|
+
onDeviceRestored(): void;
|
|
63
|
+
/** Seconds of frame updates the rig has run. */
|
|
64
|
+
get time(): number;
|
|
65
|
+
/**
|
|
66
|
+
* One frame of the rig with no draw: {@link PixelFollow.update} (camera follow, shake, flash and fade). Each frame
|
|
67
|
+
* update runs it, so `seek` from `@margoengine/cutscene/seek` replays it exactly while film code moves the target on the way.
|
|
68
|
+
*/
|
|
69
|
+
advanceTime(delta: number): void;
|
|
70
|
+
onPreUpdate(delta: number): void;
|
|
71
|
+
/**
|
|
72
|
+
* Advances by nonnegative seconds. Keep the fractional tracking state internally to avoid rounding stalls.
|
|
73
|
+
* Manual camera edits reset tracking from that position. An attached rig is updated by the engine instead.
|
|
74
|
+
*/
|
|
75
|
+
update(delta: number): this;
|
|
76
|
+
/** Current premultiplied overlay RGBA. Useful for another renderer or a GPU-free game loop. */
|
|
77
|
+
get overlay(): readonly number[];
|
|
78
|
+
/** Starts a deterministic decaying shake. Amplitudes are low-res pixels; default 4, duration 0.25 seconds, seed 1. */
|
|
79
|
+
shake(duration?: number, amplitude?: number | readonly [number, number], seed?: number): this;
|
|
80
|
+
/** Flashes from a solid colour to clear. Default 0.25 seconds and white. Replaces an existing flash. */
|
|
81
|
+
flash(duration?: number, tint?: FollowColor): this;
|
|
82
|
+
/** Fades to a solid colour and holds it. Default 0.5 seconds and black. */
|
|
83
|
+
fadeOut(duration?: number, tint?: FollowColor): this;
|
|
84
|
+
/** Fades from a solid colour to clear. Default 0.5 seconds and black. */
|
|
85
|
+
fadeIn(duration?: number, tint?: FollowColor): this;
|
|
86
|
+
/** Stops shake and clears both overlays. Leaves the follow target and camera position intact. */
|
|
87
|
+
resetEffects(): this;
|
|
88
|
+
protected fill(values: ArrayBuffer, rect: readonly number[]): void;
|
|
89
|
+
dispose(): void;
|
|
90
|
+
}
|