-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Sprite slice scaling and tiling #5213
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ use std::cmp::Ordering; | |
|
||
use crate::{ | ||
texture_atlas::{TextureAtlas, TextureAtlasSprite}, | ||
Sprite, 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}; | ||
|
@@ -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}; | ||
|
@@ -316,6 +316,7 @@ pub fn extract_sprite_events( | |
pub fn extract_sprites( | ||
mut extracted_sprites: ResMut<ExtractedSprites>, | ||
texture_atlases: Extract<Res<Assets<TextureAtlas>>>, | ||
images: Extract<Res<Assets<Image>>>, | ||
sprite_query: Extract< | ||
Query<( | ||
Entity, | ||
|
@@ -340,19 +341,71 @@ pub fn extract_sprites( | |
if !visibility.is_visible() { | ||
continue; | ||
} | ||
// 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.push(ExtractedSprite { | ||
entity, | ||
color: sprite.color, | ||
transform: *transform, | ||
rect: sprite.rect, | ||
// Pass the custom size | ||
custom_size: sprite.custom_size, | ||
flip_x: sprite.flip_x, | ||
flip_y: sprite.flip_y, | ||
image_handle_id: handle.id(), | ||
anchor: sprite.anchor.as_vec(), | ||
}); | ||
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.push(ExtractedSprite { | ||
entity, | ||
color: sprite.color, | ||
transform: *transform, | ||
rect: sprite.rect, | ||
// Pass the custom size | ||
custom_size: sprite.custom_size, | ||
flip_x: sprite.flip_x, | ||
flip_y: sprite.flip_y, | ||
image_handle_id: handle.id(), | ||
anchor: sprite.anchor.as_vec(), | ||
}); | ||
continue; | ||
} | ||
let image_size = match images.get(handle) { | ||
None => continue, | ||
Some(i) => Vec2::new( | ||
i.texture_descriptor.size.width as f32, | ||
i.texture_descriptor.size.height as f32, | ||
), | ||
}; | ||
|
||
let slices = match &sprite.draw_mode { | ||
SpriteDrawMode::Sliced(slicer) => slicer.compute_slices( | ||
sprite.rect.unwrap_or(Rect { | ||
min: Vec2::ZERO, | ||
max: image_size, | ||
}), | ||
sprite.custom_size, | ||
), | ||
SpriteDrawMode::Tiled { | ||
tile_x, | ||
tile_y, | ||
stretch_value, | ||
} => { | ||
let slice = TextureSlice { | ||
texture_rect: sprite.rect.unwrap_or(Rect { | ||
min: Vec2::ZERO, | ||
max: image_size, | ||
}), | ||
draw_size: sprite.custom_size.unwrap_or(image_size), | ||
offset: Vec2::ZERO, | ||
}; | ||
slice.tiled(*stretch_value, (*tile_x, *tile_y)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also looks like something that should be done in |
||
} | ||
SpriteDrawMode::Simple => unreachable!(), | ||
}; | ||
for slice in slices { | ||
let mut transform: GlobalTransform = *transform; | ||
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(), | ||
anchor: sprite.anchor.as_vec(), | ||
}); | ||
} | ||
} | ||
for (entity, visibility, atlas_sprite, transform, texture_atlas_handle) in atlas_query.iter() { | ||
if !visibility.is_visible() { | ||
|
@@ -545,7 +598,7 @@ pub fn queue_sprites( | |
}; | ||
let mut current_batch_entity = Entity::from_raw(u32::MAX); | ||
let mut current_image_size = Vec2::ZERO; | ||
// Add a phase item for each sprite, and detect when succesive items can be batched. | ||
// Add a phase item for each sprite, and detect when successive items can be batched. | ||
// Spawn an entity with a `SpriteBatch` component for each possible batch. | ||
// Compatible items share the same entity. | ||
// Batches are merged later (in `batch_phase_system()`), so that they can be interrupted | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'compute' suggests something that should be done in
prepare
as it is work that takes time to do and extract is meant to be as fast as possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note though that if it doesn't take any significant amount of time for a lot of such entities (i.e. doesn't add more than say 0.1ms compared to as many entities that just use simple sprites) but it would take more time in a separate
prepare
stage system due to having to iterate over them all again, then it can be OK to do it here. So basically profile and present results.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having now seen the slicer code, I suggest moving that to the
prepare
stage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll move every computation in
prepare
once #5247 is merged