More improvements

This commit is contained in:
cschwinne 2019-12-31 16:37:44 +01:00
parent 1671c78260
commit 446e2c123f

View File

@ -2458,28 +2458,24 @@ typedef struct Particle {
uint16_t WS2812FX::mode_starburst(void) { uint16_t WS2812FX::mode_starburst(void) {
uint32_t it = millis(); uint32_t it = millis();
const uint8_t numStars = 12; const uint8_t numStars = 4;
static star stars[numStars]; static star stars[numStars];
float MaxSpeed = 375.0f; // Max velocity float maxSpeed = 375.0f; // Max velocity
int NewParticleProbability = 2; // Odds of new particle (out of 255) float ParticleIgnition = 250.0f; // How long to "flash"
float ParticlePreignitonTime = 0.0f; // How long to "wink" float ParticleFadeTime = 1500.0f; // Fade out time
float ParticleIgnition = 0.06f; // How long to "flash"
float ParticleHoldTime = 0.0f; // Main lifecycle time
float ParticleFadeTime = 2.6f; // Fade out time
for (int j = 0; j < numStars; j++) for (int j = 0; j < numStars; j++)
{ {
// speed to adjust chance of a burst, max is nearly always. // speed to adjust chance of a burst, max is nearly always.
if (random8((263-SEGMENT.speed)>>1) < NewParticleProbability && stars[j].birth==0) if (random8((144-(SEGMENT.speed >> 1))) == 0 && stars[j].birth == 0)
{ {
// Pick a random color and location. // Pick a random color and location.
uint16_t startPos = random16(SEGLEN-1); uint16_t startPos = random16(SEGLEN-1);
CRGB color = col_to_crgb(color_wheel(random8()));
double multiplier = (float)(random8())/255.0 * 1.0; double multiplier = (float)(random8())/255.0 * 1.0;
stars[j].color = color; stars[j].color = col_to_crgb(color_wheel(random8()));
stars[j].pos = startPos; stars[j].pos = startPos;
stars[j].vel = MaxSpeed * (float)(random8())/255.0 * multiplier; stars[j].vel = maxSpeed * (float)(random8())/255.0 * multiplier;
stars[j].birth = it; stars[j].birth = it;
stars[j].last = it; stars[j].last = it;
// more fragments means larger burst effect // more fragments means larger burst effect
@ -2492,7 +2488,7 @@ uint16_t WS2812FX::mode_starburst(void) {
} }
} }
fill(BLACK); fill(SEGCOLOR(1));
for (int j=0; j<numStars; j++) for (int j=0; j<numStars; j++)
{ {
@ -2500,7 +2496,7 @@ uint16_t WS2812FX::mode_starburst(void) {
float dt = (it-stars[j].last)/1000.0; float dt = (it-stars[j].last)/1000.0;
for (int i=0; i < STARBURST_MAX_FRAG; i++) { for (int i=0; i < STARBURST_MAX_FRAG; i++) {
int var = i/2; int var = i >> 1;
if (stars[j].fragment[i] > 0) { if (stars[j].fragment[i] > 0) {
// spplit fragments half to each side with some travelling further // spplit fragments half to each side with some travelling further
@ -2513,7 +2509,6 @@ uint16_t WS2812FX::mode_starburst(void) {
} }
stars[j].last = it; stars[j].last = it;
stars[j].vel -= 3*stars[j].vel*dt; stars[j].vel -= 3*stars[j].vel*dt;
stars[j].color = stars[j].color.nscale8(235);
} }
CRGB c = stars[j].color; CRGB c = stars[j].color;
@ -2521,37 +2516,33 @@ uint16_t WS2812FX::mode_starburst(void) {
// If the star is brand new, it flashes white briefly. // If the star is brand new, it flashes white briefly.
// Otherwise it just fades over time. // Otherwise it just fades over time.
float fade = 0.0f; float fade = 0.0f;
float age = (it-stars[j].birth)/1000.0; float age = it-stars[j].birth;
if (age > ParticlePreignitonTime && age < ParticleIgnition + ParticlePreignitonTime) { if (age < ParticleIgnition) {
c = CRGB(ULTRAWHITE); c = col_to_crgb(color_blend(ULTRAWHITE, crgb_to_col(c), 254.5f*((age / ParticleIgnition))));
} else { } else {
// Figure out how much to fade and shrink the star based on // Figure out how much to fade and shrink the star based on
// its age relative to its lifetime // its age relative to its lifetime
if (age < ParticlePreignitonTime) { if (age > ParticleIgnition + ParticleFadeTime) {
fade = 1.0 - (age / ParticlePreignitonTime);
} else {
age -= ParticlePreignitonTime;
if (age < ParticleHoldTime + ParticleIgnition) {
fade = 0.0f; // Just born
} else if (age > ParticleHoldTime + ParticleIgnition + ParticleFadeTime) {
fade = 1.0f; // Black hole, all faded out fade = 1.0f; // Black hole, all faded out
stars[j].birth = 0; stars[j].birth = 0;
c = col_to_crgb(SEGCOLOR(1));
} else { } else {
age -= (ParticleHoldTime + ParticleIgnition); age -= ParticleIgnition;
fade = (age / ParticleFadeTime); // Fading star fade = (age / ParticleFadeTime); // Fading star
byte f = 254.5f*fade;
c = col_to_crgb(color_blend(crgb_to_col(c), SEGCOLOR(1), f));
} }
} }
c = c.nscale8(255-int(255.0*fade)/2); float ParticleSize = (1.0 - fade) * 2;
}
float ParticleSize = (1 - fade) * 4;
for (int i=0; i < STARBURST_MAX_FRAG; i++) { for (int i=0; i < STARBURST_MAX_FRAG; i++) {
if (stars[j].fragment[i] > 0) { if (stars[j].fragment[i] > 0) {
int start = int(stars[j].fragment[i]) - int(ParticleSize/2); int start = int(stars[j].fragment[i]) - int(ParticleSize);
if (start<0) start = 0; if (start < 0) start = 0;
int end = int(stars[j].fragment[i]) + int(ParticleSize/2); int end = int(stars[j].fragment[i]) + int(ParticleSize);
if (start == end) end++;
if (end > SEGLEN) end = SEGLEN; if (end > SEGLEN) end = SEGLEN;
for (int p = start; p < end; p++) { for (int p = start; p < end; p++) {
setPixelColor(SEGMENT.start+p, c.r, c.g, c.b); setPixelColor(SEGMENT.start+p, c.r, c.g, c.b);