Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#define_import_path bevy_pbr::irradiance_volume
3#import bevy_pbr::light_probe::{light_probe_iterator_new, light_probe_iterator_next}
4#import bevy_pbr::mesh_view_bindings::{
5 irradiance_volumes,
6 irradiance_volume,
7 irradiance_volume_sampler,
8 light_probes,
9};
10#import bevy_pbr::clustered_forward::ClusterableObjectIndexRanges
12#ifdef LIGHTMAP
13#import bevy_pbr::mesh_view_types::LIGHT_PROBE_FLAG_AFFECTS_LIGHTMAPPED_MESH_DIFFUSE;
14#endif
16#ifdef IRRADIANCE_VOLUMES_ARE_USABLE
18// See:
19// https://advances.realtimerendering.com/s2006/Mitchell-ShadingInValvesSourceEngine.pdf
20// Slide 28, "Ambient Cube Basis"
21fn irradiance_volume_light(
22 world_position: vec3<f32>,
23 N: vec3<f32>,
24 clusterable_object_index_ranges: ptr<function, ClusterableObjectIndexRanges>,
25) -> vec3<f32> {
26 // Find all irradiance volumes that contain the fragment. We're going to
27 // accumulate all the irradiance from them in a weighted sum.
28 var iterator = light_probe_iterator_new(
29 world_position,
30 /*is_irradiance_volume=*/ true,
31 clusterable_object_index_ranges,
32 );
34 var total_weight = 0.0;
35 var total_irradiance = vec3(0.0);
37 while (true) {
38 var query_result = light_probe_iterator_next(&iterator);
40 // If there was no irradiance volume found, we're done.
41 if (query_result.texture_index < 0) {
42 break;
43 }
45 // If we're lightmapped, and the irradiance volume contributes no diffuse
46 // light, then bail out.
47 #ifdef LIGHTMAP
48 if ((query_result.flags & LIGHT_PROBE_FLAG_AFFECTS_LIGHTMAPPED_MESH_DIFFUSE) == 0u) {
49 continue;
50 }
51 #endif // LIGHTMAP
53 #ifdef MULTIPLE_LIGHT_PROBES_IN_ARRAY
54 let irradiance_volume_texture = irradiance_volumes[query_result.texture_index];
55 #else
56 let irradiance_volume_texture = irradiance_volume;
57 #endif
59 let atlas_resolution = vec3<f32>(textureDimensions(irradiance_volume_texture));
60 let resolution = vec3<f32>(textureDimensions(irradiance_volume_texture) / vec3(1u, 2u, 3u));
62 // Make sure to clamp to the edges to avoid texture bleed.
63 var unit_pos = (query_result.light_from_world * vec4(world_position, 1.0f)).xyz;
64 let stp = clamp((unit_pos + 0.5) * resolution, vec3(0.5f), resolution - vec3(0.5f));
65 let uvw = stp / atlas_resolution;
67 // The bottom half of each cube slice is the negative part, so choose it if applicable on each
68 // slice.
69 let neg_offset = select(vec3(0.0f), vec3(0.5f), N < vec3(0.0f));
71 let uvw_x = uvw + vec3(0.0f, neg_offset.x, 0.0f);
72 let uvw_y = uvw + vec3(0.0f, neg_offset.y, 1.0f / 3.0f);
73 let uvw_z = uvw + vec3(0.0f, neg_offset.z, 2.0f / 3.0f);
75 let rgb_x = textureSampleLevel(irradiance_volume_texture, irradiance_volume_sampler, uvw_x, 0.0).rgb;
76 let rgb_y = textureSampleLevel(irradiance_volume_texture, irradiance_volume_sampler, uvw_y, 0.0).rgb;
77 let rgb_z = textureSampleLevel(irradiance_volume_texture, irradiance_volume_sampler, uvw_z, 0.0).rgb;
79 // Use Valve's formula to sample.
80 let NN = N * N;
81 total_irradiance += (rgb_x * NN.x + rgb_y * NN.y + rgb_z * NN.z) * query_result.intensity *
82 query_result.weight;
84 total_weight += query_result.weight;
85 }
87 if (total_weight != 0.0) {
88 total_irradiance /= total_weight;
89 }
91 return total_irradiance;
94#endif // IRRADIANCE_VOLUMES_ARE_USABLE
Contents