Merge branch 'master' of ssh://ssh.paoda.moe:31059/paoda/mandelbrot into bevy0.4

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-03-29 21:20:42 -05:00
commit b86f6549d3
7 changed files with 532 additions and 865 deletions

View File

@ -5,7 +5,7 @@ name: default
steps:
- name: cargo test
image: rust:1.49
image: rust:latest
commands:
- apt-get update
- apt-get install -y libasound2-dev

1084
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = "0.4"
num-complex = "0.3"
rayon = "1.5"
bevy = "^0.4"
num-complex = "^0.4"
rayon = "^1.5"

View File

@ -3,34 +3,46 @@
An interactive interface where you can navigate the Mandelbrot Set.
### Controls
<kbd>W</kbd> - Up
<br />
<kbd>A</kbd> - Left
<br />
<kbd>S</kbd> - Down
<br />
<kbd>D</kbd> - Right
<kbd>Q</kbd> Zoom Out
<br />
<kbd>E</kbd> Zoom In
#### 2D Movement
Key | Action
--- | ---
<kbd>W</kbd> | Up
<kbd>A</kbd> | Left
<kbd>S</kbd> | Down
<kbd>D</kbd> | Right
#### Zoom
Key | Action
--- | ---
<kbd>Q</kbd> | Zoom Out
<kbd>E</kbd> | Zoom In
#### 2D and Zoom Scale
Key | Action
--- | ---
<kbd>R</kbd> | Increase Scale
<kbd>F</kbd> | Decrease Scale
#### Mandelbrot Iteration Limit (Max 512 by default)
Key | Action
--- | ---
<kbd>T</kbd> | Increment by a factor of 2
<kbd>G</kbd> | Decrement by a factor of 2
### Build Instructions
Due to my current configuration (which is one that prioritizes quick development), Bevy requires
some dependencies in order to build this project. These dependencies are `clang`, `lld`, and `llvm`.
Also, this program requires Rust Nightly (as of 2020-11-26)
Once done, you can build the program with `cargo build`, or `cargo build --release`
You can build the program with `cargo build`, or `cargo build --release` on stable.
### Run Instructions
You can either execut the compiled binary, or use cargo and run `cargo run --release`
You can either execute the compiled binary, or use `cargo run --release`
### TODO
* Make Zooming feel more natural (where you zoom to is fixed, and not centered)
* Make the amount of iterations done when calculating the Mandelbrot set configurable
* preferrably with a keybind
* Add smooth colouring
* Maybe look into histograms?
- [x] Make Zooming feel more natural (where you zoom to is fixed, and not centred)
- [x] Make the amount of iterations done when calculating the Mandelbrot set configurable
- [x] Add smooth colouring
<!-- * Maybe look into histograms? -->
- [ ] Zoom with the Mouse?
- [ ] Automatically change scale? Remove the need for the user to deal with the Scale modifier

View File

@ -1,2 +1,7 @@
pub use crate::mandelbrot::Bounds;
pub use crate::mandelbrot::Mandelbrot;
pub const TEXTURE_WIDTH: usize = 1280;
pub const TEXTURE_HEIGHT: usize = 720;
mod mandelbrot;

View File

