1#import bevy_pbr::prepass_utils2#import bevy_pbr::utils3#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput4#import bevy_render::globals::Globals6#ifdef MULTISAMPLED7@group(0) @binding(0) var screen_texture: texture_2d<f32>;8@group(0) @binding(1) var motion_vectors: texture_multisampled_2d<f32>;9@group(0) @binding(2) var depth: texture_multisampled_2d<f32>;10#else11@group(0) @binding(0) var screen_texture: texture_2d<f32>;12@group(0) @binding(1) var motion_vectors: texture_2d<f32>;13@group(0) @binding(2) var depth: texture_2d<f32>;14#endif15@group(0) @binding(3) var texture_sampler: sampler;16struct MotionBlur {17 shutter_angle: f32,18 samples: u32,19#ifdef SIXTEEN_BYTE_ALIGNMENT20 // WebGL2 structs must be 16 byte aligned.21 _webgl2_padding: vec2<f32>22#endif23}24@group(0) @binding(4) var<uniform> settings: MotionBlur;25@group(0) @binding(5) var<uniform> globals: Globals;27@fragment28fn fragment(29 #ifdef MULTISAMPLED30 @builtin(sample_index) sample_index: u32,31 #endif32 in: FullscreenVertexOutput33) -> @location(0) vec4<f32> {34 let texture_size = vec2<f32>(textureDimensions(screen_texture));35 let frag_coords = vec2<i32>(in.uv * texture_size);37#ifdef MULTISAMPLED38 let base_color = textureLoad(screen_texture, frag_coords, i32(sample_index));39#else40 let base_color = textureSample(screen_texture, texture_sampler, in.uv);41#endif43 let shutter_angle = settings.shutter_angle;45#ifdef MULTISAMPLED46 let this_motion_vector = textureLoad(motion_vectors, frag_coords, i32(sample_index)).rg;47#else48 let this_motion_vector = textureSample(motion_vectors, texture_sampler, in.uv).rg;49#endif51#ifdef MULTISAMPLED52 let this_depth = textureLoad(depth, frag_coords, i32(sample_index)).x;53#else54 let this_depth = textureLoad(depth, frag_coords, 0).x;55#endif57 // The exposure vector is the distance that this fragment moved while the camera shutter was58 // open. This is the motion vector (total distance traveled) multiplied by the shutter angle (a59 // fraction). In film, the shutter angle is commonly 0.5 or "180 degrees" (out of 360 total).60 // This means that for a frame time of 20ms, the shutter is only open for 10ms.61 //62 // Using a shutter angle larger than 1.0 is non-physical, objects would need to move further63 // than they physically traveled during a frame, which is not possible. Note: we allow values64 // larger than 1.0 because it may be desired for artistic reasons.65 let exposure_vector = shutter_angle * this_motion_vector;67 var accumulator: vec4<f32>;68 var weight_total = 0.0;69 let n_samples = i32(settings.samples);70 let noise = utils::interleaved_gradient_noise(vec2<f32>(frag_coords), globals.frame_count); // 0 to 172 for (var i = -n_samples; i < n_samples; i++) {73 // The current sample step vector, from in.uv74 let step_vector = 0.5 * exposure_vector * (f32(i) + noise) / f32(n_samples);75 var sample_uv = in.uv + step_vector;77 // If the sample is off screen, skip it.78 if sample_uv.x < 0.0 || sample_uv.x > 1.0 || sample_uv.y < 0.0 || sample_uv.y > 1.0 {79 continue;80 }82 let sample_coords = vec2<i32>(sample_uv * texture_size);84 #ifdef MULTISAMPLED85 let sample_color = textureLoad(screen_texture, sample_coords, i32(sample_index));86 #else87 let sample_color = textureSample(screen_texture, texture_sampler, sample_uv);88 #endif89 #ifdef MULTISAMPLED90 let sample_motion = textureLoad(motion_vectors, sample_coords, i32(sample_index)).rg;91 #else92 let sample_motion = textureSample(motion_vectors, texture_sampler, sample_uv).rg;93 #endif95 #ifdef MULTISAMPLED96 let sample_depth = textureLoad(depth, sample_coords, i32(sample_index)).x;97 #else98 let sample_depth = textureLoad(depth, sample_coords, 0).x;99 #endif101 var weight = 1.0;102 let is_sample_in_fg = !(sample_depth < this_depth && sample_depth > 0.0);103 // If the depth is 0.0, this fragment has no depth written to it and we assume it is in the104 // background. This ensures that things like skyboxes, which do not write to depth, are105 // correctly sampled in motion blur.106 if sample_depth != 0.0 && is_sample_in_fg {107 // The following weight calculation is used to eliminate ghosting artifacts that are108 // common in motion-vector-based motion blur implementations. While some resources109 // recommend using depth, I've found that sampling the velocity results in significantly110 // better results. Unlike a depth heuristic, this is not scale dependent.111 //112 // The most distracting artifacts occur when a stationary foreground object is113 // incorrectly sampled while blurring a moving background object, causing the stationary114 // object to blur when it should be sharp ("background bleeding"). This is most obvious115 // when the camera is tracking a fast moving object. The tracked object should be sharp,116 // and should not bleed into the motion blurred background.117 //118 // To attenuate these incorrect samples, we compare the motion of the fragment being119 // blurred to the UV being sampled, to answer the question "is it possible that this120 // sample was occluding the fragment?"121 //122 // Note to future maintainers: proceed with caution when making any changes here, and123 // ensure you check all occlusion/disocclusion scenarios and fullscreen camera rotation124 // blur for regressions.125 let frag_speed = length(step_vector);126 let sample_speed = length(sample_motion) / 2.0; // Halved because the sample is centered127 let cos_angle = dot(step_vector, sample_motion) / (frag_speed * sample_speed * 2.0);128 let motion_similarity = clamp(abs(cos_angle), 0.0, 1.0);129 if sample_speed * motion_similarity < frag_speed {130 // Project the sample's motion onto the frag's motion vector. If the sample did not131 // cover enough distance to reach the original frag, there is no way it could have132 // influenced this frag at all, and should be discarded.133 weight = 0.0;134 }135 }136 weight_total += weight;137 accumulator += weight * sample_color;138 }140 let has_moved_less_than_a_pixel =141 dot(this_motion_vector * texture_size, this_motion_vector * texture_size) < 1.0;142 // In case no samples were accepted, fall back to base color.143 // We also fall back if motion is small, to not break antialiasing.144 if weight_total <= 0.0 || has_moved_less_than_a_pixel {145 accumulator = base_color;146 weight_total = 1.0;147 }148 return accumulator / weight_total;149}