Created
February 27, 2023 07:34
-
-
Save mxgrey/8ef12f7eaf5886aa6c7e9851b4ed2bbf to your computer and use it in GitHub Desktop.
Example of a flat mesh being outline by bevy_mod_outline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::f32::consts::PI; | |
use bevy::{ | |
prelude::*, | |
render::mesh::{PrimitiveTopology, Indices}, | |
window::close_on_esc, | |
}; | |
use bevy_mod_outline::*; | |
#[bevy_main] | |
fn main() { | |
App::new() | |
.insert_resource(Msaa { samples: 4 }) | |
.insert_resource(ClearColor(Color::BLACK)) | |
.add_plugins(DefaultPlugins) | |
.add_plugin(OutlinePlugin) | |
.add_startup_system(setup) | |
.add_system(oscillate) | |
.add_system(close_on_esc) | |
.run(); | |
} | |
fn setup( | |
mut commands: Commands, | |
mut meshes: ResMut<Assets<Mesh>>, | |
mut materials: ResMut<Assets<StandardMaterial>>, | |
) { | |
let mut flat_mesh = Mesh::new(PrimitiveTopology::TriangleList); | |
let positions: Vec<_> = [ | |
[-1.0, 0.0, 0.0], | |
[-1.0, 1.0, 0.0], | |
[1.0, 1.0, 0.0], | |
[2.5, 0.0, 0.0], | |
].into_iter().collect(); | |
flat_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions); | |
let normals: Vec<_> = [[0.0, 0.0, 1.0]].into_iter().cycle().take(4).collect(); | |
flat_mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals); | |
let indices: Vec<_> = [0, 2, 1, 0, 3, 2].into_iter().collect(); | |
flat_mesh.set_indices(Some(Indices::U32(indices))); | |
commands | |
.spawn(PbrBundle { | |
mesh: meshes.add(flat_mesh), | |
material: materials.add(Color::CYAN.into()), | |
..default() | |
}) | |
.insert(OutlineBundle { | |
outline: OutlineVolume { | |
visible: true, | |
width: 10.0, | |
colour: Color::RED, | |
}, | |
..default() | |
}) | |
.insert(SetOutlineDepth::Flat { model_origin: Vec3::ZERO }); | |
commands | |
.spawn(PointLightBundle { | |
point_light: PointLight { | |
intensity: 1500.0, | |
..default() | |
}, | |
transform: Transform::from_xyz(0.0, 0.0, 3.0), | |
..default() | |
}); | |
commands | |
.spawn(SpatialBundle::default()) | |
.insert(Oscillate { | |
lower: -PI * 30.0 / 180.0, | |
upper: PI * 30.0 / 180.0, | |
rate: 3.0 / (2.0 * PI), | |
axis: Vec3::X, | |
}) | |
.with_children(|p| { | |
p.spawn(Camera3dBundle { | |
transform: Transform::from_xyz(0.0, -1.0, 4.0).looking_at(Vec3::ZERO, Vec3::Z), | |
..default() | |
}); | |
}); | |
} | |
#[derive(Component)] | |
struct Oscillate { | |
lower: f32, | |
upper: f32, | |
rate: f32, | |
axis: Vec3, | |
} | |
fn oscillate( | |
mut oscillating: Query<(&mut Transform, &Oscillate)>, | |
time: Res<Time>, | |
) { | |
for (mut tf, osc) in &mut oscillating { | |
let angle = (osc.upper - osc.lower) * (osc.rate * time.elapsed_seconds()).cos() + osc.lower; | |
tf.rotation = Quat::from_axis_angle(osc.axis, angle); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment