Skip to content

Commit

Permalink
Improve idle performance
Browse files Browse the repository at this point in the history
By only doing flames maths for pixels that can potentially contain
flames.
  • Loading branch information
walles committed Jun 30, 2023
1 parent 0a3e042 commit 6532be4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,13 @@ How would you visualize this?

## TODO

- Improve idle-system performance by only trying to render clouds where they
could be, and the same thing for fire.
- Make the macOS About dialog show the latest `macos-` version number rather
than a date
- Make an icon
- Make sure the icon is visible in the Finder
- Make sure the icon is visible in the About dialog
- Try increasing the fire distortion radius when flames are higher
- Try distorting the fire more vertically than horizontally
- Animate the screenshot in a cross faded loop. Try 10s first and see if that's
enough.
- Make a Dock icon visualization
Expand Down Expand Up @@ -153,5 +152,7 @@ How would you visualize this?
- Make the clouds look more realistic:
- Have less details in the clouds
- Animate the clouds slower
- Improve idle-system performance by only trying to render clouds where they
could be, and the same thing for the flames.

[create new release]: https://github.com/walles/loadviz/releases/new
34 changes: 24 additions & 10 deletions libloadviz/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ impl Renderer {
width: usize,
height: usize,
) -> Option<[u8; 3]> {
// Higher scale number = more details.
let scale = 5.0 / width as f32;
// Higher number = more details.
let detail = 5.0 / width as f32;

// Higher speed number = faster cloud turbulence.
let speed = 0.3;
Expand All @@ -109,8 +109,8 @@ impl Renderer {
// Noise output is -1 to 1, deciphered from here:
// https://github.com/amethyst/bracket-lib/blob/0d2d5e6a9a8e7c7ae3710cfef85be4cab0109a27/bracket-noise/examples/simplex_fractal.rs#L34-L39
let noise_m1_to_1 = self.noise.get_noise3d(
scale * pixel_x as f32,
scale * pixel_y_from_top as f32,
detail * pixel_x as f32,
detail * pixel_y_from_top as f32,
speed * dt_seconds,
);

Expand Down Expand Up @@ -146,25 +146,39 @@ impl Renderer {
width: usize,
height: usize,
) -> Option<[u8; 3]> {
// Higher scale number = more details.
let scale = 10.0 / width as f32;
// Higher number = more details.
let detail = 10.0 / width as f32;

let distortion_pixel_radius = width.min(height) as f32 / 10.0;

// Check whether we should even try to do flames maths. This improves
// our idle-system benchmark by 63%.
let highest_load_0_to_1 = viz_loads
.iter()
.map(|load| load.user_0_to_1)
.max_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap();
let highest_possible_flame_height_pixels =
highest_load_0_to_1 * height as f32 + distortion_pixel_radius;
if pixel_y_from_bottom as f32 > highest_possible_flame_height_pixels {
// We're above the flames, no need for any (costly) noise maths
return None;
}

// Starting at this fraction of each flame pillar's height, the color will
// start fading towards the background color.
let fadeout_fraction = 0.8;

// Noise output is -1 to 1, deciphered from here:
// https://github.com/amethyst/bracket-lib/blob/0d2d5e6a9a8e7c7ae3710cfef85be4cab0109a27/bracket-noise/examples/simplex_fractal.rs#L34-L39
let noise1_m1_to_1 = self.noise.get_noise3d(
scale * pixel_x as f32,
scale * pixel_y_from_bottom as f32,
detail * pixel_x as f32,
detail * pixel_y_from_bottom as f32,
dt_seconds,
);
let noise2_m1_to_1 = self.noise.get_noise3d(
scale * pixel_x as f32,
scale * pixel_y_from_bottom as f32,
detail * pixel_x as f32,
detail * pixel_y_from_bottom as f32,
-dt_seconds - 1.0,
);

Expand Down

0 comments on commit 6532be4

Please sign in to comment.