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.
1#import bevy_pbr::prepass_utils
2#import bevy_pbr::utils
3#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput
4#import bevy_render::globals::Globals
6#ifdef 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>;
10#else
11@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#endif
15@group(0) @binding(3) var texture_sampler: sampler;
16struct MotionBlur {
17 shutter_angle: f32,
18 samples: u32,
19#ifdef SIXTEEN_BYTE_ALIGNMENT
20 // WebGL2 structs must be 16 byte aligned.
21 _webgl2_padding: vec2<f32>
22#endif
24@group(0) @binding(4) var<uniform> settings: MotionBlur;
25@group(0) @binding(5) var<uniform> globals: Globals;
27@fragment
28fn fragment(
29 #ifdef MULTISAMPLED
30 @builtin(sample_index) sample_index: u32,
31 #endif
32 in: FullscreenVertexOutput
33) -> @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 MULTISAMPLED
38 let base_color = textureLoad(screen_texture, frag_coords, i32(sample_index));
39#else
40 let base_color = textureSample(screen_texture, texture_sampler, in.uv);
41#endif
43 let shutter_angle = settings.shutter_angle;
45#ifdef MULTISAMPLED
46 let this_motion_vector = textureLoad(motion_vectors, frag_coords, i32(sample_index)).rg;
47#else
48 let this_motion_vector = textureSample(motion_vectors, texture_sampler, in.uv).rg;
49#endif
51#ifdef MULTISAMPLED
52 let this_depth = textureLoad(depth, frag_coords, i32(sample_index)).x;
53#else
54 let this_depth = textureLoad(depth, frag_coords, 0).x;
55#endif
57 // The exposure vector is the distance that this fragment moved while the camera shutter was
58 // open. This is the motion vector (total distance traveled) multiplied by the shutter angle (a
59 // 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 further
63 // than they physically traveled during a frame, which is not possible. Note: we allow values
64 // 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 1
72 for (var i = -n_samples; i < n_samples; i++) {
73 // The current sample step vector, from in.uv
74 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 MULTISAMPLED
85 let sample_color = textureLoad(screen_texture, sample_coords, i32(sample_index));
86 #else
87 let sample_color = textureSample(screen_texture, texture_sampler, sample_uv);
88 #endif
89 #ifdef MULTISAMPLED
90 let sample_motion = textureLoad(motion_vectors, sample_coords, i32(sample_index)).rg;
91 #else
92 let sample_motion = textureSample(motion_vectors, texture_sampler, sample_uv).rg;
93 #endif
95 #ifdef MULTISAMPLED
96 let sample_depth = textureLoad(depth, sample_coords, i32(sample_index)).x;
97 #else
98 let sample_depth = textureLoad(depth, sample_coords, 0).x;
99 #endif
101 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 the
104 // background. This ensures that things like skyboxes, which do not write to depth, are
105 // 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 are
108 // common in motion-vector-based motion blur implementations. While some resources
109 // recommend using depth, I've found that sampling the velocity results in significantly
110 // better results. Unlike a depth heuristic, this is not scale dependent.
111 //
112 // The most distracting artifacts occur when a stationary foreground object is
113 // incorrectly sampled while blurring a moving background object, causing the stationary
114 // object to blur when it should be sharp ("background bleeding"). This is most obvious
115 // 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 being
119 // blurred to the UV being sampled, to answer the question "is it possible that this
120 // sample was occluding the fragment?"
121 //
122 // Note to future maintainers: proceed with caution when making any changes here, and
123 // ensure you check all occlusion/disocclusion scenarios and fullscreen camera rotation
124 // blur for regressions.
125 let frag_speed = length(step_vector);
126 let sample_speed = length(sample_motion) / 2.0; // Halved because the sample is centered
127 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 not
131 // cover enough distance to reach the original frag, there is no way it could have
132 // 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;
Contents