2022-01-19 04:08:47 +00:00
|
|
|
use std::f32::consts::TAU;
|
|
|
|
|
2022-01-12 08:18:13 +00:00
|
|
|
use bevy::prelude::*;
|
2022-01-19 04:08:47 +00:00
|
|
|
use rand::prelude::*;
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-03-13 00:06:36 +00:00
|
|
|
use crate::planet::PLANET_RADIUS;
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-01-29 21:31:15 +00:00
|
|
|
pub const LIGHT_RANGE: f32 = 90.0;
|
2022-01-12 08:18:13 +00:00
|
|
|
|
|
|
|
#[derive(Component)]
|
2022-01-19 04:08:47 +00:00
|
|
|
struct AnimatedCyberLight {
|
2023-02-22 21:46:43 +00:00
|
|
|
_axis: Vec3,
|
|
|
|
_rate: f32,
|
2022-01-19 04:08:47 +00:00
|
|
|
}
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-03-13 22:54:33 +00:00
|
|
|
#[derive(Component, Default)]
|
|
|
|
pub(crate) struct AnimateCyberLightWireframe {
|
|
|
|
pub wired: bool,
|
|
|
|
}
|
2022-01-19 06:20:58 +00:00
|
|
|
|
2023-02-22 21:46:43 +00:00
|
|
|
fn _spawn_moving_lights(
|
2022-01-12 08:18:13 +00:00
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
) {
|
2022-01-19 04:08:47 +00:00
|
|
|
let rng = &mut thread_rng();
|
2022-01-26 21:33:48 +00:00
|
|
|
// spawn orbiting bisexual lights
|
2022-02-18 06:41:13 +00:00
|
|
|
for _ in 0..655 {
|
2022-01-19 23:07:24 +00:00
|
|
|
// mechanics
|
2023-02-22 21:46:43 +00:00
|
|
|
let _axis = crate::random_unit_vec(rng);
|
2022-01-19 04:08:47 +00:00
|
|
|
let angle = rng.gen_range(0.0..TAU);
|
2023-02-22 21:46:43 +00:00
|
|
|
let _rate: f32 = rng.gen_range(7.0..10.0);
|
|
|
|
let _rate = _rate.to_radians();
|
|
|
|
let rotation = Quat::from_axis_angle(_axis, angle);
|
2022-01-19 04:08:47 +00:00
|
|
|
let altitude = PLANET_RADIUS + rng.gen_range(8.0..20.0);
|
2023-02-22 21:46:43 +00:00
|
|
|
let perp = _axis.any_orthonormal_vector();
|
2022-01-19 04:08:47 +00:00
|
|
|
let translation = perp * altitude;
|
|
|
|
let transform = Transform::from_translation(translation);
|
|
|
|
|
2022-01-19 23:07:24 +00:00
|
|
|
// optics
|
|
|
|
let hue = rng.gen_range(240.0..300.0);
|
|
|
|
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);
|
2022-01-29 21:31:15 +00:00
|
|
|
let intensity = rng.gen_range(900.0..1300.0);
|
2022-01-19 23:07:24 +00:00
|
|
|
let radius = rng.gen::<f32>() * 2.2; // why can't this infer the gen type?
|
2022-01-19 04:08:47 +00:00
|
|
|
let point_light = PointLight {
|
|
|
|
intensity,
|
|
|
|
range: LIGHT_RANGE,
|
|
|
|
color,
|
2022-01-19 23:07:24 +00:00
|
|
|
radius: radius - 0.1,
|
2022-01-19 04:08:47 +00:00
|
|
|
shadows_enabled: true,
|
2022-01-12 08:18:13 +00:00
|
|
|
..Default::default()
|
2022-01-19 04:08:47 +00:00
|
|
|
};
|
|
|
|
|
2023-01-21 01:21:12 +00:00
|
|
|
let sbundle = SpatialBundle {
|
|
|
|
transform: Transform::from_rotation(rotation),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-01-19 04:08:47 +00:00
|
|
|
commands
|
2022-01-19 23:07:24 +00:00
|
|
|
// first, spawn an entity with a transform we can rotate
|
2023-02-22 21:46:43 +00:00
|
|
|
.spawn((AnimatedCyberLight { _axis, _rate },))
|
2023-01-21 01:21:12 +00:00
|
|
|
.insert(sbundle)
|
2022-01-19 04:08:47 +00:00
|
|
|
.with_children(|parent| {
|
|
|
|
parent
|
2022-01-19 23:07:24 +00:00
|
|
|
// now spawn a child entity with a pointlight, and a relative transform that's
|
|
|
|
// just translation from the parent
|
2023-01-20 22:40:51 +00:00
|
|
|
.spawn(PointLightBundle {
|
2022-01-19 04:08:47 +00:00
|
|
|
transform,
|
|
|
|
point_light,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_children(|builder| {
|
2022-01-19 06:20:58 +00:00
|
|
|
builder
|
2022-01-19 23:07:24 +00:00
|
|
|
// now a simple mesh to show a wireframe.
|
2023-01-20 22:40:51 +00:00
|
|
|
.spawn(PbrBundle {
|
2022-01-19 06:20:58 +00:00
|
|
|
mesh: meshes.add(Mesh::from(shape::Icosphere {
|
|
|
|
radius,
|
|
|
|
subdivisions: 1,
|
|
|
|
})),
|
|
|
|
material: materials.add(StandardMaterial {
|
2022-01-19 23:07:24 +00:00
|
|
|
base_color: Color::hsla(272.0, 0.7, 0.56, 0.7),
|
2022-01-19 06:20:58 +00:00
|
|
|
emissive: color,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2022-01-19 04:08:47 +00:00
|
|
|
..Default::default()
|
2022-01-19 06:20:58 +00:00
|
|
|
})
|
2022-03-13 22:54:33 +00:00
|
|
|
.insert(AnimateCyberLightWireframe::default());
|
2022-01-19 04:08:47 +00:00
|
|
|
}); // mesh child
|
|
|
|
}); // light child
|
|
|
|
}
|
2022-01-19 23:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn_static_lights(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
) {
|
|
|
|
let pink_light = PointLight {
|
|
|
|
intensity: 1_00.0,
|
|
|
|
range: LIGHT_RANGE,
|
|
|
|
color: Color::PINK,
|
|
|
|
radius: 1.0,
|
|
|
|
shadows_enabled: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let blue_light = PointLight {
|
|
|
|
intensity: 1_000.0,
|
|
|
|
range: LIGHT_RANGE,
|
|
|
|
color: Color::BLUE,
|
|
|
|
radius: 1.0,
|
|
|
|
shadows_enabled: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.insert_resource(AmbientLight {
|
|
|
|
color: Color::WHITE,
|
2022-02-21 21:08:49 +00:00
|
|
|
brightness: 0.2,
|
2022-01-19 23:07:24 +00:00
|
|
|
});
|
2022-01-19 04:08:47 +00:00
|
|
|
|
2022-01-12 08:18:13 +00:00
|
|
|
// up light
|
|
|
|
commands
|
2023-01-20 22:40:51 +00:00
|
|
|
.spawn(PointLightBundle {
|
2023-02-22 21:46:43 +00:00
|
|
|
transform: Transform::from_xyz(20.0, 100.0, 20.0),
|
2022-01-19 04:08:47 +00:00
|
|
|
point_light: pink_light,
|
2022-01-12 08:18:13 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_children(|builder| {
|
2023-01-20 22:40:51 +00:00
|
|
|
builder.spawn(PbrBundle {
|
2022-01-12 08:18:13 +00:00
|
|
|
mesh: meshes.add(Mesh::from(shape::Icosphere {
|
|
|
|
radius: 10.0,
|
|
|
|
subdivisions: 2,
|
|
|
|
})),
|
|
|
|
material: materials.add(StandardMaterial {
|
2022-01-19 23:07:24 +00:00
|
|
|
base_color: Color::BLUE,
|
|
|
|
emissive: Color::PINK,
|
2022-01-12 08:18:13 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// down light
|
|
|
|
commands
|
2023-01-20 22:40:51 +00:00
|
|
|
.spawn(PointLightBundle {
|
2023-02-22 21:46:43 +00:00
|
|
|
transform: Transform::from_xyz(-20.0, 100.0, -20.0),
|
2022-01-12 08:18:13 +00:00
|
|
|
point_light: blue_light,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_children(|builder| {
|
2023-01-20 22:40:51 +00:00
|
|
|
builder.spawn(PbrBundle {
|
2022-01-12 08:18:13 +00:00
|
|
|
mesh: meshes.add(Mesh::from(shape::Icosphere {
|
|
|
|
radius: 10.0,
|
|
|
|
subdivisions: 2,
|
|
|
|
})),
|
|
|
|
material: materials.add(StandardMaterial {
|
2022-01-19 23:07:24 +00:00
|
|
|
base_color: Color::PINK,
|
|
|
|
emissive: Color::BLUE,
|
2022-01-12 08:18:13 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-22 21:46:43 +00:00
|
|
|
fn _orbit_lights(time: Res<Time>, mut query: Query<(&mut Transform, &AnimatedCyberLight)>) {
|
2022-01-12 08:18:13 +00:00
|
|
|
let dt = time.delta_seconds();
|
2022-01-19 04:08:47 +00:00
|
|
|
for (mut transform, light) in query.iter_mut() {
|
2023-02-22 21:46:43 +00:00
|
|
|
let AnimatedCyberLight { _axis, _rate } = *light;
|
|
|
|
let theta = _rate * dt;
|
|
|
|
transform.rotation *= Quat::from_axis_angle(_axis, theta);
|
2022-01-12 08:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-14 00:14:08 +00:00
|
|
|
|
|
|
|
pub struct CyberSpaceLightsPlugin;
|
|
|
|
impl Plugin for CyberSpaceLightsPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
2023-02-22 21:46:43 +00:00
|
|
|
app.add_startup_system(spawn_static_lights);
|
|
|
|
//.add_startup_system(spawn_moving_lights)
|
|
|
|
//.add_system(orbit_lights);
|
2022-01-14 00:14:08 +00:00
|
|
|
}
|
|
|
|
}
|