Files
cube/fx_natur.cpp
T
mikeandClaude Opus 5.5 a8fee0e960 Regler „Effekt“: ein Parameter je Effekt (Tempo, Farbe, Empfindlichkeit, …)
- Tempo (66 Effekte): eigene Effekt-Uhr 0,25- bis 4-mal so schnell; bei
  mehr Tempo mehrere Rechenschritte pro Bild (aufwändige Effekte nur
  einer), bei weniger nicht in jedem Bild
- Farbe (Blitzlicht, Blaulicht, Geschenk, Fraktalwolken, Lampe): Farbton
  des fertigen Bilds drehen
- Empfindlichkeit (Musik): Pegel und Spektrum verstärken, Beat-Schwelle
- Eigene: Gewitter Blitze, Feuer Hitze, Regen Stärke, Schnee Dichte,
  Weihnachtsbaum Kugelfarbe
- Mitte 128 = ursprünglicher Effekt; Wert je Effekt im Flash
- API: param in /api/state, Parametername in /api/effects
- App: Regler „Effekt“ unter der Helligkeit (Titel antippen = Mitte),
  Schalter in einer Zeile, „Alle Kategorien“ heißt jetzt „Zufällig“

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 15:57:13 +01:00

351 lines
13 KiB
C++

// Kategorie Natur (dazu Gewitter und Feuer aus effects.cpp)
#include "fx.h"
// --- Regen: Tropfen fallen und ziehen unten Kreise im Wasser -------------------
struct Ripple {
float x, y;
uint32_t start; // 0 = frei
};
static Ripple ripples[6];
void renderRain(uint32_t now) {
static uint8_t next = 0;
if (fresh) {
fresh = false;
for (Ripple &r : ripples) r.start = 0;
}
if (random8() < min(255, int(30 * paramFactor()))) // Regler: Stärke
spawnParticle(random8(CUBE_SIZE), random8(CUBE_SIZE), TOP + 0.4f, 0, 0, -rnd(0.12f, 0.18f),
CRGB(highlight / 5, highlight / 3, highlight / 2));
for (Particle &p : particles)
if (p.life && p.z + p.vz < 0.5f) { // trifft aufs Wasser
ripples[next] = {p.x, p.y, now};
next = (next + 1) % 6;
p.life = 0;
}
moveParticles(0, 1, 0);
FastLED.clear();
for (uint8_t y = 0; y < CUBE_SIZE; y++)
for (uint8_t x = 0; x < CUBE_SIZE; x++) at(x, y, 0) = CRGB(0, brightness / 8, brightness / 3);
for (Ripple &r : ripples) {
if (!r.start) continue;
uint32_t t = now - r.start;
if (t > 900) {
r.start = 0;
continue;
}
float radius = t * 0.004f, fade = 1 - t / 900.0f;
for (uint8_t y = 0; y < CUBE_SIZE; y++)
for (uint8_t x = 0; x < CUBE_SIZE; x++) {
float f = (1 - fabsf(hypotf(x - r.x, y - r.y) - radius) / 0.6f) * fade;
if (f > 0) at(x, y, 0) += CRGB(0, uint8_t(f * highlight / 3), uint8_t(f * highlight / 1.5f));
}
}
drawParticles();
}
// --- Polarlicht: grüne Vorhänge mit violetten Säumen wehen über den Himmel ------
void renderAurora(uint32_t now) {
static const float PROFILE[] = {0, 0.35f, 1, 0.85f, 0.55f}; // Helligkeit je Etage
uint32_t t = now - fxStart;
FOR_XYZ {
if (z == 0) { // verschneiter Boden
at(x, y, z) = CRGB(brightness / 14, brightness / 14, brightness / 8);
continue;
}
uint8_t n = inoise8(x * 45 + t / 25, y * 45 - t / 40, t / 20);
n = qsub8(n, 95);
n = qadd8(n, qadd8(n, n));
uint8_t hue = 96 + (z >= 3 ? (z - 2) * 55 : 0) + n / 24;
at(x, y, z) = CHSV(hue, 230, uint8_t(scale8(n, min(255, brightness * 3)) * PROFILE[z]));
}
}
// --- Ozean: Wasser mit Wellen, Schaum auf den Kämmen ---------------------------
void renderOcean(uint32_t now) {
float t = (now - fxStart) * 0.001f;
uint16_t nt = (now - fxStart) * 6 / 100;
FastLED.clear();
for (uint8_t y = 0; y < CUBE_SIZE; y++)
for (uint8_t x = 0; x < CUBE_SIZE; x++) {
float h = 2.1f + 0.9f * sinf(x * 0.9f + t * 1.4f) * cosf(y * 0.7f - t) +
(inoise8(x * 40, y * 40, nt) - 128) / 128.0f * 0.6f;
for (uint8_t z = 0; z < NUM_LAYERS; z++) {
if (z < h - 0.5f) {
at(x, y, z) = CHSV(152 - z * 4, 255, uint8_t(brightness * (0.35f + 0.15f * z)));
} else {
float f = 1 - fabsf(z - h) / 0.8f; // Oberfläche
if (f <= 0) continue;
bool foam = h > 2.8f;
at(x, y, z) = CHSV(140, foam ? 60 : 160, uint8_t(f * highlight * (foam ? 0.8f : 0.5f)));
}
}
}
}
// --- Glühwürmchen: grünlich-gelbe Lichter blinken langsam im Dunkeln -----------
struct Firefly {
float p[3], v[3];
uint32_t start; // Beginn des nächsten Leuchtens
};
static Firefly flies[10];
void renderFireflies(uint32_t now) {
if (fresh) {
fresh = false;
for (Firefly &f : flies) {
for (uint8_t i = 0; i < 3; i++) {
f.p[i] = rnd(0, TOP);
f.v[i] = 0;
}
f.start = now + random16(3000);
}
}
FastLED.clear();
for (uint8_t y = 0; y < CUBE_SIZE; y++)
for (uint8_t x = 0; x < CUBE_SIZE; x++) at(x, y, 0) = CRGB(0, brightness / 14, 0); // Wiese
for (Firefly &f : flies) {
for (uint8_t i = 0; i < 3; i++) {
f.v[i] = constrain(f.v[i] + rnd(-0.002f, 0.002f), -0.015f, 0.015f);
f.p[i] += f.v[i];
if (f.p[i] < 0 || f.p[i] > TOP) f.v[i] = -f.v[i];
f.p[i] = constrain(f.p[i], 0.0f, float(TOP));
}
int32_t t = now - f.start;
if (t < 0) continue;
float env = t < 400 ? t / 400.0f : t < 700 ? 1 : t < 1500 ? 1 - (t - 700) / 800.0f : 0;
if (t >= 1500) f.start = now + random16(1000, 4000);
if (env > 0) plotSoft(f.p[0], f.p[1], f.p[2], CRGB(scale8(highlight, 150), highlight, 0).nscale8(uint8_t(env * 255)));
}
}
// --- Sonnenaufgang: Nacht, Morgenrot, Tag und Abendrot in 40 s ------------------
void renderSunrise(uint32_t now) {
float ph = ((now - fxStart) % 40000) / 40000.0f * TWO_PI - HALF_PI; // Start in der Nacht
float e = sinf(ph); // Sonnenhöhe -1..1
float sunX = MID - 2.6f * cosf(ph), sunZ = -0.5f + e * 4.5f;
float day = constrain(e * 2 + 0.3f, 0.0f, 1.0f);
float dawn = max(0.0f, 1 - fabsf(e) * 3);
CRGB night(0, 0, brightness / 6), sky(brightness / 3, brightness / 2, brightness), red(brightness, brightness / 3, 0);
FOR_XYZ {
CRGB c = blend(night, sky, uint8_t(day * 255));
c += CRGB(red).nscale8(uint8_t(dawn * 255 * (1 - z / float(NUM_LAYERS)))); // Morgenrot am Horizont
float d = dist3(x, y, z, sunX, MID, sunZ);
if (d < 1.6f) c += CRGB(highlight, scale8(highlight, 130 + uint8_t(day * 100)), scale8(highlight, 40)).nscale8(uint8_t((1 - d / 1.6f) * 255));
at(x, y, z) = c;
}
static const uint8_t STARS[][3] = {{0, 3, 2}, {3, 1, 2}, {2, 4, 2}, {4, 4, 3}, {1, 0, 3}, {2, 2, 4}, {4, 1, 4}, {0, 4, 4}};
for (const auto &s : STARS) // Sterne, nur nachts
at(s[0], s[1], s[2]) += CRGB(brightness, brightness, brightness).nscale8(uint8_t((1 - day) * scale8(sin8(now / 9 + s[0] * 50 + s[1] * 30), 200)));
}
// --- Blumenwiese: wogendes Gras, Blumen auf Stängeln, ein Schmetterling ---------
struct Flower {
uint8_t x, y, h, hue;
};
static Flower flowers[6];
static float butterfly[3], butterflyV[3];
void renderMeadow(uint32_t now) {
static const uint8_t HUES[] = {0, 40, 200, 160, 64, 230}; // 64 = weiß
if (fresh) {
fresh = false;
for (uint8_t i = 0; i < 6; i++) {
bool taken;
do {
flowers[i] = {random8(CUBE_SIZE), random8(CUBE_SIZE), random8(2, 4), HUES[i]};
taken = false;
for (uint8_t k = 0; k < i; k++) taken |= flowers[k].x == flowers[i].x && flowers[k].y == flowers[i].y;
} while (taken);
}
for (uint8_t i = 0; i < 3; i++) {
butterfly[i] = MID;
butterflyV[i] = 0;
}
}
uint32_t t = now - fxStart;
FastLED.clear();
for (uint8_t y = 0; y < CUBE_SIZE; y++)
for (uint8_t x = 0; x < CUBE_SIZE; x++) {
uint8_t n = inoise8(x * 60 + t / 8, y * 60, t / 10); // Wind im Gras
at(x, y, 0) = CHSV(80 + n / 16, 255, scale8(brightness, 120 + n / 3));
if (n > 140) at(x, y, 1) = CHSV(88, 255, scale8(brightness, (n - 140) * 2));
}
for (Flower &f : flowers) {
for (uint8_t z = 1; z < f.h; z++) at(f.x, f.y, z) = CHSV(96, 255, brightness / 2);
at(f.x, f.y, f.h) = CHSV(f.hue, f.hue == 64 ? 0 : 200, scale8(highlight, 170 + scale8(sin8(t / 10 + f.hue), 85)));
}
for (uint8_t i = 0; i < 3; i++) { // Schmetterling flattert oben herum
butterflyV[i] = constrain(butterflyV[i] + rnd(-0.01f, 0.01f), -0.04f, 0.04f);
butterfly[i] += butterflyV[i];
float lo = i == 2 ? 2.5f : 0, hi = TOP;
if (butterfly[i] < lo || butterfly[i] > hi) butterflyV[i] = -butterflyV[i];
butterfly[i] = constrain(butterfly[i], lo, hi);
}
if (t / 120 % 2) plot(butterfly[0], butterfly[1], butterfly[2], CRGB(highlight, scale8(highlight, 120), 0));
}
// --- Wasserfall: Wasser stürzt an der Rückseite herab in ein Becken ------------
void renderWaterfall(uint32_t now) {
uint32_t t = now - fxStart;
FastLED.clear();
FOR_XYZ {
if (y == TOP) { // fallendes Wasser: Muster wandert nach unten
uint8_t n = inoise8(x * 90, z * 70 + t / 2);
n = qadd8(qsub8(n, 60), qsub8(n, 60));
at(x, y, z) = CHSV(150, 140 - n / 3, scale8(n, highlight));
} else if (z == 0) { // Becken mit Wellen, die von hinten nach vorn laufen
uint8_t w = sin8((TOP - y) * 50 - t / 3);
at(x, y, z) = CHSV(155, 255, scale8(brightness, 120 + w / 2));
if (y == TOP - 1 && random8() < 50) at(x, y, z) += CRGB(highlight / 2, highlight / 2, highlight / 2); // Gischt
}
}
if (random8() < 40) spawnParticle(rnd(0, TOP), TOP - 1, 0.5f, 0, rnd(-0.04f, -0.01f), rnd(0.04f, 0.08f), CRGB(highlight / 2, highlight / 2, highlight / 2));
moveParticles(0.008f, 0.97f, 12);
drawParticles();
}
// --- Vulkan: glühender Krater, Lavaströme und Ausbrüche -------------------------
static bool isMountain(int x, int y, int z) {
float d = distAxis(x, y);
return (z == 0 && d <= 2.3f) || (z == 1 && d <= 1.5f && d > 0.5f) || (z == 2 && d <= 1.0f && d > 0.5f);
}
// Lava: dunkelrot -> rot -> orange, nie gelb oder weiß
static CRGB lava(uint8_t heat) {
static const CRGBPalette16 LAVA(CRGB(60, 0, 0), CRGB(255, 0, 0), CRGB(255, 30, 0), CRGB(255, 80, 0));
return ColorFromPalette(LAVA, scale8(heat, 240)); // 240: nicht zurück zum Anfang überblenden
}
void renderVolcano(uint32_t now) {
static uint32_t nextEruption = 0;
static int8_t flowDir[2][2];
if (fresh) {
fresh = false;
nextEruption = now + 1500;
for (auto &d : flowDir) {
d[0] = random8(2) ? 1 : -1;
d[1] = random8(3) - 1;
}
}
uint32_t t = now - fxStart;
FastLED.clear();
FOR_XYZ {
if (isMountain(x, y, z)) at(x, y, z) = CRGB(brightness / 4, brightness / 14, 0); // dunkles, rötliches Gestein
}
// Krater und Schlot glühen
at(2, 2, 1) = lava(170 + scale8(sin8(t / 4), 60)).nscale8(highlight);
at(2, 2, 2) = lava(200 + random8(40)).nscale8(highlight);
// zwei Lavaströme fließen die Hänge hinab
for (auto &d : flowDir)
for (uint8_t k = 1; k <= 2; k++) {
uint8_t pulse = sin8(k * 70 - t / 3);
at(2 + d[0] * k, 2 + d[1] * k, 2 - k) = lava(120 + pulse / 3).nscale8(scale8(highlight, 150 + pulse / 3));
}
if (int32_t(now - nextEruption) >= 0) {
nextEruption = now + random16(1500, 4000);
for (uint8_t i = random8(12, 20); i > 0; i--)
spawnParticle(MID, MID, 2.5f, rnd(-0.06f, 0.06f), rnd(-0.06f, 0.06f), rnd(0.12f, 0.2f), lava(random8(100, 255)).nscale8(highlight));
}
moveParticles(0.006f, 0.99f, 3);
drawParticles();
}
// --- Herbstlaub: bunte Blätter segeln herab und bleiben am Boden liegen --------
static uint8_t leafHue[CUBE_SIZE][CUBE_SIZE], leafLevel[CUBE_SIZE][CUBE_SIZE];
void renderAutumn(uint32_t now) {
static const uint8_t HUES[] = {0, 12, 25, 40, 20};
if (fresh) {
fresh = false;
memset(leafLevel, 0, sizeof(leafLevel));
}
float wind = 0.012f * sinf((now - fxStart) * 0.0005f); // Böen von wechselnder Seite
if (random8() < 10)
spawnParticle(rnd(0, TOP), rnd(0, TOP), TOP + 0.4f, 0, 0, -rnd(0.012f, 0.02f), CHSV(HUES[random8(5)], 255, scale8(highlight, 180)));
for (Particle &p : particles) {
if (!p.life) continue;
p.vx = wind + 0.02f * sinf(now * 0.003f + p.y * 3); // hin und her segeln
p.vy = 0.01f * cosf(now * 0.002f + p.x * 3);
if (p.z + p.vz < 0) { // gelandet
uint8_t x = constrain(lroundf(p.x), 0, TOP), y = constrain(lroundf(p.y), 0, TOP);
leafLevel[x][y] = 255;
leafHue[x][y] = rgb2hsv_approximate(p.color).h;
p.life = 0;
}
if (p.x < 0 || p.x > TOP) p.vx = -p.vx;
}
moveParticles(0, 1, 0);
static uint8_t frame = 0;
bool rot = ++frame % 8 == 0;
FastLED.clear();
for (uint8_t y = 0; y < CUBE_SIZE; y++)
for (uint8_t x = 0; x < CUBE_SIZE; x++) {
if (rot) leafLevel[x][y] = qsub8(leafLevel[x][y], 1);
at(x, y, 0) = CHSV(leafHue[x][y], 255, scale8(scale8(highlight, 150), leafLevel[x][y]));
}
for (const Particle &p : particles) // Blätter taumeln: Helligkeit flackert
if (p.life) plot(p.x, p.y, p.z, CRGB(p.color).nscale8(160 + scale8(sin8(now / 3 + p.x * 40), 95)));
}
// --- Sternschnuppen: funkelnder Nachthimmel, ab und zu zieht eine Schnuppe vorbei --
struct Meteor {
bool active;
float x, y, z, vx, vy, vz;
};
void renderShootingStars(uint32_t now) {
static Meteor m;
static uint32_t next = 0;
static uint8_t sky[12][4]; // Hintergrundsterne: x, y, z, Phase
if (fresh) {
fresh = false;
m.active = false;
next = now + 1000;
for (uint8_t i = 0; i < 12; i++) {
bool taken;
do { // zufällige, verschiedene Plätze über der untersten Etage
sky[i][0] = random8(CUBE_SIZE);
sky[i][1] = random8(CUBE_SIZE);
sky[i][2] = random8(1, NUM_LAYERS);
taken = false;
for (uint8_t j = 0; j < i; j++) taken |= !memcmp(sky[i], sky[j], 3);
} while (taken);
sky[i][3] = random8();
}
}
FastLED.clear();
for (const auto &s : sky) {
uint8_t tw = sin8(now / 7 + s[3]);
at(s[0], s[1], s[2]) = CRGB(brightness / 2, brightness / 2, brightness).nscale8(60 + scale8(tw, 195));
}
if (!m.active && int32_t(now - next) >= 0) {
bool left = random8(2);
m = {true, left ? -0.5f : TOP + 0.5f, rnd(0, TOP), rnd(3, TOP + 0.5f),
(left ? 1 : -1) * rnd(0.18f, 0.26f), rnd(-0.05f, 0.05f), -rnd(0.04f, 0.09f)};
next = now + random16(1500, 5000);
}
if (m.active) {
spawnParticle(m.x, m.y, m.z, 0, 0, 0, CRGB(highlight / 2, highlight / 2, highlight / 3)); // Schweif
m.x += m.vx;
m.y += m.vy;
m.z += m.vz;
if (m.x < -1 || m.x > TOP + 1 || m.z < -0.5f) m.active = false;
else plot(m.x, m.y, m.z, CRGB(highlight, highlight, scale8(highlight, 200)));
}
moveParticles(0, 1, 30);
drawParticles();
}