Skip to content

Commit

Permalink
use time in custom material example
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuwu committed May 14, 2023
1 parent fb79419 commit 7a65297
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
9 changes: 8 additions & 1 deletion assets/shaders/custom_fragment.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ var texture: texture_2d<f32>;
@group(1) @binding(1)
var texture_sampler: sampler;

@group(1) @binding(2)
var<uniform> time: f32;

@fragment
fn fragment(input: VertexOutput) -> @location(0) vec4<f32> {
let tex_color = vec4(1.0 - textureSample(texture, texture_sampler, input.uv).rgb, textureSample(texture, texture_sampler, input.uv).a);
let time_sin = 0.5 + sin(time * 10.0) * 0.5;
let tex_color = vec4(
textureSample(texture, texture_sampler, input.uv).rgb * time_sin + (1.0 - textureSample(texture, texture_sampler, input.uv).rgb) * (1.0 - time_sin),
textureSample(texture, texture_sampler, input.uv).a
);
return vec4(
((tex_color.a - 1.0) * input.dark_color.a + 1.0 - tex_color.rgb) * input.dark_color.rgb + tex_color.rgb * input.color.rgb,
tex_color.a * input.color.a,
Expand Down
10 changes: 4 additions & 6 deletions examples/custom_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,8 @@ pub struct MyMaterial {
#[texture(0)]
#[sampler(1)]
pub image: Handle<Image>,
}

impl MyMaterial {
pub fn new(image: Handle<Image>) -> Self {
Self { image }
}
#[uniform(2)]
pub time: f32,
}

impl Material2d for MyMaterial {
Expand Down Expand Up @@ -123,6 +119,7 @@ impl Material2d for MyMaterial {
#[derive(SystemParam)]
pub struct MyMaterialParam<'w, 's> {
my_spine_query: Query<'w, 's, &'static MySpine>,
time: Res<'w, Time>,
}

impl SpineMaterial for MyMaterial {
Expand All @@ -138,6 +135,7 @@ impl SpineMaterial for MyMaterial {
if params.my_spine_query.contains(entity) {
let mut material = material.unwrap_or_else(|| Self::default());
material.image = renderable_data.texture;
material.time = params.time.elapsed_seconds();
Some(material)
} else {
None
Expand Down

0 comments on commit 7a65297

Please sign in to comment.