Make PackedTileData clearer

This commit is contained in:
Conner Petzold 2025-07-08 23:16:41 -04:00
parent 6c3e592bed
commit b089152679
3 changed files with 17 additions and 20 deletions

View File

@ -57,8 +57,8 @@ pub struct TilemapChunk {
#[derive(Clone, Copy, Debug)]
pub struct TileData {
pub tileset_index: u16,
pub visible: bool,
pub color: Color,
pub visible: bool,
}
impl TileData {
@ -74,8 +74,8 @@ impl Default for TileData {
fn default() -> Self {
Self {
tileset_index: 0,
visible: true,
color: Color::WHITE,
visible: true,
}
}
}

View File

@ -48,22 +48,21 @@ impl Material2d for TilemapChunkMaterial {
}
}
/// Packed per-tile data for use in the `Rgba16Uint` tile data texture in `TilemapChunkMaterial`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct PackedTileData {
tileset_index: u16,
flags: u16, // flags (visibility, etc.)
color_red_green: u16, // r in low 8 bits, g in high 8 bits
color_blue_alpha: u16, // b in low 8 bits, a in high 8 bits
tileset_index: u16, // red channel
color: [u8; 4], // green and blue channels
flags: u16, // alpha channel
}
impl PackedTileData {
fn empty() -> Self {
Self {
tileset_index: u16::MAX,
color: [0, 0, 0, 0],
flags: 0,
color_red_green: 0,
color_blue_alpha: 0,
}
}
}
@ -72,17 +71,14 @@ impl From<TileData> for PackedTileData {
fn from(
TileData {
tileset_index,
visible,
color,
visible,
}: TileData,
) -> Self {
let [r, g, b, a] = color.to_srgba().to_u8_array();
Self {
tileset_index,
color: color.to_srgba().to_u8_array(),
flags: visible as u16,
color_red_green: (r as u16) | ((g as u16) << 8),
color_blue_alpha: (b as u16) | ((a as u16) << 8),
}
}
}

View File

@ -10,24 +10,25 @@
struct TileData {
tileset_index: u32,
visible: bool,
color: vec4<f32>,
visible: bool,
}
fn getTileData(coord: vec2<u32>) -> TileData {
let data = textureLoad(tile_data, coord, 0);
let tileset_index = data.r;
let visible = data.g != 0u;
let color_r = f32(data.b & 0xFFu) / 255.0;
let color_g = f32((data.b >> 8u) & 0xFFu) / 255.0;
let color_b = f32(data.a & 0xFFu) / 255.0;
let color_a = f32((data.a >> 8u) & 0xFFu) / 255.0;
let color_r = f32(data.g & 0xFFu) / 255.0;
let color_g = f32((data.g >> 8u) & 0xFFu) / 255.0;
let color_b = f32(data.b & 0xFFu) / 255.0;
let color_a = f32((data.b >> 8u) & 0xFFu) / 255.0;
let color = vec4<f32>(color_r, color_g, color_b, color_a);
return TileData(tileset_index, visible, color);
let visible = data.a != 0u;
return TileData(tileset_index, color, visible);
}
@fragment