Skip to main content

shader_prepass/
shader_prepass.rs

1//! Bevy has an optional prepass that is controlled per-material. A prepass is a rendering pass that runs before the main pass.
2//! It will optionally generate various view textures. Currently it supports depth, normal, and motion vector textures.
3//! The textures are not generated for any material using alpha blending.
4
5use bevy::{
6    core_pipeline::prepass::{DepthPrepass, MotionVectorPrepass, NormalPrepass},
7    light::NotShadowCaster,
8    pbr::PbrPlugin,
9    prelude::*,
10    reflect::TypePath,
11    render::render_resource::{AsBindGroup, ShaderType},
12    shader::ShaderRef,
13};
14
15/// This example uses a shader source file from the assets subdirectory
16const PREPASS_SHADER_ASSET_PATH: &str = "shaders/show_prepass.wgsl";
17const MATERIAL_SHADER_ASSET_PATH: &str = "shaders/custom_material.wgsl";
18
19fn main() {
20    App::new()
21        .add_plugins((
22            DefaultPlugins.set(PbrPlugin {
23                // The prepass is enabled by default on the StandardMaterial,
24                // but you can disable it if you need to.
25                //
26                // prepass_enabled: false,
27                ..default()
28            }),
29            MaterialPlugin::<CustomMaterial>::default(),
30            MaterialPlugin::<PrepassOutputMaterial>::default(),
31        ))
32        .add_systems(Startup, setup)
33        .add_systems(Update, (rotate, toggle_prepass_view))
34        .run();
35}
36
37/// set up a simple 3D scene
38fn setup(
39    mut commands: Commands,
40    mut meshes: ResMut<Assets<Mesh>>,
41    mut materials: ResMut<Assets<CustomMaterial>>,
42    mut std_materials: ResMut<Assets<StandardMaterial>>,
43    mut depth_materials: ResMut<Assets<PrepassOutputMaterial>>,
44    asset_server: Res<AssetServer>,
45) {
46    // camera
47    commands.spawn((
48        Camera3d::default(),
49        Transform::from_xyz(-2.0, 3., 5.0).looking_at(Vec3::ZERO, Vec3::Y),
50        // Disabling MSAA for maximum compatibility. Shader prepass with MSAA needs GPU capability MULTISAMPLED_SHADING
51        Msaa::Off,
52        // To enable the prepass you need to add the components associated with the ones you need
53        // This will write the depth buffer to a texture that you can use in the main pass
54        DepthPrepass,
55        // This will generate a texture containing world normals (with normal maps applied)
56        NormalPrepass,
57        // This will generate a texture containing screen space pixel motion vectors
58        MotionVectorPrepass,
59    ));
60
61    // plane
62    commands.spawn((
63        Mesh3d(meshes.add(Plane3d::default().mesh().size(5.0, 5.0))),
64        MeshMaterial3d(std_materials.add(Color::srgb(0.3, 0.5, 0.3))),
65    ));
66
67    // A quad that shows the outputs of the prepass
68    // To make it easy, we just draw a big quad right in front of the camera.
69    // For a real application, this isn't ideal.
70    commands.spawn((
71        Mesh3d(meshes.add(Rectangle::new(20.0, 20.0))),
72        MeshMaterial3d(depth_materials.add(PrepassOutputMaterial {
73            settings: ShowPrepassSettings::default(),
74        })),
75        Transform::from_xyz(-0.75, 1.25, 3.0).looking_at(Vec3::new(2.0, -2.5, -5.0), Vec3::Y),
76        NotShadowCaster,
77    ));
78
79    // Opaque cube
80    commands.spawn((
81        Mesh3d(meshes.add(Cuboid::default())),
82        MeshMaterial3d(materials.add(CustomMaterial {
83            color: LinearRgba::WHITE,
84            color_texture: Some(asset_server.load("branding/icon.png")),
85            alpha_mode: AlphaMode::Opaque,
86        })),
87        Transform::from_xyz(-1.0, 0.5, 0.0),
88        Rotates,
89    ));
90
91    // Cube with alpha mask
92    commands.spawn((
93        Mesh3d(meshes.add(Cuboid::default())),
94        MeshMaterial3d(std_materials.add(StandardMaterial {
95            alpha_mode: AlphaMode::Mask(1.0),
96            base_color_texture: Some(asset_server.load("branding/icon.png")),
97            ..default()
98        })),
99        Transform::from_xyz(0.0, 0.5, 0.0),
100    ));
101
102    // Cube with alpha blending.
103    // Transparent materials are ignored by the prepass
104    commands.spawn((
105        Mesh3d(meshes.add(Cuboid::default())),
106        MeshMaterial3d(materials.add(CustomMaterial {
107            color: LinearRgba::WHITE,
108            color_texture: Some(asset_server.load("branding/icon.png")),
109            alpha_mode: AlphaMode::Blend,
110        })),
111        Transform::from_xyz(1.0, 0.5, 0.0),
112    ));
113
114    // light
115    commands.spawn((
116        PointLight {
117            shadow_maps_enabled: true,
118            ..default()
119        },
120        Transform::from_xyz(4.0, 8.0, 4.0),
121    ));
122
123    commands.spawn((
124        Text::default(),
125        Node {
126            position_type: PositionType::Absolute,
127            top: px(12),
128            left: px(12),
129            ..default()
130        },
131        children![
132            TextSpan::new("Prepass Output: transparent\n"),
133            TextSpan::new("\n\n"),
134            TextSpan::new("Controls\n"),
135            TextSpan::new("---------------\n"),
136            TextSpan::new("Space - Change output\n"),
137        ],
138    ));
139}
140
141// This is the struct that will be passed to your shader
142#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
143struct CustomMaterial {
144    #[uniform(0)]
145    color: LinearRgba,
146    #[texture(1)]
147    #[sampler(2)]
148    color_texture: Option<Handle<Image>>,
149    alpha_mode: AlphaMode,
150}
151
152/// Not shown in this example, but if you need to specialize your material, the specialize
153/// function will also be used by the prepass
154impl Material for CustomMaterial {
155    fn fragment_shader() -> ShaderRef {
156        MATERIAL_SHADER_ASSET_PATH.into()
157    }
158
159    fn alpha_mode(&self) -> AlphaMode {
160        self.alpha_mode
161    }
162
163    // You can override the default shaders used in the prepass if your material does
164    // anything not supported by the default prepass
165    // fn prepass_fragment_shader() -> ShaderRef {
166    //     "shaders/custom_material.wgsl".into()
167    // }
168}
169
170#[derive(Component)]
171struct Rotates;
172
173fn rotate(mut q: Query<&mut Transform, With<Rotates>>, time: Res<Time>) {
174    for mut t in q.iter_mut() {
175        let rot = (ops::sin(time.elapsed_secs()) * 0.5 + 0.5) * std::f32::consts::PI * 2.0;
176        t.rotation = Quat::from_rotation_z(rot);
177    }
178}
179
180#[derive(Debug, Clone, Default, ShaderType)]
181struct ShowPrepassSettings {
182    show_depth: u32,
183    show_normals: u32,
184    show_motion_vectors: u32,
185    padding_1: u32,
186    padding_2: u32,
187}
188
189// This shader simply loads the prepass texture and outputs it directly
190#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
191struct PrepassOutputMaterial {
192    #[uniform(0)]
193    settings: ShowPrepassSettings,
194}
195
196impl Material for PrepassOutputMaterial {
197    fn fragment_shader() -> ShaderRef {
198        PREPASS_SHADER_ASSET_PATH.into()
199    }
200
201    // This needs to be transparent in order to show the scene behind the mesh
202    fn alpha_mode(&self) -> AlphaMode {
203        AlphaMode::Blend
204    }
205
206    fn enable_prepass() -> bool {
207        false
208    }
209}
210
211/// Every time you press space, it will cycle between transparent, depth and normals view
212fn toggle_prepass_view(
213    mut prepass_view: Local<u32>,
214    keycode: Res<ButtonInput<KeyCode>>,
215    material_handle: Single<&MeshMaterial3d<PrepassOutputMaterial>>,
216    mut materials: ResMut<Assets<PrepassOutputMaterial>>,
217    text: Single<Entity, With<Text>>,
218    mut writer: TextUiWriter,
219) {
220    if keycode.just_pressed(KeyCode::Space) {
221        *prepass_view = (*prepass_view + 1) % 4;
222
223        let label = match *prepass_view {
224            0 => "transparent",
225            1 => "depth",
226            2 => "normals",
227            3 => "motion vectors",
228            _ => unreachable!(),
229        };
230        let text = *text;
231        *writer.text(text, 1) = format!("Prepass Output: {label}\n");
232        writer.for_each_color(text, |mut color| {
233            color.0 = Color::WHITE;
234        });
235
236        let mut mat = materials.get_mut(*material_handle).unwrap();
237        mat.settings.show_depth = (*prepass_view == 1) as u32;
238        mat.settings.show_normals = (*prepass_view == 2) as u32;
239        mat.settings.show_motion_vectors = (*prepass_view == 3) as u32;
240    }
241}