2021-12-14 03:58:23 +00:00
|
|
|
use crate::texture::{Image, TextureFormatPixelInfo};
|
2020-07-26 22:06:01 +00:00
|
|
|
use anyhow::Result;
|
2020-10-18 20:48:15 +00:00
|
|
|
use bevy_asset::{AssetLoader, LoadContext, LoadedAsset};
|
2020-10-20 00:29:31 +00:00
|
|
|
use bevy_utils::BoxedFuture;
|
2021-12-14 03:58:23 +00:00
|
|
|
use wgpu::{Extent3d, TextureDimension, TextureFormat};
|
2020-07-26 22:06:01 +00:00
|
|
|
|
2020-08-16 03:27:41 +00:00
|
|
|
/// Loads HDR textures as Texture assets
|
2020-07-26 22:06:01 +00:00
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct HdrTextureLoader;
|
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
impl AssetLoader for HdrTextureLoader {
|
2020-10-20 00:29:31 +00:00
|
|
|
fn load<'a>(
|
|
|
|
&'a self,
|
|
|
|
bytes: &'a [u8],
|
|
|
|
load_context: &'a mut LoadContext,
|
|
|
|
) -> BoxedFuture<'a, Result<()>> {
|
|
|
|
Box::pin(async move {
|
|
|
|
let format = TextureFormat::Rgba32Float;
|
|
|
|
debug_assert_eq!(
|
|
|
|
format.pixel_size(),
|
|
|
|
4 * 4,
|
|
|
|
"Format should have 32bit x 4 size"
|
|
|
|
);
|
|
|
|
|
2022-05-28 02:00:55 +00:00
|
|
|
let decoder = image::codecs::hdr::HdrDecoder::new(bytes)?;
|
2020-10-20 00:29:31 +00:00
|
|
|
let info = decoder.metadata();
|
|
|
|
let rgb_data = decoder.read_image_hdr()?;
|
|
|
|
let mut rgba_data = Vec::with_capacity(rgb_data.len() * format.pixel_size());
|
|
|
|
|
|
|
|
for rgb in rgb_data {
|
|
|
|
let alpha = 1.0f32;
|
|
|
|
|
|
|
|
rgba_data.extend_from_slice(&rgb.0[0].to_ne_bytes());
|
|
|
|
rgba_data.extend_from_slice(&rgb.0[1].to_ne_bytes());
|
|
|
|
rgba_data.extend_from_slice(&rgb.0[2].to_ne_bytes());
|
|
|
|
rgba_data.extend_from_slice(&alpha.to_ne_bytes());
|
|
|
|
}
|
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
let texture = Image::new(
|
|
|
|
Extent3d {
|
|
|
|
width: info.width,
|
|
|
|
height: info.height,
|
|
|
|
depth_or_array_layers: 1,
|
|
|
|
},
|
2020-11-22 20:04:47 +00:00
|
|
|
TextureDimension::D2,
|
2020-10-20 00:29:31 +00:00
|
|
|
rgba_data,
|
|
|
|
format,
|
|
|
|
);
|
|
|
|
|
|
|
|
load_context.set_default_asset(LoadedAsset::new(texture));
|
|
|
|
Ok(())
|
|
|
|
})
|
2020-07-26 22:06:01 +00:00
|
|
|
}
|
2020-07-28 21:24:03 +00:00
|
|
|
|
2020-07-26 22:06:01 +00:00
|
|
|
fn extensions(&self) -> &[&str] {
|
2020-11-17 21:40:18 +00:00
|
|
|
&["hdr"]
|
2020-07-26 22:06:01 +00:00
|
|
|
}
|
|
|
|
}
|