- Kategorien Flash, Festtage (Weihnachten + Silvester), Musik, Floating, Natur, Fraktal, Ruhe mit je 12 Effekten; 66 neue Effekte, u. a. Weihnachtsbaum, Adventskranz, Countdown, Vulkan, Polarlicht, Mandelbrot, Lorenz-Attraktor, Leben 3D, Hilbertkurve, Sanduhr, Kerzenschein - eine Datei je Kategorie (fx_*.cpp), gemeinsame Hilfen in fx.h (plotSoft, fresh/fxStart für den Effektstart, ringPos, wrap8) - Gewitter bleibt der Starteffekt (jetzt in Natur) - App: Farben für die neuen Kategorien - README-Tabellen und API-Beispiele angepasst Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
365 lines
12 KiB
C++
365 lines
12 KiB
C++
// Kategorie Fraktal: Fraktale, seltsame Attraktoren und Zellautomaten
|
||
|
||
#include "fx.h"
|
||
|
||
// Farbe für Fraktal-Iterationen: innen dunkel, außen bunt
|
||
static CRGB iterColor(uint8_t i, uint8_t maxIter, uint8_t shift) {
|
||
if (i >= maxIter) return CRGB(0, 0, brightness / 10);
|
||
return CHSV(i * 12 + shift, 255, scale8(min(255, brightness * 3), 90 + min(165, i * 20)));
|
||
}
|
||
|
||
// --- Mandelbrot: Zoom ins Seepferdchental; jede Etage eine Stufe tiefer ---------
|
||
|
||
void renderMandelbrot(uint32_t now) {
|
||
const float CX = -0.743643887f, CY = 0.131825904f;
|
||
float t = ((now - fxStart) % 24000) / 1000.0f;
|
||
float scale = 1.6f * powf(0.7f, t); // in 24 s rund 5000-fach
|
||
uint8_t maxIter = 30 + t * 3;
|
||
FOR_XYZ {
|
||
float s = scale * powf(0.72f, z);
|
||
float cr = CX + (x - MID) / MID * s, ci = CY + (y - MID) / MID * s;
|
||
float zr = 0, zi = 0;
|
||
uint8_t i = 0;
|
||
for (; i < maxIter && zr * zr + zi * zi < 4; i++) {
|
||
float tmp = zr * zr - zi * zi + cr;
|
||
zi = 2 * zr * zi + ci;
|
||
zr = tmp;
|
||
}
|
||
at(x, y, z) = iterColor(i, maxIter, t * 10);
|
||
}
|
||
}
|
||
|
||
// --- Julia: der Parameter wandert im Kreis; jede Etage ist ein Stück voraus -----
|
||
|
||
void renderJulia(uint32_t now) {
|
||
float t = (now - fxStart) * 0.00025f;
|
||
const uint8_t MAX_ITER = 24;
|
||
FOR_XYZ {
|
||
float a = t + z * 0.06f;
|
||
float cr = 0.7885f * cosf(a), ci = 0.7885f * sinf(a);
|
||
float zr = (x - MID) / MID * 1.5f, zi = (y - MID) / MID * 1.5f;
|
||
uint8_t i = 0;
|
||
for (; i < MAX_ITER && zr * zr + zi * zi < 4; i++) {
|
||
float tmp = zr * zr - zi * zi + cr;
|
||
zi = 2 * zr * zi + ci;
|
||
zr = tmp;
|
||
}
|
||
at(x, y, z) = iterColor(i, MAX_ITER, now / 60);
|
||
}
|
||
}
|
||
|
||
// --- Bitmuster: Sierpinski- und XOR-Muster aus den Koordinaten, die durchwandern --
|
||
|
||
void renderBitPattern(uint32_t now) {
|
||
uint32_t t = now - fxStart;
|
||
uint8_t mode = t / 8000 % 3;
|
||
uint8_t ox = t / 500, oy = t / 700, oz = t / 900;
|
||
FastLED.clear();
|
||
FOR_XYZ {
|
||
uint8_t X = x + ox, Y = y + oy, Z = z + oz;
|
||
switch (mode) {
|
||
case 0: // Sierpinski-Tetraeder
|
||
if (((X & Y) | (Y & Z) | (X & Z)) % 8 == 0) at(x, y, z) = CHSV((X + Y + Z) * 10, 220, highlight);
|
||
break;
|
||
case 1: // XOR-Farben
|
||
at(x, y, z) = CHSV((X ^ Y ^ Z) * 32 + t / 30, 255, scale8(min(255, brightness * 2), sin8(((X ^ Y ^ Z) & 7) * 32 - t / 6)));
|
||
break;
|
||
default: // UND-Gitter
|
||
if ((X & Y & Z) % 4 != 0) at(x, y, z) = CHSV(160 + (X | Y | Z) * 8, 200, scale8(highlight, 200));
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
// --- Fraktalbaum: wächst vom Stamm aus, jeder Ast teilt sich in zwei -------------
|
||
|
||
static int8_t treeX[NUM_LAYERS][8], treeY[NUM_LAYERS][8];
|
||
static uint8_t treeN[NUM_LAYERS];
|
||
|
||
static void growTree() {
|
||
static const int8_t DIRS[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
|
||
treeX[0][0] = treeX[1][0] = 2;
|
||
treeY[0][0] = treeY[1][0] = 2;
|
||
treeN[0] = treeN[1] = 1;
|
||
for (uint8_t z = 2; z < NUM_LAYERS; z++) {
|
||
treeN[z] = 0;
|
||
for (uint8_t i = 0; i < treeN[z - 1]; i++) {
|
||
uint8_t a = random8(8), b = (a + random8(1, 8)) % 8; // zwei verschiedene Richtungen
|
||
for (uint8_t d : {a, b}) {
|
||
treeX[z][treeN[z]] = constrain(treeX[z - 1][i] + DIRS[d][0], 0, TOP);
|
||
treeY[z][treeN[z]] = constrain(treeY[z - 1][i] + DIRS[d][1], 0, TOP);
|
||
treeN[z]++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void renderFractalTree(uint32_t now) {
|
||
static uint32_t cycleStart = 0;
|
||
static uint8_t leafHue = 0;
|
||
if (fresh || now - cycleStart > 9000) {
|
||
fresh = false;
|
||
cycleStart = now;
|
||
leafHue = random8();
|
||
growTree();
|
||
}
|
||
uint32_t t = now - cycleStart;
|
||
uint8_t shown = min<uint32_t>(NUM_LAYERS, t / 600 + 1); // Etage für Etage wachsen
|
||
uint8_t fade = t < 7000 ? 255 : 255 - (t - 7000) * 255 / 2000;
|
||
FastLED.clear();
|
||
for (uint8_t z = 0; z < shown; z++)
|
||
for (uint8_t i = 0; i < treeN[z]; i++) {
|
||
CRGB c = z < TOP ? CRGB(CHSV(20 + z * 15, 200, min(255, brightness * 2)))
|
||
: CRGB(CHSV(leafHue + i * 4, 255, scale8(highlight, 160 + scale8(sin8(now / 5 + i * 40), 95))));
|
||
at(treeX[z][i], treeY[z][i], z) = c.nscale8(fade);
|
||
}
|
||
}
|
||
|
||
// --- Chaosspiel: springt immer halb zu einer Ecke, es entsteht ein Sierpinski-Tetraeder
|
||
|
||
void renderChaosGame(uint32_t now) {
|
||
static float p[3] = {MID, MID, MID};
|
||
static const uint8_t TETRA[4][3] = {{0, 0, 0}, {TOP, TOP, 0}, {TOP, 0, TOP}, {0, TOP, TOP}};
|
||
fadeToBlackBy(leds, NUM_LEDS, 10);
|
||
for (uint8_t n = 0; n < 6; n++) {
|
||
uint8_t k = random8(4);
|
||
for (uint8_t i = 0; i < 3; i++) p[i] = (p[i] + TETRA[k][i]) / 2;
|
||
plot(p[0], p[1], p[2], CHSV(k * 64 + now / 100, 220, scale8(highlight, 120)));
|
||
}
|
||
}
|
||
|
||
// --- Lorenz-Attraktor: der berühmte Schmetterling, als Leuchtspur ---------------
|
||
|
||
void renderLorenz(uint32_t now) {
|
||
static float x = 1, y = 1, z = 20;
|
||
if (fresh) {
|
||
fresh = false;
|
||
x = rnd(-1, 1);
|
||
y = 1;
|
||
z = 20;
|
||
}
|
||
fadeToBlackBy(leds, NUM_LEDS, 22);
|
||
for (uint8_t i = 0; i < 3; i++) {
|
||
const float dt = 0.006f;
|
||
float dx = 10 * (y - x), dy = x * (28 - z) - y, dz = x * y - 8.0f / 3 * z;
|
||
x += dx * dt;
|
||
y += dy * dt;
|
||
z += dz * dt;
|
||
}
|
||
plotSoft((x + 20) / 40 * TOP, (y + 27) / 54 * TOP, (z - 3) / 47 * TOP, CHSV(x > 0 ? 20 : 150 + now / 200 % 40, 220, highlight));
|
||
}
|
||
|
||
// --- Thomas-Attraktor: eine Spur windet sich durch alle Ecken des Würfels -------
|
||
|
||
void renderThomas(uint32_t now) {
|
||
static float p[3] = {1.1f, 1.1f, -0.01f};
|
||
if (fresh) {
|
||
fresh = false;
|
||
p[0] = rnd(0.5f, 1.5f);
|
||
p[1] = 1.1f;
|
||
p[2] = -0.01f;
|
||
}
|
||
fadeToBlackBy(leds, NUM_LEDS, 14);
|
||
for (uint8_t i = 0; i < 4; i++) {
|
||
const float b = 0.208186f, dt = 0.05f;
|
||
float d[3] = {sinf(p[1]) - b * p[0], sinf(p[2]) - b * p[1], sinf(p[0]) - b * p[2]};
|
||
for (uint8_t k = 0; k < 3; k++) p[k] += d[k] * dt;
|
||
}
|
||
plotSoft((p[0] + 4.5f) / 9 * TOP, (p[1] + 4.5f) / 9 * TOP, (p[2] + 4.5f) / 9 * TOP,
|
||
CHSV(wrap8(now / 40 + p[2] * 20), 220, highlight));
|
||
}
|
||
|
||
// --- Leben 3D: Conways Spiel des Lebens im Würfel (Regel B5/S4567) --------------
|
||
|
||
static uint8_t life[NUM_LAYERS][CUBE_SIZE][CUBE_SIZE], lifeOld[NUM_LAYERS][CUBE_SIZE][CUBE_SIZE];
|
||
|
||
static void seedLife() {
|
||
FOR_XYZ life[z][y][x] = random8() < 77; // 30 %
|
||
memset(lifeOld, 0, sizeof(lifeOld));
|
||
}
|
||
|
||
void renderLife3d(uint32_t now) {
|
||
static uint32_t last = 0;
|
||
static uint8_t gens = 0, hue = 0;
|
||
if (fresh) {
|
||
fresh = false;
|
||
seedLife();
|
||
gens = 0;
|
||
last = now;
|
||
}
|
||
if (now - last >= 450) {
|
||
last = now;
|
||
uint8_t next[NUM_LAYERS][CUBE_SIZE][CUBE_SIZE];
|
||
uint16_t alive = 0;
|
||
FOR_XYZ {
|
||
uint8_t n = 0;
|
||
for (int dz = -1; dz <= 1; dz++)
|
||
for (int dy = -1; dy <= 1; dy++)
|
||
for (int dx = -1; dx <= 1; dx++)
|
||
if (dx || dy || dz) n += life[(z + dz + NUM_LAYERS) % NUM_LAYERS][(y + dy + CUBE_SIZE) % CUBE_SIZE][(x + dx + CUBE_SIZE) % CUBE_SIZE];
|
||
next[z][y][x] = life[z][y][x] ? n >= 4 && n <= 7 : n == 5;
|
||
alive += next[z][y][x];
|
||
}
|
||
bool same = !memcmp(next, life, sizeof(life)), blink = !memcmp(next, lifeOld, sizeof(life));
|
||
memcpy(lifeOld, life, sizeof(life));
|
||
memcpy(life, next, sizeof(life));
|
||
if (!alive || same || blink || ++gens > 60) { // ausgestorben oder festgefahren
|
||
seedLife();
|
||
gens = 0;
|
||
hue += 70;
|
||
}
|
||
}
|
||
FOR_XYZ {
|
||
CRGB &led = at(x, y, z);
|
||
if (life[z][y][x]) led = CHSV(hue + z * 12, 220, lifeOld[z][y][x] ? min(255, brightness * 3) : highlight); // neu geboren: heller
|
||
else led.nscale8(200); // gestorbene glimmen kurz nach
|
||
}
|
||
}
|
||
|
||
// --- Zellautomat: eindimensionale Regeln 30, 90, 110 und 150 um den Rand herum ---
|
||
// Jede Generation erscheint oben, ältere rutschen nach unten.
|
||
|
||
void renderAutomaton(uint32_t now) {
|
||
static const uint8_t RULES[] = {30, 90, 110, 150};
|
||
static uint16_t rows[NUM_LAYERS]; // Bit i = Zelle i auf dem Rand
|
||
static uint32_t last = 0;
|
||
static uint8_t gen = 0, rule = 0;
|
||
if (fresh) {
|
||
fresh = false;
|
||
memset(rows, 0, sizeof(rows));
|
||
rows[TOP] = 1;
|
||
gen = 0;
|
||
last = now;
|
||
}
|
||
if (now - last >= 350) {
|
||
last = now;
|
||
uint16_t cur = rows[TOP], nxt = 0;
|
||
for (uint8_t i = 0; i < 16; i++) {
|
||
uint8_t l = cur >> ((i + 15) % 16) & 1, c = cur >> i & 1, r = cur >> ((i + 1) % 16) & 1;
|
||
if (RULES[rule] >> (l << 2 | c << 1 | r) & 1) nxt |= 1 << i;
|
||
}
|
||
memmove(rows, rows + 1, sizeof(rows[0]) * TOP);
|
||
rows[TOP] = nxt;
|
||
if (++gen >= 24 || !nxt || nxt == 0xFFFF) { // nächste Regel, neu mit einer Zelle
|
||
gen = 0;
|
||
rule = (rule + 1) % 4;
|
||
rows[TOP] |= 1 << random8(16);
|
||
}
|
||
}
|
||
FastLED.clear();
|
||
for (uint8_t z = 0; z < NUM_LAYERS; z++)
|
||
for (uint8_t i = 0; i < 16; i++) {
|
||
if (!(rows[z] >> i & 1)) continue;
|
||
uint8_t x, y;
|
||
ringPos(i, x, y);
|
||
at(x, y, z) = CHSV(rule * 64 + z * 10, 230, z == TOP ? highlight : min(255, brightness * 2));
|
||
}
|
||
}
|
||
|
||
// --- Hilbertkurve: ein Licht zeichnet die raumfüllende Kurve (4×4×4) -------------
|
||
|
||
// Index 0..63 -> Koordinaten 0..3 (Algorithmus von J. Skilling, 3 Achsen, 2 Bit)
|
||
static void hilbertPoint(uint8_t h, uint8_t p[3]) {
|
||
for (uint8_t i = 0; i < 3; i++) p[i] = 0;
|
||
for (uint8_t j = 0; j < 2; j++)
|
||
for (uint8_t i = 0; i < 3; i++) p[i] |= (h >> (j * 3 + 2 - i) & 1) << j;
|
||
uint8_t t = p[2] >> 1;
|
||
for (uint8_t i = 2; i > 0; i--) p[i] ^= p[i - 1];
|
||
p[0] ^= t;
|
||
for (uint8_t q = 2; q != 4; q <<= 1) {
|
||
uint8_t mask = q - 1;
|
||
for (int8_t i = 2; i >= 0; i--) {
|
||
if (p[i] & q) p[0] ^= mask;
|
||
else {
|
||
t = (p[0] ^ p[i]) & mask;
|
||
p[0] ^= t;
|
||
p[i] ^= t;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void renderHilbert(uint32_t now) {
|
||
const float S = TOP / 3.0f; // 0..3 auf 0..4 strecken
|
||
float pos = ((now - fxStart) % 7000) / 7000.0f * 66; // 64 Punkte, dann kurz Pause
|
||
FastLED.clear();
|
||
uint8_t a[3], b[3];
|
||
for (uint8_t h = 0; h < 64 && h <= pos; h++) {
|
||
hilbertPoint(h, a);
|
||
uint8_t v = scale8(highlight, h + 4 > pos ? 255 : 110);
|
||
plotSoft(a[0] * S, a[1] * S, a[2] * S, CHSV(h * 4, 230, v));
|
||
}
|
||
if (pos < 63) { // Kopf gleitet zum nächsten Punkt
|
||
hilbertPoint(pos, a);
|
||
hilbertPoint(pos + 1, b);
|
||
float f = pos - int(pos);
|
||
plotSoft((a[0] + (b[0] - a[0]) * f) * S, (a[1] + (b[1] - a[1]) * f) * S, (a[2] + (b[2] - a[2]) * f) * S,
|
||
CRGB(highlight, highlight, highlight));
|
||
}
|
||
}
|
||
|
||
// --- Kristall: umherirrende Teilchen bleiben am Kristall hängen (DLA) ------------
|
||
|
||
static uint8_t crystal[NUM_LAYERS][CUBE_SIZE][CUBE_SIZE]; // 0 = leer, sonst Reihenfolge
|
||
|
||
void renderCrystal(uint32_t now) {
|
||
static int8_t w[3];
|
||
static uint8_t count = 0;
|
||
static uint32_t doneAt = 0;
|
||
if (fresh || (doneAt && now - doneAt > 2500)) {
|
||
fresh = false;
|
||
memset(crystal, 0, sizeof(crystal));
|
||
crystal[0][2][2] = count = 1; // Keim unten in der Mitte
|
||
doneAt = 0;
|
||
w[0] = -1;
|
||
}
|
||
if (!doneAt)
|
||
for (uint8_t step = 0; step < 6; step++) {
|
||
if (w[0] < 0) { // neues Teilchen oben irgendwo
|
||
w[0] = random8(CUBE_SIZE);
|
||
w[1] = random8(CUBE_SIZE);
|
||
w[2] = TOP;
|
||
}
|
||
static const int8_t DIRS[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
|
||
bool stick = false;
|
||
for (const auto &d : DIRS) {
|
||
int x = w[0] + d[0], y = w[1] + d[1], z = w[2] + d[2];
|
||
if (inCube(x, y, z) && crystal[z][y][x]) stick = true;
|
||
}
|
||
if (stick && !crystal[w[2]][w[1]][w[0]]) {
|
||
crystal[w[2]][w[1]][w[0]] = ++count;
|
||
if (w[2] == TOP || count > 70) doneAt = now;
|
||
w[0] = -1;
|
||
break;
|
||
}
|
||
const int8_t *d = DIRS[random8() < 40 ? 5 : random8(6)]; // leicht nach unten
|
||
int x = w[0] + d[0], y = w[1] + d[1], z = w[2] + d[2];
|
||
if (inCube(x, y, z) && !crystal[z][y][x]) {
|
||
w[0] = x;
|
||
w[1] = y;
|
||
w[2] = z;
|
||
}
|
||
}
|
||
FastLED.clear();
|
||
FOR_XYZ {
|
||
uint8_t c = crystal[z][y][x];
|
||
if (!c) continue;
|
||
uint8_t v = doneAt ? scale8(highlight, 150 + scale8(sin8(now / 3 + c * 20), 105)) : min(255, brightness * 2);
|
||
at(x, y, z) = CHSV(140 + c * 2, 200, v);
|
||
}
|
||
if (w[0] >= 0 && !doneAt) at(w[0], w[1], w[2]) += CRGB(brightness / 2, brightness / 2, brightness / 2);
|
||
}
|
||
|
||
// --- Fraktalwolken: Rauschen über drei Größenordnungen ---------------------------
|
||
|
||
void renderFractalClouds(uint32_t now) {
|
||
uint32_t t = now - fxStart;
|
||
static const CRGBPalette16 palette(CRGB::Black, CRGB::DarkViolet, CRGB::DeepPink, CRGB::Gold);
|
||
FOR_XYZ {
|
||
uint8_t n = inoise8(x * 25, y * 25 + t / 20, z * 25) / 2 + inoise8(x * 70 + t / 8, y * 70, z * 70) / 4 +
|
||
inoise8(x * 160, y * 160, z * 160 + t / 3) / 8; // höchstens 221
|
||
n = qsub8(n, 60);
|
||
at(x, y, z) = ColorFromPalette(palette, qadd8(n, n), min(255, brightness * 3));
|
||
}
|
||
}
|