Skip to content

Simple screen space shaders made to learn more about shading techniques.

License

Notifications You must be signed in to change notification settings

AbstractBorderStudio/ScreenSpacePostProcessing

Repository files navigation

ScreenSpacePostProcessing

TL;DR

Simple screen space shaders made to learn more about shading techniques.

Pixelation effect

Pixelation effect

// pixelation effect
// create a quad and apply this shader to it
// in the quad, flip faces and scale it to (2,2)
shader_type spatial;
render_mode unshaded;

uniform sampler2D _screen : source_color, hint_screen_texture, filter_nearest;
uniform float _scale = 256.0;

vec3 pixelate(sampler2D tex, vec2 uv) {
  uv = floor(uv * _scale) / _scale;
  return texture(tex, uv).rgb;
}

void vertex() {
  // this code makes the quad full screen always in front of the camera
  POSITION = vec4(VERTEX, 1.0);
}

void fragment() {
  ALBEDO = pixelate(_screen, SCREEN_UV);
}

This can also be used to downscale a texture.

CREDITS

Dithering effect

It uses a bayer pattern (with a nearest filtering) to compare each pixel from the screen texture to assign a value.

// sample textures
vec2 ratio = normalize(VIEWPORT_SIZE);
vec3 p = texture(_pattern, SCREEN_UV *_dither_scale * ratio).rgb;
vec3 n = texture(_noise, SCREEN_UV *_dither_scale).rgb;
vec3 s = pixelate(_screen, SCREEN_UV);

// compute luminosity using yuv color space
float lum = (s.r * 0.299) + (s.g * 0.587) + (s.b * 0.114); // luminosity of screen texture pixel
float sh = (p.r * 0.299) + (p.g * 0.587) + (p.b * 0.114); // luminosity of bayer pattern pixel
float nh = (n.r * 0.299) + (n.g * 0.587) + (n.b * 0.114); // luminosity of noise pixel

// reduce bit depth
lum = floor(lum * float(bits)) / float(bits);

// compare pixels
vec3 o;
if (lum > sh * _thresh) {
	o = s;
}
else {
	o = _shadow_col * _shadow_strength;
}
ALBEDO = o;

CREDITS

About

Simple screen space shaders made to learn more about shading techniques.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published