mirror of
https://github.com/bevyengine/bevy
synced 2024-11-14 00:47:32 +00:00
a7be8a2655
# Objective
The physical width and height (pixels) of an image is always integers,
but for `GpuImage` bevy currently stores them as `Vec2` (`f32`).
Switching to `UVec2` makes this more consistent with the [underlying
texture data](https://docs.rs/wgpu/latest/wgpu/struct.Extent3d.html).
I'm not sure if this is worth the change in the surface level API. If
not, feel free to close this PR.
## Solution
- Replace uses of `Vec2` with `UVec2` when referring to texture
dimensions.
- Use integer types for the texture atlas dimensions and sections.
[`Sprite::rect`](a81a2d1da3/crates/bevy_sprite/src/sprite.rs (L29)
)
remains unchanged, so manually specifying a sub-pixel region of an image
is still possible.
---
## Changelog
- `GpuImage` now stores its size as `UVec2` instead of `Vec2`.
- Texture atlases store their size and sections as `UVec2` and `URect`
respectively.
- `UiImageSize` stores its size as `UVec2`.
## Migration Guide
- Change floating point types (`Vec2`, `Rect`) to their respective
unsigned integer versions (`UVec2`, `URect`) when using `GpuImage`,
`TextureAtlasLayout`, `TextureAtlasBuilder`,
`DynamicAtlasTextureBuilder` or `FontAtlas`.
63 lines
2 KiB
Rust
63 lines
2 KiB
Rust
//! Renders an animated sprite by loading all animation frames from a single image (a sprite sheet)
|
|
//! into a texture atlas, and changing the displayed image periodically.
|
|
|
|
use bevy::prelude::*;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) // prevents blurry sprites
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, animate_sprite)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct AnimationIndices {
|
|
first: usize,
|
|
last: usize,
|
|
}
|
|
|
|
#[derive(Component, Deref, DerefMut)]
|
|
struct AnimationTimer(Timer);
|
|
|
|
fn animate_sprite(
|
|
time: Res<Time>,
|
|
mut query: Query<(&AnimationIndices, &mut AnimationTimer, &mut TextureAtlas)>,
|
|
) {
|
|
for (indices, mut timer, mut atlas) in &mut query {
|
|
timer.tick(time.delta());
|
|
if timer.just_finished() {
|
|
atlas.index = if atlas.index == indices.last {
|
|
indices.first
|
|
} else {
|
|
atlas.index + 1
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
asset_server: Res<AssetServer>,
|
|
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
|
|
) {
|
|
let texture = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png");
|
|
let layout = TextureAtlasLayout::from_grid(UVec2::splat(24), 7, 1, None, None);
|
|
let texture_atlas_layout = texture_atlas_layouts.add(layout);
|
|
// Use only the subset of sprites in the sheet that make up the run animation
|
|
let animation_indices = AnimationIndices { first: 1, last: 6 };
|
|
commands.spawn(Camera2dBundle::default());
|
|
commands.spawn((
|
|
SpriteSheetBundle {
|
|
texture,
|
|
atlas: TextureAtlas {
|
|
layout: texture_atlas_layout,
|
|
index: animation_indices.first,
|
|
},
|
|
transform: Transform::from_scale(Vec3::splat(6.0)),
|
|
..default()
|
|
},
|
|
animation_indices,
|
|
AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
|
|
));
|
|
}
|