Skip to content
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

SpriteComponents with texture doesn't respect size #411

Closed
naithar opened this issue Sep 1, 2020 · 8 comments
Closed

SpriteComponents with texture doesn't respect size #411

naithar opened this issue Sep 1, 2020 · 8 comments
Labels
A-Rendering Drawing game state to the screen

Comments

@naithar
Copy link
Contributor

naithar commented Sep 1, 2020

Platform:
macOS 10.15.4 / iOS 13.6

Description:
Using SpriteComponents with texture and specific size value doesn't change the way sprite is rendered.
Using only color for ColorMaterial renders sprite correctly.
Using scale works for both texture and color, but color requires size being specified.

Example:

Texture:

let texture_handle = asset_server.load("icon.png").unwrap();

commands
.spawn(Camera2dComponents::default())
.spawn(SpriteComponents {
  material: materials.add(ColorMaterial::modulated_texture(texture_handle.into(), Color::rgb(0.2, 0.2, 0.8))),
  sprite: Sprite { size: Vec2::new(50.0, 50.0) },
  ..Default::default()
});

Color:

commands
.spawn(Camera2dComponents::default())
.spawn(SpriteComponents {
  material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()),
  sprite: Sprite { size: Vec2::new(50.0, 50.0) },
  ..Default::default()
});

Снимок экрана 2020-09-01 в 14 24 42 Снимок экрана 2020-09-01 в 14 25 28

@karroffel karroffel added the A-Rendering Drawing game state to the screen label Sep 1, 2020
@TheNeikos
Copy link
Contributor

This is because of this system:

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() {
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;
}
}
}
}

@naithar
Copy link
Contributor Author

naithar commented Sep 1, 2020

@TheNeikos well, then I guess the question is - is this an expected behavior.
I would expect it to fire only if sprite size is not set or zero (but this also might be the value developer want for some reason), but I might be wrong :)

@TheNeikos
Copy link
Contributor

I would even go as far as that only the sprite size is important and that the dev should always fill it.

@naithar
Copy link
Contributor Author

naithar commented Sep 1, 2020

This is because of this system:

That's the result when I remove this system, so I guess something should be done with the way this system is handled:

@naithar
Copy link
Contributor Author

naithar commented Sep 1, 2020

Maybe there is a way to save current behavior as default one, but also fixing it when developer specifies an actual size? Like using some trait like SpriteSize?

@TheNeikos
Copy link
Contributor

TheNeikos commented Sep 3, 2020

I think the size could be made into an enum:

enum Size {
  Manual(f32, f32),
  Computed(f32, f32),
  Undefined,
}

This way the system seen here (#411 (comment)) will only assign a new size when it is Computed or Undefined making the intent clear. It would also default to Size::Undefined in case one doesn't want to manually size it.

An alternative would also be to mark the Sprite as being manually sized for example:

enum ResizeBehavior {
   Fixed,
   Automatic,
}

which could then be checked, and if its automatic then the size is overwritten if the Handle changes.

@naithar
Copy link
Contributor Author

naithar commented Sep 3, 2020

I think the size could be made into an enum:

enum Size {
  Manual(f32, f32),
  Computed(f32, f32),
  Undefined,
}

This way the system seen here (#411 (comment)) will only assign a new size when it is Computed or Undefined making the intent clear. It would also default to Size::Undefined in case one doesn't want to manually size it.

This doesn't seem to work well with shader bindings system. Just like with the traits :)

enum ResizeBehavior {
   Fixed,
   Automatic,
}

This worked pretty good.

@cart
Copy link
Member

cart commented Sep 8, 2020

Closed by #430

@cart cart closed this as completed Sep 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen
Projects
None yet
Development

No branches or pull requests

4 participants