Advertisement
Guest User

godot dither quantization

a guest
Dec 11th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. shader_type canvas_item;
  2.  
  3. const float PaletteRGBSize = 4.;
  4. const float ResolutionDivisor = 2.;
  5.  
  6. float quantize(float inp, float period)
  7. {
  8.     return floor((inp+period/2.)/period)*period;
  9. }
  10.  
  11. float bayer8x8(vec2 uvScreenSpace, sampler2D image_texture)
  12. {
  13.     return texture(image_texture, uvScreenSpace/(ResolutionDivisor*8.)).r;
  14. }
  15.  
  16. vec3 getSceneColor(vec2 uv, sampler2D screen_texture)
  17. {
  18.     return texture(screen_texture, uv).rgb;
  19. }
  20.  
  21. void fragment()
  22. {    
  23.     vec3 quantizationPeriod = vec3(1./(PaletteRGBSize-1.));
  24.    
  25.     vec2 uvPixellated = floor(FRAGCOORD.xy / ResolutionDivisor)*ResolutionDivisor;
  26.    
  27.     vec3 dc = getSceneColor(uvPixellated / SCREEN_PIXEL_SIZE.xy, SCREEN_TEXTURE);
  28.    
  29.     dc += (bayer8x8(FRAGCOORD.xy, TEXTURE)-.5)*(quantizationPeriod);
  30.    
  31.     dc = vec3(
  32.         quantize(dc.r, quantizationPeriod.r),
  33.         quantize(dc.g, quantizationPeriod.g),
  34.         quantize(dc.b, quantizationPeriod.b)
  35.     );
  36.     COLOR = vec4(dc, 1.);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement