Port to Bevy 0.9.

This commit is contained in:
Robin KAY 2022-11-22 00:05:47 +00:00
parent 8afc8f9d14
commit 5ae478efe1
7 changed files with 34 additions and 30 deletions

View File

@ -11,7 +11,7 @@ keywords = ["gamedev", "bevy", "outline"]
categories = ["game-engines", "rendering"] categories = ["game-engines", "rendering"]
[dependencies] [dependencies]
bevy = { version = "0.8", default-features = false, features = [ bevy = { version = "0.9", default-features = false, features = [
"bevy_asset", "bevy_asset",
"bevy_render", "bevy_render",
"bevy_pbr", "bevy_pbr",
@ -21,7 +21,7 @@ bitfield = "0.14"
thiserror = "1.0" thiserror = "1.0"
[dev-dependencies] [dev-dependencies]
bevy = { version = "0.8", default-features = false, features = [ bevy = { version = "0.9", default-features = false, features = [
"bevy_winit", "bevy_winit",
"x11", "x11",
] } ] }

View File

@ -1,9 +1,10 @@
use std::f32::consts::PI; use std::f32::consts::PI;
use bevy::{prelude::*, window::close_on_esc}; use bevy::{prelude::*, window::close_on_esc};
use bevy_mod_outline::{ use bevy_mod_outline::{AutoGenerateOutlineNormalsPlugin, Outline, OutlineBundle, OutlinePlugin};
AutoGenerateOutlineNormalsPlugin, Outline, OutlineBundle, OutlinePlugin, OutlineStencil,
}; #[derive(Resource)]
struct Fox(Handle<AnimationClip>);
fn main() { fn main() {
App::new() App::new()
@ -27,24 +28,24 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
// Insert a resource with the current animation // Insert a resource with the current animation
commands.insert_resource::<Handle<AnimationClip>>(asset_server.load("Fox.glb#Animation0")); commands.insert_resource(Fox(asset_server.load("Fox.glb#Animation0")));
// Camera // Camera
commands.spawn_bundle(Camera3dBundle { commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(100.0, 100.0, 150.0) transform: Transform::from_xyz(100.0, 100.0, 150.0)
.looking_at(Vec3::new(0.0, 20.0, 0.0), Vec3::Y), .looking_at(Vec3::new(0.0, 20.0, 0.0), Vec3::Y),
..default() ..default()
}); });
// Plane // Plane
commands.spawn_bundle(PbrBundle { commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 500000.0 })), mesh: meshes.add(Mesh::from(shape::Plane { size: 500000.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default() ..default()
}); });
// Light // Light
commands.spawn_bundle(DirectionalLightBundle { commands.spawn(DirectionalLightBundle {
transform: Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 1.0, -PI / 4.)), transform: Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 1.0, -PI / 4.)),
directional_light: DirectionalLight { directional_light: DirectionalLight {
shadows_enabled: true, shadows_enabled: true,
@ -54,7 +55,7 @@ fn setup(
}); });
// Fox // Fox
commands.spawn_bundle(SceneBundle { commands.spawn(SceneBundle {
scene: asset_server.load("Fox.glb#Scene0"), scene: asset_server.load("Fox.glb#Scene0"),
..default() ..default()
}); });
@ -63,22 +64,21 @@ fn setup(
// Once the scene is loaded, start the animation and add an outline // Once the scene is loaded, start the animation and add an outline
fn setup_scene_once_loaded( fn setup_scene_once_loaded(
mut commands: Commands, mut commands: Commands,
animation: Res<Handle<AnimationClip>>, animation: Res<Fox>,
mut player: Query<&mut AnimationPlayer>, mut player: Query<&mut AnimationPlayer>,
entities: Query<Entity, With<Handle<Mesh>>>, entities: Query<Entity, With<Handle<Mesh>>>,
mut done: Local<bool>, mut done: Local<bool>,
) { ) {
if !*done { if !*done {
if let Ok(mut player) = player.get_single_mut() { if let Ok(mut player) = player.get_single_mut() {
player.play(animation.clone_weak()).repeat(); player.play(animation.0.clone_weak()).repeat();
for entity in entities.iter() { for entity in entities.iter() {
commands.entity(entity).insert_bundle(OutlineBundle { commands.entity(entity).insert(OutlineBundle {
outline: Outline { outline: Outline {
visible: true, visible: true,
width: 3.0, width: 3.0,
colour: Color::RED, colour: Color::RED,
}, },
stencil: OutlineStencil,
..default() ..default()
}); });
} }

View File

@ -33,7 +33,7 @@ fn setup(
) { ) {
// Add sphere with child meshes sticking out of it // Add sphere with child meshes sticking out of it
commands commands
.spawn_bundle(PbrBundle { .spawn(PbrBundle {
mesh: meshes.add( mesh: meshes.add(
UVSphere { UVSphere {
radius: 0.75, radius: 0.75,
@ -47,7 +47,7 @@ fn setup(
..default() ..default()
}) })
.insert_bundle(OutlineBundle { .insert(OutlineBundle {
outline: Outline { outline: Outline {
visible: true, visible: true,
colour: Color::WHITE, colour: Color::WHITE,
@ -59,7 +59,7 @@ fn setup(
.insert(Rotates) .insert(Rotates)
.with_children(|parent| { .with_children(|parent| {
parent parent
.spawn_bundle(PbrBundle { .spawn(PbrBundle {
mesh: meshes.add( mesh: meshes.add(
Capsule { Capsule {
radius: 0.2, radius: 0.2,
@ -76,7 +76,7 @@ fn setup(
.with_translation(Vec3::new(0.0, 0.0, 0.75)), .with_translation(Vec3::new(0.0, 0.0, 0.75)),
..default() ..default()
}) })
.insert_bundle(OutlineBundle { .insert(OutlineBundle {
outline: Outline { outline: Outline {
visible: true, visible: true,
colour: Color::WHITE, colour: Color::WHITE,
@ -87,7 +87,7 @@ fn setup(
}) })
.insert(InheritOutlineDepth); .insert(InheritOutlineDepth);
parent parent
.spawn_bundle(PbrBundle { .spawn(PbrBundle {
mesh: meshes.add( mesh: meshes.add(
Torus { Torus {
radius: 0.5, radius: 0.5,
@ -102,7 +102,7 @@ fn setup(
.with_translation(Vec3::new(0.0, 0.0, -0.75)), .with_translation(Vec3::new(0.0, 0.0, -0.75)),
..default() ..default()
}) })
.insert_bundle(OutlineBundle { .insert(OutlineBundle {
outline: Outline { outline: Outline {
visible: true, visible: true,
colour: Color::WHITE, colour: Color::WHITE,
@ -115,12 +115,12 @@ fn setup(
}); });
// Add plane, light source, and camera // Add plane, light source, and camera
commands.spawn_bundle(PbrBundle { commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(bevy::prelude::shape::Plane { size: 5.0 })), mesh: meshes.add(Mesh::from(bevy::prelude::shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default() ..default()
}); });
commands.spawn_bundle(PointLightBundle { commands.spawn(PointLightBundle {
point_light: PointLight { point_light: PointLight {
intensity: 1500.0, intensity: 1500.0,
shadows_enabled: true, shadows_enabled: true,
@ -129,7 +129,7 @@ fn setup(
transform: Transform::from_xyz(4.0, 8.0, 4.0), transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default() ..default()
}); });
commands.spawn_bundle(Camera3dBundle { commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default() ..default()
}); });

View File

@ -39,13 +39,13 @@ fn setup(
let mut cube_mesh = Mesh::from(Cube { size: 1.0 }); let mut cube_mesh = Mesh::from(Cube { size: 1.0 });
cube_mesh.generate_outline_normals().unwrap(); cube_mesh.generate_outline_normals().unwrap();
commands commands
.spawn_bundle(PbrBundle { .spawn(PbrBundle {
mesh: meshes.add(cube_mesh), mesh: meshes.add(cube_mesh),
material: materials.add(Color::rgb(0.1, 0.1, 0.9).into()), material: materials.add(Color::rgb(0.1, 0.1, 0.9).into()),
transform: Transform::from_xyz(0.0, 1.0, 0.0), transform: Transform::from_xyz(0.0, 1.0, 0.0),
..default() ..default()
}) })
.insert_bundle(OutlineBundle { .insert(OutlineBundle {
outline: Outline { outline: Outline {
visible: true, visible: true,
colour: Color::rgba(0.0, 1.0, 0.0, 1.0), colour: Color::rgba(0.0, 1.0, 0.0, 1.0),
@ -57,7 +57,7 @@ fn setup(
// Add torus using the regular surface normals for outlining // Add torus using the regular surface normals for outlining
commands commands
.spawn_bundle(PbrBundle { .spawn(PbrBundle {
mesh: meshes.add(Mesh::from(Torus { mesh: meshes.add(Mesh::from(Torus {
radius: 0.3, radius: 0.3,
ring_radius: 0.1, ring_radius: 0.1,
@ -69,7 +69,7 @@ fn setup(
.with_rotation(Quat::from_rotation_x(0.5 * PI)), .with_rotation(Quat::from_rotation_x(0.5 * PI)),
..default() ..default()
}) })
.insert_bundle(OutlineBundle { .insert(OutlineBundle {
outline: Outline { outline: Outline {
visible: true, visible: true,
colour: Color::rgba(1.0, 0.0, 1.0, 0.3), colour: Color::rgba(1.0, 0.0, 1.0, 0.3),
@ -80,12 +80,12 @@ fn setup(
.insert(Orbits); .insert(Orbits);
// Add plane, light source, and camera // Add plane, light source, and camera
commands.spawn_bundle(PbrBundle { commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(bevy::prelude::shape::Plane { size: 5.0 })), mesh: meshes.add(Mesh::from(bevy::prelude::shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default() ..default()
}); });
commands.spawn_bundle(PointLightBundle { commands.spawn(PointLightBundle {
point_light: PointLight { point_light: PointLight {
intensity: 1500.0, intensity: 1500.0,
shadows_enabled: true, shadows_enabled: true,
@ -94,7 +94,7 @@ fn setup(
transform: Transform::from_xyz(4.0, 8.0, 4.0), transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default() ..default()
}); });
commands.spawn_bundle(Camera3dBundle { commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default() ..default()
}); });

View File

@ -101,6 +101,7 @@ impl PipelineKey {
} }
} }
#[derive(Resource)]
pub struct OutlinePipeline { pub struct OutlinePipeline {
mesh_pipeline: MeshPipeline, mesh_pipeline: MeshPipeline,
pub outline_view_bind_group_layout: BindGroupLayout, pub outline_view_bind_group_layout: BindGroupLayout,

View File

@ -35,10 +35,12 @@ pub struct OutlineFragmentUniform {
pub colour: Vec4, pub colour: Vec4,
} }
#[derive(Resource)]
pub struct OutlineStencilBindGroup { pub struct OutlineStencilBindGroup {
pub bind_group: BindGroup, pub bind_group: BindGroup,
} }
#[derive(Resource)]
pub struct OutlineVolumeBindGroup { pub struct OutlineVolumeBindGroup {
pub bind_group: BindGroup, pub bind_group: BindGroup,
} }

View File

@ -19,6 +19,7 @@ pub struct OutlineViewUniform {
scale: Vec2, scale: Vec2,
} }
#[derive(Resource)]
pub struct OutlineViewBindGroup { pub struct OutlineViewBindGroup {
bind_group: BindGroup, bind_group: BindGroup,
} }