@pixagram/renderart 0.4.5 → 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.
Files changed (47) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +171 -67
  3. package/dist/crt-gpu.d.ts +30 -0
  4. package/dist/crt-gpu.d.ts.map +1 -0
  5. package/dist/crt-gpu.js +282 -0
  6. package/dist/crt-gpu.js.map +1 -0
  7. package/dist/hex-gpu.d.ts +35 -0
  8. package/dist/hex-gpu.d.ts.map +1 -0
  9. package/dist/hex-gpu.js +382 -0
  10. package/dist/hex-gpu.js.map +1 -0
  11. package/dist/index.d.ts +21 -300
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +36 -963
  14. package/dist/index.js.map +1 -1
  15. package/dist/types.d.ts +84 -0
  16. package/dist/types.d.ts.map +1 -0
  17. package/dist/types.js +7 -0
  18. package/dist/types.js.map +1 -0
  19. package/dist/wasm-wrapper.d.ts +71 -0
  20. package/dist/wasm-wrapper.d.ts.map +1 -0
  21. package/dist/wasm-wrapper.js +76 -0
  22. package/dist/wasm-wrapper.js.map +1 -0
  23. package/dist/xbrz-gpu.d.ts +34 -0
  24. package/dist/xbrz-gpu.d.ts.map +1 -0
  25. package/dist/xbrz-gpu.js +640 -0
  26. package/dist/xbrz-gpu.js.map +1 -0
  27. package/package.json +48 -35
  28. package/src/crt-gpu.ts +313 -0
  29. package/src/hex-gpu.ts +426 -0
  30. package/src/index.ts +52 -0
  31. package/src/types.ts +90 -0
  32. package/src/wasm/crt.rs +181 -0
  33. package/src/wasm/hex.rs +324 -0
  34. package/src/wasm/lib.rs +285 -0
  35. package/src/wasm/xbrz.rs +262 -0
  36. package/src/wasm-wrapper.ts +195 -0
  37. package/src/xbrz-gpu.ts +671 -0
  38. package/dist/index.d.mts +0 -305
  39. package/dist/index.mjs +0 -948
  40. package/dist/index.mjs.map +0 -1
  41. package/pkg/LICENSE +0 -21
  42. package/pkg/README.md +0 -117
  43. package/pkg/renderart_wasm.d.ts +0 -52
  44. package/pkg/renderart_wasm.js +0 -5
  45. package/pkg/renderart_wasm_bg.js +0 -283
  46. package/pkg/renderart_wasm_bg.wasm +0 -0
  47. package/pkg/renderart_wasm_bg.wasm.d.ts +0 -24
package/dist/index.js CHANGED
@@ -1,964 +1,37 @@
1
- 'use strict';
2
-
3
- // src/crt-gpu.ts
4
- var VERTEX_SHADER = `#version 300 es
5
- layout(location = 0) in vec2 position;
6
- out vec2 vUv;
7
-
8
- void main() {
9
- vUv = position * 0.5 + 0.5;
10
- gl_Position = vec4(position, 0.0, 1.0);
11
- }`;
12
- var FRAGMENT_SHADER = `#version 300 es
13
- precision highp float;
14
-
15
- uniform sampler2D uTex;
16
- uniform vec2 uRes;
17
- uniform vec2 uWarp;
18
- uniform float uScanHardness;
19
- uniform float uScanOpacity;
20
- uniform float uMaskOpacity;
21
- uniform int uEnableWarp;
22
- uniform int uEnableScanlines;
23
- uniform int uEnableMask;
24
-
25
- in vec2 vUv;
26
- out vec4 outColor;
27
-
28
- // Gamma 2.0 approximation
29
- vec3 toLinear(vec3 c) { return c * c; }
30
- vec3 toSrgb(vec3 c) { return sqrt(c); }
31
-
32
- vec2 warp(vec2 uv) {
33
- if (uEnableWarp == 0) return uv;
34
- vec2 dc = abs(0.5 - uv);
35
- vec2 dc2 = dc * dc;
36
- uv.x -= 0.5; uv.x *= 1.0 + (dc2.y * (0.3 * uWarp.x)); uv.x += 0.5;
37
- uv.y -= 0.5; uv.y *= 1.0 + (dc2.x * (0.4 * uWarp.y)); uv.y += 0.5;
38
- return uv;
39
- }
40
-
41
- float scanline(float y, float sourceHeight) {
42
- if (uEnableScanlines == 0) return 1.0;
43
- float v = fract(y * sourceHeight);
44
- float d = abs(v - 0.5);
45
- float line = exp(d * d * uScanHardness);
46
- return mix(1.0, line, uScanOpacity);
47
- }
48
-
49
- vec3 mask(vec2 pos) {
50
- if (uEnableMask == 0) return vec3(1.0);
51
- float x = fract(pos.x / 6.0);
52
- vec3 m = vec3(1.0);
53
- float step1 = 0.333;
54
- float step2 = 0.666;
55
-
56
- m.r = step(0.0, x) - step(step1, x);
57
- m.g = step(step1, x) - step(step2, x);
58
- m.b = step(step2, x) - step(1.0, x);
59
-
60
- return mix(vec3(1.0), m, uMaskOpacity);
61
- }
62
-
63
- void main() {
64
- // 1. Geometry
65
- vec2 uv = warp(vUv);
66
-
67
- // 2. Bounds Check
68
- if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {
69
- outColor = vec4(0.0);
70
- return;
71
- }
72
-
73
- // 3. Texture Sample
74
- vec4 texSample = texture(uTex, uv);
75
-
76
- if (texSample.a == 0.0) {
77
- outColor = vec4(0.0);
78
- return;
79
- }
80
-
81
- vec3 linearColor = toLinear(texSample.rgb);
82
-
83
- // 4. CRT Effects
84
- ivec2 texSize = textureSize(uTex, 0);
85
-
86
- // Smart Bloom
87
- float luma = dot(linearColor, vec3(0.299, 0.587, 0.114));
88
- float bloom = luma * 0.7;
89
-
90
- // Calculate patterns
91
- float scan = scanline(uv.y, float(texSize.y));
92
- vec3 m = mask(gl_FragCoord.xy);
93
-
94
- // Apply effects
95
- vec3 effects = m * scan;
96
- vec3 finalRGB = linearColor * mix(effects, vec3(1.0), bloom);
97
-
98
- // 5. Output
99
- outColor = vec4(toSrgb(finalRGB), texSample.a);
100
- }`;
101
- var CrtGpuRenderer = class _CrtGpuRenderer {
102
- constructor() {
103
- this.gl = null;
104
- this.canvas = null;
105
- this.program = null;
106
- this.texture = null;
107
- this.uniforms = {};
108
- this.initialized = false;
109
- this.currentCanvasSize = { width: 0, height: 0 };
110
- this.currentTexSize = { width: 0, height: 0 };
111
- }
112
- /** Create a new CRT GPU renderer */
113
- static create() {
114
- const renderer = new _CrtGpuRenderer();
115
- renderer.init();
116
- return renderer;
117
- }
118
- init() {
119
- if (typeof OffscreenCanvas === "undefined") {
120
- throw new Error("OffscreenCanvas not supported");
121
- }
122
- this.canvas = new OffscreenCanvas(1, 1);
123
- this.gl = this.canvas.getContext("webgl2", {
124
- alpha: true,
125
- premultipliedAlpha: false,
126
- desynchronized: true,
127
- powerPreference: "high-performance",
128
- antialias: false
129
- });
130
- if (!this.gl) {
131
- throw new Error("WebGL2 not supported");
132
- }
133
- const gl = this.gl;
134
- const vs = this.createShader(gl.VERTEX_SHADER, VERTEX_SHADER);
135
- const fs = this.createShader(gl.FRAGMENT_SHADER, FRAGMENT_SHADER);
136
- this.program = gl.createProgram();
137
- gl.attachShader(this.program, vs);
138
- gl.attachShader(this.program, fs);
139
- gl.linkProgram(this.program);
140
- if (!gl.getProgramParameter(this.program, gl.LINK_STATUS)) {
141
- throw new Error("Shader program link failed: " + gl.getProgramInfoLog(this.program));
142
- }
143
- gl.useProgram(this.program);
144
- this.uniforms = {
145
- uTex: gl.getUniformLocation(this.program, "uTex"),
146
- uRes: gl.getUniformLocation(this.program, "uRes"),
147
- uWarp: gl.getUniformLocation(this.program, "uWarp"),
148
- uScanHardness: gl.getUniformLocation(this.program, "uScanHardness"),
149
- uScanOpacity: gl.getUniformLocation(this.program, "uScanOpacity"),
150
- uMaskOpacity: gl.getUniformLocation(this.program, "uMaskOpacity"),
151
- uEnableWarp: gl.getUniformLocation(this.program, "uEnableWarp"),
152
- uEnableScanlines: gl.getUniformLocation(this.program, "uEnableScanlines"),
153
- uEnableMask: gl.getUniformLocation(this.program, "uEnableMask")
154
- };
155
- gl.uniform1i(this.uniforms.uTex, 0);
156
- const buf = gl.createBuffer();
157
- gl.bindBuffer(gl.ARRAY_BUFFER, buf);
158
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
159
- gl.enableVertexAttribArray(0);
160
- gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
161
- this.texture = gl.createTexture();
162
- gl.bindTexture(gl.TEXTURE_2D, this.texture);
163
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
164
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
165
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
166
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
167
- gl.clearColor(0, 0, 0, 0);
168
- this.initialized = true;
169
- }
170
- createShader(type, source) {
171
- const gl = this.gl;
172
- const shader = gl.createShader(type);
173
- gl.shaderSource(shader, source);
174
- gl.compileShader(shader);
175
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
176
- const info = gl.getShaderInfoLog(shader);
177
- gl.deleteShader(shader);
178
- throw new Error("Shader compile failed: " + info);
179
- }
180
- return shader;
181
- }
182
- /** Check if renderer is ready */
183
- isReady() {
184
- return this.initialized;
185
- }
186
- /** Render CRT effect */
187
- render(input, options = {}) {
188
- if (!this.initialized || !this.gl || !this.canvas) {
189
- throw new Error("Renderer not initialized");
190
- }
191
- const gl = this.gl;
192
- const data = input instanceof ImageData ? input.data : input.data;
193
- const width = input.width;
194
- const height = input.height;
195
- const scale = Math.min(32, Math.max(2, options.scale ?? 3));
196
- const outWidth = width * scale;
197
- const outHeight = height * scale;
198
- if (this.currentCanvasSize.width !== outWidth || this.currentCanvasSize.height !== outHeight) {
199
- this.canvas.width = outWidth;
200
- this.canvas.height = outHeight;
201
- this.currentCanvasSize = { width: outWidth, height: outHeight };
202
- gl.viewport(0, 0, outWidth, outHeight);
203
- }
204
- gl.uniform2f(this.uniforms.uRes, outWidth, outHeight);
205
- gl.uniform2f(this.uniforms.uWarp, options.warpX ?? 0.015, options.warpY ?? 0.02);
206
- gl.uniform1f(this.uniforms.uScanHardness, options.scanHardness ?? -4);
207
- gl.uniform1f(this.uniforms.uScanOpacity, options.scanOpacity ?? 0.5);
208
- gl.uniform1f(this.uniforms.uMaskOpacity, options.maskOpacity ?? 0.3);
209
- gl.uniform1i(this.uniforms.uEnableWarp, options.enableWarp !== false ? 1 : 0);
210
- gl.uniform1i(this.uniforms.uEnableScanlines, options.enableScanlines !== false ? 1 : 0);
211
- gl.uniform1i(this.uniforms.uEnableMask, options.enableMask !== false ? 1 : 0);
212
- gl.bindTexture(gl.TEXTURE_2D, this.texture);
213
- if (this.currentTexSize.width !== width || this.currentTexSize.height !== height) {
214
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
215
- this.currentTexSize = { width, height };
216
- } else {
217
- gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data);
218
- }
219
- gl.clear(gl.COLOR_BUFFER_BIT);
220
- gl.drawArrays(gl.TRIANGLES, 0, 3);
221
- const pixels = new Uint8ClampedArray(outWidth * outHeight * 4);
222
- gl.readPixels(0, 0, outWidth, outHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
223
- return {
224
- data: pixels,
225
- width: outWidth,
226
- height: outHeight
227
- };
228
- }
229
- /** Dispose resources */
230
- dispose() {
231
- if (this.gl) {
232
- if (this.texture) this.gl.deleteTexture(this.texture);
233
- if (this.program) this.gl.deleteProgram(this.program);
234
- this.gl = null;
235
- }
236
- this.canvas = null;
237
- this.initialized = false;
238
- }
239
- };
240
- var CRT_PRESETS = {
241
- default: {},
242
- authentic: {
243
- warpX: 0.02,
244
- warpY: 0.025,
245
- scanHardness: -6,
246
- scanOpacity: 0.6,
247
- maskOpacity: 0.4
248
- },
249
- subtle: {
250
- warpX: 8e-3,
251
- warpY: 0.01,
252
- scanHardness: -3,
253
- scanOpacity: 0.3,
254
- maskOpacity: 0.15
255
- },
256
- flat: {
257
- warpX: 0,
258
- warpY: 0,
259
- enableWarp: false,
260
- scanHardness: -4,
261
- scanOpacity: 0.5,
262
- maskOpacity: 0.3
263
- }
264
- };
265
-
266
- // src/hex-gpu.ts
267
- var VERTEX_SHADER2 = `#version 300 es
268
- layout(location = 0) in vec2 position;
269
- out vec2 vUv;
270
-
271
- void main() {
272
- vUv = position * 0.5 + 0.5;
273
- gl_Position = vec4(position, 0.0, 1.0);
274
- }`;
275
- var FRAGMENT_SHADER2 = `#version 300 es
276
- precision highp float;
277
-
278
- uniform sampler2D uTex;
279
- uniform vec2 uOutputRes;
280
- uniform vec2 uInputRes;
281
- uniform float uScale;
282
- uniform int uOrientation; // 0 = flat-top, 1 = pointy-top
283
- uniform int uDrawBorders;
284
- uniform vec4 uBorderColor;
285
- uniform float uBorderThickness;
286
- uniform vec4 uBackgroundColor;
287
-
288
- in vec2 vUv;
289
- out vec4 outColor;
290
-
291
- const float SQRT3 = 1.732050808;
292
- const float INV_SQRT3 = 0.577350269;
293
-
294
- // Convert output pixel to hex coordinate (flat-top)
295
- vec2 pixelToHexFlat(vec2 pos, float scale) {
296
- float hSpacing = scale * 1.5;
297
- float vSpacing = scale * SQRT3;
298
-
299
- float col = pos.x / hSpacing;
300
- int colInt = int(floor(col));
301
-
302
- // Check if odd column (offset)
303
- bool isOffset = (colInt & 1) == 1;
304
- float yOffset = isOffset ? vSpacing * 0.5 : 0.0;
305
-
306
- float row = (pos.y - yOffset) / vSpacing;
307
- int rowInt = int(floor(row));
308
-
309
- // Refine with corner detection
310
- float cellX = pos.x - float(colInt) * hSpacing;
311
- float cellY = pos.y - float(rowInt) * vSpacing - yOffset;
312
-
313
- float quarterW = scale * 0.5;
314
- float halfH = vSpacing * 0.5;
315
-
316
- if (cellX < quarterW) {
317
- float distFromCenter = abs(cellY - halfH);
318
- float edgeX = distFromCenter * INV_SQRT3;
319
-
320
- if (cellX < quarterW - edgeX) {
321
- if (cellY < halfH) {
322
- rowInt -= 1;
323
- }
324
- colInt -= 1;
325
- }
326
- }
327
-
328
- return vec2(float(colInt), float(rowInt));
329
- }
330
-
331
- // Convert output pixel to hex coordinate (pointy-top)
332
- vec2 pixelToHexPointy(vec2 pos, float scale) {
333
- float hSpacing = scale * SQRT3;
334
- float vSpacing = scale * 1.5;
335
-
336
- float row = pos.y / vSpacing;
337
- int rowInt = int(floor(row));
338
-
339
- // Check if odd row (offset)
340
- bool isOffset = (rowInt & 1) == 1;
341
- float xOffset = isOffset ? hSpacing * 0.5 : 0.0;
342
-
343
- float col = (pos.x - xOffset) / hSpacing;
344
- int colInt = int(floor(col));
345
-
346
- // Refine with corner detection
347
- float cellX = pos.x - float(colInt) * hSpacing - xOffset;
348
- float cellY = pos.y - float(rowInt) * vSpacing;
349
-
350
- float halfW = hSpacing * 0.5;
351
- float quarterH = scale * 0.5;
352
-
353
- if (cellY < quarterH) {
354
- float distFromCenter = abs(cellX - halfW);
355
- float edgeY = distFromCenter * SQRT3;
356
-
357
- if (cellY < edgeY) {
358
- if (cellX < halfW && isOffset) {
359
- colInt -= 1;
360
- }
361
- rowInt -= 1;
362
- }
363
- }
364
-
365
- return vec2(float(colInt), float(rowInt));
366
- }
367
-
368
- // Check if pixel is on hex border
369
- bool isBorderPixel(vec2 pos, float scale, int orientation, float thickness) {
370
- for (float dy = -thickness; dy <= thickness; dy += 1.0) {
371
- for (float dx = -thickness; dx <= thickness; dx += 1.0) {
372
- if (dx == 0.0 && dy == 0.0) continue;
373
-
374
- vec2 h1 = orientation == 0
375
- ? pixelToHexFlat(pos, scale)
376
- : pixelToHexPointy(pos, scale);
377
- vec2 h2 = orientation == 0
378
- ? pixelToHexFlat(pos + vec2(dx, dy), scale)
379
- : pixelToHexPointy(pos + vec2(dx, dy), scale);
380
-
381
- if (h1 != h2) return true;
382
- }
383
- }
384
- return false;
385
- }
386
-
387
- void main() {
388
- vec2 pixelPos = vUv * uOutputRes;
389
-
390
- // Get hex coordinate
391
- vec2 hexCoord = uOrientation == 0
392
- ? pixelToHexFlat(pixelPos, uScale)
393
- : pixelToHexPointy(pixelPos, uScale);
394
-
395
- // Check bounds
396
- if (hexCoord.x < 0.0 || hexCoord.y < 0.0 ||
397
- hexCoord.x >= uInputRes.x || hexCoord.y >= uInputRes.y) {
398
- outColor = uBackgroundColor;
399
- return;
400
- }
401
-
402
- // Check for border
403
- if (uDrawBorders == 1 && uBorderThickness > 0.0) {
404
- if (isBorderPixel(pixelPos, uScale, uOrientation, uBorderThickness)) {
405
- outColor = uBorderColor;
406
- return;
407
- }
408
- }
409
-
410
- // Sample source pixel (add 0.5 for pixel center)
411
- vec2 texCoord = (hexCoord + 0.5) / uInputRes;
412
- outColor = texture(uTex, texCoord);
413
- }`;
414
- function parseColor(color, defaultColor) {
415
- if (color === void 0) return defaultColor;
416
- if (typeof color === "number") {
417
- return [
418
- (color >> 24 & 255) / 255,
419
- (color >> 16 & 255) / 255,
420
- (color >> 8 & 255) / 255,
421
- (color & 255) / 255
422
- ];
423
- }
424
- if (color === "transparent") return [0, 0, 0, 0];
425
- if (color.startsWith("#")) {
426
- const hex = color.slice(1);
427
- if (hex.length === 6) {
428
- return [
429
- parseInt(hex.slice(0, 2), 16) / 255,
430
- parseInt(hex.slice(2, 4), 16) / 255,
431
- parseInt(hex.slice(4, 6), 16) / 255,
432
- 1
433
- ];
434
- }
435
- if (hex.length === 8) {
436
- return [
437
- parseInt(hex.slice(0, 2), 16) / 255,
438
- parseInt(hex.slice(2, 4), 16) / 255,
439
- parseInt(hex.slice(4, 6), 16) / 255,
440
- parseInt(hex.slice(6, 8), 16) / 255
441
- ];
442
- }
443
- }
444
- return defaultColor;
445
- }
446
- function hexGetDimensions(srcWidth, srcHeight, scale, orientation = "flat-top") {
447
- const SQRT3 = 1.732050808;
448
- if (orientation === "flat-top") {
449
- const hSpacing = scale * 1.5;
450
- const vSpacing = scale * SQRT3;
451
- const cellWidth = scale * 2;
452
- const cellHeight = scale * SQRT3;
453
- return {
454
- width: Math.ceil(srcWidth * hSpacing + cellWidth),
455
- height: Math.ceil(srcHeight * vSpacing + cellHeight)
456
- };
457
- } else {
458
- const hSpacing = scale * SQRT3;
459
- const vSpacing = scale * 1.5;
460
- const cellWidth = scale * SQRT3;
461
- const cellHeight = scale * 2;
462
- return {
463
- width: Math.ceil(srcWidth * hSpacing + cellWidth),
464
- height: Math.ceil(srcHeight * vSpacing + cellHeight)
465
- };
466
- }
467
- }
468
- var HexGpuRenderer = class _HexGpuRenderer {
469
- constructor() {
470
- this.gl = null;
471
- this.canvas = null;
472
- this.program = null;
473
- this.texture = null;
474
- this.uniforms = {};
475
- this.initialized = false;
476
- this.currentCanvasSize = { width: 0, height: 0 };
477
- this.currentTexSize = { width: 0, height: 0 };
478
- }
479
- /** Create a new HEX GPU renderer */
480
- static create() {
481
- const renderer = new _HexGpuRenderer();
482
- renderer.init();
483
- return renderer;
484
- }
485
- init() {
486
- if (typeof OffscreenCanvas === "undefined") {
487
- throw new Error("OffscreenCanvas not supported");
488
- }
489
- this.canvas = new OffscreenCanvas(1, 1);
490
- this.gl = this.canvas.getContext("webgl2", {
491
- alpha: true,
492
- premultipliedAlpha: false,
493
- desynchronized: true,
494
- powerPreference: "high-performance",
495
- antialias: false
496
- });
497
- if (!this.gl) {
498
- throw new Error("WebGL2 not supported");
499
- }
500
- const gl = this.gl;
501
- const vs = this.createShader(gl.VERTEX_SHADER, VERTEX_SHADER2);
502
- const fs = this.createShader(gl.FRAGMENT_SHADER, FRAGMENT_SHADER2);
503
- this.program = gl.createProgram();
504
- gl.attachShader(this.program, vs);
505
- gl.attachShader(this.program, fs);
506
- gl.linkProgram(this.program);
507
- if (!gl.getProgramParameter(this.program, gl.LINK_STATUS)) {
508
- throw new Error("Shader program link failed: " + gl.getProgramInfoLog(this.program));
509
- }
510
- gl.useProgram(this.program);
511
- this.uniforms = {
512
- uTex: gl.getUniformLocation(this.program, "uTex"),
513
- uOutputRes: gl.getUniformLocation(this.program, "uOutputRes"),
514
- uInputRes: gl.getUniformLocation(this.program, "uInputRes"),
515
- uScale: gl.getUniformLocation(this.program, "uScale"),
516
- uOrientation: gl.getUniformLocation(this.program, "uOrientation"),
517
- uDrawBorders: gl.getUniformLocation(this.program, "uDrawBorders"),
518
- uBorderColor: gl.getUniformLocation(this.program, "uBorderColor"),
519
- uBorderThickness: gl.getUniformLocation(this.program, "uBorderThickness"),
520
- uBackgroundColor: gl.getUniformLocation(this.program, "uBackgroundColor")
521
- };
522
- gl.uniform1i(this.uniforms.uTex, 0);
523
- const buf = gl.createBuffer();
524
- gl.bindBuffer(gl.ARRAY_BUFFER, buf);
525
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
526
- gl.enableVertexAttribArray(0);
527
- gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
528
- this.texture = gl.createTexture();
529
- gl.bindTexture(gl.TEXTURE_2D, this.texture);
530
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
531
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
532
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
533
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
534
- gl.clearColor(0, 0, 0, 0);
535
- this.initialized = true;
536
- }
537
- createShader(type, source) {
538
- const gl = this.gl;
539
- const shader = gl.createShader(type);
540
- gl.shaderSource(shader, source);
541
- gl.compileShader(shader);
542
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
543
- const info = gl.getShaderInfoLog(shader);
544
- gl.deleteShader(shader);
545
- throw new Error("Shader compile failed: " + info);
546
- }
547
- return shader;
548
- }
549
- /** Check if renderer is ready */
550
- isReady() {
551
- return this.initialized;
552
- }
553
- /** Render hexagonal effect */
554
- render(input, options = {}) {
555
- if (!this.initialized || !this.gl || !this.canvas) {
556
- throw new Error("Renderer not initialized");
557
- }
558
- const gl = this.gl;
559
- const data = input instanceof ImageData ? input.data : input.data;
560
- const width = input.width;
561
- const height = input.height;
562
- const scale = Math.min(32, Math.max(2, options.scale ?? 16));
563
- const orientation = options.orientation ?? "flat-top";
564
- const { width: outWidth, height: outHeight } = hexGetDimensions(width, height, scale, orientation);
565
- if (this.currentCanvasSize.width !== outWidth || this.currentCanvasSize.height !== outHeight) {
566
- this.canvas.width = outWidth;
567
- this.canvas.height = outHeight;
568
- this.currentCanvasSize = { width: outWidth, height: outHeight };
569
- gl.viewport(0, 0, outWidth, outHeight);
570
- }
571
- gl.uniform2f(this.uniforms.uOutputRes, outWidth, outHeight);
572
- gl.uniform2f(this.uniforms.uInputRes, width, height);
573
- gl.uniform1f(this.uniforms.uScale, scale);
574
- gl.uniform1i(this.uniforms.uOrientation, orientation === "flat-top" ? 0 : 1);
575
- gl.uniform1i(this.uniforms.uDrawBorders, options.drawBorders ? 1 : 0);
576
- gl.uniform1f(this.uniforms.uBorderThickness, options.borderThickness ?? 1);
577
- const borderColor = parseColor(options.borderColor, [0.16, 0.16, 0.16, 1]);
578
- gl.uniform4f(this.uniforms.uBorderColor, ...borderColor);
579
- const bgColor = parseColor(options.backgroundColor, [0, 0, 0, 0]);
580
- gl.uniform4f(this.uniforms.uBackgroundColor, ...bgColor);
581
- gl.bindTexture(gl.TEXTURE_2D, this.texture);
582
- if (this.currentTexSize.width !== width || this.currentTexSize.height !== height) {
583
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
584
- this.currentTexSize = { width, height };
585
- } else {
586
- gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data);
587
- }
588
- gl.clear(gl.COLOR_BUFFER_BIT);
589
- gl.drawArrays(gl.TRIANGLES, 0, 3);
590
- const pixels = new Uint8ClampedArray(outWidth * outHeight * 4);
591
- gl.readPixels(0, 0, outWidth, outHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
592
- return {
593
- data: pixels,
594
- width: outWidth,
595
- height: outHeight
596
- };
597
- }
598
- /** Dispose resources */
599
- dispose() {
600
- if (this.gl) {
601
- if (this.texture) this.gl.deleteTexture(this.texture);
602
- if (this.program) this.gl.deleteProgram(this.program);
603
- this.gl = null;
604
- }
605
- this.canvas = null;
606
- this.initialized = false;
607
- }
608
- };
609
- var HEX_PRESETS = {
610
- default: {},
611
- bordered: {
612
- drawBorders: true,
613
- borderColor: "#282828",
614
- borderThickness: 1
615
- },
616
- pointy: {
617
- orientation: "pointy-top",
618
- drawBorders: false
619
- }
620
- };
621
-
622
- // src/wasm-loader.ts
623
- var wasm = null;
624
- var wasmReady = false;
625
- function initWasm(wasmModule) {
626
- wasm = wasmModule;
627
- wasmReady = true;
628
- }
629
- function isWasmLoaded() {
630
- return wasmReady;
631
- }
632
- function getOutputData(result) {
633
- const memory = wasm.get_memory();
634
- const buffer = new Uint8Array(memory.buffer, result.ptr, result.len);
635
- return new Uint8ClampedArray(buffer.slice());
636
- }
637
- function toUint8Array(input) {
638
- const data = input instanceof ImageData ? input.data : input.data;
639
- return data instanceof Uint8Array ? data : new Uint8Array(data);
640
- }
641
- function ensureWasm() {
642
- if (!wasmReady || !wasm) {
643
- throw new Error("WASM not initialized. Call initWasm() first.");
644
- }
645
- }
646
- var CrtCpuRenderer = class _CrtCpuRenderer {
647
- /** Create renderer (WASM must be initialized first) */
648
- static create() {
649
- ensureWasm();
650
- return new _CrtCpuRenderer();
651
- }
652
- /** Check if renderer is ready */
653
- isReady() {
654
- return wasmReady;
655
- }
656
- /** Render CRT effect */
657
- render(input, options = {}) {
658
- ensureWasm();
659
- const data = toUint8Array(input);
660
- const width = input.width;
661
- const height = input.height;
662
- const scale = Math.min(32, Math.max(2, options.scale ?? 3));
663
- const result = wasm.crt_upscale_config(
664
- data,
665
- width,
666
- height,
667
- scale,
668
- options.warpX ?? 0.015,
669
- options.warpY ?? 0.02,
670
- options.scanHardness ?? -4,
671
- options.scanOpacity ?? 0.5,
672
- options.maskOpacity ?? 0.3,
673
- options.enableWarp !== false,
674
- options.enableScanlines !== false,
675
- options.enableMask !== false
676
- );
677
- return {
678
- data: getOutputData(result),
679
- width: result.width,
680
- height: result.height
681
- };
682
- }
683
- dispose() {
684
- }
685
- };
686
- function parseColorToU32(color, defaultVal) {
687
- if (color === void 0) return defaultVal;
688
- if (typeof color === "number") return color;
689
- if (color === "transparent") return 0;
690
- if (color.startsWith("#")) {
691
- const hex = color.slice(1);
692
- if (hex.length === 6) return parseInt(hex, 16) << 8 | 255;
693
- if (hex.length === 8) return parseInt(hex, 16);
694
- }
695
- return defaultVal;
696
- }
697
- var HexCpuRenderer = class _HexCpuRenderer {
698
- static create() {
699
- ensureWasm();
700
- return new _HexCpuRenderer();
701
- }
702
- isReady() {
703
- return wasmReady;
704
- }
705
- render(input, options = {}) {
706
- ensureWasm();
707
- const data = toUint8Array(input);
708
- const width = input.width;
709
- const height = input.height;
710
- const scale = Math.min(32, Math.max(2, options.scale ?? 16));
711
- const result = wasm.hex_upscale_config(
712
- data,
713
- width,
714
- height,
715
- scale,
716
- options.orientation === "pointy-top" ? 1 : 0,
717
- options.drawBorders ?? false,
718
- parseColorToU32(options.borderColor, 673720575),
719
- options.borderThickness ?? 1,
720
- parseColorToU32(options.backgroundColor, 0)
721
- );
722
- return {
723
- data: getOutputData(result),
724
- width: result.width,
725
- height: result.height
726
- };
727
- }
728
- getDimensions(srcWidth, srcHeight, scale, orientation = "flat-top") {
729
- ensureWasm();
730
- const dims = wasm.hex_get_dimensions(srcWidth, srcHeight, scale, orientation === "pointy-top" ? 1 : 0);
731
- return { width: dims[0], height: dims[1] };
732
- }
733
- dispose() {
734
- }
735
- };
736
- var XbrzCpuRenderer = class _XbrzCpuRenderer {
737
- static create() {
738
- ensureWasm();
739
- return new _XbrzCpuRenderer();
740
- }
741
- isReady() {
742
- return wasmReady;
743
- }
744
- render(input, options = {}) {
745
- ensureWasm();
746
- const data = toUint8Array(input);
747
- const width = input.width;
748
- const height = input.height;
749
- const scale = Math.min(6, Math.max(2, options.scale ?? 2));
750
- const result = wasm.xbrz_upscale_config(
751
- data,
752
- width,
753
- height,
754
- scale,
755
- options.luminanceWeight ?? 1,
756
- options.equalColorTolerance ?? 30,
757
- options.dominantDirectionThreshold ?? 4.4,
758
- options.steepDirectionThreshold ?? 2.2
759
- );
760
- return {
761
- data: getOutputData(result),
762
- width: result.width,
763
- height: result.height
764
- };
765
- }
766
- dispose() {
767
- }
768
- };
769
- var XBRZ_PRESETS = {
770
- default: {},
771
- sharp: {
772
- luminanceWeight: 1,
773
- equalColorTolerance: 20,
774
- dominantDirectionThreshold: 4.4,
775
- steepDirectionThreshold: 2.2
776
- },
777
- smooth: {
778
- luminanceWeight: 0.8,
779
- equalColorTolerance: 35,
780
- dominantDirectionThreshold: 5,
781
- steepDirectionThreshold: 2.5
782
- }
783
- };
784
-
785
- // src/renderart.ts
786
- function checkWebGL2() {
787
- if (typeof OffscreenCanvas === "undefined") return false;
788
- try {
789
- const canvas = new OffscreenCanvas(1, 1);
790
- return !!canvas.getContext("webgl2");
791
- } catch {
792
- return false;
793
- }
794
- }
795
- function getMaxTextureSize() {
796
- if (!checkWebGL2()) return 0;
797
- try {
798
- const canvas = new OffscreenCanvas(1, 1);
799
- const gl = canvas.getContext("webgl2");
800
- return gl?.getParameter(gl.MAX_TEXTURE_SIZE) ?? 0;
801
- } catch {
802
- return 0;
803
- }
804
- }
805
- var CrtEngine = class {
806
- constructor(gpuAvailable) {
807
- this.gpuRenderer = null;
808
- this.cpuRenderer = null;
809
- this.gpuAvailable = gpuAvailable;
810
- }
811
- ensureGpu() {
812
- if (!this.gpuRenderer && this.gpuAvailable) {
813
- this.gpuRenderer = CrtGpuRenderer.create();
814
- }
815
- if (!this.gpuRenderer) throw new Error("GPU renderer not available");
816
- return this.gpuRenderer;
817
- }
818
- ensureCpu() {
819
- if (!this.cpuRenderer) {
820
- this.cpuRenderer = CrtCpuRenderer.create();
821
- }
822
- return this.cpuRenderer;
823
- }
824
- /** Render with options or preset */
825
- render(input, optionsOrPreset = {}) {
826
- const options = typeof optionsOrPreset === "string" ? { ...CRT_PRESETS[optionsOrPreset] } : optionsOrPreset;
827
- const backend = options.backend ?? "auto";
828
- if (backend === "gpu" || backend === "auto" && this.gpuAvailable) {
829
- return this.ensureGpu().render(input, options);
830
- }
831
- return this.ensureCpu().render(input, options);
832
- }
833
- /** Synchronous GPU-only render */
834
- renderSync(input, options = {}) {
835
- return this.ensureGpu().render(input, options);
836
- }
837
- dispose() {
838
- this.gpuRenderer?.dispose();
839
- this.cpuRenderer?.dispose();
840
- this.gpuRenderer = null;
841
- this.cpuRenderer = null;
842
- }
843
- };
844
- var HexEngine = class {
845
- constructor(gpuAvailable) {
846
- this.gpuRenderer = null;
847
- this.cpuRenderer = null;
848
- this.gpuAvailable = gpuAvailable;
849
- }
850
- ensureGpu() {
851
- if (!this.gpuRenderer && this.gpuAvailable) {
852
- this.gpuRenderer = HexGpuRenderer.create();
853
- }
854
- if (!this.gpuRenderer) throw new Error("GPU renderer not available");
855
- return this.gpuRenderer;
856
- }
857
- ensureCpu() {
858
- if (!this.cpuRenderer) {
859
- this.cpuRenderer = HexCpuRenderer.create();
860
- }
861
- return this.cpuRenderer;
862
- }
863
- render(input, optionsOrPreset = {}) {
864
- const options = typeof optionsOrPreset === "string" ? { ...HEX_PRESETS[optionsOrPreset] } : optionsOrPreset;
865
- const backend = options.backend ?? "auto";
866
- if (backend === "gpu" || backend === "auto" && this.gpuAvailable) {
867
- return this.ensureGpu().render(input, options);
868
- }
869
- return this.ensureCpu().render(input, options);
870
- }
871
- renderSync(input, options = {}) {
872
- return this.ensureGpu().render(input, options);
873
- }
874
- getDimensions(srcWidth, srcHeight, options = {}) {
875
- const scale = options.scale ?? 16;
876
- const orientation = options.orientation ?? "flat-top";
877
- return hexGetDimensions(srcWidth, srcHeight, scale, orientation);
878
- }
879
- dispose() {
880
- this.gpuRenderer?.dispose();
881
- this.cpuRenderer?.dispose();
882
- this.gpuRenderer = null;
883
- this.cpuRenderer = null;
884
- }
885
- };
886
- var XbrzEngine = class {
887
- constructor() {
888
- this.cpuRenderer = null;
889
- }
890
- ensureCpu() {
891
- if (!this.cpuRenderer) {
892
- this.cpuRenderer = XbrzCpuRenderer.create();
893
- }
894
- return this.cpuRenderer;
895
- }
896
- render(input, optionsOrPreset = {}) {
897
- const options = typeof optionsOrPreset === "string" ? { ...XBRZ_PRESETS[optionsOrPreset] } : optionsOrPreset;
898
- return this.ensureCpu().render(input, options);
899
- }
900
- dispose() {
901
- this.cpuRenderer?.dispose();
902
- this.cpuRenderer = null;
903
- }
904
- };
905
- var RenderArt = class _RenderArt {
906
- constructor(capabilities) {
907
- this.capabilities = capabilities;
908
- this.crt = new CrtEngine(capabilities.gpu);
909
- this.hex = new HexEngine(capabilities.gpu);
910
- this.xbrz = new XbrzEngine();
911
- }
912
- /**
913
- * Create RenderArt instance.
914
- * For CPU/WASM support, call initWasm() first.
915
- */
916
- static create() {
917
- const gpuAvailable = checkWebGL2();
918
- const cpuAvailable = isWasmLoaded();
919
- return new _RenderArt({
920
- gpu: gpuAvailable,
921
- cpu: cpuAvailable,
922
- maxTextureSize: gpuAvailable ? getMaxTextureSize() : 0,
923
- recommendedBackend: gpuAvailable ? "gpu" : cpuAvailable ? "cpu" : "gpu"
924
- });
925
- }
926
- /**
927
- * Create GPU-only RenderArt instance (no WASM needed).
928
- */
929
- static createGpuOnly() {
930
- const gpuAvailable = checkWebGL2();
931
- if (!gpuAvailable) {
932
- throw new Error("WebGL2 not available");
933
- }
934
- return new _RenderArt({
935
- gpu: true,
936
- cpu: false,
937
- maxTextureSize: getMaxTextureSize(),
938
- recommendedBackend: "gpu"
939
- });
940
- }
941
- dispose() {
942
- this.crt.dispose();
943
- this.hex.dispose();
944
- this.xbrz.dispose();
945
- }
946
- };
947
-
948
- exports.CRT_PRESETS = CRT_PRESETS;
949
- exports.CrtCpuRenderer = CrtCpuRenderer;
950
- exports.CrtEngine = CrtEngine;
951
- exports.CrtGpuRenderer = CrtGpuRenderer;
952
- exports.HEX_PRESETS = HEX_PRESETS;
953
- exports.HexCpuRenderer = HexCpuRenderer;
954
- exports.HexEngine = HexEngine;
955
- exports.HexGpuRenderer = HexGpuRenderer;
956
- exports.RenderArt = RenderArt;
957
- exports.XBRZ_PRESETS = XBRZ_PRESETS;
958
- exports.XbrzCpuRenderer = XbrzCpuRenderer;
959
- exports.XbrzEngine = XbrzEngine;
960
- exports.hexGetDimensions = hexGetDimensions;
961
- exports.initWasm = initWasm;
962
- exports.isWasmLoaded = isWasmLoaded;
963
- //# sourceMappingURL=index.js.map
1
+ /**
2
+ * RenderArt - High-Performance Pixel Art Rendering
3
+ *
4
+ * A collection of GPU-accelerated (WebGL2) and WASM-powered
5
+ * pixel art upscaling and transformation engines.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ // CRT Renderer
10
+ export { CrtGpuRenderer, CRT_PRESETS } from './crt-gpu';
11
+ // Hexagonal Renderer
12
+ export { HexGpuRenderer, HEX_PRESETS, hexGetDimensions } from './hex-gpu';
13
+ // xBRZ Renderer
14
+ export { XbrzGpuRenderer, XBRZ_PRESETS } from './xbrz-gpu';
15
+ /** Convenience factory for creating renderers */
16
+ export const createRenderer = {
17
+ /** Create a CRT effect renderer */
18
+ crt: () => {
19
+ const { CrtGpuRenderer } = require('./crt-gpu');
20
+ return CrtGpuRenderer.create();
21
+ },
22
+ /** Create a hexagonal grid renderer */
23
+ hex: () => {
24
+ const { HexGpuRenderer } = require('./hex-gpu');
25
+ return HexGpuRenderer.create();
26
+ },
27
+ /** Create an xBRZ upscaling renderer */
28
+ xbrz: () => {
29
+ const { XbrzGpuRenderer } = require('./xbrz-gpu');
30
+ return XbrzGpuRenderer.create();
31
+ },
32
+ };
33
+ /** All available presets */
34
+ export { CRT_PRESETS as crtPresets } from './crt-gpu';
35
+ export { HEX_PRESETS as hexPresets } from './hex-gpu';
36
+ export { XBRZ_PRESETS as xbrzPresets } from './xbrz-gpu';
964
37
  //# sourceMappingURL=index.js.map