Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions crates/bevy_image/src/compressed_image_saver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Image, ImageFormat, ImageFormatSetting, ImageLoader, ImageLoaderSettings};

use bevy_asset::saver::{AssetSaver, SavedAsset};
use core::num::NonZero;
use futures_lite::AsyncWriteExt;
use thiserror::Error;

Expand Down Expand Up @@ -64,11 +65,14 @@ impl AssetSaver for CompressedImageSaver {
};

writer.write_all(&compressed_basis_data).await?;

let layers = NonZero::new(image.texture_descriptor.size.depth_or_array_layers);
Ok(ImageLoaderSettings {
format: ImageFormatSetting::Format(ImageFormat::Basis),
is_srgb,
sampler: image.sampler.clone(),
asset_usage: image.asset_usage,
layers,
})
}
}
17 changes: 15 additions & 2 deletions crates/bevy_image/src/image_loader.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::num::NonZero;

use crate::image::{Image, ImageFormat, ImageType, TextureError};
use bevy_asset::{io::Reader, AssetLoader, LoadContext, RenderAssetUsages};
use thiserror::Error;
Expand Down Expand Up @@ -111,6 +113,9 @@ pub struct ImageLoaderSettings {
/// Where the asset will be used - see the docs on
/// [`RenderAssetUsages`] for details.
pub asset_usage: RenderAssetUsages,
/// If the image should be loaded as a stacked 2d array
/// image with the given number of layers
pub layers: Option<NonZero<u32>>,
}

impl Default for ImageLoaderSettings {
Expand All @@ -120,6 +125,7 @@ impl Default for ImageLoaderSettings {
is_srgb: true,
sampler: ImageSampler::Default,
asset_usage: RenderAssetUsages::default(),
layers: Default::default(),
}
}
}
Expand Down Expand Up @@ -168,7 +174,8 @@ impl AssetLoader for ImageLoader {
)?)
}
};
Ok(Image::from_buffer(

let mut image = Image::from_buffer(
&bytes,
image_type,
self.supported_compressed_formats,
Expand All @@ -179,7 +186,13 @@ impl AssetLoader for ImageLoader {
.map_err(|err| FileTextureError {
error: err,
path: format!("{}", load_context.path().display()),
})?)
})?;

if let Some(layers) = settings.layers {
image.reinterpret_stacked_2d_as_array(layers.into());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized this actually panics. So I want to change this so it returns a Result, but I am not sure if I should keep the panicing version? Is there any benefit to it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please swap it to return a Result. Panicking APIs are only worth it when the operation is extremely common and the panics are extremely rare. Neither is the case here :)

}

Ok(image)
}

fn extensions(&self) -> &[&str] {
Expand Down
56 changes: 17 additions & 39 deletions examples/shader/array_texture.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! This example illustrates how to create a texture for use with a `texture_2d_array<f32>` shader
//! uniform variable.

use core::num::NonZero;

use bevy::{
prelude::*, reflect::TypePath, render::render_resource::AsBindGroup, shader::ShaderRef,
image::ImageLoaderSettings, prelude::*, reflect::TypePath,
render::render_resource::AsBindGroup, shader::ShaderRef,
};

/// This example uses a shader source file from the assets subdirectory
Expand All @@ -15,23 +18,15 @@ fn main() {
MaterialPlugin::<ArrayTextureMaterial>::default(),
))
.add_systems(Startup, setup)
.add_systems(Update, create_array_texture)
.run();
}

#[derive(Resource)]
struct LoadingTexture {
is_loaded: bool,
handle: Handle<Image>,
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Start loading the texture.
commands.insert_resource(LoadingTexture {
is_loaded: false,
handle: asset_server.load("textures/array_texture.png"),
});

fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ArrayTextureMaterial>>,
) {
// light
commands.spawn((
DirectionalLight::default(),
Expand All @@ -43,34 +38,17 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
Camera3d::default(),
Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::new(1.5, 0.0, 0.0), Vec3::Y),
));
}

fn create_array_texture(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut loading_texture: ResMut<LoadingTexture>,
mut images: ResMut<Assets<Image>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ArrayTextureMaterial>>,
) {
if loading_texture.is_loaded
|| !asset_server
.load_state(loading_texture.handle.id())
.is_loaded()
{
return;
}
loading_texture.is_loaded = true;
let image = images.get_mut(&loading_texture.handle).unwrap();

// Create a new array texture asset from the loaded texture.
let array_layers = 4;
image.reinterpret_stacked_2d_as_array(array_layers);

// Spawn some cubes using the array texture
let array_layers = 4;
let mesh_handle = meshes.add(Cuboid::default());
let material_handle = materials.add(ArrayTextureMaterial {
array_texture: loading_texture.handle.clone(),
array_texture: asset_server.load_with_settings(
"textures/array_texture.png",
move |settings: &mut ImageLoaderSettings| {
settings.layers = NonZero::new(array_layers);
},
),
});
for x in -5..=5 {
commands.spawn((
Expand Down
Loading