Advertisement
Zenn_

occtolights.glsl

Oct 7th, 2020
2,828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 450
  2. //transforms the occlusion map into a lightmap
  3. //that can be multiblended onto the screen buffer
  4.  
  5. //texture with solidity information
  6. uniform sampler2D occlusion_map;
  7. //color of ambient light
  8. uniform vec3 ambient;
  9. //dimensions of the buffers (occlusion map and cast texture should have same size)
  10. uniform vec2 dimensions;
  11. //aspect ratio, for example 0.5625 for 1280x720
  12. float aspect;
  13.  
  14. //define amount of lights to be processed
  15. uniform int light_amount;
  16. //define all light property uniforms
  17. #define DL(INDEX) \
  18. uniform vec3 light##INDEX##ob; \
  19. uniform vec3 light##INDEX##c;
  20. DL(0)
  21. DL(1)
  22. DL(2)
  23. DL(3)
  24. DL(4)
  25. DL(5)
  26. DL(6)
  27. DL(7)
  28. DL(8)
  29. DL(9)
  30. DL(10)
  31. DL(11)
  32. DL(12)
  33. DL(13)
  34. DL(14)
  35. DL(15)
  36.  
  37. //define function that sets "light" class instance to uniform params
  38. #define FETCHLIGHT(ID) \
  39.     if(light_index == ID)\
  40.     {\
  41.         light.origin        = light##ID##ob.rg;\
  42.         light.color         = light##ID##c.rgb;\
  43.         light.brightness    = light##ID##ob.b;\
  44.     }
  45.  
  46. //checks whether the pixel is in a wall or if it can be illuminated
  47. bool isOccluded(vec2 uv)
  48. {
  49.     return texture2D(occlusion_map, vec2(uv.x, 1.0 - uv.y)).r > 0.1;
  50. }
  51.  
  52. //max iteration steps for function below
  53. #define MAX_STEPS 300.0
  54. #define EPS 0.00001
  55. //calculates whether the tar-get position can be reached without hitting an occluder
  56. bool canBeReached(vec2 orig, vec2 tar)
  57. {
  58.     vec2 path = tar - orig;
  59.     //distance between light and pixel
  60.     highp float maxdistance = length(path);
  61.     //amount of occlusion pixels to sample along the way
  62.     int sample_amount = int(round(MAX_STEPS * maxdistance));
  63.     //distance the ray reaches from the origin before hitting a solid pixel
  64.     highp float d;
  65.    
  66.     for(int i = 0;i < sample_amount; i++)
  67.     {
  68.         //how much along path?
  69.         d = float(i) / float(sample_amount);
  70.         //where along the path to sample from?
  71.         //                  origin  path   distance along path
  72.         vec2 sample_point = orig + (path * d);
  73.         //check if point along path is occluded
  74.         bool occluded = !isOccluded(sample_point);
  75.        
  76.         //break condition: pixel is black/occluded
  77.         if(occluded)
  78.         {
  79.             return false;  
  80.         }
  81.     }
  82.  
  83.     return true;
  84. }
  85.  
  86. //Light struct for light info
  87. struct Light
  88. {
  89.     vec2 origin;
  90.     vec3 color;
  91.     float brightness;
  92. };
  93.  
  94. void main(){
  95.     //set aspect ratio
  96.     aspect = dimensions.y / dimensions.x;
  97.     //for this pixel...
  98.     vec2 this_pixel = vec2(gl_FragCoord.x, gl_FragCoord.y);
  99.     //final color of pixel, this will be modified down below
  100.     vec3 final_color = ambient;
  101.     //check if its occluded, if its not...
  102.     if(!isOccluded(this_pixel / dimensions)){
  103.         gl_FragColor = vec4(ambient * 0.5, 1.0);
  104.     }
  105.     else{
  106.         vec2 this_pixel_ratiod = vec2(this_pixel.x, this_pixel.y * aspect);
  107.         vec2 light_origin_ratiod;
  108.         float distance_between_light_and_pixel;
  109.         float distance_between_light_and_pixel_ratiod;
  110.         float distance_between_light_and_occluder;
  111.         //check for all the lights...
  112.         for(int light_index = 0; light_index < light_amount; light_index++){
  113.             //well, grab the light info first, but then...
  114.             Light light;
  115.             FETCHLIGHT(0)
  116.             else FETCHLIGHT(1)
  117.             else FETCHLIGHT(2)
  118.             else FETCHLIGHT(3)
  119.             else FETCHLIGHT(4)
  120.             else FETCHLIGHT(5)
  121.             else FETCHLIGHT(6)
  122.             else FETCHLIGHT(7)
  123.             else FETCHLIGHT(8)
  124.             else FETCHLIGHT(9)
  125.             else FETCHLIGHT(10)
  126.             else FETCHLIGHT(11)
  127.             else FETCHLIGHT(12)
  128.             else FETCHLIGHT(13)
  129.             else FETCHLIGHT(14)
  130.             else FETCHLIGHT(15)
  131.  
  132.            
  133.             //calculate distance between light and this pixel
  134.             /////distance_between_light_and_pixel = length(light.origin - (this_pixel / dimensions));
  135.             //calculate distance between light and this pixel (for this, consider aspect ratio!)
  136.             light_origin_ratiod = light.origin * vec2(1.0, aspect);
  137.             //light_origin_ratiod = vec2(light.origin.x, light.origin.y * aspect);
  138.             distance_between_light_and_pixel_ratiod = length((this_pixel_ratiod / dimensions) - light_origin_ratiod);
  139.             //calculate distance between light and first occluded pixel in direction of this_pixel
  140.             /////distance_between_light_and_occluder = MarchShadow((this_pixel / dimensions), (this_pixel / dimensions) - light.origin);
  141.             //check if this light ray even reaches this pixel
  142.             if(canBeReached(light.origin, (this_pixel / dimensions))){
  143.                 //determine light falloff!
  144.                 final_color += light.color * pow((1.0 - distance_between_light_and_pixel_ratiod), 5);
  145.                 //done!
  146.             }
  147.         }
  148.         gl_FragColor = vec4(final_color, 1.0);
  149.     }
  150. }
  151.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement