Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
refactor(rendering): improve cursor motion blur response
Richie McIlroya0c00262128e
diff --git a/crates/rendering/src/layers/cursor.rs b/crates/rendering/src/layers/cursor.rsindex 4ef81e5bb..6c6083463 100644--- a/crates/rendering/src/layers/cursor.rs+++ b/crates/rendering/src/layers/cursor.rs@@ -17,13 +17,12 @@ const CLICK_SHRINK_SIZE: f32 = 0.8;const CURSOR_IDLE_MIN_DELAY_MS: f64 = 500.0;const CURSOR_IDLE_FADE_OUT_MS: f64 = 400.0;const CURSOR_IDLE_RESUME_LOOKAHEAD_MS: f64 = 250.0;-const CURSOR_VECTOR_CAP: f32 = 320.0;-const CURSOR_MIN_MOTION_NORMALIZED: f32 = 0.01;-const CURSOR_MIN_MOTION_PX: f32 = 1.0;+const CURSOR_VECTOR_CAP: f32 = 64.0;+const CURSOR_MIN_MOTION_NORMALIZED: f32 = 0.004;+const CURSOR_FULL_MOTION_NORMALIZED: f32 = 0.035;const CURSOR_BASELINE_FPS: f32 = 60.0;const CURSOR_MULTIPLIER: f32 = 1.0;-const CURSOR_MAX_STRENGTH: f32 = 2.0;-const VELOCITY_BLEND_RATIO: f32 = 0.7;+const CURSOR_MAX_STRENGTH: f32 = 1.0;/// The size to render the svg to.static SVG_CURSOR_RASTERIZED_HEIGHT: u32 = 200;@@ -275,50 +274,35 @@ impl CursorLayer {let fps_scale = fps / CURSOR_BASELINE_FPS;let cursor_strength = (uniforms.motion_blur_amount * CURSOR_MULTIPLIER * fps_scale).clamp(0.0, CURSOR_MAX_STRENGTH);- let parent_motion = uniforms.display_parent_motion_px;- let child_motion = {- let delta_motion = uniforms- .prev_cursor- .as_ref()- .filter(|prev| prev.cursor_id == interpolated_cursor.cursor_id)- .map(|prev| {- let delta_uv = XY::new(- (interpolated_cursor.position.coord.x - prev.position.coord.x) as f32,- (interpolated_cursor.position.coord.y - prev.position.coord.y) as f32,- );- XY::new(- delta_uv.x * screen_size.x as f32,- delta_uv.y * screen_size.y as f32,- )- })- .unwrap_or_else(|| XY::new(0.0, 0.0));-- let spring_velocity = XY::new(- interpolated_cursor.velocity.x * screen_size.x as f32 / fps,- interpolated_cursor.velocity.y * screen_size.y as f32 / fps,- );-- XY::new(- delta_motion.x * (1.0 - VELOCITY_BLEND_RATIO)- + spring_velocity.x * VELOCITY_BLEND_RATIO,- delta_motion.y * (1.0 - VELOCITY_BLEND_RATIO)- + spring_velocity.y * VELOCITY_BLEND_RATIO,- )- };+ let child_motion = uniforms+ .prev_cursor+ .as_ref()+ .filter(|prev| prev.cursor_id == interpolated_cursor.cursor_id)+ .map(|prev| {+ let delta_uv = XY::new(+ (interpolated_cursor.position.coord.x - prev.position.coord.x) as f32,+ (interpolated_cursor.position.coord.y - prev.position.coord.y) as f32,+ );+ XY::new(+ delta_uv.x * screen_size.x as f32,+ delta_uv.y * screen_size.y as f32,+ )+ })+ .unwrap_or_else(|| XY::new(0.0, 0.0));let combined_motion_px = if cursor_strength <= f32::EPSILON {XY::new(0.0, 0.0)} else {- combine_cursor_motion(parent_motion, child_motion)+ child_motion};let normalized_motion = ((combined_motion_px.x / screen_diag).powi(2)+ (combined_motion_px.y / screen_diag).powi(2)).sqrt();- let has_motion =- normalized_motion > CURSOR_MIN_MOTION_NORMALIZED && cursor_strength > f32::EPSILON;- let scaled_motion = if has_motion {- clamp_cursor_vector(combined_motion_px * cursor_strength)+ let motion_response = cursor_motion_response(normalized_motion);+ let effective_cursor_strength = cursor_strength * motion_response;+ let scaled_motion = if effective_cursor_strength > f32::EPSILON {+ cursor_blur_vector(combined_motion_px, effective_cursor_strength)} else {XY::new(0.0, 0.0)};@@ -478,8 +462,6 @@ impl CursorLayer {zoom,) - zoomed_position;- let effective_strength = if has_motion { cursor_strength } else { 0.0 };-let cursor_uniforms = CursorUniforms {position_size: [zoomed_position.x as f32,@@ -497,7 +479,7 @@ impl CursorLayer {motion_vector_strength: [scaled_motion.x,scaled_motion.y,- effective_strength,+ effective_cursor_strength,cursor_opacity,],rotation_params: [@@ -529,22 +511,22 @@ impl CursorLayer {}}-fn combine_cursor_motion(parent: XY<f32>, child: XY<f32>) -> XY<f32> {- fn combine_axis(parent: f32, child: f32) -> f32 {- if parent.abs() > CURSOR_MIN_MOTION_PX- && child.abs() > CURSOR_MIN_MOTION_PX- && parent.signum() != child.signum()- {- 0.0- } else {- parent + child- }+fn cursor_blur_vector(motion: XY<f32>, strength: f32) -> XY<f32> {+ clamp_cursor_vector(motion * strength)+}++fn cursor_motion_response(amount: f32) -> f32 {+ if amount <= CURSOR_MIN_MOTION_NORMALIZED {+ return 0.0;+ }+ if amount >= CURSOR_FULL_MOTION_NORMALIZED {+ return 1.0;}- XY::new(- combine_axis(parent.x, child.x),- combine_axis(parent.y, child.y),- )+ let t = ((amount - CURSOR_MIN_MOTION_NORMALIZED)+ / (CURSOR_FULL_MOTION_NORMALIZED - CURSOR_MIN_MOTION_NORMALIZED))+ .clamp(0.0, 1.0);+ t * t * (3.0 - 2.0 * t)}fn clamp_cursor_vector(vec: XY<f32>) -> XY<f32> {@@ -806,6 +788,32 @@ mod tests {}}+ #[test]+ fn cursor_blur_vector_caps_fast_motion() {+ let motion = cursor_blur_vector(XY::new(800.0, 0.0), CURSOR_MAX_STRENGTH);+ let len = (motion.x * motion.x + motion.y * motion.y).sqrt();++ assert!(len <= CURSOR_VECTOR_CAP + f32::EPSILON);+ }++ #[test]+ fn cursor_motion_response_ramps_with_velocity() {+ assert_eq!(+ cursor_motion_response(CURSOR_MIN_MOTION_NORMALIZED * 0.5),+ 0.0+ );++ let mid = cursor_motion_response(+ (CURSOR_MIN_MOTION_NORMALIZED + CURSOR_FULL_MOTION_NORMALIZED) * 0.5,+ );++ assert!(mid > 0.0 && mid < 1.0);+ assert_eq!(+ cursor_motion_response(CURSOR_FULL_MOTION_NORMALIZED * 1.5),+ 1.0+ );+ }+#[test]fn opacity_stays_visible_with_recent_move() {let cursor = cursor_events(&[(0.0, 0.0, 0.0), (1500.0, 0.1, 0.1)]);diff --git a/crates/rendering/src/shaders/cursor.wgsl b/crates/rendering/src/shaders/cursor.wgslindex 34e5103e6..b9fc6d60e 100644--- a/crates/rendering/src/shaders/cursor.wgsl+++ b/crates/rendering/src/shaders/cursor.wgsl@@ -21,6 +21,27 @@ var t_cursor: texture_2d<f32>;var s_cursor: sampler;const MAX_ROTATION_RADIANS: f32 = 0.34906584;+const MAX_CURSOR_BLUR_UV: f32 = 0.24;++fn cursor_velocity_uv() -> vec2<f32> {+ let motion_vec = uniforms.motion_vector_strength.xy;+ let blur_strength = uniforms.motion_vector_strength.z;+ let cursor_size = uniforms.position_size.zw;+ let motion_len = length(motion_vec);++ if (motion_len < 0.5 || blur_strength < 0.001 || cursor_size.x <= 0.0 || cursor_size.y <= 0.0) {+ return vec2<f32>(0.0, 0.0);+ }++ let raw_velocity_uv = motion_vec / cursor_size;+ let raw_vel_len = length(raw_velocity_uv);++ if (raw_vel_len < 0.005) {+ return vec2<f32>(0.0, 0.0);+ }++ return raw_velocity_uv * min(1.0, MAX_CURSOR_BLUR_UV / raw_vel_len);+}fn rotate_point(p: vec2<f32>, center: vec2<f32>, angle: f32) -> vec2<f32> {let cos_a = cos(angle);@@ -34,21 +55,18 @@ fn rotate_point(p: vec2<f32>, center: vec2<f32>, angle: f32) -> vec2<f32> {@vertexfn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {- var positions = 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)- );-- var uvs = array<vec2<f32>, 4>(+ var corners = 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 blur_uv = cursor_velocity_uv();+ let uv_min = min(vec2<f32>(0.0, 0.0), -blur_uv);+ let uv_max = max(vec2<f32>(1.0, 1.0), vec2<f32>(1.0, 1.0) - blur_uv);+ let local_uv = uv_min + (uv_max - uv_min) * corners[vertex_index];+ let pos = vec2<f32>(local_uv.x, -local_uv.y);let screen_pos = uniforms.position_size.xy;let cursor_size = uniforms.position_size.zw;@@ -68,42 +86,40 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {var output: VertexOutput;output.position = vec4<f32>(final_pos, 0.0, 1.0);- output.uv = uvs[vertex_index];+ output.uv = local_uv;return output;}+fn sample_cursor(uv: vec2<f32>) -> vec4<f32> {+ if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {+ return vec4<f32>(0.0, 0.0, 0.0, 0.0);+ }++ return textureSample(t_cursor, s_cursor, uv);+}+@fragmentfn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {- let motion_vec = uniforms.motion_vector_strength.xy;+ let velocity_uv = cursor_velocity_uv();let blur_strength = uniforms.motion_vector_strength.z;let opacity = uniforms.motion_vector_strength.w;+ let base_color = sample_cursor(input.uv);- let motion_len = length(motion_vec);- if (motion_len < 0.5 || blur_strength < 0.001) {- return textureSample(t_cursor, s_cursor, input.uv) * opacity;- }-- let cursor_size = uniforms.position_size.zw;- let velocity_uv = motion_vec / cursor_size;- let vel_len = length(velocity_uv);-- if (vel_len < 0.005) {- return textureSample(t_cursor, s_cursor, input.uv) * opacity;+ if (length(velocity_uv) < 0.005 || blur_strength < 0.001) {+ return base_color * opacity;}- let kernel_size = 21;- let k = kernel_size - 1;- let offset_base = -vel_len / 2.0 / vel_len - 0.5;-- var color = textureSample(t_cursor, s_cursor, input.uv);+ let kernel_size = 21.0;+ let k = kernel_size - 1.0;+ var color = base_color;- for (var i = 0; i < 20; i++) {- let bias = velocity_uv * (f32(i) / f32(k) + offset_base);+ for (var i = 1; i <= 20; i = i + 1) {+ let bias = velocity_uv * (f32(i) / k);let sample_uv = input.uv + bias;- color += textureSample(t_cursor, s_cursor, sample_uv);+ color += sample_cursor(sample_uv);}- color = color / f32(kernel_size);- color *= opacity;- return color;+ color /= kernel_size;+ let blur_mix = clamp(blur_strength, 0.0, 1.0);+ return mix(base_color, color, blur_mix) * opacity;}