1.0 || uv.y < 0.0 || uv.y > 1.0) { gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); return; } vec2 p = uv * u_src; float sl = p.y * 2.0; // the 400 scanlines (double scan) float li = floor(sl); float d1 = sl - li - 0.5; float l2 = li + (d1 < 0.0 ? -1.0 : 1.0); float d2 = sl - l2 - 0.5; vec3 a = pow(signal(floor(li * 0.5), p.x), vec3(2.5)); vec3 b = pow(signal(clamp(floor(l2 * 0.5), 0.0, u_src.y - 1.0), p.x), vec3(2.5)); vec3 col = beam(a, d1) + beam(b, d2); // the slot mask: R, G, B columns of window pixels, only when a game pixel is 3+ window pixels wide float k = clamp((u_resolution.x / u_src.x - 2.5) / 3.0, 0.0, 1.0) * 0.28; float m = mod(floor(gl_FragCoord.x), 3.0); vec3 mask = vec3(m < 0.5 ? 1.0 : 0.6, (m > 0.5 && m < 1.5) ? 1.0 : 0.6, m > 1.5 ? 1.0 : 0.6); col *= mix(vec3(1.0), mask, k) * (1.0 + k * 0.6); col = col / (1.0 + 0.12 * col) * 1.12; // phosphor saturation col *= 1.0 - 0.35 * r2; // corners a little darker gl_FragColor = vec4(pow(col, vec3(1.0 / 2.2)) + 0.008, 1.0); } GLSL; private bool $on = false; /** @var array{0: int, 1: int} */ private array $size = [320, 200]; public function __construct(private WebGl $gl) { } /** Over whatever the 2D shows (any edition): false without OpenGL / WebGL. */ public function enable(): bool { if (!$this->gl->isAvailable()) { return false; } $this->gl->setLayer('above', 'native', [255, 0, 255]); // the post shader then runs on the window's pixels $this->gl->setPostShader(self::SHADER, ['u_src' => [320.0, 200.0]]); $this->size = [320, 200]; $this->on = true; return true; } /** The size of the VM's frame (320 x 200, films 320 x 240...). */ public function source(int $w, int $h): void { if ($this->on && $w > 0 && $h > 0 && [$w, $h] !== $this->size) { $this->size = [$w, $h]; $this->gl->setUniform('u_src', [(float) $w, (float) $h]); } } public function disable(): void { if ($this->on) { $this->gl->disable(); $this->on = false; } } }