diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index 1bb5e8935d599..e6c1343a4aa91 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -7,7 +7,7 @@ use crate::{ }; use bevy_asset::HandleUntyped; use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem}; -use bevy_math::Size; +use bevy_math::{Size, Vec2}; use bevy_reflect::TypeUuid; use thiserror::Error; use wgpu::{ @@ -118,6 +118,14 @@ impl Image { self.texture_descriptor.size.height as f32 / self.texture_descriptor.size.width as f32 } + /// Returns the size of a 2D image. + pub fn size(&self) -> Vec2 { + Vec2::new( + self.texture_descriptor.size.width as f32, + self.texture_descriptor.size.height as f32, + ) + } + /// Resizes the image to the new size, by removing information or appending 0 to the `data`. /// Does not properly resize the contents of the image, but only its internal `data` buffer. pub fn resize(&mut self, size: Extent3d) { @@ -440,3 +448,33 @@ impl RenderAsset for Image { }) } } + +#[cfg(test)] +mod test { + + use super::*; + + #[test] + fn image_size() { + let size = Extent3d { + width: 200, + height: 100, + depth_or_array_layers: 1, + }; + let image = Image::new_fill( + size, + TextureDimension::D2, + &[0, 0, 0, 255], + TextureFormat::Rgba8Unorm, + ); + assert_eq!( + Vec2::new(size.width as f32, size.height as f32), + image.size() + ); + } + #[test] + fn image_default_size() { + let image = Image::default(); + assert_eq!(Vec2::new(1.0, 1.0), image.size()); + } +}