Skip to content

Commit

Permalink
shadow compat
Browse files Browse the repository at this point in the history
  • Loading branch information
robtfm committed Sep 7, 2024
1 parent 58ec353 commit 7483559
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 69 deletions.
20 changes: 20 additions & 0 deletions assets/shaders/bound_prepass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,24 @@ fn fragment(

return out;
}
#else // !PREPASS_FRAGMENT (?)
@fragment
fn fragment(in: VertexOutput) {
let world_position = in.world_position.xyz;
let outside_amt = max(max(max(0.0, bounds.bounds.x - world_position.x), max(world_position.x - bounds.bounds.z, bounds.bounds.y - world_position.z)), world_position.z - bounds.bounds.w);

var noise = 0.0;
if outside_amt > 0.0 {
if outside_amt < bounds.distance {
noise = simplex_noise_3d(world_position * 2.0 + globals.time * vec3(0.2, 0.16, 0.24)) * 0.5 + 0.55;
if noise < (outside_amt - 0.125) / 2.0 {
discard;
}
} else if outside_amt > 0.05 {
discard;
}
}

prepass_alpha_discard(in);
}
#endif // PREPASS_FRAGMENT
4 changes: 0 additions & 4 deletions crates/scene_runner/src/initialize_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,10 +1201,6 @@ impl Material for LoadingMaterial {
ShaderRef::Path("shaders/loading.wgsl".into())
}

fn prepass_fragment_shader() -> ShaderRef {
ShaderRef::Path("shaders/loading.wgsl".into())
}

fn alpha_mode(&self) -> AlphaMode {
AlphaMode::Blend
}
Expand Down
99 changes: 43 additions & 56 deletions crates/scene_runner/src/update_world/lights.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use std::f32::consts::{FRAC_PI_2, PI, TAU};

