DavidNorgren

Untitled

Jun 19th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. uniform vec3 lightPosition;
  2. uniform float lightZnear;
  3. uniform float lightZfar;
  4. uniform float lightSize;
  5. uniform vec4 lightColor;
  6. uniform float isGround;
  7. uniform float brightness;
  8. uniform float shadowMapSize;
  9. uniform sampler2D shadowMap;
  10. uniform float samples;
  11. uniform float sampleSize;
  12. uniform float alpha;
  13.  
  14. varying vec3 vertPosition;
  15. varying vec3 vertNormal;
  16. varying vec2 vertTexCoord;
  17. varying vec4 shadowMapCoord;
  18.  
  19. const float pi = 3.14159265;
  20. const float tau = pi * 2.0;
  21. float texelSize = 1.0 / shadowMapSize;
  22.  
  23. float colorToFloat(vec4 c) {
  24.     return c.r + c.g / 255.0 + c.b / (255.0 * 255.0);
  25. }
  26.  
  27. vec4 texture2DBilinear(sampler2D textureSampler, vec2 uv) {
  28.     vec4 tl = texture2D(textureSampler, uv);
  29.     vec4 tr = texture2D(textureSampler, uv + vec2(texelSize, 0.0));
  30.     vec4 bl = texture2D(textureSampler, uv + vec2(0.0, texelSize));
  31.     vec4 br = texture2D(textureSampler, uv + vec2(texelSize , texelSize));
  32.     vec2 f = fract(uv.xy * shadowMapSize); // get the decimal part
  33.     vec4 tA = mix(tl, tr, f.x); // will interpolate the red dot in the image
  34.     vec4 tB = mix(bl, br, f.x); // will interpolate the blue dot in the image
  35.     return mix(tA, tB, f.y); // will interpolate the green dot in the image
  36. }
  37.  
  38. void main() {
  39.     float light, dif = 1.0;
  40.    
  41.     if (isGround < 0.5) dif = max(0.0, dot(normalize(vertNormal), normalize(lightPosition - vertPosition))); // Diffuse
  42.    
  43.     light = dif;
  44.     if (light > 0.0 && shadowMapCoord.w > 0.0 && brightness < 1.0) {
  45.         vec2 tex = vec2((shadowMapCoord.x / shadowMapCoord.z + 1.0) / 2.0, 1.0 - (shadowMapCoord.y / shadowMapCoord.z + 1.0) / 2.0);
  46.         if (tex.x > 0.0 && tex.y > 0.0 && tex.x < 1.0 && tex.y < 1.0) { // Texture position must be valid
  47.             float sampleDepth, depth = shadowMapCoord.z, shadow = 1.0;
  48.             float pixel = texelSize * ((sampleSize * shadowMapSize) / distance(vertPosition, lightPosition));
  49.             for (int i = 1; i < 100; i++) {
  50.                 if (float(i) <= samples) {
  51.                     float angle = tau * (float(i) / samples);
  52.                     sampleDepth = lightZnear + (lightZfar - lightZnear) * colorToFloat(texture2DBilinear(shadowMap, tex + vec2(cos(angle) * pixel, sin(angle) * pixel)));
  53.                     shadow -= clamp(depth - sampleDepth, 0.0, 1.0) / float(samples);
  54.                 } else break;
  55.             }
  56.             light *= shadow;
  57.         }
  58.     }
  59.     gl_FragColor = vec4(mix(lightColor.rgb * light, vec3(1.0, 1.0, 1.0), brightness), alpha*texture2D(gm_BaseTexture, vertTexCoord).a);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment