Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1import package::mesh2d::{
2 functions as mesh_functions,
3 view_bindings::view,
4 vertex_output::VertexOutput,
5};
7@group(constants::MATERIAL_BIND_GROUP) @binding(0) var tileset: texture_2d_array<f32>;
8@group(constants::MATERIAL_BIND_GROUP) @binding(1) var tileset_sampler: sampler;
9@group(constants::MATERIAL_BIND_GROUP) @binding(2) var tile_data: texture_2d<u32>;
11struct TileData {
12 tileset_index: u32,
13 color: vec4<f32>,
14 visible: bool,
15 mirror_h: bool,
16 mirror_v: bool,
17 mirror_d: bool,
20fn get_tile_data(coord: vec2<u32>) -> TileData {
21 let data = textureLoad(tile_data, coord, 0);
23 let tileset_index = data.r;
25 let color_r = f32(data.g & 0xFFu) / 255.0;
26 let color_g = f32((data.g >> 8u) & 0xFFu) / 255.0;
27 let color_b = f32(data.b & 0xFFu) / 255.0;
28 let color_a = f32((data.b >> 8u) & 0xFFu) / 255.0;
30 let color = vec4<f32>(color_r, color_g, color_b, color_a);
32 let mirror_h = (data.a & 0x8u) != 0u;
33 let mirror_v = (data.a & 0x4u) != 0u;
34 let mirror_d = (data.a & 0x2u) != 0u;
35 let visible = (data.a & 0x1u) != 0u;
37 return TileData(tileset_index, color, visible, mirror_h, mirror_v, mirror_d);
40@fragment
41fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
42 let chunk_size = textureDimensions(tile_data, 0);
43 let tile_uv = in.uv * vec2<f32>(chunk_size);
44 var tile_coord = clamp(vec2<u32>(floor(tile_uv)), vec2<u32>(0), chunk_size - 1);
45 var local_uv = tile_uv - vec2<f32>(tile_coord);
47 tile_coord.y = chunk_size.y - 1 - tile_coord.y;
49 let tile = get_tile_data(tile_coord);
51 if (tile.tileset_index == 0xffffu || !tile.visible) {
52 discard;
53 }
55 if (tile.mirror_h) {
56 local_uv.x = 1 - local_uv.x;
57 }
59 if (tile.mirror_v) {
60 local_uv.y = 1 - local_uv.y;
61 }
63 if (tile.mirror_d) {
64 local_uv = local_uv.yx;
65 }
67 let tex_color = textureSample(tileset, tileset_sampler, local_uv, tile.tileset_index);
68 let final_color = tex_color * tile.color;
70 if (final_color.a < 0.001) {
71 discard;
72 }
74 return final_color;
Contents