Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1import bevy_pbr::prepass::utils as prepass_utils;
2import bevy_pbr::render::utils;
3import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput;
4import bevy_render::globals::Globals;
6@if(MULTISAMPLED) {
7@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>;
11@else {
12@group(0) @binding(0) var screen_texture: texture_2d<f32>;
13@group(0) @binding(1) var motion_vectors: texture_2d<f32>;
14@group(0) @binding(2) var depth: texture_2d<f32>;
16@group(0) @binding(3) var texture_sampler: sampler;
17struct MotionBlur {
18 shutter_angle: f32,
19 samples: u32,
20@if(SIXTEEN_BYTE_ALIGNMENT)
21 // WebGL2 structs must be 16 byte aligned.
22 _webgl2_padding: vec2<f32>
24@group(0) @binding(4) var<uniform> settings: MotionBlur;
25@group(0) @binding(5) var<uniform> globals: Globals;
27@fragment
28fn fragment(
29 @if(MULTISAMPLED)
30 @builtin(sample_index) sample_index: u32,
31 in: FullscreenVertexOutput
32) -> @location(0) vec4<f32> {
33 let texture_size = vec2<f32>(textureDimensions(screen_texture));
34 let frag_coords = vec2<i32>(in.uv * texture_size);
36@if(MULTISAMPLED)
37 let base_color = textureLoad(screen_texture, frag_coords, i32(sample_index));
38@else
39 let base_color = textureSample(screen_texture, texture_sampler, in.uv);
41 let shutter_angle = settings.shutter_angle;
43@if(MULTISAMPLED)
44 let this_motion_vector = textureLoad(motion_vectors, frag_coords, i32(sample_index)).rg;
45@else
46 let this_motion_vector = textureSample(motion_vectors, texture_sampler, in.uv).rg;
48@if(MULTISAMPLED)
49 let this_depth = textureLoad(depth, frag_coords, i32(sample_index)).x;
50@else
51 let this_depth = textureLoad(depth, frag_coords, 0).x;
53 // The exposure vector is the distance that this fragment moved while the camera shutter was
54 // open. This is the motion vector (total distance traveled) multiplied by the shutter angle (a
55 // fraction). In film, the shutter angle is commonly 0.5 or "180 degrees" (out of 360 total).
56 // This means that for a frame time of 20ms, the shutter is only open for 10ms.
57 //
58 // Using a shutter angle larger than 1.0 is non-physical, objects would need to move further
59 // than they physically traveled during a frame, which is not possible. Note: we allow values
60 // larger than 1.0 because it may be desired for artistic reasons.
61 let exposure_vector = shutter_angle * this_motion_vector;
63 var accumulator: vec4<f32>;
64 var weight_total = 0.0;
65 let n_samples = i32(settings.samples);
66 let noise = utils::interleaved_gradient_noise(vec2<f32>(frag_coords), globals.frame_count); // 0 to 1
68 for (var i = -n_samples; i < n_samples; i++) {
69 // The current sample step vector, from in.uv
70 let step_vector = 0.5 * exposure_vector * (f32(i) + noise) / f32(n_samples);
71 var sample_uv = in.uv + step_vector;
73 // If the sample is off screen, skip it.
74 if sample_uv.x < 0.0 || sample_uv.x > 1.0 || sample_uv.y < 0.0 || sample_uv.y > 1.0 {
75 continue;
76 }
78 let sample_coords = vec2<i32>(sample_uv * texture_size);
80 @if(MULTISAMPLED)
81 let sample_color = textureLoad(screen_texture, sample_coords, i32(sample_index));
82 @else
83 let sample_color = textureSample(screen_texture, texture_sampler, sample_uv);
84 @if(MULTISAMPLED)
85 let sample_motion = textureLoad(motion_vectors, sample_coords, i32(sample_index)).rg;
86 @else
87 let sample_motion = textureSample(motion_vectors, texture_sampler, sample_uv).rg;
89 @if(MULTISAMPLED)
90 let sample_depth = textureLoad(depth, sample_coords, i32(sample_index)).x;
91 @else
92 let sample_depth = textureLoad(depth, sample_coords, 0).x;
94 var weight = 1.0;
95 let is_sample_in_fg = !(sample_depth < this_depth && sample_depth > 0.0);
96 // If the depth is 0.0, this fragment has no depth written to it and we assume it is in the
97 // background. This ensures that things like skyboxes, which do not write to depth, are
98 // correctly sampled in motion blur.
99 if sample_depth != 0.0 && is_sample_in_fg {
100 // The following weight calculation is used to eliminate ghosting artifacts that are
101 // common in motion-vector-based motion blur implementations. While some resources
102 // recommend using depth, I've found that sampling the velocity results in significantly
103 // better results. Unlike a depth heuristic, this is not scale dependent.
104 //
105 // The most distracting artifacts occur when a stationary foreground object is
106 // incorrectly sampled while blurring a moving background object, causing the stationary
107 // object to blur when it should be sharp ("background bleeding"). This is most obvious
108 // when the camera is tracking a fast moving object. The tracked object should be sharp,
109 // and should not bleed into the motion blurred background.
110 //
111 // To attenuate these incorrect samples, we compare the motion of the fragment being
112 // blurred to the UV being sampled, to answer the question "is it possible that this
113 // sample was occluding the fragment?"
114 //
115 // Note to future maintainers: proceed with caution when making any changes here, and
116 // ensure you check all occlusion/disocclusion scenarios and fullscreen camera rotation
117 // blur for regressions.
118 let frag_speed = length(step_vector);
119 let sample_speed = length(sample_motion) / 2.0; // Halved because the sample is centered
120 let cos_angle = dot(step_vector, sample_motion) / (frag_speed * sample_speed * 2.0);
121 let motion_similarity = clamp(abs(cos_angle), 0.0, 1.0);
122 if sample_speed * motion_similarity < frag_speed {
123 // Project the sample's motion onto the frag's motion vector. If the sample did not
124 // cover enough distance to reach the original frag, there is no way it could have
125 // influenced this frag at all, and should be discarded.
126 weight = 0.0;
127 }
128 }
129 weight_total += weight;
130 accumulator += weight * sample_color;
131 }
133 let has_moved_less_than_a_pixel =
134 dot(this_motion_vector * texture_size, this_motion_vector * texture_size) < 1.0;
135 // In case no samples were accepted, fall back to base color.
136 // We also fall back if motion is small, to not break antialiasing.
137 if weight_total <= 0.0 || has_moved_less_than_a_pixel {
138 accumulator = base_color;
139 weight_total = 1.0;
140 }
141 return accumulator / weight_total;
Contents