Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------Draw----------------
- var c_x = o_Player.cam_x,
- c_y = o_Player.cam_y,
- c_w = o_Player.cam_w,
- c_h = o_Player.cam_h,
- s_w = surface_get_width(application_surface),
- s_h = surface_get_height(application_surface),
- x_scale = c_w/s_w,
- y_scale = c_h/s_h;
- //Create and populate array of lights
- var l_count = instance_number(o_Light),
- l_arr = array_create(l_count * 6 + 1),
- l_i = 1;
- l_arr[0] = l_count;
- with (o_Light) {
- l_arr[l_i++] = x;
- l_arr[l_i++] = y;
- l_arr[l_i++] = in_rad;
- l_arr[l_i++] = out_rad;
- l_arr[l_i++] = dir;
- l_arr[l_i++] = fov;
- }
- //Create the light surface and set it as target
- if (!surface_exists(light_surf))
- light_surf = surface_create(s_w, s_h);
- gpu_set_blendmode_ext(bm_one, bm_zero);
- surface_set_target(light_surf); {
- camera_apply(cam);
- draw_clear_alpha(c_black, 0); //Alpha 1?
- shader_set(sh_LightArray);
- shader_set_uniform_f_array(l_array, l_arr);
- draw_surface_ext(application_surface, c_x, c_y, x_scale, y_scale, 0, c_white, 1);
- shader_reset();
- } surface_reset_target();
- //Draw light_surf back to app_surf
- draw_surface_ext(light_surf, c_x, c_y, x_scale, y_scale, 0, c_white, 1);
- gpu_set_blendmode(bm_normal);
- --------------Shader----------------
- vec3 get_radiance(float c)
- {
- // UNPACK COLOR BITS
- vec3 col;
- col.b = floor(c * 0.0000152587890625);
- float blue_bits = c - col.b * 65536.0;
- col.g = floor(blue_bits * 0.00390625);
- col.r = floor(blue_bits - col.g * 256.0);
- // NORMALIZE 0-255
- return col * 0.00390625;
- }
- varying vec2 pos; //Pixel position
- varying vec2 tex;
- uniform float l_array[512];
- #define PI 3.1415926538
- void main() {
- vec3 albedo = texture2D(gm_BaseTexture, tex).rgb;
- vec3 color = vec3(0.0);
- //Iterate over the lights array
- int num_lights = int(l_array[0]);
- int l_i = 1;
- for (int i=0; i<num_lights; ++i) {
- //Light properties
- vec2 l_pos = vec2(l_array[l_i++], l_array[l_i++]);
- //vec3 radiance = get_radiance(l_array[l_i++]); //Keeping this here just in case...
- float l_in_rad = l_array[l_i++];
- float l_out_rad = l_array[l_i++];
- float l_dir = l_array[l_i++];
- float l_fov = l_array[l_i++];
- //Vector from current pixel to the center of the circle
- vec2 dis = pos - l_pos;
- //Literal distance from current pixel to center of circle
- float dist = length(dis);
- //Convert direction + fov to radians
- float d_rad = radians(l_dir);
- float h_fov = radians(l_fov)*.5;
- //Get the angle of the current pixel relative to the center (y has to be negative)
- float angle = atan(-dis.y, dis.x);
- //Adjust angle to match direction
- float angle_diff = abs(angle - d_rad);
- //Normalize angle difference
- angle_diff = mod(angle_diff + PI, 2.*PI) - PI;
- //New alpha
- float new_alpha = 1.;
- //If this pixel is within the fov and within the outer circle, we are getting darker the farther we are from the center
- if (dist >= l_in_rad && dist <= l_out_rad && abs(angle_diff) <= h_fov) {
- new_alpha = (dist - l_in_rad)/(l_out_rad - l_in_rad);
- new_alpha = clamp(new_alpha, 0., 1.);
- }
- //Discard everything in the inner circle
- else if (dist < l_in_rad)
- new_alpha = 0.;
- //Test attenuation
- //dist = length(l_pos.xy - pos.xy) / l_out_rad;
- //new_alpha = 1./(dist*dist*.1 + 1.);
- color += albedo * new_alpha;
- }
- gl_FragColor = vec4(color, 1.);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement