Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ required-features = ["render"]
[[example]]
name = "custom_shader"
path = "examples/custom_shader.rs"
[[example]]
name = "textured_tile_shader"
path = "examples/textured_tile_shader.rs"
required-features = ["render"]
[[example]]
name = "frustum_cull_test"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn update_damage(
- [`chunking`](examples/chunking.rs) - A simple example showing how to implement an infinite tilemap by spawning multiple chunks.
- [`colors`](examples/colors.rs) - Showcases how each tile can have an individual color.
- [`custom_shader`](examples/custom_shader.rs) - An example showing how to use a custom shader.
- [`textured_tile_shader`](examples/textured_tile_shader.rs) - An example showing how to use a custom shader to apply a texture across multiple tiles while preserving the edges.
- [`frustum_cull_test`](examples/frustum_cull_test.rs) - An environment for testing frustum culling.
- [`game_of_life`](examples/game_of_life.rs) - A game of life simulator.
- [`hex_neighbors`](examples/hex_neighbors.rs) - An example showing how to get neighbors on hexagonal maps.
Expand Down
13 changes: 13 additions & 0 deletions assets/textured_tile.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import bevy_ecs_tilemap::common::process_fragment
#import bevy_ecs_tilemap::vertex_output::MeshVertexOutput
#import bevy_sprite::mesh2d_view_bindings::globals

@group(3) @binding(2) var overlay_texture: texture_2d<f32>;
@group(3) @binding(3) var overlay_sampler: sampler;

@fragment
fn fragment(in: MeshVertexOutput) -> @location(0) vec4<f32> {
let color = process_fragment(in);
let overlay_color = textureSample(overlay_texture, overlay_sampler, fract(in.world_position.xy * 0.0069444444));
return mix(color, overlay_color, color.g);
}
Binary file added assets/tile_texture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions examples/textured_tile_shader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use bevy::{prelude::*, reflect::TypePath, render::render_resource::AsBindGroup};
use bevy_ecs_tilemap::prelude::*;
mod helpers;

#[derive(AsBindGroup, TypePath, Debug, Clone, Default, Asset)]
pub struct MyMaterial {
#[texture(2)]
#[sampler(3)]
pub texture_overlay: Handle<Image>,
}

impl MaterialTilemap for MyMaterial {
fn fragment_shader() -> bevy::render::render_resource::ShaderRef {
"textured_tile.wgsl".into()
}
}

fn startup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<MyMaterial>>,
) {
commands.spawn(Camera2d);
let texture_overlay: Handle<Image> = asset_server.load("tile_texture.png");

let my_material_handle = MaterialTilemapHandle::from(materials.add(MyMaterial {
texture_overlay
}));

let texture_handle: Handle<Image> = asset_server.load("tiles.png");

let map_size = TilemapSize { x: 32, y: 32 };

// Layer 1
let mut tile_storage = TileStorage::empty(map_size);
let tilemap_entity = commands.spawn_empty().id();

fill_tilemap(
TileTextureIndex(0),
map_size,
TilemapId(tilemap_entity),
&mut commands,
&mut tile_storage,
);

let tile_size = TilemapTileSize { x: 16.0, y: 16.0 };
let grid_size = tile_size.into();
let map_type = TilemapType::default();

commands
.entity(tilemap_entity)
.insert(MaterialTilemapBundle {
grid_size,
map_type,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle.clone()),
tile_size,
anchor: TilemapAnchor::Center,
material: my_material_handle.clone(),
..Default::default()
});
}

fn main() {
App::new()
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("Textured Tiles"),
..Default::default()
}),
..default()
})
.set(ImagePlugin::default_nearest()),
)
.add_plugins(TilemapPlugin)
.add_plugins(MaterialTilemapPlugin::<MyMaterial>::default())
.add_systems(Startup, startup)
.add_systems(Update, helpers::camera::movement)
.run();
}
1 change: 1 addition & 0 deletions src/render/shaders/tilemap_vertex.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,6 @@ fn vertex(vertex_input: VertexInput) -> MeshVertexOutput {
out.position = view.clip_from_world * mesh_data.world_position;
out.color = vertex_input.color;
out.storage_position = vec2<u32>(vertex_input.position.xy);
out.world_position = mesh_data.world_position;
return out;
}
1 change: 1 addition & 0 deletions src/render/shaders/tilemap_vertex_output.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ struct MeshVertexOutput {
@location(1) color: vec4<f32>,
@location(2) @interpolate(flat) tile_id: i32,
@location(3) storage_position: vec2<u32>,
@location(4) world_position: vec4<f32>,
}