Add hollow example.

This commit is contained in:
Robin KAY 2023-03-21 18:35:10 +00:00
parent a55c60fd21
commit baf152fdbf
4 changed files with 139 additions and 2 deletions

View File

@ -30,6 +30,7 @@ bevy = { version = "0.10", default-features = false, features = [
"png", "png",
"x11", "x11",
] } ] }
bevy_mod_gltf_patched = "0.2"
[features] [features]
default = ["bevy_ui"] default = ["bevy_ui"]
@ -49,4 +50,8 @@ path = "examples/render_layers.rs"
[[example]] [[example]]
name = "animated_fox" name = "animated_fox"
path = "examples/animated_fox.rs" path = "examples/animated_fox.rs"
[[example]]
name = "hollow"
path = "examples/hollow.rs"

View File

@ -37,12 +37,18 @@ enabled.
cargo run --example render_layers cargo run --example render_layers
``` ```
An animated jointed model with an outline. An animated jointed glTF model with an outline.
```shell ```shell
cargo run --example animated_fox cargo run --example animated_fox
``` ```
A glTF model with pre-baked outline normals.
```shell
cargo run --example hollow
```
## Versions ## Versions
| This Version | Bevy version | | This Version | Bevy version |

BIN
assets/hollow.glb Normal file

Binary file not shown.

126
examples/hollow.rs Normal file
View File

@ -0,0 +1,126 @@
use std::f32::consts::{PI, TAU};
use bevy::{prelude::*, scene::SceneInstance, window::close_on_esc};
use bevy_mod_gltf_patched::GltfPlugin;
use bevy_mod_outline::*;
fn main() {
App::new()
// Disable built-in glTF plugin
.add_plugins(DefaultPlugins.build().disable::<bevy::gltf::GltfPlugin>())
// Register outline normal vertex attribute with bevy_mod_gltf_patched
.add_plugin(
GltfPlugin::default()
.add_custom_vertex_attribute("_OUTLINE_NORMAL", ATTRIBUTE_OUTLINE_NORMAL),
)
.add_plugin(OutlinePlugin)
.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 1.0,
})
.add_startup_system(setup)
.add_system(setup_scene_once_loaded)
.add_system(rotates)
.add_system(rotates_hue)
.add_system(close_on_esc)
.run();
}
#[derive(Component)]
struct Rotates;
#[derive(Component)]
struct RotatesHue;
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(20.0, 20.0, 30.0)
.looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
..default()
});
// Light
commands.spawn(DirectionalLightBundle {
transform: Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 1.0, -PI / 4.)),
directional_light: DirectionalLight {
shadows_enabled: true,
..default()
},
..default()
});
// Hollow
commands
.spawn(SceneBundle {
scene: asset_server.load("hollow.glb#Scene0"),
..default()
})
.insert(Rotates)
.insert(ComputedOutlineDepth::default());
}
// Once the scene is loaded, start the animation and add an outline
fn setup_scene_once_loaded(
mut commands: Commands,
scene_query: Query<&SceneInstance>,
scene_manager: Res<SceneSpawner>,
name_query: Query<&Name, With<Handle<StandardMaterial>>>,
mut done: Local<bool>,
) {
if !*done {
if let Ok(scene) = scene_query.get_single() {
if scene_manager.instance_is_ready(**scene) {
for entity in scene_manager.iter_instance_entities(**scene) {
commands
.entity(entity)
.insert(OutlineBundle {
outline: OutlineVolume {
visible: true,
width: 7.5,
colour: Color::BLUE,
},
stencil: OutlineStencil {
enabled: true,
offset: 0.0,
},
..default()
})
.insert(InheritOutlineDepth);
if let Ok(name) = name_query.get(entity) {
if name.as_str() == "inside" {
commands.entity(entity).insert(RotatesHue);
}
}
}
*done = true;
}
}
}
}
fn rotates(mut query: Query<&mut Transform, With<Rotates>>, timer: Res<Time>, mut t: Local<f32>) {
*t = (*t + timer.delta_seconds()) % TAU;
let a = t.sin();
for mut transform in query.iter_mut() {
*transform = Transform::from_rotation(Quat::from_rotation_y(a));
}
}
fn rotates_hue(
query: Query<&mut Handle<StandardMaterial>, With<RotatesHue>>,
mut materials: ResMut<Assets<StandardMaterial>>,
timer: Res<Time>,
) {
for handle in query.iter() {
let material = materials.get_mut(handle).unwrap();
let mut colour = material.base_color.as_hsla_f32();
colour[0] = (colour[0] + 15.0 * timer.delta_seconds()) % 360.0;
material.base_color = Color::Hsla {
hue: colour[0],
saturation: colour[1],
lightness: colour[2],
alpha: colour[3],
};
}
}