Skip to content

Commit 97f3df5

Browse files
IceSentryJMS55superdump
authored andcommitted
Add low level post process example using a custom render pass (bevyengine#6909)
Co-authored-by: JMS55 <[email protected]> Co-authored-by: Robert Swain <[email protected]>
1 parent 5ece7ce commit 97f3df5

File tree

4 files changed

+473
-2
lines changed

4 files changed

+473
-2
lines changed

Cargo.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,11 +1385,21 @@ name = "post_processing"
13851385
path = "examples/shader/post_processing.rs"
13861386

13871387
[package.metadata.example.post_processing]
1388-
name = "Post Processing"
1388+
name = "Post Processing - Render To Texture"
13891389
description = "A custom post processing effect, using two cameras, with one reusing the render texture of the first one"
13901390
category = "Shaders"
13911391
wasm = true
13921392

1393+
[[example]]
1394+
name = "post_process_pass"
1395+
path = "examples/shader/post_process_pass.rs"
1396+
1397+
[package.metadata.example.post_process_pass]
1398+
name = "Post Processing - Custom Render Pass"
1399+
description = "A custom post processing effect, using a custom render pass that runs after the main pass"
1400+
category = "Shaders"
1401+
wasm = true
1402+
13931403
[[example]]
13941404
name = "shader_defs"
13951405
path = "examples/shader/shader_defs.rs"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// This shader computes the chromatic aberration effect
2+
3+
#import bevy_pbr::utils
4+
5+
// Since post processing is a fullscreen effect, we use the fullscreen vertex shader provided by bevy.
6+
// This will import a vertex shader that renders a single fullscreen triangle.
7+
//
8+
// A fullscreen triangle is a single triangle that covers the entire screen.
9+
// The box in the top left in that diagram is the screen. The 4 x are the corner of the screen
10+
//
11+
// Y axis
12+
// 1 | x-----x......
13+
// 0 | | s | . ´
14+
// -1 | x_____x´
15+
// -2 | : .´
16+
// -3 | :´
17+
// +--------------- X axis
18+
// -1 0 1 2 3
19+
//
20+
// As you can see, the triangle ends up bigger than the screen.
21+
//
22+
// You don't need to worry about this too much since bevy will compute the correct UVs for you.
23+
#import bevy_core_pipeline::fullscreen_vertex_shader
24+
25+
@group(0) @binding(0)
26+
var screen_texture: texture_2d<f32>;
27+
@group(0) @binding(1)
28+
var texture_sampler: sampler;
29+
struct PostProcessSettings {
30+
intensity: f32,
31+
}
32+
@group(0) @binding(2)
33+
var<uniform> settings: PostProcessSettings;
34+
35+
@fragment
36+
fn fragment(in: FullscreenVertexOutput) -> @location(0) vec4<f32> {
37+
// Chromatic aberration strength
38+
let offset_strength = settings.intensity;
39+
40+
// Sample each color channel with an arbitrary shift
41+
return vec4<f32>(
42+
textureSample(screen_texture, texture_sampler, in.uv + vec2<f32>(offset_strength, -offset_strength)).r,
43+
textureSample(screen_texture, texture_sampler, in.uv + vec2<f32>(-offset_strength, 0.0)).g,
44+
textureSample(screen_texture, texture_sampler, in.uv + vec2<f32>(0.0, offset_strength)).b,
45+
1.0
46+
);
47+
}
48+

examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ Example | Description
278278
[Material - GLSL](../examples/shader/shader_material_glsl.rs) | A shader that uses the GLSL shading language
279279
[Material - Screenspace Texture](../examples/shader/shader_material_screenspace_texture.rs) | A shader that samples a texture with view-independent UV coordinates
280280
[Material Prepass](../examples/shader/shader_prepass.rs) | A shader that uses the various textures generated by the prepass
281-
[Post Processing](../examples/shader/post_processing.rs) | A custom post processing effect, using two cameras, with one reusing the render texture of the first one
281+
[Post Processing - Custom Render Pass](../examples/shader/post_process_pass.rs) | A custom post processing effect, using a custom render pass that runs after the main pass
282+
[Post Processing - Render To Texture](../examples/shader/post_processing.rs) | A custom post processing effect, using two cameras, with one reusing the render texture of the first one
282283
[Shader Defs](../examples/shader/shader_defs.rs) | A shader that uses "shaders defs" (a bevy tool to selectively toggle parts of a shader)
283284
[Texture Binding Array (Bindless Textures)](../examples/shader/texture_binding_array.rs) | A shader that shows how to bind and sample multiple textures as a binding array (a.k.a. bindless textures).
284285

0 commit comments

Comments
 (0)