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

Removing default bevy features #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Type1J
Copy link

@Type1J Type1J commented Sep 30, 2021

Hey, great library! I was trying out a scene with this library on native, and it worked perfectly!

However, when I tried to make a wasm build to allow some others to view the scene with their web browser without installing anything, it brought in some libraries that don't work in wasm, but are part of bevy's default features. Because crates that use the library can't disable features (it can only enable them), this change removes the bevy default features, which allows the library to work in all environments.

There is an issue where the mouse events are not being processed, but I believe that it's a bevy issue, and not an issue with this library. However, L-Shift and Space still do what they are supposed to do.

To allow you to test, I'll drop in my Cargo.toml, main.rs, and lib.rs here.

Cargo.toml:

[package]
name = "bevy_app"
version = "0.1.0"
authors = []
edition = "2018"
resolver = "2"

[lib]
name = "bevy_app_lib"
crate-type = ["cdylib", "rlib"]

# Dependencies for all targets go here.
[dependencies]
rand = "0.7"
getrandom = {version="0.1", features=["wasm-bindgen"]}
wasm-bindgen = "0.2"
winit = "0.24.0"
bevy_fly_camera = { path = "../bevy_fly_camera" }

# Dependencies for native only.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
bevy = {version = "0.5", default-features = false, features = ["bevy_wgpu", "bevy_winit", "render", "png", "x11"]}

# Dependencies for WASM only.
[target.'cfg(target_arch = "wasm32")'.dependencies]
bevy = {version = "0.5", default-features = false, features = ["bevy_winit", "render", "png"]}
bevy_webgl2 = "0.5"

main.rs

fn main() {
    bevy_app_lib::run();
}

lib.rs

use bevy::prelude::*;
use bevy_fly_camera::{FlyCamera, FlyCameraPlugin};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn run() {
    let mut app = App::build();
    app.insert_resource(Msaa { samples: 4 })
        .add_plugins(DefaultPlugins)
        .add_plugin(FlyCameraPlugin);
    #[cfg(target_arch = "wasm32")]
    app.add_plugin(bevy_webgl2::WebGL2Plugin);
    app.add_startup_system(setup.system()).run();
}

/// set up a simple 3D scene
fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    // add entities to the world
    // plane
    commands.spawn_bundle(PbrBundle {
        mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
        material: materials.add(Color::rgb(0.5, 0.3, 0.3).into()),
        ..Default::default()
    });
    // cube
    commands.spawn_bundle(PbrBundle {
        mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
        material: materials.add(Color::rgb(0.6, 0.8, 0.7).into()),
        transform: Transform::from_translation(Vec3::new(0.0, 0.5, 0.0)),
        ..Default::default()
    });
    // light
    commands.spawn_bundle(LightBundle {
        transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
        ..Default::default()
    });
    // camera
    commands.spawn_bundle(PerspectiveCameraBundle {
        transform: Transform::from_translation(Vec3::new(3.0, 2.5, 5.0))
            .looking_at(Vec3::default(), Vec3::Y),
        ..Default::default()
    })
        .insert(FlyCamera::default());
}

To run native version:

cargo run --release

To run wasm version:

cargo install wasm-pack
cargo install basic-http-server
wasm-pack build --target web --release

Then, for Windows:

copy index.html pkg\
cd pkg
.\basic-http-server

...or for macOS or Linux:

cp index.html pkg/
cd pkg
./basic-http-server

@Type1J
Copy link
Author

Type1J commented Sep 30, 2021

It looks like some features may be necessary. I'll try to build those checks, and figure out which ones.

@zeerooth
Copy link

I'd be amazing if this PR were to be finally merged, I'm stuck in the same situation where this project doesn't build for wasm at all...

@Type1J
Copy link
Author

Type1J commented Dec 28, 2021

@mcpar-land Is this project still active?

@PikminGuts92
Copy link
Contributor

I'd like to see this merged. There's a lot of default features in bevy that I'm not using. Workaround for me is to not use DefaultPlugins and instead inject each plugin I want manually. This greatly reduces the output binary size for me. However, actually removing imports at the feature level could also improve build time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants