Skip to content

Commit a2b84c1

Browse files
committed
feat: allow for saving reverse gifs, simplify ui, adjust sim settings
1 parent 93fa2a9 commit a2b84c1

4 files changed

Lines changed: 54 additions & 38 deletions

File tree

src/app.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ pub struct ObamifyApp {
161161
#[cfg(not(target_arch = "wasm32"))]
162162
current_drawing_id: Arc<AtomicU32>,
163163
current_filter_mode: wgpu::FilterMode,
164+
165+
reverse: bool,
164166
}
165167

166168
impl ObamifyApp {
@@ -231,7 +233,8 @@ impl ObamifyApp {
231233
source: Preset,
232234
change_index: usize,
233235
) {
234-
let (seed_count, seeds, colors, sim) = morph_sim::init_image(self.size.0, source);
236+
let (seed_count, mut seeds, colors, mut sim) = morph_sim::init_image(self.size.0, source);
237+
sim.prepare_play(&mut seeds, self.reverse);
235238
self.apply_sim_init(device, queue, seed_count, seeds, colors, sim);
236239
self.gui.current_preset = change_index;
237240
}
@@ -798,6 +801,8 @@ impl ObamifyApp {
798801
#[cfg(target_arch = "wasm32")]
799802
inbox: Vec::new(),
800803
current_filter_mode: wgpu::FilterMode::Linear,
804+
805+
reverse: false,
801806
}
802807
}
803808

src/app/gif_recorder.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ impl GifRecorder {
231231
self.should_stop
232232
}
233233
}
234+
235+
pub(crate) fn get_name(&self, name: String, reverse: bool) -> String {
236+
if reverse {
237+
format!("unobamify_{}", name)
238+
} else {
239+
format!("obamify_{}", name)
240+
}
241+
}
234242
}
235243

