-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Resize mode for sprite #430
Conversation
4349804
to
6d52e05
Compare
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 think a render compatible way (so as not to copy data to the GPU needlessly) would be to use SpriteResizeMode
(or just ResizeMode
, but one has to make sure it doesn't name-clash in the future) as a component and add it to the SpriteComponents
bundle.
6d52e05
to
cac929e
Compare
Remade Edit: CI failed, but I think it's not related to the PR.
|
cac929e
to
9f99141
Compare
This is definitely a useful feature! I actually would prefer it if this lived in the Sprite component as its logic thats central to how a sprite is rendered (and it pairs nicely with Sprite.size). But we also don't want to pass this in as data to the shader. We can resolve this with the following changes: sprite.rs#[derive(Default, RenderResources)]
pub struct Sprite {
pub size: Vec2,
#[render_resources(ignore)]
pub resize_mode: SpriteResizeMode,
}
/// Determines how `Sprite` resize should be handled
#[derive(Debug)]
pub enum SpriteResizeMode {
Manual,
Automatic,
}
impl Default for SpriteResizeMode {
fn default() -> Self {
SpriteResizeMode::Automatic
}
}
pub fn sprite_system(
materials: Res<Assets<ColorMaterial>>,
textures: Res<Assets<Texture>>,
mut query: Query<(&mut Sprite, &Handle<ColorMaterial>)>,
) {
for (mut sprite, handle) in &mut query.iter() {
match sprite.resize_mode {
SpriteResizeMode::Manual => continue,
SpriteResizeMode::Automatic => {
let material = materials.get(&handle).unwrap();
if let Some(texture_handle) = material.texture {
if let Some(texture) = textures.get(&texture_handle) {
sprite.size = texture.size;
}
}
}
}
}
} sprite.vertlayout(set = 2, binding = 1) uniform Sprite_size {
vec2 size;
};
void main() {
v_Uv = Vertex_Uv;
vec3 position = Vertex_Position * vec3(size, 1.0);
gl_Position = ViewProj * Model * vec4(position, 1.0);
} |
9f99141
to
eba129b
Compare
@cart thanks for the input. It also looks almost like the first implementation. :) Is there a reason to remove It seems like |
I didn't know about |
@naithar Yeah we need to remove TextureAtlasSprite doesn't ignore fields, so it is directly byte-convertible. @TheNeikos We cant just use |
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.
Alrighty i think this is good to go after we remove repr(C)!
crates/bevy_sprite/src/sprite.rs
Outdated
renderer::{RenderResource, RenderResources}, | ||
texture::Texture, | ||
}; | ||
use bevy_render::{renderer::RenderResources, texture::Texture}; | ||
|
||
#[repr(C)] |
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.
lets remove repr(C)
as its no longer required.
Adds a 'resize_mode' field for 'Sprite'. This allows different resize handling based on 'SpriteResizeMode' enum value. Co-authored-by: Marcel Müller <neikos@neikos.email>
eba129b
to
0877dcf
Compare
Adds a 'resize_mode' field for 'Sprite'. This allows different resize handling based on 'SpriteResizeMode' enum value.
Related issue and discussion: #411
Tested on
sprite
,breakout
andtexture_atlas
examples on macOS.Also tested in project from #411 on iOS and macOS with
Automatic
andManual
modes.ResizeMode
is exposed in case developer would need to changeresize_mode
value in systems.Most (if not all) of the credit goes to @TheNeikos