FraserLee / bevy_sprite3d

Use sprites in a 3d bevy scene.
MIT License
144 stars 22 forks source link

panicked at 'called `Option::unwrap()` on a `None` value' #5

Closed rayanmargham closed 1 year ago

rayanmargham commented 1 year ago

What's the Error? when attempting to spawn a sprite3d and upon calling bundle this line of code panics: let image_size = params.images.get(&self.image).unwrap().texture_descriptor.size; however bevy is able to load the image just fine. here's my full code.


use bevy::prelude::*;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_sprite3d::Sprite3d;
use bevy_sprite3d::*;
use std::sync::Mutex;
use kira::{
    manager::{
        AudioManager, AudioManagerSettings,
        backend::cpal::CpalBackend,
    },
    sound::streaming::{StreamingSoundData, StreamingSoundSettings},
};
#[derive(Resource)]
struct Epic
{
    manager: Mutex<AudioManager>
}

fn main() {
    let mut manager = Epic { manager: AudioManager::<CpalBackend>::new(AudioManagerSettings::default()).unwrap().into()};
    App::new()
        .insert_resource(manager)
        .add_plugins(DefaultPlugins)
        .add_plugin(Sprite3dPlugin)
        .add_plugin(WorldInspectorPlugin::new())
        .add_startup_system(setup)
        .run();
}

fn setup(mut manager: ResMut<Epic>, mut commands: Commands,  asset_server: Res<AssetServer>,  mut sprite_params : Sprite3dParams) 
{
    println!("do something");

    let sound_data = StreamingSoundData::from_file("assets/audio/isle.mp3", StreamingSoundSettings::default()).unwrap();

    manager.manager.lock().unwrap().play(sound_data).unwrap();
    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(0.0, 0.0, 0.0),
        ..default()
    });
    let image = asset_server.load("bf.png");

    commands.spawn(Sprite3d
    {
        image: image,
        unlit: true,
        ..default()
    }.bundle(&mut sprite_params));
}
rayanmargham commented 1 year ago

Please respond ASAP, I need this plugin for a project

FraserLee commented 1 year ago

asset_server.load("bf.png"); doesn't immediately load the image, instead it queues all assets to load later in the background. At the point that you call .bundle(&mut sprite_params)); bevy_sprite3d needs the image to be loaded in order to construct the mesh.

To fix this either add a loading stage as shown in examples/sprite.rs or use bevy asset loader as shown in examples/bevy_asset_loader/sprite.rs (this example isn't yet updated to 0.10, though the changes are pretty minor).

More info: https://github.com/FraserLee/bevy_sprite3d/blob/main/readme.md?plain=1#L92-L98 https://docs.rs/bevy/latest/bevy/asset/struct.AssetServer.html#method.load https://bevy-cheatbook.github.io/assets/assetserver.html

I'm cramming for finals rn, will look into adding a more informative error message when I get a sec. Ask away and re-open this issue if you need any help though I might not respond for a while. Good luck!