236244
impl ObamifyApp {

src/app/gui.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ impl App for ObamifyApp {
216216

217217
if self.gif_recorder.should_stop() {
218218
// finish recording
219-
if !self.gif_recorder.finish(self.sim.name()) {
219+
if !self.gif_recorder.finish(
220+
self.gif_recorder.get_name(self.sim.name(), self.reverse),
221+
) {
220222
// cancelled
221223
self.stop_recording_gif(device, &rs.queue);
222224
}
@@ -311,28 +313,21 @@ impl App for ObamifyApp {
311313

312314
GuiMode::Transform => {
313315
ui.horizontal_wrapped(|ui| {
314-
if ui
315-
.add_enabled(
316-
!self.gui.animate,
317-
egui::Button::new("play transformation"), //.fill(egui::Color32::from_rgb(47, 92, 34)),
318-
)
319-
.clicked()
320-
{
316+
if ui.add(egui::Button::new("play transformation")).clicked() {
321317
self.gui.animate = true;
318+
self.sim.prepare_play(&mut self.seeds, self.reverse);
322319
}
323320
if ui
324-
.add_enabled(
325-
self.gui.animate,
326-
egui::Button::new("switch target"),
327-
)
328-
.clicked()
321+
.add(egui::Checkbox::new(&mut self.reverse, "reverse"))
322+
.changed()
329323
{
330-
self.sim.switch();
331-
}
332-
if ui.button("reload").clicked() {
324+
self.gui.animate = true;
333325
self.reset_sim(device, &rs.queue);
334-
self.gui.animate = false;
335326
}
327+
// if ui.button("reload").clicked() {
328+
// self.reset_sim(device, &rs.queue);
329+
// self.gui.animate = false;
330+
// }
336331
});
337332
ui.separator();
338333

@@ -436,7 +431,7 @@ impl App for ObamifyApp {
436431
preset.clone(),
437432
i,
438433
);
439-
self.gui.animate = false;
434+
self.gui.animate = true;
440435
self.gui.current_preset = i;
441436
close_menu = true;
442437
}

src/app/morph_sim.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn init_image(sidelen: u32, source: Preset) -> (u32, Vec<SeedPos>, Vec<SeedC
2323

2424
sim.set_assignments(assignments, sidelen);
2525
for cell in &mut sim.cells {
26-
cell.dst_force = 0.14;
26+
cell.dst_force = 0.13;
2727
}
2828
(seeds_n as u32, seeds, colors, sim)
2929
}
@@ -100,10 +100,10 @@ pub struct CellBody {
100100

101101
const PERSONAL_SPACE: f32 = 0.95;
102102
const MAX_VELOCITY: f32 = 6.0;
103-
const ALIGNMENT_FACTOR: f32 = 0.7;
103+
const ALIGNMENT_FACTOR: f32 = 0.8;
104104

105105
fn factor_curve(x: f32) -> f32 {
106-
(x * x * x).min(10.0)
106+
(x * x * x).min(1000.0)
107107
}
108108

109109
impl CellBody {
@@ -186,22 +186,11 @@ impl CellBody {
186186
self.accy -= dy * weight;
187187
} else if dist.abs() < f32::EPSILON {
188188
// if they are exactly on top of each other, push in a random direction
189-
// deterministic pseudo-random push based on position
190-
let x_bits = pos.xy[0].to_bits();
191-
let y_bits = pos.xy[1].to_bits();
192-
let mut h = x_bits ^ y_bits.rotate_left(16) ^ 0x9E3779B9;
193-
h ^= h >> 15;
194-
h = h.wrapping_mul(0x85EB_CA6B);
195-
h ^= h >> 13;
196-
let hx = h;
197-
198-
let mut h2 = hx ^ 0xC2B2_AE35;
199-
h2 ^= h2 >> 16;
200-
h2 = h2.wrapping_mul(0x27D4_EB2D);
201-
h2 ^= h2 >> 15;
202-
203-
let r1 = (hx as f32) / (u32::MAX as f32); // [0, 1)
204-
let r2 = (h2 as f32) / (u32::MAX as f32); // [0, 1)
189+
let seed = (pos.xy[0].to_bits() as u64) ^ ((pos.xy[1].to_bits() as u64) << 32);
190+
let mut rng = frand::Rand::with_seed(seed);
191+
192+
let r1 = rng.gen_range(0.0..1.0);
193+
let r2 = rng.gen_range(0.0..1.0);
205194

206195
self.accx += (r1 - 0.5) * 0.1;
207196
self.accy += (r2 - 0.5) * 0.1;
@@ -236,6 +225,7 @@ pub struct Sim {
236225
//elapsed_frames: u32,
237226
pub cells: Vec<CellBody>,
238227
name: String,
228+
reversed: bool,
239229
}
240230

241231
impl Sim {
@@ -244,6 +234,7 @@ impl Sim {
244234
cells: Vec::new(),
245235
//elapsed_frames: 0,
246236
name,
237+
reversed: false,
247238
}
248239
}
249240

@@ -261,6 +252,7 @@ impl Sim {
261252
mem::swap(&mut cell.srcy, &mut cell.dsty);
262253
cell.age = 0;
263254
}
255+
self.reversed = !self.reversed;
264256
}
265257

266258
pub fn update(&mut self, positions: &mut [SeedPos], sidelen: u32) {
@@ -366,6 +358,22 @@ impl Sim {
366358
self.cells[*src_idx].stroke_id = prev.stroke_id;
367359
}
368360
}
361+
362+
pub(crate) fn prepare_play(&mut self, positions: &mut [SeedPos], reverse: bool) {
363+
if self.reversed == reverse {
364+
for (i, cell) in self.cells.iter_mut().enumerate() {
365+
positions[i].xy[0] = cell.srcx;
366+
positions[i].xy[1] = cell.srcy;
367+
cell.age = 0;
368+
}
369+
} else {
370+
for (i, cell) in self.cells.iter().enumerate() {
371+
positions[i].xy[0] = cell.dstx;
372+
positions[i].xy[1] = cell.dsty;
373+
}
374+
self.switch();
375+
}
376+
}
369377
}
370378

371379
// pub fn preset_path_to_name(source_dir: &Path) -> String {

0 commit comments

Comments
 (0)