bevy_fly_camera icon indicating copy to clipboard operation
bevy_fly_camera copied to clipboard

Removing default bevy features

Open Type1J opened this issue 3 years ago • 4 comments

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 avatar Sep 30 '21 17:09 Type1J

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

Type1J avatar Sep 30 '21 23:09 Type1J

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...

zeerooth avatar Dec 26 '21 16:12 zeerooth

@mcpar-land Is this project still active?

Type1J avatar Dec 28 '21 00:12 Type1J

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.

PikminGuts92 avatar Jan 25 '22 07:01 PikminGuts92