From 13543a65539f8078a3adeb869ab40b077e01f433 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 18 Jan 2022 22:20:58 -0800 Subject: [PATCH] Blink moving lights' meshes on and off. --- src/glamor.rs | 28 +++++++++++++++++++++++++--- src/lights.rs | 32 ++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/glamor.rs b/src/glamor.rs index a25d301..38d35ad 100644 --- a/src/glamor.rs +++ b/src/glamor.rs @@ -3,10 +3,11 @@ use bevy::{ prelude::*, 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 wireframe_config: ResMut, query: Query>, @@ -16,6 +17,26 @@ fn wireframe( commands.entity(ent).insert(Wireframe); } +fn wireframify_lights( + mut commands: Commands, + no_wires: Query, Without)>, + wires: Query, With)>, +) { + let chance = 0.005; + + let rng = &mut thread_rng(); + for e in no_wires.iter() { + if rng.gen::() < chance { + commands.entity(e).insert(Wireframe); + } + } + for e in wires.iter() { + if rng.gen::() < chance { + commands.entity(e).remove::(); + } + } +} + // public plugin pub struct CyberGlamorPlugin; impl Plugin for CyberGlamorPlugin { @@ -24,7 +45,8 @@ impl Plugin for CyberGlamorPlugin { features: WgpuFeatures::POLYGON_MODE_LINE, ..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); } } diff --git a/src/lights.rs b/src/lights.rs index 4f44f24..5cbf9b1 100644 --- a/src/lights.rs +++ b/src/lights.rs @@ -13,6 +13,9 @@ struct AnimatedCyberLight { rate: f32, } +#[derive(Component)] +pub(crate) struct AnimateCyberLightWireframe; + fn spawn_lights( mut commands: Commands, mut meshes: ResMut>, @@ -45,8 +48,8 @@ fn spawn_lights( // spawn 200 orbiting bisexual lights for _ in 0..255 { let hue = rng.gen_range(260.0..320.0); - let saturation = rng.gen_range(0.8..0.95); - let lightness = rng.gen_range(0.4..0.7); + let saturation = rng.gen_range(0.85..0.99); + let lightness = rng.gen_range(0.3..0.7); let color = Color::hsl(hue, saturation, lightness); let axis = crate::random_sphere_vec(rng); @@ -85,19 +88,20 @@ fn spawn_lights( ..Default::default() }) .with_children(|builder| { - builder.spawn_bundle(PbrBundle { - mesh: meshes.add(Mesh::from(shape::Icosphere { - radius, - subdivisions: 2, - })), - - material: materials.add(StandardMaterial { - base_color: color, - emissive: color, + builder + .spawn_bundle(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Icosphere { + radius, + subdivisions: 1, + })), + material: materials.add(StandardMaterial { + base_color: color, + emissive: color, + ..Default::default() + }), ..Default::default() - }), - ..Default::default() - }); + }) + .insert(AnimateCyberLightWireframe); }); // mesh child }); // light child }