Skip to content

Commit

Permalink
switch to UVec2 when working with texture dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
CptPotato committed Feb 4, 2024
1 parent 71be08a commit 80e61f8
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 47 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl FromWorld for MeshPipeline {
texture_view,
texture_format: image.texture_descriptor.format,
sampler,
size: image.size_f32(),
size: image.size(),
mip_level_count: image.texture_descriptor.mip_level_count,
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/texture/fallback_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn fallback_image_new(
texture_view,
texture_format: image.texture_descriptor.format,
sampler,
size: image.size_f32(),
size: image.size(),
mip_level_count: image.texture_descriptor.mip_level_count,
}
}
Expand Down
10 changes: 4 additions & 6 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ pub struct GpuImage {
pub texture_view: TextureView,
pub texture_format: TextureFormat,
pub sampler: Sampler,
pub size: Vec2,
pub size: UVec2,
pub mip_level_count: u32,
}

Expand Down Expand Up @@ -834,16 +834,13 @@ impl RenderAsset for Image {
&self.data,
);

let size = self.size();
let texture_view = texture.create_view(
self.texture_view_descriptor
.or_else(|| Some(TextureViewDescriptor::default()))
.as_ref()
.unwrap(),
);
let size = Vec2::new(
self.texture_descriptor.size.width as f32,
self.texture_descriptor.size.height as f32,
);
let sampler = match self.sampler {
ImageSampler::Default => (***default_sampler).clone(),
ImageSampler::Descriptor(descriptor) => {
Expand Down Expand Up @@ -922,7 +919,6 @@ impl CompressedImageFormats {

#[cfg(test)]
mod test {

use super::*;
use crate::render_asset::RenderAssetUsages;

Expand All @@ -945,9 +941,11 @@ mod test {
image.size_f32()
);
}

#[test]
fn image_default_size() {
let image = Image::default();
assert_eq!(UVec2::ONE, image.size());
assert_eq!(Vec2::ONE, image.size_f32());
}
}
6 changes: 3 additions & 3 deletions crates/bevy_sprite/src/dynamic_texture_atlas_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::TextureAtlasLayout;
use bevy_asset::{Assets, Handle};
use bevy_math::{IVec2, Rect, Vec2};
use bevy_math::{IVec2, Rect, UVec2};
use bevy_render::{
render_asset::{RenderAsset, RenderAssetUsages},
texture::{Image, TextureFormatPixelInfo},
Expand All @@ -23,7 +23,7 @@ impl DynamicTextureAtlasBuilder {
///
/// * `size` - total size for the atlas
/// * `padding` - gap added between textures in the atlas, both in x axis and y axis
pub fn new(size: Vec2, padding: i32) -> Self {
pub fn new(size: UVec2, padding: i32) -> Self {
Self {
atlas_allocator: AtlasAllocator::new(to_size2(size)),
padding,
Expand Down Expand Up @@ -102,6 +102,6 @@ fn to_rect(rectangle: guillotiere::Rectangle) -> Rect {
}
}

fn to_size2(vec2: Vec2) -> guillotiere::Size {
fn to_size2(vec2: UVec2) -> guillotiere::Size {
guillotiere::Size::new(vec2.x as i32, vec2.y as i32)
}
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/mesh2d/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl FromWorld for Mesh2dPipeline {
texture_view,
texture_format: image.texture_descriptor.format,
sampler,
size: image.size_f32(),
size: image.size(),
mip_level_count: image.texture_descriptor.mip_level_count,
}
};
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl FromWorld for SpritePipeline {
texture_view,
texture_format: image.texture_descriptor.format,
sampler,
size: image.size_f32(),
size: image.size(),
mip_level_count: image.texture_descriptor.mip_level_count,
}
};
Expand Down Expand Up @@ -617,7 +617,7 @@ pub fn prepare_sprites(
continue;
};

batch_image_size = Vec2::new(gpu_image.size.x, gpu_image.size.y);
batch_image_size = gpu_image.size.as_vec2();
batch_image_handle = extracted_sprite.image_handle_id;
image_bind_groups
.values
Expand Down
26 changes: 13 additions & 13 deletions crates/bevy_sprite/src/texture_atlas.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy_asset::{Asset, AssetId, Assets, Handle};
use bevy_ecs::component::Component;
use bevy_math::{Rect, Vec2};
use bevy_math::{Rect, UVec2};
use bevy_reflect::Reflect;
use bevy_render::texture::Image;
use bevy_utils::HashMap;
Expand All @@ -19,7 +19,7 @@ use bevy_utils::HashMap;
#[reflect(Debug)]
pub struct TextureAtlasLayout {
// TODO: add support to Uniforms derive to write dimensions and sprites to the same buffer
pub size: Vec2,
pub size: UVec2,
/// The specific areas of the atlas where each texture can be found
pub textures: Vec<Rect>,
/// Maps from a specific image handle to the index in `textures` where they can be found.
Expand Down Expand Up @@ -51,7 +51,7 @@ pub struct TextureAtlas {

impl TextureAtlasLayout {
/// Create a new empty layout with custom `dimensions`
pub fn new_empty(dimensions: Vec2) -> Self {
pub fn new_empty(dimensions: UVec2) -> Self {
Self {
size: dimensions,
texture_handles: None,
Expand All @@ -73,16 +73,16 @@ impl TextureAtlasLayout {
/// * `padding` - Optional padding between cells
/// * `offset` - Optional global grid offset
pub fn from_grid(
tile_size: Vec2,
columns: usize,
rows: usize,
padding: Option<Vec2>,
offset: Option<Vec2>,
tile_size: UVec2,
columns: u32,
rows: u32,
padding: Option<UVec2>,
offset: Option<UVec2>,
) -> Self {
let padding = padding.unwrap_or_default();
let offset = offset.unwrap_or_default();
let mut sprites = Vec::new();
let mut current_padding = Vec2::ZERO;
let mut current_padding = UVec2::ZERO;

for y in 0..rows {
if y > 0 {
Expand All @@ -93,18 +93,18 @@ impl TextureAtlasLayout {
current_padding.x = padding.x;
}

let cell = Vec2::new(x as f32, y as f32);
let cell = UVec2::new(x, y);

let rect_min = (tile_size + current_padding) * cell + offset;

sprites.push(Rect {
min: rect_min,
max: rect_min + tile_size,
min: rect_min.as_vec2(),
max: (rect_min + tile_size).as_vec2(),
});
}
}

let grid_size = Vec2::new(columns as f32, rows as f32);
let grid_size = UVec2::new(columns, rows);

Self {
size: ((tile_size + current_padding) * grid_size) - current_padding,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/texture_atlas_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl<'a> TextureAtlasBuilder<'a> {

Ok((
TextureAtlasLayout {
size: atlas_texture.size_f32(),
size: atlas_texture.size(),
textures: texture_rects,
texture_handles: Some(texture_ids),
},
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_text/src/font_atlas.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ab_glyph::{GlyphId, Point};
use bevy_asset::{Assets, Handle};
use bevy_math::Vec2;
use bevy_math::UVec2;
use bevy_render::{
render_asset::RenderAssetUsages,
render_resource::{Extent3d, TextureDimension, TextureFormat},
Expand Down Expand Up @@ -51,12 +51,12 @@ impl FontAtlas {
pub fn new(
textures: &mut Assets<Image>,
texture_atlases: &mut Assets<TextureAtlasLayout>,
size: Vec2,
size: UVec2,
) -> FontAtlas {
let texture = textures.add(Image::new_fill(
Extent3d {
width: size.x as u32,
height: size.y as u32,
width: size.x,
height: size.y,
depth_or_array_layers: 1,
},
TextureDimension::D2,
Expand Down
14 changes: 4 additions & 10 deletions crates/bevy_text/src/font_atlas_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ab_glyph::{GlyphId, OutlinedGlyph, Point};
use bevy_asset::{AssetEvent, AssetId};
use bevy_asset::{Assets, Handle};
use bevy_ecs::prelude::*;
use bevy_math::Vec2;
use bevy_math::UVec2;
use bevy_reflect::Reflect;
use bevy_render::texture::Image;
use bevy_sprite::TextureAtlasLayout;
Expand Down Expand Up @@ -84,13 +84,7 @@ impl FontAtlasSet {
let font_atlases = self
.font_atlases
.entry(FloatOrd(font_size))
.or_insert_with(|| {
vec![FontAtlas::new(
textures,
texture_atlases,
Vec2::splat(512.0),
)]
});
.or_insert_with(|| vec![FontAtlas::new(textures, texture_atlases, UVec2::splat(512))]);

let glyph_texture = Font::get_outlined_glyph_texture(outlined_glyph);
let add_char_to_font_atlas = |atlas: &mut FontAtlas| -> bool {
Expand All @@ -110,11 +104,11 @@ impl FontAtlasSet {
.height
.max(glyph_texture.width());
// Pick the higher of 512 or the smallest power of 2 greater than glyph_max_size
let containing = (1u32 << (32 - glyph_max_size.leading_zeros())).max(512) as f32;
let containing = (1u32 << (32 - glyph_max_size.leading_zeros())).max(512);
font_atlases.push(FontAtlas::new(
textures,
texture_atlases,
Vec2::new(containing, containing),
UVec2::splat(containing),
));
if !font_atlases.last_mut().unwrap().add_glyph(
textures,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ pub fn extract_uinodes(
continue;
};
let mut atlas_rect = layout.textures[atlas.index];
let mut atlas_size = layout.size;
let mut atlas_size = layout.size.as_vec2();
let scale = uinode.size() / atlas_rect.size();
atlas_rect.min *= scale;
atlas_rect.max *= scale;
Expand Down Expand Up @@ -607,7 +607,7 @@ pub fn extract_text_uinodes(
color,
rect,
image: atlas_info.texture.id(),
atlas_size: Some(atlas.size * inverse_scale_factor),
atlas_size: Some(atlas.size.as_vec2() * inverse_scale_factor),
clip: clip.map(|clip| clip.clip),
flip_x: false,
flip_y: false,
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn setup(
mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,
) {
let texture = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png");
let atlas = TextureAtlasLayout::from_grid(Vec2::new(24.0, 24.0), 7, 1, None, None);
let atlas = TextureAtlasLayout::from_grid(UVec2::splat(24), 7, 1, None, None);
let texture_atlas = texture_atlases.add(atlas);
// Use only the subset of sprites in the sheet that make up the run animation
let animation_indices = AnimationIndices { first: 1, last: 6 };
Expand Down
2 changes: 1 addition & 1 deletion examples/stress_tests/many_animated_sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn setup(
let half_y = (map_size.y / 2.0) as i32;

let texture_handle = assets.load("textures/rpg/chars/gabe/gabe-idle-run.png");
let texture_atlas = TextureAtlasLayout::from_grid(Vec2::new(24.0, 24.0), 7, 1, None, None);
let texture_atlas = TextureAtlasLayout::from_grid(UVec2::splat(24), 7, 1, None, None);
let texture_atlas_handle = texture_atlases.add(texture_atlas);

// Spawns the camera
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/ui_texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn setup(
};

let texture_handle = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png");
let texture_atlas = TextureAtlasLayout::from_grid(Vec2::new(24.0, 24.0), 7, 1, None, None);
let texture_atlas = TextureAtlasLayout::from_grid(UVec2::splat(24), 7, 1, None, None);
let texture_atlas_handle = texture_atlases.add(texture_atlas);

// root node
Expand Down

0 comments on commit 80e61f8

Please sign in to comment.