Trouv / bevy_ecs_ldtk

ECS-friendly ldtk plugin for bevy, leveraging bevy_ecs_tilemap
Other
641 stars 73 forks source link

Can't set atlas texture sampling to Nearest in WASM #212

Closed zment4 closed 11 months ago

zment4 commented 11 months ago

Decided to try out rust & Bevy & bevy_ecs_ldtk for a gamejam. I'm currently experiencing issues trying to set the atlas texture to nearest neighbour sampling.

Using the following code:

fn tilemap_sampler_fix(
    mut ev_asset: EventReader<AssetEvent<Image>>, 
    mut assets: ResMut<Assets<Image>>
) {
    for ev in ev_asset.iter() {
        match ev {
            AssetEvent::Created { handle } => {
                if let Some(texture) = assets.get_mut(&handle) {
                    info!("Setting texture sampling to nearest");
                    texture.sampler_descriptor = ImageSampler::nearest();
                }
            },
            _ => {}
        }
    }
}

On desktop runs, with just a simple cargo run I always get the correct texture sampling, and I get crisp pixels. However, when running cargo run --target wasm32-unknown-unknown, I randomly get either linear sampled pixels or nearest neighbour.

Sample images: image image

Simply just refreshing the page might or might not give out the correct result. I had an event reader for level loading setup at one point to try debug it and it seemed that sometimes the nearest neighbour gets set very very last, and in those cases it doesn't "stick" - maybe some scheduling issue?

I have no experience in either rust nor Bevy, so I'm at a bit of a loss here. Thanks for any possible help!

Here's the main code

fn main() {
    App::new()
        .add_plugins((
            DefaultPlugins.set(
                WindowPlugin {
                    primary_window: Some(Window {
                        title: "This is window, hello hi".to_string(),
                        resolution: (1024., 768.).into(),
                        present_mode: PresentMode::AutoVsync,
                        ..default()
                    }),
                    ..default()
                }
            ),
            LdtkPlugin))
        .insert_resource(LevelSelection::Uid(0))
        .insert_resource(LdtkSettings {
            level_spawn_behavior: LevelSpawnBehavior::UseWorldTranslation {
                load_level_neighbors: false
            },
            set_clear_color: SetClearColor::No,
            ..Default::default()
        })
        .add_systems(Startup, setup)
        .add_systems(Update, tilemap_sampler_fix)
        .run()
}

...and here is the link for the game jam project files https://github.com/zment4/rj-jam-game for full source

Trouv commented 11 months ago

Yeah things happening indeterministically is usually a sign of a scheduling issue. However, maybe we can avoid that - have you tried setting the sampling setting on the ImagePlugin? https://github.com/Trouv/bevy_ecs_ldtk/blob/9a10bc8dae321bfec798fc7b9ea140b4233ae177/examples/basic.rs#L7

zment4 commented 11 months ago

Yup that worked, thanks! Funny that I didn't come across the default sampler setting at all when looking this up. Must've missed it or something. Anyway, thanks for the help!