Get wasm build running in browser

This commit is contained in:
Danny McGee
2023-09-28 23:33:08 -04:00
parent 842f985c23
commit bc8994a780
28 changed files with 3832 additions and 131 deletions
+1
View File
@@ -0,0 +1 @@
RUSTFLAGS = "--cfg=web_sys_unstable_apis"
+72 -5
View File
@@ -3,17 +3,84 @@ name = "hello-world"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
bevy = { workspace = true }
wasm-bindgen = { version = "0.2.84", optional = true }
console_error_panic_hook = { version = "0.1.7", optional = true }
[target.'cfg(target_family = "wasm")'.dependencies]
console_error_panic_hook = "0.1.7"
[features]
wasm = [
"dep:wasm-bindgen",
"dep:console_error_panic_hook",
]
[dependencies.bevy]
workspace = true
default-features = false
# TODO: Narrow this down to the features we actually need
# FIXME: WebGPU is sparsely supported, but using the default WebGL backend
# causes a panic upstream: "Tex storage 2D multisample is not supported"
features = [
"animation",
"bevy_asset",
"bevy_audio",
"bevy_gilrs",
"bevy_scene",
"bevy_winit",
"bevy_core_pipeline",
"bevy_pbr",
"bevy_gltf",
"bevy_render",
"bevy_sprite",
"bevy_text",
"bevy_ui",
"png",
"hdr",
"ktx2",
"zstd",
"vorbis",
"x11",
"filesystem_watcher",
"bevy_gizmos",
"android_shared_stdcxx",
"tonemapping_luts",
"default_font",
]
[target.'cfg(not(target_family = "wasm"))'.dependencies.bevy]
workspace = true
features = ["dynamic_linking"]
default-features = false
features = [
"animation",
"bevy_asset",
"bevy_audio",
"bevy_gilrs",
"bevy_scene",
"bevy_winit",
"bevy_core_pipeline",
"bevy_pbr",
"bevy_gltf",
"bevy_render",
"bevy_sprite",
"bevy_text",
"bevy_ui",
"png",
"hdr",
"ktx2",
"zstd",
"vorbis",
"x11",
"filesystem_watcher",
"bevy_gizmos",
"android_shared_stdcxx",
"tonemapping_luts",
"default_font",
# TODO: This line is the only difference from default. Is there a way to
# deduplicate the feature list?
"dynamic_linking",
]
# FIXME: Profiles for the non-root package are ignored, but [target] keys are
# not allowed in the root package >_<
+8 -4
View File
@@ -12,13 +12,17 @@
"configurations": {
"production": {
"release": true
},
"web": {
"release": true,
"target": "wasm32-unknown-unknown"
}
}
},
"build:web": {
"executor": "nx:run-commands",
"options": {
"command": "wasm-pack build --out-dir ../dist/hello-world/ -- --features wasm",
"cwd": "hello-world",
"env": "hello-world/.env"
}
},
"run": {
"executor": "@nxrs/cargo:build",
"options": {
+41
View File
@@ -0,0 +1,41 @@
#![allow(clippy::type_complexity)]
use bevy::prelude::*;
use wasm_bindgen::prelude::*;
mod orbit_cam;
mod test_scene;
mod ui;
use orbit_cam::{OrbitCam, OrbitCamPlugin};
use test_scene::TestScenePlugin;
use ui::UiLayerPlugin;
#[wasm_bindgen]
pub fn init() {
#[cfg(target_arch = "wasm32")]
console_error_panic_hook::set_once();
App::new()
.add_plugins((
DefaultPlugins,
OrbitCamPlugin,
TestScenePlugin,
UiLayerPlugin,
))
.add_systems(Update, draw_world_axes.after(orbit_cam::pan_orbit_camera))
.run();
}
fn draw_world_axes(mut gizmos: Gizmos, q: Query<&OrbitCam>) {
let &OrbitCam { focus, radius, .. } = q.single();
let mut draw_axis = |direction: Vec3, color: Color| {
let end = focus + direction * radius / 15.;
gizmos.line(focus, end, color);
};
draw_axis(Vec3::X, Color::RED);
draw_axis(Vec3::Y, Color::GREEN);
draw_axis(Vec3::Z, Color::BLUE);
}
+1 -37
View File
@@ -1,39 +1,3 @@
#![allow(clippy::type_complexity)]
use bevy::prelude::*;
mod orbit_cam;
mod test_scene;
mod ui;
use orbit_cam::{OrbitCam, OrbitCamPlugin};
use test_scene::TestScenePlugin;
use ui::UiLayerPlugin;
fn main() {
#[cfg(target_arch = "wasm32")]
console_error_panic_hook::set_once();
App::new()
.add_plugins((
DefaultPlugins,
OrbitCamPlugin,
TestScenePlugin,
UiLayerPlugin,
))
.add_systems(Update, draw_world_axes.after(orbit_cam::pan_orbit_camera))
.run();
}
fn draw_world_axes(mut gizmos: Gizmos, q: Query<&OrbitCam>) {
let &OrbitCam { focus, radius, .. } = q.single();
let mut draw_axis = |direction: Vec3, color: Color| {
let end = focus + direction * radius / 15.;
gizmos.line(focus, end, color);
};
draw_axis(Vec3::X, Color::RED);
draw_axis(Vec3::Y, Color::GREEN);
draw_axis(Vec3::Z, Color::BLUE);
hello_world::init();
}
+69 -65
View File
@@ -1,37 +1,41 @@
// TODO: Figure out asset paths/loading for web
#[rustfmt::skip]
use bevy::{
pbr::NotShadowCaster,
// pbr::NotShadowCaster,
prelude::*,
reflect::{TypePath, TypeUuid},
render::render_resource::{AsBindGroup, ShaderRef},
// reflect::{TypePath, TypeUuid},
// render::render_resource::{AsBindGroup, ShaderRef},
};
use crate::orbit_cam::{self, OrbitCam};
// use crate::orbit_cam::{self, OrbitCam};
pub struct TestScenePlugin;
impl Plugin for TestScenePlugin {
fn build(&self, app: &mut App) {
app.insert_resource(ClearColor(Color::hsl(0.0, 0.0, 0.25)))
.add_plugins(MaterialPlugin::<ProceduralGridMaterial> {
prepass_enabled: true,
..default()
})
// .add_plugins(MaterialPlugin::<ProceduralGridMaterial> {
// prepass_enabled: true,
// ..default()
// })
.add_systems(Startup, spawn_scene)
.add_systems(
PostUpdate,
move_plane_with_camera.after(orbit_cam::pan_orbit_camera),
);
// .add_systems(
// PostUpdate,
// move_plane_with_camera.after(orbit_cam::pan_orbit_camera),
// );
;
}
}
#[derive(Component)]
struct WorldGridPlane;
// #[derive(Component)]
// struct WorldGridPlane;
fn spawn_scene(
mut cmd: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut std_mats: ResMut<Assets<StandardMaterial>>,
mut mat_proc_grid: ResMut<Assets<ProceduralGridMaterial>>,
// mut mat_proc_grid: ResMut<Assets<ProceduralGridMaterial>>,
) {
let mut light_xform = Transform::IDENTITY;
light_xform.rotation = Quat::from_rotation_y(f32::to_radians(-70.0)) * light_xform.rotation;
@@ -59,60 +63,60 @@ fn spawn_scene(
});
// Plane
let plane = meshes.add(Mesh::from(shape::Plane {
size: 200.0,
subdivisions: 0,
}));
let proc_grid = mat_proc_grid.add(ProceduralGridMaterial {});
// let plane = meshes.add(Mesh::from(shape::Plane {
// size: 200.0,
// subdivisions: 0,
// }));
// let proc_grid = mat_proc_grid.add(ProceduralGridMaterial {});
cmd.spawn((
SpatialBundle::from_transform(Transform::IDENTITY),
WorldGridPlane,
))
.with_children(|parent| {
parent.spawn((
MaterialMeshBundle {
mesh: plane.clone(),
material: proc_grid.clone(),
transform: Transform::IDENTITY,
..default()
},
NotShadowCaster,
));
parent.spawn((
MaterialMeshBundle {
mesh: plane,
material: proc_grid,
transform: Transform::IDENTITY.looking_to(Vec3::Z, Vec3::NEG_Y),
..default()
},
NotShadowCaster,
));
});
// cmd.spawn((
// SpatialBundle::from_transform(Transform::IDENTITY),
// WorldGridPlane,
// ))
// .with_children(|parent| {
// parent.spawn((
// MaterialMeshBundle {
// mesh: plane.clone(),
// material: proc_grid.clone(),
// transform: Transform::IDENTITY,
// ..default()
// },
// NotShadowCaster,
// ));
// parent.spawn((
// MaterialMeshBundle {
// mesh: plane,
// material: proc_grid,
// transform: Transform::IDENTITY.looking_to(Vec3::Z, Vec3::NEG_Y),
// ..default()
// },
// NotShadowCaster,
// ));
// });
}
fn move_plane_with_camera(
mut plane: Query<(&WorldGridPlane, &mut Transform)>,
camera: Query<&OrbitCam, Changed<OrbitCam>>,
) {
if let Ok(&OrbitCam { focus, .. }) = camera.get_single() {
let (_, mut xform) = plane.single_mut();
// fn move_plane_with_camera(
// mut plane: Query<(&WorldGridPlane, &mut Transform)>,
// camera: Query<&OrbitCam, Changed<OrbitCam>>,
// ) {
// if let Ok(&OrbitCam { focus, .. }) = camera.get_single() {
// let (_, mut xform) = plane.single_mut();
xform.translation.x = focus.x;
xform.translation.z = focus.z;
}
}
// xform.translation.x = focus.x;
// xform.translation.z = focus.z;
// }
// }
#[derive(AsBindGroup, Clone, TypePath, TypeUuid)]
#[uuid = "CE331A13-F247-4F5B-A75E-670B2BD5776A"]
struct ProceduralGridMaterial {}
// #[derive(AsBindGroup, Clone, TypePath, TypeUuid)]
// #[uuid = "CE331A13-F247-4F5B-A75E-670B2BD5776A"]
// struct ProceduralGridMaterial {}
impl Material for ProceduralGridMaterial {
fn fragment_shader() -> ShaderRef {
"shaders/procedural_grid.wgsl".into()
}
// impl Material for ProceduralGridMaterial {
// fn fragment_shader() -> ShaderRef {
// "shaders/procedural_grid.wgsl".into()
// }
fn alpha_mode(&self) -> AlphaMode {
AlphaMode::Blend
}
}
// fn alpha_mode(&self) -> AlphaMode {
// AlphaMode::Blend
// }
// }