Added fade-in and fade-out shaders.

This commit is contained in:
Filipe Rodrigues 2022-11-28 22:22:12 +00:00
parent 7a94791d28
commit 3a47fb935b
7 changed files with 203 additions and 2 deletions

View File

@ -104,6 +104,12 @@ pub enum PanelsShader {
/// Fade-white
FadeWhite { strength: f32 },
/// Fade-out
FadeOut { strength: f32 },
/// Fade-in
FadeIn { strength: f32 },
}
impl PanelsShader {
@ -113,6 +119,8 @@ impl PanelsShader {
match self {
Self::Fade => "Fade",
Self::FadeWhite { .. } => "Fade White",
Self::FadeOut { .. } => "Fade Out",
Self::FadeIn { .. } => "Fade In",
}
}
}

View File

@ -86,6 +86,18 @@ impl PanelsRenderer {
&image_bind_group_layout,
include_str!("renderer/fade_white.wgsl"),
);
let fade_out_pipeline = self::create_render_pipeline(
&wgpu,
&uniforms_bind_group_layout,
&image_bind_group_layout,
include_str!("renderer/fade_out.wgsl"),
);
let fade_in_pipeline = self::create_render_pipeline(
&wgpu,
&uniforms_bind_group_layout,
&image_bind_group_layout,
include_str!("renderer/fade_in.wgsl"),
);
// Create the framebuffer
let msaa_framebuffer = self::create_msaa_framebuffer(&wgpu, wgpu.surface_size(surface_resource));
@ -94,6 +106,8 @@ impl PanelsRenderer {
pipelines: PanelsPipelines {
fade: fade_render_pipeline,
fade_white: fade_white_render_pipeline,
fade_out: fade_out_pipeline,
fade_in: fade_in_pipeline,
},
vertices,
indices,
@ -188,6 +202,8 @@ impl PanelsRenderer {
let pipeline = match resource.shader {
PanelsShader::Fade => &self.pipelines.fade,
PanelsShader::FadeWhite { .. } => &self.pipelines.fade_white,
PanelsShader::FadeOut { .. } => &self.pipelines.fade_out,
PanelsShader::FadeIn { .. } => &self.pipelines.fade_in,
};
render_pass.set_pipeline(pipeline);
render_pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
@ -202,9 +218,12 @@ impl PanelsRenderer {
for descriptor in panel.image_descriptors() {
// Update the uniforms
let uvs_matrix = descriptor.uvs_matrix(cursor_pos);
#[allow(clippy::match_same_arms)] // They might differ in the future
let extra = match resource.shader {
PanelsShader::Fade => [0.0; 3],
PanelsShader::FadeWhite { strength } => [strength, 0.0, 0.0],
PanelsShader::FadeOut { strength } => [strength, 0.0, 0.0],
PanelsShader::FadeIn { strength } => [strength, 0.0, 0.0],
};
let uniforms = PanelUniforms::new(pos_matrix, uvs_matrix, descriptor.alpha(), extra);
let image = descriptor.image();
@ -229,6 +248,12 @@ pub struct PanelsPipelines {
/// Fade-white Render pipeline
fade_white: wgpu::RenderPipeline,
/// Fade-out Render pipeline
fade_out: wgpu::RenderPipeline,
/// Fade-in Render pipeline
fade_in: wgpu::RenderPipeline,
}
/// Creates the vertices

View File

@ -0,0 +1,73 @@
// Vertex input
struct VertexInput {
@location(0)
pos: vec2<f32>,
@location(1)
uvs: vec2<f32>,
};
// Vertex output
struct VertexOutput {
@builtin(position)
pos: vec4<f32>,
@location(0)
uvs: vec2<f32>,
};
// Uniforms
struct Uniforms {
pos_matrix: mat4x4<f32>,
uvs_matrix: mat4x4<f32>,
alpha: f32,
strength: f32,
};
// Uniforms
@group(0) @binding(0)
var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.pos = uniforms.pos_matrix * vec4<f32>(in.pos, 0.0, 1.0);
out.uvs = in.uvs;
return out;
}
// Frag output
struct FragOutput {
@location(0)
color: vec4<f32>,
};
// Texture
@group(1) @binding(0)
var texture: texture_2d<f32>;
// Sampler
@group(1) @binding(1)
var texture_sampler: sampler;
@fragment
fn fs_main(in: VertexOutput) -> FragOutput {
var out: FragOutput;
// Sample the color and set the alpha
let uvs = uniforms.uvs_matrix * vec4<f32>(in.uvs, 0.0, 1.0);
let mid = vec2<f32>(uniforms.uvs_matrix[0][0] / 2.0 + uniforms.uvs_matrix[3].x, uniforms.uvs_matrix[1][1] / 2.0 + uniforms.uvs_matrix[3].y);
let new_uvs = (uvs.xy - mid) / pow(uniforms.alpha, uniforms.strength) + mid;
out.color = textureSample(texture, texture_sampler, new_uvs);
out.color.a = uniforms.alpha;
// TODO: Use a background color?
if (new_uvs.x < 0.0 || new_uvs.x >= 1.0 || new_uvs.y < 0.0 || new_uvs.y >= 1.0) {
out.color.a = 0.0;
}
return out;
}

View File

@ -0,0 +1,68 @@
// Vertex input
struct VertexInput {
@location(0)
pos: vec2<f32>,
@location(1)
uvs: vec2<f32>,
};
// Vertex output
struct VertexOutput {
@builtin(position)
pos: vec4<f32>,
@location(0)
uvs: vec2<f32>,
};
// Uniforms
struct Uniforms {
pos_matrix: mat4x4<f32>,
uvs_matrix: mat4x4<f32>,
alpha: f32,
strength: f32,
};
// Uniforms
@group(0) @binding(0)
var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.pos = uniforms.pos_matrix * vec4<f32>(in.pos, 0.0, 1.0);
out.uvs = in.uvs;
return out;
}
// Frag output
struct FragOutput {
@location(0)
color: vec4<f32>,
};
// Texture
@group(1) @binding(0)
var texture: texture_2d<f32>;
// Sampler
@group(1) @binding(1)
var texture_sampler: sampler;
@fragment
fn fs_main(in: VertexOutput) -> FragOutput {
var out: FragOutput;
// Sample the color and set the alpha
let uvs = uniforms.uvs_matrix * vec4<f32>(in.uvs, 0.0, 1.0);
let mid = vec2<f32>(uniforms.uvs_matrix[0][0] / 2.0 + uniforms.uvs_matrix[3].x, uniforms.uvs_matrix[1][1] / 2.0 + uniforms.uvs_matrix[3].y);
let new_uvs = (uvs.xy - mid) * pow(uniforms.alpha, uniforms.strength) + mid;
out.color = textureSample(texture, texture_sampler, new_uvs);
out.color.a = uniforms.alpha;
return out;
}

View File

@ -77,4 +77,10 @@ pub enum PanelsShader {
/// Fade-white
FadeWhite { strength: f32 },
/// Fade-out
FadeOut { strength: f32 },
/// Fade-in
FadeIn { strength: f32 },
}

View File

@ -332,11 +332,16 @@ fn draw_panels(
ui.vertical(|ui| {
ui.label("Shader");
let cur_shader = panels_editor.shader_mut(panels_resource);
egui::ComboBox::from_label("Select one!")
egui::ComboBox::from_id_source(std::ptr::addr_of!(cur_shader))
.selected_text(cur_shader.name())
.show_ui(ui, |ui| {
// TODO: Not have default values here?
let shaders = [PanelsShader::Fade, PanelsShader::FadeWhite { strength: 1.0 }];
let shaders = [
PanelsShader::Fade,
PanelsShader::FadeWhite { strength: 1.0 },
PanelsShader::FadeOut { strength: 0.2 },
PanelsShader::FadeIn { strength: 0.2 },
];
for shader in shaders {
ui.selectable_value(cur_shader, shader, shader.name());
}
@ -350,6 +355,18 @@ fn draw_panels(
egui::Slider::new(strength, 0.0..=20.0).ui(ui);
});
},
PanelsShader::FadeOut { strength } => {
ui.horizontal(|ui| {
ui.label("Strength");
egui::Slider::new(strength, 0.0..=2.0).ui(ui);
});
},
PanelsShader::FadeIn { strength } => {
ui.horizontal(|ui| {
ui.label("Strength");
egui::Slider::new(strength, 0.0..=2.0).ui(ui);
});
},
}
});

View File

@ -58,6 +58,8 @@ impl ProfileApplier {
match panels_shader {
profile::PanelsShader::Fade => PanelsShader::Fade,
profile::PanelsShader::FadeWhite { strength } => PanelsShader::FadeWhite { strength },
profile::PanelsShader::FadeOut { strength } => PanelsShader::FadeOut { strength },
profile::PanelsShader::FadeIn { strength } => PanelsShader::FadeIn { strength },
}
}
@ -66,6 +68,8 @@ impl ProfileApplier {
match panels_shader {
PanelsShader::Fade => profile::PanelsShader::Fade,
PanelsShader::FadeWhite { strength } => profile::PanelsShader::FadeWhite { strength },
PanelsShader::FadeOut { strength } => profile::PanelsShader::FadeOut { strength },
PanelsShader::FadeIn { strength } => profile::PanelsShader::FadeIn { strength },
}
}
}