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 convenient methods for Image #10221

Merged
merged 4 commits into from
Oct 22, 2023
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
2 changes: 1 addition & 1 deletion crates/bevy_render/src/texture/compressed_image_saver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 22 additions & 10 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -255,16 +255,28 @@ 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
pub fn aspect_ratio(&self) -> f32 {
self.height() as f32 / self.width() as f32
}

/// 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) -> Vec2 {
Vec2::new(
self.texture_descriptor.size.width as f32,
self.texture_descriptor.size.height as f32,
)
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`.
Expand Down Expand Up @@ -633,12 +645,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());
}
}
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/tonemapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down