Advertisement
Guest User

Untitled

a guest
Aug 4th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #shader vertex
  2.  
  3. void main(void){
  4.  
  5. }
  6.  
  7. #shader fragment
  8.  
  9. #version 330 core
  10.  
  11. #ifdef GL_ES
  12. #define LOWP lowp
  13. precision mediump float;
  14. #else
  15. #define LOWP
  16. #endif
  17.  
  18. #define PI 3.14
  19. varying vec2 vTexCoord0;
  20. varying LOWP vec4 vColor;
  21.  
  22. uniform sampler2D u_texture;
  23. uniform vec2 resolution;
  24.  
  25. uniform float softShadows;
  26.  
  27. //sample from the distance map
  28. float sample(vec2 coord, float r) {
  29.   return step(r, texture2D(u_texture, coord).r);
  30. }
  31.  
  32. void main(void) {
  33.     //rectangular to polar
  34.     vec2 norm = vTexCoord0.st * 2.0 - 1.0;
  35.     float theta = atan(norm.y, norm.x);
  36.     float r = length(norm);
  37.     float coord = (theta + PI) / (2.0*PI);
  38.  
  39.     //the tex coord to sample our 1D lookup texture
  40.     //always 0.0 on y axis
  41.     vec2 tc = vec2(coord, 0.0);
  42.  
  43.     //the center tex coord, which gives us hard shadows
  44.     float center = sample(vec2(tc.x, tc.y), r);
  45.  
  46.     //we multiply the blur amount by our distance from center
  47.     //this leads to more blurriness as the shadow "fades away"
  48.     float blur = (1./resolution.x)  * smoothstep(0., 1., r);
  49.  
  50.     //now we use a simple gaussian blur
  51.     float sum = 0.0;
  52.  
  53.     sum += sample(vec2(tc.x - 4.0*blur, tc.y), r) * 0.05;
  54.     sum += sample(vec2(tc.x - 3.0*blur, tc.y), r) * 0.09;
  55.     sum += sample(vec2(tc.x - 2.0*blur, tc.y), r) * 0.12;
  56.     sum += sample(vec2(tc.x - 1.0*blur, tc.y), r) * 0.15;
  57.  
  58.     sum += center * 0.16;
  59.  
  60.     sum += sample(vec2(tc.x + 1.0*blur, tc.y), r) * 0.15;
  61.     sum += sample(vec2(tc.x + 2.0*blur, tc.y), r) * 0.12;
  62.     sum += sample(vec2(tc.x + 3.0*blur, tc.y), r) * 0.09;
  63.     sum += sample(vec2(tc.x + 4.0*blur, tc.y), r) * 0.05;
  64.  
  65.     //1.0 -> in light, 0.0 -> in shadow
  66.     float lit = mix(center, sum, softShadows);
  67.  
  68.     //multiply the summed amount by our distance, which gives us a radial falloff
  69.     //then multiply by vertex (light) color
  70.     gl_FragColor = vColor * vec4(vec3(1.0), lit * smoothstep(1.0, 0.0, r));
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement