Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(rendering): support gradient noise in wgpu background layer

Richie McIlroydf236479495c
diff --git a/crates/rendering/src/layers/background.rs b/crates/rendering/src/layers/background.rs
index 45fd94e2f..43ab066a0 100644
--- a/crates/rendering/src/layers/background.rs
+++ b/crates/rendering/src/layers/background.rs
@@ -5,16 +5,15 @@ use serde::{Deserialize, Serialize};
use specta::Type;
use wgpu::{include_wgsl, util::DeviceExt};
-use crate::{
- ProjectUniforms, RenderVideoConstants, RenderingError, create_shader_render_pipeline,
- srgb_to_linear,
-};
+use crate::{ProjectUniforms, RenderVideoConstants, RenderingError, create_shader_render_pipeline};
#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize, Type)]
pub struct Gradient {
start: [f32; 4],
end: [f32; 4],
angle: f32,
+ noise_intensity: f32,
+ noise_scale: f32,
}
#[derive(PartialEq)]
@@ -34,25 +33,34 @@ impl From<BackgroundSource> for Background {
fn from(value: BackgroundSource) -> Self {
match value {
BackgroundSource::Color { value, alpha } => Background::Color([
- srgb_to_linear(value[0]),
- srgb_to_linear(value[1]),
- srgb_to_linear(value[2]),
+ value[0] as f32 / 255.0,
+ value[1] as f32 / 255.0,
+ value[2] as f32 / 255.0,
alpha as f32 / 255.0,
]),
- BackgroundSource::Gradient { from, to, angle } => Background::Gradient(Gradient {
+ BackgroundSource::Gradient {
+ from,
+ to,
+ angle,
+ noise_intensity,
+ noise_scale,
+ ..
+ } => Background::Gradient(Gradient {
start: [
- srgb_to_linear(from[0]),
- srgb_to_linear(from[1]),
- srgb_to_linear(from[2]),
+ from[0] as f32 / 255.0,
+ from[1] as f32 / 255.0,
+ from[2] as f32 / 255.0,
1.0,
],
end: [
- srgb_to_linear(to[0]),
- srgb_to_linear(to[1]),
- srgb_to_linear(to[2]),
+ to[0] as f32 / 255.0,
+ to[1] as f32 / 255.0,
+ to[2] as f32 / 255.0,
1.0,
],
angle: angle as f32,
+ noise_intensity: noise_intensity.unwrap_or(0.0),
+ noise_scale: noise_scale.unwrap_or(50.0),
}),
BackgroundSource::Image { path } | BackgroundSource::Wallpaper { path } => {
if let Some(path) = path
@@ -436,7 +444,9 @@ pub struct GradientOrColorUniforms {
pub start: [f32; 4],
pub end: [f32; 4],
pub angle: f32,
- _padding: [f32; 3],
+ pub noise_intensity: f32,
+ pub noise_scale: f32,
+ _padding: f32,
}
impl GradientOrColorUniforms {
@@ -468,9 +478,17 @@ impl From<Background> for GradientOrColorUniforms {
color[3],
],
angle: 0.0,
- _padding: [0.0; 3],
+ noise_intensity: 0.0,
+ noise_scale: 0.0,
+ _padding: 0.0,
},
- Background::Gradient(Gradient { start, end, angle }) => Self {
+ Background::Gradient(Gradient {
+ start,
+ end,
+ angle,
+ noise_intensity,
+ noise_scale,
+ }) => Self {
start: [
start[0] * start[3],
start[1] * start[3],
@@ -479,7 +497,9 @@ impl From<Background> for GradientOrColorUniforms {
],
end: [end[0] * end[3], end[1] * end[3], end[2] * end[3], end[3]],
angle,
- _padding: [0.0; 3],
+ noise_intensity,
+ noise_scale,
+ _padding: 0.0,
},
Background::Image { .. } => {
unreachable!("Image backgrounds should be handled separately")
@@ -539,40 +559,46 @@ mod tests {
#[test]
fn test_transparent_color_conversion() {
let source = BackgroundSource::Color {
- value: [255, 0, 0], // Red
- alpha: 128, // 50% opacity
+ value: [255, 0, 0],
+ alpha: 128,
};
let background = Background::from(source);
match background {
Background::Color(color) => {
- assert!((color[0] - 1.0).abs() < 1e-6); // Red in linear
+ assert!((color[0] - 1.0).abs() < 1e-6);
assert_eq!(color[1], 0.0);
assert_eq!(color[2], 0.0);
- assert!((color[3] - 0.5).abs() < 0.01); // Alpha 128/255 ≈ 0.5
+ assert!((color[3] - 0.5).abs() < 0.01);
}
_ => panic!("Expected Color variant"),
}
}
#[test]
- fn test_transparent_gradient_conversion() {
+ fn test_gradient_conversion() {
let source = BackgroundSource::Gradient {
- from: [0, 255, 0], // Green
- to: [0, 0, 255], // Blue
+ from: [0, 255, 0],
+ to: [0, 0, 255],
angle: 90,
+ noise_intensity: Some(50.0),
+ noise_scale: Some(30.0),
+ animated: None,
+ animation_speed: None,
};
let background = Background::from(source);
match background {
Background::Gradient(gradient) => {
assert_eq!(gradient.start[0], 0.0);
- assert_eq!(gradient.start[1], 1.0); // Green in linear
+ assert_eq!(gradient.start[1], 1.0);
assert_eq!(gradient.start[2], 0.0);
- assert_eq!(gradient.start[3], 1.0); // Alpha 255/255 = 1.0
+ assert_eq!(gradient.start[3], 1.0);
assert_eq!(gradient.end[0], 0.0);
assert_eq!(gradient.end[1], 0.0);
- assert_eq!(gradient.end[2], 1.0); // Blue in linear
- assert_eq!(gradient.end[3], 0.0); // Alpha 0/255 = 0.0
+ assert_eq!(gradient.end[2], 1.0);
+ assert_eq!(gradient.end[3], 1.0);
assert_eq!(gradient.angle, 90.0);
+ assert_eq!(gradient.noise_intensity, 50.0);
+ assert_eq!(gradient.noise_scale, 30.0);
}
_ => panic!("Expected Gradient variant"),
}