Certainly! Below is a fragment shader that emulates a CRT (Cathode Ray Tube) screen effect using OpenGL ES 3.0. This shader includes features such as scanlines, chromatic aberration, screen curvature (barrel distortion), and subtle noise to mimic the vintage CRT look.
Shader Features and Uniforms
-
Scanlines:
- Purpose: Simulates the horizontal lines seen on CRT monitors.
- Implementation: Alternates the brightness of each horizontal line based on its y-coordinate.
-
Chromatic Aberration:
- Purpose: Mimics the slight color separation (red, green, blue) inherent in CRT displays.
- Implementation: Offsets each color channel slightly in different directions.
-
Barrel Distortion (Screen Curvature):
- Purpose: Adds a subtle curvature to the screen edges, characteristic of CRT monitors.
- Implementation: Applies a radial distortion to the texture coordinates.
-
Noise:
- Purpose: Introduces a faint static to enhance the vintage feel.
- Implementation: Adds procedural noise based on the fragment position and time.
Uniforms:
-
Existing Uniforms:
uniform sampler2D uSampler0; — The input texture to which the CRT effect is applied.
uniform vec2 resolution; — The resolution of the screen (width, height).
uniform float time; — A time variable to animate the noise.
-
Additional Uniforms:
-
uniform float distortion;
Description: Controls the amount of barrel distortion. Typical values range from 0.0 (no distortion) to 0.5.
-
uniform float scanlineIntensity;
Description: Adjusts the brightness variation of the scanlines. Range: 0.0 (no scanlines) to 1.0 (full intensity).
-
uniform float chromAbShift;
Description: Sets the amount of chromatic aberration. Range: 0.0 (no shift) to 0.01 for subtle separation.
Fragment Shader Code:
#version 300 es
precision mediump float;
uniform sampler2D uSampler0;
uniform vec2 resolution;
uniform float time;
// Additional Uniforms
uniform float distortion; // e.g., 0.1
uniform float scanlineIntensity; // e.g., 0.5
uniform float chromAbShift; // e.g., 0.003
in vec2 vTexCoord;
out vec4 fragColor;
// Simple hash function for noise
float hash(vec2 p) {
return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
}
void main() {
// Normalize coordinates to range [-1, 1]
vec2 centeredCoord = (vTexCoord * 2.0) - 1.0;
// Apply barrel distortion
float dist = length(centeredCoord);
vec2 distortedCoord = centeredCoord * (1.0 + distortion * pow(dist, 2.0));
// Convert back to [0,1] range
vec2 finalCoord = (distortedCoord + 1.0) / 2.0;
// Prevent sampling outside the texture
if(finalCoord.x < 0.0 || finalCoord.x > 1.0 || finalCoord.y < 0.0 || finalCoord.y > 1.0){
fragColor = vec4(0.0);
return;
}
// Chromatic aberration: sample red, green, blue channels with slight offsets
vec2 redOffset = finalCoord + vec2(chromAbShift, 0.0);
vec2 greenOffset = finalCoord;
vec2 blueOffset = finalCoord + vec2(-chromAbShift, 0.0);
float r = texture(uSampler0, redOffset).r;
float g = texture(uSampler0, greenOffset).g;
float b = texture(uSampler0, blueOffset).b;
vec3 color = vec3(r, g, b);
// Apply scanlines
float scanline = sin(finalCoord.y * resolution.y * 3.14159) * 0.5 + 0.5; // Adjust frequency as needed
color *= mix(1.0, scanline, scanlineIntensity);
// Add noise
float noise = hash(floor(finalCoord * resolution.xy)) * 0.05; // Adjust noise intensity
color += noise;
// Optional: Clamp the color to [0,1]
color = clamp(color, 0.0, 1.0);
fragColor = vec4(color, 1.0);
}
Explanation of the Shader Stages:
-
Normalization and Barrel Distortion:
- The texture coordinates
vTexCoord are first normalized to a range of [-1, 1] to center the distortion effect.
- Barrel distortion is applied based on the
distortion uniform and the radial distance from the center.
- The distorted coordinates are then converted back to the
[0, 1] range for texture sampling.
-
Chromatic Aberration:
- Separate offsets are applied to the red and blue channels using the
chromAbShift uniform to create slight color separations.
- Green remains unshifted for a natural look.
-
Scanlines:
- A sine function modulates the brightness of each horizontal line, creating the scanline effect.
- The frequency of the scanlines is determined by multiplying the y-coordinate by the screen height and π.
scanlineIntensity controls how pronounced the scanlines appear.
-
Noise:
- A simple hash function generates procedural noise based on the fragment's position and time.
- This noise is subtly added to the color to emulate the static often seen on CRT displays.
-
Final Color Composition:
- The color is clamped to ensure it remains within the valid
[0, 1] range.
- The final color is output with full opacity.
Usage Notes:
Feel free to adjust the shader parameters to better fit the specific aesthetics you aim to achieve with the CRT effect!