From ccbbfb10a9300f315b4a462ab1b8805bcf4748a3 Mon Sep 17 00:00:00 2001 From: roman Date: Sat, 21 Oct 2023 20:07:29 +0300 Subject: [PATCH 1/4] Add convenient methods for Image --- crates/bevy_render/src/texture/image.rs | 25 +++++++++++++++++++++---- crates/bevy_sprite/src/lib.rs | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index d14388d87504c..85d6308a527e5 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -14,7 +14,7 @@ use crate::{ use bevy_asset::Asset; use bevy_derive::{Deref, DerefMut}; use bevy_ecs::system::{lifetimeless::SRes, Resource, SystemParamItem}; -use bevy_math::Vec2; +use bevy_math::{UVec2, Vec2}; use bevy_reflect::Reflect; use serde::{Deserialize, Serialize}; use std::hash::Hash; @@ -256,17 +256,34 @@ impl Image { /// Returns the aspect ratio (height/width) of a 2D image. pub fn aspect_2d(&self) -> f32 { - self.texture_descriptor.size.height as f32 / self.texture_descriptor.size.width as f32 + self.height() as f32 / self.width() as f32 } - /// Returns the size of a 2D image. - pub fn size(&self) -> Vec2 { + /// Returns the size of a 2D image as f32. + pub fn size_f32(&self) -> Vec2 { Vec2::new( self.texture_descriptor.size.width as f32, self.texture_descriptor.size.height as f32, ) } + /// Returns the size of a 2D image. + pub fn size(&self) -> UVec2 { + UVec2::new( + self.texture_descriptor.size.width, + self.texture_descriptor.size.height, + ) + } + + /// Returns the size of a 2D image. + pub fn width(&self) -> u32 { + self.texture_descriptor.size.width + } + + pub fn height(&self) -> u32 { + self.texture_descriptor.size.height + } + /// 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) { diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index 1eb3b1a5cc64c..9f68a9a3b981e 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -138,7 +138,7 @@ pub fn calculate_bounds_2d( for (entity, sprite, texture_handle) in &sprites_without_aabb { if let Some(size) = sprite .custom_size - .or_else(|| images.get(texture_handle).map(|image| image.size())) + .or_else(|| images.get(texture_handle).map(|image| image.size_f32())) { let aabb = Aabb { center: (-sprite.anchor.as_vec() * size).extend(0.0).into(), From 26903352c2c820e9ea9bec17ae3267777df2f8ca Mon Sep 17 00:00:00 2001 From: roman Date: Sat, 21 Oct 2023 20:35:04 +0300 Subject: [PATCH 2/4] Fix tests --- crates/bevy_render/src/texture/compressed_image_saver.rs | 2 +- crates/bevy_render/src/texture/image.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_render/src/texture/compressed_image_saver.rs b/crates/bevy_render/src/texture/compressed_image_saver.rs index 26ab7d22785ce..a557447db3d46 100644 --- a/crates/bevy_render/src/texture/compressed_image_saver.rs +++ b/crates/bevy_render/src/texture/compressed_image_saver.rs @@ -40,7 +40,7 @@ impl AssetSaver for CompressedImageSaver { let mut source_image = compressor_params.source_image_mut(0); let size = image.size(); - source_image.init(&image.data, size.x as u32, size.y as u32, 4); + source_image.init(&image.data, size.x, size.y, 4); let mut compressor = basis_universal::Compressor::new(4); // SAFETY: the CompressorParams are "valid" to the best of our knowledge. The basis-universal diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index 85d6308a527e5..693efb7d80473 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -650,12 +650,12 @@ mod test { ); assert_eq!( Vec2::new(size.width as f32, size.height as f32), - image.size() + image.size_f32() ); } #[test] fn image_default_size() { let image = Image::default(); - assert_eq!(Vec2::ONE, image.size()); + assert_eq!(Vec2::ONE, image.size_f32()); } } From 5a33e9bba8220f57e6efc3304462ec60db892b8f Mon Sep 17 00:00:00 2001 From: roman Date: Sat, 21 Oct 2023 20:38:34 +0300 Subject: [PATCH 3/4] Apply suggestions --- crates/bevy_render/src/texture/image.rs | 31 +++++++++++-------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index 693efb7d80473..0edc6c318951d 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -255,35 +255,30 @@ impl Image { } /// Returns the aspect ratio (height/width) of a 2D image. - pub fn aspect_2d(&self) -> f32 { + pub fn aspect_ratio(&self) -> f32 { self.height() as f32 / self.width() as f32 } - /// Returns the size of a 2D image as f32. - pub fn size_f32(&self) -> Vec2 { - Vec2::new( - self.texture_descriptor.size.width as f32, - self.texture_descriptor.size.height as f32, - ) - } - - /// Returns the size of a 2D image. - pub fn size(&self) -> UVec2 { - UVec2::new( - self.texture_descriptor.size.width, - self.texture_descriptor.size.height, - ) - } - - /// Returns the size of a 2D image. + /// Returns the width of a 2D image. pub fn width(&self) -> u32 { self.texture_descriptor.size.width } + /// Returns the height of a 2D image. pub fn height(&self) -> u32 { self.texture_descriptor.size.height } + /// Returns the size of a 2D image as f32. + pub fn size_f32(&self) -> Vec2 { + Vec2::new(self.width() as f32, self.height() as f32) + } + + /// Returns the size of a 2D image. + pub fn size(&self) -> UVec2 { + UVec2::new(self.width(), self.height()) + } + /// 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) { From 4c2cfad05fe39e182f9ac3da4f0c7cf32279e30f Mon Sep 17 00:00:00 2001 From: roman Date: Sat, 21 Oct 2023 21:15:06 +0300 Subject: [PATCH 4/4] Fix the example --- examples/3d/tonemapping.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/3d/tonemapping.rs b/examples/3d/tonemapping.rs index 595f3b0b4ecac..a7eb2973392ec 100644 --- a/examples/3d/tonemapping.rs +++ b/examples/3d/tonemapping.rs @@ -336,7 +336,7 @@ fn update_image_viewer( if let Some(base_color_texture) = mat.base_color_texture.clone() { if image_changed_id == base_color_texture.id() { if let Some(image_changed) = images.get(image_changed_id) { - let size = image_changed.size().normalize_or_zero() * 1.4; + let size = image_changed.size_f32().normalize_or_zero() * 1.4; // Resize Mesh let quad = Mesh::from(shape::Quad::new(size)); meshes.insert(mesh_h, quad);