forked from Game4all/bevy_vox_mesh
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathssao-model.rs
98 lines (90 loc) · 2.85 KB
/
ssao-model.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use bevy::{
core_pipeline::{
bloom::Bloom,
experimental::taa::{TemporalAntiAliasPlugin, TemporalAntiAliasing},
},
input::keyboard::KeyboardInput,
pbr::ScreenSpaceAmbientOcclusion,
prelude::*,
};
use bevy_vox_scene::VoxScenePlugin;
use utilities::{PanOrbitCamera, PanOrbitCameraPlugin};
/// Press any key to toggle Screen Space Ambient Occlusion
fn main() {
let mut app = App::new();
app.add_plugins((
DefaultPlugins,
PanOrbitCameraPlugin,
VoxScenePlugin::default(),
))
.insert_resource(AmbientLight {
color: Color::srgb_u8(128, 126, 124),
brightness: 0.5,
})
.add_systems(Startup, setup)
.add_systems(Update, toggle_ssao.run_if(on_event::<KeyboardInput>));
// *Note:* TAA is not _required_ for SSAO, but
// it enhances the look of the resulting blur effects.
// Sadly, it's not available under WebGL.
#[cfg(not(all(feature = "webgl2", target_arch = "wasm32")))]
app.add_plugins(TemporalAntiAliasPlugin);
app.run();
}
#[derive(Component)]
#[require(ScreenSpaceAmbientOcclusion)]
struct SSAOVisible(bool);
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
Camera3d::default(),
Camera {
hdr: true,
..default()
},
Transform::from_xyz(20.0, 10.0, 40.0).looking_at(Vec3::ZERO, Vec3::Y),
PanOrbitCamera::default(),
Bloom {
intensity: 0.3,
..default()
},
#[cfg(not(all(feature = "webgl2", target_arch = "wasm32")))]
Msaa::Off,
#[cfg(not(all(feature = "webgl2", target_arch = "wasm32")))]
TemporalAntiAliasing::default(),
EnvironmentMapLight {
diffuse_map: asset_server.load("pisa_diffuse.ktx2"),
specular_map: asset_server.load("pisa_specular.ktx2"),
intensity: 500.0,
..default()
},
ScreenSpaceAmbientOcclusion::default(),
SSAOVisible(true),
));
commands.spawn(
// Load a model nested inside a group by using a `/` to separate the path components
SceneRoot(asset_server.load("study.vox#tank/goldfish")),
);
}
fn toggle_ssao(
mut commands: Commands,
keys: Res<ButtonInput<KeyCode>>,
mut query: Query<(Entity, &mut SSAOVisible)>,
) {
let Ok((entity, mut ssao_visible)) = query.get_single_mut() else {
return;
};
if keys.get_just_pressed().next().is_some() {
ssao_visible.0 = !ssao_visible.0;
match ssao_visible.0 {
true => {
commands
.entity(entity)
.insert(ScreenSpaceAmbientOcclusion::default());
}
false => {
commands
.entity(entity)
.remove::<ScreenSpaceAmbientOcclusion>();
}
}
}
}