@ -1,6 +1,6 @@
use bevy::prelude::*;
use bevy::render::texture::{Extent3d, TextureDimension, TextureFormat};
use mandelbrot::Mandelbrot;
use mandelbrot::{Bounds, Mandelbrot, TEXTURE_HEIGHT, TEXTURE_WIDTH};
use std::time::Instant;
fn main() {
@ -8,58 +8,60 @@ fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(startup.system())
.add_resource(SpriteScale::default())
.add_resource(TextureOptions::default())
.add_system(mandelbrot_render_system.system())
.add_system(transform_mandelbrot_system.system())
.run();
}
struct SpriteScale {
struct TextureOptions {
zoom: f64,
horiz: f64,
verti: f64,
horizontal: f64,
vertical: f64,
step: f64,
iterations: u32,
iteration_limit: u32,
}
impl Default for SpriteScale {
impl Default for TextureOptions {
fn default() -> Self {
Self {
zoom: 1.0,
horiz: 0.0,
verti: 0.0,
horizontal: 0.0,
vertical: 0.0,
step: 0.01,
iterations: 64,
iteration_limit: 64,
}
}
}
fn transform_mandelbrot_system(input: Res<Input<KeyCode>>, mut scale: ResMut<SpriteScale>) {
fn transform_mandelbrot_system(input: Res<Input<KeyCode>>, mut scale: ResMut<TextureOptions>) {
// Zoom: E to Zoom In, Q to Zoom Out
// Horizontal Movement: D to move right, A to move left
// Vertical Movement: W to move up, S to move Down
// R to increase the steps taken by zoom, horizontal movement, and vertical movement
// T to increase the Iteration Limit, G to decrease the iteration limit
let zoom = input.pressed(KeyCode::Q) as i8 - input.pressed(KeyCode::E) as i8;
let horizontal = input.pressed(KeyCode::D) as i8 - input.pressed(KeyCode::A) as i8;
let vertical = input.pressed(KeyCode::W) as i8 - input.pressed(KeyCode::S) as i8;
let step_mod = input.pressed(KeyCode::R) as i8 - input.pressed(KeyCode::F) as i8;
let iter_mod = input.pressed(KeyCode::T) as i8 - input.pressed(KeyCode::G) as i8;
let step_change = input.pressed(KeyCode::R) as i8 - input.pressed(KeyCode::F) as i8;
let limit_change = input.just_pressed(KeyCode::T) as i8 - input.just_pressed(KeyCode::G) as i8;
scale.step += (scale.step / 10.0) * step_mod as f64;
scale.verti += scale.step * vertical as f64;
scale.horiz += scale.step * horizontal as f64;
scale.step += (scale.step / 10.0) * step_change as f64;
scale.vertical += scale.step * vertical as f64;
scale.horizontal += scale.step * horizontal as f64;
scale.zoom += scale.step * zoom as f64;
if iter_mod == 1 {
scale.iterations += (scale.step * 100.0) as u32;
if limit_change == 1 {
scale.iteration_limit *= 2;
if scale.iterations > 512 {
scale.iterations = 512;
if scale.iteration_limit > 512 {
scale.iteration_limit = 512;
}
} else if iter_mod == -1 {
let step = (scale.step * 100.0) as u32;
let diff = scale.iterations as i64 - step as i64;
} else if limit_change == -1 {
scale.iteration_limit /= 2;
if diff < 32 {
scale.iterations = 32;
} else {
scale.iterations -= step;
if scale.iteration_limit < 32 {
scale.iteration_limit = 32;
}
}
@ -75,7 +77,7 @@ fn transform_mandelbrot_system(input: Res<Input<KeyCode>>, mut scale: ResMut<Spr
fn mandelbrot_render_system(
materials: Res<Assets<ColorMaterial>>,
mut textures: ResMut<Assets<Texture>>,
scale: Res<SpriteScale>,
scale: Res<TextureOptions>,
mut query: Query<(&mut Mandelbrot, &Handle<ColorMaterial>)>,
) {
for (mut fractal, handle) in query.iter_mut() {
@ -83,20 +85,23 @@ fn mandelbrot_render_system(
if let Some(texture_handle) = &material.texture {
if let Some(texture) = textures.get_mut(texture_handle) {
let z = scale.zoom;
let h = scale.horiz;
let v = scale.verti;
let iters = scale.iterations;
let h = scale.horizontal;
let v = scale.vertical;
let limit = scale.iteration_limit;
let buf = fractal.generate_scaled_image(
((-2.5 * z) + h, (1.0 * z) + h),
((-1.0 * z) - v, (1.0 * z) - v),
iters,
);
let x = ((-2.5 * z) + h, (1.0 * z) + h);
let y = ((-1.0 * z) - v, (1.0 * z) - v);
let bounds = Bounds::new(x, y);
let start = Instant::now();
// FIXME: This will not work due to https://github.com/bevyengine/bevy/issues/1161
// The workaround involves "getting a mutable borrow of the ColorMaterial you use to display the Texture"
// according to @FrancoisM#2474, a developer of Bevy on Discord
texture.data.copy_from_slice(buf);
texture
.data
.copy_from_slice(fractal.scaled_image(bounds, limit));
let _diff = Instant::now() - start;
}
}
}
@ -108,14 +113,13 @@ fn startup(
mut textures: ResMut<Assets<Texture>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let img_size = Extent3d::new(Mandelbrot::width() as u32, Mandelbrot::height() as u32, 1);
let dimension = TextureDimension::D2;
let img_buf = Mandelbrot::new().generate_image().to_vec();
let texture_size = Extent3d::new(TEXTURE_WIDTH as u32, TEXTURE_HEIGHT as u32, 1);
let texture_data = Mandelbrot::new().image().to_vec();
let texture_handle = textures.add(Texture::new(
img_size,
dimension,
img_buf,
texture_size,
TextureDimension::D2,
texture_data,
TextureFormat::Rgba8UnormSrgb,
));

View File

@ -1,79 +1,98 @@
use crate::{TEXTURE_HEIGHT, TEXTURE_WIDTH};
use num_complex::Complex;
use rayon::prelude::*;
const MAX_ITERATIONS: u16 = 512;
const DEFAULT_ITERATION_LIMIT: u32 = 64;
const DEFAULT_BOUNDS: Bounds = Bounds::new((-2.5, 1.0), (-1.0, 1.0));
pub struct Coordinate(usize, usize);
impl Coordinate {
pub fn new(x: usize, y: usize) -> Self {
Self(x, y)
}
pub fn x(&self) -> usize {
self.0
}
pub fn y(&self) -> usize {
self.1
}
}
#[derive(Debug, Clone, Copy)]
pub struct Bounds {
x: (f64, f64),
y: (f64, f64),
}
impl Bounds {
pub const fn new(x: (f64, f64), y: (f64, f64)) -> Self {
Self { x, y }
}
pub fn x(&self) -> (f64, f64) {
self.x
}
pub fn y(&self) -> (f64, f64) {
self.y
}
}
#[derive(Debug, Clone)]
pub struct Mandelbrot {
texture_buffer: Vec<u8>,
frame_buffer: Box<[u8; (TEXTURE_WIDTH * TEXTURE_HEIGHT) * 4]>,
}
impl Mandelbrot {
const IMG_WIDTH: usize = 1280;
const IMG_HEIGHT: usize = 720;
pub fn new() -> Self {
Mandelbrot {
texture_buffer: vec![0; (Self::IMG_WIDTH * Self::IMG_HEIGHT) * 4],
frame_buffer: Box::new([0; (TEXTURE_WIDTH * TEXTURE_HEIGHT) * 4]),
}
}
pub fn width() -> usize {
Self::IMG_WIDTH
}
pub fn image(&mut self) -> &[u8] {
let limit_f64 = DEFAULT_ITERATION_LIMIT as f64;
pub fn height() -> usize {
Self::IMG_HEIGHT
}
pub fn generate_image(&mut self) -> &[u8] {
self.texture_buffer
self.frame_buffer
.par_chunks_mut(4)
.enumerate()
.for_each(|(i, buf)| {
let iters = Self::new_escape_time(i, (-2.5, 1.0), (-1.0, 1.0), 64);
let normalized_iters = iters / MAX_ITERATIONS as f64;
let h = 0.5 + (10.0 * normalized_iters);
buf.copy_from_slice(&Self::hsv_to_rgb(h, 0.6, 1.0));
});
&self.texture_buffer
}
pub fn generate_scaled_image(
&mut self,
x_bounds: (f64, f64),
y_bounds: (f64, f64),
max_iterations: u32,
) -> &[u8] {
self.texture_buffer
.par_chunks_mut(4)
.enumerate()
.for_each(|(i, buf)| {
let max_iters = max_iterations as f64;
let iters = Self::new_escape_time(i, x_bounds, y_bounds, max_iterations);
let normalized_iters = iters / max_iters;
let iterations = Self::escape_time(i, DEFAULT_BOUNDS, DEFAULT_ITERATION_LIMIT);
let normalized_iters = iterations / limit_f64;
let h = normalized_iters * 350.0;
let v = if iters == max_iters { 0.0 } else { 1.0 };
let v = if iterations == limit_f64 { 0.0 } else { 1.0 };
buf.copy_from_slice(&Self::hsv_to_rgb(h, 1.0, v));
});
self.frame_buffer.as_ref()
}
pub fn scaled_image(&mut self, bounds: Bounds, limit: u32) -> &[u8] {
self.frame_buffer
.par_chunks_mut(4)
.enumerate()
.for_each(|(i, buf)| {
let limit_f64 = limit as f64;
let iterations = Self::escape_time(i, bounds, limit);
let normalized_iters = iterations / limit_f64;
let h = normalized_iters * 350.0;
let v = if iterations == limit_f64 { 0.0 } else { 1.0 };
buf.copy_from_slice(&Self::hsv_to_rgb(h, 1.0, v))
});
&self.texture_buffer
self.frame_buffer.as_ref()
}
fn new_escape_time(
i: usize,
x_bounds: (f64, f64),
y_bounds: (f64, f64),
max_iterations: u32,
) -> f64 {
let px = i % Self::IMG_WIDTH;
let py = i / Self::IMG_WIDTH;
let c = Self::coords_to_complex(px, py, x_bounds, y_bounds);
fn escape_time(i: usize, bounds: Bounds, iteration_limit: u32) -> f64 {
let point = Coordinate::new(i % TEXTURE_WIDTH, i / TEXTURE_WIDTH);
let c = Self::coords_to_complex(point, bounds);
Self::new_calc_num_iters(c, max_iterations)
Self::count_iterations(c, iteration_limit)
}
#[inline]
@ -108,53 +127,50 @@ impl Mandelbrot {
[r, g, b, a]
}
fn new_calc_num_iters(c: Complex<f64>, max_iterations: u32) -> f64 {
fn count_iterations(c: Complex<f64>, maximum: u32) -> f64 {
let mut z: Complex<f64> = Complex::new(0.0, 0.0);
let mut num_iters: u32 = 0;
let mut count: u32 = 0;
loop {
if z.norm_sqr() > 4.0 {
break;
}
if num_iters >= max_iterations {
if count >= maximum {
break;
}
z = (z * z) + c;
num_iters += 1;
count += 1;
}
if num_iters < max_iterations {
(num_iters as f64 + 1.0) - z.norm().ln().ln() / 2f64.ln()
if count < maximum {
(count as f64 + 1.0) - z.norm().ln().ln() / 2f64.ln()
} else {
num_iters as f64
count as f64
}
}
fn coords_to_complex(
px: usize,
py: usize,
x_bounds: (f64, f64),
y_bounds: (f64, f64),
) -> Complex<f64> {
fn coords_to_complex(point: Coordinate, bounds: Bounds) -> Complex<f64> {
Complex::new(
Self::scale_width(px, x_bounds),
Self::scale_height(py, y_bounds),
Self::scale_width(point.x(), bounds.x()),
Self::scale_height(point.y(), bounds.y()),
)
}
fn scale_width(px: usize, x_bounds: (f64, f64)) -> f64 {
#[inline]
fn scale_width(x: usize, bounds: (f64, f64)) -> f64 {
// const X_MIN: f64 = -2.5;
// const X_MAX: f64 = 1.0;
x_bounds.0 + ((x_bounds.1 - x_bounds.0) * (px as f64 - 0.0)) / Self::IMG_WIDTH as f64 - 0.0
bounds.0 + ((bounds.1 - bounds.0) * (x as f64 - 0.0)) / TEXTURE_WIDTH as f64 - 0.0
}
fn scale_height(py: usize, y_bounds: (f64, f64)) -> f64 {
#[inline]
fn scale_height(y: usize, bounds: (f64, f64)) -> f64 {
// const Y_MIN: f64 = -1.0;
// const Y_MAX: f64 = 1.0;
y_bounds.0 + ((y_bounds.1 - y_bounds.0) * (py as f64 - 0.0)) / Self::IMG_HEIGHT as f64 - 0.0
bounds.0 + ((bounds.1 - bounds.0) * (y as f64 - 0.0)) / TEXTURE_HEIGHT as f64 - 0.0
}
}