Blink moving lights' meshes on and off.

This commit is contained in:
Joe Ardent 2022-01-18 22:20:58 -08:00
parent 718884a8da
commit 13543a6553
2 changed files with 43 additions and 17 deletions

View File

@ -3,10 +3,11 @@ use bevy::{
prelude::*, prelude::*,
render::{options::WgpuOptions, render_resource::WgpuFeatures}, render::{options::WgpuOptions, render_resource::WgpuFeatures},
}; };
use rand::{thread_rng, Rng};
use crate::geometry::CyberSphere; use crate::{geometry::CyberSphere, lights::AnimateCyberLightWireframe};
fn wireframe( fn wireframe_planet(
mut commands: Commands, mut commands: Commands,
mut wireframe_config: ResMut<WireframeConfig>, mut wireframe_config: ResMut<WireframeConfig>,
query: Query<Entity, With<CyberSphere>>, query: Query<Entity, With<CyberSphere>>,
@ -16,6 +17,26 @@ fn wireframe(
commands.entity(ent).insert(Wireframe); commands.entity(ent).insert(Wireframe);
} }
fn wireframify_lights(
mut commands: Commands,
no_wires: Query<Entity, (With<AnimateCyberLightWireframe>, Without<Wireframe>)>,
wires: Query<Entity, (With<AnimateCyberLightWireframe>, With<Wireframe>)>,
) {
let chance = 0.005;
let rng = &mut thread_rng();
for e in no_wires.iter() {
if rng.gen::<f32>() < chance {
commands.entity(e).insert(Wireframe);
}
}
for e in wires.iter() {
if rng.gen::<f32>() < chance {
commands.entity(e).remove::<Wireframe>();
}
}
}
// public plugin // public plugin
pub struct CyberGlamorPlugin; pub struct CyberGlamorPlugin;
impl Plugin for CyberGlamorPlugin { impl Plugin for CyberGlamorPlugin {
@ -24,7 +45,8 @@ impl Plugin for CyberGlamorPlugin {
features: WgpuFeatures::POLYGON_MODE_LINE, features: WgpuFeatures::POLYGON_MODE_LINE,
..Default::default() ..Default::default()
}) })
.add_startup_system_to_stage(StartupStage::PostStartup, wireframe) .add_startup_system_to_stage(StartupStage::PostStartup, wireframe_planet)
.add_system(wireframify_lights)
.add_plugin(WireframePlugin); .add_plugin(WireframePlugin);
} }
} }

View File

@ -13,6 +13,9 @@ struct AnimatedCyberLight {
rate: f32, rate: f32,
} }
#[derive(Component)]
pub(crate) struct AnimateCyberLightWireframe;
fn spawn_lights( fn spawn_lights(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
@ -45,8 +48,8 @@ fn spawn_lights(
// spawn 200 orbiting bisexual lights // spawn 200 orbiting bisexual lights
for _ in 0..255 { for _ in 0..255 {
let hue = rng.gen_range(260.0..320.0); let hue = rng.gen_range(260.0..320.0);
let saturation = rng.gen_range(0.8..0.95); let saturation = rng.gen_range(0.85..0.99);
let lightness = rng.gen_range(0.4..0.7); let lightness = rng.gen_range(0.3..0.7);
let color = Color::hsl(hue, saturation, lightness); let color = Color::hsl(hue, saturation, lightness);
let axis = crate::random_sphere_vec(rng); let axis = crate::random_sphere_vec(rng);
@ -85,19 +88,20 @@ fn spawn_lights(
..Default::default() ..Default::default()
}) })
.with_children(|builder| { .with_children(|builder| {
builder.spawn_bundle(PbrBundle { builder
mesh: meshes.add(Mesh::from(shape::Icosphere { .spawn_bundle(PbrBundle {
radius, mesh: meshes.add(Mesh::from(shape::Icosphere {
subdivisions: 2, radius,
})), subdivisions: 1,
})),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: color, base_color: color,
emissive: color, emissive: color,
..Default::default()
}),
..Default::default() ..Default::default()
}), })
..Default::default() .insert(AnimateCyberLightWireframe);
});
}); // mesh child }); // mesh child
}); // light child }); // light child
} }