From 443769ed7558923b97b0dc0f3207676668b2bb06 Mon Sep 17 00:00:00 2001 From: Louis Johnson Date: Wed, 27 Oct 2021 12:20:05 +1000 Subject: [PATCH 01/10] adding transparency to window builder --- Cargo.toml | 4 ++++ crates/bevy_window/src/window.rs | 2 ++ crates/bevy_winit/src/winit_windows.rs | 3 ++- examples/window/transparent_window.rs | 32 ++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 examples/window/transparent_window.rs diff --git a/Cargo.toml b/Cargo.toml index ff97fb1b45e27..b9e17de3835e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -451,6 +451,10 @@ path = "examples/window/multiple_windows.rs" name = "scale_factor_override" path = "examples/window/scale_factor_override.rs" +[[example]] +name = "transparent_window" +path = "examples/window/transparent_window.rs" + [[example]] name = "window_settings" path = "examples/window/window_settings.rs" diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index f8e2e249da76e..f89023530b5e1 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -534,6 +534,7 @@ pub struct WindowDescriptor { pub cursor_visible: bool, pub cursor_locked: bool, pub mode: WindowMode, + pub transparent: bool, #[cfg(target_arch = "wasm32")] pub canvas: Option, } @@ -552,6 +553,7 @@ impl Default for WindowDescriptor { cursor_locked: false, cursor_visible: true, mode: WindowMode::Windowed, + transparent: false, #[cfg(target_arch = "wasm32")] canvas: None, } diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index b05f812f4d1b5..9877d7497ff32 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -57,7 +57,8 @@ impl WinitWindows { } } .with_resizable(window_descriptor.resizable) - .with_decorations(window_descriptor.decorations), + .with_decorations(window_descriptor.decorations) + .with_transparent(window_descriptor.transparent), }; let constraints = window_descriptor.resize_constraints.check_constraints(); diff --git a/examples/window/transparent_window.rs b/examples/window/transparent_window.rs new file mode 100644 index 0000000000000..21e68f09f06c5 --- /dev/null +++ b/examples/window/transparent_window.rs @@ -0,0 +1,32 @@ +use bevy::{ + prelude::*, + render::pass::ClearColor, + window::WindowDescriptor, +}; + +fn main() { + App::new() + .insert_resource(ClearColor(Color::rgba(0.0, 0.0, 0.0, 0.0))) + .insert_resource(WindowDescriptor { + vsync: false, + transparent: true, + decorations: false, + ..Default::default() + }) + .add_startup_system(setup) + .add_plugins(DefaultPlugins) + .run(); +} + +fn setup( + mut commands: Commands, + asset_server: Res, + mut materials: ResMut>, +) { + let texture_handle = asset_server.load("branding/icon.png"); + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); + commands.spawn_bundle(SpriteBundle { + material: materials.add(texture_handle.into()), + ..Default::default() + }); +} \ No newline at end of file From efe395986b4d77d47ab87021be2aa129ab9c7795 Mon Sep 17 00:00:00 2001 From: Louis Johnson Date: Wed, 27 Oct 2021 17:43:15 +1000 Subject: [PATCH 02/10] code review feedback --- crates/bevy_render/src/color.rs | 1 + examples/window/transparent_window.rs | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/bevy_render/src/color.rs b/crates/bevy_render/src/color.rs index 5c12053d3e4db..3b81a9a35a027 100644 --- a/crates/bevy_render/src/color.rs +++ b/crates/bevy_render/src/color.rs @@ -85,6 +85,7 @@ impl Color { pub const SILVER: Color = Color::rgb(0.75, 0.75, 0.75); pub const TEAL: Color = Color::rgb(0.0, 0.5, 0.5); pub const TOMATO: Color = Color::rgb(1.0, 0.39, 0.28); + pub const TRANSPARENT: Color = Color::rgba(0., 0., 0., 0.); pub const TURQUOISE: Color = Color::rgb(0.25, 0.88, 0.82); pub const VIOLET: Color = Color::rgb(0.93, 0.51, 0.93); pub const WHITE: Color = Color::rgb(1.0, 1.0, 1.0); diff --git a/examples/window/transparent_window.rs b/examples/window/transparent_window.rs index 21e68f09f06c5..2f8936bd8cc02 100644 --- a/examples/window/transparent_window.rs +++ b/examples/window/transparent_window.rs @@ -6,10 +6,12 @@ use bevy::{ fn main() { App::new() - .insert_resource(ClearColor(Color::rgba(0.0, 0.0, 0.0, 0.0))) + // rgba value needs to be [0, 0, 0, 0], otherwise some color will bleed through + .insert_resource(ClearColor(Color::TRANSPARENT)) .insert_resource(WindowDescriptor { - vsync: false, + // setting transparent allows the window to become transparent when clear color has the correct value transparent: true, + // Disabling window desoration to make it feel more like a widget than a window decorations: false, ..Default::default() }) From 3c568301bcdc18f13c022b056a5810c517c65ca4 Mon Sep 17 00:00:00 2001 From: Louis Johnson Date: Thu, 28 Oct 2021 22:24:15 +1000 Subject: [PATCH 03/10] replaced color with NONE const - removed transparent const color --- crates/bevy_render/src/color.rs | 1 - examples/window/transparent_window.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/bevy_render/src/color.rs b/crates/bevy_render/src/color.rs index 3b81a9a35a027..5c12053d3e4db 100644 --- a/crates/bevy_render/src/color.rs +++ b/crates/bevy_render/src/color.rs @@ -85,7 +85,6 @@ impl Color { pub const SILVER: Color = Color::rgb(0.75, 0.75, 0.75); pub const TEAL: Color = Color::rgb(0.0, 0.5, 0.5); pub const TOMATO: Color = Color::rgb(1.0, 0.39, 0.28); - pub const TRANSPARENT: Color = Color::rgba(0., 0., 0., 0.); pub const TURQUOISE: Color = Color::rgb(0.25, 0.88, 0.82); pub const VIOLET: Color = Color::rgb(0.93, 0.51, 0.93); pub const WHITE: Color = Color::rgb(1.0, 1.0, 1.0); diff --git a/examples/window/transparent_window.rs b/examples/window/transparent_window.rs index 2f8936bd8cc02..f39e084522ed5 100644 --- a/examples/window/transparent_window.rs +++ b/examples/window/transparent_window.rs @@ -7,7 +7,7 @@ use bevy::{ fn main() { App::new() // rgba value needs to be [0, 0, 0, 0], otherwise some color will bleed through - .insert_resource(ClearColor(Color::TRANSPARENT)) + .insert_resource(ClearColor(Color::NONE)) .insert_resource(WindowDescriptor { // setting transparent allows the window to become transparent when clear color has the correct value transparent: true, From 8ba1924409536e39a5495e4c37008f905c6d3dfb Mon Sep 17 00:00:00 2001 From: Louis Johnson Date: Thu, 28 Oct 2021 22:45:58 +1000 Subject: [PATCH 04/10] added support comment --- examples/window/transparent_window.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/window/transparent_window.rs b/examples/window/transparent_window.rs index f39e084522ed5..09734aeac3c23 100644 --- a/examples/window/transparent_window.rs +++ b/examples/window/transparent_window.rs @@ -1,3 +1,7 @@ +/// # Platform-specific +/// - iOS / Android / Web: Unsupported. +/// - OSX: Not working as expected. + use bevy::{ prelude::*, render::pass::ClearColor, From f8e02dd3c69c95affc16c7551eb57a157a6bdb4f Mon Sep 17 00:00:00 2001 From: louisgjohnson Date: Fri, 29 Oct 2021 14:01:29 +1000 Subject: [PATCH 05/10] updated example readme for transparent window --- examples/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/README.md b/examples/README.md index 7796afdb16223..b60820753de7d 100644 --- a/examples/README.md +++ b/examples/README.md @@ -247,6 +247,7 @@ Example | File | Description `clear_color` | [`window/clear_color.rs`](./window/clear_color.rs) | Creates a solid color window `multiple_windows` | [`window/multiple_windows.rs`](./window/multiple_windows.rs) | Creates two windows and cameras viewing the same mesh `scale_factor_override` | [`window/scale_factor_override.rs`](./window/scale_factor_override.rs) | Illustrates how to customize the default window settings +`transparent_window` | [`window/transparent_window.rs`](./window/transparent_window.rs) | Illustrates making the window transparent and hiding the window decoration `window_settings` | [`window/window_settings.rs`](./window/window_settings.rs) | Demonstrates customizing default window settings # Platform-Specific Examples From 183410ef8938d037bd9d8cc667d150fb01b0e990 Mon Sep 17 00:00:00 2001 From: Louis Johnson Date: Fri, 29 Oct 2021 15:41:40 +1000 Subject: [PATCH 06/10] cargo fmt --- examples/window/transparent_window.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/examples/window/transparent_window.rs b/examples/window/transparent_window.rs index 09734aeac3c23..6de01ed063aae 100644 --- a/examples/window/transparent_window.rs +++ b/examples/window/transparent_window.rs @@ -1,12 +1,7 @@ /// # Platform-specific /// - iOS / Android / Web: Unsupported. /// - OSX: Not working as expected. - -use bevy::{ - prelude::*, - render::pass::ClearColor, - window::WindowDescriptor, -}; +use bevy::{prelude::*, render::pass::ClearColor, window::WindowDescriptor}; fn main() { App::new() @@ -35,4 +30,4 @@ fn setup( material: materials.add(texture_handle.into()), ..Default::default() }); -} \ No newline at end of file +} From b40428cfd687df15bcc9b121de43eb82f6525a5d Mon Sep 17 00:00:00 2001 From: Louis Johnson Date: Mon, 1 Nov 2021 17:23:17 +1000 Subject: [PATCH 07/10] fixing up typo's --- examples/window/transparent_window.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/window/transparent_window.rs b/examples/window/transparent_window.rs index 6de01ed063aae..7b0878425ee53 100644 --- a/examples/window/transparent_window.rs +++ b/examples/window/transparent_window.rs @@ -5,12 +5,12 @@ use bevy::{prelude::*, render::pass::ClearColor, window::WindowDescriptor}; fn main() { App::new() - // rgba value needs to be [0, 0, 0, 0], otherwise some color will bleed through + // ClearColor must have 0 alpha, otherwise some color will bleed through .insert_resource(ClearColor(Color::NONE)) .insert_resource(WindowDescriptor { - // setting transparent allows the window to become transparent when clear color has the correct value + // Setting `transparent` allows the `ClearColor`'s alpha value to take effect transparent: true, - // Disabling window desoration to make it feel more like a widget than a window + // Disabling window decorations to make it feel more like a widget than a window decorations: false, ..Default::default() }) From cab7931667d68b994b7797fd6804752f6efa82e4 Mon Sep 17 00:00:00 2001 From: Louis Johnson Date: Mon, 8 Nov 2021 21:56:58 +1000 Subject: [PATCH 08/10] added docs to window transparent property --- crates/bevy_window/src/window.rs | 4 ++++ examples/window/transparent_window.rs | 5 ++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index f89023530b5e1..5a55883ef8719 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -534,6 +534,10 @@ pub struct WindowDescriptor { pub cursor_visible: bool, pub cursor_locked: bool, pub mode: WindowMode, + /// Sets whether the background of the window should be transparent. + /// # Platform-specific + /// - iOS / Android / Web: Unsupported. + /// - OSX: Not working as expected. pub transparent: bool, #[cfg(target_arch = "wasm32")] pub canvas: Option, diff --git a/examples/window/transparent_window.rs b/examples/window/transparent_window.rs index 7b0878425ee53..f30d5df97be8c 100644 --- a/examples/window/transparent_window.rs +++ b/examples/window/transparent_window.rs @@ -1,6 +1,5 @@ -/// # Platform-specific -/// - iOS / Android / Web: Unsupported. -/// - OSX: Not working as expected. +/// An example of how to display a window in transparent mode +/// [Documentation & Platform support.](https://github.com/bevyengine/bevy/blob/latest/crates/bevy_window/src/window.rs) use bevy::{prelude::*, render::pass::ClearColor, window::WindowDescriptor}; fn main() { From 0db8ffaac5f03682b1691be15ff2d5c4bede4b12 Mon Sep 17 00:00:00 2001 From: loui Date: Wed, 17 Nov 2021 20:50:02 +1000 Subject: [PATCH 09/10] updated comments based on code review feedback --- crates/bevy_window/src/window.rs | 4 +++- examples/window/transparent_window.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index babe3eb73d9af..6a75f49310c46 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -538,7 +538,9 @@ pub struct WindowDescriptor { /// Sets whether the background of the window should be transparent. /// # Platform-specific /// - iOS / Android / Web: Unsupported. - /// - OSX: Not working as expected. + /// - OSX / Linux : Not working as expected. + /// OSX transparent works with winit out of the box, so this issue might be related to: https://github.com/gfx-rs/wgpu/issues/687 + /// Linux now works with this pr merged in but bevy is using an older version of winit: https://github.com/rust-windowing/winit/pull/2006 pub transparent: bool, #[cfg(target_arch = "wasm32")] pub canvas: Option, diff --git a/examples/window/transparent_window.rs b/examples/window/transparent_window.rs index f30d5df97be8c..c56fd816d60f4 100644 --- a/examples/window/transparent_window.rs +++ b/examples/window/transparent_window.rs @@ -1,5 +1,5 @@ /// An example of how to display a window in transparent mode -/// [Documentation & Platform support.](https://github.com/bevyengine/bevy/blob/latest/crates/bevy_window/src/window.rs) +/// [Documentation & Platform support.](https://docs.rs/bevy/latest/bevy/prelude/struct.WindowDescriptor.html#structfield.transparent) use bevy::{prelude::*, render::pass::ClearColor, window::WindowDescriptor}; fn main() { From 18c58612392d7e87ef8fdd4a117f8db6574ee4e8 Mon Sep 17 00:00:00 2001 From: loui Date: Sat, 20 Nov 2021 10:26:52 +1000 Subject: [PATCH 10/10] fixing mistake in comment about winit version --- crates/bevy_window/src/window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 6a75f49310c46..cbbf0cac46cbf 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -540,7 +540,7 @@ pub struct WindowDescriptor { /// - iOS / Android / Web: Unsupported. /// - OSX / Linux : Not working as expected. /// OSX transparent works with winit out of the box, so this issue might be related to: https://github.com/gfx-rs/wgpu/issues/687 - /// Linux now works with this pr merged in but bevy is using an older version of winit: https://github.com/rust-windowing/winit/pull/2006 + /// Linux now works with this pr merged in, which should work with the next release of winit : https://github.com/rust-windowing/winit/pull/2006 pub transparent: bool, #[cfg(target_arch = "wasm32")] pub canvas: Option,