Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Motion blur on webcam

Richie McIlroye8d5487837f6
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index 85d2d8330..2811afa85 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -569,7 +569,8 @@ impl ProjectUniforms {
mirror_x: if project.camera.mirror { 1.0 } else { 0.0 },
velocity_uv: velocity,
motion_blur_amount,
- ..Default::default()
+ camera_motion_blur_amount: 0.0,
+ _padding: [0.0; 4],
},
zoom,
)
@@ -626,6 +627,19 @@ impl ProjectUniforms {
position[1] + size[1],
];
+ // Calculate camera motion blur based on zoom transition
+ let camera_motion_blur = {
+ let base_blur = project.motion_blur.unwrap_or(0.2) as f32;
+ let zoom_delta = (current_zoom - prev_zoom).abs() as f32;
+
+ // Calculate a smooth transition factor
+ let transition_speed = 30.0f32; // Frames per second
+ let transition_factor = (zoom_delta * transition_speed).min(1.0);
+
+ // Reduce multiplier from 3.0 to 2.0 for weaker blur
+ (base_blur * 2.0 * transition_factor).min(1.0)
+ };
+
CompositeVideoFrameUniforms {
output_size,
frame_size,
@@ -643,8 +657,9 @@ impl ProjectUniforms {
rounding_px: project.camera.rounding / 100.0 * 0.5 * size[0],
mirror_x: if project.camera.mirror { 1.0 } else { 0.0 },
velocity_uv: [0.0, 0.0],
- motion_blur_amount: 0.5,
- ..Default::default()
+ motion_blur_amount,
+ camera_motion_blur_amount: camera_motion_blur,
+ _padding: [0.0; 4],
}
});
@@ -1099,7 +1114,7 @@ fn draw_cursor(
screen_bounds: uniforms.display.target_bounds,
cursor_size: cursor_size_percentage,
last_click_time: last_click_time.min(0.5),
- velocity,
+ velocity: [0.0, 0.0],
motion_blur_amount,
_alignment: [0.0; 7],
};
@@ -1177,7 +1192,8 @@ struct CompositeVideoFrameUniforms {
pub rounding_px: f32,
pub mirror_x: f32,
pub motion_blur_amount: f32,
- _padding: [f32; 1],
+ pub camera_motion_blur_amount: f32,
+ _padding: [f32; 4],
}
impl CompositeVideoFrameUniforms {
diff --git a/crates/rendering/src/shaders/composite-video-frame.wgsl b/crates/rendering/src/shaders/composite-video-frame.wgsl
index 0ae87f1f1..2ce254515 100644
--- a/crates/rendering/src/shaders/composite-video-frame.wgsl
+++ b/crates/rendering/src/shaders/composite-video-frame.wgsl
@@ -8,7 +8,8 @@ struct Uniforms {
rounding_px: f32,
mirror_x: f32,
motion_blur_amount: f32,
- _padding: f32,
+ camera_motion_blur_amount: f32,
+ _padding: vec4<f32>,
};
@group(0) @binding(0) var<uniform> u: Uniforms;
@@ -35,14 +36,18 @@ fn main_image(frag_color: vec4<f32>, frag_coord: vec2<f32>) -> vec4<f32> {
var base_color = sample_texture(target_uv, crop_bounds_uv);
base_color = apply_rounded_corners(base_color, target_uv);
- if u.motion_blur_amount <= 0.0 {
+ let blur_amount = select(u.motion_blur_amount, u.camera_motion_blur_amount, u.camera_motion_blur_amount > 0.0);
+
+ if blur_amount < 0.01 {
return mix(textureSample(prev_tex, sampler0, uv), base_color, base_color.a);
}
let center = vec2<f32>(0.5, 0.5);
let dir = normalize(target_uv - center);
- let num_samples = 16;
+ let base_samples = 16.0;
+ let num_samples = i32(base_samples * smoothstep(0.0, 1.0, blur_amount));
+
var accum = base_color;
var weight_sum = 1.0;
@@ -50,10 +55,16 @@ fn main_image(frag_color: vec4<f32>, frag_coord: vec2<f32>) -> vec4<f32> {
let t = f32(i) / f32(num_samples);
let dist_from_center = length(target_uv - center);
- let random_offset = (rand(target_uv + vec2<f32>(t)) - 0.5) * 0.1;
- let scale = dist_from_center * u.motion_blur_amount * (0.08 + random_offset);
+ let random_offset = (rand(target_uv + vec2<f32>(t)) - 0.5) * 0.1 * smoothstep(0.0, 0.2, blur_amount);
+
+ let base_scale = select(
+ 0.08, // Regular content scale
+ 0.16, // Camera scale (reduced from 0.24 to 0.16)
+ u.camera_motion_blur_amount > 0.0
+ );
+ let scale = dist_from_center * blur_amount * (base_scale + random_offset) * smoothstep(0.0, 0.1, blur_amount);
- let angle_variation = (rand(target_uv + vec2<f32>(t * 2.0)) - 0.5) * 0.1;
+ let angle_variation = (rand(target_uv + vec2<f32>(t * 2.0)) - 0.5) * 0.1 * smoothstep(0.0, 0.2, blur_amount);
let rotated_dir = vec2<f32>(
dir.x * cos(angle_variation) - dir.y * sin(angle_variation),
dir.x * sin(angle_variation) + dir.y * cos(angle_variation)
@@ -115,6 +126,12 @@ fn rand(co: vec2<f32>) -> f32 {
return fract(sin(dot(co, vec2<f32>(12.9898, 78.233))) * 43758.5453);
}
+// Add smoothstep helper function if not already present
+fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
+ let t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
+ return t * t * (3.0 - 2.0 * t);
+}
+
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,