Advertisement
Guest User

Untitled

a guest
Aug 29th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. --------------Draw----------------
  2.  
  3. var c_x = o_Player.cam_x,
  4. c_y = o_Player.cam_y,
  5. c_w = o_Player.cam_w,
  6. c_h = o_Player.cam_h,
  7. s_w = surface_get_width(application_surface),
  8. s_h = surface_get_height(application_surface),
  9. x_scale = c_w/s_w,
  10. y_scale = c_h/s_h;
  11.  
  12. //Create and populate array of lights
  13. var l_count = instance_number(o_Light),
  14. l_arr = array_create(l_count * 6 + 1),
  15. l_i = 1;
  16.  
  17. l_arr[0] = l_count;
  18.  
  19. with (o_Light) {
  20. l_arr[l_i++] = x;
  21. l_arr[l_i++] = y;
  22. l_arr[l_i++] = in_rad;
  23. l_arr[l_i++] = out_rad;
  24. l_arr[l_i++] = dir;
  25. l_arr[l_i++] = fov;
  26. }
  27.  
  28. //Create the light surface and set it as target
  29. if (!surface_exists(light_surf))
  30. light_surf = surface_create(s_w, s_h);
  31.  
  32. gpu_set_blendmode_ext(bm_one, bm_zero);
  33. surface_set_target(light_surf); {
  34. camera_apply(cam);
  35. draw_clear_alpha(c_black, 0); //Alpha 1?
  36. shader_set(sh_LightArray);
  37. shader_set_uniform_f_array(l_array, l_arr);
  38. draw_surface_ext(application_surface, c_x, c_y, x_scale, y_scale, 0, c_white, 1);
  39. shader_reset();
  40. } surface_reset_target();
  41.  
  42. //Draw light_surf back to app_surf
  43. draw_surface_ext(light_surf, c_x, c_y, x_scale, y_scale, 0, c_white, 1);
  44. gpu_set_blendmode(bm_normal);
  45.  
  46. --------------Shader----------------
  47.  
  48. vec3 get_radiance(float c)
  49. {
  50. // UNPACK COLOR BITS
  51. vec3 col;
  52. col.b = floor(c * 0.0000152587890625);
  53. float blue_bits = c - col.b * 65536.0;
  54. col.g = floor(blue_bits * 0.00390625);
  55. col.r = floor(blue_bits - col.g * 256.0);
  56.  
  57. // NORMALIZE 0-255
  58. return col * 0.00390625;
  59. }
  60.  
  61. varying vec2 pos; //Pixel position
  62. varying vec2 tex;
  63.  
  64. uniform float l_array[512];
  65.  
  66. #define PI 3.1415926538
  67.  
  68. void main() {
  69.  
  70. vec3 albedo = texture2D(gm_BaseTexture, tex).rgb;
  71. vec3 color = vec3(0.0);
  72.  
  73. //Iterate over the lights array
  74. int num_lights = int(l_array[0]);
  75. int l_i = 1;
  76. for (int i=0; i<num_lights; ++i) {
  77.  
  78. //Light properties
  79. vec2 l_pos = vec2(l_array[l_i++], l_array[l_i++]);
  80. //vec3 radiance = get_radiance(l_array[l_i++]); //Keeping this here just in case...
  81. float l_in_rad = l_array[l_i++];
  82. float l_out_rad = l_array[l_i++];
  83. float l_dir = l_array[l_i++];
  84. float l_fov = l_array[l_i++];
  85.  
  86. //Vector from current pixel to the center of the circle
  87. vec2 dis = pos - l_pos;
  88.  
  89. //Literal distance from current pixel to center of circle
  90. float dist = length(dis);
  91.  
  92. //Convert direction + fov to radians
  93. float d_rad = radians(l_dir);
  94. float h_fov = radians(l_fov)*.5;
  95.  
  96. //Get the angle of the current pixel relative to the center (y has to be negative)
  97. float angle = atan(-dis.y, dis.x);
  98.  
  99. //Adjust angle to match direction
  100. float angle_diff = abs(angle - d_rad);
  101.  
  102. //Normalize angle difference
  103. angle_diff = mod(angle_diff + PI, 2.*PI) - PI;
  104.  
  105. //New alpha
  106. float new_alpha = 1.;
  107. //If this pixel is within the fov and within the outer circle, we are getting darker the farther we are from the center
  108. if (dist >= l_in_rad && dist <= l_out_rad && abs(angle_diff) <= h_fov) {
  109. new_alpha = (dist - l_in_rad)/(l_out_rad - l_in_rad);
  110. new_alpha = clamp(new_alpha, 0., 1.);
  111. }
  112. //Discard everything in the inner circle
  113. else if (dist < l_in_rad)
  114. new_alpha = 0.;
  115.  
  116.  
  117. //Test attenuation
  118. //dist = length(l_pos.xy - pos.xy) / l_out_rad;
  119. //new_alpha = 1./(dist*dist*.1 + 1.);
  120.  
  121. color += albedo * new_alpha;
  122. }
  123.  
  124. gl_FragColor = vec4(color, 1.);
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement