r/creativecoding • • Oct 13 '26

A Nebula Rearranged

A variation of the 'generalized rainbow smoke algorithm'.

The algorithm is made of 2 breadth-first-searches (BFS), one choosing colors and the other placing them. This video shows the 'placement BFS' constructing the image. Due to random sampling, the 'color BFS' can't always find an unused color, in which case it gives up, leaving a hole.

Once the placement BFS runs out of places to fill, the color BFS resets and the process is repeated on the unfilled holes. The choice of how to handle these points has a big impact on the final image and it's the main thing that's different here compared to the original 'generalized' algorithm (afaik).

The animating effect comes from 'rolling' the image used by the color BFS, (the Rosette Nebula in this case), with the speed of the animation being proportional to the number of unplaced pixels remaining.

A longer 4K loop is also available:

https://youtu.be/ZzxQXPHAVn4?si=ud_53baLFXAYlNpX

Links for more info about the algorithm:

195 Upvotes

15 comments sorted by

3

u/labmaster_q Oct 13 '26

'Tis beautiful, can we get the code?

1

u/Single-Lab-5692 Oct 14 '26

Thanks!

Here's a link to an earlier implementation that's easier to follow:
https://github.com/heyoeyo/pixel_pilfer/blob/38c2fda8c1f7d0aeed2998a5547d856958c95144/src/imports/task_main.rs#L531-L559
There's a 'enable_full_search' toggle, when it's on, the algorithm behaves like the original 'generalized' implementation (from the linked youtube video), when it's off, it produces the holes that are seen in the video I posted.

More recent commits on the repo run a lot faster using some binning tricks (brute force BFS is slow at 4K resolutions), but it's maybe not as readable.

1

u/gnome_of_the_damned Oct 13 '26

wonderful work! would love to see the code!

2

u/Single-Lab-5692 Oct 14 '26

Thank you! I posted a link to the repo in another reply above

1

u/gnome_of_the_damned Oct 14 '26

Awesome, that's super cool. Thanks!

1

u/johndavid_artist Oct 15 '26

Tying the roll speed to the number of unplaced pixels is a lovely touch, the image eases into place as the holes close. On the second pass, do the holes take the nearest unused color or a fresh random sample?

1

u/Single-Lab-5692 Oct 15 '26 edited Oct 15 '26

The holes get a fresh sample (typically a color that was already used in the first pass) in this example. It's also possible that the second pass gets stuck in the same way when trying to fill large holes, in which case it'll do a third/fourth/etc. pass (so a color could be re-used 3+ times).

However if the nearest unused color is taken on the first pass (i.e. so holes never form) it produces results like the video this is all based on (first youtube link above, by Generative Garden), I assume that's how they handled these samples. If unused colors are taken on a second pass, it produces results that sort of resemble the 'coral' patterns from the original version of the algorithm (second link, by József Fejesz).

1

u/hardikgodhaa Oct 20 '26

This is really cool, would love to try something like this myself.

2

u/Single-Lab-5692 Oct 20 '26

Give it a shot! The basic algorithm is easy to implement compared to most creative coding ideas I think. Keeping it fast at higher resolutions (>1000x1000) can be tricky though.

0

u/[deleted] Oct 17 '26

[removed] — view removed comment

1

u/Single-Lab-5692 Oct 17 '26

Ultra settings enabled lol!