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

[Merged by Bors] - Add a size method on Image. #3696

Closed
Closed
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
40 changes: 39 additions & 1 deletion crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}
}