Skip to content

Commit

Permalink
projections and lights
Browse files Browse the repository at this point in the history
  • Loading branch information
robtfm committed Jan 13, 2025
1 parent a95ecb7 commit e94c900
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 157 deletions.
105 changes: 53 additions & 52 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions crates/common/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use bevy::{
despawn_with_children_recursive, BuildWorldChildren, Bundle, Entity, GlobalTransform,
IntoSystemConfigs, Plugin, With, World,
},
render::view::{Layer, RenderLayers},
tasks::Task,
};
use ethers_core::types::H160;
Expand Down Expand Up @@ -440,3 +441,16 @@ impl VolumePanning<'_, '_> {
(volume, panning)
}
}

pub fn camera_to_render_layer(camera_layer: u32) -> Layer {
(match camera_layer {
0 => 0,
nonzero => nonzero + 5,
}) as Layer
}

pub fn camera_to_render_layers<'a>(camera_layers: impl Iterator<Item = &'a u32>) -> RenderLayers {
camera_layers.fold(RenderLayers::none(), |result, camera_layer| {
result.with(camera_to_render_layer(*camera_layer))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ message PBGlobalLight {
// ambient light intensity. the explorer default ambient brightness is multiplied by this non-physical quantity.
// default 1
optional float ambient_brightness = 3;

// camera layers to apply global light to. defaults to 0 only
repeated uint32 layers = 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ message PBTextureCamera {
// default infinity
optional float far_plane = 7;

// ambient light overrides for this camera
optional decentraland.common.Color3 ambient_color_override = 11;
optional float ambient_brightness_override = 12;

oneof mode {
Perspective perspective = 8;
Orthographic orthographic = 9;
Expand Down
15 changes: 14 additions & 1 deletion crates/scene_runner/src/update_world/lights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use common::{
dynamics::PLAYER_COLLIDER_RADIUS,
sets::SceneSets,
structs::{AppConfig, PrimaryUser, PRIMARY_AVATAR_LIGHT_LAYER},
util::TryPushChildrenEx,
util::{camera_to_render_layers, TryPushChildrenEx},
};
use dcl::interface::ComponentPosition;
use dcl_component::{
Expand Down Expand Up @@ -88,14 +88,25 @@ pub struct GlobalLight {
pub direction: Option<Vec3>,
pub ambient_color: Option<Color>,
pub ambient_brightness: Option<f32>,
pub layers: RenderLayers,
}

impl From<PbGlobalLight> for GlobalLight {
fn from(value: PbGlobalLight) -> Self {
let mut layers = if value.layers.is_empty() {
RenderLayers::default()
} else {
camera_to_render_layers(value.layers.iter())
};
if layers.intersects(&RenderLayers::default()) {
layers = layers.with(1);
}

Self {
direction: value.direction.as_ref().map(Vector3::world_vec_to_vec3),
ambient_color: value.ambient_color.map(Into::into),
ambient_brightness: value.ambient_brightness,
layers,
}
}
}
Expand All @@ -117,6 +128,7 @@ fn update_directional_light(
dir_direction: Quat::from_euler(EulerRot::YXZ, FRAC_PI_2 * 0.8, -t, 0.0) * Vec3::NEG_Z,
ambient_color: Color::srgb(0.85, 0.85, 1.0),
ambient_brightness: 1.0,
layers: RenderLayers::default().with(1),
};

let Ok(player) = player.get_single() else {
Expand Down Expand Up @@ -149,6 +161,7 @@ fn update_directional_light(
if let Some(brightness) = global.ambient_brightness {
global_light.ambient_brightness = brightness;
};
global_light.layers = global.layers.clone();
}
};

Expand Down
11 changes: 8 additions & 3 deletions crates/system_bridge/src/settings/bloom_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ impl AppSetting for BloomSetting {
fn apply(&self, cam_res: SystemParamItem<Self::Param>, commands: Commands) {
self.apply_to_camera(&cam_res, commands, cam_res.0);
}

fn apply_to_camera(&self, _: &SystemParamItem<Self::Param>, mut commands: Commands, camera_entity: Entity) {

fn apply_to_camera(
&self,
_: &SystemParamItem<Self::Param>,
mut commands: Commands,
camera_entity: Entity,
) {
let Some(mut cmds) = commands.get_entity(camera_entity) else {
return;
};
Expand All @@ -71,5 +76,5 @@ impl AppSetting for BloomSetting {
..BloomSettings::OLD_SCHOOL
}),
};
}
}
}
27 changes: 23 additions & 4 deletions crates/system_bridge/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,34 @@ pub struct NewCameraEvent(pub Entity);

impl Plugin for SettingBridgePlugin {
fn build(&self, app: &mut App) {
fn apply_to_camera<S: AppSetting>(mut commands: Commands, config: Res<AppConfig>, mut new_camera_events: EventReader<NewCameraEvent>, param: StaticSystemParam<S::Param>) {
fn apply_to_camera<S: AppSetting>(
mut commands: Commands,
config: Res<AppConfig>,
mut new_camera_events: EventReader<NewCameraEvent>,
param: StaticSystemParam<S::Param>,
) {
let param = param.into_inner();
for ev in new_camera_events.read() {
let setting = S::load(&config);
setting.apply_to_camera(&param, commands.reborrow(), ev.0);
}
}

fn add_int_setting<T: IntAppSetting>(app: &mut App, settings: &mut Settings, schedule: &mut Schedule) {
fn add_int_setting<T: IntAppSetting>(
app: &mut App,
settings: &mut Settings,
schedule: &mut Schedule,
) {
settings.add_int_setting::<T>();
schedule.add_systems(apply_setting::<T>);
app.add_systems(Update, apply_to_camera::<T>);
}

fn add_enum_setting<T: EnumAppSetting>(app: &mut App, settings: &mut Settings, schedule: &mut Schedule) {
fn add_enum_setting<T: EnumAppSetting>(
app: &mut App,
settings: &mut Settings,
schedule: &mut Schedule,
) {
settings.add_enum_setting::<T>();
schedule.add_systems(apply_setting::<T>);
app.add_systems(Update, apply_to_camera::<T>);
Expand Down Expand Up @@ -172,7 +185,13 @@ pub trait AppSetting: Eq + 'static {
fn load(config: &AppConfig) -> Self;
fn save(&self, config: &mut AppConfig);
fn apply(&self, param: SystemParamItem<Self::Param>, commands: Commands);
fn apply_to_camera(&self, _param: &SystemParamItem<Self::Param>, _commands: Commands, _camera_entity: Entity) {}
fn apply_to_camera(
&self,
_param: &SystemParamItem<Self::Param>,
_commands: Commands,
_camera_entity: Entity,
) {
}
}

pub trait EnumAppSetting: AppSetting + Sized + std::fmt::Debug {
Expand Down
12 changes: 8 additions & 4 deletions crates/system_bridge/src/settings/shadow_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ impl AppSetting for ShadowSetting {
(config, cam_res, mut lights): SystemParamItem<Self::Param>,
commands: Commands,
) {

let value = if config.graphics.shadow_distance == 0.0 {
ShadowSetting::Off
} else {
Expand Down Expand Up @@ -102,9 +101,14 @@ impl AppSetting for ShadowSetting {
let primary_cam = cam_res.0;
self.apply_to_camera(&(config, cam_res, lights), commands, primary_cam);
}

fn apply_to_camera(&self, _: &SystemParamItem<Self::Param>, mut commands: Commands, camera_entity: Entity) {
let Some(mut cmds) = commands.get_entity(camera_entity) else {

fn apply_to_camera(
&self,
_: &SystemParamItem<Self::Param>,
mut commands: Commands,
camera_entity: Entity,
) {
let Some(mut cmds) = commands.get_entity(camera_entity) else {
return;
};
match self {
Expand Down
9 changes: 7 additions & 2 deletions crates/system_bridge/src/settings/ssao_setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ impl AppSetting for SsaoSetting {
let primary_cam = cam_res.0;
self.apply_to_camera(&(cam_res, msaa_res), commands, primary_cam);
}

fn apply_to_camera(&self, (_, msaa_res): &SystemParamItem<Self::Param>, mut commands: Commands, camera_entity: Entity) {

fn apply_to_camera(
&self,
(_, msaa_res): &SystemParamItem<Self::Param>,
mut commands: Commands,
camera_entity: Entity,
) {
let Some(mut cmds) = commands.get_entity(camera_entity) else {
return;
};
Expand Down
1 change: 1 addition & 0 deletions crates/texture_camera/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ world_ui = { workspace = true }
analytics = { workspace = true }
social = { workspace = true }
system_bridge = { workspace = true }
visuals = { workspace = true }

bevy = { workspace = true }
bevy_egui = { workspace = true }
Expand Down
Loading

0 comments on commit e94c900

Please sign in to comment.