2022-10-12 17:31:02 +00:00
|
|
|
use std::f32::consts::PI;
|
|
|
|
|
2024-02-16 21:51:21 +00:00
|
|
|
use bevy::{prelude::*, scene::SceneInstance, window::close_on_esc};
|
2022-11-22 19:11:36 +00:00
|
|
|
use bevy_mod_outline::{
|
2023-11-08 21:40:19 +00:00
|
|
|
AutoGenerateOutlineNormalsPlugin, InheritOutlineBundle, OutlineBundle, OutlinePlugin,
|
|
|
|
OutlineVolume,
|
2022-11-22 19:11:36 +00:00
|
|
|
};
|
2022-11-22 00:05:47 +00:00
|
|
|
|
|
|
|
#[derive(Resource)]
|
|
|
|
struct Fox(Handle<AnimationClip>);
|
2022-10-12 17:31:02 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
2023-08-14 00:51:43 +00:00
|
|
|
.add_plugins((
|
|
|
|
DefaultPlugins,
|
|
|
|
OutlinePlugin,
|
|
|
|
AutoGenerateOutlineNormalsPlugin,
|
|
|
|
))
|
2024-02-16 21:51:21 +00:00
|
|
|
.insert_resource(AmbientLight::default())
|
2023-08-14 00:51:43 +00:00
|
|
|
.add_systems(Startup, setup)
|
|
|
|
.add_systems(Update, (setup_scene_once_loaded, close_on_esc))
|
2022-10-12 17:31:02 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(
|
|
|
|
mut commands: Commands,
|
|
|
|
asset_server: Res<AssetServer>,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
) {
|
|
|
|
// Insert a resource with the current animation
|
2022-11-22 00:05:47 +00:00
|
|
|
commands.insert_resource(Fox(asset_server.load("Fox.glb#Animation0")));
|
2022-10-12 17:31:02 +00:00
|
|
|
|
|
|
|
// Camera
|
2022-11-22 00:05:47 +00:00
|
|
|
commands.spawn(Camera3dBundle {
|
2022-10-12 17:31:02 +00:00
|
|
|
transform: Transform::from_xyz(100.0, 100.0, 150.0)
|
|
|
|
.looking_at(Vec3::new(0.0, 20.0, 0.0), Vec3::Y),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
|
|
|
|
// Plane
|
2022-11-22 00:05:47 +00:00
|
|
|
commands.spawn(PbrBundle {
|
2024-02-16 21:51:21 +00:00
|
|
|
mesh: meshes.add(
|
|
|
|
Plane3d::new(Vec3::Y)
|
|
|
|
.mesh()
|
|
|
|
.size(500000.0, 500000.0)
|
|
|
|
.build(),
|
|
|
|
),
|
2024-02-16 00:30:51 +00:00
|
|
|
material: materials.add(StandardMaterial::from(Color::rgb(0.3, 0.5, 0.3))),
|
2022-10-12 17:31:02 +00:00
|
|
|
..default()
|
|
|
|
});
|
|
|
|
|
|
|
|
// Light
|
2022-11-22 00:05:47 +00:00
|
|
|
commands.spawn(DirectionalLightBundle {
|
2022-10-12 17:31:02 +00:00
|
|
|
transform: Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 1.0, -PI / 4.)),
|
|
|
|
directional_light: DirectionalLight {
|
|
|
|
shadows_enabled: true,
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
|
|
|
|
// Fox
|
2023-11-08 21:40:19 +00:00
|
|
|
commands
|
|
|
|
.spawn(SceneBundle {
|
|
|
|
scene: asset_server.load("Fox.glb#Scene0"),
|
|
|
|
..default()
|
|
|
|
})
|
|
|
|
.insert(OutlineBundle {
|
|
|
|
outline: OutlineVolume {
|
|
|
|
visible: true,
|
|
|
|
width: 3.0,
|
|
|
|
colour: Color::RED,
|
|
|
|
},
|
|
|
|
..default()
|
|
|
|
});
|
2022-10-12 17:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Once the scene is loaded, start the animation and add an outline
|
|
|
|
fn setup_scene_once_loaded(
|
|
|
|
mut commands: Commands,
|
2023-03-07 23:46:58 +00:00
|
|
|
scene_query: Query<&SceneInstance>,
|
|
|
|
scene_manager: Res<SceneSpawner>,
|
|
|
|
mut player_query: Query<&mut AnimationPlayer>,
|
2022-11-22 00:05:47 +00:00
|
|
|
animation: Res<Fox>,
|
2022-10-12 17:31:02 +00:00
|
|
|
mut done: Local<bool>,
|
|
|
|
) {
|
|
|
|
if !*done {
|
2023-03-07 23:46:58 +00:00
|
|
|
if let (Ok(scene), Ok(mut player)) =
|
|
|
|
(scene_query.get_single(), player_query.get_single_mut())
|
|
|
|
{
|
2023-03-17 01:00:38 +00:00
|
|
|
if scene_manager.instance_is_ready(**scene) {
|
|
|
|
for entity in scene_manager.iter_instance_entities(**scene) {
|
2023-11-08 21:40:19 +00:00
|
|
|
commands
|
|
|
|
.entity(entity)
|
|
|
|
.insert(InheritOutlineBundle::default());
|
2023-03-17 01:00:38 +00:00
|
|
|
}
|
|
|
|
player.play(animation.0.clone_weak()).repeat();
|
|
|
|
*done = true;
|
2022-10-12 17:31:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|