From 3ca8844c9014747c8db18f92ea10650a27e7db08 Mon Sep 17 00:00:00 2001 From: loui Date: Wed, 8 Dec 2021 20:53:35 +0000 Subject: [PATCH] Added transparency to window builder (#3105) Applogies, had to recreate this pr because of branching issue. Old PR: https://github.com/bevyengine/bevy/pull/3033 # Objective Fixes #3032 Allowing a user to create a transparent window ## Solution I've allowed the transparent bool to be passed to the winit window builder --- Cargo.toml | 4 ++++ crates/bevy_window/src/window.rs | 8 +++++++ crates/bevy_winit/src/winit_windows.rs | 3 ++- examples/README.md | 1 + examples/window/transparent_window.rs | 32 ++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 examples/window/transparent_window.rs diff --git a/Cargo.toml b/Cargo.toml index 6b5267be70da8..7a112614a0d7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -523,6 +523,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 a9aa5844ad2af..83e1afbfc99b0 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -547,6 +547,13 @@ 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 / 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, 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, } @@ -566,6 +573,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 81dc6f537f9c5..db4d58b997347 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -79,7 +79,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/README.md b/examples/README.md index 8329c0908cc46..05ae0bc41ef62 100644 --- a/examples/README.md +++ b/examples/README.md @@ -262,6 +262,7 @@ Example | File | Description `clear_color_pipelined` | [`window/clear_color_pipelined.rs`](./window/clear_color_pipelined.rs) | Creates a solid color window with the pipelined renderer `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 diff --git a/examples/window/transparent_window.rs b/examples/window/transparent_window.rs new file mode 100644 index 0000000000000..c56fd816d60f4 --- /dev/null +++ b/examples/window/transparent_window.rs @@ -0,0 +1,32 @@ +/// An example of how to display a window in transparent mode +/// [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() { + App::new() + // ClearColor must have 0 alpha, otherwise some color will bleed through + .insert_resource(ClearColor(Color::NONE)) + .insert_resource(WindowDescriptor { + // Setting `transparent` allows the `ClearColor`'s alpha value to take effect + transparent: true, + // Disabling window decorations to make it feel more like a widget than a window + 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() + }); +}