|
| 1 | +class CRTEffect { |
| 2 | + constructor(targetCanvas, container) { |
| 3 | + this.gameCanvas = targetCanvas; |
| 4 | + |
| 5 | + // Create WebGL canvas with fixed 1024x1024 dimensions |
| 6 | + this.glCanvas = document.createElement('canvas'); |
| 7 | + this.glCanvas.width = 1024; |
| 8 | + this.glCanvas.height = 1024; |
| 9 | + |
| 10 | + // Force exact pixel dimensions in style |
| 11 | + this.glCanvas.style.width = '1024px'; |
| 12 | + this.glCanvas.style.height = '1024px'; |
| 13 | + this.glCanvas.style.display = 'block'; |
| 14 | + |
| 15 | + // Center in container |
| 16 | + container.appendChild(this.glCanvas); |
| 17 | + |
| 18 | + // Init WebGL with correct size |
| 19 | + this.gl = this.glCanvas.getContext('webgl2', { |
| 20 | + premultipliedAlpha: false, |
| 21 | + alpha: false |
| 22 | + }); |
| 23 | + |
| 24 | + this.createShaders(); |
| 25 | + this.createBuffers(); |
| 26 | + this.createTexture(); |
| 27 | + } |
| 28 | + |
| 29 | + createShaders() { |
| 30 | + const vsSource = `#version 300 es |
| 31 | + in vec2 a_position; |
| 32 | + in vec2 a_texCoord; |
| 33 | + out vec2 v_texCoord; |
| 34 | + void main() { |
| 35 | + gl_Position = vec4(a_position, 0, 1); |
| 36 | + v_texCoord = a_texCoord; |
| 37 | + }`; |
| 38 | + |
| 39 | + const fsSource = `#version 300 es |
| 40 | + precision highp float; |
| 41 | + |
| 42 | + uniform sampler2D u_image; |
| 43 | + uniform vec2 u_resolution; |
| 44 | + uniform float u_time; |
| 45 | + |
| 46 | + in vec2 v_texCoord; |
| 47 | + out vec4 outColor; |
| 48 | +
|
| 49 | + #define SCANLINE_INTENSITY 0.1 |
| 50 | + #define VIGNETTE_STRENGTH 0.2 |
| 51 | + |
| 52 | + void main() { |
| 53 | + vec2 uv = v_texCoord; |
| 54 | + |
| 55 | + // Screen curve |
| 56 | + vec2 curve_uv = uv * 2.0 - 1.0; |
| 57 | + vec2 offset = curve_uv.yx * curve_uv.yx * vec2(0.075, 0.075); |
| 58 | + curve_uv += curve_uv * offset; |
| 59 | + uv = curve_uv * 0.5 + 0.5; |
| 60 | +
|
| 61 | + // Check bounds |
| 62 | + if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { |
| 63 | + outColor = vec4(0.0, 0.0, 0.0, 1.0); |
| 64 | + return; |
| 65 | + } |
| 66 | +
|
| 67 | + // Sample with subtle RGB shift |
| 68 | + float r = texture(u_image, uv + vec2(0.001, 0.0)).r; |
| 69 | + float g = texture(u_image, uv).g; |
| 70 | + float b = texture(u_image, uv - vec2(0.001, 0.0)).b; |
| 71 | + vec4 color = vec4(r, g, b, 1.0); |
| 72 | + |
| 73 | + // Scanlines |
| 74 | + float scanline = sin(uv.y * u_resolution.y * 1.5) * 0.5 + 0.5; |
| 75 | + color *= 1.0 - scanline * SCANLINE_INTENSITY; |
| 76 | + |
| 77 | + // Vignette |
| 78 | + float vignette = 1.0 - length(curve_uv) * VIGNETTE_STRENGTH; |
| 79 | + color *= vignette; |
| 80 | + |
| 81 | + // Subtle noise |
| 82 | + float noise = fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453); |
| 83 | + color *= 0.98 + noise * 0.02; |
| 84 | +
|
| 85 | + outColor = color; |
| 86 | + }`; |
| 87 | + |
| 88 | + const vertexShader = this.createShader(this.gl.VERTEX_SHADER, vsSource); |
| 89 | + const fragmentShader = this.createShader(this.gl.FRAGMENT_SHADER, fsSource); |
| 90 | + |
| 91 | + this.program = this.createProgram(vertexShader, fragmentShader); |
| 92 | + |
| 93 | + // Get locations |
| 94 | + this.positionLoc = this.gl.getAttribLocation(this.program, 'a_position'); |
| 95 | + this.texCoordLoc = this.gl.getAttribLocation(this.program, 'a_texCoord'); |
| 96 | + this.resolutionLoc = this.gl.getUniformLocation(this.program, 'u_resolution'); |
| 97 | + this.timeLoc = this.gl.getUniformLocation(this.program, 'u_time'); |
| 98 | + } |
| 99 | + |
| 100 | + createBuffers() { |
| 101 | + // Vertex positions |
| 102 | + const positions = new Float32Array([ |
| 103 | + -1, -1, // bottom left |
| 104 | + 1, -1, // bottom right |
| 105 | + -1, 1, // top left |
| 106 | + 1, 1, // top right |
| 107 | + ]); |
| 108 | + |
| 109 | + this.positionBuffer = this.gl.createBuffer(); |
| 110 | + this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positionBuffer); |
| 111 | + this.gl.bufferData(this.gl.ARRAY_BUFFER, positions, this.gl.STATIC_DRAW); |
| 112 | + |
| 113 | + // Texture coordinates |
| 114 | + const texCoords = new Float32Array([ |
| 115 | + 0, 1, // bottom left |
| 116 | + 1, 1, // bottom right |
| 117 | + 0, 0, // top left |
| 118 | + 1, 0, // top right |
| 119 | + ]); |
| 120 | + |
| 121 | + this.texCoordBuffer = this.gl.createBuffer(); |
| 122 | + this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.texCoordBuffer); |
| 123 | + this.gl.bufferData(this.gl.ARRAY_BUFFER, texCoords, this.gl.STATIC_DRAW); |
| 124 | + } |
| 125 | + |
| 126 | + createTexture() { |
| 127 | + this.texture = this.gl.createTexture(); |
| 128 | + this.gl.bindTexture(this.gl.TEXTURE_2D, this.texture); |
| 129 | + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE); |
| 130 | + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE); |
| 131 | + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR); |
| 132 | + this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR); |
| 133 | + } |
| 134 | + |
| 135 | + createShader(type, source) { |
| 136 | + const shader = this.gl.createShader(type); |
| 137 | + this.gl.shaderSource(shader, source); |
| 138 | + this.gl.compileShader(shader); |
| 139 | + if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) { |
| 140 | + throw new Error(this.gl.getShaderInfoLog(shader)); |
| 141 | + } |
| 142 | + return shader; |
| 143 | + } |
| 144 | + |
| 145 | + createProgram(vertexShader, fragmentShader) { |
| 146 | + const program = this.gl.createProgram(); |
| 147 | + this.gl.attachShader(program, vertexShader); |
| 148 | + this.gl.attachShader(program, fragmentShader); |
| 149 | + this.gl.linkProgram(program); |
| 150 | + if (!this.gl.getProgramParameter(program, this.gl.LINK_STATUS)) { |
| 151 | + throw new Error(this.gl.getProgramInfoLog(program)); |
| 152 | + } |
| 153 | + return program; |
| 154 | + } |
| 155 | + |
| 156 | + setScale(scale) { |
| 157 | + this.glCanvas.style.transform = `scale(${scale})`; |
| 158 | + } |
| 159 | + |
| 160 | + render(time) { |
| 161 | + const gl = this.gl; |
| 162 | + |
| 163 | + // Ensure viewport matches fixed canvas size |
| 164 | + gl.viewport(0, 0, 1024, 1024); |
| 165 | + gl.clearColor(0, 0, 0, 0); |
| 166 | + gl.clear(gl.COLOR_BUFFER_BIT); |
| 167 | + |
| 168 | + gl.useProgram(this.program); |
| 169 | + |
| 170 | + // Upload game canvas as texture |
| 171 | + gl.bindTexture(gl.TEXTURE_2D, this.texture); |
| 172 | + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this.gameCanvas); |
| 173 | + |
| 174 | + // Set uniforms |
| 175 | + gl.uniform2f(this.resolutionLoc, gl.canvas.width, gl.canvas.height); |
| 176 | + gl.uniform1f(this.timeLoc, time * 0.001); |
| 177 | + |
| 178 | + // Set attributes |
| 179 | + gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer); |
| 180 | + gl.enableVertexAttribArray(this.positionLoc); |
| 181 | + gl.vertexAttribPointer(this.positionLoc, 2, gl.FLOAT, false, 0, 0); |
| 182 | + |
| 183 | + gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer); |
| 184 | + gl.enableVertexAttribArray(this.texCoordLoc); |
| 185 | + gl.vertexAttribPointer(this.texCoordLoc, 2, gl.FLOAT, false, 0, 0); |
| 186 | + |
| 187 | + // Draw |
| 188 | + gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +export default CRTEffect; |
0 commit comments