Update textures add scene directional light

This commit is contained in:
Máté Homolya 2025-06-02 13:40:27 -07:00
parent b9374ee6f3
commit ab33914573
No known key found for this signature in database
7 changed files with 21 additions and 20 deletions

View File

@ -140,7 +140,7 @@ fn importance_sample_ggx(xi: vec2f, roughness: f32, normal: vec3f) -> vec3f {
let a = roughness * roughness; let a = roughness * roughness;
// Sample in spherical coordinates // Sample in spherical coordinates
let phi = 2.0 * PI * xi.x; let phi = PI_2 * xi.x;
// GGX mapping from uniform random to GGX distribution // GGX mapping from uniform random to GGX distribution
let cos_theta = fast_sqrt((1.0 - xi.y) / (1.0 + (a * a - 1.0) * xi.y)); let cos_theta = fast_sqrt((1.0 - xi.y) / (1.0 + (a * a - 1.0) * xi.y));
@ -343,9 +343,6 @@ fn generate_irradiance_map(@builtin(global_invocation_id) global_id: vec3u) {
// Normalize by total weight // Normalize by total weight
irradiance = irradiance / total_weight; irradiance = irradiance / total_weight;
// Scale by PI to account for the Lambert BRDF normalization factor
irradiance *= PI;
// Reverse tonemap to restore HDR range // Reverse tonemap to restore HDR range
irradiance = reverse_tonemap(irradiance); irradiance = reverse_tonemap(irradiance);

View File

@ -335,7 +335,7 @@ impl Default for GeneratedEnvironmentMapLight {
intensity: 0.0, intensity: 0.0,
rotation: Quat::IDENTITY, rotation: Quat::IDENTITY,
affects_lightmapped_mesh_diffuse: true, affects_lightmapped_mesh_diffuse: true,
white_point: 2.0, white_point: 1.0,
} }
} }
} }
@ -568,20 +568,10 @@ pub fn prepare_generator_bind_groups(
let mut radiance_bind_groups = Vec::with_capacity(num_mips); let mut radiance_bind_groups = Vec::with_capacity(num_mips);
for mip in 0..num_mips { for mip in 0..num_mips {
let roughness = mip as f32 / (num_mips - 1) as f32; // Calculate roughness from 0.0 (mip 0) to 0.889 (mip 8)
// We don't need roughness=1.0 as a mip level because it's handled by the separate diffuse irradiance map
// For higher roughness values, use importance sampling with optimized sample count let roughness = mip as f32 / num_mips as f32;
let sample_count = if roughness < 0.01 { let sample_count = 32u32 * 2u32.pow((roughness * 4.0) as u32);
1 // Mirror reflection
} else if roughness < 0.25 {
16
} else if roughness < 0.5 {
32
} else if roughness < 0.75 {
64
} else {
128
};
let radiance_constants = FilteringConstants { let radiance_constants = FilteringConstants {
mip_level: mip as f32, mip_level: mip as f32,

View File

@ -167,6 +167,15 @@ fn spawn_generated_environment_map(commands: &mut Commands, cubemaps: &Cubemaps)
}, },
Transform::from_scale(Vec3::splat(2.0)), Transform::from_scale(Vec3::splat(2.0)),
)); ));
// spawn directional light
commands.spawn((
DirectionalLight {
illuminance: 30_000.0,
..default()
},
Transform::from_xyz(1.0, 0.5, 0.6).looking_at(Vec3::ZERO, Vec3::Y),
));
} }
// Spawns the help text. // Spawns the help text.
@ -209,6 +218,7 @@ fn change_reflection_type(
light_probe_query: Query<Entity, With<LightProbe>>, light_probe_query: Query<Entity, With<LightProbe>>,
sky_box_query: Query<Entity, With<Skybox>>, sky_box_query: Query<Entity, With<Skybox>>,
camera_query: Query<Entity, With<Camera3d>>, camera_query: Query<Entity, With<Camera3d>>,
directional_light_query: Query<Entity, With<DirectionalLight>>,
keyboard: Res<ButtonInput<KeyCode>>, keyboard: Res<ButtonInput<KeyCode>>,
mut app_status: ResMut<AppStatus>, mut app_status: ResMut<AppStatus>,
cubemaps: Res<Cubemaps>, cubemaps: Res<Cubemaps>,
@ -229,6 +239,9 @@ fn change_reflection_type(
for skybox in sky_box_query.iter() { for skybox in sky_box_query.iter() {
commands.entity(skybox).remove::<Skybox>(); commands.entity(skybox).remove::<Skybox>();
} }
for directional_light in directional_light_query.iter() {
commands.entity(directional_light).despawn();
}
match app_status.reflection_mode { match app_status.reflection_mode {
ReflectionMode::None | ReflectionMode::EnvironmentMap => {} ReflectionMode::None | ReflectionMode::EnvironmentMap => {}
ReflectionMode::ReflectionProbe => spawn_reflection_probe(&mut commands, &cubemaps), ReflectionMode::ReflectionProbe => spawn_reflection_probe(&mut commands, &cubemaps),
@ -369,7 +382,8 @@ impl FromWorld for Cubemaps {
specular_reflection_probe: world specular_reflection_probe: world
.load_asset("environment_maps/cubes_reflection_probe_specular_rgb9e5_zstd.ktx2"), .load_asset("environment_maps/cubes_reflection_probe_specular_rgb9e5_zstd.ktx2"),
specular_environment_map: specular_map.clone(), specular_environment_map: specular_map.clone(),
unfiltered_environment_map: world.load_asset("environment_maps/goegap_road_2k.ktx2"), unfiltered_environment_map: world
.load_asset("environment_maps/spiaggia_di_mondello_2k_skybox.ktx2"),
skybox: specular_map, skybox: specular_map,
} }
} }