|
| 1 | +import { |
| 2 | + drawPass, ensureTexture, getGLCtx, getQuadVAO, glAvailable, |
| 3 | + linkProgram, readoutToCanvas, resizeGLCanvas, uploadSourceTexture, |
| 4 | + type Program, |
| 5 | +} from "gl"; |
| 6 | + |
| 7 | +// Lichtenstein-style pop-art pass: saturation boost + colour posterise + |
| 8 | +// luminance-driven Ben-Day dots on a white background. JS-orientation |
| 9 | +// pixel coordinates match the reference loop so dot placement is stable. |
| 10 | +const FS = `#version 300 es |
| 11 | +precision highp float; |
| 12 | +in vec2 v_uv; |
| 13 | +out vec4 fragColor; |
| 14 | +uniform sampler2D u_source; |
| 15 | +uniform vec2 u_res; |
| 16 | +uniform float u_dotSize; |
| 17 | +uniform float u_levels; |
| 18 | +uniform float u_satBoost; |
| 19 | +
|
| 20 | +void main() { |
| 21 | + vec2 px = v_uv * u_res; |
| 22 | + float x = floor(px.x); |
| 23 | + float y = u_res.y - 1.0 - floor(px.y); |
| 24 | +
|
| 25 | + vec3 src = texture(u_source, vec2((x + 0.5) / u_res.x, 1.0 - (y + 0.5) / u_res.y)).rgb * 255.0; |
| 26 | +
|
| 27 | + // Saturation boost around luma. |
| 28 | + float gray = 0.2126 * src.r + 0.7152 * src.g + 0.0722 * src.b; |
| 29 | + vec3 sat = clamp(gray + (src - gray) * u_satBoost, 0.0, 255.0); |
| 30 | + sat = floor(sat + 0.5); |
| 31 | +
|
| 32 | + // Posterise. |
| 33 | + float step = 255.0 / (u_levels - 1.0); |
| 34 | + vec3 post = floor(floor(sat / step + 0.5) * step + 0.5); |
| 35 | +
|
| 36 | + // Ben-Day dots. |
| 37 | + float lum = (0.2126 * post.r + 0.7152 * post.g + 0.0722 * post.b) / 255.0; |
| 38 | + float cellX = mod(x, u_dotSize); |
| 39 | + float cellY = mod(y, u_dotSize); |
| 40 | + float cx = u_dotSize * 0.5; |
| 41 | + float dist = length(vec2(cellX - cx, cellY - cx)); |
| 42 | + float dotR = cx * (1.0 - lum); |
| 43 | +
|
| 44 | + vec3 outCol = dist < dotR ? post / 255.0 : vec3(1.0); |
| 45 | + fragColor = vec4(outCol, 1.0); |
| 46 | +} |
| 47 | +`; |
| 48 | + |
| 49 | +type Cache = { prog: Program }; |
| 50 | +let _cache: Cache | null = null; |
| 51 | +const initCache = (gl: WebGL2RenderingContext): Cache => { |
| 52 | + if (_cache) return _cache; |
| 53 | + _cache = { prog: linkProgram(gl, FS, [ |
| 54 | + "u_source", "u_res", "u_dotSize", "u_levels", "u_satBoost", |
| 55 | + ] as const) }; |
| 56 | + return _cache; |
| 57 | +}; |
| 58 | + |
| 59 | +export const popArtGLAvailable = (): boolean => glAvailable(); |
| 60 | + |
| 61 | +export const renderPopArtGL = ( |
| 62 | + source: HTMLCanvasElement | OffscreenCanvas, |
| 63 | + width: number, height: number, |
| 64 | + dotSize: number, levels: number, saturationBoost: number, |
| 65 | +): HTMLCanvasElement | OffscreenCanvas | null => { |
| 66 | + const ctx = getGLCtx(); |
| 67 | + if (!ctx) return null; |
| 68 | + const { gl, canvas } = ctx; |
| 69 | + const cache = initCache(gl); |
| 70 | + const vao = getQuadVAO(gl); |
| 71 | + resizeGLCanvas(canvas, width, height); |
| 72 | + const sourceTex = ensureTexture(gl, "popArt:source", width, height); |
| 73 | + uploadSourceTexture(gl, sourceTex, source); |
| 74 | + drawPass(gl, null, width, height, cache.prog, () => { |
| 75 | + gl.activeTexture(gl.TEXTURE0); |
| 76 | + gl.bindTexture(gl.TEXTURE_2D, sourceTex.tex); |
| 77 | + gl.uniform1i(cache.prog.uniforms.u_source, 0); |
| 78 | + gl.uniform2f(cache.prog.uniforms.u_res, width, height); |
| 79 | + gl.uniform1f(cache.prog.uniforms.u_dotSize, dotSize); |
| 80 | + gl.uniform1f(cache.prog.uniforms.u_levels, levels); |
| 81 | + gl.uniform1f(cache.prog.uniforms.u_satBoost, saturationBoost); |
| 82 | + }, vao); |
| 83 | + return readoutToCanvas(canvas, width, height); |
| 84 | +}; |
0 commit comments