add HDR image loader

Using the `image` crate, HDR images can be loaded into RGBA-f32 textures.
This commit is contained in:
Thomas Herzog 2020-07-27 00:06:01 +02:00
parent fb9f04ba90
commit 7412b0ec25
7 changed files with 52 additions and 3 deletions

View file

@ -15,3 +15,4 @@
## Assets ## Assets
* Generic RPG Pack (CC0 license) by [Bakudas](https://twitter.com/bakudas) and [Gabe Fern](https://twitter.com/_Gabrielfer) * Generic RPG Pack (CC0 license) by [Bakudas](https://twitter.com/bakudas) and [Gabe Fern](https://twitter.com/_Gabrielfer)
* Environment maps (`.hdr` files) from [HDRIHaven](https://hdrihaven.com) (CC0 license)

Binary file not shown.

View file

@ -22,6 +22,7 @@ spirv-reflect = "0.2.3"
glsl-to-spirv = { git = "https://github.com/cart/glsl-to-spirv" } glsl-to-spirv = { git = "https://github.com/cart/glsl-to-spirv" }
# TODO: move this to its own crate # TODO: move this to its own crate
png = "0.16.0" png = "0.16.0"
image = "0.23"
# misc # misc
log = { version = "0.4", features = ["release_max_level_info"] } log = { version = "0.4", features = ["release_max_level_info"] }

View file

@ -38,7 +38,7 @@ use render_graph::{
}; };
use renderer::{AssetRenderResourceBindings, RenderResourceBindings}; use renderer::{AssetRenderResourceBindings, RenderResourceBindings};
use std::ops::Range; use std::ops::Range;
use texture::{PngTextureLoader, TextureResourceSystemState}; use texture::{HdrTextureLoader, PngTextureLoader, TextureResourceSystemState};
pub mod stage { pub mod stage {
/// Stage where render resources are set up /// Stage where render resources are set up
@ -75,6 +75,7 @@ impl AppPlugin for RenderPlugin {
.add_asset::<Texture>() .add_asset::<Texture>()
.add_asset::<Shader>() .add_asset::<Shader>()
.add_asset::<PipelineDescriptor>() .add_asset::<PipelineDescriptor>()
.add_asset_loader::<Texture, HdrTextureLoader>()
.add_asset_loader::<Texture, PngTextureLoader>() .add_asset_loader::<Texture, PngTextureLoader>()
.register_component::<Camera>() .register_component::<Camera>()
.register_component::<Draw>() .register_component::<Draw>()

View file

@ -0,0 +1,43 @@
use super::{Texture, TextureFormat};
use anyhow::Result;
use bevy_asset::AssetLoader;
use bevy_math::Vec2;
use std::path::Path;
#[derive(Clone, Default)]
pub struct HdrTextureLoader;
impl AssetLoader<Texture> for HdrTextureLoader {
fn from_bytes(&self, _asset_path: &Path, bytes: Vec<u8>) -> Result<Texture> {
let format = TextureFormat::Rgba32Float;
debug_assert_eq!(
format.pixel_size(),
4 * 4 * 1,
"Format should have 32bit x 4 size"
);
let decoder = image::hdr::HdrDecoder::new(bytes.as_slice())?;
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());
}
Ok(Texture::new(
Vec2::new(info.width as f32, info.height as f32),
rgba_data,
format,
))
}
fn extensions(&self) -> &[&str] {
static EXTENSIONS: &[&str] = &["hdr"];
EXTENSIONS
}
}

View file

@ -1,9 +1,11 @@
mod hdr_texture_loader;
mod png_texture_loader; mod png_texture_loader;
mod sampler_descriptor; mod sampler_descriptor;
mod texture; mod texture;
mod texture_descriptor; mod texture_descriptor;
mod texture_dimension; mod texture_dimension;
pub use hdr_texture_loader::*;
pub use png_texture_loader::*; pub use png_texture_loader::*;
pub use sampler_descriptor::*; pub use sampler_descriptor::*;
pub use texture::*; pub use texture::*;

View file

@ -67,7 +67,8 @@ impl Texture {
self.size = size; self.size = size;
let width = size.x() as usize; let width = size.x() as usize;
let height = size.y() as usize; let height = size.y() as usize;
self.data.resize(width * height * self.format.pixel_size(), 0); self.data
.resize(width * height * self.format.pixel_size(), 0);
} }
pub fn texture_resource_system( pub fn texture_resource_system(