Advertisement
Zenn_

Untitled

Oct 2nd, 2020
1,942
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 100 //
  54. #define EPS 0.000001
  55. //calculates distance between two vectors than can be traversed before hitting an object
  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.     //distance the ray reaches from the origin before hitting a solid pixel
  62.     highp float d = 0.0;
  63.    
  64.     for(int i = 0;i < MAX_STEPS;i++)
  65.     {
  66.         //fetch red channel from solidityMap at origin + dir*d
  67.         //ds becomes 1 if pixel is white and 0 if pixel is black (cutoff hardcoded as 0.1)
  68.         highp float ds = float(isOccluded(orig + (path * vec2(1.0, aspect) * d)));
  69.         //add to distance
  70.         ds *= maxdistance / MAX_STEPS;
  71.         d += ds;
  72.        
  73.         //safety i assume? <-- break condition: pixel is black
  74.         if(ds < EPS)
  75.         {
  76.             return false;  
  77.         }
  78.     }
  79.  
  80.     return true;
  81. }
  82.  
  83. //Light struct for light info
  84. struct Light
  85. {
  86.     vec2 origin;
  87.     vec3 color;
  88.     float brightness;
  89. };
  90.  
  91. void main(){
  92.     //set aspect ratio
  93.     aspect = dimensions.y / dimensions.x;
  94.     //for this pixel...
  95.     vec2 this_pixel = vec2(gl_FragCoord.x, gl_FragCoord.y);
  96.     //final color of pixel, this will be modified down below
  97.     vec3 final_color = ambient;
  98.     //check if its occluded, if its not...
  99.     if(!isOccluded(this_pixel / dimensions)){
  100.         gl_FragColor = vec4(255, 0, 0,  1.0);
  101.     }
  102.     else{
  103.         vec2 this_pixel_ratiod = vec2(this_pixel.x, this_pixel.y * aspect);
  104.         vec2 light_origin_ratiod;
  105.         float distance_between_light_and_pixel;
  106.         float distance_between_light_and_pixel_ratiod;
  107.         float distance_between_light_and_occluder;
  108.         //check for all the lights...
  109.         for(int light_index = 0; light_index < light_amount; light_index++){
  110.             //well, grab the light info first, but then...
  111.             Light light;
  112.             FETCHLIGHT(0)
  113.             else FETCHLIGHT(1)
  114.             else FETCHLIGHT(2)
  115.             else FETCHLIGHT(3)
  116.             else FETCHLIGHT(4)
  117.             else FETCHLIGHT(5)
  118.             else FETCHLIGHT(6)
  119.             else FETCHLIGHT(7)
  120.             else FETCHLIGHT(8)
  121.             else FETCHLIGHT(9)
  122.             else FETCHLIGHT(10)
  123.             else FETCHLIGHT(11)
  124.             else FETCHLIGHT(12)
  125.             else FETCHLIGHT(13)
  126.             else FETCHLIGHT(14)
  127.             else FETCHLIGHT(15)
  128.  
  129.            
  130.             //calculate distance between light and this pixel
  131.             /////distance_between_light_and_pixel = length(light.origin - (this_pixel / dimensions));
  132.             //calculate distance between light and this pixel (for this, consider aspect ratio!)
  133.  
  134.             //light_origin_ratiod = vec2(light.origin.x, light.origin.y * aspect);
  135.             distance_between_light_and_pixel_ratiod = length(light_origin_ratiod - (this_pixel_ratiod / dimensions));
  136.             //calculate distance between light and first occluded pixel in direction of this_pixel
  137.             /////distance_between_light_and_occluder = MarchShadow((this_pixel / dimensions), (this_pixel / dimensions) - light.origin);
  138.             //check if this light ray even reaches this pixel
  139.             if(canBeReached(light.origin, (this_pixel / dimensions))){
  140.                 //determine light falloff!
  141.                 final_color += light.color * (1.0 - (pow(distance_between_light_and_pixel_ratiod, 1)));
  142.                 //done!
  143.             }
  144.         }
  145.         gl_FragColor = vec4(final_color, 1.0);
  146.     }
  147. }
  148.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement