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::query_light_probe
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 IRRADIANCE_VOLUMES_ARE_USABLE
14// See:
15// https://advances.realtimerendering.com/s2006/Mitchell-ShadingInValvesSourceEngine.pdf
16// Slide 28, "Ambient Cube Basis"
17fn irradiance_volume_light(
18 world_position: vec3<f32>,
19 N: vec3<f32>,
20 clusterable_object_index_ranges: ptr<function, ClusterableObjectIndexRanges>,
21) -> vec3<f32> {
22 // Search for an irradiance volume that contains the fragment.
23 let query_result = query_light_probe(
24 world_position,
25 /*is_irradiance_volume=*/ true,
26 clusterable_object_index_ranges,
27 );
29 // If there was no irradiance volume found, bail out.
30 if (query_result.texture_index < 0) {
31 return vec3(0.0f);
32 }
34 // If we're lightmapped, and the irradiance volume contributes no diffuse
35 // light, then bail out.
36#ifdef LIGHTMAP
37 if (!query_result.affects_lightmapped_mesh_diffuse) {
38 return vec3(0.0f);
39 }
40#endif // LIGHTMAP
42#ifdef MULTIPLE_LIGHT_PROBES_IN_ARRAY
43 let irradiance_volume_texture = irradiance_volumes[query_result.texture_index];
44#else
45 let irradiance_volume_texture = irradiance_volume;
46#endif
48 let atlas_resolution = vec3<f32>(textureDimensions(irradiance_volume_texture));
49 let resolution = vec3<f32>(textureDimensions(irradiance_volume_texture) / vec3(1u, 2u, 3u));
51 // Make sure to clamp to the edges to avoid texture bleed.
52 var unit_pos = (query_result.light_from_world * vec4(world_position, 1.0f)).xyz;
53 let stp = clamp((unit_pos + 0.5) * resolution, vec3(0.5f), resolution - vec3(0.5f));
54 let uvw = stp / atlas_resolution;
56 // The bottom half of each cube slice is the negative part, so choose it if applicable on each
57 // slice.
58 let neg_offset = select(vec3(0.0f), vec3(0.5f), N < vec3(0.0f));
60 let uvw_x = uvw + vec3(0.0f, neg_offset.x, 0.0f);
61 let uvw_y = uvw + vec3(0.0f, neg_offset.y, 1.0f / 3.0f);
62 let uvw_z = uvw + vec3(0.0f, neg_offset.z, 2.0f / 3.0f);
64 let rgb_x = textureSampleLevel(irradiance_volume_texture, irradiance_volume_sampler, uvw_x, 0.0).rgb;
65 let rgb_y = textureSampleLevel(irradiance_volume_texture, irradiance_volume_sampler, uvw_y, 0.0).rgb;
66 let rgb_z = textureSampleLevel(irradiance_volume_texture, irradiance_volume_sampler, uvw_z, 0.0).rgb;
68 // Use Valve's formula to sample.
69 let NN = N * N;
70 return (rgb_x * NN.x + rgb_y * NN.y + rgb_z * NN.z) * query_result.intensity;
73#endif // IRRADIANCE_VOLUMES_ARE_USABLE
Contents