add ability to load .dds, .tga, and .jpeg texture formats (#1038)

add ability to load `.dds`, `.tga`, and `.jpeg` texture formats
This commit is contained in:
Nathan Jeffords 2020-12-09 18:34:27 -08:00 committed by GitHub
parent 4a5bcccde2
commit 9239621ffc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 7 deletions

View file

@ -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"]

View file

@ -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"]

View file

@ -53,3 +53,6 @@ shaderc = "0.7.0"
[features]
png = ["image/png"]
hdr = ["image/hdr"]
dds = ["image/dds"]
tga = ["image/tga"]
jpeg = ["image/jpeg"]

View file

@ -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,
@ -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
@ -159,6 +160,6 @@ impl AssetLoader for ImageTextureLoader {
}
fn extensions(&self) -> &[&str] {
&["png"]
FILE_EXTENSIONS
}
}