Skip to content

Commit b089152

Browse files
committed
Make PackedTileData clearer
1 parent 6c3e592 commit b089152

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

crates/bevy_sprite/src/tilemap_chunk/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ pub struct TilemapChunk {
5757
#[derive(Clone, Copy, Debug)]
5858
pub struct TileData {
5959
pub tileset_index: u16,
60-
pub visible: bool,
6160
pub color: Color,
61+
pub visible: bool,
6262
}
6363

6464
impl TileData {
@@ -74,8 +74,8 @@ impl Default for TileData {
7474
fn default() -> Self {
7575
Self {
7676
tileset_index: 0,
77-
visible: true,
7877
color: Color::WHITE,
78+
visible: true,
7979
}
8080
}
8181
}

crates/bevy_sprite/src/tilemap_chunk/tilemap_chunk_material.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,21 @@ impl Material2d for TilemapChunkMaterial {
4848
}
4949
}
5050

51+
/// Packed per-tile data for use in the `Rgba16Uint` tile data texture in `TilemapChunkMaterial`.
5152
#[repr(C)]
5253
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
5354
pub struct PackedTileData {
54-
tileset_index: u16,
55-
flags: u16, // flags (visibility, etc.)
56-
color_red_green: u16, // r in low 8 bits, g in high 8 bits
57-
color_blue_alpha: u16, // b in low 8 bits, a in high 8 bits
55+
tileset_index: u16, // red channel
56+
color: [u8; 4], // green and blue channels
57+
flags: u16, // alpha channel
5858
}
5959

6060
impl PackedTileData {
6161
fn empty() -> Self {
6262
Self {
6363
tileset_index: u16::MAX,
64+
color: [0, 0, 0, 0],
6465
flags: 0,
65-
color_red_green: 0,
66-
color_blue_alpha: 0,
6766
}
6867
}
6968
}
@@ -72,17 +71,14 @@ impl From<TileData> for PackedTileData {
7271
fn from(
7372
TileData {
7473
tileset_index,
75-
visible,
7674
color,
75+
visible,
7776
}: TileData,
7877
) -> Self {
79-
let [r, g, b, a] = color.to_srgba().to_u8_array();
80-
8178
Self {
8279
tileset_index,
80+
color: color.to_srgba().to_u8_array(),
8381
flags: visible as u16,
84-
color_red_green: (r as u16) | ((g as u16) << 8),
85-
color_blue_alpha: (b as u16) | ((a as u16) << 8),
8682
}
8783
}
8884
}

crates/bevy_sprite/src/tilemap_chunk/tilemap_chunk_material.wgsl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,25 @@
1010

1111
struct TileData {
1212
tileset_index: u32,
13-
visible: bool,
1413
color: vec4<f32>,
14+
visible: bool,
1515
}
1616

1717
fn getTileData(coord: vec2<u32>) -> TileData {
1818
let data = textureLoad(tile_data, coord, 0);
1919

2020
let tileset_index = data.r;
21-
let visible = data.g != 0u;
2221

23-
let color_r = f32(data.b & 0xFFu) / 255.0;
24-
let color_g = f32((data.b >> 8u) & 0xFFu) / 255.0;
25-
let color_b = f32(data.a & 0xFFu) / 255.0;
26-
let color_a = f32((data.a >> 8u) & 0xFFu) / 255.0;
22+
let color_r = f32(data.g & 0xFFu) / 255.0;
23+
let color_g = f32((data.g >> 8u) & 0xFFu) / 255.0;
24+
let color_b = f32(data.b & 0xFFu) / 255.0;
25+
let color_a = f32((data.b >> 8u) & 0xFFu) / 255.0;
2726

2827
let color = vec4<f32>(color_r, color_g, color_b, color_a);
2928

30-
return TileData(tileset_index, visible, color);
29+
let visible = data.a != 0u;
30+
31+
return TileData(tileset_index, color, visible);
3132
}
3233

3334
@fragment

0 commit comments

Comments
 (0)