Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom shaders #428

Merged
merged 17 commits into from
Jul 23, 2023
Merged
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
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ jobs:
command: check
args: --all-targets

# Clean between builds to avoid disk space issues.
- name: Cargo Clean
uses: "actions-rs/cargo@v1"
with:
command: clean

- name: Run cargo check feature atlas
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -83,4 +89,5 @@ jobs:
with:
toolchain: stable
- run: cargo build --examples
- run: cargo clean
- run: cargo build --examples --features atlas
118 changes: 118 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,121 @@ features = [
"bevy_text",
"bevy_sprite",
]


[[example]]
name="3d_iso"
path="examples/3d_iso.rs"
required-features=["render"]
[[example]]
name="accessing_tiles"
path="examples/accessing_tiles.rs"
required-features=["render"]
[[example]]
name="animation"
path="examples/animation.rs"
required-features=["render"]
[[example]]
name="basic"
path="examples/basic.rs"
required-features=["render"]
[[example]]
name="bench"
path="examples/bench.rs"
required-features=["render"]
[[example]]
name="chunking"
path="examples/chunking.rs"
required-features=["render"]
[[example]]
name="colors"
path="examples/colors.rs"
required-features=["render"]
[[example]]
name="custom_shader"
path="examples/custom_shader.rs"
required-features=["render"]
[[example]]
name="frustum_cull_test"
path="examples/frustum_cull_test.rs"
required-features=["render"]
[[example]]
name="game_of_life"
path="examples/game_of_life.rs"
required-features=["render"]
[[example]]
name="hex_neighbors_radius_chunks"
path="examples/hex_neighbors_radius_chunks.rs"
required-features=["render"]
[[example]]
name="hex_neighbors"
path="examples/hex_neighbors.rs"
required-features=["render"]
[[example]]
name="hexagon_column"
path="examples/hexagon_column.rs"
required-features=["render"]
[[example]]
name="hexagon_generation"
path="examples/hexagon_generation.rs"
required-features=["render"]
[[example]]
name="hexagon_row"
path="examples/hexagon_row.rs"
required-features=["render"]
[[example]]
name="iso_diamond"
path="examples/iso_diamond.rs"
required-features=["render"]
[[example]]
name="iso_staggered"
path="examples/iso_staggered.rs"
required-features=["render"]
[[example]]
name="layers"
path="examples/layers.rs"
required-features=["render"]
[[example]]
name="ldtk"
path="examples/ldtk.rs"
required-features=["render"]
[[example]]
name="mouse_to_tile"
path="examples/mouse_to_tile.rs"
required-features=["render"]
[[example]]
name="move_tile"
path="examples/move_tile.rs"
required-features=["render"]
[[example]]
name="random_map"
path="examples/random_map.rs"
required-features=["render"]
[[example]]
name="remove_tiles"
path="examples/remove_tiles.rs"
required-features=["render"]
[[example]]
name="spacing"
path="examples/spacing.rs"
required-features=["render"]
[[example]]
name="texture_container"
path="examples/texture_container.rs"
required-features=["render"]
[[example]]
name="texture_vec"
path="examples/texture_vec.rs"
required-features=["render"]
[[example]]
name="tiled_rotated"
path="examples/tiled_rotated.rs"
required-features=["render"]
[[example]]
name="tiled"
path="examples/tiled.rs"
required-features=["render"]
[[example]]
name="visibility"
path="examples/visibility.rs"
required-features=["render"]
18 changes: 18 additions & 0 deletions assets/custom_shader.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#import bevy_ecs_tilemap::common process_fragment
#import bevy_ecs_tilemap::vertex_output MeshVertexOutput
#import bevy_sprite::mesh2d_view_bindings globals

fn hsv2rgb(c: vec3<f32>) -> vec3<f32>
{
let K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
let p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, vec3(0.0), vec3(1.0)), c.y);
}

@fragment
fn fragment(in: MeshVertexOutput) -> @location(0) vec4<f32> {
let color = process_fragment(in);

let hsv = vec3(abs(sin(globals.time)), 1.0, 1.0);
return vec4(color.rgb + hsv2rgb(hsv), color.a);
}
4 changes: 2 additions & 2 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy::{
};
use bevy_ecs_tilemap::prelude::*;
use bevy_ecs_tilemap::tiles::{AnimatedTile, TileBundle, TilePos, TileStorage, TileTextureIndex};
use bevy_ecs_tilemap::{TilemapBundle, TilemapPlugin};
use bevy_ecs_tilemap::TilemapPlugin;
use rand::seq::IteratorRandom;
use rand::thread_rng;

Expand Down Expand Up @@ -140,7 +140,7 @@ fn main() {
.set(ImagePlugin::default_nearest()),
)
.add_plugins(LogDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin)
.add_plugins(TilemapPlugin)
.add_systems(Startup, startup)
.add_systems(Startup, create_background)
Expand Down
2 changes: 1 addition & 1 deletion examples/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main() {
..Default::default()
})
.add_plugins(LogDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin)
.add_plugins(TilemapPlugin)
.add_systems(Startup, startup)
.add_systems(Update, helpers::camera::movement)
Expand Down
108 changes: 108 additions & 0 deletions examples/custom_shader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use bevy::{
prelude::*,
reflect::{TypePath, TypeUuid},
render::render_resource::AsBindGroup,
};
use bevy_ecs_tilemap::prelude::*;
mod helpers;

