@margoengine/pixel 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.
@@ -0,0 +1,367 @@
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 { n as e, r as t, t as n } from "./errors-CDgOgzCd.js";
5
+ import { t as r } from "./texture-option-3VFmNske.js";
6
+ import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
7
+ import { hexColor as a } from "@margoengine/math";
8
+ import { invertMatrix as o } from "@margoengine/math/mat4";
9
+ //#region src/render3d.ts
10
+ var s = (t) => {
11
+ let n = o(t);
12
+ if (!n) throw e("A Render3D camera matrix needs an inverse", "pixel-low-res");
13
+ return n;
14
+ }, c = (e, t, n, r) => {
15
+ let i = e.depth[r * e.width + n];
16
+ if (!i) return;
17
+ let a = (n + .5) / e.width * 2 - 1, o = 1 - (r + .5) / e.height * 2, s = t, c = s[3] * a + s[7] * o + s[11] * i + s[15];
18
+ return [
19
+ 0,
20
+ 1,
21
+ 2
22
+ ].map((e) => (s[e] * a + s[e + 4] * o + s[e + 8] * i + s[e + 12]) / c);
23
+ };
24
+ function l(e, t = {}) {
25
+ let { width: n, height: r, eye: i, forward: a } = e, o = s(e.matrix), { depthEdge: l = .1, crease: u = .6 } = t, d = new Uint8Array(n * r), f = (t, i) => t < 0 || i < 0 || t >= n || i >= r ? void 0 : c(e, o, t, i), p = (e) => (e[0] - i[0]) * a[0] + (e[1] - i[1]) * a[1] + (e[2] - i[2]) * a[2], m = (e, t, n) => {
26
+ let r = f(e + 1, t), i = f(e, t + 1);
27
+ if (!r || !i) return;
28
+ let a = [
29
+ r[0] - n[0],
30
+ r[1] - n[1],
31
+ r[2] - n[2]
32
+ ], o = [
33
+ i[0] - n[0],
34
+ i[1] - n[1],
35
+ i[2] - n[2]
36
+ ], s = [
37
+ a[1] * o[2] - a[2] * o[1],
38
+ a[2] * o[0] - a[0] * o[2],
39
+ a[0] * o[1] - a[1] * o[0]
40
+ ], c = Math.hypot(s[0], s[1], s[2]);
41
+ return c ? s.map((e) => e / c) : void 0;
42
+ };
43
+ for (let e = 0; e < r; e++) for (let t = 0; t < n; t++) {
44
+ let i = f(t, e);
45
+ if (!i) continue;
46
+ let a = p(i), o = !1;
47
+ for (let [i, s] of [
48
+ [1, 0],
49
+ [-1, 0],
50
+ [0, 1],
51
+ [0, -1]
52
+ ]) {
53
+ let c = f(t + i, e + s);
54
+ t + i < 0 || e + s < 0 || t + i >= n || e + s >= r || (!c || p(c) - a > l) && (o = !0);
55
+ }
56
+ if (!o && u > 0) {
57
+ let n = m(t, e, i);
58
+ for (let [r, i] of [[1, 0], [0, 1]]) {
59
+ let a = f(t + r, e + i), s = a && m(t + r, e + i, a);
60
+ n && s && n[0] * s[0] + n[1] * s[1] + n[2] * s[2] < Math.cos(u) && (o = !0);
61
+ }
62
+ }
63
+ o && (d[e * n + t] = 1);
64
+ }
65
+ return d;
66
+ }
67
+ var u = (e) => `
68
+ struct Settings{inverse:mat4x4f,eye:vec4f,forward:vec4f,color:vec4f,size:vec2f}
69
+ @group(0) @binding(0) var<uniform> settings:Settings;
70
+ @group(0) @binding(1) var image:texture_2d<f32>;
71
+ @group(0) @binding(2) var depth:${e > 1 ? "texture_depth_multisampled_2d" : "texture_depth_2d"};
72
+ @vertex fn vertexMain(@builtin(vertex_index) i:u32)->@builtin(position) vec4f{
73
+ return vec4f(f32(i&1u)*4-1,f32(i>>1u)*4-1,0,1);
74
+ }
75
+ fn inside(t:vec2i)->bool{return all(t>=vec2i(0))&&all(vec2f(t)<settings.size);}
76
+ fn world(t:vec2i)->vec4f{
77
+ let d=textureLoad(depth,t,0);
78
+ if(d==0){return vec4f(0);}
79
+ let n=(vec2f(t)+0.5)/settings.size*2-1;
80
+ let h=settings.inverse*vec4f(n.x,-n.y,d,1);
81
+ return vec4f(h.xyz/h.w,1);
82
+ }
83
+ fn along(p:vec4f)->f32{return dot(p.xyz-settings.eye.xyz,settings.forward.xyz);}
84
+ fn normal(t:vec2i,p:vec4f)->vec4f{
85
+ let r=t+vec2i(1,0);
86
+ let u=t+vec2i(0,1);
87
+ if(!inside(r)||!inside(u)){return vec4f(0);}
88
+ let a=world(r);
89
+ let b=world(u);
90
+ if(a.w==0||b.w==0){return vec4f(0);}
91
+ let n=cross(a.xyz-p.xyz,b.xyz-p.xyz);
92
+ let size=length(n);
93
+ if(size==0){return vec4f(0);}
94
+ return vec4f(n/size,1);
95
+ }
96
+ fn farther(t:vec2i,z:f32)->bool{
97
+ if(!inside(t)){return false;}
98
+ let q=world(t);
99
+ return q.w==0||along(q)-z>settings.eye.w;
100
+ }
101
+ fn turned(t:vec2i,n:vec4f)->bool{
102
+ if(!inside(t)){return false;}
103
+ let q=world(t);
104
+ if(q.w==0){return false;}
105
+ let m=normal(t,q);
106
+ return m.w!=0&&dot(n.xyz,m.xyz)<settings.forward.w;
107
+ }
108
+ @fragment fn fragmentMain(@builtin(position) f:vec4f)->@location(0) vec4f{
109
+ let t=vec2i(f.xy);
110
+ let source=textureLoad(image,t,0);
111
+ let p=world(t);
112
+ if(p.w==0){return source;}
113
+ let z=along(p);
114
+ var edge=farther(t+vec2i(1,0),z)||farther(t-vec2i(1,0),z)||farther(t+vec2i(0,1),z)||farther(t-vec2i(0,1),z);
115
+ if(!edge&&settings.forward.w<1){
116
+ let n=normal(t,p);
117
+ edge=n.w!=0&&(turned(t+vec2i(1,0),n)||turned(t+vec2i(0,1),n));
118
+ }
119
+ if(edge){return settings.color;}
120
+ return source;
121
+ }`, d = 128, f = 0, p = class o {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
122
+ #e;
123
+ #t;
124
+ #n;
125
+ #r;
126
+ #i;
127
+ #a;
128
+ #o;
129
+ #s;
130
+ #c;
131
+ #l;
132
+ #u;
133
+ #d;
134
+ #f;
135
+ #p;
136
+ #m;
137
+ #h;
138
+ #g;
139
+ #_;
140
+ #v = new Float32Array(d / 4);
141
+ #y = !1;
142
+ constructor(n) {
143
+ if (!n?.camera) throw t("Render3D needs a camera", "pixel-low-res");
144
+ let { width: i, height: a } = n, o = n.order ?? 20, s = n.samples ?? 1;
145
+ if (!Number.isInteger(i) || !Number.isInteger(a) || i < 1 || a < 1) throw e("Invalid Render3D size", "pixel-low-res");
146
+ if (!Number.isFinite(o) || !Number.isInteger(s) || s < 1) throw e("Invalid Render3D value", "pixel-low-res");
147
+ this.#e = n.camera, this.width = i, this.height = a, this.#t = new r(n.target, "Render3D target", "setTarget"), this.outline = n.outline ?? {}, this.#n = n.background, this.#r = n.scene, this.#i = n.layers, this.#a = s, this.#o = o, this.name = n.name ?? `render3d-${++f}`;
148
+ }
149
+ static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
150
+ let n = new o(t);
151
+ return (t.scene ?? i(e)).use(n), n;
152
+ }
153
+ get view() {
154
+ if (this.#y) throw n("disposed", "Render3D disposed; create a new one", "disposed");
155
+ let e = this.#s?.gpu;
156
+ if (!e) throw n("state", "Render3D needs attaching before its view is read", "pixel-low-res");
157
+ if (e !== this.#c && (this.#b(), this.#c = e), !this.#f) {
158
+ let t = GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC, n = [this.width, this.height];
159
+ this.#l = e.createTexture({
160
+ label: "render3d colour",
161
+ size: n,
162
+ format: "rgba8unorm",
163
+ usage: t
164
+ }), this.#u = e.createTexture({
165
+ label: "render3d depth",
166
+ size: n,
167
+ format: e.features.has("depth32float-stencil8") ? "depth32float-stencil8" : "depth32float",
168
+ sampleCount: this.#a,
169
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | (this.#a > 1 ? 0 : GPUTextureUsage.COPY_SRC)
170
+ }), this.#f = {
171
+ id: this.name,
172
+ camera: this.#e,
173
+ pixels: !0,
174
+ viewport: [
175
+ 0,
176
+ 0,
177
+ this.width,
178
+ this.height
179
+ ],
180
+ target: {
181
+ color: this.#l,
182
+ depth: this.#u
183
+ },
184
+ clear: { color: this.#n ?? !1 },
185
+ ...this.#r ? { scene: this.#r } : {},
186
+ ...this.#i === void 0 ? {} : { layers: this.#i }
187
+ };
188
+ }
189
+ return this.#f;
190
+ }
191
+ setTarget(e) {
192
+ this.#t.set(e, this.#s?.gpu);
193
+ }
194
+ get output() {
195
+ return this.view, this.#t.get(this.#c, this.#s) || (this.#d ??= this.#c.createTexture({
196
+ label: "render3d output",
197
+ size: [this.width, this.height],
198
+ format: "rgba8unorm",
199
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC | GPUTextureUsage.COPY_DST
200
+ }), this.#d);
201
+ }
202
+ get depth() {
203
+ return this.view, this.#u;
204
+ }
205
+ get color() {
206
+ return this.view, this.#l;
207
+ }
208
+ setup(e) {
209
+ if (this.#y) throw n("disposed", "Render3D disposed; create a new one");
210
+ if (e.backend !== "webgpu") throw n("unsupported", "Render3D needs WebGPU", "webgpu");
211
+ this.#s = e, this.#t.fix(e.gpu);
212
+ let t = (e) => e.backend === "webgpu" && !!this.#l && e.view?.target?.color === this.#l;
213
+ this.#n || e.frameGraph.add({
214
+ name: `${this.name} clear`,
215
+ order: -1,
216
+ execute: (e) => {
217
+ t(e) && e.encoder.beginRenderPass({ colorAttachments: [{
218
+ view: this.#l.createView(),
219
+ loadOp: "clear",
220
+ clearValue: [
221
+ 0,
222
+ 0,
223
+ 0,
224
+ 0
225
+ ],
226
+ storeOp: "store"
227
+ }] }).end();
228
+ }
229
+ }), e.frameGraph.add({
230
+ name: this.name,
231
+ order: this.#o,
232
+ execute: (e) => {
233
+ t(e) && this.#S(e);
234
+ }
235
+ });
236
+ }
237
+ onDeviceRestored() {
238
+ this.#b();
239
+ }
240
+ dispose() {
241
+ this.#y || (this.#y = !0, (this.#r ?? this.#s)?.removePlugin(this.name), this.#s = void 0, this.#b());
242
+ }
243
+ [Symbol.dispose ?? Symbol()]() {
244
+ this.dispose();
245
+ }
246
+ #b() {
247
+ this.#l?.destroy(), this.#u?.destroy(), this.#d?.destroy(), this.#g?.destroy(), this.#l = this.#u = this.#d = this.#f = this.#c = void 0, this.#p = this.#g = this.#_ = this.#m = this.#h = void 0;
248
+ }
249
+ #x(e) {
250
+ this.#p || (this.#p = e.createShaderModule({
251
+ label: "render3d outline",
252
+ code: u(this.#a)
253
+ }), this.#g = e.createBuffer({
254
+ label: "render3d outline",
255
+ size: d,
256
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
257
+ }));
258
+ }
259
+ #S(t) {
260
+ let r = t.device, i = this.output, o = this.outline;
261
+ if (i.width !== this.width || i.height !== this.height) throw n("state", "Render3D target differs in size", "pixel-low-res");
262
+ if (!o) {
263
+ t.encoder.copyTextureToTexture({ texture: this.#l }, { texture: i }, [this.width, this.height]);
264
+ return;
265
+ }
266
+ let { color: c = "#000000", depthEdge: l = .1, crease: u = .6 } = o, d = a(c);
267
+ if (!d || !(l >= 0) || !(u >= 0)) throw e("Invalid Render3D value", "pixel-low-res");
268
+ this.#x(r);
269
+ let f = this.#v, p = this.#e, m = p.target[0] - p.position[0], h = p.target[1] - p.position[1], g = p.target[2] - p.position[2], _ = Math.hypot(m, h, g);
270
+ f.set(s(p.matrix)), f.set([
271
+ p.position[0],
272
+ p.position[1],
273
+ p.position[2],
274
+ l
275
+ ], 16), f.set([
276
+ m / _,
277
+ h / _,
278
+ g / _,
279
+ u > 0 ? Math.cos(u) : 2
280
+ ], 20), f.set([
281
+ d[0],
282
+ d[1],
283
+ d[2],
284
+ 1
285
+ ], 24), f.set([this.width, this.height], 28), r.queue.writeBuffer(this.#g, 0, f), i.format !== this.#h && (this.#h = i.format, this.#m = r.createRenderPipeline({
286
+ label: "render3d outline",
287
+ layout: "auto",
288
+ vertex: { module: this.#p },
289
+ fragment: {
290
+ module: this.#p,
291
+ targets: [{ format: i.format }]
292
+ }
293
+ }), this.#_ = r.createBindGroup({
294
+ layout: this.#m.getBindGroupLayout(0),
295
+ entries: [
296
+ {
297
+ binding: 0,
298
+ resource: { buffer: this.#g }
299
+ },
300
+ {
301
+ binding: 1,
302
+ resource: this.#l.createView()
303
+ },
304
+ {
305
+ binding: 2,
306
+ resource: this.#u.createView({ aspect: "depth-only" })
307
+ }
308
+ ]
309
+ }));
310
+ let v = t.encoder.beginRenderPass({ colorAttachments: [{
311
+ view: i.createView(),
312
+ loadOp: "clear",
313
+ clearValue: [
314
+ 0,
315
+ 0,
316
+ 0,
317
+ 0
318
+ ],
319
+ storeOp: "store"
320
+ }] });
321
+ v.setPipeline(this.#m), v.setBindGroup(0, this.#_), v.draw(3), v.end();
322
+ }
323
+ };
324
+ function m(t, n, r) {
325
+ let { angles: a, width: o, height: s, camera: c, outline: l, background: u, samples: d } = r, f = r.columns ?? a?.length;
326
+ if (!a?.length || !Number.isInteger(f) || f < 1) throw e("Invalid Render3D value", "pixel-low-res");
327
+ let m = i(t), h = p.attach(t, {
328
+ camera: c,
329
+ width: o,
330
+ height: s,
331
+ outline: l,
332
+ background: u,
333
+ samples: d
334
+ }), g = Math.ceil(a.length / f), _ = m.gpu, v = _.createTexture({
335
+ label: "sprite frames",
336
+ size: [f * o, g * s],
337
+ format: "rgba8unorm",
338
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC | GPUTextureUsage.RENDER_ATTACHMENT
339
+ }), y = n.rotation.y, b = [];
340
+ try {
341
+ a.forEach((e, r) => {
342
+ n.rotation.y = e, t.render({
343
+ delta: 0,
344
+ views: [h.view]
345
+ });
346
+ let i = r % f * o, a = Math.floor(r / f) * s, c = _.createCommandEncoder();
347
+ c.copyTextureToTexture({ texture: h.output }, {
348
+ texture: v,
349
+ origin: [i, a]
350
+ }, [o, s]), _.queue.submit([c.finish()]), b.push({
351
+ x: i,
352
+ y: a,
353
+ width: o,
354
+ height: s,
355
+ angle: e
356
+ });
357
+ });
358
+ } finally {
359
+ n.rotation.y = y, h.dispose();
360
+ }
361
+ return {
362
+ texture: v,
363
+ frames: b
364
+ };
365
+ }
366
+ //#endregion
367
+ export { p as Render3D, l as outlineMask, m as renderSpriteFrames };
@@ -0,0 +1,38 @@
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
+ /** How {@link PixelScale} fills each output pixel. */
6
+ export type ScaleFilter = 'nearest' | 'sharp-bilinear' | 'edge' | 'xbr3' | 'xbr4';
7
+ /** Settings of {@link PixelScale}. */
8
+ export interface PixelScaleOptions extends ScreenPassOptions {
9
+ /** Read each frame. Default `'sharp-bilinear'`. */
10
+ filter?: ScaleFilter;
11
+ }
12
+ /**
13
+ * Draws a low-res picture to the canvas (or a target) at any size with a pixel-art filter, for screens whose size is not
14
+ * a whole multiple of the art. One frame-graph pass a view. WebGPU only.
15
+ * @throws TypeError / RangeError on invalid options or, each frame, an invalid `filter` or `fit`.
16
+ * @throws MargoError `'unsupported'` on a WebGL engine, `'disposed'` after `dispose()`, `'state'` when the source and the
17
+ * target are the same texture.
18
+ */
19
+ export declare class PixelScale extends ScreenPass {
20
+ /** Read each frame. */
21
+ filter: ScaleFilter;
22
+ constructor(options: PixelScaleOptions);
23
+ /** Creates a pass and attaches it to `instance`. */
24
+ static attach(instance: EngineKernel, options: PixelScaleOptions): PixelScale;
25
+ protected fill(values: ArrayBuffer, rect: readonly number[], source: GPUTexture): void;
26
+ }
27
+ /** An RGBA picture, 4 bytes a pixel, rows from the top. */
28
+ export interface PixelImage {
29
+ readonly width: number;
30
+ readonly height: number;
31
+ readonly data: Uint8Array;
32
+ }
33
+ /**
34
+ * The picture doubled by the Scale2x rule, on the CPU (the rule `'edge'` draws with): each texel becomes four, and a
35
+ * corner takes the colour of its two neighbours toward it when they match and the other two differ. Texels past the
36
+ * edge repeat the edge. For exporting sprites at twice the size.
37
+ */
38
+ export declare function scale2x(image: PixelImage): PixelImage;
package/dist/scale.js ADDED
@@ -0,0 +1,123 @@
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 { 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/scale.ts
8
+ var a = `
9
+ struct S{rect:vec4i,mode:u32,grid:i32}
10
+ @group(0) @binding(0) var<uniform> s:S;
11
+ @group(0) @binding(1) var image:texture_2d<f32>;
12
+ ${t}
13
+ fn at(t:vec2i)->vec4f{
14
+ return textureLoad(image,clamp(t,vec2i(0),vec2i(textureDimensions(image))-1),0);
15
+ }
16
+
17
+ fn difference(a:vec4f,b:vec4f)->f32{
18
+ let d=abs(vec4i(round(a*255))-vec4i(round(b*255)));
19
+ return f32(3*d.r+6*d.g+d.b+10*d.a);
20
+ }
21
+ fn contour(j:vec2i)->vec4f{
22
+ let bounded=clamp(j,vec2i(0),vec2i(textureDimensions(image))*s.grid-1);
23
+ let t=bounded/s.grid;
24
+ let q=(vec2f(bounded-t*s.grid)+0.5)/f32(s.grid);
25
+ let e=at(t);
26
+ var sum=vec4f(0);
27
+ var coverage=0.0;
28
+
29
+ for(var corner=0;corner<4;corner++){
30
+ let sign=vec2i(select(-1,1,(corner&1)==1),select(-1,1,corner>=2));
31
+ let dx=vec2i(sign.x,0); let dy=vec2i(0,sign.y);
32
+ let f=at(t+dx); let h=at(t+dy); let i=at(t+dx+dy);
33
+ if(all(e==f)||all(e==h)){continue;}
34
+ if(all(f==h)&&all(f==at(t-dx))&&all(f==at(t-dy))){continue;}
35
+ let along=4*difference(f,h)+difference(e,at(t+dx-dy))+difference(e,at(t-dx+dy))
36
+ +difference(i,at(t+2*dx))+difference(i,at(t+2*dy));
37
+ let across=4*difference(e,i)+difference(h,at(t-dx))+difference(f,at(t-dy))
38
+ +difference(h,at(t+dx+2*dy))+difference(f,at(t+2*dx+dy));
39
+ if(along>=across){continue;}
40
+ let candidate=select((f+h)*0.5,select(f,h,difference(e,h)<difference(e,f)),difference(e,h)!=difference(e,f));
41
+ let u=select(1-q.x,q.x,sign.x>0); let v=select(1-q.y,q.y,sign.y>0);
42
+ let horizontal=difference(h,at(t+2*dx+dy));
43
+ let vertical=difference(f,at(t+dx+2*dy));
44
+ var edge=u+v-1.5;
45
+ if(2*horizontal<vertical){edge=0.5*u+v-1.25;}
46
+ if(2*vertical<horizontal){edge=u+0.5*v-1.25;}
47
+ let weight=clamp(edge*f32(s.grid)+0.5,0,1);
48
+ sum+=candidate*weight; coverage+=weight;
49
+ }
50
+ return (e*max(0,1-coverage)+sum)/max(1,coverage);
51
+ }
52
+ fn cell(j:vec2i)->vec4f{
53
+ if(s.grid==1){return at(j);}
54
+ if(s.grid>=3){return contour(j);}
55
+ let t=j/2;
56
+ let q=j-t*2;
57
+ let e=at(t);
58
+ let b=at(t+vec2i(0,-1));
59
+ let h=at(t+vec2i(0,1));
60
+ let d=at(t+vec2i(-1,0));
61
+ let f=at(t+vec2i(1,0));
62
+ let side=select(d,f,q.x==1);
63
+ let vert=select(b,h,q.y==1);
64
+ let oside=select(f,d,q.x==1);
65
+ let overt=select(h,b,q.y==1);
66
+ if(all(side==vert)&&any(vert!=oside)&&any(side!=overt)){return side;}
67
+ return e;
68
+ }
69
+ @fragment fn fs(@builtin(position) p:vec4f)->@location(0) vec4f{
70
+ let n=vec2i(textureDimensions(image))*s.grid;
71
+ let o=vec2i(floor(p.xy))-s.rect.xy;
72
+ let w=s.rect.zw;
73
+ if(s.mode==0u){return cell(clamp((o*2+1)*n/(w*2),vec2i(0),n-1));}
74
+ let right=vec2f((o+1)*n)/vec2f(w);
75
+ let last=vec2i(floor(right));
76
+ let k=clamp((right-vec2f(last))*vec2f(w)/vec2f(n),vec2f(0),vec2f(1));
77
+ let a=last-1;
78
+ let top=mix(cell(vec2i(a.x,a.y)),cell(vec2i(last.x,a.y)),k.x);
79
+ let bottom=mix(cell(vec2i(a.x,last.y)),cell(last),k.x);
80
+ return mix(top,bottom,k.y);
81
+ }`, o = 0, s = class e extends r {
82
+ constructor(e) {
83
+ super(e, "PixelScale", a, 32, `pixel-scale-${++o}`), this.filter = e.filter ?? "sharp-bilinear", c(this.filter);
84
+ }
85
+ static attach(t, n) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
86
+ let r = new e(n);
87
+ return i(t).use(r), r;
88
+ }
89
+ fill(e, t, n) {
90
+ c(this.filter);
91
+ let r = new Int32Array(e);
92
+ r.set(t, 0), r[4] = this.filter === "nearest" ? 0 : 1;
93
+ let i = this.filter === "xbr4" ? 4 : this.filter === "xbr3" ? 3 : this.filter === "edge" ? 2 : 1;
94
+ r[5] = Math.min(t[2] / n.width, t[3] / n.height) >= i ? i : 1;
95
+ }
96
+ }, c = (t) => {
97
+ if (![
98
+ "nearest",
99
+ "sharp-bilinear",
100
+ "edge",
101
+ "xbr3",
102
+ "xbr4"
103
+ ].includes(t)) throw e(`PixelScale filter must be nearest, sharp-bilinear, edge, xbr3 or xbr4, got ${t}`, "pixel-options");
104
+ };
105
+ function l(t) {
106
+ let { width: n, height: r, data: i } = t;
107
+ if (!(Number.isInteger(n) && Number.isInteger(r) && n > 0 && r > 0) || i?.length !== n * r * 4) throw e("scale2x needs a whole width and height and width * height * 4 bytes", "pixel-options");
108
+ let a = new Uint8Array(i.length * 4), o = (e, t) => (Math.min(r - 1, Math.max(0, t)) * n + Math.min(n - 1, Math.max(0, e))) * 4, s = (e, t) => i[e] === i[t] && i[e + 1] === i[t + 1] && i[e + 2] === i[t + 2] && i[e + 3] === i[t + 3];
109
+ for (let e = 0; e < r; e++) for (let t = 0; t < n; t++) {
110
+ let r = o(t, e), c = o(t, e - 1), l = o(t, e + 1), u = o(t - 1, e), d = o(t + 1, e);
111
+ for (let o = 0; o < 4; o++) {
112
+ let f = o & 1, p = o >> 1, m = f ? d : u, h = p ? l : c, g = f ? u : d, _ = p ? c : l, v = s(m, h) && !s(h, g) && !s(m, _) ? m : r, y = ((e * 2 + p) * n * 2 + t * 2 + f) * 4;
113
+ a.set(i.subarray(v, v + 4), y);
114
+ }
115
+ }
116
+ return {
117
+ width: n * 2,
118
+ height: r * 2,
119
+ data: a
120
+ };
121
+ }
122
+ //#endregion
123
+ export { s as PixelScale, n as fitRect, l as scale2x };
@@ -0,0 +1,39 @@
1
+ import { t as e } from "./errors-CDgOgzCd.js";
2
+ //#region ../shared/borrowed.js
3
+ function t(e, t, n) {
4
+ return e.value = t, e.device = typeof t == "function" ? void 0 : n, e.made = void 0, e.told = !1, e;
5
+ }
6
+ function n(e, t) {
7
+ typeof e.value != "function" && (e.device ??= t);
8
+ }
9
+ var r = (e, n, r, i) => t({
10
+ name: n,
11
+ setter: r
12
+ }, e, i);
13
+ function i(t, n, r) {
14
+ let { value: i } = t;
15
+ if (typeof i == "function") return t.device !== n && (t.made = i(t.device = n)), t.made;
16
+ if (i != null) {
17
+ if (t.device ??= n, t.device === n) return i;
18
+ t.told || (t.told = !0, r?.onError(e("state", `${t.name} lost with the device; pass a function or call ${t.setter}`, "borrowed-lost")));
19
+ }
20
+ }
21
+ //#endregion
22
+ //#region ../shared/texture-option.js
23
+ var a = class {
24
+ constructor(e, t, n) {
25
+ this.value = e, this.slot = r(typeof e == "function" ? void 0 : e, t, n);
26
+ }
27
+ set(e, n) {
28
+ this.value = e, t(this.slot, typeof e == "function" ? void 0 : e, n);
29
+ }
30
+ fix(e) {
31
+ e && n(this.slot, e);
32
+ }
33
+ get(e, t) {
34
+ let n = this.value;
35
+ return typeof n == "function" ? n() : i(this.slot, e, t);
36
+ }
37
+ };
38
+ //#endregion
39
+ export { a as t };
@@ -0,0 +1,39 @@
1
+ //#region ../shared/view-rect.js
2
+ function e(e) {
3
+ return e?.view?.id ?? 0;
4
+ }
5
+ function t(e, t) {
6
+ if (!Number.isInteger(e) || e < 0 || e > 4294967295) throw RangeError(`${t} ${e} is not a 32-bit layer mask; pass an integer from 0 to 0xffffffff`);
7
+ return e >>> 0;
8
+ }
9
+ var n = class {
10
+ constructor(e, t = () => {}) {
11
+ this.create = e, this.release = t, this.states = /* @__PURE__ */ new Map(), this.frame = 0;
12
+ }
13
+ get(t) {
14
+ let n = e(t), r = this.states.get(n);
15
+ return r || this.states.set(n, r = {
16
+ state: this.create(t),
17
+ frame: 0
18
+ }), r.frame = this.frame, r.state;
19
+ }
20
+ find(e) {
21
+ return this.states.get(e)?.state;
22
+ }
23
+ *values() {
24
+ for (let e of this.states.values()) yield e.state;
25
+ }
26
+ *drawn() {
27
+ for (let e of this.states.values()) e.frame === this.frame && (yield e.state);
28
+ }
29
+ prune() {
30
+ for (let [e, t] of this.states) t.frame < this.frame && (this.states.delete(e), this.release(t.state));
31
+ this.frame++;
32
+ }
33
+ clear() {
34
+ for (let e of this.states.values()) this.release(e.state);
35
+ this.states.clear();
36
+ }
37
+ };
38
+ //#endregion
39
+ export { t as n, n as t };
package/package.json CHANGED
@@ -1,6 +1,74 @@
1
- {
2
- "name": "@margoengine/pixel",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@margoengine/pixel",
3
+ "version": "0.1.0",
4
+ "description": "Pixel art rendering: a pixel camera with integer zoom and parallax, and a layer compositor that scales low-res layers by whole numbers and turns them with every output pixel one source pixel.",
5
+ "keywords": [
6
+ "margoengine",
7
+ "webgpu",
8
+ "2d",
9
+ "pixel-art",
10
+ "parallax"
11
+ ],
12
+ "homepage": "https://margoengine.com",
13
+ "bugs": {
14
+ "url": "https://github.com/scottcampbelldata/margoengine/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/scottcampbelldata/margoengine.git",
19
+ "directory": "packages/pixel"
20
+ },
21
+ "license": "Apache-2.0",
22
+ "author": "Scott Campbell",
23
+ "type": "module",
24
+ "sideEffects": false,
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
29
+ },
30
+ "./palette": {
31
+ "types": "./dist/palette.d.ts",
32
+ "import": "./dist/palette.js"
33
+ },
34
+ "./render3d": {
35
+ "types": "./dist/render3d.d.ts",
36
+ "import": "./dist/render3d.js"
37
+ },
38
+ "./scale": {
39
+ "types": "./dist/scale.d.ts",
40
+ "import": "./dist/scale.js"
41
+ },
42
+ "./follow": {
43
+ "types": "./dist/follow.d.ts",
44
+ "import": "./dist/follow.js"
45
+ },
46
+ "./crt": {
47
+ "types": "./dist/crt.d.ts",
48
+ "import": "./dist/crt.js"
49
+ }
50
+ },
51
+ "files": [
52
+ "dist",
53
+ "LICENSE",
54
+ "NOTICE"
55
+ ],
56
+ "publishConfig": {
57
+ "access": "public"
58
+ },
59
+ "peerDependencies": {
60
+ "@margoengine/core": "^0.1.0"
61
+ },
62
+ "dependencies": {
63
+ "@margoengine/math": "^0.1.0"
64
+ },
65
+ "devDependencies": {
66
+ "@margoengine/cameras": "^0.1.0"
67
+ },
68
+ "scripts": {
69
+ "build": "node ../../scripts/build-optional.js pixel",
70
+ "test": "npm run build && node --test tests/*.test.js",
71
+ "test:gpu": "cd ../.. && node packages/pixel/tests/browser-check.mjs && node packages/pixel/tests/palette-check.mjs && node packages/pixel/tests/render3d-check.mjs && node packages/pixel/tests/scale-check.mjs && node packages/pixel/tests/crt-check.mjs && node packages/pixel/tests/follow-check.mjs && node packages/pixel/tests/pixel-restore-check.mjs",
72
+ "prepack": "npm run build"
73
+ }
74
+ }