mirror of
https://github.com/bevyengine/bevy
synced 2024-11-21 20:23:28 +00:00
add HDR image loader
Using the `image` crate, HDR images can be loaded into RGBA-f32 textures.
This commit is contained in:
parent
fb9f04ba90
commit
7412b0ec25
7 changed files with 52 additions and 3 deletions
|
@ -15,3 +15,4 @@
|
|||
## Assets
|
||||
|
||||
* 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)
|
BIN
assets/textures/spiaggia_di_mondello_1k.hdr
Normal file
BIN
assets/textures/spiaggia_di_mondello_1k.hdr
Normal file
Binary file not shown.
|
@ -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"] }
|
||||
|
|
|
@ -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
|
||||
|
@ -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>()
|
||||
|
|
43
crates/bevy_render/src/texture/hdr_texture_loader.rs
Normal file
43
crates/bevy_render/src/texture/hdr_texture_loader.rs
Normal 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
|
||||
}
|
||||
}
|
|
@ -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::*;
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue