swamp_material/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
pub mod prelude;

use limnus_app::prelude::{App, Plugin};
use limnus_asset_id::{AssetName, RawWeakId};
use limnus_asset_registry::AssetRegistry;
use limnus_assets::Assets;
use limnus_assets_loader::{AssetLoader, ConversionError, WrappedAssetLoaderRegistry};
use limnus_resource::ResourceStorage;
use limnus_wgpu_window::BasicDeviceInfo;
use swamp_render_wgpu::{Material, Render};
use tracing::debug;

pub struct MaterialPlugin;

impl Plugin for MaterialPlugin {
    fn build(&self, app: &mut App) {
        {
            let registry = app.resource_mut::<WrappedAssetLoaderRegistry>();
            let loader = MaterialWgpuProcessor::new();

            registry.value.lock().unwrap().register_loader(loader);
        }

        app.insert_resource(Assets::<Material>::default());
    }
}

#[derive(Default)]
pub struct MaterialWgpuProcessor;

impl MaterialWgpuProcessor {
    pub fn new() -> Self {
        Self {}
    }
}

impl AssetLoader for MaterialWgpuProcessor {
    type AssetType = Material;

    fn convert_and_insert(
        &self,
        id: RawWeakId,
        octets: &[u8],
        resources: &mut ResourceStorage,
    ) -> Result<(), ConversionError> {
        let device_info = resources.fetch::<BasicDeviceInfo>();

        let name: AssetName;
        {
            let asset_container = resources.fetch::<AssetRegistry>();
            name = asset_container
                .name_raw(id)
                .expect("should know about this Id");
        }

        debug!("convert from png {name}");
        let img = image::load_from_memory_with_format(octets, image::ImageFormat::Png)
            .expect("Failed to load image");
        let img = img.to_rgba8();

        debug!("creating texture {name}");
        let wgpu_texture = swamp_wgpu_sprites::load_texture_from_memory(
            &device_info.device,
            &device_info.queue,
            img,
            name.value(),
        );

        debug!("creating material {name}");
        {
            let swamp_render_wgpu = resources.fetch_mut::<Render>();
            let wgpu_material = swamp_render_wgpu.material_from_texture(wgpu_texture, name.value());

            let image_assets = resources.fetch_mut::<Assets<Material>>();
            image_assets.set_raw(id, wgpu_material);
        }

        debug!("material complete {name}");

        Ok(())
    }
}