Skip to content

Commit

Permalink
add ability to load .dds, .tga, and .jpeg texture formats (#1038)
Browse files Browse the repository at this point in the history
add ability to load `.dds`, `.tga`, and `.jpeg` texture formats
  • Loading branch information
blunted2night authored Dec 10, 2020
1 parent 4a5bccc commit 9239621
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ wgpu_trace = ["bevy_internal/wgpu_trace"]
# Image format support for texture loading (PNG and HDR are enabled by default)
hdr = ["bevy_internal/hdr"]
png = ["bevy_internal/png"]
dds = ["bevy_internal/dds"]
tga = ["bevy_internal/tga"]
jpeg = ["bevy_internal/jpeg"]

# Audio format support (MP3 is enabled by default)
flac = ["bevy_internal/flac"]
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ trace_chrome = [ "bevy_log/tracing-chrome" ]
# Image format support for texture loading (PNG and HDR are enabled by default)
hdr = ["bevy_render/hdr"]
png = ["bevy_render/png"]
dds = ["bevy_render/dds"]
tga = ["bevy_render/tga"]
jpeg = ["bevy_render/jpeg"]

# Audio format support (MP3 is enabled by default)
flac = ["bevy_audio/flac"]
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ shaderc = "0.7.0"
[features]
png = ["image/png"]
hdr = ["image/hdr"]
dds = ["image/dds"]
tga = ["image/tga"]
jpeg = ["image/jpeg"]
15 changes: 8 additions & 7 deletions crates/bevy_render/src/texture/image_texture_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use bevy_utils::BoxedFuture;
#[derive(Clone, Default)]
pub struct ImageTextureLoader;

const FILE_EXTENSIONS: &[&str] = &["png", "dds", "tga", "jpg", "jpeg"];

impl AssetLoader for ImageTextureLoader {
fn load<'a>(
&'a self,
Expand All @@ -23,16 +25,15 @@ impl AssetLoader for ImageTextureLoader {

let ext = load_context.path().extension().unwrap().to_str().unwrap();

// NOTE: If more formats are added they can be added here.
let img_format = if ext.eq_ignore_ascii_case("png") {
image::ImageFormat::Png
} else {
panic!(
let img_format = image::ImageFormat::from_extension(ext)
.ok_or_else(|| {
format!(
"Unexpected image format {:?} for file {}, this is an error in `bevy_render`.",
ext,
load_context.path().display()
)
};
})
.unwrap();

// Load the image in the expected format.
// Some formats like PNG allow for R or RG textures too, so the texture
Expand Down Expand Up @@ -159,6 +160,6 @@ impl AssetLoader for ImageTextureLoader {
}

fn extensions(&self) -> &[&str] {
&["png"]
FILE_EXTENSIONS
}
}

0 comments on commit 9239621

Please sign in to comment.