Advertisement
HephepTeam

Lighting shader

May 6th, 2025
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. shader_type canvas_item;
  2.  
  3. uniform vec4 modulate_color : source_color = vec4(0.0);
  4. uniform vec4 black_point : source_color = vec4(0.0);
  5. uniform vec3 lights[100];
  6. uniform int number_of_lights = 0;
  7. uniform float band_1_radius = 54;
  8. uniform float band_2_radius = 38;
  9. uniform float band_1_strength = 0.3;
  10. uniform float band_2_strength = 0.6;
  11. uniform sampler2D screen_texture : hint_screen_texture, filter_nearest;
  12. uniform float light_radius = 64.0;
  13.  
  14.  
  15. struct Light_data{
  16.     float dist;
  17.     float radius;
  18. };
  19.  
  20. Light_data distance_to_light(vec2 coords) {
  21.     float dist = 1000.0;
  22.     float rad = 0.0;
  23.     Light_data data;
  24.    
  25.     float best_ratio = 1000.0;
  26.    
  27.     for (int i = 0; i < number_of_lights; i++) {
  28.         float new_dist = distance(coords, lights[i].xy);
  29.         float new_rad = lights[i].z;
  30.        
  31.         float ratio = new_dist / new_rad;
  32.        
  33.         if (ratio < best_ratio) {
  34.             best_ratio = ratio;
  35.             data.dist = new_dist;
  36.             data.radius = new_rad;
  37.         }
  38.     }
  39.     return data;
  40. }
  41.  
  42. vec4 set_black_point(vec4 color) {
  43.     float bp_avg = black_point.r + black_point.g + black_point.b;
  44.     float color_avg = color.r + color.g + color.b;
  45.    
  46.     if (color_avg < bp_avg) {
  47.         return black_point;
  48.     } else {
  49.         return color;
  50.     }
  51. }
  52.  
  53. vec4 modulate(vec4 color, Light_data data) {
  54.     vec4 modulated = color * modulate_color;
  55.     float band_radius = 0.8 * data.radius;
  56.    
  57.     if (data.dist > data.radius) {
  58.         return modulated;
  59.     }
  60.    
  61.     if (data.dist > band_radius) {
  62.         return mix(modulated, color, band_1_strength);
  63.     }
  64.    
  65.     return color;
  66. }
  67.  
  68. void fragment() {
  69.    
  70.     COLOR = texture(screen_texture, SCREEN_UV);
  71.     if (number_of_lights > 0){
  72.         COLOR = modulate(COLOR, distance_to_light(FRAGCOORD.xy));
  73.         COLOR = set_black_point(COLOR);
  74.     }
  75.     else{
  76.         COLOR = COLOR *modulate_color;
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement