forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using the `image` crate, HDR images can be loaded into RGBA-f32 textures.
- Loading branch information
Showing
7 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters