1#define_import_path bevy_pbr::lightmap 3#import bevy_pbr::mesh_bindings::mesh 5#ifdef MULTIPLE_LIGHTMAPS_IN_ARRAY 6@group(1) @binding(4) var lightmaps_textures: binding_array<texture_2d<f32>, 4>; 7@group(1) @binding(5) var lightmaps_samplers: binding_array<sampler, 4>; 8#else // MULTIPLE_LIGHTMAPS_IN_ARRAY 9@group(1) @binding(4) var lightmaps_texture: texture_2d<f32>; 10@group(1) @binding(5) var lightmaps_sampler: sampler; 11#endif // MULTIPLE_LIGHTMAPS_IN_ARRAY 14fn lightmap(uv: vec2<f32>, exposure: f32, instance_index: u32) -> vec3<f32> { 15 let packed_uv_rect = mesh[instance_index].lightmap_uv_rect; 16 let uv_rect = vec4<f32>( 17 unpack2x16unorm(packed_uv_rect.x), 18 unpack2x16unorm(packed_uv_rect.y), 20 let lightmap_uv = mix(uv_rect.xy, uv_rect.zw, uv); 21 let lightmap_slot = mesh[instance_index].material_and_lightmap_bind_group_slot >> 16u; 26#ifdef LIGHTMAP_BICUBIC_SAMPLING 27 let texture_size = vec2<f32>(lightmap_size(lightmap_slot)); 28 let texel_size = 1.0 / texture_size; 29 let puv = lightmap_uv * texture_size + 0.5; 34 let h0x = h0_approx(fuv.x); 35 let h1x = h1_approx(fuv.x); 36 let h0y = h0_approx(fuv.y); 37 let h1y = h1_approx(fuv.y); 38 let p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - 0.5) * texel_size; 39 let p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - 0.5) * texel_size; 40 let p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - 0.5) * texel_size; 41 let p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - 0.5) * texel_size; 42 let color = g0(fuv.y) * (g0x * sample(p0, lightmap_slot) + g1x * sample(p1, lightmap_slot)) + g1(fuv.y) * (g0x * sample(p2, lightmap_slot) + g1x * sample(p3, lightmap_slot)); 44 let color = sample(lightmap_uv, lightmap_slot); 47 return color * exposure; 50fn lightmap_size(lightmap_slot: u32) -> vec2<u32> { 51#ifdef MULTIPLE_LIGHTMAPS_IN_ARRAY 52 return textureDimensions(lightmaps_textures[lightmap_slot]); 54 return textureDimensions(lightmaps_texture); 58fn sample(uv: vec2<f32>, lightmap_slot: u32) -> vec3<f32> { 62#ifdef MULTIPLE_LIGHTMAPS_IN_ARRAY 63 return textureSampleLevel(lightmaps_textures[lightmap_slot], lightmaps_samplers[lightmap_slot], uv, 0.0).rgb; 65 return textureSampleLevel(lightmaps_texture, lightmaps_sampler, uv, 0.0).rgb; 70 return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0); 74 return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0); 78 return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0); 82 return (1.0 / 6.0) * (a * a * a); 93fn h0_approx(a: f32) -> f32 { 94 return -0.2 - a * (0.24 * a - 0.44); 97fn h1_approx(a: f32) -> f32 { 98 return 1.0 + a * (0.24 * a - 0.04);