Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}