-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.rs
executable file
·179 lines (158 loc) · 5.48 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use bevy::{
gltf::Gltf,
log,
pbr::{
CascadeShadowConfigBuilder,
DirectionalLightShadowMap,
// At some stage in the future I'll want to use these!
// ExtendedMaterial, MaterialExtension,
// OpaqueRendererMethod,
},
prelude::*,
render::render_resource::{AsBindGroup, ShaderRef},
};
use bevy_panorbit_camera::PanOrbitCamera;
use std::f32::consts::*;
/// marker component for our knight
#[derive(Resource, PartialEq, Eq)]
struct Knight {
handle: Handle<Gltf>,
}
/// marker resource to track whether or not we can spawn the loaded knight asset
#[derive(Resource, PartialEq, Eq)]
struct WasLoaded(bool);
fn main() {
// Check the knight model is available and if not, show where it can be got.
// The environment maps used, are automatically downloadable so the shadplay/build.rs fetches them for you.
let knight_model_path = std::path::Path::new("../assets/scenes/knight.glb");
if !knight_model_path.exists() {
dbg!("knight.glb does not exist at {:?}.\nThere's a free non-rigged version available of it here: https://sketchfab.com/3d-models/elysia-knight-d099f11914f445afbe727fe5c3ddd39d or,\nYou can a rigged version purchase here: https://www.artstation.com/marketplace/p/RGmbB/medieval-armor-set-unreal-engine-rigged", knight_model_path);
}
App::new()
.insert_resource(DirectionalLightShadowMap { size: 4096 })
.add_plugins((DefaultPlugins, bevy_panorbit_camera::PanOrbitCameraPlugin))
.add_systems(Startup, setup)
.add_systems(Update, (animate_light_direction, quit_listener))
// Our Systems:
.add_systems(Startup, load_knight)
.add_systems(
Update,
spawn_knight
.after(load_knight)
.run_if(resource_exists::<Knight>)
.run_if(resource_exists_and_equals::<WasLoaded>(WasLoaded(false))),
)
// Our Materials
.add_plugins(MaterialPlugin::<AuraMaterial>::default())
.run();
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
Camera3d::default(),
Transform::from_xyz(2.0, 6.0, 6.0).looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
PanOrbitCamera::default(),
EnvironmentMapLight {
diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
intensity: 1.0,
..Default::default()
},
));
commands.spawn((
DirectionalLight {
shadows_enabled: true,
..default()
},
CascadeShadowConfigBuilder {
num_cascades: 1,
maximum_distance: 1.6,
..default()
}
.build(),
));
// ground plane
commands.spawn((
Mesh3d(meshes.add(Plane3d::default().mesh().size(50.0, 50.0))),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::BLACK,
perceptual_roughness: 1.0,
double_sided: false,
unlit: true,
..default()
})),
));
}
// Loads our knight into the asset server, it isn't spawned.
fn load_knight(mut commands: Commands, asset_server: Res<AssetServer>) {
let hndl = asset_server.load("scenes/knight.glb");
commands.insert_resource(Knight { handle: hndl });
commands.insert_resource(WasLoaded(false));
log::info!("load_knight done.");
}
// Spawns the knight model in.
fn spawn_knight(
mut commands: Commands,
knight: Res<Knight>,
assets_gltf: Res<Assets<Gltf>>,
mut meshes: ResMut<Assets<Mesh>>,
mut was_loaded: ResMut<WasLoaded>,
// mut materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, AuraMaterial>>>,
mut materials: ResMut<Assets<AuraMaterial>>,
) {
if let Some(gltf) = assets_gltf.get(&knight.handle) {
log::info!("Spawning scene...");
let disc = Mesh::from(Cylinder::new(1.2, 0.001));
let as_custom_mat = AuraMaterial { inner: 0.0 };
commands
.spawn(SceneRoot(gltf.scenes[0].clone()))
.with_children(|parent| {
parent.spawn((
Mesh3d(meshes.add(disc)),
MeshMaterial3d(materials.add(as_custom_mat)),
));
});
was_loaded.0 = true;
log::info!("Spawn complete...");
}
}
// from the bevy load_gltf example
fn animate_light_direction(
time: Res<Time>,
mut query: Query<&mut Transform, With<DirectionalLight>>,
) {
for mut transform in &mut query {
transform.rotation = Quat::from_euler(
EulerRot::ZYX,
0.0,
time.elapsed_secs() * PI / 5.0,
-FRAC_PI_4,
);
}
}
/// Our Aura shader:
#[derive(Asset, AsBindGroup, TypePath, Debug, Clone)]
pub struct AuraMaterial {
/// This is currently unused but reserving for future use :wink
#[uniform(100)]
inner: f32,
}
impl Material for AuraMaterial {
fn fragment_shader() -> ShaderRef {
"shaders/aura.wgsl".into()
}
// Available in StandardMaterial
fn alpha_mode(&self) -> AlphaMode {
AlphaMode::Blend
}
}
/// System: listening for `q` or `esc` to quit.
fn quit_listener(input: Res<ButtonInput<KeyCode>>) {
if input.just_pressed(KeyCode::KeyQ) || input.just_pressed(KeyCode::Escape) {
std::process::exit(0)
}
}