Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

wip: Motion blur on zoom

Richie McIlroy3480b8f79b07
diff --git a/apps/desktop/src/routes/editor/ConfigSidebar.tsx b/apps/desktop/src/routes/editor/ConfigSidebar.tsx
index 9424e1be3..93be76bf1 100644
--- a/apps/desktop/src/routes/editor/ConfigSidebar.tsx
+++ b/apps/desktop/src/routes/editor/ConfigSidebar.tsx
@@ -508,8 +508,9 @@ export function ConfigSidebar() {
<Slider
value={[project.cursor.size]}
onChange={(v) => setProject("cursor", "size", v[0])}
- minValue={500}
+ minValue={20}
maxValue={300}
+ step={1}
/>
</Field>
<ComingSoonTooltip>
diff --git a/apps/desktop/src/utils/tauri.ts b/apps/desktop/src/utils/tauri.ts
index bc63f872a..3fc0a74cc 100644
--- a/apps/desktop/src/utils/tauri.ts
+++ b/apps/desktop/src/utils/tauri.ts
@@ -404,7 +404,7 @@ export type OSPermissionStatus = "notNeeded" | "empty" | "granted" | "denied"
export type OSPermissionsCheck = { screenRecording: OSPermissionStatus; microphone: OSPermissionStatus; camera: OSPermissionStatus; accessibility: OSPermissionStatus }
export type Plan = { upgraded: boolean; last_checked: number }
export type PreCreatedVideo = { id: string; link: string; config: S3UploadMeta }
-export type ProjectConfiguration = { aspectRatio: AspectRatio | null; background: BackgroundConfiguration; camera: CameraConfiguration; audio: AudioConfiguration; cursor: CursorConfiguration; hotkeys: HotkeysConfiguration; timeline?: TimelineConfiguration | null }
+export type ProjectConfiguration = { aspectRatio: AspectRatio | null; background: BackgroundConfiguration; camera: CameraConfiguration; audio: AudioConfiguration; cursor: CursorConfiguration; hotkeys: HotkeysConfiguration; timeline?: TimelineConfiguration | null; motionBlur: number | null }
export type ProjectRecordings = { display: Video; camera: Video | null; audio: Audio | null }
export type RecordingMeta = { pretty_name: string; sharing?: SharingMeta | null; display: Display; camera?: CameraMeta | null; audio?: AudioMeta | null; segments?: RecordingSegment[]; cursor: string | null }
export type RecordingMetaChanged = { id: string }
diff --git a/crates/project/src/configuration.rs b/crates/project/src/configuration.rs
index f8f2a13cd..181c492a9 100644
--- a/crates/project/src/configuration.rs
+++ b/crates/project/src/configuration.rs
@@ -328,6 +328,7 @@ pub struct ProjectConfiguration {
pub hotkeys: HotkeysConfiguration,
#[serde(default)]
pub timeline: Option<TimelineConfiguration>,
+ pub motion_blur: Option<f32>,
}
impl ProjectConfiguration {
@@ -361,6 +362,7 @@ impl Default for ProjectConfiguration {
cursor: CursorConfiguration::default(),
hotkeys: HotkeysConfiguration::default(),
timeline: None,
+ motion_blur: None,
}
}
}
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index 1e897da1b..a3acd73b5 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -483,8 +483,27 @@ impl ProjectUniforms {
let cursor_position = interpolate_cursor_position(&constants.cursor, time);
let zoom_keyframes = ZoomKeyframes::new(project);
+ let current_zoom = zoom_keyframes.get_amount(time as f64);
+ let prev_zoom = zoom_keyframes.get_amount((time - 1.0 / 30.0) as f64);
+
+ let velocity = if current_zoom != prev_zoom {
+ let scale_change = (current_zoom - prev_zoom) as f32;
+ // Reduce the velocity scale from 0.05 to 0.02
+ [
+ (scale_change * output_size.0 as f32) * 0.02, // Reduced from 0.05
+ (scale_change * output_size.1 as f32) * 0.02,
+ ]
+ } else {
+ [0.0, 0.0]
+ };
- let (zoom, zoom_origin_uv) = if let Some(cursor_position) = cursor_position {
+ let motion_blur_amount = if current_zoom != prev_zoom {
+ project.motion_blur.unwrap_or(0.2) // Reduced from 0.5 to 0.2
+ } else {
+ 0.0
+ };
+
+ let zoom_origin_uv = if let Some(cursor_position) = cursor_position {
(zoom_keyframes.get_amount(time as f64), cursor_position)
} else {
(1.0, Coord::new(XY { x: 0.0, y: 0.0 }))
@@ -492,9 +511,17 @@ impl ProjectUniforms {
let crop = Self::get_crop(options, project);
- let zoom_origin = zoom_origin_uv
- .to_raw_display_space(options)
- .to_cropped_display_space(options, project);
+ let zoom_origin = if let Some(cursor_position) = cursor_position {
+ cursor_position
+ .to_raw_display_space(options)
+ .to_cropped_display_space(options, project)
+ } else {
+ let center = XY::new(
+ options.screen_size.x as f64 / 2.0,
+ options.screen_size.y as f64 / 2.0,
+ );
+ Coord::<RawDisplaySpace>::new(center).to_cropped_display_space(options, project)
+ };
let (display, zoom) = {
let output_size = XY::new(output_size.0 as f64, output_size.1 as f64);
@@ -518,7 +545,7 @@ impl ProjectUniforms {
.clamp(display_offset.coord, end.coord);
let zoom = Zoom {
- amount: zoom,
+ amount: zoom_keyframes.get_amount(time as f64),
zoom_origin: screen_scale_origin,
// padding: screen_scale_origin,
};
@@ -543,6 +570,9 @@ impl ProjectUniforms {
target_size: [target_size.x as f32, target_size.y as f32],
rounding_px: (project.background.rounding / 100.0 * 0.5 * min_target_axis)
as f32,
+ mirror_x: if project.camera.mirror { 1.0 } else { 0.0 },
+ velocity_uv: velocity,
+ motion_blur_amount,
..Default::default()
},
zoom,
@@ -600,6 +630,8 @@ 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()
}
});
@@ -744,14 +776,16 @@ pub async fn produce_frame(
// First, clear the background
{
+ let bind_group = constants.gradient_or_color_pipeline.bind_group(
+ &constants.device,
+ &GradientOrColorUniforms::from(background).to_buffer(&constants.device),
+ );
+
do_render_pass(
&mut encoder,
get_either(texture_views, output_is_left),
&constants.gradient_or_color_pipeline.render_pipeline,
- constants.gradient_or_color_pipeline.bind_group(
- &constants.device,
- &GradientOrColorUniforms::from(background).to_buffer(&constants.device),
- ),
+ bind_group,
wgpu::LoadOp::Clear(wgpu::Color::BLACK),
);
@@ -1006,13 +1040,13 @@ fn draw_cursor(
let cursor_size_percentage = if uniforms.cursor_size <= 0.0 {
100.0
} else {
- uniforms.cursor_size
+ uniforms.cursor_size / 100.0 // Convert percentage to scale factor
};
// Calculate normalized size maintaining aspect ratio
let normalized_size = [
- STANDARD_CURSOR_HEIGHT * aspect_ratio,
- STANDARD_CURSOR_HEIGHT,
+ STANDARD_CURSOR_HEIGHT * aspect_ratio * cursor_size_percentage,
+ STANDARD_CURSOR_HEIGHT * cursor_size_percentage,
];
let position = uniforms
@@ -1031,7 +1065,7 @@ fn draw_cursor(
screen_bounds: uniforms.display.target_bounds,
cursor_size: cursor_size_percentage,
padding: [0.0; 3],
- _alignment: [0.0; 4],
+ _alignment: [0.0; 8],
};
let cursor_uniform_buffer =
@@ -1106,7 +1140,8 @@ struct CompositeVideoFrameUniforms {
pub target_size: [f32; 2],
pub rounding_px: f32,
pub mirror_x: f32,
- _padding: [f32; 3],
+ pub motion_blur_amount: f32,
+ _padding: [f32; 1],
}
impl CompositeVideoFrameUniforms {
@@ -1501,7 +1536,7 @@ struct CursorUniforms {
screen_bounds: [f32; 4],
cursor_size: f32,
padding: [f32; 3],
- _alignment: [f32; 4],
+ _alignment: [f32; 8],
}
fn find_cursor_event(cursor: &CursorData, time: f32) -> &CursorMoveEvent {
@@ -1531,7 +1566,7 @@ impl CursorPipeline {
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
- min_binding_size: Some(std::num::NonZeroU64::new(96).unwrap()),
+ min_binding_size: Some(std::num::NonZeroU64::new(112).unwrap()),
},
count: None,
},
diff --git a/crates/rendering/src/shaders/composite-video-frame.wgsl b/crates/rendering/src/shaders/composite-video-frame.wgsl
index e707388be..0ae87f1f1 100644
--- a/crates/rendering/src/shaders/composite-video-frame.wgsl
+++ b/crates/rendering/src/shaders/composite-video-frame.wgsl
@@ -6,7 +6,9 @@ struct Uniforms {
velocity_uv: vec2<f32>,
target_size: vec2<f32>,
rounding_px: f32,
- mirror_x: f32
+ mirror_x: f32,
+ motion_blur_amount: f32,
+ _padding: f32,
};
@group(0) @binding(0) var<uniform> u: Uniforms;
@@ -26,28 +28,55 @@ fn main_image(frag_color: vec4<f32>, frag_coord: vec2<f32>) -> vec4<f32> {
let target_uv = (frag_coord - u.target_bounds.xy) / u.target_size;
let crop_bounds_uv = vec4<f32>(u.crop_bounds.xy / u.frame_size, u.crop_bounds.zw / u.frame_size);
- let blur_scale = 5.0;
- var blur_samples = 50;
-
- if u.velocity_uv.x == 0.0 && u.velocity_uv.y == 0.0 {
- blur_samples = 1;
+ if target_uv.x < 0.0 || target_uv.x > 1.0 || target_uv.y < 0.0 || target_uv.y > 1.0 {
+ return textureSample(prev_tex, sampler0, uv);
}
- var color = vec4<f32>(0.0, 0.0, 0.0, 0.0);
-
- for (var i = 0; i < blur_samples; i++) {
- let t = f32(i) / f32(blur_samples - 1);
- let offset = u.velocity_uv * t * blur_scale;
- let sample_uv = target_uv - offset;
- var sample_color = sample_texture(sample_uv, crop_bounds_uv);
- sample_color = apply_rounded_corners(sample_color, sample_uv);
+ var base_color = sample_texture(target_uv, crop_bounds_uv);
+ base_color = apply_rounded_corners(base_color, target_uv);
- color += sample_color;
+ if u.motion_blur_amount <= 0.0 {
+ return mix(textureSample(prev_tex, sampler0, uv), base_color, base_color.a);
}
- let ret_color = color / f32(blur_samples);
+ let center = vec2<f32>(0.5, 0.5);
+ let dir = normalize(target_uv - center);
+
+ let num_samples = 16;
+ var accum = base_color;
+ var weight_sum = 1.0;
+
+ for (var i = 1; i < num_samples; i++) {
+ 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 angle_variation = (rand(target_uv + vec2<f32>(t * 2.0)) - 0.5) * 0.1;
+ 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)
+ );
+
+ let offset = rotated_dir * scale * t;
+
+ let sample_uv = target_uv - offset;
+ if sample_uv.x >= 0.0 && sample_uv.x <= 1.0 && sample_uv.y >= 0.0 && sample_uv.y <= 1.0 {
+ var sample_color = sample_texture(sample_uv, crop_bounds_uv);
+ sample_color = apply_rounded_corners(sample_color, sample_uv);
+
+ let weight = (1.0 - t) * (1.0 + random_offset * 0.2);
+ accum += sample_color * weight;
+ weight_sum += weight;
+ }
+ }
- return mix(textureSample(prev_tex, sampler0, uv), ret_color.rgba, ret_color.a);
+ let final_color = accum / weight_sum;
+
+ let blurred = vec4(final_color.rgb, base_color.a);
+
+ return mix(textureSample(prev_tex, sampler0, uv), blurred, blurred.a);
}
fn sample_texture(uv: vec2<f32>, crop_bounds_uv: vec4<f32>) -> vec4<f32> {
@@ -81,6 +110,11 @@ fn apply_rounded_corners(current_color: vec4<f32>, target_uv: vec2<f32>) -> vec4
return current_color;
}
+// Add this helper function for pseudo-random numbers
+fn rand(co: vec2<f32>) -> f32 {
+ return fract(sin(dot(co, vec2<f32>(12.9898, 78.233))) * 43758.5453);
+}
+
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
diff --git a/crates/rendering/src/shaders/cursor.wgsl b/crates/rendering/src/shaders/cursor.wgsl
index 1ee63d1c7..5ecdd42c0 100644
--- a/crates/rendering/src/shaders/cursor.wgsl
+++ b/crates/rendering/src/shaders/cursor.wgsl
@@ -1,3 +1,8 @@
+struct VertexOutput {
+ @builtin(position) position: vec4<f32>,
+ @location(0) uv: vec2<f32>,
+};
+
struct Uniforms {
position: vec4<f32>,
size: vec4<f32>,
@@ -5,39 +10,51 @@ struct Uniforms {
screen_bounds: vec4<f32>,
cursor_size: f32,
padding: vec3<f32>,
+ _alignment: vec4<f32>,
};
-@group(0) @binding(0) var<uniform> uniforms: Uniforms;
-@group(0) @binding(1) var cursor_texture: texture_2d<f32>;
-@group(0) @binding(2) var cursor_sampler: sampler;
+@group(0) @binding(0)
+var<uniform> uniforms: Uniforms;
-struct VertexOutput {
- @builtin(position) position: vec4<f32>,
- @location(0) tex_coords: vec2<f32>,
-};
+@group(0) @binding(1)
+var t_cursor: texture_2d<f32>;
+
+@group(0) @binding(2)
+var s_cursor: sampler;
@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
- var out: VertexOutput;
-
- let x = f32(vertex_index & 1u);
- let y = f32((vertex_index >> 1u) & 1u);
-
- let pos = uniforms.position.xy + vec2<f32>(x * uniforms.size.x, y * uniforms.size.y);
-
- let clip_pos = vec2<f32>(
- (pos.x / uniforms.output_size.x) * 2.0 - 1.0,
- -((pos.y / uniforms.output_size.y) * 2.0 - 1.0)
+ var positions = array<vec2<f32>, 4>(
+ vec2<f32>(-1.0, 1.0),
+ vec2<f32>(-1.0, -1.0),
+ vec2<f32>(1.0, 1.0),
+ vec2<f32>(1.0, -1.0)
);
+
+ var uvs = array<vec2<f32>, 4>(
+ vec2<f32>(0.0, 0.0),
+ vec2<f32>(0.0, 1.0),
+ vec2<f32>(1.0, 0.0),
+ vec2<f32>(1.0, 1.0)
+ );
+
+ let pos = positions[vertex_index];
+ let size = uniforms.size.xy;
+ let screen_pos = uniforms.position.xy;
- out.position = vec4<f32>(clip_pos, 0.0, 1.0);
- out.tex_coords = vec2<f32>(x, y);
+ // Apply cursor size scaling
+ let scaled_size = size * uniforms.cursor_size;
- return out;
+ // Calculate final position
+ let final_pos = (pos * scaled_size + screen_pos) / uniforms.output_size.xy * 2.0 - 1.0;
+
+ var output: VertexOutput;
+ output.position = vec4<f32>(final_pos, 0.0, 1.0);
+ output.uv = uvs[vertex_index];
+ return output;
}
@fragment
-fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
- let color = textureSample(cursor_texture, cursor_sampler, in.tex_coords);
- return color;
+fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
+ return textureSample(t_cursor, s_cursor, input.uv);
}