cybereality

Godot Quantize Shader

Dec 12th, 2019
834
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. shader_type canvas_item;
  2.  
  3. uniform float PaletteRGBSize = 4.;
  4. uniform float ResolutionDivisor = 2.;
  5. uniform sampler2D FilterTexture; // attach 8x8 texture in editor
  6. uniform vec2 ScreenDim; // set screen width/height in editor
  7.  
  8. float quantize(float inp, float period)
  9. {
  10.     return floor((inp+period/2.)/period)*period;
  11. }
  12.  
  13. float bayer8x8(vec2 uvScreenSpace, sampler2D image_texture)
  14. {
  15.     return texture(image_texture, uvScreenSpace/(ResolutionDivisor*8.)).r;
  16. }
  17.  
  18. vec3 getSceneColor(vec2 uv, sampler2D screen_texture)
  19. {
  20.     return texture(screen_texture, uv).rgb;
  21. }
  22.  
  23. void fragment()
  24. {    
  25.     vec3 quantizationPeriod = vec3(1./(PaletteRGBSize-1.));
  26.    
  27.     vec2 uvPixellated = floor(FRAGCOORD.xy / ResolutionDivisor)*ResolutionDivisor;
  28.    
  29.     vec3 dc = getSceneColor(uvPixellated / ScreenDim, TEXTURE);
  30.    
  31.     dc += (bayer8x8(FRAGCOORD.xy, FilterTexture)-.5)*(quantizationPeriod);
  32.    
  33.     dc = vec3(
  34.         quantize(dc.r, quantizationPeriod.r),
  35.         quantize(dc.g, quantizationPeriod.g),
  36.         quantize(dc.b, quantizationPeriod.b)
  37.     );
  38.     COLOR = vec4(dc, 1.);
  39. }
RAW Paste Data