Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/shader_advanced/fullscreen_material.rs
9341 views
1
//! Demonstrates how to write a custom fullscreen shader
2
//!
3
//! This example demonstrates working in 3d. To make the example work in 2d,
4
//! replace 3d components with their 2d counterparts, and schedule the work
5
//! to run in the `Core2d` schedule as described in the `FullscreenMaterial`
6
//! comment in this file.
7
8
use bevy::{
9
core_pipeline::fullscreen_material::{FullscreenMaterial, FullscreenMaterialPlugin},
10
prelude::*,
11
render::{extract_component::ExtractComponent, render_resource::ShaderType},
12
shader::ShaderRef,
13
};
14
15
fn main() {
16
App::new()
17
.add_plugins((
18
DefaultPlugins,
19
FullscreenMaterialPlugin::<FullscreenEffect>::default(),
20
))
21
.add_systems(Startup, setup)
22
.run();
23
}
24
25
fn setup(
26
mut commands: Commands,
27
mut meshes: ResMut<Assets<Mesh>>,
28
mut materials: ResMut<Assets<StandardMaterial>>,
29
) {
30
commands.spawn((
31
Camera3d::default(),
32
Transform::from_translation(Vec3::new(0.0, 0.0, 5.0)).looking_at(Vec3::default(), Vec3::Y),
33
FullscreenEffect { intensity: 0.005 },
34
));
35
36
commands.spawn((
37
Mesh3d(meshes.add(Cuboid::default())),
38
MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))),
39
Transform::default(),
40
));
41
42
commands.spawn(DirectionalLight {
43
illuminance: 1_000.,
44
..default()
45
});
46
}
47
48
#[derive(Component, ExtractComponent, Clone, Copy, ShaderType, Default)]
49
struct FullscreenEffect {
50
intensity: f32,
51
}
52
53
impl FullscreenMaterial for FullscreenEffect {
54
fn fragment_shader() -> ShaderRef {
55
"shaders/fullscreen_effect.wgsl".into()
56
}
57
58
// The `FullscreenMaterial` uses 3d schedules by default.
59
// To make this work in 2d, you would need to schedule to
60
// run in `Core2d` and in a `Core2dSystems` set.
61
//
62
// fn schedule() -> impl bevy::ecs::schedule::ScheduleLabel + Clone {
63
// bevy::core_pipeline::Core2d
64
// }
65
// fn run_in() -> impl SystemSet {
66
// bevy::core_pipeline::Core2dSystems::PostProcess
67
// }
68
}
69
70