Advertisement
Guest User

Dithered Opacity Shader For Godot 3.1

a guest
Jun 6th, 2019
2,787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // My custom shader for implementing dithered alpha
  2. shader_type spatial;
  3. render_mode blend_mix,depth_draw_opaque,cull_disabled,diffuse_burley,specular_schlick_ggx;
  4.  
  5. // Texture maps
  6. uniform sampler2D texture_albedo : hint_albedo;
  7. uniform sampler2D texture_masks : hint_white;
  8. uniform sampler2D texture_normal : hint_normal;
  9.  
  10. // Parameters
  11. uniform vec4 albedo : hint_color;
  12. uniform float specular = 0.5;
  13. uniform float metallic = 0.0;
  14. uniform float roughness : hint_range(0,1) = 0.5;
  15. uniform float normal_strength : hint_range(0,2) = 1.0;
  16. uniform float alpha_clip: hint_range(0,1) = 0.0;
  17.  
  18. void fragment() {
  19.     vec4 albedo_tex = texture(texture_albedo, UV);
  20.     vec4 masks_tex = texture(texture_masks, UV);
  21.     float alpha = albedo_tex.a;
  22.        
  23.     ALBEDO = albedo.rgb * albedo_tex.rgb;
  24.     METALLIC = metallic * masks_tex.r;
  25.     ROUGHNESS = roughness * masks_tex.g;
  26.     NORMALMAP = texture(texture_normal, UV).rgb;
  27.     NORMALMAP_DEPTH = normal_strength;
  28.    
  29.     // Fancy dithered alpha stuff
  30.     float opacity = albedo_tex.a;
  31.     int x = int(FRAGCOORD.x) % 4;
  32.     int y = int(FRAGCOORD.y) % 4;
  33.     int index = x + y * 4;
  34.     float limit = 0.0;
  35.  
  36.     if (x < 8) {
  37.         if (index == 0) limit = 0.0625;
  38.         if (index == 1) limit = 0.5625;
  39.         if (index == 2) limit = 0.1875;
  40.         if (index == 3) limit = 0.6875;
  41.         if (index == 4) limit = 0.8125;
  42.         if (index == 5) limit = 0.3125;
  43.         if (index == 6) limit = 0.9375;
  44.         if (index == 7) limit = 0.4375;
  45.         if (index == 8) limit = 0.25;
  46.         if (index == 9) limit = 0.75;
  47.         if (index == 10) limit = 0.125;
  48.         if (index == 11) limit = 0.625;
  49.         if (index == 12) limit = 1.0;
  50.         if (index == 13) limit = 0.5;
  51.         if (index == 14) limit = 0.875;
  52.         if (index == 15) limit = 0.375;
  53.     }
  54.     // Is this pixel below the opacity limit? Skip drawing it
  55.     if (opacity < limit || opacity < alpha_clip)
  56.         discard;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement