|
2 | 2 |
|
3 | 3 | import React, { useEffect, useRef } from 'react'; |
4 | 4 | import "./pixelatedBackground.css"; |
| 5 | +import { useSecret } from '@src/context/SecretContext'; |
5 | 6 |
|
6 | | -// 2. Define Vertex Shader (Draws a full-screen quad) |
7 | | -const vsSource = ` |
| 7 | +export default function PixelatedBackground() { |
| 8 | + const canvasRef = useRef<HTMLCanvasElement | null>(null); |
| 9 | + const contextRef = useRef<WebGLRenderingContext | null>(null); |
| 10 | + const shaderProgramRef = useRef<WebGLProgram | null>(null); |
| 11 | + const positionLocationRef = useRef<number | null>(null); |
| 12 | + const timeUniformLocationRef = useRef<WebGLUniformLocation | null>(null); |
| 13 | + const resolutionUniformLocationRef = useRef<WebGLUniformLocation | null>(null); |
| 14 | + const positionBufferRef = useRef<WebGLBuffer | null>(null); |
| 15 | + const frameRef = useRef<number | null>(null); |
| 16 | + const { isMissingNoSecretActive, is404SecretActive } = useSecret(); |
| 17 | + |
| 18 | + // 2. Define Vertex Shader (Draws a full-screen quad) |
| 19 | + const vsSource = ` |
8 | 20 | attribute vec2 position; |
9 | 21 | void main() { |
10 | 22 | gl_Position = vec4(position, 0.0, 1.0); |
11 | 23 | } |
12 | 24 | `; |
13 | 25 |
|
14 | | -// 3. Define Fragment Shader (Generates pixelated procedural waves) |
15 | | -const fsSource = ` |
16 | | - precision mediump float; |
17 | | - uniform vec2 u_resolution; |
18 | | - uniform float u_time; |
| 26 | + // 3. Define Fragment Shader (Generates pixelated procedural waves) |
| 27 | + const defaultfsSource = ` |
| 28 | +precision mediump float; |
| 29 | +uniform vec2 u_resolution; |
| 30 | +uniform float u_time; |
19 | 31 |
|
20 | | - void main() { |
21 | | - // Pixel grid sizing: Adjust this number to change pixel size |
22 | | - float pixelSize = 12.0; // 8.0 |
| 32 | +void main() { |
| 33 | + // Pixel grid sizing: Adjust this number to change pixel size |
| 34 | + float pixelSize = 12.0; // 8.0 |
23 | 35 |
|
24 | | - // Quantize coordinates to create the pixelated effect |
25 | | - vec2 uv = floor(gl_FragCoord.xy / pixelSize) * pixelSize / u_resolution.xy; |
| 36 | + // Quantize coordinates to create the pixelated effect |
| 37 | + vec2 uv = floor(gl_FragCoord.xy / pixelSize) * pixelSize / u_resolution.xy; |
26 | 38 |
|
27 | | - // Create an example retro procedural animation pattern |
28 | | - float colorA = sin(uv.x * 4.0 + u_time) * 0.5 + 0.5; |
29 | | - float colorB = cos(uv.y * 4.0 - u_time) * 0.5 + 0.5; |
| 39 | + // Create an example retro procedural animation pattern |
| 40 | + float colorA = sin(uv.x * 4.0 + u_time) * 0.5 + 0.5; |
| 41 | + float colorB = cos(uv.y * 4.0 - u_time) * 0.5 + 0.5; |
30 | 42 |
|
31 | | - gl_FragColor = vec4(colorA, colorB, 0.8, 1.0); |
| 43 | + gl_FragColor = vec4(colorA, colorB, 0.8, 1.0); |
| 44 | +} |
| 45 | +`; |
| 46 | + |
| 47 | + const missingNofsSource = ` |
| 48 | +precision mediump float; |
| 49 | +uniform vec2 u_resolution; |
| 50 | +uniform float u_time; |
| 51 | +
|
| 52 | +// Retro Pseudorandom Hash Function |
| 53 | +float rand(vec2 co) { |
| 54 | + return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); |
| 55 | +} |
| 56 | +
|
| 57 | +void main() { |
| 58 | + // 1. Define gameboy/retro-esque MissingNo. hex colors |
| 59 | + vec4 colorWhite = vec4(0.93, 0.93, 0.88, 0.5); |
| 60 | + vec4 colorLight = vec4(0.68, 0.65, 0.70, 0.5); // Iconic light purple/gray |
| 61 | + vec4 colorDark = vec4(0.40, 0.38, 0.35, 0.5); // Fawn / dark gray |
| 62 | + vec4 colorBlack = vec4(0.08, 0.08, 0.10, 0.5); |
| 63 | +
|
| 64 | + // 2. Set pixel sizes to look like a raw Game Boy VRAM dump |
| 65 | + // MissingNo blocks are wider horizontally than they are tall! |
| 66 | + vec2 blockDimensions = vec2(8.0, 4.0); //vec2(32.0, 12.0); |
| 67 | +
|
| 68 | + // Get base pixel coordinates |
| 69 | + vec2 blockCoord = floor(gl_FragCoord.xy / blockDimensions); |
| 70 | +
|
| 71 | + // 3. Inject horizontal shifting to simulate data misalignment |
| 72 | + // Every horizontal row shifts left or right based on time and row hash |
| 73 | + float shiftTime = floor(u_time * 2.0); // 8.0 Steps smoothly like 8-bit frames |
| 74 | + float rowShift = floor(rand(vec2(blockCoord.y, shiftTime)) * 12.0) - 6.0; |
| 75 | + blockCoord.x += rowShift; |
| 76 | +
|
| 77 | + // 4. Generate the glitch sequence hash value |
| 78 | + float noiseValue = rand(blockCoord); |
| 79 | +
|
| 80 | + // 5. Build an abstract shape bounding system |
| 81 | + // MissingNo looks like a rough 'reversed L' block shape |
| 82 | + vec2 uv = gl_FragCoord.xy / u_resolution.xy; |
| 83 | +
|
| 84 | + // Generate random empty transparent holes to break the background up |
| 85 | + float maskingNoise = rand(floor(gl_FragCoord.xy / vec2(64.0, 64.0)) + shiftTime); |
| 86 | + if (maskingNoise > 0.82) { |
| 87 | + //discard; // Erases sections dynamically to replicate broken sprite data |
32 | 88 | } |
| 89 | +
|
| 90 | + // 6. Map the raw float noise cleanly to the 4 discrete colors |
| 91 | + vec4 finalColor; |
| 92 | + if (noiseValue < 0.25) { |
| 93 | + finalColor = colorBlack; |
| 94 | + } else if (noiseValue < 0.50) { |
| 95 | + finalColor = colorDark; |
| 96 | + } else if (noiseValue < 0.75) { |
| 97 | + finalColor = colorLight; |
| 98 | + } else { |
| 99 | + finalColor = colorWhite; |
| 100 | + } |
| 101 | +
|
| 102 | + gl_FragColor = finalColor; |
| 103 | +} |
33 | 104 | `; |
34 | 105 |
|
35 | | -export default function PixelatedBackground() { |
36 | | - const canvasRef = useRef<HTMLCanvasElement | null>(null); |
37 | | - const contextRef = useRef<WebGLRenderingContext | null>(null); |
38 | | - const shaderProgramRef = useRef<WebGLProgram | null>(null); |
39 | | - const positionLocationRef = useRef<number | null>(null); |
40 | | - const timeUniformLocationRef = useRef<WebGLUniformLocation | null>(null); |
41 | | - const resolutionUniformLocationRef = useRef<WebGLUniformLocation | null>(null); |
42 | | - const positionBufferRef = useRef<WebGLBuffer | null>(null); |
43 | | - const frameRef = useRef<number | null>(null); |
| 106 | + const sh1fsSource = ` |
| 107 | +precision mediump float; |
| 108 | +uniform vec2 u_resolution; |
| 109 | +uniform float u_time; |
| 110 | +
|
| 111 | +// Retro Pseudorandom Hash Function |
| 112 | +float rand(vec2 co) { |
| 113 | + return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); |
| 114 | +} |
| 115 | +
|
| 116 | +// 2D Noise for organic, rolling fog layers |
| 117 | +float noise(vec2 st) { |
| 118 | + vec2 i = floor(st); |
| 119 | + vec2 f = fract(st); |
| 120 | + float a = rand(i); |
| 121 | + float b = rand(i + vec2(1.0, 0.0)); |
| 122 | + float c = rand(i + vec2(0.0, 1.0)); |
| 123 | + float d = rand(i + vec2(1.0, 1.0)); |
| 124 | + vec2 u = f * f * (3.0 - 2.0 * f); |
| 125 | + return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y; |
| 126 | +} |
| 127 | +
|
| 128 | +void main() { |
| 129 | + // 1. Core PS1 Silent Hill Color Palette (Industrial, grime-tinted grays) |
| 130 | + vec3 fogDark = vec3(0.18, 0.18, 0.19); // Deep, oppressive background fog |
| 131 | + vec3 fogLight = vec3(0.42, 0.41, 0.40); // Ash-choked daylight sky fog |
| 132 | + vec3 ashColor = vec3(0.55, 0.55, 0.53); // Drifting burnt flakes |
| 133 | +
|
| 134 | + // 2. Pixelation Grid |
| 135 | + float pixelSize = 4.0; |
| 136 | + vec2 pixelCoord = floor(gl_FragCoord.xy / pixelSize) * pixelSize; |
| 137 | + vec2 uv = pixelCoord / u_resolution.xy; |
| 138 | +
|
| 139 | + // 3. Procedural Fog Simulation (Layered rolling noise) |
| 140 | + vec2 fogUV = uv * 3.0; |
| 141 | + fogUV.y -= u_time * 0.05; // Slow upward/forward drifting motion |
| 142 | + fogUV.x += sin(u_time * 0.1 + uv.y) * 0.2; // Eerie swaying swing |
| 143 | + |
| 144 | + float fogDensity = noise(fogUV) * 0.6 + noise(fogUV * 2.5 + u_time * 0.02) * 0.4; |
| 145 | + vec3 finalFog = mix(fogDark, fogLight, fogDensity); |
| 146 | +
|
| 147 | + // 4. PS1 Ordering Dither Matrix Simulation (4x4 retro posterization grid) |
| 148 | + float ditherPattern = mod(pixelCoord.x / pixelSize, 4.0); |
| 149 | + float ditherPatternY = mod(pixelCoord.y / pixelSize, 4.0); |
| 150 | + float ditherThreshold = (ditherPattern * 4.0 + ditherPatternY) / 16.0; |
| 151 | + |
| 152 | + // Crunch the fog colors into dithered gradients |
| 153 | + finalFog += (ditherThreshold - 0.5) * 0.07; |
| 154 | + finalFog = floor(finalFog * 8.0) / 8.0; // Hard clamp to 15-bit color space emulation |
| 155 | +
|
| 156 | + // 5. Slowly Falling Ash Layer |
| 157 | + vec2 ashUV = pixelCoord; |
| 158 | + ashUV.y += u_time * 45.0; // Drifts downwards continuously |
| 159 | + ashUV.x += sin(u_time * 0.5 + pixelCoord.y * 0.02) * 15.0; // Sideways wind swaying |
| 160 | +
|
| 161 | + // Slice screen into tiny discrete particle cell structures |
| 162 | + vec2 ashGrid = floor(ashUV / 16.0); |
| 163 | + float ashSpawnHash = rand(ashGrid); |
| 164 | +
|
| 165 | + float ashParticle = 0.0; |
| 166 | + // Generate individual falling flakes based on threshold matching |
| 167 | + if (ashSpawnHash > 0.96) { |
| 168 | + vec2 centerOffset = fract(ashUV / 16.0) - 0.5; |
| 169 | + // Keep flakes sharp and blocky instead of circular |
| 170 | + if (abs(centerOffset.x) < 0.2 && abs(centerOffset.y) < 0.2) { |
| 171 | + ashParticle = 1.0; |
| 172 | + } |
| 173 | + } |
| 174 | +
|
| 175 | + // 6. Blend Ash over Fog |
| 176 | + vec3 finalColor = mix(finalFog, ashColor, ashParticle * 0.7); |
| 177 | +
|
| 178 | + gl_FragColor = vec4(finalColor, 1.0); |
| 179 | +} |
| 180 | +`; |
44 | 181 |
|
45 | 182 | // Render loop |
46 | 183 | function render(time: number) { |
@@ -79,6 +216,18 @@ export default function PixelatedBackground() { |
79 | 216 | const gl = contextRef.current; |
80 | 217 | if (!gl) return; |
81 | 218 |
|
| 219 | + let fsSource; |
| 220 | + |
| 221 | + if (is404SecretActive) { |
| 222 | + fsSource = sh1fsSource; |
| 223 | + } |
| 224 | + else if (isMissingNoSecretActive) { |
| 225 | + fsSource = missingNofsSource; |
| 226 | + } |
| 227 | + else { |
| 228 | + fsSource = defaultfsSource; |
| 229 | + } |
| 230 | + |
82 | 231 | const vertexShader = createShader(gl, gl.VERTEX_SHADER, vsSource); |
83 | 232 | const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fsSource); |
84 | 233 |
|
|
0 commit comments