Last active
March 6, 2023 15:11
-
-
Save mxgrey/c126b8f4be45c93d49d02f1bf744f2cb to your computer and use it in GitHub Desktop.
Strange bug where outlines diverge from their object
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 bevy::{ | |
prelude::*, | |
render::mesh::shape::{Box, UVSphere}, | |
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 mesh = Mesh::from(Box::new(10.0, 0.1, 2.5)); | |
mesh.generate_outline_normals().unwrap(); | |
let model_origin = Vec3::new(0.0, 0.0, 0.0); | |
commands | |
.spawn(PbrBundle { | |
mesh: meshes.add(mesh), | |
material: materials.add(Color::CYAN.into()), | |
..default() | |
}) | |
.insert(OutlineBundle { | |
outline: OutlineVolume { | |
visible: true, | |
width: 5.0, | |
colour: Color::RED, | |
}, | |
..default() | |
}).insert(SetOutlineDepth::Flat { model_origin }); | |
// Put a sphere at the model origin as a reference point | |
commands | |
.spawn(PbrBundle { | |
mesh: meshes.add(UVSphere { | |
radius: 0.1, | |
sectors: 36, | |
stacks: 18, | |
}.into()), | |
material: materials.add(Color::YELLOW.into()), | |
transform: Transform::from_translation(model_origin), | |
..default() | |
}); | |
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: -5.0, | |
upper: 0.0, | |
rate: 0.5, | |
}) | |
.with_children(|p| { | |
p.spawn(Camera3dBundle { | |
transform: Transform::from_xyz(-0.1, 1.0, 3.0).looking_at(Vec3::new(10.0, -1.0, 2.5), Vec3::Z), | |
..default() | |
}); | |
}); | |
} | |
#[derive(Component)] | |
struct Oscillate { | |
lower: f32, | |
upper: f32, | |
rate: f32, | |
} | |
fn oscillate( | |
mut oscillating: Query<(&mut Transform, &Oscillate)>, | |
time: Res<Time>, | |
) { | |
for (mut tf, osc) in &mut oscillating { | |
let x = (osc.upper - osc.lower) * (osc.rate * time.elapsed_seconds()).cos() + osc.lower; | |
tf.translation.x = x; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment