Skip to content

Commit

Permalink
add HDR image loader
Browse files Browse the repository at this point in the history
Using the `image` crate, HDR images can be loaded into RGBA-f32 textures.
  • Loading branch information
karroffel committed Jul 27, 2020
1 parent fb9f04b commit 7412b0e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CREDITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@

## 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 added assets/textures/spiaggia_di_mondello_1k.hdr
Binary file not shown.
1 change: 1 addition & 0 deletions crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ spirv-reflect = "0.2.3"
glsl-to-spirv = { git = "https://github.com/cart/glsl-to-spirv" }
# TODO: move this to its own crate
png = "0.16.0"
image = "0.23"

# misc
log = { version = "0.4", features = ["release_max_level_info"] }
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use render_graph::{
};
use renderer::{AssetRenderResourceBindings, RenderResourceBindings};
use std::ops::Range;
use texture::{PngTextureLoader, TextureResourceSystemState};
use texture::{HdrTextureLoader, PngTextureLoader, TextureResourceSystemState};

pub mod stage {
/// Stage where render resources are set up
Expand Down Expand Up @@ -75,6 +75,7 @@ impl AppPlugin for RenderPlugin {
.add_asset::<Texture>()
.add_asset::<Shader>()
.add_asset::<PipelineDescriptor>()
.add_asset_loader::<Texture, HdrTextureLoader>()
.add_asset_loader::<Texture, PngTextureLoader>()
.register_component::<Camera>()
.register_component::<Draw>()
Expand Down
43 changes: 43 additions & 0 deletions crates/bevy_render/src/texture/hdr_texture_loader.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 2 additions & 0 deletions crates/bevy_render/src/texture/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod hdr_texture_loader;
mod png_texture_loader;
mod sampler_descriptor;
mod texture;
mod texture_descriptor;
mod texture_dimension;

pub use hdr_texture_loader::*;
pub use png_texture_loader::*;
pub use sampler_descriptor::*;
pub use texture::*;
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/texture/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ impl Texture {
self.size = size;
let width = size.x() 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(
Expand Down

0 comments on commit 7412b0e

Please sign in to comment.