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

Fix wasm #992

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
257 changes: 38 additions & 219 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion bevy_nannou_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ proc-macro = true
[dependencies]
syn = { version = "1", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"

[dev-dependencies]
bevy = { workspace = true }
Expand Down
2 changes: 0 additions & 2 deletions bevy_nannou_draw/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ bevy = { workspace = true }
lyon = "1.0"
nannou_core = { path = "../nannou_core" }
rusttype = { version = "0.8", features = ["gpu_cache"] }
num-traits = "0.2"
bytemuck = "1.15.0"
rayon = { workspace = true }
uuid = "1.8"
bitflags = "2.6.0"
Expand Down
2 changes: 1 addition & 1 deletion bevy_nannou_draw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn reset_draw(mut draw_q: Query<&mut DrawHolder>) {
}
}

fn spawn_draw(mut commands: Commands, query: Query<Entity, Added<Window>>) {
fn spawn_draw(mut commands: Commands, query: Query<Entity, (Without<DrawHolder>, With<Window>)>) {
for entity in query.iter() {
commands
.entity(entity)
Expand Down
1 change: 0 additions & 1 deletion bevy_nannou_isf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition = "2021"

[dependencies]
bevy = { workspace = true, features = ["shader_format_glsl"] }
bevy_egui = { workspace = true }
bevy-inspector-egui = { workspace = true }
isf = "0.1.0"
thiserror = "1"
Expand Down
3 changes: 0 additions & 3 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ edition = "2018"

[dev-dependencies]
audrey = "0.3"
hotglsl = "0.2"
hrtf = "0.2"
nannou = { version ="0.19.0", path = "../nannou" }
nannou_audio = { version ="0.19.0", path = "../nannou_audio" }
nannou_laser = { version ="0.19.0", features = ["ffi", "ilda-idtf"], path = "../nannou_laser" }
nannou_osc = { version ="0.19.0", path = "../nannou_osc" }
pitch_calc = { version = "0.12", features = ["serde"] }
time_calc = { version= "0.13", features = ["serde"] }
walkdir = "2"
hound = "3.4.0"
ringbuf = "0.2.2"
Expand Down
27 changes: 27 additions & 0 deletions examples/wasm/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<style>
body {
background: linear-gradient(
135deg,
white 0%,
white 49%,
black 49%,
black 51%,
white 51%,
white 100%
) repeat;
background-size: 20px 20px;
}
canvas {
background-color: white;
}
</style>
<title>Wasm Example</title>
</head>
<script type="module">
import init from './target/draw.js'
init()
</script>
</html>
10 changes: 2 additions & 8 deletions nannou/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,28 @@ bevy_egui = { workspace = true, optional = true }
bevy_common_assets = { workspace = true, optional = true }
bevy_nannou = { version = "0.1.0", path = "../bevy_nannou" }
bevy_nannou_derive = { version = "0.1.0", path = "../bevy_nannou_derive" }
futures = "0.3"
find_folder = "0.3"
getrandom = "0.2.3"
instant = "0.1.9"
lyon = "1.0"
nannou_core = { version ="0.19.0", path = "../nannou_core", features = ["std"] }
nannou_wgpu = { version = "0.19.0", path = "../nannou_wgpu", features = ["capturer"]}
noise = "0.7"
notosans = { version = "0.1", optional = true }
num_cpus = "1"
pennereq = "0.3"
rusttype = { version = "0.8", features = ["gpu_cache"] }
serde = { workspace = true, features = ["derive"], optional = true}
serde_json = { workspace = true, optional = true }
toml = { workspace = true, optional = true }
walkdir = "2"
web-sys = { version = "0.3.64", optional = true }
wgpu = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1", features = ["full"]}
image = { workspace = true, features = ["default"] }
[target.'cfg(target_arch = "wasm32")'.dependencies]
bevy = { workspace = true, features = ["webgpu"] }
tokio = { version = "1", features = ["rt"]}
image = { workspace = true, features = [], default-features = false }

[features]
default = ["notosans"]
default = []
egui = ["bevy_egui", "bevy-inspector-egui"]
hot_reload = ["bevy/file_watcher"]
isf = ["bevy_nannou/isf"]
Expand Down
15 changes: 11 additions & 4 deletions nannou/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,17 @@ where
let mut app = bevy::app::App::new();
app.add_plugins((
DefaultPlugins.set(WindowPlugin {
#[cfg(not(target_arch = "wasm32"))]
// Don't spawn a window by default, we'll handle this ourselves
primary_window: None,
#[cfg(target_arch = "wasm32")]
// We create a default window on wasm to make sure that the render initialization
// has a canvas to attach to when configuring the surface.
primary_window: Some(Window {
title: "Nannou".to_string(),
resolution: (1024.0, 768.0).into(),
..default()
}),
exit_condition: ExitCondition::OnAllClosed,
..default()
}),
Expand Down Expand Up @@ -1061,10 +1070,8 @@ where
};
}
};
#[cfg(not(target_os = "unknown"))]
{
let _ = window.primary().build();
}

let _ = window.primary().build();
}

// Initialise the model.
Expand Down
52 changes: 40 additions & 12 deletions nannou/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,26 +410,54 @@ where
self
}

#[cfg(not(target_os = "unknown"))]
/// Builds the window, inserts it into the `App`'s display map and returns the unique ID.
pub fn build(self) -> Entity {
let entity = self
.app
.component_world_mut()
.spawn((self.window, WindowUserFunctions(self.user_functions)))
.id();
if cfg!(target_arch = "wasm32") && !self.primary {
// TODO: figure out a way to dynamically attach to a canvas with new windows
panic!("Non-primary windows are not supported on wasm");
}

let entity = if !cfg!(target_arch = "wasm32") {
let entity = self
.app
.component_world_mut()
.spawn((self.window, WindowUserFunctions(self.user_functions)))
.id();

if self.primary {
let mut q = self.app.component_world_mut().query::<&PrimaryWindow>();
if q.get_single(&mut self.app.component_world_mut()).is_ok() {
panic!("Only one primary window can be created");
}

if self.primary {
let mut q = self.app.component_world_mut().query::<&PrimaryWindow>();
if q.get_single(&mut self.app.component_world_mut()).is_ok() {
panic!("Only one primary window can be created");
self.app
.component_world_mut()
.entity_mut(entity)
.insert(PrimaryWindow);
}

entity
} else {
let mut q = self
.app
.component_world_mut()
.query_filtered::<(Entity, &mut bevy::window::Window), With<PrimaryWindow>>();
let entity = if let Ok((entity, mut window)) =
q.get_single_mut(&mut self.app.component_world_mut())
{
*window = self.window;
entity
} else {
panic!("No primary window found");
};

self.app
.component_world_mut()
.entity_mut(entity)
.insert(PrimaryWindow);
}
.insert(WindowUserFunctions(self.user_functions));

entity
};

let layer = RenderLayers::layer(self.app.window_count());

Expand Down
4 changes: 1 addition & 3 deletions nannou_wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ edition = "2018"
[dependencies]
futures = "0.3"
image = { workspace = true, optional = true }
instant = { version = "0.1.9", optional = true }
num_cpus = { version = "1", optional = true }
wgpu = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand All @@ -22,7 +20,7 @@ tokio = { version = "1", features = ["full"]}
tokio = { version = "1", features = ["rt"]}

[features]
capturer = ["image", "instant", "num_cpus"]
capturer = ["image"]
replay = ["wgpu/replay"]
serde = ["wgpu/serde"]
spirv = ["wgpu/spirv"]
Expand Down