Skip to content

Instantly share code, notes, and snippets.

@rparrett
Created July 10, 2023 14:31
Show Gist options
  • Save rparrett/b122349a0ed2a13dd3d303cd085c50db to your computer and use it in GitHub Desktop.
Save rparrett/b122349a0ed2a13dd3d303cd085c50db to your computer and use it in GitHub Desktop.
bevy_ecs_tilemap shaders 0.11
diff --git a/src/render/shaders/tilemap_fragment.wgsl b/src/render/shaders/tilemap_fragment.wgsl
index be535aa..e515117 100644
--- a/src/render/shaders/tilemap_fragment.wgsl
+++ b/src/render/shaders/tilemap_fragment.wgsl
@@ -1,15 +1,9 @@
+#import bevy_ecs_tilemap::vertex_output MeshVertexOutput
+#import bevy_ecs_tilemap::common tilemap_data
#import bevy_ecs_tilemap::common sprite_texture, sprite_sampler
-struct VertexOutput {
- @builtin(position) position: vec4<f32>,
- // #import bevy_ecs_tilemap::vertex_output
- @location(0) uv: vec4<f32>,
- @location(1) color: vec4<f32>,
- @location(2) @interpolate(flat) tile_id: i32,
-}
-
@fragment
-fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
+fn fragment(mesh: MeshVertexOutput) -> @location(0) vec4<f32> {
#ifdef ATLAS
let half_texture_pixel_size_u = 0.5 / tilemap_data.texture_size.x;
let half_texture_pixel_size_v = 0.5 / tilemap_data.texture_size.y;
@@ -19,24 +13,24 @@ fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
// Offset the UV 1/2 pixel from the sides of the tile, so that the sampler doesn't bleed onto
// adjacent tiles at the edges.
var uv_offset: vec2<f32> = vec2<f32>(0.0, 0.0);
- if (in.uv.z < half_tile_pixel_size_u) {
+ if (mesh.uv.z < half_tile_pixel_size_u) {
uv_offset.x = half_texture_pixel_size_u;
- } else if (in.uv.z > (1.0 - half_tile_pixel_size_u)) {
+ } else if (mesh.uv.z > (1.0 - half_tile_pixel_size_u)) {
uv_offset.x = - half_texture_pixel_size_u;
}
- if (in.uv.w < half_tile_pixel_size_v) {
+ if (mesh.uv.w < half_tile_pixel_size_v) {
uv_offset.y = half_texture_pixel_size_v;
- } else if (in.uv.w > (1.0 - half_tile_pixel_size_v)) {
+ } else if (mesh.uv.w > (1.0 - half_tile_pixel_size_v)) {
uv_offset.y = - half_texture_pixel_size_v;
}
- let color = textureSample(sprite_texture, sprite_sampler, in.uv.xy + uv_offset) * in.color;
+ let color = textureSample(sprite_texture, sprite_sampler, mesh.uv.xy + uv_offset) * mesh.color;
if (color.a < 0.001) {
discard;
}
return color;
#else
- let color = textureSample(sprite_texture, sprite_sampler, in.uv.xy, in.tile_id) * in.color;
+ let color = textureSample(sprite_texture, sprite_sampler, mesh.uv.xy, mesh.tile_id) * mesh.color;
if (color.a < 0.001) {
discard;
}
diff --git a/src/render/shaders/tilemap_vertex_output.wgsl b/src/render/shaders/tilemap_vertex_output.wgsl
index 4dde0d0..61d3f89 100644
--- a/src/render/shaders/tilemap_vertex_output.wgsl
+++ b/src/render/shaders/tilemap_vertex_output.wgsl
@@ -1,5 +1,8 @@
#define_import_path bevy_ecs_tilemap::vertex_output
-// @location(0) uv: vec4<f32>,
-// @location(1) color: vec4<f32>,
-// @location(2) @interpolate(flat) tile_id: i32,
+struct MeshVertexOutput {
+ @builtin(position) position: vec4<f32>,
+ @location(0) uv: vec4<f32>,
+ @location(1) color: vec4<f32>,
+ @location(2) @interpolate(flat) tile_id: i32,
+}
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment