23 #define MAX_BRUTEFORCE_SAMPLING 8
28 positions[0] = positions[0] - p;
29 positions[1] = positions[1] - p;
30 positions[2] = positions[2] - p;
32 positions[0] = normalize(positions[0]);
33 positions[1] = normalize(positions[1]);
34 positions[2] = normalize(positions[2]);
36 vec3 a = cross(positions[1] - positions[0], positions[2] - positions[0]);
41 projected_tri_area(mat3 positions, vec3 p, vec3 n, vec3
V,
float phong_exp,
float phong_scale,
float phong_weight)
43 positions[0] = positions[0] - p;
44 positions[1] = positions[1] - p;
45 positions[2] = positions[2] - p;
47 vec3 g = cross(positions[1] - positions[0], positions[2] - positions[0]);
48 if ( dot(n, positions[0]) <= 0 && dot(n, positions[1]) <= 0 && dot(n, positions[2]) <= 0 )
50 if ( dot(g, positions[0]) >= 0 && dot(g, positions[1]) >= 0 && dot(g, positions[2]) >= 0 )
53 vec3
L = normalize(positions * vec3(1.0 / 3.0));
54 float specular = phong(n,
L,
V, phong_exp) * phong_scale;
55 float brdf =
mix(1.0, specular, phong_weight);
57 positions[0] = normalize(positions[0]);
58 positions[1] = normalize(positions[1]);
59 positions[2] = normalize(positions[2]);
61 vec3 a = cross(positions[1] - positions[0], positions[2] - positions[0]);
62 float pa = max(length(a) - 1e-5, 0.);
69 light_normal = cross(positions[1] - positions[0], positions[2] - positions[0]);
70 light_normal = normalize(light_normal);
72 positions[0] = positions[0] - p;
73 positions[1] = positions[1] - p;
74 positions[2] = positions[2] - p;
76 float o = dot(light_normal, positions[0]);
78 positions[0] = normalize(positions[0]);
79 positions[1] = normalize(positions[1]);
80 positions[2] = normalize(positions[2]);
82 vec3 projected_normal = cross(positions[1] - positions[0], positions[2] - positions[0]);
84 vec3 direction = positions * sample_triangle(rnd);
85 float dl = length(direction);
89 vec3 lo = direction * (o / dot(light_normal, direction));
91 projected_area = length(projected_normal) * 0.5;
99 addr = addr * global_ubo.num_static_lights + light;
100 addr = addr * 6 + side;
116 out vec3 position_light,
117 out vec3 light_color,
119 out
float projected_area,
122 position_light = vec3(0);
124 light_color = vec3(0);
130 uint list_start = get_light_list_offsets(list_idx);
131 uint list_end = get_light_list_offsets(list_idx + 1);
135 float fpart = min(floor(rng.x), partitions-1);
137 list_start +=
int(fpart);
138 int stride =
int(partitions);
146 if (n_idx >= list_end)
149 uint current_idx = get_light_list_lights(n_idx);
151 if(current_idx == ~0u)
157 LightPolygon light = get_light_polygon(current_idx);
161 float light_lum = luminance(light.color);
168 light_lum *= is_gradient ? light.prev_style_scale : light.light_style_scale;
174 m *= clamp(sun_color_ubo.sky_luminance, global_ubo.pt_min_log_sky_luminance, global_ubo.pt_max_log_sky_luminance);
181 if(global_ubo.pt_light_stats != 0
183 && current_idx < global_ubo.num_static_lights)
185 uint buffer_idx = global_ubo.current_frame_idx;
194 uint num_hits = light_stats_bufers[buffer_idx].stats[addr];
195 uint num_misses = light_stats_bufers[buffer_idx].stats[addr + 1];
196 uint num_total = num_hits + num_misses;
202 m *= max(
float(num_hits) /
float(num_total), 0.1);
214 int current_idx = -1;
220 if (n_idx >= list_end)
222 pdf = light_masses[i];
223 current_idx =
int(n_idx);
236 if (current_idx >= 0) {
237 current_idx =
int(get_light_list_lights(current_idx));
239 LightPolygon light = get_light_polygon(current_idx);
244 vec3
L = normalize(position_light - p);
249 float LdotNL = max(0, -dot(light_normal,
L));
250 float spotlight = sqrt(LdotNL);
252 if(light.color.r >= 0)
254 light_color = light.color * (projected_area * spotlight * light.light_style_scale);
258 light_color =
env_map(
L,
true) * projected_area * global_ubo.pt_env_scale;
260 light_index = current_idx;
271 float max_solid_angle,
272 out vec3 position_light,
273 out vec3 light_color,
276 position_light = vec3(0);
277 light_color = vec3(0);
279 if(global_ubo.num_sphere_lights == 0)
282 float random_light = rng.x * global_ubo.num_sphere_lights;
283 uint light_idx = min(global_ubo.num_sphere_lights - 1,
uint(random_light));
285 vec4 light_center_radius = global_ubo.sphere_light_data[light_idx * 2];
286 float sphere_radius = light_center_radius.w;
288 light_color = global_ubo.sphere_light_data[light_idx * 2 + 1].rgb;
290 vec3
c = light_center_radius.xyz - p;
291 float dist = length(
c);
292 float rdist = 1.0 / dist;
295 float irradiance = 2 * (1 - sqrt(max(0, 1 - square(sphere_radius * rdist))));
296 irradiance = min(irradiance, max_solid_angle);
297 irradiance *= float(global_ubo.num_sphere_lights);
299 mat3 onb = construct_ONB_frisvad(
L);
302 diskpt.z = sqrt(max(0, 1 - diskpt.x * diskpt.x - diskpt.y * diskpt.y));
304 position_light = light_center_radius.xyz + (onb[0] * diskpt.x + onb[2] * diskpt.y -
L * diskpt.z) * sphere_radius;
305 light_color *= irradiance;
307 if(dot(position_light - p, gn) <= 0)
308 light_color = vec3(0);