@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.
- package/LICENSE +1 -1
- package/README.md +171 -67
- package/dist/crt-gpu.d.ts +30 -0
- package/dist/crt-gpu.d.ts.map +1 -0
- package/dist/crt-gpu.js +282 -0
- package/dist/crt-gpu.js.map +1 -0
- package/dist/hex-gpu.d.ts +35 -0
- package/dist/hex-gpu.d.ts.map +1 -0
- package/dist/hex-gpu.js +382 -0
- package/dist/hex-gpu.js.map +1 -0
- package/dist/index.d.ts +21 -300
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -963
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +84 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/dist/wasm-wrapper.d.ts +71 -0
- package/dist/wasm-wrapper.d.ts.map +1 -0
- package/dist/wasm-wrapper.js +76 -0
- package/dist/wasm-wrapper.js.map +1 -0
- package/dist/xbrz-gpu.d.ts +34 -0
- package/dist/xbrz-gpu.d.ts.map +1 -0
- package/dist/xbrz-gpu.js +640 -0
- package/dist/xbrz-gpu.js.map +1 -0
- package/package.json +48 -35
- package/src/crt-gpu.ts +313 -0
- package/src/hex-gpu.ts +426 -0
- package/src/index.ts +52 -0
- package/src/types.ts +90 -0
- package/src/wasm/crt.rs +181 -0
- package/src/wasm/hex.rs +324 -0
- package/src/wasm/lib.rs +285 -0
- package/src/wasm/xbrz.rs +262 -0
- package/src/wasm-wrapper.ts +195 -0
- package/src/xbrz-gpu.ts +671 -0
- package/dist/index.d.mts +0 -305
- package/dist/index.mjs +0 -948
- package/dist/index.mjs.map +0 -1
- package/pkg/LICENSE +0 -21
- package/pkg/README.md +0 -117
- package/pkg/renderart_wasm.d.ts +0 -52
- package/pkg/renderart_wasm.js +0 -5
- package/pkg/renderart_wasm_bg.js +0 -283
- package/pkg/renderart_wasm_bg.wasm +0 -0
- package/pkg/renderart_wasm_bg.wasm.d.ts +0 -24
package/src/hex-gpu.ts
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hexagonal GPU Renderer using WebGL2
|
|
3
|
+
*
|
|
4
|
+
* High-performance hexagonal pixel grid transformation using fragment shaders.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { HexOptions, HexOrientation, ImageInput, ImageOutput, Renderer } from './types';
|
|
8
|
+
|
|
9
|
+
// Vertex shader
|
|
10
|
+
const VERTEX_SHADER = `#version 300 es
|
|
11
|
+
layout(location = 0) in vec2 position;
|
|
12
|
+
out vec2 vUv;
|
|
13
|
+
|
|
14
|
+
void main() {
|
|
15
|
+
vUv = position * 0.5 + 0.5;
|
|
16
|
+
gl_Position = vec4(position, 0.0, 1.0);
|
|
17
|
+
}`;
|
|
18
|
+
|
|
19
|
+
// Fragment shader for hexagonal rendering
|
|
20
|
+
const FRAGMENT_SHADER = `#version 300 es
|
|
21
|
+
precision highp float;
|
|
22
|
+
|
|
23
|
+
uniform sampler2D uTex;
|
|
24
|
+
uniform vec2 uOutputRes;
|
|
25
|
+
uniform vec2 uInputRes;
|
|
26
|
+
uniform float uScale;
|
|
27
|
+
uniform int uOrientation; // 0 = flat-top, 1 = pointy-top
|
|
28
|
+
uniform int uDrawBorders;
|
|
29
|
+
uniform vec4 uBorderColor;
|
|
30
|
+
uniform float uBorderThickness;
|
|
31
|
+
uniform vec4 uBackgroundColor;
|
|
32
|
+
|
|
33
|
+
in vec2 vUv;
|
|
34
|
+
out vec4 outColor;
|
|
35
|
+
|
|
36
|
+
const float SQRT3 = 1.732050808;
|
|
37
|
+
const float INV_SQRT3 = 0.577350269;
|
|
38
|
+
|
|
39
|
+
// Convert output pixel to hex coordinate (flat-top)
|
|
40
|
+
vec2 pixelToHexFlat(vec2 pos, float scale) {
|
|
41
|
+
float hSpacing = scale * 1.5;
|
|
42
|
+
float vSpacing = scale * SQRT3;
|
|
43
|
+
|
|
44
|
+
float col = pos.x / hSpacing;
|
|
45
|
+
int colInt = int(floor(col));
|
|
46
|
+
|
|
47
|
+
// Check if odd column (offset)
|
|
48
|
+
bool isOffset = (colInt & 1) == 1;
|
|
49
|
+
float yOffset = isOffset ? vSpacing * 0.5 : 0.0;
|
|
50
|
+
|
|
51
|
+
float row = (pos.y - yOffset) / vSpacing;
|
|
52
|
+
int rowInt = int(floor(row));
|
|
53
|
+
|
|
54
|
+
// Refine with corner detection
|
|
55
|
+
float cellX = pos.x - float(colInt) * hSpacing;
|
|
56
|
+
float cellY = pos.y - float(rowInt) * vSpacing - yOffset;
|
|
57
|
+
|
|
58
|
+
float quarterW = scale * 0.5;
|
|
59
|
+
float halfH = vSpacing * 0.5;
|
|
60
|
+
|
|
61
|
+
if (cellX < quarterW) {
|
|
62
|
+
float distFromCenter = abs(cellY - halfH);
|
|
63
|
+
float edgeX = distFromCenter * INV_SQRT3;
|
|
64
|
+
|
|
65
|
+
if (cellX < quarterW - edgeX) {
|
|
66
|
+
if (cellY < halfH) {
|
|
67
|
+
rowInt -= 1;
|
|
68
|
+
}
|
|
69
|
+
colInt -= 1;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return vec2(float(colInt), float(rowInt));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Convert output pixel to hex coordinate (pointy-top)
|
|
77
|
+
vec2 pixelToHexPointy(vec2 pos, float scale) {
|
|
78
|
+
float hSpacing = scale * SQRT3;
|
|
79
|
+
float vSpacing = scale * 1.5;
|
|
80
|
+
|
|
81
|
+
float row = pos.y / vSpacing;
|
|
82
|
+
int rowInt = int(floor(row));
|
|
83
|
+
|
|
84
|
+
// Check if odd row (offset)
|
|
85
|
+
bool isOffset = (rowInt & 1) == 1;
|
|
86
|
+
float xOffset = isOffset ? hSpacing * 0.5 : 0.0;
|
|
87
|
+
|
|
88
|
+
float col = (pos.x - xOffset) / hSpacing;
|
|
89
|
+
int colInt = int(floor(col));
|
|
90
|
+
|
|
91
|
+
// Refine with corner detection
|
|
92
|
+
float cellX = pos.x - float(colInt) * hSpacing - xOffset;
|
|
93
|
+
float cellY = pos.y - float(rowInt) * vSpacing;
|
|
94
|
+
|
|
95
|
+
float halfW = hSpacing * 0.5;
|
|
96
|
+
float quarterH = scale * 0.5;
|
|
97
|
+
|
|
98
|
+
if (cellY < quarterH) {
|
|
99
|
+
float distFromCenter = abs(cellX - halfW);
|
|
100
|
+
float edgeY = distFromCenter * SQRT3;
|
|
101
|
+
|
|
102
|
+
if (cellY < edgeY) {
|
|
103
|
+
if (cellX < halfW && isOffset) {
|
|
104
|
+
colInt -= 1;
|
|
105
|
+
}
|
|
106
|
+
rowInt -= 1;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return vec2(float(colInt), float(rowInt));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Check if pixel is on hex border
|
|
114
|
+
bool isBorderPixel(vec2 pos, float scale, int orientation, float thickness) {
|
|
115
|
+
for (float dy = -thickness; dy <= thickness; dy += 1.0) {
|
|
116
|
+
for (float dx = -thickness; dx <= thickness; dx += 1.0) {
|
|
117
|
+
if (dx == 0.0 && dy == 0.0) continue;
|
|
118
|
+
|
|
119
|
+
vec2 h1 = orientation == 0
|
|
120
|
+
? pixelToHexFlat(pos, scale)
|
|
121
|
+
: pixelToHexPointy(pos, scale);
|
|
122
|
+
vec2 h2 = orientation == 0
|
|
123
|
+
? pixelToHexFlat(pos + vec2(dx, dy), scale)
|
|
124
|
+
: pixelToHexPointy(pos + vec2(dx, dy), scale);
|
|
125
|
+
|
|
126
|
+
if (h1 != h2) return true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
void main() {
|
|
133
|
+
vec2 pixelPos = vUv * uOutputRes;
|
|
134
|
+
|
|
135
|
+
// Get hex coordinate
|
|
136
|
+
vec2 hexCoord = uOrientation == 0
|
|
137
|
+
? pixelToHexFlat(pixelPos, uScale)
|
|
138
|
+
: pixelToHexPointy(pixelPos, uScale);
|
|
139
|
+
|
|
140
|
+
// Check bounds
|
|
141
|
+
if (hexCoord.x < 0.0 || hexCoord.y < 0.0 ||
|
|
142
|
+
hexCoord.x >= uInputRes.x || hexCoord.y >= uInputRes.y) {
|
|
143
|
+
outColor = uBackgroundColor;
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Check for border
|
|
148
|
+
if (uDrawBorders == 1 && uBorderThickness > 0.0) {
|
|
149
|
+
if (isBorderPixel(pixelPos, uScale, uOrientation, uBorderThickness)) {
|
|
150
|
+
outColor = uBorderColor;
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Sample source pixel (add 0.5 for pixel center)
|
|
156
|
+
vec2 texCoord = (hexCoord + 0.5) / uInputRes;
|
|
157
|
+
outColor = texture(uTex, texCoord);
|
|
158
|
+
}`;
|
|
159
|
+
|
|
160
|
+
/** Parse color from CSS string or number */
|
|
161
|
+
function parseColor(color: string | number | undefined, defaultColor: [number, number, number, number]): [number, number, number, number] {
|
|
162
|
+
if (color === undefined) return defaultColor;
|
|
163
|
+
|
|
164
|
+
if (typeof color === 'number') {
|
|
165
|
+
return [
|
|
166
|
+
((color >> 24) & 0xFF) / 255,
|
|
167
|
+
((color >> 16) & 0xFF) / 255,
|
|
168
|
+
((color >> 8) & 0xFF) / 255,
|
|
169
|
+
(color & 0xFF) / 255,
|
|
170
|
+
];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Parse CSS color
|
|
174
|
+
if (color === 'transparent') return [0, 0, 0, 0];
|
|
175
|
+
|
|
176
|
+
// Parse hex color
|
|
177
|
+
if (color.startsWith('#')) {
|
|
178
|
+
const hex = color.slice(1);
|
|
179
|
+
if (hex.length === 6) {
|
|
180
|
+
return [
|
|
181
|
+
parseInt(hex.slice(0, 2), 16) / 255,
|
|
182
|
+
parseInt(hex.slice(2, 4), 16) / 255,
|
|
183
|
+
parseInt(hex.slice(4, 6), 16) / 255,
|
|
184
|
+
1,
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
if (hex.length === 8) {
|
|
188
|
+
return [
|
|
189
|
+
parseInt(hex.slice(0, 2), 16) / 255,
|
|
190
|
+
parseInt(hex.slice(2, 4), 16) / 255,
|
|
191
|
+
parseInt(hex.slice(4, 6), 16) / 255,
|
|
192
|
+
parseInt(hex.slice(6, 8), 16) / 255,
|
|
193
|
+
];
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return defaultColor;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Calculate output dimensions for hex rendering */
|
|
201
|
+
export function hexGetDimensions(
|
|
202
|
+
srcWidth: number,
|
|
203
|
+
srcHeight: number,
|
|
204
|
+
scale: number,
|
|
205
|
+
orientation: HexOrientation = 'flat-top'
|
|
206
|
+
): { width: number; height: number } {
|
|
207
|
+
const SQRT3 = 1.732050808;
|
|
208
|
+
|
|
209
|
+
if (orientation === 'flat-top') {
|
|
210
|
+
const hSpacing = scale * 1.5;
|
|
211
|
+
const vSpacing = scale * SQRT3;
|
|
212
|
+
const cellWidth = scale * 2;
|
|
213
|
+
const cellHeight = scale * SQRT3;
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
width: Math.ceil(srcWidth * hSpacing + cellWidth),
|
|
217
|
+
height: Math.ceil(srcHeight * vSpacing + cellHeight),
|
|
218
|
+
};
|
|
219
|
+
} else {
|
|
220
|
+
const hSpacing = scale * SQRT3;
|
|
221
|
+
const vSpacing = scale * 1.5;
|
|
222
|
+
const cellWidth = scale * SQRT3;
|
|
223
|
+
const cellHeight = scale * 2;
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
width: Math.ceil(srcWidth * hSpacing + cellWidth),
|
|
227
|
+
height: Math.ceil(srcHeight * vSpacing + cellHeight),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** HEX GPU Renderer */
|
|
233
|
+
export class HexGpuRenderer implements Renderer<HexOptions> {
|
|
234
|
+
private gl: WebGL2RenderingContext | null = null;
|
|
235
|
+
private canvas: OffscreenCanvas | null = null;
|
|
236
|
+
private program: WebGLProgram | null = null;
|
|
237
|
+
private texture: WebGLTexture | null = null;
|
|
238
|
+
private uniforms: Record<string, WebGLUniformLocation | null> = {};
|
|
239
|
+
private initialized = false;
|
|
240
|
+
private currentCanvasSize = { width: 0, height: 0 };
|
|
241
|
+
private currentTexSize = { width: 0, height: 0 };
|
|
242
|
+
|
|
243
|
+
/** Create a new HEX GPU renderer */
|
|
244
|
+
static create(): HexGpuRenderer {
|
|
245
|
+
const renderer = new HexGpuRenderer();
|
|
246
|
+
renderer.init();
|
|
247
|
+
return renderer;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private init(): void {
|
|
251
|
+
if (typeof OffscreenCanvas === 'undefined') {
|
|
252
|
+
throw new Error('OffscreenCanvas not supported');
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
this.canvas = new OffscreenCanvas(1, 1);
|
|
256
|
+
this.gl = this.canvas.getContext('webgl2', {
|
|
257
|
+
alpha: true,
|
|
258
|
+
premultipliedAlpha: false,
|
|
259
|
+
desynchronized: true,
|
|
260
|
+
powerPreference: 'high-performance',
|
|
261
|
+
antialias: false,
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
if (!this.gl) {
|
|
265
|
+
throw new Error('WebGL2 not supported');
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const gl = this.gl;
|
|
269
|
+
|
|
270
|
+
// Create shaders
|
|
271
|
+
const vs = this.createShader(gl.VERTEX_SHADER, VERTEX_SHADER);
|
|
272
|
+
const fs = this.createShader(gl.FRAGMENT_SHADER, FRAGMENT_SHADER);
|
|
273
|
+
|
|
274
|
+
// Create program
|
|
275
|
+
this.program = gl.createProgram()!;
|
|
276
|
+
gl.attachShader(this.program, vs);
|
|
277
|
+
gl.attachShader(this.program, fs);
|
|
278
|
+
gl.linkProgram(this.program);
|
|
279
|
+
|
|
280
|
+
if (!gl.getProgramParameter(this.program, gl.LINK_STATUS)) {
|
|
281
|
+
throw new Error('Shader program link failed: ' + gl.getProgramInfoLog(this.program));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
gl.useProgram(this.program);
|
|
285
|
+
|
|
286
|
+
// Get uniform locations
|
|
287
|
+
this.uniforms = {
|
|
288
|
+
uTex: gl.getUniformLocation(this.program, 'uTex'),
|
|
289
|
+
uOutputRes: gl.getUniformLocation(this.program, 'uOutputRes'),
|
|
290
|
+
uInputRes: gl.getUniformLocation(this.program, 'uInputRes'),
|
|
291
|
+
uScale: gl.getUniformLocation(this.program, 'uScale'),
|
|
292
|
+
uOrientation: gl.getUniformLocation(this.program, 'uOrientation'),
|
|
293
|
+
uDrawBorders: gl.getUniformLocation(this.program, 'uDrawBorders'),
|
|
294
|
+
uBorderColor: gl.getUniformLocation(this.program, 'uBorderColor'),
|
|
295
|
+
uBorderThickness: gl.getUniformLocation(this.program, 'uBorderThickness'),
|
|
296
|
+
uBackgroundColor: gl.getUniformLocation(this.program, 'uBackgroundColor'),
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
gl.uniform1i(this.uniforms.uTex, 0);
|
|
300
|
+
|
|
301
|
+
// Setup geometry
|
|
302
|
+
const buf = gl.createBuffer();
|
|
303
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
|
|
304
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
|
|
305
|
+
gl.enableVertexAttribArray(0);
|
|
306
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
|
|
307
|
+
|
|
308
|
+
// Create texture
|
|
309
|
+
this.texture = gl.createTexture();
|
|
310
|
+
gl.bindTexture(gl.TEXTURE_2D, this.texture);
|
|
311
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
312
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
313
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
314
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
315
|
+
|
|
316
|
+
gl.clearColor(0.0, 0.0, 0.0, 0.0);
|
|
317
|
+
|
|
318
|
+
this.initialized = true;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
private createShader(type: number, source: string): WebGLShader {
|
|
322
|
+
const gl = this.gl!;
|
|
323
|
+
const shader = gl.createShader(type)!;
|
|
324
|
+
gl.shaderSource(shader, source);
|
|
325
|
+
gl.compileShader(shader);
|
|
326
|
+
|
|
327
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
328
|
+
const info = gl.getShaderInfoLog(shader);
|
|
329
|
+
gl.deleteShader(shader);
|
|
330
|
+
throw new Error('Shader compile failed: ' + info);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return shader;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** Check if renderer is ready */
|
|
337
|
+
isReady(): boolean {
|
|
338
|
+
return this.initialized;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Render hexagonal effect */
|
|
342
|
+
render(input: ImageInput | ImageData, options: HexOptions = {}): ImageOutput {
|
|
343
|
+
if (!this.initialized || !this.gl || !this.canvas) {
|
|
344
|
+
throw new Error('Renderer not initialized');
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const gl = this.gl;
|
|
348
|
+
const data = input instanceof ImageData ? input.data : input.data;
|
|
349
|
+
const width = input.width;
|
|
350
|
+
const height = input.height;
|
|
351
|
+
|
|
352
|
+
const scale = Math.min(32, Math.max(2, options.scale ?? 16));
|
|
353
|
+
const orientation: HexOrientation = options.orientation ?? 'flat-top';
|
|
354
|
+
const { width: outWidth, height: outHeight } = hexGetDimensions(width, height, scale, orientation);
|
|
355
|
+
|
|
356
|
+
// Resize canvas if needed
|
|
357
|
+
if (this.currentCanvasSize.width !== outWidth || this.currentCanvasSize.height !== outHeight) {
|
|
358
|
+
this.canvas.width = outWidth;
|
|
359
|
+
this.canvas.height = outHeight;
|
|
360
|
+
this.currentCanvasSize = { width: outWidth, height: outHeight };
|
|
361
|
+
gl.viewport(0, 0, outWidth, outHeight);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Update uniforms
|
|
365
|
+
gl.uniform2f(this.uniforms.uOutputRes, outWidth, outHeight);
|
|
366
|
+
gl.uniform2f(this.uniforms.uInputRes, width, height);
|
|
367
|
+
gl.uniform1f(this.uniforms.uScale, scale);
|
|
368
|
+
gl.uniform1i(this.uniforms.uOrientation, orientation === 'flat-top' ? 0 : 1);
|
|
369
|
+
gl.uniform1i(this.uniforms.uDrawBorders, options.drawBorders ? 1 : 0);
|
|
370
|
+
gl.uniform1f(this.uniforms.uBorderThickness, options.borderThickness ?? 1);
|
|
371
|
+
|
|
372
|
+
const borderColor = parseColor(options.borderColor, [0.16, 0.16, 0.16, 1]);
|
|
373
|
+
gl.uniform4f(this.uniforms.uBorderColor, ...borderColor);
|
|
374
|
+
|
|
375
|
+
const bgColor = parseColor(options.backgroundColor, [0, 0, 0, 0]);
|
|
376
|
+
gl.uniform4f(this.uniforms.uBackgroundColor, ...bgColor);
|
|
377
|
+
|
|
378
|
+
// Update texture
|
|
379
|
+
gl.bindTexture(gl.TEXTURE_2D, this.texture);
|
|
380
|
+
if (this.currentTexSize.width !== width || this.currentTexSize.height !== height) {
|
|
381
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
|
|
382
|
+
this.currentTexSize = { width, height };
|
|
383
|
+
} else {
|
|
384
|
+
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Render
|
|
388
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
389
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
390
|
+
|
|
391
|
+
// Read pixels
|
|
392
|
+
const pixels = new Uint8ClampedArray(outWidth * outHeight * 4);
|
|
393
|
+
gl.readPixels(0, 0, outWidth, outHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
|
|
394
|
+
|
|
395
|
+
return {
|
|
396
|
+
data: pixels,
|
|
397
|
+
width: outWidth,
|
|
398
|
+
height: outHeight,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** Dispose resources */
|
|
403
|
+
dispose(): void {
|
|
404
|
+
if (this.gl) {
|
|
405
|
+
if (this.texture) this.gl.deleteTexture(this.texture);
|
|
406
|
+
if (this.program) this.gl.deleteProgram(this.program);
|
|
407
|
+
this.gl = null;
|
|
408
|
+
}
|
|
409
|
+
this.canvas = null;
|
|
410
|
+
this.initialized = false;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** HEX presets */
|
|
415
|
+
export const HEX_PRESETS: Record<string, Partial<HexOptions>> = {
|
|
416
|
+
default: {},
|
|
417
|
+
bordered: {
|
|
418
|
+
drawBorders: true,
|
|
419
|
+
borderColor: '#282828',
|
|
420
|
+
borderThickness: 1,
|
|
421
|
+
},
|
|
422
|
+
pointy: {
|
|
423
|
+
orientation: 'pointy-top',
|
|
424
|
+
drawBorders: false,
|
|
425
|
+
},
|
|
426
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
|
|
10
|
+
// Types
|
|
11
|
+
export type {
|
|
12
|
+
ImageInput,
|
|
13
|
+
ImageOutput,
|
|
14
|
+
Renderer,
|
|
15
|
+
CrtOptions,
|
|
16
|
+
HexOptions,
|
|
17
|
+
HexOrientation,
|
|
18
|
+
XbrzOptions,
|
|
19
|
+
} from './types';
|
|
20
|
+
|
|
21
|
+
// CRT Renderer
|
|
22
|
+
export { CrtGpuRenderer, CRT_PRESETS } from './crt-gpu';
|
|
23
|
+
|
|
24
|
+
// Hexagonal Renderer
|
|
25
|
+
export { HexGpuRenderer, HEX_PRESETS, hexGetDimensions } from './hex-gpu';
|
|
26
|
+
|
|
27
|
+
// xBRZ Renderer
|
|
28
|
+
export { XbrzGpuRenderer, XBRZ_PRESETS } from './xbrz-gpu';
|
|
29
|
+
|
|
30
|
+
/** Convenience factory for creating renderers */
|
|
31
|
+
export const createRenderer = {
|
|
32
|
+
/** Create a CRT effect renderer */
|
|
33
|
+
crt: () => {
|
|
34
|
+
const { CrtGpuRenderer } = require('./crt-gpu');
|
|
35
|
+
return CrtGpuRenderer.create();
|
|
36
|
+
},
|
|
37
|
+
/** Create a hexagonal grid renderer */
|
|
38
|
+
hex: () => {
|
|
39
|
+
const { HexGpuRenderer } = require('./hex-gpu');
|
|
40
|
+
return HexGpuRenderer.create();
|
|
41
|
+
},
|
|
42
|
+
/** Create an xBRZ upscaling renderer */
|
|
43
|
+
xbrz: () => {
|
|
44
|
+
const { XbrzGpuRenderer } = require('./xbrz-gpu');
|
|
45
|
+
return XbrzGpuRenderer.create();
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** All available presets */
|
|
50
|
+
export { CRT_PRESETS as crtPresets } from './crt-gpu';
|
|
51
|
+
export { HEX_PRESETS as hexPresets } from './hex-gpu';
|
|
52
|
+
export { XBRZ_PRESETS as xbrzPresets } from './xbrz-gpu';
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RenderArt Shared Types
|
|
3
|
+
*
|
|
4
|
+
* Common type definitions for all rendering engines.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Input image data */
|
|
8
|
+
export interface ImageInput {
|
|
9
|
+
/** Raw RGBA pixel data */
|
|
10
|
+
data: Uint8ClampedArray | Uint8Array;
|
|
11
|
+
/** Image width in pixels */
|
|
12
|
+
width: number;
|
|
13
|
+
/** Image height in pixels */
|
|
14
|
+
height: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Output image data */
|
|
18
|
+
export interface ImageOutput {
|
|
19
|
+
/** Raw RGBA pixel data */
|
|
20
|
+
data: Uint8ClampedArray;
|
|
21
|
+
/** Output width in pixels */
|
|
22
|
+
width: number;
|
|
23
|
+
/** Output height in pixels */
|
|
24
|
+
height: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Base renderer interface */
|
|
28
|
+
export interface Renderer<TOptions = object> {
|
|
29
|
+
/** Check if renderer is ready for use */
|
|
30
|
+
isReady(): boolean;
|
|
31
|
+
/** Render image with given options */
|
|
32
|
+
render(input: ImageInput | ImageData, options?: TOptions): ImageOutput;
|
|
33
|
+
/** Dispose of renderer resources */
|
|
34
|
+
dispose(): void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** CRT effect options */
|
|
38
|
+
export interface CrtOptions {
|
|
39
|
+
/** Output scale factor (2-32, default: 3) */
|
|
40
|
+
scale?: number;
|
|
41
|
+
/** Horizontal warp amount (default: 0.015) */
|
|
42
|
+
warpX?: number;
|
|
43
|
+
/** Vertical warp amount (default: 0.02) */
|
|
44
|
+
warpY?: number;
|
|
45
|
+
/** Scanline hardness, negative values (default: -4.0) */
|
|
46
|
+
scanHardness?: number;
|
|
47
|
+
/** Scanline opacity (0-1, default: 0.5) */
|
|
48
|
+
scanOpacity?: number;
|
|
49
|
+
/** Shadow mask opacity (0-1, default: 0.3) */
|
|
50
|
+
maskOpacity?: number;
|
|
51
|
+
/** Enable barrel distortion (default: true) */
|
|
52
|
+
enableWarp?: boolean;
|
|
53
|
+
/** Enable scanline effect (default: true) */
|
|
54
|
+
enableScanlines?: boolean;
|
|
55
|
+
/** Enable shadow mask (default: true) */
|
|
56
|
+
enableMask?: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Hexagonal grid orientation */
|
|
60
|
+
export type HexOrientation = 'flat-top' | 'pointy-top';
|
|
61
|
+
|
|
62
|
+
/** Hexagonal grid options */
|
|
63
|
+
export interface HexOptions {
|
|
64
|
+
/** Output scale factor (2-32, default: 16) */
|
|
65
|
+
scale?: number;
|
|
66
|
+
/** Hexagon orientation (default: 'flat-top') */
|
|
67
|
+
orientation?: HexOrientation;
|
|
68
|
+
/** Draw borders between hexagons (default: false) */
|
|
69
|
+
drawBorders?: boolean;
|
|
70
|
+
/** Border color as CSS color string or RGBA number (default: '#282828') */
|
|
71
|
+
borderColor?: string | number;
|
|
72
|
+
/** Border thickness in pixels (default: 1) */
|
|
73
|
+
borderThickness?: number;
|
|
74
|
+
/** Background color for out-of-bounds areas (default: 'transparent') */
|
|
75
|
+
backgroundColor?: string | number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** xBRZ scaling options */
|
|
79
|
+
export interface XbrzOptions {
|
|
80
|
+
/** Output scale factor (2-6, default: 2) */
|
|
81
|
+
scale?: number;
|
|
82
|
+
/** Luminance weight for color comparison (default: 1.0) */
|
|
83
|
+
luminanceWeight?: number;
|
|
84
|
+
/** Color equality tolerance (0-255, default: 30) */
|
|
85
|
+
equalColorTolerance?: number;
|
|
86
|
+
/** Steep direction threshold (default: 2.2) */
|
|
87
|
+
steepDirectionThreshold?: number;
|
|
88
|
+
/** Dominant direction threshold (default: 3.6) */
|
|
89
|
+
dominantDirectionThreshold?: number;
|
|
90
|
+
}
|