#[derive(AsBindGroup, TypeUuid, TypePath, Debug, Clone, Default)]
#[uuid = "31575692-a956-4762-98e2-5d457f552d0a"]
pub struct MyMaterial {}

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

fn startup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<MyMaterial>>,
) {
commands.spawn(Camera2dBundle::default());

let my_material_handle = materials.add(MyMaterial {});

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,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
material: my_material_handle.clone(),
..Default::default()
});

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

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

commands
.entity(tilemap_entity)
.insert(MaterialTilemapBundle {
grid_size,
map_type,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size: TilemapTileSize { x: 16.0, y: 16.0 },
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 1.0)
* Transform::from_xyz(32.0, 32.0, 0.0),
material: my_material_handle,
..Default::default()
});
}

fn main() {
App::new()
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("Custom Shader"),
..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();
}
10 changes: 5 additions & 5 deletions examples/hex_neighbors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ fn hover_highlight_tile_label(
for highlighted_tile_entity in highlighted_tiles_q.iter() {
if let Ok(label) = tile_label_q.get(highlighted_tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for mut section in tile_text.sections.iter_mut() {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::BLACK;
}
commands.entity(highlighted_tile_entity).remove::<Hovered>();
Expand All @@ -314,7 +314,7 @@ fn hover_highlight_tile_label(
if let Some(tile_entity) = tile_storage.get(&tile_pos) {
if let Ok(label) = tile_label_q.get(tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for mut section in tile_text.sections.iter_mut() {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::RED;
}
commands.entity(tile_entity).insert(Hovered);
Expand Down Expand Up @@ -343,7 +343,7 @@ fn highlight_neighbor_label(
for highlighted_tile_entity in highlighted_tiles_q.iter() {
if let Ok(label) = tile_label_q.get(highlighted_tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for mut section in tile_text.sections.iter_mut() {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::BLACK;
}
commands
Expand All @@ -370,7 +370,7 @@ fn highlight_neighbor_label(
if let Some(tile_entity) = tile_storage.checked_get(neighbor_pos) {
if let Ok(label) = tile_label_q.get(tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for mut section in tile_text.sections.iter_mut() {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::BLUE;
}
commands.entity(tile_entity).insert(NeighborHighlight);
Expand Down Expand Up @@ -411,7 +411,7 @@ fn highlight_neighbor_label(
if let Some(tile_entity) = tile_storage.checked_get(&tile_pos) {
if let Ok(label) = tile_label_q.get(tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for mut section in tile_text.sections.iter_mut() {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::GREEN;
}
commands.entity(tile_entity).insert(NeighborHighlight);
Expand Down
8 changes: 4 additions & 4 deletions examples/hex_neighbors_radius_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ fn hover_highlight_tile_label(
for highlighted_tile_entity in highlighted_tiles_q.iter() {
if let Ok(label) = tile_label_q.get(highlighted_tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for mut section in tile_text.sections.iter_mut() {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::BLACK;
}
commands.entity(highlighted_tile_entity).remove::<Hovered>();
Expand All @@ -429,7 +429,7 @@ fn hover_highlight_tile_label(
if let Some(tile_entity) = tile_storage.get(&tile_pos) {
if let Ok(label) = tile_label_q.get(tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for mut section in tile_text.sections.iter_mut() {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::RED;
}
commands.entity(tile_entity).insert(Hovered);
Expand Down Expand Up @@ -474,7 +474,7 @@ fn highlight_neighbor_labels(
for highlighted_tile_entity in highlighted_tiles_q.iter() {
if let Ok(label) = tile_label_q.get(highlighted_tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for mut section in tile_text.sections.iter_mut() {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::BLACK;
}
commands
Expand Down Expand Up @@ -510,7 +510,7 @@ fn highlight_neighbor_labels(
if neighbors.contains(&tile_hex_pos) {
if let Ok(label) = tile_label_q.get(*tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for mut section in tile_text.sections.iter_mut() {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::BLUE;
}
commands.entity(*tile_entity).insert(NeighborHighlight);
Expand Down
4 changes: 2 additions & 2 deletions examples/mouse_to_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fn highlight_tile_labels(
for highlighted_tile_entity in highlighted_tiles_q.iter() {
if let Ok(label) = tile_label_q.get(highlighted_tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for mut section in tile_text.sections.iter_mut() {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::BLACK;
}
commands
Expand Down Expand Up @@ -359,7 +359,7 @@ fn highlight_tile_labels(
if let Some(tile_entity) = tile_storage.get(&tile_pos) {
if let Ok(label) = tile_label_q.get(tile_entity) {
if let Ok(mut tile_text) = text_q.get_mut(label.0) {
for mut section in tile_text.sections.iter_mut() {
for section in tile_text.sections.iter_mut() {
section.style.color = Color::RED;
}
commands.entity(tile_entity).insert(HighlightedLabel);
Expand Down
Loading