use bevy::{
pbr::CubemapVisibleEntities,
prelude::*,
render::{
primitives::{CubemapFrusta, Frustum},
view::VisibleEntities,
},
};
use common::{sets::SceneSets, structs::PrimaryUser};
use bevy::prelude::*;
use common::{sets::SceneSets, structs::PrimaryUser, util::TryPushChildrenEx};
use dcl::interface::ComponentPosition;
use dcl_component::{
proto_components::{
Expand Down Expand Up @@ -46,7 +39,7 @@ impl Plugin for LightsPlugin {
}
}

#[derive(Component)]
#[derive(Component, Debug)]
pub struct Light {
pub enabled: bool,
pub illuminance: Option<f32>,
Expand Down Expand Up @@ -186,43 +179,42 @@ fn update_directional_light(
}
}

#[derive(Component)]
pub struct LightEntity;

fn update_point_lights(
q: Query<
(
Entity,
&Light,
Option<&SpotlightAngles>,
Option<&PointLight>,
Option<&SpotLight>,
),
(Entity, &Light, Option<&SpotlightAngles>, Option<&Children>),
(
Without<RendererSceneContext>,
Or<(Changed<Light>, Changed<SpotlightAngles>)>,
),
>,
mut commands: Commands,
mut removed_spots: RemovedComponents<SpotlightAngles>,
mut removed_points: RemovedComponents<Light>,
children: Query<&Children>,
child_lights: Query<&LightEntity>,
) {
for (entity, light, angles, maybe_point, maybe_spot) in q.iter() {
for (entity, light, angles, maybe_children) in q.iter() {
// despawn any previous
if let Some(children) = maybe_children {
for child in children.iter() {
if child_lights.get(*child).is_ok() {
commands.entity(*child).despawn_recursive();
}
}
}

let lumens = if light.enabled {
light.illuminance.unwrap_or(10000.0) * 4.0 * PI
} else {
0.0
};
// 10 lumens cutoff
let range = light.illuminance.unwrap_or(10000.0).sqrt();
let Some(mut commands) = commands.get_entity(entity) else {
continue;
};
match angles {
Some(angles) => {
if maybe_point.is_some() {
commands.remove::<(PointLight, CubemapVisibleEntities, CubemapFrusta)>();
}

commands.try_insert((
SpotLight {
let range = light.illuminance.unwrap_or(10000.0).powf(0.25);
let light_id = match angles {
Some(angles) => commands
.spawn(SpotLightBundle {
spot_light: SpotLight {
color: light.color.unwrap_or(Color::WHITE),
intensity: lumens,
range,
Expand All @@ -232,40 +224,35 @@ fn update_point_lights(
inner_angle: angles.inner_angle,
..Default::default()
},
VisibleEntities::default(),
Frustum::default(),
));
}
None => {
if maybe_spot.is_some() {
commands.remove::<(SpotLight, VisibleEntities, Frustum)>();
}

commands.insert((
PointLight {
..Default::default()
})
.id(),
None => commands
.spawn(PointLightBundle {
point_light: PointLight {
color: light.color.unwrap_or(Color::WHITE),
intensity: lumens,
range,
radius: 0.0,
shadows_enabled: light.shadows.unwrap_or(false),
..Default::default()
},
CubemapVisibleEntities::default(),
CubemapFrusta::default(),
));
}
}
}
..Default::default()
})
.id(),
};

for removed_spot in removed_spots.read() {
if let Some(mut commands) = commands.get_entity(removed_spot) {
commands.remove::<(SpotLight, VisibleEntities, Frustum)>();
}
commands.entity(entity).try_push_children(&[light_id]);
}

for removed_point in removed_points.read() {
if let Some(mut commands) = commands.get_entity(removed_point) {
commands.remove::<(PointLight, CubemapVisibleEntities, CubemapFrusta)>();
for removed_light in removed_points.read() {
let Ok(children) = children.get(removed_light) else {
continue;
};
for child in children {
if child_lights.get(*child).is_ok() {
commands.entity(*child).despawn_recursive();
}
}
}
}
28 changes: 19 additions & 9 deletions crates/visuals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn setup(
));
}

#[derive(Resource, Default, Clone)]
#[derive(Resource, Default, Clone, Debug)]
pub struct SceneGlobalLight {
pub source: Option<Entity>,
pub dir_color: Color,
Expand Down Expand Up @@ -135,7 +135,11 @@ fn apply_global_light(
let Ok((camera, mut skybox)) = camera.get_single_mut() else {
return;
};
skybox.brightness = (next_light.dir_illuminance.sqrt() * 40.0).min(2000.0);
let dir_light_lightness = Lcha::from(next_light.dir_color).lightness;
skybox.brightness =
(next_light.dir_illuminance.sqrt() * 40.0 * dir_light_lightness).min(2000.0);
atmosphere.rayleigh_coefficient =
Vec3::new(5.5e-6, 13.0e-6, 22.4e-6) * next_light.dir_color.to_srgba().to_vec3();

if let Ok((mut light_trans, mut directional)) = sun.get_single_mut() {
light_trans.rotation = rotation;
Expand All @@ -144,26 +148,32 @@ fn apply_global_light(

if let Ok(mut fog) = fog.get_single_mut() {
let distance = scene_distance.load + scene_distance.unload + camera.distance * 5.0;

let base_color =
next_light.ambient_color.to_srgba() * next_light.ambient_brightness * 0.5;
let base_color = Color::from(base_color).with_alpha(1.0);

fog.color = base_color;
match setting.graphics.fog {
FogSetting::Off => {
fog.falloff = FogFalloff::from_visibility_squared(distance * 200.0);
fog.directional_light_color = Color::srgb(0.3, 0.2, 0.1);
fog.directional_light_color = base_color;
}
FogSetting::Basic => {
fog.falloff = FogFalloff::from_visibility_squared(distance * 2.0);
fog.directional_light_color = Color::srgb(0.3, 0.2, 0.1);
fog.directional_light_color = base_color;
}
FogSetting::Atmospheric => {
fog.falloff = FogFalloff::from_visibility_squared(distance * 2.0);
fog.directional_light_color = next_light.dir_color;
}
}

let sun_up = atmosphere.sun_position.dot(Vec3::Y);
let rgb = Vec3::new(0.4, 0.4, 0.2) * sun_up.clamp(0.0, 1.0)
+ Vec3::new(0.0, 0.0, 0.0) * (8.0 * (0.125 - sun_up.clamp(0.0, 0.125)));
let rgb = rgb.powf(1.0 / 2.2);
fog.color = Color::srgb(rgb.x, rgb.y, rgb.z);
// let sun_up = atmosphere.sun_position.dot(Vec3::Y);
// let rgb = Vec3::new(0.4, 0.4, 0.2) * sun_up.clamp(0.0, 1.0)
// + Vec3::new(0.0, 0.0, 0.0) * (8.0 * (0.125 - sun_up.clamp(0.0, 0.125)));
// let rgb = rgb.powf(1.0 / 2.2);
// fog.color = Color::srgb(rgb.x, rgb.y, rgb.z);
}
}

Expand Down

0 comments on commit 7483559

Please sign in to comment.