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

Add material_tilemap_layout #460

Merged
merged 8 commits into from
Sep 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
10 changes: 9 additions & 1 deletion assets/custom_shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
#import bevy_ecs_tilemap::vertex_output MeshVertexOutput
#import bevy_sprite::mesh2d_view_bindings globals

struct MyMaterial {
brightness: f32,
_padding: vec3<f32>
};

@group(3) @binding(0)
var<uniform> material: MyMaterial;

fn hsv2rgb(c: vec3<f32>) -> vec3<f32>
{
let K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
Expand All @@ -14,5 +22,5 @@ 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);
return vec4((color.rgb + hsv2rgb(hsv)) * material.brightness, color.a);
}
13 changes: 11 additions & 2 deletions examples/custom_shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ mod helpers;

#[derive(AsBindGroup, TypeUuid, TypePath, Debug, Clone, Default)]
#[uuid = "31575692-a956-4762-98e2-5d457f552d0a"]
pub struct MyMaterial {}
pub struct MyMaterial {
#[uniform(0)]
brightness: f32,
// webgl2 requires 16 byte alignment
#[uniform(0)]
_padding: Vec3,
}

impl MaterialTilemap for MyMaterial {
fn fragment_shader() -> bevy::render::render_resource::ShaderRef {
Expand All @@ -23,7 +29,10 @@ fn startup(
) {
commands.spawn(Camera2dBundle::default());

let my_material_handle = materials.add(MyMaterial {});
let my_material_handle = materials.add(MyMaterial {
brightness: 0.5,
..default()
});

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

Expand Down
1 change: 1 addition & 0 deletions src/render/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ where
self.tilemap_pipeline.view_layout.clone(),
self.tilemap_pipeline.mesh_layout.clone(),
self.tilemap_pipeline.material_layout.clone(),
self.material_tilemap_layout.clone(),
];

M::specialize(&mut descriptor, key);
Expand Down