Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tonemapping switch to bloom 2d example #17789

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/2d/bloom_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,40 @@ fn main() {
.run();
}

/// Resource to allow cycling through available Tonemapping algorithms
#[derive(Resource)]
struct Tonemapper {
hukasu marked this conversation as resolved.
Show resolved Hide resolved
tonemapper: Tonemapping,
}

impl Tonemapper {
/// Modifies resources tonemapping algorithm for the next one and returns it
fn next(&mut self) -> Tonemapping {
let next = match self.tonemapper {
Tonemapping::None => Tonemapping::AcesFitted,
Tonemapping::AcesFitted => Tonemapping::AgX,
Tonemapping::AgX => Tonemapping::BlenderFilmic,
Tonemapping::BlenderFilmic => Tonemapping::Reinhard,
Tonemapping::Reinhard => Tonemapping::ReinhardLuminance,
Tonemapping::ReinhardLuminance => Tonemapping::SomewhatBoringDisplayTransform,
Tonemapping::SomewhatBoringDisplayTransform => Tonemapping::TonyMcMapface,
Tonemapping::TonyMcMapface => Tonemapping::None,
};
self.tonemapper = next;
next
}
}

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
asset_server: Res<AssetServer>,
) {
commands.insert_resource(Tonemapper {
tonemapper: Tonemapping::TonyMcMapface,
});

commands.spawn((
Camera2d,
Camera {
Expand Down Expand Up @@ -76,6 +104,7 @@ fn update_bloom_settings(
camera: Single<(Entity, Option<&mut Bloom>), With<Camera>>,
mut text: Single<&mut Text>,
mut commands: Commands,
mut tonemapper: ResMut<Tonemapper>,
keycode: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
Expand Down Expand Up @@ -110,6 +139,7 @@ fn update_bloom_settings(
bloom.prefilter.threshold_softness
));
text.push_str(&format!("(I/K) Horizontal Scale: {}\n", bloom.scale.x));
text.push_str(&format!("(O) Tonemapper: {:?}\n", tonemapper.tonemapper));
hukasu marked this conversation as resolved.
Show resolved Hide resolved

if keycode.just_pressed(KeyCode::Space) {
commands.entity(entity).remove::<Bloom>();
Expand Down Expand Up @@ -180,6 +210,10 @@ fn update_bloom_settings(
bloom.scale.x += dt * 2.0;
}
bloom.scale.x = bloom.scale.x.clamp(0.0, 16.0);

if keycode.just_pressed(KeyCode::KeyO) {
commands.entity(entity).insert(tonemapper.next());
}
}

(entity, None) => {
Expand Down