1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
//! 2D Sprite Rendering implementation details. use ron::de::from_bytes as from_ron_bytes; use serde::{Deserialize, Serialize}; use crate::{error, types::Texture}; use amethyst_assets::{Asset, Format, Handle}; use amethyst_core::ecs::prelude::{Component, DenseVecStorage}; use amethyst_error::Error; pub mod prefab; /// An asset handle to sprite sheet metadata. pub type SpriteSheetHandle = Handle<SpriteSheet>; /// Meta data for a sprite sheet texture. /// /// Contains a handle to the texture and the sprite coordinates on the texture. #[derive(Clone, Debug, PartialEq)] pub struct SpriteSheet { /// `Texture` handle of the spritesheet texture pub texture: Handle<Texture>, /// A list of sprites in this sprite sheet. pub sprites: Vec<Sprite>, } impl Asset for SpriteSheet { const NAME: &'static str = "renderer::SpriteSheet"; type Data = Self; type HandleStorage = DenseVecStorage<Handle<Self>>; } /// Dimensions and texture coordinates of each sprite in a sprite sheet. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Sprite { /// Pixel width of the sprite pub width: f32, /// Pixel height of the sprite pub height: f32, /// Number of pixels to shift the sprite to the left and down relative to the entity pub offsets: [f32; 2], /// Texture coordinates of the sprite pub tex_coords: TextureCoordinates, } /// Texture coordinates of the sprite /// /// The coordinates should be normalized to a value between 0.0 and 1.0: /// /// * X axis: 0.0 is the left side and 1.0 is the right side. /// * Y axis: 0.0 is the bottom and 1.0 is the top. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct TextureCoordinates { /// Normalized left x coordinate pub left: f32, /// Normalized right x coordinate pub right: f32, /// Normalized bottom y coordinate pub bottom: f32, /// Normalized top y coordinate pub top: f32, } impl Sprite { /// Creates a `Sprite` from pixel values. /// /// This function expects pixel coordinates -- starting from the top left of the image. X /// increases to the right, Y increases downwards. Texture coordinates are calculated from the /// pixel values. /// /// # Parameters /// /// * `image_w`: Width of the full sprite sheet. /// * `image_h`: Height of the full sprite sheet. /// * `sprite_w`: Width of the sprite. /// * `sprite_h`: Height of the sprite. /// * `pixel_left`: Pixel X coordinate of the left side of the sprite. /// * `pixel_top`: Pixel Y coordinate of the top of the sprite. /// * `offsets`: Number of pixels to shift the sprite to the left and down relative to the /// entity. pub fn from_pixel_values( image_w: u32, image_h: u32, sprite_w: u32, sprite_h: u32, pixel_left: u32, pixel_top: u32, offsets: [f32; 2], flip_horizontal: bool, flip_vertical: bool, ) -> Sprite { let image_w = image_w as f32; let image_h = image_h as f32; let offsets = [offsets[0] as f32, offsets[1] as f32]; let pixel_right = (pixel_left + sprite_w) as f32; let pixel_bottom = (pixel_top + sprite_h) as f32; let pixel_left = pixel_left as f32; let pixel_top = pixel_top as f32; // Texture coordinates are expressed as fractions of the position on the image. // // For pixel perfect result, the sprite border must be rendered exactly at // screen pixel border or use nearest-neighbor sampling. // <http://www.mindcontrol.org/~hplus/graphics/opengl-pixel-perfect.html> // NOTE: Maybe we should provide an option to round coordinates from `Transform` // to nearest integer in `DrawFlat2D` pass before rendering. let left = (pixel_left) / image_w; let right = (pixel_right) / image_w; let top = (pixel_top) / image_h; let bottom = (pixel_bottom) / image_h; let (left, right) = if flip_horizontal { (right, left) } else { (left, right) }; let (top, bottom) = if flip_vertical { (bottom, top) } else { (top, bottom) }; let tex_coords = TextureCoordinates { left, right, top, bottom, }; Sprite { width: sprite_w as f32, height: sprite_h as f32, offsets, tex_coords, } } } impl From<((f32, f32), [f32; 4])> for Sprite { fn from((dimensions, tex_coords): ((f32, f32), [f32; 4])) -> Self { Self::from((dimensions, [0.0; 2], tex_coords)) } } impl From<((f32, f32), [f32; 2], [f32; 4])> for Sprite { fn from(((width, height), offsets, tex_coords): ((f32, f32), [f32; 2], [f32; 4])) -> Self { Sprite { width, height, offsets, tex_coords: TextureCoordinates::from(tex_coords), } } } impl From<((f32, f32), (f32, f32))> for TextureCoordinates { fn from(((left, right), (bottom, top)): ((f32, f32), (f32, f32))) -> Self { TextureCoordinates { left, right, bottom, top, } } } impl From<[f32; 4]> for TextureCoordinates { fn from(uv: [f32; 4]) -> Self { TextureCoordinates { left: uv[0], right: uv[1], bottom: uv[2], top: uv[3], } } } impl From<&TextureCoordinates> for [f32; 4] { fn from(item: &TextureCoordinates) -> Self { [item.left, item.right, item.bottom, item.top] } } /// Information for rendering a sprite. /// /// Instead of using a `Mesh` on a `DrawFlat` render pass, we can use a simpler set of shaders to /// render textures to quads. This struct carries the information necessary for the draw2dflat pass. #[derive(Clone, Debug, PartialEq)] pub struct SpriteRender { /// Handle to the sprite sheet of the sprite pub sprite_sheet: Handle<SpriteSheet>, /// Index of the sprite on the sprite sheet pub sprite_number: usize, } impl Component for SpriteRender { type Storage = DenseVecStorage<Self>; } /// Represents one sprite in `SpriteList`. /// Positions originate in the top-left corner (bitmap image convention). #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub struct SpritePosition { /// Horizontal position of the sprite in the sprite sheet pub x: u32, /// Vertical position of the sprite in the sprite sheet pub y: u32, /// Width of the sprite pub width: u32, /// Height of the sprite pub height: u32, /// Number of pixels to shift the sprite to the left and down relative to the entity holding it #[serde(default = "default_offsets")] pub offsets: Option<[f32; 2]>, /// Flip the sprite horizontally during rendering #[serde(default = "default_flip")] pub flip_horizontal: bool, /// Flip the sprite vertically during rendering #[serde(default = "default_flip")] pub flip_vertical: bool, } fn default_offsets() -> Option<[f32; 2]> { None } fn default_flip() -> bool { false } /// `SpriteList` controls how a sprite list is generated when using `Sprites::List` in a /// `SpriteSheetPrefab`. #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub struct SpriteList { /// Width of the texture in pixels. pub texture_width: u32, /// Height of the texture in pixels. pub texture_height: u32, /// Description of the sprites pub sprites: Vec<SpritePosition>, } /// `SpriteGrid` controls how a sprite grid is generated when using `Sprites::Grid` in a /// `SpriteSheetPrefab`. /// /// The number of columns in the grid must always be provided, and one of the other fields must also /// be provided. The grid will be layout row major, starting with the sprite in the upper left corner, /// and ending with the sprite in the lower right corner. For example a grid with 2 rows and 4 columns /// will have the order below for the sprites. /// /// ```text /// |---|---|---|---| /// | 0 | 1 | 2 | 3 | /// |---|---|---|---| /// | 4 | 5 | 6 | 7 | /// |---|---|---|---| /// ``` #[derive(Clone, Debug, Deserialize, Serialize, Default)] pub struct SpriteGrid { /// Width of the texture in pixels. pub texture_width: u32, /// Height of the texture in pixels. pub texture_height: u32, /// Specifies the number of columns in the spritesheet, this value must always be given. pub columns: u32, /// Specifies the number of rows in the spritesheet. If this is not given it will be calculated /// using either `sprite_count` (`sprite_count / columns`), or `cell_size` (`sheet_size / cell_size`). pub rows: Option<u32>, /// Specifies the number of sprites in the spritesheet. If this is not given it will be /// calculated using `rows` (`columns * rows`). pub sprite_count: Option<u32>, /// Specifies the size of the individual sprites in the spritesheet in pixels. If this is not /// given it will be calculated using the spritesheet size, `columns` and `rows`. /// Tuple order is `(width, height)`. pub cell_size: Option<(u32, u32)>, /// Specifies the position of the grid on a texture. If this is not given it will be set to (0, 0). /// Positions originate in the top-left corner (bitmap image convention). pub position: Option<(u32, u32)>, } /// Defined the sprites that are part of a `SpriteSheetPrefab`. #[derive(Clone, Debug, Deserialize, Serialize)] pub enum Sprites { /// A list of sprites List(SpriteList), /// Generate a grid sprite list, see `SpriteGrid` for more information. Grid(SpriteGrid), } impl Sprites { fn build_sprites(&self) -> Vec<Sprite> { match self { Sprites::List(list) => list.build_sprites(), Sprites::Grid(grid) => grid.build_sprites(), } } } impl SpriteList { /// Creates a `Vec<Sprite>` from `SpriteList`. pub fn build_sprites(&self) -> Vec<Sprite> { self.sprites .iter() .map(|pos| { Sprite::from_pixel_values( self.texture_width, self.texture_height, pos.width, pos.height, pos.x, pos.y, pos.offsets.unwrap_or([0.0; 2]), pos.flip_horizontal, pos.flip_vertical, ) }) .collect() } } impl SpriteGrid { /// The width of the part of the texture that the sprites reside on fn sheet_width(&self) -> u32 { self.texture_width - self.position().0 } /// The height of the part of the texture that the sprites reside on fn sheet_height(&self) -> u32 { self.texture_height - self.position().1 } fn rows(&self) -> u32 { self.rows.unwrap_or_else(|| { self.sprite_count .map(|c| { if (c % self.columns) == 0 { (c / self.columns) } else { (c / self.columns) + 1 } }) .or_else(|| self.cell_size.map(|(_, y)| (self.sheet_height() / y))) .unwrap_or(1) }) } fn sprite_count(&self) -> u32 { self.sprite_count .unwrap_or_else(|| self.columns * self.rows()) } fn cell_size(&self) -> (u32, u32) { self.cell_size.unwrap_or_else(|| { ( (self.sheet_width() / self.columns), (self.sheet_height() / self.rows()), ) }) } fn position(&self) -> (u32, u32) { self.position.unwrap_or((0, 0)) } /// Creates a `Vec<Sprite>` from `SpriteGrid`. pub fn build_sprites(&self) -> Vec<Sprite> { let rows = self.rows(); let sprite_count = self.sprite_count(); let cell_size = self.cell_size(); let position = self.position(); if (self.columns * cell_size.0) > self.sheet_width() { log::warn!( "Grid spritesheet contains more columns than can fit in the given width: {} * {} > {} - {}", self.columns, cell_size.0, self.texture_width, position.0 ); } if (rows * cell_size.1) > self.sheet_height() { log::warn!( "Grid spritesheet contains more rows than can fit in the given height: {} * {} > {} - {}", rows, cell_size.1, self.texture_height, position.1 ); } (0..sprite_count) .map(|cell| { let row = cell / self.columns; let column = cell - (row * self.columns); let x = column * cell_size.0 + position.0; let y = row * cell_size.1 + position.1; Sprite::from_pixel_values( self.texture_width, self.texture_height, cell_size.0, cell_size.1, x, y, [0.0; 2], false, false, ) }) .collect() } } /// Allows loading of sprite sheets in RON format. /// /// This format allows to conveniently load a sprite sheet from a RON file. /// /// Example: /// ```text,ignore /// #![enable(implicit_some)] /// ( /// // Width of the texture /// texture_width: 48, /// // Height of the texture /// texture_height: 16, /// // List of sprites the sheet holds /// sprites: [ /// ( /// // Horizontal position of the sprite in the sprite sheet /// x: 0, /// // Vertical position of the sprite in the sprite sheet /// y: 0, /// // Width of the sprite /// width: 16, /// // Height of the sprite /// height: 16, /// // Number of pixels to shift the sprite to the left and down relative to the entity holding it when rendering /// offsets: (0.0, 0.0), // This is optional and defaults to (0.0, 0.0) /// ), /// ( /// x: 16, /// y: 0, /// width: 32, /// height: 16, /// ), /// ], /// ) /// ``` /// /// Such a spritesheet description can be loaded using a `Loader` by passing it the handle of the corresponding loaded texture. /// ```rust,no_run /// # use amethyst_core::ecs::{World, WorldExt}; /// # use amethyst_assets::{Loader, AssetStorage}; /// # use amethyst_rendy::{sprite::{SpriteSheetFormat, SpriteSheet}, Texture, formats::texture::ImageFormat}; /// # /// # fn load_sprite_sheet() { /// # let world = World::new(); // Normally, you would use Amethyst's world /// # let loader = world.read_resource::<Loader>(); /// # let spritesheet_storage = world.read_resource::<AssetStorage<SpriteSheet>>(); /// # let texture_storage = world.read_resource::<AssetStorage<Texture>>(); /// let texture_handle = loader.load( /// "my_texture.png", /// ImageFormat(Default::default()), /// (), /// &texture_storage, /// ); /// let spritesheet_handle = loader.load( /// "my_spritesheet.ron", /// SpriteSheetFormat(texture_handle), /// (), /// &spritesheet_storage, /// ); /// # } /// ``` #[derive(Clone, Debug)] pub struct SpriteSheetFormat(pub Handle<Texture>); impl Format<SpriteSheet> for SpriteSheetFormat { fn name(&self) -> &'static str { "SPRITE_SHEET" } fn import_simple(&self, bytes: Vec<u8>) -> Result<SpriteSheet, Error> { let sprite_list: SpriteList = from_ron_bytes(&bytes).map_err(|_| error::Error::LoadSpritesheetError)?; Ok(SpriteSheet { texture: self.0.clone(), sprites: sprite_list.build_sprites(), }) } } #[cfg(test)] mod test { use super::{Sprite, TextureCoordinates}; #[test] fn texture_coordinates_from_tuple_maps_fields_correctly() { assert_eq!( TextureCoordinates { left: 0., right: 0.5, bottom: 0.75, top: 1.0, }, ((0.0, 0.5), (0.75, 1.0)).into() ); } #[test] fn texture_coordinates_from_slice_maps_fields_correctly() { assert_eq!( TextureCoordinates { left: 0., right: 0.5, bottom: 0.75, top: 1.0, }, [0.0, 0.5, 0.75, 1.0].into() ); } #[test] fn sprite_from_tuple_maps_fields_correctly() { assert_eq!( Sprite { width: 10., height: 40., offsets: [5., 20.], tex_coords: TextureCoordinates { left: 0., right: 0.5, bottom: 0.75, top: 1.0, }, }, ((10., 40.), [5., 20.], [0.0, 0.5, 0.75, 1.0]).into() ); } #[test] fn sprite_offsets_default_to_zero() { assert_eq!( Sprite { width: 10., height: 40., offsets: [0., 0.], tex_coords: TextureCoordinates { left: 0., right: 0.5, bottom: 0.75, top: 1.0, }, }, ((10., 40.), [0.0, 0.5, 0.75, 1.0]).into() ); } #[test] fn sprite_from_pixel_values_calculates_pixel_perfect_coordinates() { let image_w = 30; let image_h = 40; let sprite_w = 10; let sprite_h = 20; let pixel_left = 0; let pixel_top = 20; let offsets = [-5.0, -10.0]; assert_eq!( Sprite::from(( (10., 20.), // Sprite w and h [-5., -10.], // Offsets [0., 10. / 30., 1., 20. / 40.], // Texture coordinates )), Sprite::from_pixel_values( image_w, image_h, sprite_w, sprite_h, pixel_left, pixel_top, offsets, false, false ) ); } }