-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,122 @@ | ||
use rand::Rng; | ||
use sled::driver::{BufferContainer, Driver, Filters, TimeInfo}; | ||
use sled::{color::Rgb, Sled, SledError}; | ||
use sled::{color::Rgb, Sled, SledError, Vec2}; | ||
|
||
use std::f32::consts::TAU; | ||
const INV_TAU: f32 = 1.0 / TAU; | ||
|
||
const GREEN_RADIUS: f32 = 2.33; | ||
const GREEN_COUNT: usize = 64; | ||
const GREEN: Rgb = Rgb::new(0.6, 0.93, 0.762); | ||
|
||
const BLUE_RADIUS: f32 = 3.0; | ||
const BLUE_COUNT: usize = 96; | ||
const BLUE: Rgb = Rgb::new(0.4, 0.51, 0.93); | ||
|
||
const TRAIL_RADIUS: f32 = 1.2; | ||
const NUM_STARS: usize = 5000; | ||
const VELOCITY: f32 = 6.0; | ||
const DIRECTION: Vec2 = Vec2::new(-0.7071, -0.7071); | ||
|
||
#[allow(dead_code)] | ||
pub fn build_driver() -> Driver { | ||
let mut driver = Driver::new(); | ||
|
||
driver.set_startup_commands(startup); | ||
driver.set_compute_commands(compute); | ||
driver.set_draw_commands(draw); | ||
driver | ||
|
||
return driver; | ||
} | ||
|
||
fn startup( | ||
sled: &mut Sled, | ||
buffers: &mut BufferContainer, | ||
_filters: &mut Filters, | ||
) -> Result<(), SledError> { | ||
let stars = buffers.create_buffer::<Vec2>("stars"); | ||
let center = sled.center_point(); | ||
let mut rng = rand::thread_rng(); | ||
|
||
let orth = DIRECTION.perp(); | ||
|
||
for _ in 0..NUM_STARS { | ||
let sign = match rng.gen_bool(0.5) { | ||
true => 1.0, | ||
false => -1.0, | ||
}; | ||
|
||
let spawn_pos = center | ||
+ (DIRECTION * rng.gen_range(40.0..300.0)) | ||
+ (orth * rng.gen_range(1.45..35.0) * sign); | ||
|
||
stars.push(spawn_pos); | ||
} | ||
|
||
let colors = buffers.create_buffer::<Rgb>("colors"); | ||
colors.extend([ | ||
Rgb::new(0.15, 0.5, 1.0), | ||
Rgb::new(0.25, 0.3, 1.0), | ||
Rgb::new(0.05, 0.4, 0.8), | ||
Rgb::new(0.7, 0.0, 0.6), | ||
Rgb::new(0.05, 0.75, 1.0), | ||
Rgb::new(0.1, 0.8, 0.6), | ||
Rgb::new(0.6, 0.05, 0.2), | ||
Rgb::new(0.85, 0.15, 0.3), | ||
Rgb::new(0.0, 0.0, 1.0), | ||
Rgb::new(1.0, 0.71, 0.705), | ||
]); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn compute( | ||
sled: &Sled, | ||
buffers: &mut BufferContainer, | ||
_filters: &mut Filters, | ||
time_info: &TimeInfo, | ||
) -> Result<(), SledError> { | ||
let mut rng = rand::thread_rng(); | ||
let delta = time_info.delta.as_secs_f32(); | ||
let stars = buffers.get_buffer_mut::<Vec2>("stars")?; | ||
let center = sled.center_point(); | ||
|
||
let orth = DIRECTION.perp(); | ||
|
||
for star in stars { | ||
*star -= DIRECTION * VELOCITY * delta; | ||
if star.x.signum() != DIRECTION.x.signum() && star.y.signum() != DIRECTION.y.signum() { | ||
let dq = (*star - center).length_squared(); | ||
if dq > 1000.0 { | ||
let sign = match rng.gen_bool(0.5) { | ||
true => 1.0, | ||
false => -1.0, | ||
}; | ||
|
||
let spawn_pos = center | ||
+ (DIRECTION * rng.gen_range(40.0..300.0)) | ||
+ (orth * rng.gen_range(1.5..35.0) * sign); | ||
|
||
*star = spawn_pos; | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn draw( | ||
sled: &mut Sled, | ||
_buffers: &BufferContainer, | ||
buffers: &BufferContainer, | ||
_filters: &Filters, | ||
time_info: &TimeInfo, | ||
) -> Result<(), SledError> { | ||
let elapsed = time_info.elapsed.as_secs_f32(); | ||
let stars = buffers.get_buffer::<Vec2>("stars")?; | ||
let center = sled.center_point(); | ||
let delta = time_info.delta.as_secs_f32(); | ||
|
||
let inner_time_scale = elapsed / GREEN_RADIUS; | ||
let outer_time_scale = elapsed / BLUE_RADIUS; | ||
let fade_amount = 1.0 - (delta * 25.0); | ||
|
||
// speckle in swirling green points | ||
for i in 0..GREEN_COUNT { | ||
let angle = inner_time_scale + (TAU / GREEN_COUNT as f32) * i as f32 % TAU; | ||
sled.modulate_at_angle(angle, |led| led.color + GREEN); | ||
} | ||
sled.for_each(|led| led.color *= fade_amount); | ||
|
||
// speckle in swirling blue points | ||
for i in 0..BLUE_COUNT { | ||
let angle = outer_time_scale + (TAU / BLUE_COUNT as f32) * i as f32 % TAU; | ||
sled.modulate_at_angle(angle, |led| led.color + BLUE); | ||
let mut i = 0; | ||
for star in stars { | ||
let d = Vec2::new(star.x - center.x, star.y - center.y); | ||
let c = *buffers.get_buffer_item::<Rgb>("colors", i % 10)?; | ||
sled.modulate_at_dir(d, |led| { | ||
let d_sq = (d.length() - led.distance()).powi(2); | ||
led.color + (c * 1.0 / d_sq) | ||
}); | ||
i += 1; | ||
} | ||
|
||
// brighten or darken points depending on time and angle to simulate a sweeping | ||
// trail thing. | ||
let radar_time_scale = elapsed / TRAIL_RADIUS; | ||
let angle = (radar_time_scale % TAU) + TAU; | ||
sled.map(|led| { | ||
let da = (led.angle() + angle) % TAU; | ||
let fac = 1.0 - (da * INV_TAU).powf(1.25); | ||
led.color * fac | ||
}); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters