Skip to content

Commit

Permalink
Rebased on main
Browse files Browse the repository at this point in the history
  • Loading branch information
ManevilleF committed Nov 14, 2022
1 parent 138c23f commit a893795
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 26 deletions.
3 changes: 1 addition & 2 deletions crates/bevy_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ pub mod prelude {
#[doc(hidden)]
pub use crate::{
bundle::{SpriteBundle, SpriteSheetBundle},
rect::{BorderRect, Rect},
sprite::{Sprite, SpriteDrawMode},
texture_atlas::{TextureAtlas, TextureAtlasSprite},
texture_slice::{SliceScaleMode, TextureSlicer},
texture_slice::{BorderRect, SliceScaleMode, TextureSlicer},
ColorMaterial, ColorMesh2dBundle, TextureAtlasBuilder,
};
}
Expand Down
16 changes: 9 additions & 7 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cmp::Ordering;

use crate::{
texture_atlas::{TextureAtlas, TextureAtlasSprite},
Rect, Sprite, SpriteDrawMode, TextureSlice, SPRITE_SHADER_HANDLE,
Sprite, SpriteDrawMode, TextureSlice, SPRITE_SHADER_HANDLE,
};
use bevy_asset::{AssetEvent, Assets, Handle, HandleId};
use bevy_core_pipeline::{core_2d::Transparent2d, tonemapping::Tonemapping};
Expand Down Expand Up @@ -30,7 +30,7 @@ use bevy_render::{
},
Extract,
};
use bevy_transform::components::GlobalTransform;
use bevy_transform::components::{GlobalTransform, Transform};
use bevy_utils::FloatOrd;
use bevy_utils::HashMap;
use bytemuck::{Pod, Zeroable};
Expand Down Expand Up @@ -343,15 +343,16 @@ pub fn extract_sprites(
}
if let SpriteDrawMode::Simple = sprite.draw_mode {
// PERF: we don't check in this function that the `Image` asset is ready, since it should be in most cases and hashing the handle is expensive
extracted_sprites.sprites.alloc().init(ExtractedSprite {
extracted_sprites.sprites.push(ExtractedSprite {
entity,
color: sprite.color,
transform: *transform,
// Use the full texture
rect: None,
custom_size: sprite.custom_size,
flip_x: sprite.flip_x,
flip_y: sprite.flip_y,
image_handle_id: handle.id,
image_handle_id: handle.id(),
anchor: sprite.anchor.as_vec(),
});
continue;
Expand Down Expand Up @@ -391,16 +392,17 @@ pub fn extract_sprites(
};
for slice in slices {
let mut transform: GlobalTransform = *transform;
transform.translation = transform.mul_vec3(slice.offset.extend(0.0));
extracted_sprites.sprites.alloc().init(ExtractedSprite {
transform =
transform.mul_transform(Transform::from_translation(slice.offset.extend(0.0)));
extracted_sprites.sprites.push(ExtractedSprite {
entity,
color: sprite.color,
transform,
rect: Some(slice.texture_rect),
custom_size: Some(slice.draw_size),
flip_x: sprite.flip_x,
flip_y: sprite.flip_y,
image_handle_id: handle.id,
image_handle_id: handle.id(),
anchor: sprite.anchor.as_vec(),
});
}
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_sprite/src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub enum Anchor {
}

impl Anchor {
#[inline]
#[must_use]
pub fn as_vec(&self) -> Vec2 {
match self {
Anchor::Center => Vec2::ZERO,
Expand Down
67 changes: 62 additions & 5 deletions crates/bevy_sprite/src/texture_slice.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,63 @@
use crate::{BorderRect, Rect};
use bevy_math::Vec2;
use bevy_reflect::Reflect;
use bevy_math::{Rect, Vec2};
use bevy_reflect::{FromReflect, Reflect};

/// Struct defining a [`Sprite`](crate::Sprite) border with padding values
#[derive(Default, Clone, Copy, Debug, Reflect, FromReflect)]
pub struct BorderRect {
/// Pixel padding to the left
pub left: f32,
/// Pixel padding to the right
pub right: f32,
/// Pixel padding to the top
pub top: f32,
/// Pixel padding to the bottom
pub bottom: f32,
}

impl BorderRect {
/// Creates a new border as a square, with identical pixel padding values on every direction
#[must_use]
#[inline]
pub const fn square(value: f32) -> Self {
Self {
left: value,
right: value,
top: value,
bottom: value,
}
}

/// Creates a new border as a rectangle, with:
/// - `horizontal` for left and right pixel padding
/// - `vertical` for top and bottom pixel padding
#[must_use]
#[inline]
pub const fn rectangle(horizontal: f32, vertical: f32) -> Self {
Self {
left: horizontal,
right: horizontal,
top: vertical,
bottom: vertical,
}
}
}

impl From<f32> for BorderRect {
fn from(v: f32) -> Self {
Self::square(v)
}
}

impl From<[f32; 4]> for BorderRect {
fn from([left, right, top, bottom]: [f32; 4]) -> Self {
Self {
left,
right,
top,
bottom,
}
}
}

/// Slices a texture using the **9-slicing** technique. This allows to reuse an image at various sizes
/// without needing to prepare multiple assets. The associated texture will be split into nine portions,
Expand All @@ -10,7 +67,7 @@ use bevy_reflect::Reflect;
/// sections will be scaled or tiled.
///
/// See [9-sliced](https://en.wikipedia.org/wiki/9-slice_scaling) textures.
#[derive(Debug, Clone, Reflect)]
#[derive(Debug, Clone, Reflect, FromReflect)]
pub struct TextureSlicer {
/// The sprite borders, defining the 9 sections of the image
pub border: BorderRect,
Expand All @@ -23,7 +80,7 @@ pub struct TextureSlicer {
}

/// Defines how a texture slice scales when resized
#[derive(Debug, Copy, Clone, Default, Reflect)]
#[derive(Debug, Copy, Clone, Default, Reflect, FromReflect)]
pub enum SliceScaleMode {
/// The slice will be stretched to fit the area
#[default]
Expand Down
26 changes: 14 additions & 12 deletions examples/2d/sprite_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use bevy::prelude::*;

fn main() {
App::new()
.insert_resource(WindowDescriptor {
width: 1350.0,
height: 700.0,
.add_plugins(DefaultPlugins.set(WindowPlugin {
window: WindowDescriptor {
width: 1350.0,
height: 700.0,
..default()
},
..default()
})
.add_plugins(DefaultPlugins)
}))
.add_startup_system(setup)
.run();
}
Expand All @@ -21,7 +23,7 @@ fn spawn_sprites(
slice_border: f32,
) {
// Reference sprite
commands.spawn_bundle(SpriteBundle {
commands.spawn(SpriteBundle {
transform: Transform::from_translation(base_pos),
texture: texture_handle.clone(),
sprite: Sprite {
Expand All @@ -32,7 +34,7 @@ fn spawn_sprites(
});

// Scaled regular sprite
commands.spawn_bundle(SpriteBundle {
commands.spawn(SpriteBundle {
transform: Transform::from_translation(base_pos + Vec3::X * 150.0),
texture: texture_handle.clone(),
sprite: Sprite {
Expand All @@ -43,7 +45,7 @@ fn spawn_sprites(
});

// Stretched Scaled sliced sprite
commands.spawn_bundle(SpriteBundle {
commands.spawn(SpriteBundle {
transform: Transform::from_translation(base_pos + Vec3::X * 300.0),
texture: texture_handle.clone(),
sprite: Sprite {
Expand All @@ -59,7 +61,7 @@ fn spawn_sprites(
});

// Scaled sliced sprite
commands.spawn_bundle(SpriteBundle {
commands.spawn(SpriteBundle {
transform: Transform::from_translation(base_pos + Vec3::X * 450.0),
texture: texture_handle.clone(),
sprite: Sprite {
Expand All @@ -76,7 +78,7 @@ fn spawn_sprites(
});

// Scaled sliced sprite horizontally
commands.spawn_bundle(SpriteBundle {
commands.spawn(SpriteBundle {
transform: Transform::from_translation(base_pos + Vec3::X * 700.0),
texture: texture_handle.clone(),
sprite: Sprite {
Expand All @@ -93,7 +95,7 @@ fn spawn_sprites(
});

// Scaled sliced sprite horizontally with max scale
commands.spawn_bundle(SpriteBundle {
commands.spawn(SpriteBundle {
transform: Transform::from_translation(base_pos + Vec3::X * 1050.0),
texture: texture_handle,
sprite: Sprite {
Expand All @@ -111,7 +113,7 @@ fn spawn_sprites(
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(Camera2dBundle::default());
commands.spawn(Camera2dBundle::default());
// Load textures
let handle_1 = asset_server.load("textures/slice_square.png");
let handle_2 = asset_server.load("textures/slice_square_2.png");
Expand Down

0 comments on commit a893795

Please sign in to comment.