sunnuntai 17. heinäkuuta 2022

ShaderToy bacteria

ShaderToy Bacteria2

How it works

I saw video of "ants" coding adventures ( Coding Adventure: Ant and Slime Simulations Youtube ) and I wanted to try to recreate something similar.


Something similar was agents / particles / bacteria, simulated with gpu on a pixel grid using shadertoy.

The bacteria has 2D coordinates, direction and "life left seconds" (packs nicely in 4 floats of RGBA framebuffer). 
The bacteria roams on screen  and leave behind a trail ("pheromones"). The bacteria sniffs 2 points ahead of itself (somewhat like antennas) for pheromone strength, which it then uses to turn towards the most powerful pheromone trail. For a little random "gains life" logic, the bacteria receives more life if it is on top of lots of pheromone.
Each bacteria has life, which expires at some point, once expired the bacteria spawns on random point on the screen.

Common

Common source contains settings / defines for the bacterial behavior. There are also all the structures and all the helper functions for the program. 

Buffer A

Buffer A contains the bacterial structure, each pixel can contain live bacterial data, there should be at least 2 in the FRAME_COUNT define, this way we have "previous frame" data in the -1 frame. The current "buffer" area is writable, and reading from same buffer same pixel where writing happens, is probably a bad idea.. but this does it anyways :o .. Hmm, yeh there should be a different buffer where to read the bacterial data, rather than this FRAME_COUNT hack, but, it seems to work :D

Anyhow, the previous bacterial data is read, "life logic" is run with the bacteria, and the new data is written into the current pixel. If the pixel corresponds out of current "buffer" discard happens. 

!TODO rethink the buffer logic, so that reading / writing doesnt happen on the same buffer/texture.

Buffer B

Buffer B contains the pheromone data for the bacteria, this is very very heavy shader.
First decay is applied to the pixel, pheromone decays over time.
Then for each pixel, all bacteria is iterated through, the pixel position is compared to a line between bacteria last and current position, if the pixel is close enough to that line, pheromone is added to the pixel.

Image

Final image generation just reads Buffer B data and applies transformation to that 1D pheromone data to produce beautiful color palette on screen, the algorithm is straight from https://iquilezles.org/articles/palettes .

Summary

The bacteria produces random images, that are somewhat pleasing, there is no deep patterns, as there is not enough logic or desire for the bacteria to interact with everything, or there is the 2 "antennae" sniffing logic and the "add life" logic, which account for the bacteria turning and staying alive longer, but to create more interesting patterns, there should be other goals too, like gathering resources and returning to base.

Another interesting thing would be to isolate these "life goals" for the bacteria in certain place, so that it could be easily replaced with other directives.