cyber_rider/src/lights.rs

169 lines
5.3 KiB
Rust
Raw Normal View History

2022-01-19 04:08:47 +00:00
use std::f32::consts::TAU;
use bevy::prelude::*;
2022-01-19 04:08:47 +00:00
use rand::prelude::*;
use crate::geometry::PLANET_RADIUS;
2022-01-19 04:08:47 +00:00
pub const LIGHT_RANGE: f32 = 50.0;
#[derive(Component)]
2022-01-19 04:08:47 +00:00
struct AnimatedCyberLight {
axis: Vec3,
rate: f32,
}
#[derive(Component)]
pub(crate) struct AnimateCyberLightWireframe;
2022-01-14 00:14:08 +00:00
fn spawn_lights(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
2022-01-19 04:08:47 +00:00
let pink_light = PointLight {
intensity: 1_00.0,
range: LIGHT_RANGE,
2022-01-19 04:08:47 +00:00
color: Color::PINK,
radius: 1.0,
2022-01-19 04:08:47 +00:00
shadows_enabled: true,
..Default::default()
};
let blue_light = PointLight {
intensity: 1_000.0,
range: LIGHT_RANGE,
color: Color::BLUE,
radius: 1.0,
2022-01-19 04:08:47 +00:00
shadows_enabled: true,
..Default::default()
};
commands.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 0.32,
});
2022-01-19 04:08:47 +00:00
let rng = &mut thread_rng();
// spawn 200 orbiting bisexual lights
for _ in 0..255 {
let hue = rng.gen_range(260.0..320.0);
let saturation = rng.gen_range(0.85..0.99);
let lightness = rng.gen_range(0.3..0.7);
2022-01-19 04:08:47 +00:00
let color = Color::hsl(hue, saturation, lightness);
2022-01-19 04:08:47 +00:00
let axis = crate::random_sphere_vec(rng);
let angle = rng.gen_range(0.0..TAU);
let rate: f32 = rng.gen_range(7.0..10.0);
let rate = rate.to_radians();
let rotation = Quat::from_axis_angle(axis, angle);
let altitude = PLANET_RADIUS + rng.gen_range(8.0..20.0);
let perp = axis.any_orthonormal_vector();
let translation = perp * altitude;
let transform = Transform::from_translation(translation);
let intensity = rng.gen_range(500.0..900.0);
let radius = rng.gen::<f32>() * 2.5; // why can't this infer the gen type?
let point_light = PointLight {
intensity,
range: LIGHT_RANGE,
color,
radius,
shadows_enabled: true,
..Default::default()
2022-01-19 04:08:47 +00:00
};
commands
.spawn_bundle((
AnimatedCyberLight { axis, rate },
Transform::from_rotation(rotation),
GlobalTransform::default(),
))
.with_children(|parent| {
parent
.spawn_bundle(PointLightBundle {
transform,
point_light,
..Default::default()
})
.with_children(|builder| {
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()
}),
2022-01-19 04:08:47 +00:00
..Default::default()
})
.insert(AnimateCyberLightWireframe);
2022-01-19 04:08:47 +00:00
}); // mesh child
}); // light child
}
// up light
commands
.spawn_bundle(PointLightBundle {
2022-01-19 04:08:47 +00:00
transform: Transform::from_xyz(0.0, PLANET_RADIUS + 30.0, 0.0),
point_light: pink_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 10.0,
subdivisions: 2,
})),
material: materials.add(StandardMaterial {
base_color: Color::RED,
emissive: Color::rgba_linear(100.0, 0.0, 0.0, 0.0),
..Default::default()
}),
..Default::default()
});
});
// down light
commands
.spawn_bundle(PointLightBundle {
2022-01-19 04:08:47 +00:00
transform: Transform::from_xyz(0.0, -PLANET_RADIUS - 30.0, 0.0),
point_light: blue_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 10.0,
subdivisions: 2,
})),
material: materials.add(StandardMaterial {
base_color: Color::BLUE,
emissive: Color::rgba_linear(0.0, 0.0, 100.0, 0.0),
..Default::default()
}),
..Default::default()
});
});
}
2022-01-19 04:08:47 +00:00
fn orbit_lights(time: Res<Time>, mut query: Query<(&mut Transform, &AnimatedCyberLight)>) {
let dt = time.delta_seconds();
2022-01-19 04:08:47 +00:00
for (mut transform, light) in query.iter_mut() {
let AnimatedCyberLight { axis, rate } = *light;
let theta = rate * dt;
transform.rotation *= Quat::from_axis_angle(axis, theta);
}
}
2022-01-14 00:14:08 +00:00
pub struct CyberSpaceLightsPlugin;
impl Plugin for CyberSpaceLightsPlugin {
fn build(&self, app: &mut App) {
app.add_startup_system(spawn_lights)
2022-01-19 04:08:47 +00:00
.add_system(orbit_lights);
2022-01-14 00:14:08 +00:00
}
}