Get a bunch of lights to rotate.

This commit is contained in:
Joe Ardent 2022-01-18 20:08:47 -08:00
parent cfc32a741f
commit 18032861d2
4 changed files with 97 additions and 132 deletions

13
Cargo.lock generated
View file

@ -2386,9 +2386,18 @@ dependencies = [
[[package]]
name = "sha1"
version = "0.6.0"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d"
checksum = "c1da05c97445caa12d05e848c4a4fcbbea29e748ac28f7e80e9b010392063770"
dependencies = [
"sha1_smol",
]
[[package]]
name = "sha1_smol"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012"
[[package]]
name = "sharded-slab"

View file

@ -3,7 +3,7 @@ use heron::prelude::{CollisionShape, RigidBody};
use crate::Label;
pub const PLANET_RADIUS: f32 = 350.0;
pub const PLANET_RADIUS: f32 = 340.0;
pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS + 100.0;
#[derive(Component, Debug)]
@ -55,8 +55,8 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
})
.insert(CyberBike)
.insert(RigidBody::Dynamic)
.insert(CollisionShape::Cone {
half_height: 2.0,
.insert(CollisionShape::Capsule {
half_segment: 2.0,
radius: 0.8,
})
.insert(CyberBikeState::default());

View file

@ -2,6 +2,7 @@ use bevy::{
ecs::schedule::StageLabel,
prelude::{ResMut, SystemLabel, Vec3, Windows},
};
use heron::rapier_plugin::nalgebra::ComplexField;
pub mod action;
pub mod camera;
@ -42,5 +43,5 @@ pub fn random_sphere_vec(r: &mut impl rand::prelude::Rng) -> Vec3 {
let x = 2.0 * x1 * sqrt;
let y = 2.0 * x2 * sqrt;
let z = 1.0 - 2.0 * ssum;
Vec3::new(x, y, z)
Vec3::new(x, y, z).normalize()
}

View file

@ -1,23 +1,29 @@
use std::f32::consts::TAU;
use bevy::prelude::*;
use rand::prelude::*;
use crate::geometry::PLANET_RADIUS;
pub const LIGHT_RANGE: f32 = PLANET_RADIUS * 0.6;
pub const LIGHT_DIST: f32 = PLANET_RADIUS * 1.2;
pub const LIGHT_RANGE: f32 = 50.0;
#[derive(Component)]
struct Animate;
struct AnimatedCyberLight {
axis: Vec3,
rate: f32,
}
fn spawn_lights(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let red_light = PointLight {
intensity: 1_000.0,
let pink_light = PointLight {
intensity: 1_00.0,
range: LIGHT_RANGE,
color: Color::RED,
color: Color::PINK,
radius: 1.0,
shadows_enabled: true,
..Default::default()
};
@ -26,14 +32,7 @@ fn spawn_lights(
range: LIGHT_RANGE,
color: Color::BLUE,
radius: 1.0,
..Default::default()
};
let purple_light = PointLight {
intensity: 1_000.0,
range: LIGHT_RANGE,
color: Color::PURPLE,
radius: 1.0,
shadows_enabled: true,
..Default::default()
};
@ -42,100 +41,72 @@ fn spawn_lights(
brightness: 0.32,
});
// east light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(LIGHT_DIST, 0.0, 0.0),
point_light: purple_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default()
});
})
.insert(Animate);
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.8..0.95);
let lightness = rng.gen_range(0.4..0.7);
let color = Color::hsl(hue, saturation, lightness);
// west light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(-LIGHT_DIST, 0.0, 0.0),
point_light: purple_light,
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()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default()
});
})
.insert(Animate);
// north light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(0.0, 0.0, LIGHT_DIST),
point_light: purple_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default()
});
})
.insert(Animate);
// south light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(0.0, 0.0, -LIGHT_DIST),
point_light: purple_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default()
});
})
.insert(Animate);
};
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: 2,
})),
material: materials.add(StandardMaterial {
base_color: color,
emissive: color,
..Default::default()
}),
..Default::default()
});
}); // mesh child
}); // light child
}
// up light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(0.0, LIGHT_DIST, 0.0),
point_light: red_light,
transform: Transform::from_xyz(0.0, PLANET_RADIUS + 30.0, 0.0),
point_light: pink_light,
..Default::default()
})
.with_children(|builder| {
@ -155,7 +126,7 @@ fn spawn_lights(
// down light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(0.0, -LIGHT_DIST, 0.0),
transform: Transform::from_xyz(0.0, -PLANET_RADIUS - 30.0, 0.0),
point_light: blue_light,
..Default::default()
})
@ -175,28 +146,12 @@ fn spawn_lights(
});
}
fn animate_lights(
time: Res<Time>,
mut query: Query<&mut Transform, (With<PointLight>, With<Animate>)>,
) {
fn orbit_lights(time: Res<Time>, mut query: Query<(&mut Transform, &AnimatedCyberLight)>) {
let dt = time.delta_seconds();
for mut transform in query.iter_mut() {
let translation = &transform.translation;
let x = translation.x;
let y = translation.y;
let z = if translation.z.abs() < 0.1 {
translation.z + 0.15
} else {
translation.z
};
let rads = 10.0f32.to_radians();
let anim_rate = rads * dt;
let theta = z.atan2(x) + anim_rate;
let x_new = LIGHT_DIST * theta.cos();
let z_new = LIGHT_DIST * theta.sin();
*transform = Transform::from_xyz(x_new, y, z_new);
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);
}
}
@ -204,6 +159,6 @@ pub struct CyberSpaceLightsPlugin;
impl Plugin for CyberSpaceLightsPlugin {
fn build(&self, app: &mut App) {
app.add_startup_system(spawn_lights)
.add_system(animate_lights);
.add_system(orbit_lights);
}
}